2017-01-13 07:08:52 +00:00
|
|
|
// Copyright 2017 clair authors
|
2016-05-17 21:22:18 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-01-26 22:57:32 +00:00
|
|
|
package pgsql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-05-02 22:33:03 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2016-01-26 22:57:32 +00:00
|
|
|
"github.com/coreos/clair/database"
|
2016-12-28 01:45:11 +00:00
|
|
|
"github.com/coreos/clair/ext/versionfmt"
|
2017-01-03 21:00:20 +00:00
|
|
|
"github.com/coreos/clair/ext/versionfmt/dpkg"
|
2017-01-13 07:08:52 +00:00
|
|
|
"github.com/coreos/clair/pkg/commonerr"
|
2016-01-26 22:57:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNotification(t *testing.T) {
|
2016-05-02 22:33:03 +00:00
|
|
|
datastore, err := openDatabaseForTest("Notification", false)
|
2016-01-26 22:57:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer datastore.Close()
|
|
|
|
|
|
|
|
// Try to get a notification when there is none.
|
|
|
|
_, err = datastore.GetAvailableNotification(time.Second)
|
2017-01-13 07:08:52 +00:00
|
|
|
assert.Equal(t, commonerr.ErrNotFound, err)
|
2016-01-26 22:57:32 +00:00
|
|
|
|
|
|
|
// Create some data.
|
|
|
|
f1 := database.Feature{
|
2016-12-28 01:45:11 +00:00
|
|
|
Name: "TestNotificationFeature1",
|
|
|
|
Namespace: database.Namespace{
|
|
|
|
Name: "TestNotificationNamespace1",
|
2017-01-03 21:00:20 +00:00
|
|
|
VersionFormat: dpkg.ParserName,
|
2016-12-28 01:45:11 +00:00
|
|
|
},
|
2016-01-26 22:57:32 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 20:12:41 +00:00
|
|
|
f2 := database.Feature{
|
2016-12-28 01:45:11 +00:00
|
|
|
Name: "TestNotificationFeature2",
|
|
|
|
Namespace: database.Namespace{
|
|
|
|
Name: "TestNotificationNamespace1",
|
2017-01-03 21:00:20 +00:00
|
|
|
VersionFormat: dpkg.ParserName,
|
2016-12-28 01:45:11 +00:00
|
|
|
},
|
2016-02-04 20:12:41 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 22:57:32 +00:00
|
|
|
l1 := database.Layer{
|
|
|
|
Name: "TestNotificationLayer1",
|
|
|
|
Features: []database.FeatureVersion{
|
2016-02-25 00:29:36 +00:00
|
|
|
{
|
2016-01-26 22:57:32 +00:00
|
|
|
Feature: f1,
|
2016-12-28 01:45:11 +00:00
|
|
|
Version: "0.1",
|
2016-01-26 22:57:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
l2 := database.Layer{
|
|
|
|
Name: "TestNotificationLayer2",
|
|
|
|
Features: []database.FeatureVersion{
|
2016-02-25 00:29:36 +00:00
|
|
|
{
|
2016-01-26 22:57:32 +00:00
|
|
|
Feature: f1,
|
2016-12-28 01:45:11 +00:00
|
|
|
Version: "0.2",
|
2016-01-26 22:57:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
l3 := database.Layer{
|
|
|
|
Name: "TestNotificationLayer3",
|
|
|
|
Features: []database.FeatureVersion{
|
2016-02-25 00:29:36 +00:00
|
|
|
{
|
2016-01-26 22:57:32 +00:00
|
|
|
Feature: f1,
|
2016-12-28 01:45:11 +00:00
|
|
|
Version: "0.3",
|
2016-01-26 22:57:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-02-04 20:12:41 +00:00
|
|
|
l4 := database.Layer{
|
|
|
|
Name: "TestNotificationLayer4",
|
|
|
|
Features: []database.FeatureVersion{
|
2016-02-25 00:29:36 +00:00
|
|
|
{
|
2016-02-04 20:12:41 +00:00
|
|
|
Feature: f2,
|
2016-12-28 01:45:11 +00:00
|
|
|
Version: "0.1",
|
2016-02-04 20:12:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !assert.Nil(t, datastore.InsertLayer(l1)) ||
|
|
|
|
!assert.Nil(t, datastore.InsertLayer(l2)) ||
|
|
|
|
!assert.Nil(t, datastore.InsertLayer(l3)) ||
|
|
|
|
!assert.Nil(t, datastore.InsertLayer(l4)) {
|
2016-02-04 16:34:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert a new vulnerability that is introduced by three layers.
|
|
|
|
v1 := database.Vulnerability{
|
|
|
|
Name: "TestNotificationVulnerability1",
|
|
|
|
Namespace: f1.Namespace,
|
|
|
|
Description: "TestNotificationDescription1",
|
|
|
|
Link: "TestNotificationLink1",
|
|
|
|
Severity: "Unknown",
|
|
|
|
FixedIn: []database.FeatureVersion{
|
2016-02-25 00:29:36 +00:00
|
|
|
{
|
2016-02-04 16:34:01 +00:00
|
|
|
Feature: f1,
|
2016-12-28 01:45:11 +00:00
|
|
|
Version: "1.0",
|
2016-01-26 22:57:32 +00:00
|
|
|
},
|
2016-02-04 16:34:01 +00:00
|
|
|
},
|
|
|
|
}
|
2016-02-14 09:25:07 +00:00
|
|
|
assert.Nil(t, datastore.insertVulnerability(v1, false, true))
|
2016-01-26 22:57:32 +00:00
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
// Get the notification associated to the previously inserted vulnerability.
|
|
|
|
notification, err := datastore.GetAvailableNotification(time.Second)
|
2016-01-26 22:57:32 +00:00
|
|
|
|
2016-02-04 20:12:41 +00:00
|
|
|
if assert.Nil(t, err) && assert.NotEmpty(t, notification.Name) {
|
|
|
|
// Verify the renotify behaviour.
|
|
|
|
if assert.Nil(t, datastore.SetNotificationNotified(notification.Name)) {
|
|
|
|
_, err := datastore.GetAvailableNotification(time.Second)
|
2017-01-13 07:08:52 +00:00
|
|
|
assert.Equal(t, commonerr.ErrNotFound, err)
|
2016-02-04 20:12:41 +00:00
|
|
|
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
notificationB, err := datastore.GetAvailableNotification(20 * time.Millisecond)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, notification.Name, notificationB.Name)
|
|
|
|
|
|
|
|
datastore.SetNotificationNotified(notification.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get notification.
|
|
|
|
filledNotification, nextPage, err := datastore.GetNotification(notification.Name, 2, database.VulnerabilityNotificationFirstPage)
|
|
|
|
if assert.Nil(t, err) {
|
|
|
|
assert.NotEqual(t, database.NoVulnerabilityNotificationPage, nextPage)
|
|
|
|
assert.Nil(t, filledNotification.OldVulnerability)
|
|
|
|
|
|
|
|
if assert.NotNil(t, filledNotification.NewVulnerability) {
|
|
|
|
assert.Equal(t, v1.Name, filledNotification.NewVulnerability.Name)
|
|
|
|
assert.Len(t, filledNotification.NewVulnerability.LayersIntroducingVulnerability, 2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get second page.
|
|
|
|
filledNotification, nextPage, err = datastore.GetNotification(notification.Name, 2, nextPage)
|
|
|
|
if assert.Nil(t, err) {
|
|
|
|
assert.Equal(t, database.NoVulnerabilityNotificationPage, nextPage)
|
|
|
|
assert.Nil(t, filledNotification.OldVulnerability)
|
|
|
|
|
|
|
|
if assert.NotNil(t, filledNotification.NewVulnerability) {
|
|
|
|
assert.Equal(t, v1.Name, filledNotification.NewVulnerability.Name)
|
|
|
|
assert.Len(t, filledNotification.NewVulnerability.LayersIntroducingVulnerability, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete notification.
|
|
|
|
assert.Nil(t, datastore.DeleteNotification(notification.Name))
|
|
|
|
|
|
|
|
_, err = datastore.GetAvailableNotification(time.Millisecond)
|
2017-01-13 07:08:52 +00:00
|
|
|
assert.Equal(t, commonerr.ErrNotFound, err)
|
2016-02-04 16:34:01 +00:00
|
|
|
}
|
2016-01-26 22:57:32 +00:00
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
// Update a vulnerability and ensure that the old/new vulnerabilities are correct.
|
2016-02-04 20:12:41 +00:00
|
|
|
v1b := v1
|
2017-01-19 18:42:37 +00:00
|
|
|
v1b.Severity = database.HighSeverity
|
2016-02-04 20:12:41 +00:00
|
|
|
v1b.FixedIn = []database.FeatureVersion{
|
2016-02-25 00:29:36 +00:00
|
|
|
{
|
2016-02-04 20:12:41 +00:00
|
|
|
Feature: f1,
|
2016-12-28 01:45:11 +00:00
|
|
|
Version: versionfmt.MinVersion,
|
2016-02-04 20:12:41 +00:00
|
|
|
},
|
2016-02-25 00:29:36 +00:00
|
|
|
{
|
2016-02-04 20:12:41 +00:00
|
|
|
Feature: f2,
|
2016-12-28 01:45:11 +00:00
|
|
|
Version: versionfmt.MaxVersion,
|
2016-02-04 20:12:41 +00:00
|
|
|
},
|
|
|
|
}
|
2016-01-26 22:57:32 +00:00
|
|
|
|
2016-02-14 09:25:07 +00:00
|
|
|
if assert.Nil(t, datastore.insertVulnerability(v1b, false, true)) {
|
2016-02-04 20:12:41 +00:00
|
|
|
notification, err = datastore.GetAvailableNotification(time.Second)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotEmpty(t, notification.Name)
|
|
|
|
|
|
|
|
if assert.Nil(t, err) && assert.NotEmpty(t, notification.Name) {
|
|
|
|
filledNotification, nextPage, err := datastore.GetNotification(notification.Name, 2, database.VulnerabilityNotificationFirstPage)
|
|
|
|
if assert.Nil(t, err) {
|
|
|
|
if assert.NotNil(t, filledNotification.OldVulnerability) {
|
|
|
|
assert.Equal(t, v1.Name, filledNotification.OldVulnerability.Name)
|
|
|
|
assert.Equal(t, v1.Severity, filledNotification.OldVulnerability.Severity)
|
|
|
|
assert.Len(t, filledNotification.OldVulnerability.LayersIntroducingVulnerability, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
if assert.NotNil(t, filledNotification.NewVulnerability) {
|
|
|
|
assert.Equal(t, v1b.Name, filledNotification.NewVulnerability.Name)
|
|
|
|
assert.Equal(t, v1b.Severity, filledNotification.NewVulnerability.Severity)
|
|
|
|
assert.Len(t, filledNotification.NewVulnerability.LayersIntroducingVulnerability, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, -1, nextPage.NewVulnerability)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Nil(t, datastore.DeleteNotification(notification.Name))
|
|
|
|
}
|
|
|
|
}
|
2016-01-26 22:57:32 +00:00
|
|
|
|
2016-02-04 20:12:41 +00:00
|
|
|
// Delete a vulnerability and verify the notification.
|
|
|
|
if assert.Nil(t, datastore.DeleteVulnerability(v1b.Namespace.Name, v1b.Name)) {
|
|
|
|
notification, err = datastore.GetAvailableNotification(time.Second)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotEmpty(t, notification.Name)
|
|
|
|
|
|
|
|
if assert.Nil(t, err) && assert.NotEmpty(t, notification.Name) {
|
|
|
|
filledNotification, _, err := datastore.GetNotification(notification.Name, 2, database.VulnerabilityNotificationFirstPage)
|
|
|
|
if assert.Nil(t, err) {
|
|
|
|
assert.Nil(t, filledNotification.NewVulnerability)
|
|
|
|
|
|
|
|
if assert.NotNil(t, filledNotification.OldVulnerability) {
|
|
|
|
assert.Equal(t, v1b.Name, filledNotification.OldVulnerability.Name)
|
|
|
|
assert.Equal(t, v1b.Severity, filledNotification.OldVulnerability.Severity)
|
|
|
|
assert.Len(t, filledNotification.OldVulnerability.LayersIntroducingVulnerability, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Nil(t, datastore.DeleteNotification(notification.Name))
|
|
|
|
}
|
|
|
|
}
|
2016-01-26 22:57:32 +00:00
|
|
|
}
|