From 9d0ea4480570c910aa430fe554f323e1f83e37d0 Mon Sep 17 00:00:00 2001 From: Mariana Date: Fri, 5 Apr 2019 12:16:04 +0200 Subject: [PATCH 1/2] Fix flaky service_integration_windows_test Some services can change state before reader.Read() call is executed which will result in a test faillure. Fix test by introducing a confidence factor and enriching the error message with the state-changed service details. Fixes #8880 and #7977 --- CHANGELOG.next.asciidoc | 1 + .../service_integration_windows_test.go | 23 ++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index a3fba8f8ed10..1362835d4de6 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -58,6 +58,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d - Fix template always being overwritten. {pull}11671[11671] - Fix matching of string arrays in contains condition. {pull}11691[11691] - Fix initialization of the TCP input logger. {pull}11605[11605] +- Fix flaky service_integration_windows_test test by introducing a confidence factor and enriching the error message with more service details. {issue}8880[8880] and {issue}7977[7977] *Auditbeat* diff --git a/metricbeat/module/windows/service/service_integration_windows_test.go b/metricbeat/module/windows/service/service_integration_windows_test.go index 1c7a56375baa..cdc880e6ef97 100644 --- a/metricbeat/module/windows/service/service_integration_windows_test.go +++ b/metricbeat/module/windows/service/service_integration_windows_test.go @@ -22,6 +22,8 @@ package service import ( "testing" + "github.com/elastic/beats/libbeat/common" + "github.com/StackExchange/wmi" "github.com/stretchr/testify/assert" @@ -68,6 +70,8 @@ func TestReadService(t *testing.T) { t.Fatal(err) } + var stateChangedServices []common.MapStr + // Compare our module's data against WMI. for _, s := range services { // Look if the service is in the WMI data. @@ -78,14 +82,17 @@ func TestReadService(t *testing.T) { assert.Equal(t, w.ProcessId, s["pid"], "PID of service %v does not match", w.DisplayName) } - assert.Equal(t, w.State, s["state"], - "State of service %v does not match", w.DisplayName) - // For some services DisplayName and Name are the same. It seems to be a bug from the wmi query. if w.DisplayName != w.Name { assert.Equal(t, w.DisplayName, s["display_name"], "Display name of service %v does not match", w.Name) } + //Some services have changed state before the second retrieval + if w.State != s["state"] { + changed := s + changed["initial_state"] = w.State + stateChangedServices = append(stateChangedServices, changed) + } found = true break } @@ -96,4 +103,14 @@ func TestReadService(t *testing.T) { t.Errorf("Service %s can not be found by wmi query", s["name"]) } } + // If more than 90% of the services have the same state then we have enough confidence the state check works while being resilient to race conditions, + // else it will require further investigation on which services are failing + if stateChangedServices != nil { + failing := float64(len(stateChangedServices)) / float64(len(services)) * 100 + if failing > 90 { + // print entire information on the services failing + t.Errorf("%.2f%% of the services have a different state than initial one \n : %s", failing, stateChangedServices) + } + } + } From 2abafae75832178bc5c6c322cdf489886bffa4ed Mon Sep 17 00:00:00 2001 From: Andrew Kroh Date: Fri, 5 Apr 2019 16:27:05 +0200 Subject: [PATCH 2/2] Update metricbeat/module/windows/service/service_integration_windows_test.go Co-Authored-By: narph --- .../module/windows/service/service_integration_windows_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metricbeat/module/windows/service/service_integration_windows_test.go b/metricbeat/module/windows/service/service_integration_windows_test.go index cdc880e6ef97..6bba4bab0a94 100644 --- a/metricbeat/module/windows/service/service_integration_windows_test.go +++ b/metricbeat/module/windows/service/service_integration_windows_test.go @@ -87,7 +87,7 @@ func TestReadService(t *testing.T) { assert.Equal(t, w.DisplayName, s["display_name"], "Display name of service %v does not match", w.Name) } - //Some services have changed state before the second retrieval + // Some services have changed state before the second retrieval. if w.State != s["state"] { changed := s changed["initial_state"] = w.State