@@ -12,8 +12,8 @@ import (
12
12
"os/exec"
13
13
"path/filepath"
14
14
"runtime"
15
+ "strings"
15
16
"sync"
16
- "syscall"
17
17
"testing"
18
18
"time"
19
19
@@ -1043,7 +1043,7 @@ func Test_takedownWatcher(t *testing.T) {
1043
1043
}{
1044
1044
{
1045
1045
name : "no contention for watcher applocker" ,
1046
- setup : func (t * testing.T , log * logger.Logger , workdir string ) (watcherPIDsFetcher , []testProcess ) {
1046
+ setup : func (_ * testing.T , _ * logger.Logger , _ string ) (watcherPIDsFetcher , []testProcess ) {
1047
1047
// nothing to do here, always return and empty list of pids
1048
1048
return func () ([]int , error ) {
1049
1049
return nil , nil
@@ -1065,8 +1065,7 @@ func Test_takedownWatcher(t *testing.T) {
1065
1065
{
1066
1066
name : "contention with test binary listening to signals: test binary is terminated gracefully" ,
1067
1067
setup : func (t * testing.T , log * logger.Logger , workdir string ) (watcherPIDsFetcher , []testProcess ) {
1068
-
1069
- cmd , testChan := createTestlockerCommand (t , log , applockerFileName , testExecutableAbsolutePath , workdir , false )
1068
+ cmd , testChan := createTestlockerCommand (t , log .Named ("testlocker" ), applockerFileName , testExecutableAbsolutePath , workdir , false )
1070
1069
require .NoError (t , err , "error starting testlocker binary" )
1071
1070
1072
1071
// wait for test binary to acquire lock
@@ -1087,7 +1086,7 @@ func Test_takedownWatcher(t *testing.T) {
1087
1086
require .NotNil (t , testlockerProcess , "test locker process info should have a not nil cmd" )
1088
1087
1089
1088
require .Eventually (t , func () bool {
1090
- running , checkErr := isProcessRunning (testlockerProcess .cmd )
1089
+ running , checkErr := isProcessRunning (t , testlockerProcess .cmd )
1091
1090
if checkErr != nil {
1092
1091
t .Logf ("error checking for testlocker process running: %s" , checkErr .Error ())
1093
1092
return false
@@ -1112,7 +1111,7 @@ func Test_takedownWatcher(t *testing.T) {
1112
1111
{
1113
1112
name : "contention with test binary not listening to signals: test binary is not terminated" ,
1114
1113
setup : func (t * testing.T , log * logger.Logger , workdir string ) (watcherPIDsFetcher , []testProcess ) {
1115
- cmd , waitChan := createTestlockerCommand (t , log , applockerFileName , testExecutableAbsolutePath , workdir , true )
1114
+ cmd , waitChan := createTestlockerCommand (t , log . Named ( "testlocker" ) , applockerFileName , testExecutableAbsolutePath , workdir , true )
1116
1115
require .NoError (t , err , "error starting testlocker binary" )
1117
1116
1118
1117
// wait for test binary to acquire lock
@@ -1134,7 +1133,7 @@ func Test_takedownWatcher(t *testing.T) {
1134
1133
1135
1134
// check that the process is still running for a time
1136
1135
assert .Never (t , func () bool {
1137
- running , checkErr := isProcessRunning (testlockerProcess .cmd )
1136
+ running , checkErr := isProcessRunning (t , testlockerProcess .cmd )
1138
1137
if checkErr != nil {
1139
1138
t .Logf ("error checking for testlocker process running: %s" , checkErr .Error ())
1140
1139
return false
@@ -1159,9 +1158,12 @@ func Test_takedownWatcher(t *testing.T) {
1159
1158
t .Run (tc .name , func (t * testing.T ) {
1160
1159
workDir := t .TempDir ()
1161
1160
log , obsLogs := loggertest .New (t .Name ())
1161
+ t .Cleanup (func () {
1162
+ // however it ends, try to print out the logs of TakedownWatcher
1163
+ loggertest .PrintObservedLogs (obsLogs .All (), t .Log )
1164
+ })
1162
1165
pidFetcher , processInfos := tc .setup (t , log , workDir )
1163
- tc .wantErr (t , TakedownWatcher (t .Context (), log , pidFetcher ))
1164
- t .Logf ("test logs: %v" , obsLogs )
1166
+ tc .wantErr (t , TakedownWatcher (t .Context (), log .Named ("TakedownWatcher" ), pidFetcher ))
1165
1167
if tc .assertPostTakedown != nil {
1166
1168
tc .assertPostTakedown (t , workDir , processInfos )
1167
1169
}
@@ -1195,30 +1197,30 @@ func createTestlockerCommand(t *testing.T, log *logger.Logger, applockerFileName
1195
1197
return watcherCmd , watchTerminated
1196
1198
}
1197
1199
1198
- func isProcessRunning (cmd * exec.Cmd ) (bool , error ) {
1200
+ func isProcessRunning (t * testing. T , cmd * exec.Cmd ) (bool , error ) {
1199
1201
if cmd .Process == nil {
1200
1202
return false , nil
1201
1203
}
1202
-
1204
+ t . Logf ( "checking if pid %d is still running" , cmd . Process . Pid )
1203
1205
// search for the pid on the running processes
1204
1206
process , err := os .FindProcess (cmd .Process .Pid )
1205
1207
if err != nil {
1208
+ t .Logf ("error string: %q" , err .Error ())
1209
+ if runtime .GOOS == "windows" && strings .Contains (err .Error (), "The parameter is incorrect" ) {
1210
+ // in windows, noone can hear you scream
1211
+ // invalid parameter means that the process object cannot be found
1212
+ t .Logf ("pid %d is not running because on windows we got an incorrect parameter error" , cmd .Process .Pid )
1213
+ return false , nil
1214
+ }
1215
+
1216
+ t .Logf ("error finding process: %T %v" , err , err )
1206
1217
return false , err
1207
1218
}
1208
1219
1209
1220
if process == nil {
1221
+ t .Logf ("pid %d is not running because os.GetProcess returned a nil process" , cmd .Process .Pid )
1210
1222
return false , nil
1211
1223
}
1212
- // if process is not nil we need to split between unix and non-unix OSes
1213
- if runtime .GOOS == "windows" {
1214
- return true , nil
1215
- } else {
1216
- // on unix system we always get a process back, we need to do some further checks
1217
- signalErr := cmd .Process .Signal (syscall .Signal (0 ))
1218
- if signalErr != nil {
1219
- return false , nil //nolint:nilerr // if we receive an error it means that the process is not running, so the check completed without errors
1220
- } else {
1221
- return true , nil
1222
- }
1223
- }
1224
+
1225
+ return isProcessLive (cmd .Process )
1224
1226
}
0 commit comments