From ec278a9fde6be45fc609ed098cfac93b06a83462 Mon Sep 17 00:00:00 2001 From: Victor Lyuboslavsky Date: Mon, 20 May 2024 17:12:07 -0500 Subject: [PATCH] Fixing flaky test. Software insertion is no longer deterministic. (#19150) Fixing flaky test. Software insertion is no longer deterministic. --- server/service/integration_core_test.go | 29 +++++++++++++++++-- server/service/integration_enterprise_test.go | 6 ++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/server/service/integration_core_test.go b/server/service/integration_core_test.go index fae0a8cd98..d5f60a7e93 100644 --- a/server/service/integration_core_test.go +++ b/server/service/integration_core_test.go @@ -6610,6 +6610,17 @@ func (s *integrationTestSuite) TestListSoftwareAndSoftwareDetails() { sws[i] = sw } + sortByNameAlphanumeric := func(sw []fleet.Software, a, b int) bool { + aNum, _ := strconv.Atoi(strings.TrimPrefix(sw[a].Name, "sw")) + bNum, _ := strconv.Atoi(strings.TrimPrefix(sw[b].Name, "sw")) + return aNum < bNum + } + sortEntryByNameAlphanumeric := func(sw []fleet.HostSoftwareEntry, a, b int) bool { + aNum, _ := strconv.Atoi(strings.TrimPrefix(sw[a].Name, "sw")) + bNum, _ := strconv.Atoi(strings.TrimPrefix(sw[b].Name, "sw")) + return aNum < bNum + } + // mark them as installed on the hosts, with host at index 0 having all 20, // at index 1 having 19, index 2 = 18, etc. until index 19 = 1. So software // sws[0] is only used by 1 host, while sws[19] is used by all. @@ -6624,6 +6635,12 @@ func (s *integrationTestSuite) TestListSoftwareAndSoftwareDetails() { for _, s := range h.Software { sws = append(sws, s.Software) } + // Sort software by Name (alphanumeric) + sort.Slice( + sws, func(a, b int) bool { + return sortByNameAlphanumeric(sws, a, b) + }, + ) } } @@ -6639,6 +6656,12 @@ func (s *integrationTestSuite) TestListSoftwareAndSoftwareDetails() { require.NoError(t, s.ds.LoadHostSoftware(context.Background(), hosts[0], false)) // add CVEs for the first 10 software, which are the least used (lower hosts_count) + // Sort software by Name (alphanumeric) + sort.Slice( + hosts[0].Software, func(a, b int) bool { + return sortEntryByNameAlphanumeric(hosts[0].Software, a, b) + }, + ) testCvePrefix := "cve-123-123" for i, sw := range hosts[0].Software[:10] { inserted, err := s.ds.InsertSoftwareVulnerability(context.Background(), fleet.SoftwareVulnerability{ @@ -6685,9 +6708,7 @@ func (s *integrationTestSuite) TestListSoftwareAndSoftwareDetails() { require.Len(t, resp.Software, len(want)) for i := range resp.Software { wantID, gotID := want[i].ID, resp.Software[i].ID - assert.Equal(t, wantID, gotID) - wantCount, gotCount := counts[i], resp.Software[i].HostsCount - assert.Equal(t, wantCount, gotCount) + assert.Equal(t, wantID, gotID, "want.Name: %s got.Name: %s", want[i].Name, resp.Software[i].Name) wantName, gotName := want[i].Name, resp.Software[i].Name assert.Equal(t, wantName, gotName) wantVersion, gotVersion := want[i].Version, resp.Software[i].Version @@ -6696,6 +6717,8 @@ func (s *integrationTestSuite) TestListSoftwareAndSoftwareDetails() { assert.Equal(t, wantSource, gotSource) wantBrowser, gotBrowser := want[i].Browser, resp.Software[i].Browser assert.Equal(t, wantBrowser, gotBrowser) + wantCount, gotCount := counts[i], resp.Software[i].HostsCount + assert.Equal(t, wantCount, gotCount) } if ts.IsZero() { assert.Nil(t, resp.CountsUpdatedAt) diff --git a/server/service/integration_enterprise_test.go b/server/service/integration_enterprise_test.go index e5a7aca8b7..d363b40dd6 100644 --- a/server/service/integration_enterprise_test.go +++ b/server/service/integration_enterprise_test.go @@ -6994,6 +6994,12 @@ func (s *integrationEnterpriseTestSuite) TestAllSoftwareTitles() { require.NotZero(t, got[i].Versions[j].ID) got[i].Versions[j].ID = 0 } + // Sort versions by version + sort.Slice( + got[i].Versions, func(a, b int) bool { + return got[i].Versions[a].Version < got[i].Versions[b].Version + }, + ) } // sort and use EqualValues instead of ElementsMatch in order