-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager_test.go
218 lines (195 loc) · 7.55 KB
/
manager_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package jasper
import (
"context"
"errors"
"testing"
"github.com/tychoish/fun/assert"
"github.com/tychoish/fun/assert/check"
"github.com/tychoish/fun/testt"
"github.com/tychoish/jasper/options"
"github.com/tychoish/jasper/testutil"
)
type mockProcessTracker struct {
FailAdd bool
FailCleanup bool
Infos []ProcessInfo
}
func (t *mockProcessTracker) Add(info ProcessInfo) error {
if t.FailAdd {
return errors.New("failed in Add")
}
t.Infos = append(t.Infos, info)
return nil
}
func (t *mockProcessTracker) Cleanup() error {
if t.FailCleanup {
return errors.New("failed in Cleanup")
}
t.Infos = []ProcessInfo{}
return nil
}
func TestTrackedManager(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for managerName, makeManager := range map[string]func() *basicProcessManager{
"Basic": func() *basicProcessManager {
return &basicProcessManager{
procs: map[string]Process{},
loggers: NewLoggingCache(),
tracker: &mockProcessTracker{
Infos: []ProcessInfo{},
},
}
},
} {
t.Run(managerName, func(t *testing.T) {
for name, test := range map[string]func(context.Context, *testing.T, *basicProcessManager, *options.Create){
"ValidateFixtureSetup": func(ctx context.Context, t *testing.T, manager *basicProcessManager, opts *options.Create) {
check.True(t, nil != manager.tracker)
check.Equal(t, len(manager.procs), 0)
check.True(t, nil != manager.LoggingCache(ctx))
},
"CreateProcessTracksProcess": func(ctx context.Context, t *testing.T, manager *basicProcessManager, opts *options.Create) {
proc, err := manager.CreateProcess(ctx, opts)
assert.NotError(t, err)
check.Equal(t, len(manager.procs), 1)
mockTracker, ok := manager.tracker.(*mockProcessTracker)
assert.True(t, ok)
assert.Equal(t, len(mockTracker.Infos), 1)
check.Equal(t, proc.Info(ctx).ID, mockTracker.Infos[0].ID)
},
"CreateCommandTracksCommandAfterRun": func(ctx context.Context, t *testing.T, manager *basicProcessManager, opts *options.Create) {
err := manager.CreateCommand(ctx).Add(opts.Args).Background(true).Run(ctx)
assert.NotError(t, err)
check.Equal(t, len(manager.procs), 1)
mockTracker, ok := manager.tracker.(*mockProcessTracker)
assert.True(t, ok)
assert.Equal(t, len(mockTracker.Infos), 1)
assert.True(t, len(mockTracker.Infos[0].Options.Args) != 0)
},
"DoNotTrackProcessIfCreateProcessDoesNotMakeProcess": func(ctx context.Context, t *testing.T, manager *basicProcessManager, opts *options.Create) {
opts.Args = []string{"foo"}
_, err := manager.CreateProcess(ctx, opts)
assert.Error(t, err)
check.Equal(t, len(manager.procs), 0)
mockTracker, ok := manager.tracker.(*mockProcessTracker)
assert.True(t, ok)
check.Equal(t, len(mockTracker.Infos), 0)
},
"DoNotTrackProcessIfCreateCommandDoesNotMakeProcess": func(ctx context.Context, t *testing.T, manager *basicProcessManager, opts *options.Create) {
opts.Args = []string{"foo"}
cmd := manager.CreateCommand(ctx).Add(opts.Args).Background(true)
cmd.opts.Process = *opts
err := cmd.Run(ctx)
assert.Error(t, err)
check.Equal(t, len(manager.procs), 0)
mockTracker, ok := manager.tracker.(*mockProcessTracker)
assert.True(t, ok)
check.Equal(t, len(mockTracker.Infos), 0)
},
"CloseCleansUpProcesses": func(ctx context.Context, t *testing.T, manager *basicProcessManager, opts *options.Create) {
cmd := manager.CreateCommand(ctx).Background(true).Add(opts.Args)
cmd.opts.Process = *opts
assert.NotError(t, cmd.Run(ctx))
check.Equal(t, len(manager.procs), 1)
mockTracker, ok := manager.tracker.(*mockProcessTracker)
assert.True(t, ok)
assert.Equal(t, len(mockTracker.Infos), 1)
check.NotZero(t, mockTracker.Infos[0].ID)
assert.NotError(t, manager.Close(ctx))
check.Equal(t, len(mockTracker.Infos), 0)
assert.NotError(t, manager.Close(ctx))
},
"CloseWithNoProcessesIsNotError": func(ctx context.Context, t *testing.T, manager *basicProcessManager, opts *options.Create) {
mockTracker, ok := manager.tracker.(*mockProcessTracker)
assert.True(t, ok)
assert.NotError(t, manager.Close(ctx))
check.Equal(t, len(mockTracker.Infos), 0)
assert.NotError(t, manager.Close(ctx))
check.Equal(t, len(mockTracker.Infos), 0)
},
"DoubleCloseIsNotError": func(ctx context.Context, t *testing.T, manager *basicProcessManager, opts *options.Create) {
cmd := manager.CreateCommand(ctx).Background(true).Add(opts.Args)
cmd.opts.Process = *opts
assert.NotError(t, cmd.Run(ctx))
check.Equal(t, len(manager.procs), 1)
mockTracker, ok := manager.tracker.(*mockProcessTracker)
assert.True(t, ok)
assert.Equal(t, len(mockTracker.Infos), 1)
check.NotZero(t, mockTracker.Infos[0].ID)
assert.NotError(t, manager.Close(ctx))
check.Equal(t, len(mockTracker.Infos), 0)
assert.NotError(t, manager.Close(ctx))
check.Equal(t, len(mockTracker.Infos), 0)
},
// "": func(ctx context.Context, t *testing.T, manager Manager, mod testutil.OptsModify) {},
} {
tctx, cancel := context.WithTimeout(ctx, testutil.ManagerTestTimeout)
defer cancel()
t.Run(name+"Manager/BlockingProcess", func(t *testing.T) {
opts := testutil.SleepCreateOpts(1)
opts.Implementation = options.ProcessImplementationBlocking
test(tctx, t, makeManager(), opts)
})
t.Run(name+"Manager/BasicProcess", func(t *testing.T) {
opts := testutil.SleepCreateOpts(1)
opts.Implementation = options.ProcessImplementationBasic
test(tctx, t, makeManager(), opts)
})
}
})
}
}
func TestManagerSetsEnvironmentVariables(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for managerName, makeManager := range map[string]func() *basicProcessManager{
"Basic": func() *basicProcessManager {
return &basicProcessManager{
procs: map[string]Process{},
loggers: NewLoggingCache(),
tracker: &mockProcessTracker{
Infos: []ProcessInfo{},
},
}
},
} {
t.Run(managerName, func(t *testing.T) {
for testName, testCase := range map[string]func(context.Context, *testing.T, *basicProcessManager){
"CreateProcessSetsManagerEnvironmentVariables": func(ctx context.Context, t *testing.T, manager *basicProcessManager) {
proc, err := manager.CreateProcess(ctx, testutil.SleepCreateOpts(1))
assert.NotError(t, err)
env := proc.Info(ctx).Options.Environment
assert.True(t, env != nil)
value, ok := env[ManagerEnvironID]
assert.True(t, ok)
check.Equal(t, value, manager.id)
testt.Log(t, "process should have manager environment variable set")
},
"CreateCommandAddsEnvironmentVariables": func(ctx context.Context, t *testing.T, manager *basicProcessManager) {
envVar := ManagerEnvironID
value := manager.id
cmdArgs := []string{"yes"}
cmd := manager.CreateCommand(ctx).AddEnv(ManagerEnvironID, manager.id).Add(cmdArgs).Background(true)
assert.NotError(t, cmd.Run(ctx))
ids := cmd.GetProcIDs()
assert.Equal(t, len(ids), 1)
proc, err := manager.Get(ctx, ids[0])
assert.NotError(t, err)
env := proc.Info(ctx).Options.Environment
assert.True(t, env != nil)
actualValue, ok := env[envVar]
assert.True(t, ok)
check.Equal(t, value, actualValue)
},
} {
t.Run(testName, func(t *testing.T) {
tctx, cancel := context.WithTimeout(ctx, testutil.ManagerTestTimeout)
defer cancel()
testCase(tctx, t, makeManager())
})
}
})
}
}