Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

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.

Blame · ci/agent/images_test.go

Line attribution on main · 68 line(s)

3455902a
Keenan Nemetz
3d ago
1 package agent
2
3 import (
4 "context"
de3eec16
Keenan Nemetz
2h ago
5 "errors"
6 "io"
3455902a
Keenan Nemetz
3d ago
7 "testing"
8 "time"
de3eec16
Keenan Nemetz
2h ago
9
10 "rodent.software/vole/ci/execute"
3455902a
Keenan Nemetz
3d ago
11 )
12
de3eec16
Keenan Nemetz
2h ago
13 // stubExecutor is an executor whose only interesting behaviour is its prune, which
14 // is what the runner's image trimmer drives. The trimming policy belongs to the
15 // backend (ci/container tests it there); what is left here is the beat and the
16 // once-only complaint.
17 type stubExecutor struct {
18 calls int
19 err error
20 present bool
3455902a
Keenan Nemetz
3d ago
21 }
22
de3eec16
Keenan Nemetz
2h ago
23 func (s *stubExecutor) Capabilities() execute.Capabilities {
24 return execute.Capabilities{Backend: "stub"}
3455902a
Keenan Nemetz
3d ago
25 }
de3eec16
Keenan Nemetz
2h ago
26 func (s *stubExecutor) Probe(context.Context) error { return nil }
27 func (s *stubExecutor) Prepare(context.Context, execute.Image, io.Writer, execute.Runner) (string, func(), error) {
28 return "", nil, errors.New("not used")
3455902a
Keenan Nemetz
3d ago
29 }
de3eec16
Keenan Nemetz
2h ago
30 func (s *stubExecutor) Start(context.Context, execute.Spec) (execute.Sandbox, error) {
31 return execute.Nothing(), nil
32 }
33 func (s *stubExecutor) Reconcile(context.Context) error { return nil }
34 func (s *stubExecutor) Prune(_ context.Context, age time.Duration) (int64, error) {
35 s.calls++
36 if age <= 0 {
37 return 0, nil
3455902a
Keenan Nemetz
3d ago
38 }
de3eec16
Keenan Nemetz
2h ago
39 return 0, s.err
3455902a
Keenan Nemetz
3d ago
40 }
41
de3eec16
Keenan Nemetz
2h ago
42 // A backend that cannot prune is a line, not a retry every five minutes and not a
3455902a
Keenan Nemetz
3d ago
43 // runner that refuses to serve.
de3eec16
Keenan Nemetz
2h ago
44 func TestImageTrimmerStopsAfterTheExecutorRefuses(t *testing.T) {
45 exec := &stubExecutor{err: errors.New("the image store is not available")}
46 trimmer := newImageTrimmer(exec, time.Hour)
47 trimmer.trim(context.Background())
48 trimmer.trim(context.Background())
49 if exec.calls != 1 {
50 t.Fatalf("a refusing executor was asked %d times, want once", exec.calls)
3455902a
Keenan Nemetz
3d ago
51 }
52 if !trimmer.stopped {
de3eec16
Keenan Nemetz
2h ago
53 t.Fatal("the trimmer kept trying after the executor refused")
3455902a
Keenan Nemetz
3d ago
54 }
55 }
56
de3eec16
Keenan Nemetz
2h ago
57 // Zero is a policy ("keep every image"), not a very short age: the pass is never
58 // scheduled at all.
3455902a
Keenan Nemetz
3d ago
59 func TestImageTrimmerIsOffAtAgeZero(t *testing.T) {
de3eec16
Keenan Nemetz
2h ago
60 exec := &stubExecutor{}
61 trimmer := newImageTrimmer(exec, 0)
3455902a
Keenan Nemetz
3d ago
62 if trimmer.enabled() {
63 t.Fatal("age 0 enabled the image trimmer")
64 }
de3eec16
Keenan Nemetz
2h ago
65 if trimmer.age != 0 {
66 t.Fatalf("age = %s", trimmer.age)
3455902a
Keenan Nemetz
3d ago
67 }
68 }