From 5adb8a64539f5cf6a16142f26a0d817334bb0131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Dugovi=C4=8D?= Date: Tue, 13 Aug 2024 14:41:57 +0200 Subject: [PATCH] RHINENG-8141: resolve using getReportedAdvisories instead of getReportedAdvisoryNames --- evaluator/evaluate_advisories.go | 21 +++++++++++++++++---- evaluator/evaluate_advisories_test.go | 9 +++++---- evaluator/remediations.go | 16 ---------------- evaluator/remediations_test.go | 6 ------ 4 files changed, 22 insertions(+), 30 deletions(-) diff --git a/evaluator/evaluate_advisories.go b/evaluator/evaluate_advisories.go index 98100ad89..294368537 100644 --- a/evaluator/evaluate_advisories.go +++ b/evaluator/evaluate_advisories.go @@ -123,7 +123,7 @@ func evaluateChanges(vmaasData *vmaas.UpdatesV3Response, stored SystemAdvisoryMa // LazySaveAdvisories finds advisories reported by VMaaS and missing in the DB and lazy saves them. func lazySaveAdvisories(vmaasData *vmaas.UpdatesV3Response, inventoryID string) error { - reportedNames := getReportedAdvisoryNames(vmaasData) + reportedNames := getReportedAdvisories(vmaasData) if len(reportedNames) < 1 { return nil } @@ -191,14 +191,27 @@ func getAdvisoryMetadataIDs(names []string) (map[string]int64, error) { } // GetMissingAdvisories determines if advisories from DB are properly stored based on advisory metadata existence. -func getMissingAdvisories(advisoryNames []string) ([]string, error) { +func getMissingAdvisories(advisoriesMap map[string]int) ([]string, error) { + advisoryNames := make([]string, 0, len(advisoriesMap)) + present := make(map[string]bool, len(advisoriesMap)) + for name := range advisoriesMap { + if len(name) > 0 && !present[name] { + advisoryNames = append(advisoryNames, name) + present[name] = true + } + } + name2AdvisoryID, err := getAdvisoryMetadataIDs(advisoryNames) if err != nil { return nil, err } - missingNames := make([]string, 0, len(advisoryNames)-len(name2AdvisoryID)) - for _, name := range advisoryNames { + if len(advisoriesMap) == len(name2AdvisoryID) { + return []string{}, nil + } + + missingNames := make([]string, 0, len(advisoriesMap)-len(name2AdvisoryID)) + for name := range advisoriesMap { if _, found := name2AdvisoryID[name]; !found { missingNames = append(missingNames, name) } diff --git a/evaluator/evaluate_advisories_test.go b/evaluator/evaluate_advisories_test.go index 6e0f8f92c..6b24d6fcb 100644 --- a/evaluator/evaluate_advisories_test.go +++ b/evaluator/evaluate_advisories_test.go @@ -193,9 +193,10 @@ func TestGetMissingAdvisories(t *testing.T) { utils.SkipWithoutDB(t) core.SetupTestEnvironment() - advisories := []string{"ER-1", "RH-1", "ER-2", "RH-2"} - advisoryIDs := getAdvisoryIDsByNames(t, advisories) - missingNames, err := getMissingAdvisories(advisories) + advisoryNames := []string{"ER-1", "RH-1", "ER-2", "RH-2"} + advisoryMap := map[string]int{"ER-1": 0, "RH-1": 0, "ER-2": 0, "RH-2": 0} + advisoryIDs := getAdvisoryIDsByNames(t, advisoryNames) + missingNames, err := getMissingAdvisories(advisoryMap) assert.Nil(t, err) assert.Equal(t, 2, len(advisoryIDs)) assert.Equal(t, 2, len(missingNames)) @@ -207,7 +208,7 @@ func TestGetMissingAdvisoriesEmptyString(t *testing.T) { advisories := []string{""} advisoryIDs := getAdvisoryIDsByNames(t, advisories) - missingNames, err := getMissingAdvisories(advisories) + missingNames, err := getMissingAdvisories(map[string]int{"": 0}) assert.Nil(t, err) assert.Equal(t, 0, len(advisoryIDs)) assert.Equal(t, 1, len(missingNames)) diff --git a/evaluator/remediations.go b/evaluator/remediations.go index 37c333aad..a73f1d21a 100644 --- a/evaluator/remediations.go +++ b/evaluator/remediations.go @@ -55,22 +55,6 @@ func getReportedAdvisories(vmaasData *vmaas.UpdatesV3Response) map[string]int { return advisories } -func getReportedAdvisoryNames(vmaasData *vmaas.UpdatesV3Response) []string { - updateList := vmaasData.GetUpdateList() - reportedNames := make([]string, 0, len(updateList)) - present := make(map[string]bool, len(updateList)) - for _, updates := range updateList { - for _, update := range updates.GetAvailableUpdates() { - advisoryName := update.GetErratum() - if len(advisoryName) > 0 && !present[advisoryName] { - reportedNames = append(reportedNames, advisoryName) - present[advisoryName] = true - } - } - } - return reportedNames -} - func getReportedPackageUpdates(vmaasData *vmaas.UpdatesV3Response) map[string]bool { updateList := vmaasData.GetUpdateList() packages := make(map[string]bool, len(updateList)) diff --git a/evaluator/remediations_test.go b/evaluator/remediations_test.go index 85471561c..86e5803cc 100644 --- a/evaluator/remediations_test.go +++ b/evaluator/remediations_test.go @@ -44,9 +44,3 @@ func TestCreateRemediationsState(t *testing.T) { "patch:firefox-0:77.0.1-1.fc31.x86_64", "patch:firefox-1:76.0.1-1.fc31.x86_64", "patch:kernel-5.10.13-200.fc31.x86_64"}) } - -func TestGetReportedAdvisoryNames(t *testing.T) { - vmaasData := mockVMaaSResponse() - names := getReportedAdvisoryNames(&vmaasData) - assert.Equal(t, 4, len(names)) -}