Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] fix process scraper test to not run on Darwin and Windows for cgroups #31394

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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" {
Expand All @@ -722,15 +722,15 @@ func TestScrapeMetrics_ProcessErrors(t *testing.T) {
},
{
name: "Cgroup Error",
osFilter: "linux",
osFilter: []string{"darwin", "windows"},
djaglowski marked this conversation as resolved.
Show resolved Hide resolved
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`,
},
Expand All @@ -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"),
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -902,15 +904,15 @@ 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())

assert.EqualError(t, err, test.expectedError)
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)
Expand All @@ -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
Expand Down Expand Up @@ -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" {
Expand Down
Loading