vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
| 1 | package container |
| 2 | |
| 3 | // The backend is what the runner and `vole workflow run` talk to, so these tests |
| 4 | // drive it the way a caller does: through execute.Executor, with a fake runtime on |
| 5 | // PATH standing in for a container engine. Nothing here starts a real container. |
| 6 | |
| 7 | import ( |
| 8 | "context" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "strconv" |
| 12 | "strings" |
| 13 | "sync" |
| 14 | "testing" |
| 15 | "time" |
| 16 | |
| 17 | "rodent.software/vole/ci" |
| 18 | "rodent.software/vole/ci/execute" |
| 19 | ) |
| 20 | |
| 21 | // fakeRuntime puts a script on PATH under the runtime's name and returns a |
| 22 | // directory the script writes what it was asked to do into. |
| 23 | // newFakeRuntime is the backend tests' fixture; container_test.go has its own, which |
| 24 | // installs a script under a named runtime instead. |
| 25 | func newFakeRuntime(t *testing.T, script string) string { |
| 26 | t.Helper() |
| 27 | dir := t.TempDir() |
| 28 | state := filepath.Join(dir, "state") |
| 29 | if err := os.MkdirAll(state, 0o755); err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | if err := os.WriteFile(filepath.Join(dir, "docker"), []byte(script), 0o755); err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH")) |
| 36 | t.Setenv("FAKE_STATE", state) |
| 37 | return state |
| 38 | } |
| 39 | |
| 40 | func calls(t *testing.T, state string) string { |
| 41 | t.Helper() |
| 42 | data, err := os.ReadFile(filepath.Join(state, "calls")) |
| 43 | if os.IsNotExist(err) { |
| 44 | return "" |
| 45 | } |
| 46 | if err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | return string(data) |
| 50 | } |
| 51 | |
| 52 | // testBackend is a Backend with docker on PATH and a fresh cache identity. |
| 53 | func testBackend(t *testing.T, pull string) *Backend { |
| 54 | t.Helper() |
| 55 | cfg, err := Validate(Config{Runtime: "docker", Pull: pull}) |
| 56 | if err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | return New(cfg, "runner1") |
| 60 | } |
| 61 | |
| 62 | // A runtime that answers `image inspect` for a tag it has built, and `build` for one |
| 63 | // it has not. A build can be made slow so two callers overlap, or made to fail. |
| 64 | func fakeImageRuntime(t *testing.T, sleep string, fail bool) string { |
| 65 | t.Helper() |
| 66 | state := newFakeRuntime(t, `#!/bin/sh |
| 67 | cmd="$1"; shift |
| 68 | state="${FAKE_STATE:?}" |
| 69 | case "$cmd" in |
| 70 | image) |
| 71 | tag="$2" |
| 72 | m=$(printf '%s' "$tag" | tr '/:' '__') |
| 73 | [ -f "$state/$m" ] && exit 0 |
| 74 | exit 1 |
| 75 | ;; |
| 76 | build) |
| 77 | tag=; df= |
| 78 | while [ $# -gt 0 ]; do |
| 79 | case "$1" in |
| 80 | -t) tag=$2; shift 2 ;; |
| 81 | -f) df=$2; shift 2 ;; |
| 82 | *) shift ;; |
| 83 | esac |
| 84 | done |
| 85 | printf 'build\n' >> "$state/builds" |
| 86 | cp "$df" "$state/Dockerfile.captured" |
| 87 | sleep "${FAKE_BUILD_SLEEP:-0}" |
| 88 | [ "${FAKE_BUILD_FAIL:-0}" = 0 ] || exit 1 |
| 89 | m=$(printf '%s' "$tag" | tr '/:' '__') |
| 90 | : > "$state/$m" |
| 91 | ;; |
| 92 | esac |
| 93 | `) |
| 94 | t.Setenv("FAKE_BUILD_SLEEP", sleep) |
| 95 | if fail { |
| 96 | t.Setenv("FAKE_BUILD_FAIL", "1") |
| 97 | } |
| 98 | return state |
| 99 | } |
| 100 | |
| 101 | func buildCount(t *testing.T, state string) int { |
| 102 | t.Helper() |
| 103 | builds, err := os.ReadFile(filepath.Join(state, "builds")) |
| 104 | if os.IsNotExist(err) { |
| 105 | return 0 |
| 106 | } |
| 107 | if err != nil { |
| 108 | t.Fatal(err) |
| 109 | } |
| 110 | return strings.Count(string(builds), "build") |
| 111 | } |
| 112 | |
| 113 | func imageJob() execute.Image { |
| 114 | content := "version: 1\nfrom: alpine:3.22\nenv:\n X: \"1\"\nrun:\n - echo hello\n" |
| 115 | return execute.Image{File: "go", Definition: content, Digest: ci.ImageDigest(content)} |
| 116 | } |
| 117 | |
| 118 | func TestCapabilitiesNameTheRuntimeAndItsOffering(t *testing.T) { |
| 119 | newFakeRuntime(t, "#!/bin/sh\nexit 0\n") |
| 120 | closed := testBackend(t, "missing") |
| 121 | if got := closed.Capabilities(); got.Backend != "docker" || got.Network || !got.Services || !got.Limits { |
| 122 | t.Fatalf("closed capabilities = %+v", got) |
| 123 | } |
| 124 | open, err := Validate(Config{Runtime: "docker", Network: "bridge"}) |
| 125 | if err != nil { |
| 126 | t.Fatal(err) |
| 127 | } |
| 128 | if got := New(open, "r").Capabilities(); !got.Network { |
| 129 | t.Fatalf("a bridge runner does not report a network: %+v", got) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // A definition is built once, tagged by its digest, and reused by a later job. The |
| 134 | // fake runtime records the Dockerfile the backend generated and the build count. |
| 135 | func TestPrepareBuildsAndCachesDefinition(t *testing.T) { |
| 136 | state := fakeImageRuntime(t, "0", false) |
| 137 | b := testBackend(t, "missing") |
| 138 | in := imageJob() |
| 139 | |
| 140 | var out strings.Builder |
| 141 | tag, release, err := b.Prepare(context.Background(), in, &out, execute.Direct{}) |
| 142 | if err != nil { |
| 143 | t.Fatalf("Prepare: %v", err) |
| 144 | } |
| 145 | if release == nil { |
| 146 | t.Fatal("Prepare returned no release") |
| 147 | } |
| 148 | release() |
| 149 | if !strings.HasPrefix(tag, "vole/image-file:go-") { |
| 150 | t.Fatalf("tag = %q", tag) |
| 151 | } |
| 152 | captured, err := os.ReadFile(filepath.Join(state, "Dockerfile.captured")) |
| 153 | if err != nil { |
| 154 | t.Fatal(err) |
| 155 | } |
| 156 | for _, want := range []string{"FROM alpine:3.22", `ENV X="1"`, `RUN ["/bin/sh", "-c", "echo hello"]`} { |
| 157 | if !strings.Contains(string(captured), want) { |
| 158 | t.Fatalf("Dockerfile does not contain %q:\n%s", want, captured) |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // The second call sees the digest tag and does not build again. |
| 163 | out.Reset() |
| 164 | again, release2, err := b.Prepare(context.Background(), in, &out, execute.Direct{}) |
| 165 | if err != nil || again != tag { |
| 166 | t.Fatalf("cached prepare = %q, %v; want %q", again, err, tag) |
| 167 | } |
| 168 | release2() |
| 169 | if !strings.Contains(out.String(), "using cached") { |
| 170 | t.Fatalf("second build was not cached:\n%s", out.String()) |
| 171 | } |
| 172 | if n := buildCount(t, state); n != 1 { |
| 173 | t.Fatalf("build count = %d, want 1", n) |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // A registry reference is used as written and costs no build. |
| 178 | func TestPrepareUsesARegistryReferenceAsWritten(t *testing.T) { |
| 179 | fakeImageRuntime(t, "0", false) |
| 180 | b := testBackend(t, "missing") |
| 181 | ref, release, err := b.Prepare(context.Background(), execute.Image{Ref: "alpine:3.22"}, &strings.Builder{}, execute.Direct{}) |
| 182 | if err != nil { |
| 183 | t.Fatalf("Prepare: %v", err) |
| 184 | } |
| 185 | release() |
| 186 | if ref != "alpine:3.22" { |
| 187 | t.Fatalf("ref = %q", ref) |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // A failed Prepare must not leave the image in flight, or a prune pass would give |
| 192 | // way to a job that is not running. |
| 193 | func TestPrepareReleasesTheImageWhenItFails(t *testing.T) { |
| 194 | fakeImageRuntime(t, "0", true) |
| 195 | b := testBackend(t, "missing") |
| 196 | if _, release, err := b.Prepare(context.Background(), imageJob(), &strings.Builder{}, execute.Direct{}); err == nil { |
| 197 | t.Fatal("a failing build was reported as success") |
| 198 | } else if release != nil { |
| 199 | t.Fatal("a failed Prepare returned a release") |
| 200 | } |
| 201 | release, ok := b.images.exclusive() |
| 202 | if !ok { |
| 203 | t.Fatal("a failed Prepare left the image in flight") |
| 204 | } |
| 205 | release() |
| 206 | } |
| 207 | |
| 208 | // Two slots, one digest: the first builds it, the second joins that build instead of |
| 209 | // spending the time and the disk again. Both end up with the same tag. |
| 210 | func TestTwoJobsPrepareOneImage(t *testing.T) { |
| 211 | state := fakeImageRuntime(t, "0.3", false) |
| 212 | b := testBackend(t, "missing") |
| 213 | in := imageJob() |
| 214 | |
| 215 | var ( |
| 216 | wg sync.WaitGroup |
| 217 | tags [2]string |
| 218 | errs [2]error |
| 219 | logs [2]strings.Builder |
| 220 | ) |
| 221 | start := make(chan struct{}) |
| 222 | for i := range tags { |
| 223 | wg.Add(1) |
| 224 | go func(i int) { |
| 225 | defer wg.Done() |
| 226 | <-start |
| 227 | var release func() |
| 228 | tags[i], release, errs[i] = b.Prepare(context.Background(), in, &logs[i], execute.Direct{}) |
| 229 | if release != nil { |
| 230 | release() |
| 231 | } |
| 232 | }(i) |
| 233 | } |
| 234 | close(start) |
| 235 | wg.Wait() |
| 236 | |
| 237 | for i := range tags { |
| 238 | if errs[i] != nil { |
| 239 | t.Fatalf("job %d: %v", i, errs[i]) |
| 240 | } |
| 241 | if tags[i] != tags[0] { |
| 242 | t.Fatalf("two jobs got two tags: %q and %q", tags[0], tags[i]) |
| 243 | } |
| 244 | } |
| 245 | if n := buildCount(t, state); n != 1 { |
| 246 | t.Fatalf("%d builds for one digest, want 1", n) |
| 247 | } |
| 248 | // One of them waited, and said so in its own log: a reader of that job's log has |
| 249 | // to be able to tell why the steps had not started. |
| 250 | waited := strings.Contains(logs[0].String(), "being built by another job") || |
| 251 | strings.Contains(logs[1].String(), "being built by another job") |
| 252 | if !waited { |
| 253 | t.Fatalf("neither job said it was waiting:\n%s\n%s", logs[0].String(), logs[1].String()) |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // A build that fails fails every job waiting on it: the same definition would fail |
| 258 | // the same way, and a second attempt only spends the time again. |
| 259 | func TestAFailedBuildFailsTheJobsWaitingOnIt(t *testing.T) { |
| 260 | fakeImageRuntime(t, "0.2", true) |
| 261 | b := testBackend(t, "missing") |
| 262 | in := imageJob() |
| 263 | |
| 264 | var ( |
| 265 | wg sync.WaitGroup |
| 266 | errs [2]error |
| 267 | ) |
| 268 | start := make(chan struct{}) |
| 269 | for i := range errs { |
| 270 | wg.Add(1) |
| 271 | go func(i int) { |
| 272 | defer wg.Done() |
| 273 | <-start |
| 274 | _, release, err := b.Prepare(context.Background(), in, &strings.Builder{}, execute.Direct{}) |
| 275 | if release != nil { |
| 276 | release() |
| 277 | } |
| 278 | errs[i] = err |
| 279 | }(i) |
| 280 | } |
| 281 | close(start) |
| 282 | wg.Wait() |
| 283 | |
| 284 | for i := range errs { |
| 285 | if errs[i] == nil { |
| 286 | t.Fatalf("job %d saw a successful build", i) |
| 287 | } |
| 288 | if !strings.Contains(errs[i].Error(), "go") { |
| 289 | t.Fatalf("job %d: %v does not name the image-file", i, errs[i]) |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | // The map is the whole mechanism, so its edges are worth pinning: one builder at a |
| 295 | // time, and a finished build is forgotten rather than answered forever. |
| 296 | func TestImageBuildsJoinAndLeave(t *testing.T) { |
| 297 | t.Parallel() |
| 298 | builds := &imageBuilds{} |
| 299 | first, mine := builds.join("tag") |
| 300 | if !mine { |
| 301 | t.Fatal("the first join is not the builder") |
| 302 | } |
| 303 | second, mine := builds.join("tag") |
| 304 | if mine || second != first { |
| 305 | t.Fatal("the second join started its own build of the same digest") |
| 306 | } |
| 307 | other, mine := builds.join("another") |
| 308 | if !mine || other == first { |
| 309 | t.Fatal("a different digest joined the wrong build") |
| 310 | } |
| 311 | builds.leave("tag") |
| 312 | again, mine := builds.join("tag") |
| 313 | if !mine || again == first { |
| 314 | t.Fatal("a forgotten digest was not rebuilt") |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | // sandboxRuntime answers the verbs a job with one service needs, recording the order |
| 319 | // of the calls. FAKE_SERVICE_RUNNING says whether the service looks up. |
| 320 | func sandboxRuntime(t *testing.T, running bool) string { |
| 321 | t.Helper() |
| 322 | state := newFakeRuntime(t, `#!/bin/sh |
| 323 | cmd="$1"; shift |
| 324 | state="${FAKE_STATE:?}" |
| 325 | case "$cmd" in |
| 326 | run) |
| 327 | name= |
| 328 | while [ $# -gt 0 ]; do |
| 329 | case "$1" in |
| 330 | --name) name="$2"; shift 2 ;; |
| 331 | --network) printf '%s\n' "$2" >> "$state/net.$2"; shift 2 ;; |
| 332 | --label) |
| 333 | case "$2" in |
| 334 | vole.runner.pid=*) printf '%s\n' "$2" >> "$state/labels" ;; |
| 335 | esac |
| 336 | shift 2 |
| 337 | ;; |
| 338 | *) shift ;; |
| 339 | esac |
| 340 | done |
| 341 | printf 'run %s\n' "$name" >> "$state/calls" |
| 342 | ;; |
| 343 | inspect) |
| 344 | for last in "$@"; do :; done |
| 345 | printf '%s' "${FAKE_RUNNING:-true}" |
| 346 | printf 'inspect %s\n' "$last" >> "$state/calls" |
| 347 | ;; |
| 348 | logs) |
| 349 | printf 'db says hi\n' |
| 350 | printf 'logs %s\n' "$1" >> "$state/calls" |
| 351 | ;; |
| 352 | rm) |
| 353 | for last in "$@"; do :; done |
| 354 | printf 'rm %s\n' "$last" >> "$state/calls" |
| 355 | ;; |
| 356 | network) |
| 357 | sub="$1"; shift |
| 358 | case "$sub" in |
| 359 | create) |
| 360 | name= |
| 361 | while [ $# -gt 0 ]; do |
| 362 | case "$1" in |
| 363 | --label) shift 2 ;; |
| 364 | *) name="$1"; shift ;; |
| 365 | esac |
| 366 | done |
| 367 | printf 'network create %s\n' "$name" >> "$state/calls" |
| 368 | ;; |
| 369 | rm) |
| 370 | printf 'network rm %s\n' "$1" >> "$state/calls" |
| 371 | ;; |
| 372 | esac |
| 373 | ;; |
| 374 | exec) |
| 375 | printf 'exec\n' >> "$state/calls" |
| 376 | ;; |
| 377 | esac |
| 378 | `) |
| 379 | t.Setenv("FAKE_RUNNING", map[bool]string{true: "true", false: "false"}[running]) |
| 380 | return state |
| 381 | } |
| 382 | |
| 383 | func jobSpec(dir string) execute.Spec { |
| 384 | return execute.Spec{ |
| 385 | Name: "vole-run1-build-1", |
| 386 | Image: "alpine:3.22", |
| 387 | Work: execute.Share{Host: dir, Path: "/workspace"}, |
| 388 | Shares: []execute.Share{{Host: dir, Path: "/cache"}}, |
| 389 | Network: ci.NetworkBridge, |
| 390 | Services: []execute.Service{ |
| 391 | {ID: "db", Image: "postgres:17", Env: map[string]string{"A": "1"}}, |
| 392 | }, |
| 393 | Labels: map[string]string{"vole.run": "run1"}, |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | // Start is the whole attempt: the network first, then each service by name, then the |
| 398 | // job. A step reaches the service by its declared name, which is why the names are |
| 399 | // derived from the attempt and not from anything the workflow wrote. |
| 400 | func TestStartCreatesNetworkServicesThenJob(t *testing.T) { |
| 401 | state := sandboxRuntime(t, true) |
| 402 | b := testBackend(t, "missing") |
| 403 | sb, err := b.Start(context.Background(), jobSpec(t.TempDir())) |
| 404 | if err != nil { |
| 405 | t.Fatalf("Start: %v", err) |
| 406 | } |
| 407 | defer sb.Stop() |
| 408 | order := calls(t, state) |
| 409 | want := []string{ |
| 410 | "network create vole-run1-build-1-net", |
| 411 | "run vole-run1-build-1-svc-db", |
| 412 | "run vole-run1-build-1", |
| 413 | } |
| 414 | last := -1 |
| 415 | for _, w := range want { |
| 416 | at := strings.Index(order, w) |
| 417 | if at < 0 { |
| 418 | t.Fatalf("missing %q:\n%s", w, order) |
| 419 | } |
| 420 | if at < last { |
| 421 | t.Fatalf("%q is out of order:\n%s", w, order) |
| 422 | } |
| 423 | last = at |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | // The caller resolves the job's network request; the backend applies it, and asks |
| 428 | // for nothing else. This is what keeps a local run and a pushed run from disagreeing |
| 429 | // about a job that asked for none. |
| 430 | func TestStartAppliesTheJobsNetworkMode(t *testing.T) { |
| 431 | state := sandboxRuntime(t, true) |
| 432 | b := testBackend(t, "missing") |
| 433 | spec := jobSpec(t.TempDir()) |
| 434 | spec.Services = nil |
| 435 | spec.Network = ci.NetworkNone |
| 436 | sb, err := b.Start(context.Background(), spec) |
| 437 | if err != nil { |
| 438 | t.Fatalf("Start: %v", err) |
| 439 | } |
| 440 | defer sb.Stop() |
| 441 | if _, err := os.Stat(filepath.Join(state, "net.none")); err != nil { |
| 442 | t.Fatalf("the job container was not started with --network none: %v", err) |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | // The attempt's labels carry this process, which is what Reconcile matches on: a |
| 447 | // caller cannot know the pid, and without it a live sibling's containers look |
| 448 | // abandoned after a restart. |
| 449 | func TestStartLabelsCarryThisProcess(t *testing.T) { |
| 450 | state := sandboxRuntime(t, true) |
| 451 | b := testBackend(t, "missing") |
| 452 | spec := jobSpec(t.TempDir()) |
| 453 | spec.Services = nil |
| 454 | sb, err := b.Start(context.Background(), spec) |
| 455 | if err != nil { |
| 456 | t.Fatalf("Start: %v", err) |
| 457 | } |
| 458 | defer sb.Stop() |
| 459 | labels, err := os.ReadFile(filepath.Join(state, "labels")) |
| 460 | if err != nil { |
| 461 | t.Fatalf("no runner label on the job container: %v", err) |
| 462 | } |
| 463 | if want := "vole.runner.pid=" + strconv.Itoa(os.Getpid()) + "\n"; string(labels) != want { |
| 464 | t.Fatalf("labels = %q, want %q", labels, want) |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | // A step runs in the job tree, in the environment's own shell, with the secrets file |
| 469 | // sourced first: the values are never an argument. |
| 470 | func TestExecRunsInTheJobTreeWithTheSecretsSourced(t *testing.T) { |
| 471 | sandboxRuntime(t, true) |
| 472 | b := testBackend(t, "missing") |
| 473 | spec := jobSpec(t.TempDir()) |
| 474 | spec.SecretsFile = "/run/vole/secrets.env" |
| 475 | sb, err := b.Start(context.Background(), spec) |
| 476 | if err != nil { |
| 477 | t.Fatalf("Start: %v", err) |
| 478 | } |
| 479 | defer sb.Stop() |
| 480 | cmd := sb.Exec(context.Background(), "echo hi") |
| 481 | joined := strings.Join(cmd.Args, "\x00") |
| 482 | if !strings.Contains(joined, "/bin/sh\x00-c") { |
| 483 | t.Fatalf("step is not a shell: %q", cmd.Args) |
| 484 | } |
| 485 | script := cmd.Args[len(cmd.Args)-1] |
| 486 | if !strings.HasPrefix(script, ". /run/vole/secrets.env\necho hi") { |
| 487 | t.Fatalf("the step does not source the secrets first: %q", script) |
| 488 | } |
| 489 | if !strings.Contains(joined, "\x00-w\x00/workspace\x00") { |
| 490 | t.Fatalf("the step does not run in the job tree: %q", cmd.Args) |
| 491 | } |
| 492 | if cmd.Dir != spec.Work.Host { |
| 493 | t.Fatalf("client directory = %q, want the job tree", cmd.Dir) |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | // The stop order is load-bearing: logs are gone once the service is removed, and a |
| 498 | // network with a member cannot be removed. |
| 499 | func TestServiceLogsBeforeStopThenNetwork(t *testing.T) { |
| 500 | state := sandboxRuntime(t, true) |
| 501 | b := testBackend(t, "missing") |
| 502 | sb, err := b.Start(context.Background(), jobSpec(t.TempDir())) |
| 503 | if err != nil { |
| 504 | t.Fatalf("Start: %v", err) |
| 505 | } |
| 506 | logs := sb.ServiceLogs(context.Background(), execute.MaxServiceLogBytes) |
| 507 | if len(logs) != 1 || logs[0].ID != "db" || !strings.Contains(string(logs[0].Data), "db says hi") { |
| 508 | t.Fatalf("service logs = %+v", logs) |
| 509 | } |
| 510 | sb.Stop() |
| 511 | order := calls(t, state) |
| 512 | logsAt := strings.Index(order, "logs vole-run1-build-1-svc-db") |
| 513 | rmAt := strings.Index(order, "rm vole-run1-build-1-svc-db") |
| 514 | netAt := strings.Index(order, "network rm vole-run1-build-1-net") |
| 515 | if logsAt < 0 || rmAt < 0 || netAt < 0 || !(logsAt < rmAt && rmAt < netAt) { |
| 516 | t.Fatalf("wrong cleanup order:\n%s", order) |
| 517 | } |
| 518 | // A second stop must not re-read or re-remove anything. |
| 519 | before := calls(t, state) |
| 520 | sb.Stop() |
| 521 | if after := calls(t, state); after != before { |
| 522 | t.Fatalf("a second stop did more work:\n%s", after[len(before):]) |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | // A service that exited before the steps begin is a failed job, not a connection |
| 527 | // error in the first step that talks to it. Start still returns the sandbox so the |
| 528 | // caller can put the service's output in the attempt. |
| 529 | func TestStartRefusesAServiceThatExited(t *testing.T) { |
| 530 | sandboxRuntime(t, false) |
| 531 | b := testBackend(t, "missing") |
| 532 | sb, err := b.Start(context.Background(), jobSpec(t.TempDir())) |
| 533 | if err == nil || !strings.Contains(err.Error(), "exited before the steps started") { |
| 534 | t.Fatalf("Start = %v", err) |
| 535 | } |
| 536 | if sb == nil { |
| 537 | t.Fatal("Start returned no sandbox to stop") |
| 538 | } |
| 539 | sb.Stop() |
| 540 | } |
| 541 | |
| 542 | // A killed runner's leftovers are removed, a live sibling's are not: the pid in the |
| 543 | // label is what tells them apart. |
| 544 | func TestReconcileSkipsALiveRunnersNetwork(t *testing.T) { |
| 545 | state := newFakeRuntime(t, `#!/bin/sh |
| 546 | cmd="$1"; shift |
| 547 | case "$cmd" in |
| 548 | network) |
| 549 | sub="$1"; shift |
| 550 | case "$sub" in |
| 551 | ls) printf 'dead-net\nlive-net\n' ;; |
| 552 | inspect) |
| 553 | for last in "$@"; do :; done |
| 554 | case "$last" in |
| 555 | dead-net) printf '{"vole.runner.pid":"999999"}\n' ;; |
| 556 | live-net) printf '{"vole.runner.pid":"%s"}\n' "$FAKE_LIVE_PID" ;; |
| 557 | esac |
| 558 | ;; |
| 559 | rm) printf 'network rm %s\n' "$1" >> "$FAKE_STATE/calls" ;; |
| 560 | esac |
| 561 | ;; |
| 562 | ps) printf 'ps\n' >> "$FAKE_STATE/calls" ;; |
| 563 | esac |
| 564 | `) |
| 565 | t.Setenv("FAKE_LIVE_PID", strconv.Itoa(os.Getpid())) |
| 566 | b := testBackend(t, "missing") |
| 567 | if err := b.Reconcile(context.Background()); err != nil { |
| 568 | t.Fatalf("Reconcile: %v", err) |
| 569 | } |
| 570 | got := calls(t, state) |
| 571 | if !strings.Contains(got, "network rm dead-net") { |
| 572 | t.Fatalf("a dead runner's network was not removed:\n%s", got) |
| 573 | } |
| 574 | if strings.Contains(got, "network rm live-net") { |
| 575 | t.Fatalf("a live runner's network was removed:\n%s", got) |
| 576 | } |
| 577 | if !strings.Contains(got, "ps") { |
| 578 | t.Fatalf("containers were not reconciled:\n%s", got) |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | // fakePruneRuntime records how the backend asked the runtime to prune. |
| 583 | func fakePruneRuntime(t *testing.T) string { |
| 584 | t.Helper() |
| 585 | log := filepath.Join(t.TempDir(), "prune.log") |
| 586 | newFakeRuntime(t, `#!/bin/sh |
| 587 | printf '%s\n' "$*" >> "$FAKE_PRUNE_LOG" |
| 588 | [ "${FAKE_PRUNE_FAIL:-0}" = 0 ] || { |
| 589 | echo 'Error: the image store is not available' >&2 |
| 590 | exit 1 |
| 591 | } |
| 592 | echo 'Total reclaimed space: 1.5GB' |
| 593 | `) |
| 594 | t.Setenv("FAKE_PRUNE_LOG", log) |
| 595 | return log |
| 596 | } |
| 597 | |
| 598 | func pruneCalls(t *testing.T, log string) []string { |
| 599 | t.Helper() |
| 600 | data, err := os.ReadFile(log) |
| 601 | if os.IsNotExist(err) { |
| 602 | return nil |
| 603 | } |
| 604 | if err != nil { |
| 605 | t.Fatal(err) |
| 606 | } |
| 607 | return strings.Split(strings.TrimSpace(string(data)), "\n") |
| 608 | } |
| 609 | |
| 610 | // The policy is the runtime's own age filter, and the scope depends on whether the |
| 611 | // runner could fetch an image again: a never-pull runner keeps every tag. |
| 612 | func TestPruneUsesTheRuntimesOwnAgeFilter(t *testing.T) { |
| 613 | log := fakePruneRuntime(t) |
| 614 | b := testBackend(t, "missing") |
| 615 | if _, err := b.Prune(context.Background(), 24*time.Hour); err != nil { |
| 616 | t.Fatalf("Prune: %v", err) |
| 617 | } |
| 618 | got := pruneCalls(t, log) |
| 619 | if len(got) != 1 || !strings.Contains(got[0], "until=24h0m0s") || !strings.Contains(got[0], "--all") { |
| 620 | t.Fatalf("prune calls = %q, want one aged, all-images prune", got) |
| 621 | } |
| 622 | |
| 623 | if err := os.Remove(log); err != nil { |
| 624 | t.Fatal(err) |
| 625 | } |
| 626 | never := testBackend(t, "never") |
| 627 | if _, err := never.Prune(context.Background(), 24*time.Hour); err != nil { |
| 628 | t.Fatalf("Prune: %v", err) |
| 629 | } |
| 630 | got = pruneCalls(t, log) |
| 631 | if len(got) != 1 || strings.Contains(got[0], "--all") { |
| 632 | t.Fatalf("never-pull prune calls = %q, want dangling layers only", got) |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | // A pass that races a job preparing or starting an image gives way. The lease is the |
| 637 | // thing that must not lose; the next beat tries again. |
| 638 | func TestPruneGivesWayToAnImageInFlight(t *testing.T) { |
| 639 | log := fakePruneRuntime(t) |
| 640 | b := testBackend(t, "missing") |
| 641 | release := b.images.hold() |
| 642 | if _, err := b.Prune(context.Background(), time.Hour); err != nil { |
| 643 | t.Fatalf("Prune: %v", err) |
| 644 | } |
| 645 | if got := pruneCalls(t, log); len(got) != 0 { |
| 646 | t.Fatalf("prune ran while a job held the image guard: %q", got) |
| 647 | } |
| 648 | release() |
| 649 | if _, err := b.Prune(context.Background(), time.Hour); err != nil { |
| 650 | t.Fatalf("Prune: %v", err) |
| 651 | } |
| 652 | if got := pruneCalls(t, log); len(got) != 1 { |
| 653 | t.Fatalf("prune did not run after the job released the guard: %q", got) |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | // Zero is a policy ("keep every image"), not a very short age. |
| 658 | func TestPruneAtAgeZeroKeepsEverything(t *testing.T) { |
| 659 | log := fakePruneRuntime(t) |
| 660 | b := testBackend(t, "missing") |
| 661 | if _, err := b.Prune(context.Background(), 0); err != nil { |
| 662 | t.Fatalf("Prune: %v", err) |
| 663 | } |
| 664 | if got := pruneCalls(t, log); len(got) != 0 { |
| 665 | t.Fatalf("age 0 pruned images: %q", got) |
| 666 | } |
| 667 | } |
| 668 |