-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathorchestrator_test.go
361 lines (318 loc) · 9.46 KB
/
orchestrator_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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package task
import (
"context"
"fmt"
"io"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"sync"
"syscall"
"testing"
"time"
"github.com/circleci/ex/testing/testcontext"
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
"github.com/circleci/runner-init/clients/runner"
"github.com/circleci/runner-init/internal/testing/fakerunnerapi"
)
var testOnce sync.Once
func TestOrchestrator(t *testing.T) {
if os.Getenv("BE_TASK_AGENT") == "true" {
// If BE_TASK_AGENT is set, then this test will run itself as a fake task agent.
// This provides a way to run checks against the task agent command.
beFakeTaskAgent(t)
return
}
testOnce.Do(func() {
// Reduce the process reap timeout to speed up the tests
reapTimeout = 500 * time.Millisecond
})
// Re-parent any child processes to us to simulate the orchestrator being init
const PrSetChildSubreaper = 36
_, _, err := syscall.Syscall(syscall.SYS_PRCTL, PrSetChildSubreaper, uintptr(1), 0)
assert.Check(t, err == 0)
testPath := os.Args[0]
scratchDir := t.TempDir()
defaultConfig := Config{
Token: "testtoken",
TaskAgentPath: testPath + " -test.run=TestOrchestrator",
Allocation: "testalloc",
}
tests := []struct {
name string
config Config
env map[string]string
gracePeriod time.Duration
timeout time.Duration
additionalTasks []fakerunnerapi.Task
wantError string
wantTimeout bool
wantTaskUnclaims []fakerunnerapi.TaskUnclaim
wantTaskEvents []fakerunnerapi.TaskEvent
extraChecks []func(t *testing.T)
}{
{
name: "happy path",
env: map[string]string{
"CIRCLECI_GOAT_CONFIG": "{}",
},
config: defaultConfig,
},
{
name: "custom entrypoint",
config: Config{
Cmd: []string{"/bin/sh", "-c", fmt.Sprintf("touch %s/testfile", scratchDir)},
Token: "testtoken",
TaskAgentPath: testPath + " -test.run=TestOrchestrator",
},
extraChecks: []func(t *testing.T){
func(t *testing.T) {
_, err := os.Stat(scratchDir + "/testfile")
assert.NilError(t, err, "expected custom entrypoint to create file")
},
},
},
{
name: "finish within grace period",
config: defaultConfig,
timeout: 500 * time.Millisecond,
gracePeriod: 2 * time.Second,
wantError: "",
},
{
name: "zombie processes are reaped",
config: defaultConfig,
env: map[string]string{
"SIMULATE_A_ZOMBIE_PROCESS": scratchDir + "/task.pid",
},
extraChecks: []func(t *testing.T){
func(t *testing.T) {
b, err := os.ReadFile(scratchDir + "/task.pid") //nolint:gosec // this is a test
assert.NilError(t, err)
pid, err := strconv.Atoi(strings.TrimSpace(string(b)))
assert.NilError(t, err)
time.Sleep(500 * time.Millisecond)
p, err := os.FindProcess(pid)
assert.NilError(t, err)
err = p.Signal(syscall.Signal(0))
assert.Check(t, cmp.ErrorIs(err, os.ErrProcessDone))
},
},
},
{
name: "wait for service containers",
config: func() Config {
readinessFilePath := t.TempDir() + "/ready"
go func() {
_, _ = os.Create(readinessFilePath) //nolint:gosec
}()
c := defaultConfig
c.ReadinessFilePath = readinessFilePath
return c
}(),
wantError: "",
},
{
name: "error: interrupted task",
config: defaultConfig,
env: map[string]string{
"SIMULATE_RUNNING_A_TASK": "true",
},
timeout: 500 * time.Millisecond,
wantError: "error on shutdown: task agent process is still running, " +
"which could interrupt the task. Possible reasons include the Pod being evicted or deleted",
wantTaskEvents: []fakerunnerapi.TaskEvent{
{
Allocation: defaultConfig.Allocation,
TimestampMilli: time.Now().UnixMilli(),
Message: []byte("error on shutdown: task agent process is still running, " +
"which could interrupt the task. Possible reasons include the Pod being evicted or deleted: " +
"Check container logs for more details"),
},
},
},
{
name: "error: task agent encountered fatal error",
config: defaultConfig,
env: map[string]string{
"SIMULATE_FATAL_ERROR": "true",
},
wantError: "error while executing task agent: " +
"task agent command exited with an unexpected error: exit status 1",
wantTaskEvents: []fakerunnerapi.TaskEvent{
{
Allocation: defaultConfig.Allocation,
TimestampMilli: time.Now().UnixMilli(),
Message: []byte("error while executing task agent: " +
"task agent command exited with an unexpected error: exit status 1: fatal!!!: " +
"Check container logs for more details"),
},
},
},
{
name: "retryable error: task agent failed to start",
config: Config{
TaskID: "retry",
Token: "retry-token",
TaskAgentPath: "thiswontstart",
},
wantError: "",
wantTaskUnclaims: []fakerunnerapi.TaskUnclaim{
{
ID: "retry",
Token: "retry-token",
},
},
},
{
name: "retryable error: an unsafe retry",
config: Config{
TaskID: "retry",
EnableUnsafeRetries: true,
Token: "retry-token",
TaskAgentPath: defaultConfig.TaskAgentPath,
},
env: map[string]string{
"SIMULATE_FATAL_ERROR": "true",
},
wantError: "",
wantTaskUnclaims: []fakerunnerapi.TaskUnclaim{
{
ID: "retry",
Token: "retry-token",
},
},
},
{
name: "error: retryable, but exhausted all retries",
config: Config{
TaskID: "no-retry",
Token: "no-retry-token",
TaskAgentPath: "thiswontstart",
},
additionalTasks: []fakerunnerapi.Task{
{
ID: "no-retry",
Token: "no-retry-token",
UnclaimCount: 3,
},
},
wantError: "failed to retry task: exhausted all task retries",
wantTaskUnclaims: []fakerunnerapi.TaskUnclaim{
{
ID: "no-retry",
Token: "no-retry-token",
},
},
wantTaskEvents: []fakerunnerapi.TaskEvent{
{
TimestampMilli: time.Now().UnixMilli(),
Message: []byte("error while executing task agent: " +
"failed to start task agent command: " +
"exec: thiswontstart: executable file not found in $PATH: " +
"Check container logs for more details"),
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("BE_TASK_AGENT", "true")
for k, v := range tt.env {
t.Setenv(k, v)
}
ctx := testcontext.Background()
if tt.timeout == 0 {
tt.timeout = 20 * time.Second
}
ctx, cancel := context.WithTimeout(ctx, tt.timeout)
defer cancel()
c := tt.config
defaultTask := fakerunnerapi.Task{
ID: c.TaskID,
Token: c.Token,
Allocation: c.Allocation,
}
runnerAPI := fakerunnerapi.New(ctx, append(tt.additionalTasks, defaultTask))
server := httptest.NewServer(runnerAPI)
defer server.Close()
r := runner.NewClient(runner.ClientConfig{
BaseURL: server.URL,
AuthToken: c.Token,
})
o := NewOrchestrator(tt.config, r, tt.gracePeriod)
err := o.Run(ctx)
if tt.wantError != "" {
assert.Check(t, cmp.ErrorContains(err, tt.wantError))
} else {
assert.NilError(t, err)
}
assert.Check(t, cmp.DeepEqual(runnerAPI.TaskUnclaims(), tt.wantTaskUnclaims))
assert.Check(t, cmp.DeepEqual(runnerAPI.TaskEvents(), tt.wantTaskEvents, fakerunnerapi.CmpTaskEvent))
for _, check := range tt.extraChecks {
check(t)
}
})
}
}
func TestOrchestrator_waitForReadiness(t *testing.T) {
t.Run("readiness file already present", func(t *testing.T) {
ctx, cancel := context.WithTimeout(testcontext.Background(), 1*time.Second)
defer cancel()
o := Orchestrator{}
o.config.ReadinessFilePath = filepath.Join(t.TempDir(), "ready")
_, err := os.Create(o.config.ReadinessFilePath)
assert.NilError(t, err)
err = o.waitForReadiness(ctx)
assert.NilError(t, err)
})
t.Run("readiness file created later", func(t *testing.T) {
ctx, cancel := context.WithTimeout(testcontext.Background(), 2*time.Second)
defer cancel()
o := Orchestrator{}
o.config.ReadinessFilePath = filepath.Join(t.TempDir(), "ready")
go func() {
time.Sleep(250 * time.Millisecond)
_, err := os.Create(o.config.ReadinessFilePath)
assert.NilError(t, err)
}()
err := o.waitForReadiness(ctx)
assert.NilError(t, err)
})
t.Run("timed out", func(t *testing.T) {
ctx := testcontext.Background()
originalTimeout := waitForReadinessTimeout
waitForReadinessTimeout = 1 * time.Nanosecond
t.Cleanup(func() { waitForReadinessTimeout = originalTimeout })
o := Orchestrator{}
err := o.waitForReadiness(ctx)
assert.Check(t, cmp.ErrorContains(err, "context deadline exceeded"))
})
}
func beFakeTaskAgent(t *testing.T) {
t.Helper()
assert.Check(t, cmp.Equal(os.Args[2], "_internal"))
assert.Check(t, cmp.Equal(os.Args[3], "agent-runner"))
for _, env := range os.Environ() {
assert.Check(t, !strings.Contains(env, "CIRCLECI_GOAT"),
"orchestrator configuration shouldn't be in the task environment")
}
b, err := io.ReadAll(os.Stdin)
assert.NilError(t, err)
assert.Check(t, cmp.Equal(string(b), "testtoken"), "expected the task token on stdin")
if os.Getenv("SIMULATE_RUNNING_A_TASK") == "true" {
time.Sleep(30 * time.Second)
}
if os.Getenv("SIMULATE_FATAL_ERROR") == "true" {
_, _ = os.Stderr.WriteString("fatal!!!")
os.Exit(1)
}
if pidfile := os.Getenv("SIMULATE_A_ZOMBIE_PROCESS"); pidfile != "" {
c := exec.Command("/bin/sh", "-c", "echo $$ >"+pidfile+" && sleep 300") //nolint:gosec // this is a test
assert.NilError(t, c.Start())
}
}