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.
Put job execution behind an interface
de3eec162f93a697575c8bc756bd496fe4020376 · Keenan Nemetz · 2h ago · parent f784d769
Put job execution behind an interface Job execution was container-shaped all the way into the runner: ci/agent created the network, started the services and the job container, and held *container.Container and *container.Network values directly, and `vole workflow run` kept its own copy of the same orchestration. The only abstraction was Config.Runtime, a string naming one of three flag-compatible CLIs, which is enough for docker, podman and nerdctl and nothing else. ci/execute is that seam. An Executor prepares the job's image, starts its environment, runs one step while the caller keeps the lease alive, stops it, reclaims what a killed runner left behind, and bounds its own storage. A Sandbox is one job's environment: the job, the services beside it, and the network they share. Capabilities is what a backend can honour, so a job that asks for a network or for services is refused before it starts rather than quietly given less. ci/container implements it and takes over the orchestration that was in ci/agent and cmd/vole: the per-attempt network, the service containers and their log collection, the stale-container and stale-network reclamation, and the image build and prune coordination. The isolation flags are unchanged. This is the boundary a virtual-machine backend needs: a macOS guest behind Tart (#356), a Windows guest, or a Linux microVM disagree with a container about pids limits, user namespaces, tmpfs, capabilities and mounts, so they cannot be expressed as another runtime string. Nothing about the image or workflow schema changes here. closes: 01M8Z2M9PVKPNH923S3VJ5FR5E
ci/agent/sandbox_test.go +52−170
| @@ -4,56 +4,77 @@ import ( | |||
| 4 | 4 | "context" | |
| 5 | 5 | "os" | |
| 6 | 6 | "path/filepath" | |
| 7 | "strconv" | ||
| 8 | 7 | "strings" | |
| 9 | 8 | "testing" | |
| 10 | 9 | ||
| 11 | 10 | "rodent.software/vole/ci" | |
| 12 | 11 | "rodent.software/vole/ci/container" | |
| 12 | "rodent.software/vole/ci/execute" | ||
| 13 | 13 | ) | |
| 14 | 14 | ||
| 15 | func TestContainerOptionsRequireRuntime(t *testing.T) { | ||
| 16 | if _, err := containerOptions(Options{ContainerRuntime: "definitely-not-a-runtime"}, "/tmp/cache"); err == nil { | ||
| 15 | // capsFor is a container-shaped capability set for the network tests. | ||
| 16 | func capsFor(network string, services bool) execute.Capabilities { | ||
| 17 | return execute.Capabilities{ | ||
| 18 | Backend: "docker", | ||
| 19 | Network: network == ci.NetworkBridge, | ||
| 20 | Services: services, | ||
| 21 | Limits: true, | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | func TestNewRunnerBoxRequiresARuntime(t *testing.T) { | ||
| 26 | if _, err := newRunnerBox(Options{ContainerRuntime: "definitely-not-a-runtime"}, "/tmp/cache"); err == nil { | ||
| 17 | 27 | t.Fatal("an unknown container runtime was accepted") | |
| 18 | 28 | } | |
| 19 | 29 | } | |
| 20 | 30 | ||
| 21 | // The job's `network:` decides what the container gets, and the runner's own flag is | ||
| 22 | // only the default for a job that says nothing. A job that asks for more than the | ||
| 23 | // machine can give is refused before anything runs, rather than starting a build that | ||
| 24 | // cannot reach what it needs. | ||
| 31 | // The job's `network:` decides what the job gets, and the runner's own flag is only | ||
| 32 | // the default for a job that says nothing. A job that asks for more than the machine | ||
| 33 | // can give is refused before anything runs, rather than starting a build that cannot | ||
| 34 | // reach what it needs. | ||
| 35 | // | ||
| 36 | // The refusal is stated in terms of what the executor can do, not in terms of the | ||
| 37 | // container flags: that is what makes the same check correct for a backend whose | ||
| 38 | // network is not a container network at all. | ||
| 25 | 39 | func TestNetworkForUsesTheJobsRequest(t *testing.T) { | |
| 26 | 40 | t.Parallel() | |
| 27 | 41 | cases := []struct { | |
| 28 | 42 | name string | |
| 29 | 43 | runner string | |
| 44 | services bool | ||
| 30 | 45 | job string | |
| 31 | 46 | want string | |
| 32 | 47 | wantError string | |
| 33 | services []runnerService | ||
| 48 | declared []runnerService | ||
| 34 | 49 | }{ | |
| 35 | 50 | // A job that says nothing gets the runner's default, which is what every | |
| 36 | 51 | // workflow meant before the field existed. | |
| 37 | {"silent job on a bridge runner", ci.NetworkBridge, "", ci.NetworkBridge, "", nil}, | ||
| 38 | {"silent job on a closed runner", ci.NetworkNone, "", ci.NetworkNone, "", nil}, | ||
| 52 | {"silent job on a bridge runner", ci.NetworkBridge, true, "", ci.NetworkBridge, "", nil}, | ||
| 53 | {"silent job on a closed runner", ci.NetworkNone, true, "", ci.NetworkNone, "", nil}, | ||
| 39 | 54 | // An explicit request wins in both directions: a bridge runner can withhold | |
| 40 | 55 | // the network, and a closed runner never silently provides one. | |
| 41 | {"none on a bridge runner", ci.NetworkBridge, ci.NetworkNone, ci.NetworkNone, "", nil}, | ||
| 42 | {"none on a closed runner", ci.NetworkNone, ci.NetworkNone, ci.NetworkNone, "", nil}, | ||
| 43 | {"bridge on a bridge runner", ci.NetworkBridge, ci.NetworkBridge, ci.NetworkBridge, "", nil}, | ||
| 44 | {"bridge on a closed runner", ci.NetworkNone, ci.NetworkBridge, ci.NetworkNone, "network: bridge", nil}, | ||
| 56 | {"none on a bridge runner", ci.NetworkBridge, true, ci.NetworkNone, ci.NetworkNone, "", nil}, | ||
| 57 | {"none on a closed runner", ci.NetworkNone, true, ci.NetworkNone, ci.NetworkNone, "", nil}, | ||
| 58 | {"bridge on a bridge runner", ci.NetworkBridge, true, ci.NetworkBridge, ci.NetworkBridge, "", nil}, | ||
| 59 | {"bridge on a closed runner", ci.NetworkNone, true, ci.NetworkBridge, ci.NetworkNone, "network: bridge", nil}, | ||
| 45 | 60 | // A value the validator would refuse cannot arrive through a push, but the | |
| 46 | 61 | // runner checks the job it was handed rather than trusting the row. | |
| 47 | {"unknown value", ci.NetworkBridge, "host", ci.NetworkBridge, "not a network mode", nil}, | ||
| 62 | {"unknown value", ci.NetworkBridge, true, "host", ci.NetworkBridge, "not a network mode", nil}, | ||
| 48 | 63 | // A service lives on the job's network, so the job has to ask for one. | |
| 49 | {"services need bridge", ci.NetworkBridge, "", ci.NetworkBridge, "network: bridge", []runnerService{{ID: "db", Image: "postgres:17"}}}, | ||
| 50 | {"services with none", ci.NetworkBridge, ci.NetworkNone, ci.NetworkNone, "network: bridge", []runnerService{{ID: "db", Image: "postgres:17"}}}, | ||
| 51 | {"services with a bridge job", ci.NetworkBridge, ci.NetworkBridge, ci.NetworkBridge, "", []runnerService{{ID: "db", Image: "postgres:17"}}}, | ||
| 64 | {"services need bridge", ci.NetworkBridge, true, "", ci.NetworkBridge, "network: bridge", []runnerService{{ID: "db", Image: "postgres:17"}}}, | ||
| 65 | {"services with none", ci.NetworkBridge, true, ci.NetworkNone, ci.NetworkNone, "network: bridge", []runnerService{{ID: "db", Image: "postgres:17"}}}, | ||
| 66 | {"services with a bridge job", ci.NetworkBridge, true, ci.NetworkBridge, ci.NetworkBridge, "", []runnerService{{ID: "db", Image: "postgres:17"}}}, | ||
| 67 | // A backend that cannot start services beside a job says so before the job | ||
| 68 | // runs, rather than starting the job without the database it declares. | ||
| 69 | {"services on a backend without them", ci.NetworkBridge, false, ci.NetworkBridge, ci.NetworkBridge, "cannot start beside a job", []runnerService{{ID: "db", Image: "postgres:17"}}}, | ||
| 52 | 70 | } | |
| 53 | 71 | for _, c := range cases { | |
| 54 | 72 | t.Run(c.name, func(t *testing.T) { | |
| 55 | box := runnerBox{Config: container.Config{Network: c.runner}} | ||
| 56 | cfg, err := box.networkFor(&runnerJob{ID: "r1", Network: c.job, Services: c.services}) | ||
| 73 | box := runnerBox{ | ||
| 74 | cfg: container.Config{Network: c.runner}, | ||
| 75 | caps: capsFor(c.runner, c.services), | ||
| 76 | } | ||
| 77 | got, err := box.networkFor(&runnerJob{ID: "r1", Network: c.job, Services: c.declared}) | ||
| 57 | 78 | if c.wantError != "" { | |
| 58 | 79 | if err == nil { | |
| 59 | 80 | t.Fatalf("network %q on a %s runner was accepted", c.job, c.runner) | |
| @@ -66,155 +87,13 @@ func TestNetworkForUsesTheJobsRequest(t *testing.T) { | |||
| 66 | 87 | if err != nil { | |
| 67 | 88 | t.Fatalf("network %q on a %s runner: %v", c.job, c.runner, err) | |
| 68 | 89 | } | |
| 69 | if cfg.Network != c.want { | ||
| 70 | t.Fatalf("network %q on a %s runner = %q, want %q", c.job, c.runner, cfg.Network, c.want) | ||
| 90 | if got != c.want { | ||
| 91 | t.Fatalf("network %q on a %s runner = %q, want %q", c.job, c.runner, got, c.want) | ||
| 71 | 92 | } | |
| 72 | 93 | }) | |
| 73 | 94 | } | |
| 74 | 95 | } | |
| 75 | 96 | ||
| 76 | // The job sandbox stops the job first, reads a service's output, then removes the | ||
| 77 | // service and the network. The failure paths rely on this order: logs are gone once | ||
| 78 | // the container is removed, and a network with a member cannot be removed. | ||
| 79 | func TestJobSandboxCollectsServiceLogsBeforeStopping(t *testing.T) { | ||
| 80 | dir := t.TempDir() | ||
| 81 | state := filepath.Join(dir, "state") | ||
| 82 | if err := os.MkdirAll(state, 0o755); err != nil { | ||
| 83 | t.Fatal(err) | ||
| 84 | } | ||
| 85 | script := `#!/bin/sh | ||
| 86 | cmd="$1"; shift | ||
| 87 | state="${FAKE_STATE:?}" | ||
| 88 | case "$cmd" in | ||
| 89 | run) | ||
| 90 | name= | ||
| 91 | while [ $# -gt 0 ]; do | ||
| 92 | case "$1" in | ||
| 93 | --name) name="$2"; shift 2 ;; | ||
| 94 | *) shift ;; | ||
| 95 | esac | ||
| 96 | done | ||
| 97 | printf 'run %s\n' "$name" >> "$state/calls" | ||
| 98 | ;; | ||
| 99 | logs) | ||
| 100 | printf 'db says hi\n' | ||
| 101 | printf 'logs %s\n' "$1" >> "$state/calls" | ||
| 102 | ;; | ||
| 103 | rm) | ||
| 104 | for last in "$@"; do :; done | ||
| 105 | printf 'rm %s\n' "$last" >> "$state/calls" | ||
| 106 | ;; | ||
| 107 | network) | ||
| 108 | sub="$1"; shift | ||
| 109 | case "$sub" in | ||
| 110 | create) | ||
| 111 | name= | ||
| 112 | while [ $# -gt 0 ]; do | ||
| 113 | case "$1" in | ||
| 114 | --label) shift 2 ;; | ||
| 115 | *) name="$1"; shift ;; | ||
| 116 | esac | ||
| 117 | done | ||
| 118 | printf 'network create %s\n' "$name" >> "$state/calls" | ||
| 119 | ;; | ||
| 120 | rm) | ||
| 121 | printf 'network rm %s\n' "$1" >> "$state/calls" | ||
| 122 | ;; | ||
| 123 | esac | ||
| 124 | ;; | ||
| 125 | esac | ||
| 126 | ` | ||
| 127 | if err := os.WriteFile(filepath.Join(dir, "docker"), []byte(script), 0o755); err != nil { | ||
| 128 | t.Fatal(err) | ||
| 129 | } | ||
| 130 | t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH")) | ||
| 131 | t.Setenv("FAKE_STATE", state) | ||
| 132 | |||
| 133 | cfg, err := containerOptions(Options{ContainerRuntime: "docker"}, t.TempDir()) | ||
| 134 | if err != nil { | ||
| 135 | t.Fatal(err) | ||
| 136 | } | ||
| 137 | net, err := container.CreateNetwork(context.Background(), cfg.Config, "vole-test-net", map[string]string{"vole.runner.id": cfg.id}) | ||
| 138 | if err != nil { | ||
| 139 | t.Fatal(err) | ||
| 140 | } | ||
| 141 | svc, err := container.StartService(cfg.Config, container.ServiceSpec{ | ||
| 142 | Name: "svc-db", Alias: "db", Image: "postgres:17", | ||
| 143 | }) | ||
| 144 | if err != nil { | ||
| 145 | t.Fatal(err) | ||
| 146 | } | ||
| 147 | box := &jobSandbox{services: []*container.Container{svc}, serviceIDs: []string{"db"}, network: net} | ||
| 148 | var out strings.Builder | ||
| 149 | box.StopWithLogs(&out) | ||
| 150 | if got := out.String(); !strings.Contains(got, "=== service db ===") || !strings.Contains(got, "db says hi") { | ||
| 151 | t.Fatalf("service log = %q", got) | ||
| 152 | } | ||
| 153 | calls, err := os.ReadFile(filepath.Join(state, "calls")) | ||
| 154 | if err != nil { | ||
| 155 | t.Fatal(err) | ||
| 156 | } | ||
| 157 | order := string(calls) | ||
| 158 | logsAt := strings.Index(order, "logs svc-db") | ||
| 159 | rmAt := strings.Index(order, "rm svc-db") | ||
| 160 | netAt := strings.Index(order, "network rm vole-test-net") | ||
| 161 | if logsAt < 0 || rmAt < 0 || netAt < 0 || !(logsAt < rmAt && rmAt < netAt) { | ||
| 162 | t.Fatalf("wrong cleanup order:\n%s", order) | ||
| 163 | } | ||
| 164 | // A second stop must not re-read or re-remove anything. | ||
| 165 | out.Reset() | ||
| 166 | box.StopWithLogs(&out) | ||
| 167 | if out.Len() != 0 { | ||
| 168 | t.Fatalf("a second stop wrote %q", out.String()) | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | func TestCleanupStaleNetworksSkipsALiveRunner(t *testing.T) { | ||
| 173 | dir := t.TempDir() | ||
| 174 | state := filepath.Join(dir, "calls") | ||
| 175 | script := `#!/bin/sh | ||
| 176 | cmd="$1"; shift | ||
| 177 | case "$cmd" in | ||
| 178 | network) | ||
| 179 | sub="$1"; shift | ||
| 180 | case "$sub" in | ||
| 181 | ls) | ||
| 182 | printf 'dead-net\nlive-net\n' | ||
| 183 | ;; | ||
| 184 | inspect) | ||
| 185 | for last in "$@"; do :; done | ||
| 186 | case "$last" in | ||
| 187 | dead-net) printf '{"vole.runner.pid":"999999"}\n' ;; | ||
| 188 | live-net) printf '{"vole.runner.pid":"%s"}\n' "$FAKE_LIVE_PID" ;; | ||
| 189 | esac | ||
| 190 | ;; | ||
| 191 | rm) | ||
| 192 | printf 'network rm %s\n' "$1" >> "${FAKE_STATE:?}" | ||
| 193 | ;; | ||
| 194 | esac | ||
| 195 | ;; | ||
| 196 | esac | ||
| 197 | ` | ||
| 198 | if err := os.WriteFile(filepath.Join(dir, "docker"), []byte(script), 0o755); err != nil { | ||
| 199 | t.Fatal(err) | ||
| 200 | } | ||
| 201 | t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH")) | ||
| 202 | t.Setenv("FAKE_STATE", state) | ||
| 203 | t.Setenv("FAKE_LIVE_PID", strconv.Itoa(os.Getpid())) | ||
| 204 | |||
| 205 | cleanupStaleNetworks(runnerBox{Config: container.Config{Runtime: "docker"}, id: "runner1"}) | ||
| 206 | calls, err := os.ReadFile(state) | ||
| 207 | if err != nil { | ||
| 208 | t.Fatal(err) | ||
| 209 | } | ||
| 210 | if !strings.Contains(string(calls), "network rm dead-net") { | ||
| 211 | t.Fatalf("a dead runner's network was not removed:\n%s", calls) | ||
| 212 | } | ||
| 213 | if strings.Contains(string(calls), "network rm live-net") { | ||
| 214 | t.Fatalf("a live runner's network was removed:\n%s", calls) | ||
| 215 | } | ||
| 216 | } | ||
| 217 | |||
| 218 | 97 | // A runtime whose binary is on PATH but which cannot create a container is the | |
| 219 | 98 | // machine's problem, not the repository's: the probe runs before the first lease, so | |
| 220 | 99 | // no job is taken and none is recorded as a failure of a commit that never ran a | |
| @@ -241,20 +120,23 @@ exit 125 | |||
| 241 | 120 | } | |
| 242 | 121 | } | |
| 243 | 122 | ||
| 244 | // The size of the job's /tmp is the operator's, and it reaches the container settings | ||
| 245 | // the runner actually uses. | ||
| 246 | func TestContainerOptionsCarryTheTmpfsLimit(t *testing.T) { | ||
| 123 | // The size of the job's /tmp is the operator's, and it reaches the settings the | ||
| 124 | // executor actually uses. | ||
| 125 | func TestRunnerBoxCarriesTheTmpfsLimit(t *testing.T) { | ||
| 247 | 126 | dir := t.TempDir() | |
| 248 | 127 | if err := os.WriteFile(filepath.Join(dir, "docker"), []byte("#!/bin/sh\nexit 0\n"), 0o755); err != nil { | |
| 249 | 128 | t.Fatal(err) | |
| 250 | 129 | } | |
| 251 | 130 | t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH")) | |
| 252 | 131 | ||
| 253 | box, err := containerOptions(Options{ContainerRuntime: "docker", ContainerTmpfs: "3g"}, t.TempDir()) | ||
| 132 | box, err := newRunnerBox(Options{ContainerRuntime: "docker", ContainerTmpfs: "3g"}, t.TempDir()) | ||
| 254 | 133 | if err != nil { | |
| 255 | 134 | t.Fatal(err) | |
| 256 | 135 | } | |
| 257 | if box.Tmpfs != "3g" { | ||
| 258 | t.Fatalf("tmpfs = %q, want 3g", box.Tmpfs) | ||
| 136 | if box.cfg.Tmpfs != "3g" { | ||
| 137 | t.Fatalf("tmpfs = %q, want 3g", box.cfg.Tmpfs) | ||
| 138 | } | ||
| 139 | if box.caps.Backend != "docker" { | ||
| 140 | t.Fatalf("backend = %q, want docker", box.caps.Backend) | ||
| 259 | 141 | } | |
| 260 | 142 | } | |