2016-01-26 22:57:32 +00:00
|
|
|
package pgsql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/coreos/clair/database"
|
|
|
|
cerrors "github.com/coreos/clair/utils/errors"
|
|
|
|
"github.com/coreos/clair/utils/types"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNotification(t *testing.T) {
|
|
|
|
datastore, err := OpenForTest("Notification", false)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer datastore.Close()
|
|
|
|
|
|
|
|
// Try to get a notification when there is none.
|
|
|
|
_, err = datastore.GetAvailableNotification(time.Second)
|
|
|
|
assert.Equal(t, cerrors.ErrNotFound, err)
|
|
|
|
|
|
|
|
// Create some data.
|
|
|
|
f1 := database.Feature{
|
|
|
|
Name: "TestNotificationFeature1",
|
|
|
|
Namespace: database.Namespace{Name: "TestNotificationNamespace1"},
|
|
|
|
}
|
|
|
|
|
2016-02-04 20:12:41 +00:00
|
|
|
f2 := database.Feature{
|
|
|
|
Name: "TestNotificationFeature2",
|
|
|
|
Namespace: database.Namespace{Name: "TestNotificationNamespace1"},
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
Version: types.NewVersionUnsafe("0.1"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
Version: types.NewVersionUnsafe("0.2"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
Version: types.NewVersionUnsafe("0.3"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
Version: types.NewVersionUnsafe("0.1"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
Version: types.NewVersionUnsafe("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)
|
|
|
|
assert.Equal(t, cerrors.ErrNotFound, err)
|
|
|
|
|
|
|
|
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)
|
2016-02-04 16:34:01 +00:00
|
|
|
assert.Equal(t, cerrors.ErrNotFound, err)
|
|
|
|
}
|
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
|
|
|
|
v1b.Severity = types.High
|
|
|
|
v1b.FixedIn = []database.FeatureVersion{
|
2016-02-25 00:29:36 +00:00
|
|
|
{
|
2016-02-04 20:12:41 +00:00
|
|
|
Feature: f1,
|
|
|
|
Version: types.MinVersion,
|
|
|
|
},
|
2016-02-25 00:29:36 +00:00
|
|
|
{
|
2016-02-04 20:12:41 +00:00
|
|
|
Feature: f2,
|
|
|
|
Version: types.MaxVersion,
|
|
|
|
},
|
|
|
|
}
|
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
|
|
|
}
|