-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager_windows_test.go
103 lines (91 loc) · 4.07 KB
/
manager_windows_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//go:build windows
// +build windows
package jasper
import (
"context"
"os"
"syscall"
"testing"
"github.com/tychoish/fun/assert"
"github.com/tychoish/fun/assert/check"
"github.com/tychoish/jasper/options"
"github.com/tychoish/jasper/testutil"
)
func TestBasicManagerWithTrackedProcesses(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for managerName, makeManager := range map[string]func(ctx context.Context, t *testing.T) *basicProcessManager{
"Basic": func(ctx context.Context, t *testing.T) *basicProcessManager {
basicManager, err := newBasicProcessManager(map[string]Process{}, true, false)
assert.NotError(t, err)
return basicManager.(*basicProcessManager)
},
} {
t.Run(managerName, func(t *testing.T) {
for testName, testCase := range map[string]func(context.Context, *testing.T, *basicProcessManager, *windowsProcessTracker, *options.Create){
"ProcessTrackerCreatedEmpty": func(_ context.Context, t *testing.T, m *basicProcessManager, tracker *windowsProcessTracker, _ *options.Create) {
assert.True(t, tracker.job != nil)
info, err := QueryInformationJobObjectProcessIdList(tracker.job.handle)
check.NotError(t, err)
check.Zero(t, info.NumberOfAssignedProcesses)
},
"CreateAddsProcess": func(ctx context.Context, t *testing.T, m *basicProcessManager, tracker *windowsProcessTracker, opts *options.Create) {
proc, err := m.CreateProcess(ctx, opts)
assert.NotError(t, err)
info, err := QueryInformationJobObjectProcessIdList(tracker.job.handle)
check.NotError(t, err)
check.Equal(t, 1, int(info.NumberOfAssignedProcesses))
check.Equal(t, proc.Info(ctx).PID, int(info.ProcessIdList[0]))
check.NotError(t, m.Close(ctx))
},
"RegisterAddsProcess": func(ctx context.Context, t *testing.T, m *basicProcessManager, tracker *windowsProcessTracker, opts *options.Create) {
proc, err := newBasicProcess(ctx, opts)
assert.NotError(t, err)
check.NotError(t, m.Register(ctx, proc))
info, err := QueryInformationJobObjectProcessIdList(tracker.job.handle)
check.NotError(t, err)
check.Equal(t, 1, int(info.NumberOfAssignedProcesses))
check.Equal(t, proc.Info(ctx).PID, int(info.ProcessIdList[0]))
check.NotError(t, m.Close(ctx))
},
"ClosePerformsProcessTrackingCleanup": func(ctx context.Context, t *testing.T, m *basicProcessManager, tracker *windowsProcessTracker, opts *options.Create) {
proc, err := m.CreateProcess(ctx, opts)
assert.NotError(t, err)
info, err := QueryInformationJobObjectProcessIdList(tracker.job.handle)
check.NotError(t, err)
check.Equal(t, 1, int(info.NumberOfAssignedProcesses))
check.Equal(t, proc.Info(ctx).PID, int(info.ProcessIdList[0]))
check.NotError(t, m.Close(ctx))
exitCode, err := proc.Wait(ctx)
check.NotError(t, err)
check.Zero(t, exitCode)
check.True(t, !proc.Running(ctx))
check.True(t, proc.Complete(ctx))
},
"CloseOnTerminatedProcessSucceeds": func(ctx context.Context, t *testing.T, m *basicProcessManager, tracker *windowsProcessTracker, opts *options.Create) {
proc, err := m.CreateProcess(ctx, opts)
assert.NotError(t, err)
info, err := QueryInformationJobObjectProcessIdList(tracker.job.handle)
check.NotError(t, err)
check.Equal(t, 1, int(info.NumberOfAssignedProcesses))
check.Equal(t, proc.Info(ctx).PID, int(info.ProcessIdList[0]))
check.NotError(t, proc.Signal(ctx, syscall.SIGKILL))
check.NotError(t, m.Close(ctx))
},
} {
t.Run(testName, func(t *testing.T) {
if _, runningInEvgAgent := os.LookupEnv("EVR_TASK_ID"); runningInEvgAgent {
t.Skip("Evergreen makes its own job object, so these will not pass in Evergreen tests ",
"(although they will pass if locally run).")
}
tctx, cancel := context.WithTimeout(ctx, testutil.TestTimeout)
defer cancel()
manager := makeManager(tctx, t)
tracker, ok := manager.tracker.(*windowsProcessTracker)
assert.True(t, ok)
testCase(tctx, t, manager, tracker, testutil.SleepCreateOpts(1))
})
}
})
}
}