| 1 | package release |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "encoding/hex" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | const ( |
| 14 | finishVersion = "abc1234" |
| 15 | keptID = "KEPTINSTANCE00000000000000" |
| 16 | ) |
| 17 | |
| 18 | // finishRig is production as the 2026-11-19 cutover left it: steps 1 to 5 |
| 19 | // recorded, the instance serving finishVersion on the kept database, the |
| 20 | // old runner stopped with its old origin and files. scripts/cutover-finish-thin.sh |
| 21 | // runs against stub carlos, systemctl, journalctl and curl on a temporary |
| 22 | // HOME; carlos db (the replica) fails if anything calls it. |
| 23 | type finishRig struct { |
| 24 | t *testing.T |
| 25 | root, home, runner string |
| 26 | repo, log string |
| 27 | serviceEnv, unit string |
| 28 | } |
| 29 | |
| 30 | func newFinishRig(t *testing.T) *finishRig { |
| 31 | t.Helper() |
| 32 | root := t.TempDir() |
| 33 | r := &finishRig{t: t, root: root, home: filepath.Join(root, "home"), log: filepath.Join(root, "calls.log"), repo: filepath.Join(root, "repo")} |
| 34 | r.runner = filepath.Join(r.home, ".local/share/equipo-runner") |
| 35 | for _, name := range []string{"cutover-finish-thin.sh", "cutover-thin.sh", "upgrade-thin.sh", "runner-config.py"} { |
| 36 | src, err := os.ReadFile("../../scripts/" + name) |
| 37 | r.must(err) |
| 38 | r.write(filepath.Join(r.repo, "scripts", name), string(src), 0700) |
| 39 | } |
| 40 | release := filepath.Join(r.repo, "releases", "equipo-runner-linux-amd64-"+finishVersion) |
| 41 | runner := "#!/bin/sh\n" + rendererStub(r.log) + "if [ \"$1\" = verify-installed ]; then echo \"equipo-runner verify-installed $EQUIPO_TOOL_PROXY_PATH\" >> " + r.log + "; exit; fi\necho \"equipo-runner $1 $EQUIPO_INSTANCE_ID $EQUIPO_ORIGIN\" >> " + r.log + "\n[ -e " + filepath.Join(root, "enrolfail") + " ] && exit 1\n# The running runner holds the state lock enrol needs.\n[ -e " + filepath.Join(root, "active") + " ] && { echo state locked >&2; exit 1; }\necho new > \"$EQUIPO_RUNNER_STATE_DIR/credentials.json\"\n" + linkedScanner + linkedRenderer |
| 42 | r.write(filepath.Join(release, "equipo-runner"), runner, 0700) |
| 43 | r.write(filepath.Join(release, RendererArchive), string(rendererArchiveBytes), 0600) |
| 44 | r.write(filepath.Join(release, "equipo-tool-proxy"), "proxy", 0700) |
| 45 | r.write(filepath.Join(release, "gitleaks"), scannerBytes, 0700) |
| 46 | sum := func(s string) string { h := sha256.Sum256([]byte(s)); return hex.EncodeToString(h[:]) } |
| 47 | r.write(filepath.Join(r.repo, "releases", "staged", finishVersion+".json"), `{"staging": "PASS", "sha256": {"runner": "`+sum(runner)+`", "tool_proxy": "`+sum("proxy")+`", "gitleaks": "`+sum(scannerBytes)+`"}}`, 0600) |
| 48 | r.serviceEnv = "EQUIPO_ORIGIN=\"https://equipo.bds.oncarlos.com\"\nEQUIPO_INSTANCE_ID=\"" + keptID + "\"\nEQUIPO_TOOL_PROXY_PATH=\"/old/equipo-tool-proxy\"\nEQUIPO_RUNNER_STATE_DIR=\"" + filepath.Join(r.runner, "state") + "\"\n" |
| 49 | r.unit = "[Service]\nEnvironmentFile=" + filepath.Join(r.runner, "service.env") + "\nExecStart=/old/equipo-runner run\nRestart=on-failure\nRestartSec=5\n" |
| 50 | r.write(filepath.Join(r.runner, "service.env"), r.serviceEnv, 0600) |
| 51 | r.write(filepath.Join(r.runner, "equipo-runner.service"), r.unit, 0600) |
| 52 | r.write(filepath.Join(r.runner, "state", "credentials.json"), "old", 0600) |
| 53 | backups := filepath.Join(r.home, ".local/share/equipo-cutover") |
| 54 | r.write(filepath.Join(backups, "cutover.state"), "version="+finishVersion+"\nstamp=20261119T140850Z\nprev_version=prev999\nprev_always_on=1\nprev_database=-unset-\nprev_origin=https://equipo.bds.oncarlos.com\nold_instance_id="+keptID+"\ndone_preflight=1\ndone_export=1\ndone_config=1\ndone_deploy=1\n", 0600) |
| 55 | r.write(filepath.Join(backups, "prior", "service.env"), r.serviceEnv, 0600) |
| 56 | r.write(filepath.Join(backups, "prior", "equipo-runner.service"), r.unit, 0600) |
| 57 | r.write(filepath.Join(root, "served"), finishVersion+"\n", 0600) |
| 58 | r.setInstall("https://equipo.oficina.works", finishVersion, keptID) |
| 59 | r.must(os.MkdirAll(filepath.Join(root, "proc", "4242"), 0700)) |
| 60 | r.must(os.Symlink(filepath.Join(r.runner, "releases", finishVersion, "equipo-runner"), filepath.Join(root, "proc", "4242", "exe"))) |
| 61 | bin := filepath.Join(root, "bin") |
| 62 | log := "echo \"$(basename $0) $*\" >> " + r.log + "\n" |
| 63 | r.write(filepath.Join(bin, "curl"), "#!/bin/sh\n"+log+`case "$*" in |
| 64 | */install.sh) cat `+filepath.Join(root, "install.sh")+` ;; |
| 65 | *) cat `+filepath.Join(root, "served")+` ;; |
| 66 | esac |
| 67 | `, 0700) |
| 68 | unitStubs(r.write, root, filepath.Join(r.runner, "equipo-runner.service"), r.log) |
| 69 | // mv and install fail once when asked to (root/failmv, root/failinstallstate): |
| 70 | // a step interrupted half way. |
| 71 | r.write(filepath.Join(bin, "mv"), "#!/bin/sh\nif [ -e "+filepath.Join(root, "failmv")+" ]; then rm "+filepath.Join(root, "failmv")+"; exit 1; fi\nexec /usr/bin/mv \"$@\"\n", 0700) |
| 72 | r.write(filepath.Join(bin, "install"), "#!/bin/sh\ncase \"$*\" in *equipo-runner/state) if [ -e "+filepath.Join(root, "failinstallstate")+" ]; then rm "+filepath.Join(root, "failinstallstate")+"; exit 1; fi ;; esac\nexec /usr/bin/install \"$@\"\n", 0700) |
| 73 | r.write(filepath.Join(bin, "carlos"), "#!/bin/sh\n"+log+`case "$1 $2" in |
| 74 | "env list") printf 'using console\nKEY VALUE\nEQUIPO_DATABASE equipo-v1.db\nEQUIPO_ORIGIN https://equipo.oficina.works\n' ;; |
| 75 | db*) echo "the copy failed its integrity check" >&2; exit 1 ;; |
| 76 | esac |
| 77 | `, 0700) |
| 78 | return r |
| 79 | } |
| 80 | |
| 81 | // setInstall is what the instance's /install.sh says. |
| 82 | func (r *finishRig) setInstall(origin, version, instance string) { |
| 83 | r.write(filepath.Join(r.root, "install.sh"), "#!/bin/sh\nset -eu\ncurl --fail '"+origin+"/releases/"+version+"/install-v1.sh' | sh -s -- '"+origin+"' '"+version+"' '"+instance+"'\n", 0600) |
| 84 | } |
| 85 | |
| 86 | func (r *finishRig) must(err error) { |
| 87 | r.t.Helper() |
| 88 | if err != nil { |
| 89 | r.t.Fatal(err) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | func (r *finishRig) write(path, body string, mode os.FileMode) { |
| 94 | r.t.Helper() |
| 95 | r.must(os.MkdirAll(filepath.Dir(path), 0700)) |
| 96 | r.must(os.WriteFile(path, []byte(body), mode)) |
| 97 | } |
| 98 | |
| 99 | func (r *finishRig) read(path string) string { |
| 100 | raw, _ := os.ReadFile(path) |
| 101 | return string(raw) |
| 102 | } |
| 103 | |
| 104 | func (r *finishRig) run(script string, args ...string) (string, error) { |
| 105 | cmd := exec.Command("bash", append([]string{filepath.Join(r.repo, "scripts", script)}, args...)...) |
| 106 | cmd.Env = []string{"HOME=" + r.home, "PATH=" + filepath.Join(r.root, "bin") + ":/usr/bin:/bin", "CUTOVER_YES=1", "CUTOVER_TRIES=3", "CUTOVER_WAIT=0", "CUTOVER_PROC=" + filepath.Join(r.root, "proc"), "LANG=C.UTF-8"} |
| 107 | out, err := cmd.CombinedOutput() |
| 108 | return string(out), err |
| 109 | } |
| 110 | |
| 111 | func (r *finishRig) state() string { |
| 112 | return r.read(filepath.Join(r.home, ".local/share/equipo-cutover/cutover.state")) |
| 113 | } |
| 114 | |
| 115 | // The plan changes nothing, never reads the replica, and lists the steps. |
| 116 | func TestCutoverFinishPlanChangesNothing(t *testing.T) { |
| 117 | r := newFinishRig(t) |
| 118 | state := r.state() |
| 119 | out, err := r.run("cutover-finish-thin.sh") |
| 120 | if err != nil { |
| 121 | t.Fatalf("plan: %v\n%s", err, out) |
| 122 | } |
| 123 | inOrder(t, out, "instance "+keptID, "== 6 identity", "sh -s -- 'https://equipo.oficina.works' '"+finishVersion+"' '"+keptID+"'", |
| 124 | "== 7 config", "env unset --app equipo --account oficina --console https://console.carloku.com EQUIPO_DATABASE", |
| 125 | "== 8 install", "runner-config.py render", "EQUIPO_INSTANCE_ID="+keptID, "EQUIPO_ORIGIN=https://equipo.oficina.works", "state.pre-thin-20261119T140850Z", |
| 126 | "== 9 enrol", "runner-config.py check", "verify-installed", "enrol", "== 10 start", "systemctl --user start", "== 11 verify", "_SYSTEMD_INVOCATION_ID=", "msg=welcome", "Plan only.") |
| 127 | if strings.Contains(out, "would refuse") || strings.Contains(out, "carlos db") { |
| 128 | t.Fatalf("plan refuses or reads the replica:\n%s", out) |
| 129 | } |
| 130 | if r.read(r.log) != "" || r.state() != state || r.read(filepath.Join(r.runner, "service.env")) != r.serviceEnv { |
| 131 | t.Fatalf("the plan changed something:\n%s", r.read(r.log)) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // The finish moves the runner to the public origin with the kept instance |
| 136 | // id, enrols it, verifies through the instance alone, and records the |
| 137 | // steps so the upgrade accepts the cutover as finished. |
| 138 | func TestCutoverFinishOnTheKeptDatabase(t *testing.T) { |
| 139 | r := newFinishRig(t) |
| 140 | out, err := r.run("cutover-finish-thin.sh", "--execute") |
| 141 | if err != nil { |
| 142 | t.Fatalf("finish: %v\n%s", err, out) |
| 143 | } |
| 144 | calls := r.read(r.log) |
| 145 | inOrder(t, calls, "install.sh", "env unset --app equipo --account oficina --console https://console.carloku.com EQUIPO_DATABASE", "install.sh", |
| 146 | "daemon-reload", "equipo-runner verify-installed "+filepath.Join(r.runner, "releases", finishVersion, "equipo-tool-proxy"), |
| 147 | "equipo-runner enrol "+keptID+" https://equipo.oficina.works", "systemctl --user start", "install.sh", "journalctl --user -b _SYSTEMD_INVOCATION_ID=0123456789abcdef0123456789abcdef _PID=4242") |
| 148 | for _, never := range []string{"carlos db", "promote", "deploy", "env set"} { |
| 149 | if strings.Contains(calls, never) { |
| 150 | t.Fatalf("finish ran %q:\n%s", never, calls) |
| 151 | } |
| 152 | } |
| 153 | env := r.read(filepath.Join(r.runner, "service.env")) |
| 154 | if !strings.Contains(env, "EQUIPO_ORIGIN=\"https://equipo.oficina.works\"") || !strings.Contains(env, "EQUIPO_INSTANCE_ID=\""+keptID+"\"") { |
| 155 | t.Fatalf("runner config not moved:\n%s", env) |
| 156 | } |
| 157 | if r.read(filepath.Join(r.runner, "state.pre-thin-20261119T140850Z", "credentials.json")) != "old" { |
| 158 | t.Fatal("old runner state not kept aside") |
| 159 | } |
| 160 | st := r.state() |
| 161 | for _, want := range []string{"kept_database=1", "new_instance_id=" + keptID, "done_identity=1", "done_unset=1", "done_runner=1", "done_enrol=1", "done_verify=1"} { |
| 162 | if !strings.Contains(st, want) { |
| 163 | t.Fatalf("cutover.state lacks %q:\n%s", want, st) |
| 164 | } |
| 165 | } |
| 166 | if out, err = r.run("upgrade-thin.sh", "bbb2222"); err != nil || strings.Contains(out, "would refuse") { |
| 167 | t.Fatalf("upgrade does not accept the finished cutover: %v\n%s", err, out) |
| 168 | } |
| 169 | if out, err = r.run("cutover-finish-thin.sh", "--execute"); err == nil || !strings.Contains(out, "complete") { |
| 170 | t.Fatalf("second finish allowed: %v\n%s", err, out) |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // Anything that does not show the kept database, or a cutover not at its |
| 175 | // deploy, stops the finish before it changes anything. |
| 176 | func TestCutoverFinishRefusesBeforeChangingAnything(t *testing.T) { |
| 177 | for name, set := range map[string]func(r *finishRig){ |
| 178 | "another instance id": func(r *finishRig) { |
| 179 | r.setInstall("https://equipo.oficina.works", finishVersion, "FRESHINSTANCE0000000000000") |
| 180 | }, |
| 181 | "the old origin": func(r *finishRig) { r.setInstall("https://equipo.bds.oncarlos.com", finishVersion, keptID) }, |
| 182 | "another version": func(r *finishRig) { r.write(filepath.Join(r.root, "served"), "ccc3333\n", 0600) }, |
| 183 | "other runner bytes": func(r *finishRig) { |
| 184 | r.write(filepath.Join(r.repo, "releases", "equipo-runner-linux-amd64-"+finishVersion, "equipo-runner"), "#!/bin/sh\n", 0700) |
| 185 | }, |
| 186 | "unstaged scanner": func(r *finishRig) { |
| 187 | r.write(filepath.Join(r.repo, "releases", "staged", finishVersion+".json"), strings.Replace(r.read(filepath.Join(r.repo, "releases", "staged", finishVersion+".json")), `"gitleaks": "`, `"gitleaks": "0`, 1), 0600) |
| 188 | }, |
| 189 | "unstaged version": func(r *finishRig) { |
| 190 | r.write(filepath.Join(r.repo, "releases", "staged", finishVersion+".json"), `{"staging": "FAIL"}`, 0600) |
| 191 | }, |
| 192 | "no deploy": func(r *finishRig) { |
| 193 | r.write(filepath.Join(r.home, ".local/share/equipo-cutover/cutover.state"), strings.Replace(r.state(), "done_deploy=1\n", "", 1), 0600) |
| 194 | }, |
| 195 | } { |
| 196 | t.Run(name, func(t *testing.T) { |
| 197 | r := newFinishRig(t) |
| 198 | r.t = t |
| 199 | set(r) |
| 200 | state := r.state() |
| 201 | out, err := r.run("cutover-finish-thin.sh", "--execute") |
| 202 | if err == nil || !strings.Contains(out, "refused") { |
| 203 | t.Fatalf("not refused: %v\n%s", err, out) |
| 204 | } |
| 205 | for _, change := range []string{"env unset", "systemctl", "enrol"} { |
| 206 | if strings.Contains(r.read(r.log), change) { |
| 207 | t.Fatalf("changed something (%s) before refusing:\n%s", change, r.read(r.log)) |
| 208 | } |
| 209 | } |
| 210 | if r.state() != state || r.read(filepath.Join(r.runner, "service.env")) != r.serviceEnv || r.read(filepath.Join(r.runner, "state", "credentials.json")) != "old" { |
| 211 | t.Fatal("state or runner changed before refusing") |
| 212 | } |
| 213 | }) |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // Verify needs the staged bytes running and the instance's welcome to that |
| 218 | // process in its log; once both hold, the same run resumes and finishes. |
| 219 | func TestCutoverFinishVerifiesThroughTheInstance(t *testing.T) { |
| 220 | r := newFinishRig(t) |
| 221 | r.write(filepath.Join(r.root, "journal"), "msg=\"runner stream\" err=unauthorized\n", 0600) |
| 222 | if out, err := r.run("cutover-finish-thin.sh", "--execute"); err == nil || !strings.Contains(out, "not connected") { |
| 223 | t.Fatalf("verified without a welcome: %v\n%s", err, out) |
| 224 | } |
| 225 | r.write(filepath.Join(r.root, "journal"), "msg=welcome generation=2\n", 0600) |
| 226 | exe := filepath.Join(r.root, "proc", "4242", "exe") |
| 227 | r.must(os.Remove(exe)) |
| 228 | r.must(os.Symlink("/bin/sh", exe)) |
| 229 | if out, err := r.run("cutover-finish-thin.sh", "--execute"); err == nil || !strings.Contains(out, "not the staged") { |
| 230 | t.Fatalf("accepted another running binary: %v\n%s", err, out) |
| 231 | } |
| 232 | if strings.Contains(r.state(), "done_verify=1") { |
| 233 | t.Fatal("verify recorded after a failed check") |
| 234 | } |
| 235 | r.must(os.Remove(exe)) |
| 236 | r.must(os.Symlink(filepath.Join(r.runner, "releases", finishVersion, "equipo-runner"), exe)) |
| 237 | if out, err := r.run("cutover-finish-thin.sh", "--execute"); err != nil || strings.Count(r.read(r.log), "equipo-runner enrol") != 1 { |
| 238 | t.Fatalf("did not resume past finished steps: %v\n%s\n%s", err, out, r.read(r.log)) |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | // Rollback puts the runner back as the cutover left it, stopped, offers |
| 243 | // no earlier version, and the upgrade and the cutover's own rollback then |
| 244 | // refuse. |
| 245 | func TestCutoverFinishRollbackOffersNoEarlierVersion(t *testing.T) { |
| 246 | r := newFinishRig(t) |
| 247 | if out, err := r.run("cutover-finish-thin.sh", "--execute"); err != nil { |
| 248 | t.Fatalf("finish: %v\n%s", err, out) |
| 249 | } |
| 250 | before := r.read(r.log) |
| 251 | out, err := r.run("cutover-finish-thin.sh", "rollback", "--execute") |
| 252 | if err != nil { |
| 253 | t.Fatalf("rollback: %v\n%s", err, out) |
| 254 | } |
| 255 | if r.read(filepath.Join(r.runner, "service.env")) != r.serviceEnv || r.read(filepath.Join(r.runner, "equipo-runner.service")) != r.unit || r.read(filepath.Join(r.runner, "state", "credentials.json")) != "old" { |
| 256 | t.Fatal("runner not put back exactly") |
| 257 | } |
| 258 | calls := strings.TrimPrefix(r.read(r.log), before) |
| 259 | for _, never := range []string{"promote", "deploy", "env set", "systemctl --user start"} { |
| 260 | if strings.Contains(calls, never) { |
| 261 | t.Fatalf("rollback ran %q:\n%s", never, calls) |
| 262 | } |
| 263 | } |
| 264 | if !strings.Contains(out, "prev999 is not offered") || !strings.Contains(out, "cannot read the migrated schema") { |
| 265 | t.Fatalf("rollback does not say why the earlier version is not offered:\n%s", out) |
| 266 | } |
| 267 | if out, err = r.run("upgrade-thin.sh", "bbb2222"); err != nil || !strings.Contains(out, "would refuse: the cutover has not finished") { |
| 268 | t.Fatalf("upgrade accepts a rolled-back finish: %v\n%s", err, out) |
| 269 | } |
| 270 | if out, err = r.run("cutover-thin.sh", "rollback", "--execute"); err == nil || !strings.Contains(out, "prev999 cannot read it") { |
| 271 | t.Fatalf("the cutover's rollback offers prev999: %v\n%s", err, out) |
| 272 | } |
| 273 | if out, err = r.run("cutover-finish-thin.sh", "rollback", "--execute"); err == nil { |
| 274 | t.Fatalf("second rollback allowed: %s", out) |
| 275 | } |
| 276 | if out, err = r.run("cutover-finish-thin.sh", "--execute"); err != nil { |
| 277 | t.Fatalf("finish again after rollback: %v\n%s", err, out) |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | // finished asserts the finish ended with the runner moved, the old state |
| 282 | // kept aside, and the original runner files recorded. |
| 283 | func (r *finishRig) finished(out string, err error) { |
| 284 | r.t.Helper() |
| 285 | if err != nil { |
| 286 | r.t.Fatalf("finish: %v\n%s", err, out) |
| 287 | } |
| 288 | if !strings.Contains(r.read(filepath.Join(r.runner, "service.env")), "EQUIPO_ORIGIN=\"https://equipo.oficina.works\"") || |
| 289 | r.read(filepath.Join(r.runner, "state.pre-thin-20261119T140850Z", "credentials.json")) != "old" || |
| 290 | r.read(filepath.Join(r.runner, "state", "credentials.json")) != "new\n" || !strings.Contains(r.state(), "done_verify=1") { |
| 291 | r.t.Fatalf("not finished:\n%s\n%s", out, r.state()) |
| 292 | } |
| 293 | snap := filepath.Join(r.home, ".local/share/equipo-cutover/finish-20261119T140850Z") |
| 294 | if r.read(filepath.Join(snap, "service.env")) != r.serviceEnv || r.read(filepath.Join(snap, "equipo-runner.service")) != r.unit { |
| 295 | r.t.Fatal("the recorded runner files are not the originals") |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | // Step 8 interrupted anywhere resumes: after the files are written but |
| 300 | // before the state moves, after the move but before the new state exists, |
| 301 | // after the new state exists, and after a move nobody recorded. |
| 302 | func TestCutoverFinishRunnerStepResumes(t *testing.T) { |
| 303 | for _, flag := range []string{"failmv", "failinstallstate", "failreload"} { |
| 304 | t.Run(flag, func(t *testing.T) { |
| 305 | r := newFinishRig(t) |
| 306 | r.t = t |
| 307 | r.write(filepath.Join(r.root, flag), "", 0600) |
| 308 | if out, err := r.run("cutover-finish-thin.sh", "--execute"); err == nil || strings.Contains(r.state(), "done_runner=1") { |
| 309 | t.Fatalf("step 8 did not stop: %v\n%s", err, out) |
| 310 | } |
| 311 | r.finished(r.run("cutover-finish-thin.sh", "--execute")) |
| 312 | }) |
| 313 | } |
| 314 | t.Run("unrecorded move", func(t *testing.T) { |
| 315 | r := newFinishRig(t) |
| 316 | r.t = t |
| 317 | r.must(os.Rename(filepath.Join(r.runner, "state"), filepath.Join(r.runner, "state.pre-thin-20261119T140850Z"))) |
| 318 | r.finished(r.run("cutover-finish-thin.sh", "--execute")) |
| 319 | }) |
| 320 | t.Run("both states and no record", func(t *testing.T) { |
| 321 | r := newFinishRig(t) |
| 322 | r.t = t |
| 323 | r.write(filepath.Join(r.runner, "state.pre-thin-20261119T140850Z", "credentials.json"), "older", 0600) |
| 324 | out, err := r.run("cutover-finish-thin.sh", "--execute") |
| 325 | if err == nil || !strings.Contains(out, "no move is recorded") || r.read(filepath.Join(r.runner, "state", "credentials.json")) != "old" { |
| 326 | t.Fatalf("went on with two runner states: %v\n%s", err, out) |
| 327 | } |
| 328 | }) |
| 329 | } |
| 330 | |
| 331 | // A record that cannot be written stops the run at once. |
| 332 | func TestCutoverFinishStopsWhenItCannotRecord(t *testing.T) { |
| 333 | r := newFinishRig(t) |
| 334 | r.must(os.Chmod(filepath.Join(r.home, ".local/share/equipo-cutover/cutover.state"), 0400)) |
| 335 | out, err := r.run("cutover-finish-thin.sh", "--execute") |
| 336 | if err == nil || !strings.Contains(out, "could not record") || strings.Contains(r.read(r.log), "env unset") { |
| 337 | t.Fatalf("went on without its record: %v\n%s", err, out) |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | // The runner's files must hold each expected entry exactly once; the |
| 342 | // written and the loaded configuration are checked again before enrolment. |
| 343 | func TestCutoverFinishChecksTheRunnerConfig(t *testing.T) { |
| 344 | for name, set := range map[string]func(r *finishRig){ |
| 345 | "origin twice": func(r *finishRig) { |
| 346 | r.serviceEnv += "EQUIPO_ORIGIN=\"https://elsewhere.example.com\"\n" |
| 347 | r.write(filepath.Join(r.runner, "service.env"), r.serviceEnv, 0600) |
| 348 | }, |
| 349 | "no instance id": func(r *finishRig) { |
| 350 | r.serviceEnv = strings.Replace(r.serviceEnv, "EQUIPO_INSTANCE_ID=\""+keptID+"\"\n", "", 1) |
| 351 | r.write(filepath.Join(r.runner, "service.env"), r.serviceEnv, 0600) |
| 352 | }, |
| 353 | "no RestartSec": func(r *finishRig) { |
| 354 | r.unit = strings.Replace(r.unit, "RestartSec=5\n", "", 1) |
| 355 | r.write(filepath.Join(r.runner, "equipo-runner.service"), r.unit, 0600) |
| 356 | }, |
| 357 | } { |
| 358 | t.Run(name, func(t *testing.T) { |
| 359 | r := newFinishRig(t) |
| 360 | r.t = t |
| 361 | set(r) |
| 362 | out, err := r.run("cutover-finish-thin.sh", "--execute") |
| 363 | if err == nil || !strings.Contains(out, "not one") || strings.Contains(r.read(r.log), "equipo-runner enrol") || r.read(filepath.Join(r.runner, "state", "credentials.json")) != "old" { |
| 364 | t.Fatalf("went on with an unexpected runner config: %v\n%s", err, out) |
| 365 | } |
| 366 | }) |
| 367 | } |
| 368 | t.Run("changed before enrol", func(t *testing.T) { |
| 369 | r := newFinishRig(t) |
| 370 | r.t = t |
| 371 | r.write(filepath.Join(r.root, "enrolfail"), "", 0600) |
| 372 | if out, err := r.run("cutover-finish-thin.sh", "--execute"); err == nil || !strings.Contains(r.state(), "done_runner=1") { |
| 373 | t.Fatalf("enrol failure not reached: %v\n%s", err, out) |
| 374 | } |
| 375 | r.must(os.Remove(filepath.Join(r.root, "enrolfail"))) |
| 376 | env := filepath.Join(r.runner, "service.env") |
| 377 | r.write(env, strings.Replace(r.read(env), "https://equipo.oficina.works", "https://equipo.bds.oncarlos.com", 1), 0600) |
| 378 | before := strings.Count(r.read(r.log), "equipo-runner enrol") |
| 379 | out, err := r.run("cutover-finish-thin.sh", "--execute") |
| 380 | if err == nil || !strings.Contains(out, "does not hold EQUIPO_ORIGIN") || strings.Count(r.read(r.log), "equipo-runner enrol") != before { |
| 381 | t.Fatalf("enrolled with a changed config: %v\n%s", err, out) |
| 382 | } |
| 383 | }) |
| 384 | t.Run("another unit loaded", func(t *testing.T) { |
| 385 | r := newFinishRig(t) |
| 386 | r.t = t |
| 387 | r.write(filepath.Join(r.root, "loaded"), "{ path=/old/equipo-runner ; argv[]=/old/equipo-runner run }\n", 0600) |
| 388 | out, err := r.run("cutover-finish-thin.sh", "--execute") |
| 389 | if err == nil || !strings.Contains(out, "the loaded equipo-runner.service does not start") || strings.Contains(r.read(r.log), "equipo-runner enrol") { |
| 390 | t.Fatalf("enrolled with another unit loaded: %v\n%s", err, out) |
| 391 | } |
| 392 | }) |
| 393 | t.Run("unsafe value", func(t *testing.T) { |
| 394 | cmd := exec.Command("python3", "../../scripts/runner-config.py", "check", t.TempDir(), "--exec", "/x/equipo-runner", "EQUIPO_ORIGIN=https://a|b") |
| 395 | if out, err := cmd.CombinedOutput(); err == nil || !strings.Contains(string(out), "unexpected characters") { |
| 396 | t.Fatalf("accepted a value with a replacement character: %v %s", err, out) |
| 397 | } |
| 398 | }) |
| 399 | } |
| 400 | |
| 401 | // Verified means connected now, in this invocation. |
| 402 | func TestCutoverFinishVerifiesTheCurrentConnection(t *testing.T) { |
| 403 | for name, set := range map[string]func(r *finishRig){ |
| 404 | "dropped since": func(r *finishRig) { |
| 405 | r.write(filepath.Join(r.root, "journal"), "msg=welcome generation=1\nmsg=\"runner stream ended\" retry_in=1s\n", 0600) |
| 406 | }, |
| 407 | "another invocation each time": func(r *finishRig) { r.write(filepath.Join(r.root, "flap"), "", 0600) }, |
| 408 | } { |
| 409 | t.Run(name, func(t *testing.T) { |
| 410 | r := newFinishRig(t) |
| 411 | r.t = t |
| 412 | set(r) |
| 413 | out, err := r.run("cutover-finish-thin.sh", "--execute") |
| 414 | if err == nil || !strings.Contains(out, "not connected") || strings.Contains(r.state(), "done_verify=1") { |
| 415 | t.Fatalf("verified without a current connection: %v\n%s", err, out) |
| 416 | } |
| 417 | }) |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | // A rollback that leaves the runner running is not recorded. |
| 422 | func TestCutoverFinishRollbackChecksTheRunnerStopped(t *testing.T) { |
| 423 | r := newFinishRig(t) |
| 424 | if out, err := r.run("cutover-finish-thin.sh", "--execute"); err != nil { |
| 425 | t.Fatalf("finish: %v\n%s", err, out) |
| 426 | } |
| 427 | r.write(filepath.Join(r.root, "stuck"), "", 0600) |
| 428 | if out, err := r.run("cutover-finish-thin.sh", "rollback", "--execute"); err == nil || !strings.Contains(out, "still running") || strings.Contains(r.state(), "kept_database=0") { |
| 429 | t.Fatalf("rollback recorded with the runner running: %v\n%s", err, out) |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | // A run cut off right after the unit starts resumes forward: enrolment is |
| 434 | // already recorded, so enrol (which the running runner's state lock would |
| 435 | // refuse) does not run again, and the unit is not started twice. |
| 436 | func TestCutoverFinishResumesAfterTheStart(t *testing.T) { |
| 437 | r := newFinishRig(t) |
| 438 | r.write(filepath.Join(r.root, "killafterstart"), "", 0600) |
| 439 | if out, err := r.run("cutover-finish-thin.sh", "--execute"); err == nil { |
| 440 | t.Fatalf("the run was not interrupted:\n%s", out) |
| 441 | } |
| 442 | if !strings.Contains(r.state(), "done_enrol=1") || strings.Contains(r.state(), "done_start=1") { |
| 443 | t.Fatalf("enrol not recorded before the start:\n%s", r.state()) |
| 444 | } |
| 445 | r.finished(r.run("cutover-finish-thin.sh", "--execute")) |
| 446 | if n := strings.Count(r.read(r.log), "equipo-runner enrol"); n != 1 { |
| 447 | t.Fatalf("enrol ran %d times", n) |
| 448 | } |
| 449 | } |
| 450 | |