From 8fcf5075e487b79fbaced2ca84803d770ba4b236 Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Tue, 27 Feb 2024 06:35:51 -0800 Subject: [PATCH] [chore] fix process scraper test to not run on Darwin and Windows for cgroups (#31394) **Description:** Fix tests on Mac for process scraper. --- .../processscraper/process_scraper_test.go | 52 +++++++++++-------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_test.go b/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_test.go index 0873f284aa83..546608ac9f91 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_test.go +++ b/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_test.go @@ -681,7 +681,7 @@ func TestScrapeMetrics_ProcessErrors(t *testing.T) { type testCase struct { name string - osFilter string + osFilter []string nameError error exeError error cgroupError error @@ -704,13 +704,13 @@ func TestScrapeMetrics_ProcessErrors(t *testing.T) { testCases := []testCase{ { name: "Name Error", - osFilter: "windows", + osFilter: []string{"windows"}, nameError: errors.New("err1"), expectedError: `error reading process name for pid 1: err1`, }, { name: "Exe Error", - osFilter: "darwin", + osFilter: []string{"darwin"}, exeError: errors.New("err1"), expectedError: func() string { if runtime.GOOS == "windows" { @@ -722,15 +722,15 @@ func TestScrapeMetrics_ProcessErrors(t *testing.T) { }, { name: "Cgroup Error", - osFilter: "linux", + osFilter: []string{"darwin", "windows"}, cgroupError: errors.New("err1"), expectedError: func() string { - return `error reading cgroup for pid 1: err1` + return `error reading process cgroup for pid 1: err1` }(), }, { name: "Cmdline Error", - osFilter: "darwin", + osFilter: []string{"darwin"}, cmdlineError: errors.New("err2"), expectedError: `error reading command for process "test" (pid 1): err2`, }, @@ -756,55 +756,55 @@ func TestScrapeMetrics_ProcessErrors(t *testing.T) { }, { name: "Memory Percent Error", - osFilter: "darwin", + osFilter: []string{"darwin"}, memoryPercentError: errors.New("err-mem-percent"), expectedError: `error reading memory utilization for process "test" (pid 1): err-mem-percent`, }, { name: "IO Counters Error", - osFilter: "darwin", + osFilter: []string{"darwin"}, ioCountersError: errors.New("err7"), expectedError: `error reading disk usage for process "test" (pid 1): err7`, }, { name: "Parent PID Error", - osFilter: "darwin", + osFilter: []string{"darwin"}, parentPidError: errors.New("err8"), expectedError: `error reading parent pid for process "test" (pid 1): err8`, }, { name: "Page Faults Error", - osFilter: "darwin", + osFilter: []string{"darwin"}, pageFaultsError: errors.New("err-paging"), expectedError: `error reading memory paging info for process "test" (pid 1): err-paging`, }, { name: "Thread count Error", - osFilter: "darwin", + osFilter: []string{"darwin"}, numThreadsError: errors.New("err8"), expectedError: `error reading thread info for process "test" (pid 1): err8`, }, { name: "Context Switches Error", - osFilter: "darwin", + osFilter: []string{"darwin"}, numCtxSwitchesError: errors.New("err9"), expectedError: `error reading context switch counts for process "test" (pid 1): err9`, }, { name: "File Descriptors Error", - osFilter: "darwin", + osFilter: []string{"darwin"}, numFDsError: errors.New("err10"), expectedError: `error reading open file descriptor count for process "test" (pid 1): err10`, }, { name: "Signals Pending Error", - osFilter: "darwin", + osFilter: []string{"darwin"}, rlimitError: errors.New("err-rlimit"), expectedError: `error reading pending signals for process "test" (pid 1): err-rlimit`, }, { name: "Multiple Errors", - osFilter: "darwin", + osFilter: []string{"darwin"}, cmdlineError: errors.New("err2"), usernameError: errors.New("err3"), createTimeError: errors.New("err4"), @@ -843,8 +843,10 @@ func TestScrapeMetrics_ProcessErrors(t *testing.T) { for _, test := range testCases { t.Run(test.name, func(t *testing.T) { - if test.osFilter == runtime.GOOS { - t.Skipf("skipping test %v on %v", test.name, runtime.GOOS) + for _, os := range test.osFilter { + if os == runtime.GOOS { + t.Skipf("skipping test %v on %v", test.name, runtime.GOOS) + } } metricsBuilderConfig := metadata.DefaultMetricsBuilderConfig() @@ -902,7 +904,7 @@ func TestScrapeMetrics_ProcessErrors(t *testing.T) { executableError = test.cmdlineError } - expectedResourceMetricsLen, expectedMetricsLen := getExpectedLengthOfReturnedMetrics(test.nameError, executableError, test.timesError, test.memoryInfoError, test.memoryPercentError, test.ioCountersError, test.pageFaultsError, test.numThreadsError, test.numCtxSwitchesError, test.numFDsError, test.rlimitError) + expectedResourceMetricsLen, expectedMetricsLen := getExpectedLengthOfReturnedMetrics(test.nameError, executableError, test.timesError, test.memoryInfoError, test.memoryPercentError, test.ioCountersError, test.pageFaultsError, test.numThreadsError, test.numCtxSwitchesError, test.numFDsError, test.rlimitError, test.cgroupError) assert.Equal(t, expectedResourceMetricsLen, md.ResourceMetrics().Len()) assert.Equal(t, expectedMetricsLen, md.MetricCount()) @@ -910,7 +912,7 @@ func TestScrapeMetrics_ProcessErrors(t *testing.T) { isPartial := scrapererror.IsPartialScrapeError(err) assert.True(t, isPartial) if isPartial { - expectedFailures := getExpectedScrapeFailures(test.nameError, executableError, test.timesError, test.memoryInfoError, test.memoryPercentError, test.ioCountersError, test.pageFaultsError, test.numThreadsError, test.numCtxSwitchesError, test.numFDsError, test.rlimitError) + expectedFailures := getExpectedScrapeFailures(test.nameError, executableError, test.timesError, test.memoryInfoError, test.memoryPercentError, test.ioCountersError, test.pageFaultsError, test.numThreadsError, test.numCtxSwitchesError, test.numFDsError, test.rlimitError, test.cgroupError) var scraperErr scrapererror.PartialScrapeError require.ErrorAs(t, err, &scraperErr) assert.Equal(t, expectedFailures, scraperErr.Failed) @@ -919,10 +921,13 @@ func TestScrapeMetrics_ProcessErrors(t *testing.T) { } } -func getExpectedLengthOfReturnedMetrics(nameError, exeError, timeError, memError, memPercentError, diskError, pageFaultsError, threadError, contextSwitchError, fileDescriptorError, rlimitError error) (int, int) { +func getExpectedLengthOfReturnedMetrics(nameError, exeError, timeError, memError, memPercentError, diskError, pageFaultsError, threadError, contextSwitchError, fileDescriptorError, rlimitError, cgroupError error) (int, int) { if runtime.GOOS == "windows" && exeError != nil { return 0, 0 } + if runtime.GOOS == "linux" && cgroupError != nil { + return 0, 0 + } if nameError != nil { return 0, 0 @@ -963,15 +968,18 @@ func getExpectedLengthOfReturnedMetrics(nameError, exeError, timeError, memError return 1, expectedLen } -func getExpectedScrapeFailures(nameError, exeError, timeError, memError, memPercentError, diskError, pageFaultsError, threadError, contextSwitchError, fileDescriptorError error, rlimitError error) int { +func getExpectedScrapeFailures(nameError, exeError, timeError, memError, memPercentError, diskError, pageFaultsError, threadError, contextSwitchError, fileDescriptorError error, rlimitError error, cgroupError error) int { if runtime.GOOS == "windows" && exeError != nil { return 2 } + if runtime.GOOS == "linux" && cgroupError != nil { + return 1 + } if nameError != nil || exeError != nil { return 1 } - _, expectedMetricsLen := getExpectedLengthOfReturnedMetrics(nameError, exeError, timeError, memError, memPercentError, diskError, pageFaultsError, threadError, contextSwitchError, fileDescriptorError, rlimitError) + _, expectedMetricsLen := getExpectedLengthOfReturnedMetrics(nameError, exeError, timeError, memError, memPercentError, diskError, pageFaultsError, threadError, contextSwitchError, fileDescriptorError, rlimitError, cgroupError) // excluding unsupported metrics from darwin 'metricsLen' if runtime.GOOS == "darwin" {