From b8e31fee318abf95632b5a2f2d06ac6346b7d1bb Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Thu, 2 Nov 2023 13:50:11 +0100 Subject: [PATCH 1/2] Windows is passing --- .../internal/scraper/processscraper/process_scraper_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_test.go b/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_test.go index f57814e39611..279bd9a6cdd6 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_test.go +++ b/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_test.go @@ -524,6 +524,9 @@ func initDefaultsHandleMock(t mock.TestingT, handleMock *processHandleMock) { if !handleMock.IsMethodCallable(t, "CreateTimeWithContext", mock.Anything) { handleMock.On("CreateTimeWithContext", mock.Anything).Return(time.Now().UnixMilli(), nil) } + if !handleMock.IsMethodCallable(t, "ExeWithContext", mock.Anything) { + handleMock.On("ExeWithContext", mock.Anything).Return("processname", nil) + } } func TestScrapeMetrics_Filtered(t *testing.T) { From d3436083e1d878c5e29f9f1f0fe8594612c6cc26 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Thu, 2 Nov 2023 13:53:23 +0100 Subject: [PATCH 2/2] Fixed TestScrapeMetrics_MuteErrorFlags for darwin --- .../processscraper/process_scraper_test.go | 109 ++++++++++++------ 1 file changed, 76 insertions(+), 33 deletions(-) diff --git a/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_test.go b/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_test.go index ec0885646659..f57814e39611 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_test.go +++ b/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_test.go @@ -475,23 +475,55 @@ func (p *processHandleMock) RlimitUsageWithContext(ctx context.Context, b bool) return args.Get(0).([]process.RlimitStat), args.Error(1) } -func newDefaultHandleMock() *processHandleMock { - handleMock := &processHandleMock{} - handleMock.On("UsernameWithContext", mock.Anything).Return("username", nil) - handleMock.On("CmdlineWithContext", mock.Anything).Return("cmdline", nil) - handleMock.On("CmdlineSliceWithContext", mock.Anything).Return([]string{"cmdline"}, nil) - handleMock.On("TimesWithContext", mock.Anything).Return(&cpu.TimesStat{}, nil) - handleMock.On("PercentWithContext", mock.Anything, mock.Anything).Return(float64(0), nil) - handleMock.On("MemoryInfoWithContext", mock.Anything).Return(&process.MemoryInfoStat{}, nil) - handleMock.On("MemoryPercentWithContext", mock.Anything).Return(float32(0), nil) - handleMock.On("IOCountersWithContext", mock.Anything).Return(&process.IOCountersStat{}, nil) - handleMock.On("PpidWithContext", mock.Anything).Return(int32(2), nil) - handleMock.On("NumThreadsWithContext", mock.Anything).Return(int32(0), nil) - handleMock.On("PageFaultsWithContext", mock.Anything).Return(&process.PageFaultsStat{}, nil) - handleMock.On("NumCtxSwitchesWithContext", mock.Anything).Return(&process.NumCtxSwitchesStat{}, nil) - handleMock.On("NumFDsWithContext", mock.Anything).Return(int32(0), nil) - handleMock.On("RlimitUsageWithContext", mock.Anything, mock.Anything).Return([]process.RlimitStat{}, nil) - return handleMock +func initDefaultsHandleMock(t mock.TestingT, handleMock *processHandleMock) { + if !handleMock.IsMethodCallable(t, "UsernameWithContext", mock.Anything) { + handleMock.On("UsernameWithContext", mock.Anything).Return("username", nil) + } + if !handleMock.IsMethodCallable(t, "NameWithContext", mock.Anything) { + handleMock.On("NameWithContext", mock.Anything).Return("processname", nil) + } + if !handleMock.IsMethodCallable(t, "CmdlineWithContext", mock.Anything) { + handleMock.On("CmdlineWithContext", mock.Anything).Return("cmdline", nil) + } + if !handleMock.IsMethodCallable(t, "CmdlineSliceWithContext", mock.Anything) { + handleMock.On("CmdlineSliceWithContext", mock.Anything).Return([]string{"cmdline"}, nil) + } + if !handleMock.IsMethodCallable(t, "TimesWithContext", mock.Anything) { + handleMock.On("TimesWithContext", mock.Anything).Return(&cpu.TimesStat{}, nil) + } + if !handleMock.IsMethodCallable(t, "PercentWithContext", mock.Anything, mock.Anything) { + handleMock.On("PercentWithContext", mock.Anything, mock.Anything).Return(float64(0), nil) + } + if !handleMock.IsMethodCallable(t, "MemoryInfoWithContext", mock.Anything) { + handleMock.On("MemoryInfoWithContext", mock.Anything).Return(&process.MemoryInfoStat{}, nil) + } + if !handleMock.IsMethodCallable(t, "MemoryPercentWithContext", mock.Anything) { + handleMock.On("MemoryPercentWithContext", mock.Anything).Return(float32(0), nil) + } + if !handleMock.IsMethodCallable(t, "IOCountersWithContext", mock.Anything) { + handleMock.On("IOCountersWithContext", mock.Anything).Return(&process.IOCountersStat{}, nil) + } + if !handleMock.IsMethodCallable(t, "PpidWithContext", mock.Anything) { + handleMock.On("PpidWithContext", mock.Anything).Return(int32(2), nil) + } + if !handleMock.IsMethodCallable(t, "NumThreadsWithContext", mock.Anything) { + handleMock.On("NumThreadsWithContext", mock.Anything).Return(int32(0), nil) + } + if !handleMock.IsMethodCallable(t, "PageFaultsWithContext", mock.Anything) { + handleMock.On("PageFaultsWithContext", mock.Anything).Return(&process.PageFaultsStat{}, nil) + } + if !handleMock.IsMethodCallable(t, "NumCtxSwitchesWithContext", mock.Anything) { + handleMock.On("NumCtxSwitchesWithContext", mock.Anything).Return(&process.NumCtxSwitchesStat{}, nil) + } + if !handleMock.IsMethodCallable(t, "NumFDsWithContext", mock.Anything) { + handleMock.On("NumFDsWithContext", mock.Anything).Return(int32(0), nil) + } + if !handleMock.IsMethodCallable(t, "RlimitUsageWithContext", mock.Anything, mock.Anything) { + handleMock.On("RlimitUsageWithContext", mock.Anything, mock.Anything).Return([]process.RlimitStat{}, nil) + } + if !handleMock.IsMethodCallable(t, "CreateTimeWithContext", mock.Anything) { + handleMock.On("CreateTimeWithContext", mock.Anything).Return(time.Now().UnixMilli(), nil) + } } func TestScrapeMetrics_Filtered(t *testing.T) { @@ -598,10 +630,12 @@ func TestScrapeMetrics_Filtered(t *testing.T) { handles := make([]*processHandleMock, 0, len(test.names)) for i, name := range test.names { - handleMock := newDefaultHandleMock() + handleMock := &processHandleMock{} handleMock.On("NameWithContext", mock.Anything).Return(name, nil) handleMock.On("ExeWithContext", mock.Anything).Return(name, nil) handleMock.On("CreateTimeWithContext", mock.Anything).Return(time.Now().UnixMilli()-test.upTimeMs[i], nil) + initDefaultsHandleMock(t, handleMock) + handles = append(handles, handleMock) } @@ -1001,22 +1035,34 @@ func TestScrapeMetrics_MuteErrorFlags(t *testing.T) { }, { name: "Process User Error Muted", - skipTestCase: runtime.GOOS != "linux", muteProcessUserError: true, skipProcessNameError: true, muteProcessExeError: true, muteProcessNameError: true, - expectedCount: 4, + expectedCount: func() int { + if runtime.GOOS == "darwin" { + // disk.io is not collected on darwin + return 3 + } + return 4 + }(), }, { name: "Process User Error Unmuted", - skipTestCase: runtime.GOOS != "linux", muteProcessUserError: false, skipProcessNameError: true, muteProcessExeError: true, muteProcessNameError: true, - expectedError: fmt.Sprintf("error reading username for process \"processname\" (pid 1): %v", processNameError), - expectedCount: 4, + expectedError: func() string { + return fmt.Sprintf("error reading username for process \"processname\" (pid 1): %v", processNameError) + }(), + expectedCount: func() int { + if runtime.GOOS == "darwin" { + // disk.io is not collected on darwin + return 3 + } + return 4 + }(), }, } @@ -1037,21 +1083,17 @@ func TestScrapeMetrics_MuteErrorFlags(t *testing.T) { err = scraper.start(context.Background(), componenttest.NewNopHost()) require.NoError(t, err, "Failed to initialize process scraper: %v", err) - handleMock := newDefaultHandleMock() + handleMock := &processHandleMock{} if !test.skipProcessNameError { handleMock.On("NameWithContext", mock.Anything).Return("test", processNameError) + handleMock.On("ExeWithContext", mock.Anything).Return("test", processNameError) + handleMock.On("CmdlineWithContext", mock.Anything).Return("test", processNameError) } else { - for _, c := range handleMock.ExpectedCalls { - if c.Method == "UsernameWithContext" { - c.ReturnArguments = []interface{}{"processname", processNameError} - break - } - } + handleMock.On("UsernameWithContext", mock.Anything).Return("processname", processNameError) handleMock.On("NameWithContext", mock.Anything).Return("processname", nil) handleMock.On("CreateTimeWithContext", mock.Anything).Return(time.Now().UnixMilli(), nil) } - handleMock.On("ExeWithContext", mock.Anything).Return("test", processNameError) - handleMock.On("CmdlineWithContext", mock.Anything).Return("test", processNameError) + initDefaultsHandleMock(t, handleMock) if config.MuteProcessIOError { handleMock.On("IOCountersWithContext", mock.Anything).Return("test", errors.New("permission denied")) @@ -1182,10 +1224,11 @@ func TestScrapeMetrics_CpuUtilizationWhenCpuTimesIsDisabled(t *testing.T) { err = scraper.start(context.Background(), componenttest.NewNopHost()) require.NoError(t, err, "Failed to initialize process scraper: %v", err) - handleMock := newDefaultHandleMock() + handleMock := &processHandleMock{} handleMock.On("NameWithContext", mock.Anything).Return("test", nil) handleMock.On("ExeWithContext", mock.Anything).Return("test", nil) handleMock.On("CreateTimeWithContext", mock.Anything).Return(time.Now().UnixMilli(), nil) + initDefaultsHandleMock(t, handleMock) scraper.getProcessHandles = func(context.Context) (processHandles, error) { return &processHandlesMock{handles: []*processHandleMock{handleMock}}, nil