diff --git a/database/pgsql/notification_test.go b/database/pgsql/notification_test.go index 05edb41d..73e59175 100644 --- a/database/pgsql/notification_test.go +++ b/database/pgsql/notification_test.go @@ -28,6 +28,11 @@ func TestNotification(t *testing.T) { Namespace: database.Namespace{Name: "TestNotificationNamespace1"}, } + f2 := database.Feature{ + Name: "TestNotificationFeature2", + Namespace: database.Namespace{Name: "TestNotificationNamespace1"}, + } + l1 := database.Layer{ Name: "TestNotificationLayer1", Features: []database.FeatureVersion{ @@ -58,7 +63,20 @@ func TestNotification(t *testing.T) { }, } - if !assert.Nil(t, datastore.InsertLayer(l1)) || !assert.Nil(t, datastore.InsertLayer(l2)) || !assert.Nil(t, datastore.InsertLayer(l3)) { + l4 := database.Layer{ + Name: "TestNotificationLayer4", + Features: []database.FeatureVersion{ + database.FeatureVersion{ + 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)) { return } @@ -80,46 +98,112 @@ func TestNotification(t *testing.T) { // Get the notification associated to the previously inserted vulnerability. notification, err := datastore.GetAvailableNotification(time.Second) - 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) + 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) 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) - assert.Nil(t, err) - assert.NotEqual(t, database.NoVulnerabilityNotificationPage, nextPage) - assert.Nil(t, filledNotification.OldVulnerability) - 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) - assert.Nil(t, err) - assert.Equal(t, database.NoVulnerabilityNotificationPage, nextPage) - assert.Nil(t, filledNotification.OldVulnerability) - 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) - assert.Equal(t, cerrors.ErrNotFound, err) - // Update a vulnerability and ensure that the old/new vulnerabilities are correct. + v1b := v1 + v1b.Severity = types.High + v1b.FixedIn = []database.FeatureVersion{ + database.FeatureVersion{ + Feature: f1, + Version: types.MinVersion, + }, + database.FeatureVersion{ + Feature: f2, + Version: types.MaxVersion, + }, + } + + if assert.Nil(t, datastore.insertVulnerability(v1b, false)) { + 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)) + } + } // 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)) + } + } }