@@ -475,23 +475,58 @@ func (p *processHandleMock) RlimitUsageWithContext(ctx context.Context, b bool)
475
475
return args .Get (0 ).([]process.RlimitStat ), args .Error (1 )
476
476
}
477
477
478
- func newDefaultHandleMock () * processHandleMock {
479
- handleMock := & processHandleMock {}
480
- handleMock .On ("UsernameWithContext" , mock .Anything ).Return ("username" , nil )
481
- handleMock .On ("CmdlineWithContext" , mock .Anything ).Return ("cmdline" , nil )
482
- handleMock .On ("CmdlineSliceWithContext" , mock .Anything ).Return ([]string {"cmdline" }, nil )
483
- handleMock .On ("TimesWithContext" , mock .Anything ).Return (& cpu.TimesStat {}, nil )
484
- handleMock .On ("PercentWithContext" , mock .Anything , mock .Anything ).Return (float64 (0 ), nil )
485
- handleMock .On ("MemoryInfoWithContext" , mock .Anything ).Return (& process.MemoryInfoStat {}, nil )
486
- handleMock .On ("MemoryPercentWithContext" , mock .Anything ).Return (float32 (0 ), nil )
487
- handleMock .On ("IOCountersWithContext" , mock .Anything ).Return (& process.IOCountersStat {}, nil )
488
- handleMock .On ("PpidWithContext" , mock .Anything ).Return (int32 (2 ), nil )
489
- handleMock .On ("NumThreadsWithContext" , mock .Anything ).Return (int32 (0 ), nil )
490
- handleMock .On ("PageFaultsWithContext" , mock .Anything ).Return (& process.PageFaultsStat {}, nil )
491
- handleMock .On ("NumCtxSwitchesWithContext" , mock .Anything ).Return (& process.NumCtxSwitchesStat {}, nil )
492
- handleMock .On ("NumFDsWithContext" , mock .Anything ).Return (int32 (0 ), nil )
493
- handleMock .On ("RlimitUsageWithContext" , mock .Anything , mock .Anything ).Return ([]process.RlimitStat {}, nil )
494
- return handleMock
478
+ func initDefaultsHandleMock (t mock.TestingT , handleMock * processHandleMock ) {
479
+ if ! handleMock .IsMethodCallable (t , "UsernameWithContext" , mock .Anything ) {
480
+ handleMock .On ("UsernameWithContext" , mock .Anything ).Return ("username" , nil )
481
+ }
482
+ if ! handleMock .IsMethodCallable (t , "NameWithContext" , mock .Anything ) {
483
+ handleMock .On ("NameWithContext" , mock .Anything ).Return ("processname" , nil )
484
+ }
485
+ if ! handleMock .IsMethodCallable (t , "CmdlineWithContext" , mock .Anything ) {
486
+ handleMock .On ("CmdlineWithContext" , mock .Anything ).Return ("cmdline" , nil )
487
+ }
488
+ if ! handleMock .IsMethodCallable (t , "CmdlineSliceWithContext" , mock .Anything ) {
489
+ handleMock .On ("CmdlineSliceWithContext" , mock .Anything ).Return ([]string {"cmdline" }, nil )
490
+ }
491
+ if ! handleMock .IsMethodCallable (t , "TimesWithContext" , mock .Anything ) {
492
+ handleMock .On ("TimesWithContext" , mock .Anything ).Return (& cpu.TimesStat {}, nil )
493
+ }
494
+ if ! handleMock .IsMethodCallable (t , "PercentWithContext" , mock .Anything , mock .Anything ) {
495
+ handleMock .On ("PercentWithContext" , mock .Anything , mock .Anything ).Return (float64 (0 ), nil )
496
+ }
497
+ if ! handleMock .IsMethodCallable (t , "MemoryInfoWithContext" , mock .Anything ) {
498
+ handleMock .On ("MemoryInfoWithContext" , mock .Anything ).Return (& process.MemoryInfoStat {}, nil )
499
+ }
500
+ if ! handleMock .IsMethodCallable (t , "MemoryPercentWithContext" , mock .Anything ) {
501
+ handleMock .On ("MemoryPercentWithContext" , mock .Anything ).Return (float32 (0 ), nil )
502
+ }
503
+ if ! handleMock .IsMethodCallable (t , "IOCountersWithContext" , mock .Anything ) {
504
+ handleMock .On ("IOCountersWithContext" , mock .Anything ).Return (& process.IOCountersStat {}, nil )
505
+ }
506
+ if ! handleMock .IsMethodCallable (t , "PpidWithContext" , mock .Anything ) {
507
+ handleMock .On ("PpidWithContext" , mock .Anything ).Return (int32 (2 ), nil )
508
+ }
509
+ if ! handleMock .IsMethodCallable (t , "NumThreadsWithContext" , mock .Anything ) {
510
+ handleMock .On ("NumThreadsWithContext" , mock .Anything ).Return (int32 (0 ), nil )
511
+ }
512
+ if ! handleMock .IsMethodCallable (t , "PageFaultsWithContext" , mock .Anything ) {
513
+ handleMock .On ("PageFaultsWithContext" , mock .Anything ).Return (& process.PageFaultsStat {}, nil )
514
+ }
515
+ if ! handleMock .IsMethodCallable (t , "NumCtxSwitchesWithContext" , mock .Anything ) {
516
+ handleMock .On ("NumCtxSwitchesWithContext" , mock .Anything ).Return (& process.NumCtxSwitchesStat {}, nil )
517
+ }
518
+ if ! handleMock .IsMethodCallable (t , "NumFDsWithContext" , mock .Anything ) {
519
+ handleMock .On ("NumFDsWithContext" , mock .Anything ).Return (int32 (0 ), nil )
520
+ }
521
+ if ! handleMock .IsMethodCallable (t , "RlimitUsageWithContext" , mock .Anything , mock .Anything ) {
522
+ handleMock .On ("RlimitUsageWithContext" , mock .Anything , mock .Anything ).Return ([]process.RlimitStat {}, nil )
523
+ }
524
+ if ! handleMock .IsMethodCallable (t , "CreateTimeWithContext" , mock .Anything ) {
525
+ handleMock .On ("CreateTimeWithContext" , mock .Anything ).Return (time .Now ().UnixMilli (), nil )
526
+ }
527
+ if ! handleMock .IsMethodCallable (t , "ExeWithContext" , mock .Anything ) {
528
+ handleMock .On ("ExeWithContext" , mock .Anything ).Return ("processname" , nil )
529
+ }
495
530
}
496
531
497
532
func TestScrapeMetrics_Filtered (t * testing.T ) {
@@ -598,10 +633,12 @@ func TestScrapeMetrics_Filtered(t *testing.T) {
598
633
599
634
handles := make ([]* processHandleMock , 0 , len (test .names ))
600
635
for i , name := range test .names {
601
- handleMock := newDefaultHandleMock ()
636
+ handleMock := & processHandleMock {}
602
637
handleMock .On ("NameWithContext" , mock .Anything ).Return (name , nil )
603
638
handleMock .On ("ExeWithContext" , mock .Anything ).Return (name , nil )
604
639
handleMock .On ("CreateTimeWithContext" , mock .Anything ).Return (time .Now ().UnixMilli ()- test .upTimeMs [i ], nil )
640
+ initDefaultsHandleMock (t , handleMock )
641
+
605
642
handles = append (handles , handleMock )
606
643
}
607
644
@@ -1001,22 +1038,34 @@ func TestScrapeMetrics_MuteErrorFlags(t *testing.T) {
1001
1038
},
1002
1039
{
1003
1040
name : "Process User Error Muted" ,
1004
- skipTestCase : runtime .GOOS != "linux" ,
1005
1041
muteProcessUserError : true ,
1006
1042
skipProcessNameError : true ,
1007
1043
muteProcessExeError : true ,
1008
1044
muteProcessNameError : true ,
1009
- expectedCount : 4 ,
1045
+ expectedCount : func () int {
1046
+ if runtime .GOOS == "darwin" {
1047
+ // disk.io is not collected on darwin
1048
+ return 3
1049
+ }
1050
+ return 4
1051
+ }(),
1010
1052
},
1011
1053
{
1012
1054
name : "Process User Error Unmuted" ,
1013
- skipTestCase : runtime .GOOS != "linux" ,
1014
1055
muteProcessUserError : false ,
1015
1056
skipProcessNameError : true ,
1016
1057
muteProcessExeError : true ,
1017
1058
muteProcessNameError : true ,
1018
- expectedError : fmt .Sprintf ("error reading username for process \" processname\" (pid 1): %v" , processNameError ),
1019
- expectedCount : 4 ,
1059
+ expectedError : func () string {
1060
+ return fmt .Sprintf ("error reading username for process \" processname\" (pid 1): %v" , processNameError )
1061
+ }(),
1062
+ expectedCount : func () int {
1063
+ if runtime .GOOS == "darwin" {
1064
+ // disk.io is not collected on darwin
1065
+ return 3
1066
+ }
1067
+ return 4
1068
+ }(),
1020
1069
},
1021
1070
}
1022
1071
@@ -1037,21 +1086,17 @@ func TestScrapeMetrics_MuteErrorFlags(t *testing.T) {
1037
1086
err = scraper .start (context .Background (), componenttest .NewNopHost ())
1038
1087
require .NoError (t , err , "Failed to initialize process scraper: %v" , err )
1039
1088
1040
- handleMock := newDefaultHandleMock ()
1089
+ handleMock := & processHandleMock {}
1041
1090
if ! test .skipProcessNameError {
1042
1091
handleMock .On ("NameWithContext" , mock .Anything ).Return ("test" , processNameError )
1092
+ handleMock .On ("ExeWithContext" , mock .Anything ).Return ("test" , processNameError )
1093
+ handleMock .On ("CmdlineWithContext" , mock .Anything ).Return ("test" , processNameError )
1043
1094
} else {
1044
- for _ , c := range handleMock .ExpectedCalls {
1045
- if c .Method == "UsernameWithContext" {
1046
- c .ReturnArguments = []interface {}{"processname" , processNameError }
1047
- break
1048
- }
1049
- }
1095
+ handleMock .On ("UsernameWithContext" , mock .Anything ).Return ("processname" , processNameError )
1050
1096
handleMock .On ("NameWithContext" , mock .Anything ).Return ("processname" , nil )
1051
1097
handleMock .On ("CreateTimeWithContext" , mock .Anything ).Return (time .Now ().UnixMilli (), nil )
1052
1098
}
1053
- handleMock .On ("ExeWithContext" , mock .Anything ).Return ("test" , processNameError )
1054
- handleMock .On ("CmdlineWithContext" , mock .Anything ).Return ("test" , processNameError )
1099
+ initDefaultsHandleMock (t , handleMock )
1055
1100
1056
1101
if config .MuteProcessIOError {
1057
1102
handleMock .On ("IOCountersWithContext" , mock .Anything ).Return ("test" , errors .New ("permission denied" ))
@@ -1182,10 +1227,11 @@ func TestScrapeMetrics_CpuUtilizationWhenCpuTimesIsDisabled(t *testing.T) {
1182
1227
err = scraper .start (context .Background (), componenttest .NewNopHost ())
1183
1228
require .NoError (t , err , "Failed to initialize process scraper: %v" , err )
1184
1229
1185
- handleMock := newDefaultHandleMock ()
1230
+ handleMock := & processHandleMock {}
1186
1231
handleMock .On ("NameWithContext" , mock .Anything ).Return ("test" , nil )
1187
1232
handleMock .On ("ExeWithContext" , mock .Anything ).Return ("test" , nil )
1188
1233
handleMock .On ("CreateTimeWithContext" , mock .Anything ).Return (time .Now ().UnixMilli (), nil )
1234
+ initDefaultsHandleMock (t , handleMock )
1189
1235
1190
1236
scraper .getProcessHandles = func (context.Context ) (processHandles , error ) {
1191
1237
return & processHandlesMock {handles : []* processHandleMock {handleMock }}, nil
0 commit comments