From f87906b1d1325ce4190784b3ddee7d82039f17c0 Mon Sep 17 00:00:00 2001 From: Victor Lyuboslavsky Date: Thu, 16 May 2024 14:49:09 -0500 Subject: [PATCH] Fixing flaky software tests. (#19069) Attempt to fix the flaky software tests. Because the insert order of software is now not deterministic. --- server/datastore/mysql/hosts_test.go | 14 +++++++--- server/service/integration_core_test.go | 28 +++++++++++++------ server/service/integration_enterprise_test.go | 14 +++++++--- 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/server/datastore/mysql/hosts_test.go b/server/datastore/mysql/hosts_test.go index f6e5613b86..783fa156b7 100644 --- a/server/datastore/mysql/hosts_test.go +++ b/server/datastore/mysql/hosts_test.go @@ -8689,8 +8689,11 @@ func testHostHealth(t *testing.T, ds *Datastore) { require.NoError(t, ds.LoadHostSoftware(context.Background(), h, false)) soft1 := h.Software[0] - if soft1.Name != "bar" { - soft1 = h.Software[1] + for _, item := range h.Software { + if item.Name == "bar" { + soft1 = item + break + } } cpes := []fleet.SoftwareCPE{{SoftwareID: soft1.ID, CPE: "somecpe"}} @@ -8700,8 +8703,11 @@ func testHostHealth(t *testing.T, ds *Datastore) { // Reload software so that 'GeneratedCPEID is set. require.NoError(t, ds.LoadHostSoftware(context.Background(), h, false)) soft1 = h.Software[0] - if soft1.Name != "bar" { - soft1 = h.Software[1] + for _, item := range h.Software { + if item.Name == "bar" { + soft1 = item + break + } } inserted, err := ds.InsertSoftwareVulnerability( diff --git a/server/service/integration_core_test.go b/server/service/integration_core_test.go index 1035e06949..52e2ac25ce 100644 --- a/server/service/integration_core_test.go +++ b/server/service/integration_core_test.go @@ -725,8 +725,11 @@ func (s *integrationTestSuite) TestVulnerableSoftware() { require.NoError(t, s.ds.LoadHostSoftware(context.Background(), host, false)) soft1 := host.Software[0] - if soft1.Name != "bar" { - soft1 = host.Software[1] + for _, item := range host.Software { + if item.Name == "bar" { + soft1 = item + break + } } cpes := []fleet.SoftwareCPE{{SoftwareID: soft1.ID, CPE: "somecpe"}} @@ -736,8 +739,11 @@ func (s *integrationTestSuite) TestVulnerableSoftware() { // Reload software so that 'GeneratedCPEID is set. require.NoError(t, s.ds.LoadHostSoftware(context.Background(), host, false)) soft1 = host.Software[0] - if soft1.Name != "bar" { - soft1 = host.Software[1] + for _, item := range host.Software { + if item.Name == "bar" { + soft1 = item + break + } } inserted, err := s.ds.InsertSoftwareVulnerability( @@ -10862,8 +10868,11 @@ func (s *integrationTestSuite) TestHostHealth() { require.NoError(t, s.ds.LoadHostSoftware(context.Background(), host, false)) soft1 := host.Software[0] - if soft1.Name != "bar" { - soft1 = host.Software[1] + for _, item := range host.Software { + if item.Name == "bar" { + soft1 = item + break + } } cpes := []fleet.SoftwareCPE{{SoftwareID: soft1.ID, CPE: "somecpe"}} @@ -10873,8 +10882,11 @@ func (s *integrationTestSuite) TestHostHealth() { // Reload software so that 'GeneratedCPEID is set. require.NoError(t, s.ds.LoadHostSoftware(context.Background(), host, false)) soft1 = host.Software[0] - if soft1.Name != "bar" { - soft1 = host.Software[1] + for _, item := range host.Software { + if item.Name == "bar" { + soft1 = item + break + } } inserted, err := s.ds.InsertSoftwareVulnerability( diff --git a/server/service/integration_enterprise_test.go b/server/service/integration_enterprise_test.go index f8e7f925e4..096b4178b8 100644 --- a/server/service/integration_enterprise_test.go +++ b/server/service/integration_enterprise_test.go @@ -7054,8 +7054,11 @@ func (s *integrationEnterpriseTestSuite) TestAllSoftwareTitles() { require.NoError(t, s.ds.LoadHostSoftware(context.Background(), host, false)) soft1 := host.Software[0] - if soft1.Name != "bar" { - soft1 = host.Software[1] + for _, item := range host.Software { + if item.Name == "bar" { + soft1 = item + break + } } cpes := []fleet.SoftwareCPE{{SoftwareID: soft1.ID, CPE: "somecpe"}} @@ -7065,8 +7068,11 @@ func (s *integrationEnterpriseTestSuite) TestAllSoftwareTitles() { // Reload software so that 'GeneratedCPEID is set. require.NoError(t, s.ds.LoadHostSoftware(context.Background(), host, false)) soft1 = host.Software[0] - if soft1.Name != "bar" { - soft1 = host.Software[1] + for _, item := range host.Software { + if item.Name == "bar" { + soft1 = item + break + } } inserted, err := s.ds.InsertSoftwareVulnerability(