Skip to content

Commit

Permalink
RHINENG-8141: resolve using getReportedAdvisories instead of getRepor…
Browse files Browse the repository at this point in the history
…tedAdvisoryNames
  • Loading branch information
Dugowitch committed Aug 13, 2024
1 parent de64f76 commit d90c430
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 30 deletions.
21 changes: 17 additions & 4 deletions evaluator/evaluate_advisories.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 5 additions & 4 deletions evaluator/evaluate_advisories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down
16 changes: 0 additions & 16 deletions evaluator/remediations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 0 additions & 6 deletions evaluator/remediations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit d90c430

Please sign in to comment.