| 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 | "time" |
| 12 | ) |
| 13 | |
| 14 | const ( |
| 15 | upgradeFrom = "aaa1111" |
| 16 | upgradeTo = "bbb2222" |
| 17 | ) |
| 18 | |
| 19 | // upgradeRig runs scripts/upgrade-thin.sh against stub carlos, systemctl, |
| 20 | // curl, git and make on a temporary HOME, after a finished cutover to |
| 21 | // upgradeFrom. Nothing here reaches a real service. |
| 22 | type upgradeRig struct { |
| 23 | t *testing.T |
| 24 | root, home, runner string |
| 25 | script, log string |
| 26 | serviceEnv, unit string |
| 27 | // env is added to every run's environment. |
| 28 | env []string |
| 29 | } |
| 30 | |
| 31 | func newUpgradeRig(t *testing.T) *upgradeRig { |
| 32 | t.Helper() |
| 33 | root := t.TempDir() |
| 34 | r := &upgradeRig{t: t, root: root, home: filepath.Join(root, "home"), log: filepath.Join(root, "calls.log")} |
| 35 | r.runner = filepath.Join(r.home, ".local/share/equipo-runner") |
| 36 | repo := filepath.Join(root, "repo") |
| 37 | src, err := os.ReadFile("../../scripts/upgrade-thin.sh") |
| 38 | r.must(err) |
| 39 | r.script = filepath.Join(repo, "scripts", "upgrade-thin.sh") |
| 40 | r.write(r.script, string(src), 0700) |
| 41 | helper, err := os.ReadFile("../../scripts/runner-config.py") |
| 42 | r.must(err) |
| 43 | r.write(filepath.Join(repo, "scripts", "runner-config.py"), string(helper), 0700) |
| 44 | r.write(filepath.Join(repo, "releases", "equipo-linux-arm64-"+upgradeTo), "instance", 0600) |
| 45 | release := filepath.Join(repo, "releases", "equipo-runner-linux-amd64-"+upgradeTo) |
| 46 | runner := "#!/bin/sh\n" + rendererStub(r.log) + "if [ \"$1\" = verify-installed ]; then echo \"equipo-runner verify-installed $EQUIPO_TOOL_PROXY_PATH\" >> " + r.log + "; [ ! -e " + filepath.Join(root, "verifyfail") + " ]; exit; fi\necho \"equipo-runner $1\" >> " + r.log + "\n" + linkedScanner + linkedRenderer |
| 47 | r.write(filepath.Join(release, "equipo-runner"), runner, 0700) |
| 48 | r.write(filepath.Join(release, RendererArchive), string(rendererArchiveBytes), 0600) |
| 49 | r.write(filepath.Join(release, "equipo-tool-proxy"), "proxy", 0700) |
| 50 | r.write(filepath.Join(release, "gitleaks"), scannerBytes, 0700) |
| 51 | sum := sha256.Sum256([]byte(runner)) |
| 52 | r.write(filepath.Join(repo, "releases", "staged", upgradeTo+".json"), `{"staging": "PASS", "sha256": {"runner": "`+hex.EncodeToString(sum[:])+`"}}`, 0600) |
| 53 | old := filepath.Join(r.runner, "releases", upgradeFrom) |
| 54 | r.serviceEnv = "EQUIPO_ORIGIN=\"https://equipo.oficina.works\"\nEQUIPO_INSTANCE_ID=\"INSTANCE0000000000000000000\"\nEQUIPO_TOOL_PROXY_PATH=\"" + old + "/equipo-tool-proxy\"\nEQUIPO_RUNNER_STATE_DIR=\"" + filepath.Join(r.runner, "state") + "\"\nEQUIPO_SCRATCH_ROOT=\"/prod/scratch\"\nEQUIPO_WORKSPACE_ROOT=\"/prod/work\"\n" |
| 55 | r.unit = "[Service]\nEnvironmentFile=" + filepath.Join(r.runner, "service.env") + "\nExecStart=" + old + "/equipo-runner run\nRestart=on-failure\nRestartSec=5\n" |
| 56 | r.write(filepath.Join(r.runner, "service.env"), r.serviceEnv, 0600) |
| 57 | r.write(filepath.Join(r.runner, "equipo-runner.service"), r.unit, 0600) |
| 58 | r.write(filepath.Join(r.runner, "state", "credentials.json"), "enrolled", 0600) |
| 59 | r.write(filepath.Join(r.home, ".local/share/equipo-cutover/cutover.state"), "version="+upgradeFrom+"\ndone_preflight=1\ndone_verify=1\n", 0600) |
| 60 | r.write(filepath.Join(root, "served"), upgradeFrom+"\n", 0600) |
| 61 | r.write(filepath.Join(root, "inflight"), "0/0/0", 0600) |
| 62 | // The runner it replaces, which a rollback starts again. |
| 63 | r.write(filepath.Join(old, "equipo-runner"), "#!/bin/sh\n# old runner\n", 0700) |
| 64 | // The running runner's executable, as /proc shows it: the installed one. |
| 65 | r.must(os.MkdirAll(filepath.Join(root, "proc", "4242"), 0700)) |
| 66 | r.must(os.Symlink(filepath.Join(r.runner, "releases", upgradeTo, "equipo-runner"), filepath.Join(root, "proc", "4242", "exe"))) |
| 67 | bin := filepath.Join(root, "bin") |
| 68 | log := "echo \"$(basename $0) $*\" >> " + r.log + "\n" |
| 69 | r.write(filepath.Join(bin, "make"), "#!/bin/sh\n"+log, 0700) |
| 70 | r.write(filepath.Join(bin, "git"), "#!/bin/sh\n"+log, 0700) |
| 71 | r.write(filepath.Join(bin, "curl"), "#!/bin/sh\ncat "+filepath.Join(root, "served")+"\n", 0700) |
| 72 | unitStubs(r.write, root, filepath.Join(r.runner, "equipo-runner.service"), r.log) |
| 73 | r.write(filepath.Join(bin, "carlos"), "#!/bin/sh\n"+log+`case "$1 $2" in |
| 74 | "db "*) [ -e `+filepath.Join(root, "replicabroken")+` ] && { echo "the copy failed its integrity check" >&2; exit 1; } |
| 75 | case "$2 $*" in pull*) for a; do case $prev in --out) python3 -c "import sqlite3,sys; c=sqlite3.connect(sys.argv[1]); [c.execute('create table '+t+'(x)') for t in ('teams','chat_tasks','chat_steps','chat_turns','runners')]; c.commit()" "$a" ;; esac; prev=$a; done ;; |
| 76 | *integrity_check*) printf 'integrity_check\nok\n' ;; *step_timeout*) if [ -s `+filepath.Join(root, "unfinished.seq")+` ]; then n=$(head -1 `+filepath.Join(root, "unfinished.seq")+`); sed -i 1d `+filepath.Join(root, "unfinished.seq")+`; else n=$(cat `+filepath.Join(root, "unfinished")+` 2>/dev/null || echo 0); fi; printf 'x\n%s\n' "$n" ;; *) printf 'x\n%s\n' "$(cat `+filepath.Join(root, "inflight")+`)" ;; esac ;; |
| 77 | deploy*) for a; do case $prev in --version) echo "$a" > `+filepath.Join(root, "served")+` ;; esac; prev=$a; done ;; |
| 78 | promote*) shift; for a; do last2=$last; last=$a; done; echo "$last2" > `+filepath.Join(root, "served")+` ;; |
| 79 | esac |
| 80 | `, 0700) |
| 81 | return r |
| 82 | } |
| 83 | |
| 84 | func (r *upgradeRig) must(err error) { |
| 85 | r.t.Helper() |
| 86 | if err != nil { |
| 87 | r.t.Fatal(err) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | func (r *upgradeRig) write(path, body string, mode os.FileMode) { |
| 92 | r.t.Helper() |
| 93 | r.must(os.MkdirAll(filepath.Dir(path), 0700)) |
| 94 | r.must(os.WriteFile(path, []byte(body), mode)) |
| 95 | } |
| 96 | |
| 97 | func (r *upgradeRig) read(path string) string { |
| 98 | raw, _ := os.ReadFile(path) |
| 99 | return string(raw) |
| 100 | } |
| 101 | |
| 102 | func (r *upgradeRig) run(args ...string) (string, error) { |
| 103 | cmd := exec.Command("bash", append([]string{r.script}, args...)...) |
| 104 | cmd.Env = []string{"HOME=" + r.home, "PATH=" + filepath.Join(r.root, "bin") + ":/usr/bin:/bin", "UPGRADE_YES=1", "UPGRADE_TRIES=3", "UPGRADE_WAIT=0", "UPGRADE_PROC=" + filepath.Join(r.root, "proc"), "LANG=C.UTF-8"} |
| 105 | cmd.Env = append(cmd.Env, r.env...) |
| 106 | out, err := cmd.CombinedOutput() |
| 107 | return string(out), err |
| 108 | } |
| 109 | |
| 110 | func (r *upgradeRig) state() string { |
| 111 | return r.read(filepath.Join(r.home, ".local/share/equipo-cutover/upgrade.state")) |
| 112 | } |
| 113 | |
| 114 | // inOrder fails unless each want appears in s after the one before it. |
| 115 | func inOrder(t *testing.T, s string, want ...string) { |
| 116 | t.Helper() |
| 117 | at := 0 |
| 118 | for _, w := range want { |
| 119 | i := strings.Index(s[at:], w) |
| 120 | if i < 0 { |
| 121 | t.Fatalf("%q missing or out of order in:\n%s", w, s) |
| 122 | } |
| 123 | at += i + len(w) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // The plan changes nothing and lists the staging release order: preflight, |
| 128 | // backup, runners (after a second idle check), then the instance, then verify. |
| 129 | func TestUpgradePlanChangesNothingAndRunsRunnersFirst(t *testing.T) { |
| 130 | r := newUpgradeRig(t) |
| 131 | out, err := r.run(upgradeTo) |
| 132 | if err != nil { |
| 133 | t.Fatalf("plan: %v\n%s", err, out) |
| 134 | } |
| 135 | inOrder(t, out, "from "+upgradeFrom+" to "+upgradeTo, "== 1 preflight", "staged-check RELEASE_VERSION="+upgradeTo, "must be 0/0", |
| 136 | "== 2 backup", "carlos db pull", "pragma integrity_check", "chmod 0400", |
| 137 | "== 3 runner", "verify-installed", "runner-config.py render", "daemon-reload", "short window, not a drain", "systemctl --user restart equipo-runner.service", |
| 138 | "== 4 instance", "--swap hold --version "+upgradeTo, "== 5 verify", "_SYSTEMD_INVOCATION_ID=", "Plan only.") |
| 139 | if strings.Contains(out, "would refuse") || strings.Contains(out, "env set") { |
| 140 | t.Fatalf("plan refuses or changes the configuration:\n%s", out) |
| 141 | } |
| 142 | if r.read(r.log) != "" || r.state() != "" || r.read(filepath.Join(r.runner, "service.env")) != r.serviceEnv { |
| 143 | t.Fatalf("the plan changed something:\n%s", r.read(r.log)) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // The upgrade switches the runner first, then the instance, on the same |
| 148 | // database, and checks the running runner's bytes; it is not run twice. |
| 149 | func TestUpgradeSwitchesRunnerThenInstanceOnTheSameDatabase(t *testing.T) { |
| 150 | r := newUpgradeRig(t) |
| 151 | out, err := r.run(upgradeTo, "--execute") |
| 152 | if err != nil { |
| 153 | t.Fatalf("upgrade: %v\n%s", err, out) |
| 154 | } |
| 155 | calls := r.read(r.log) |
| 156 | inOrder(t, calls, "make -C", "staged-check", "git -C", "merge-base --is-ancestor "+upgradeFrom+" "+upgradeTo, "db query", "db pull", |
| 157 | "equipo-runner verify-installed "+filepath.Join(r.runner, "releases", upgradeTo, "equipo-tool-proxy"), "daemon-reload", "show -p ExecStart", "db query", |
| 158 | "systemctl --user restart equipo-runner.service", "carlos deploy", "--swap hold --version "+upgradeTo, |
| 159 | "journalctl --user -b _SYSTEMD_INVOCATION_ID=0123456789abcdef0123456789abcdef _PID=4242") |
| 160 | // Idle counts dispatched turns that have not ended, as well as steps and merges. |
| 161 | if !strings.Contains(calls, "(SELECT count(*) FROM chat_turns WHERE dispatched=1 AND state NOT IN ('replied','failed','cancelled'))") { |
| 162 | t.Fatalf("the idle reading does not count unfinished turns:\n%s", calls) |
| 163 | } |
| 164 | for _, never := range []string{"env set", "env unset", "equipo-runner enrol", "promote"} { |
| 165 | if strings.Contains(calls, never) { |
| 166 | t.Fatalf("upgrade ran %q:\n%s", never, calls) |
| 167 | } |
| 168 | } |
| 169 | env, unit := r.read(filepath.Join(r.runner, "service.env")), r.read(filepath.Join(r.runner, "equipo-runner.service")) |
| 170 | if !strings.Contains(env, filepath.Join(r.runner, "releases", upgradeTo, "equipo-tool-proxy")) || !strings.Contains(env, "INSTANCE0000000000000000000") || |
| 171 | !strings.Contains(unit, "ExecStart="+filepath.Join(r.runner, "releases", upgradeTo, "equipo-runner")+" run") { |
| 172 | t.Fatalf("runner not switched:\n%s\n%s", env, unit) |
| 173 | } |
| 174 | if r.read(filepath.Join(r.runner, "state", "credentials.json")) != "enrolled" { |
| 175 | t.Fatal("runner enrolment changed") |
| 176 | } |
| 177 | st := r.state() |
| 178 | for _, want := range []string{"prev_version=" + upgradeFrom, "backup_sha256=", "done_verify=1"} { |
| 179 | if !strings.Contains(st, want) { |
| 180 | t.Fatalf("state lacks %q:\n%s", want, st) |
| 181 | } |
| 182 | } |
| 183 | if out, err = r.run(upgradeTo, "--execute"); err == nil || !strings.Contains(out, "already on") { |
| 184 | t.Fatalf("second upgrade allowed: %v %s", err, out) |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // Each refusal stops --execute before anything changes. |
| 189 | func TestUpgradeRefusesBeforeChangingAnything(t *testing.T) { |
| 190 | for name, set := range map[string]func(r *upgradeRig){ |
| 191 | "cutover unfinished": func(r *upgradeRig) { |
| 192 | r.write(filepath.Join(r.home, ".local/share/equipo-cutover/cutover.state"), "version="+upgradeFrom+"\ndone_deploy=1\n", 0600) |
| 193 | }, |
| 194 | "no cutover": func(r *upgradeRig) { |
| 195 | r.must(os.Remove(filepath.Join(r.home, ".local/share/equipo-cutover/cutover.state"))) |
| 196 | }, |
| 197 | "steps in flight": func(r *upgradeRig) { r.write(filepath.Join(r.root, "inflight"), "1/0/0", 0600) }, |
| 198 | "turns in flight": func(r *upgradeRig) { r.write(filepath.Join(r.root, "inflight"), "0/0/1", 0600) }, |
| 199 | "another served": func(r *upgradeRig) { r.write(filepath.Join(r.root, "served"), "ccc3333\n", 0600) }, |
| 200 | "staging unit": func(r *upgradeRig) { |
| 201 | r.write(filepath.Join(r.root, "fragment"), filepath.Join(r.home, ".config/systemd/user/equipo-runner-staging.service"), 0600) |
| 202 | }, |
| 203 | "runner on the old origin": func(r *upgradeRig) { |
| 204 | r.write(filepath.Join(r.runner, "service.env"), strings.Replace(r.serviceEnv, "equipo.oficina.works", "equipo.bds.oncarlos.com", 1), 0600) |
| 205 | }, |
| 206 | "replica unreadable": func(r *upgradeRig) { r.write(filepath.Join(r.root, "replicabroken"), "", 0600) }, |
| 207 | "another upgrade unfinished": func(r *upgradeRig) { |
| 208 | r.write(filepath.Join(r.home, ".local/share/equipo-cutover/upgrade.state"), "version=ddd4444\nprev_version="+upgradeFrom+"\ndone_preflight=1\n", 0600) |
| 209 | }, |
| 210 | } { |
| 211 | t.Run(name, func(t *testing.T) { |
| 212 | r := newUpgradeRig(t) |
| 213 | r.t = t |
| 214 | set(r) |
| 215 | env := r.read(filepath.Join(r.runner, "service.env")) |
| 216 | out, err := r.run(upgradeTo, "--execute") |
| 217 | if err == nil || !strings.Contains(out, "refused") { |
| 218 | t.Fatalf("not refused: %v\n%s", err, out) |
| 219 | } |
| 220 | calls := r.read(r.log) |
| 221 | for _, change := range []string{"db pull", "restart", "deploy", "daemon-reload"} { |
| 222 | if strings.Contains(calls, change) { |
| 223 | t.Fatalf("changed something (%s) before refusing:\n%s", change, calls) |
| 224 | } |
| 225 | } |
| 226 | if r.read(filepath.Join(r.runner, "service.env")) != env || strings.Contains(r.state(), "done_preflight=1\ndone") { |
| 227 | t.Fatal("runner config or state changed") |
| 228 | } |
| 229 | }) |
| 230 | } |
| 231 | // An unreadable replica is named, with the check to run, and no way around it. |
| 232 | r := newUpgradeRig(t) |
| 233 | r.write(filepath.Join(r.root, "replicabroken"), "", 0600) |
| 234 | out, _ := r.run(upgradeTo, "--execute") |
| 235 | if !strings.Contains(out, "replica of equipo's database cannot be read") || !strings.Contains(out, "--sql 'PRAGMA integrity_check' (it must print ok)") { |
| 236 | t.Fatalf("unclear refusal for an unreadable replica:\n%s", out) |
| 237 | } |
| 238 | // The plan still prints, and says what it would refuse. |
| 239 | r = newUpgradeRig(t) |
| 240 | r.write(filepath.Join(r.home, ".local/share/equipo-cutover/cutover.state"), "version="+upgradeFrom+"\ndone_deploy=1\n", 0600) |
| 241 | if out, err := r.run(upgradeTo); err != nil || !strings.Contains(out, "would refuse: the cutover has not finished") || !strings.Contains(out, "== 5 verify") { |
| 242 | t.Fatalf("plan: %v\n%s", err, out) |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | // A running runner that is not the staged bytes, or programs that fail |
| 247 | // verify-installed, stop the upgrade; once fixed, the same run resumes. |
| 248 | func TestUpgradeVerifiesTheRunnerItRuns(t *testing.T) { |
| 249 | r := newUpgradeRig(t) |
| 250 | r.write(filepath.Join(r.root, "verifyfail"), "", 0600) |
| 251 | if out, err := r.run(upgradeTo, "--execute"); err == nil || strings.Contains(r.read(r.log), "restart") { |
| 252 | t.Fatalf("switched after a failed verify-installed: %v\n%s", err, out) |
| 253 | } |
| 254 | if r.read(filepath.Join(r.runner, "service.env")) != r.serviceEnv { |
| 255 | t.Fatal("runner config changed before verify-installed passed") |
| 256 | } |
| 257 | r.must(os.Remove(filepath.Join(r.root, "verifyfail"))) |
| 258 | exe := filepath.Join(r.root, "proc", "4242", "exe") |
| 259 | r.must(os.Remove(exe)) |
| 260 | r.must(os.Symlink("/bin/sh", exe)) |
| 261 | if out, err := r.run(upgradeTo, "--execute"); err == nil || !strings.Contains(out, "not the staged") { |
| 262 | t.Fatalf("accepted another running binary: %v\n%s", err, out) |
| 263 | } |
| 264 | r.must(os.Remove(exe)) |
| 265 | r.must(os.Symlink(filepath.Join(r.runner, "releases", upgradeTo, "equipo-runner"), exe)) |
| 266 | out, err := r.run(upgradeTo, "--execute") |
| 267 | if err != nil || strings.Count(r.read(r.log), "db pull") != 1 || strings.Count(r.read(r.log), "carlos deploy") != 1 { |
| 268 | t.Fatalf("did not resume past finished steps: %v\n%s\n%s", err, out, r.read(r.log)) |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // Rollback restores the previous runner and version exactly, says the |
| 273 | // database is not rolled back and names the backup; the next upgrade can |
| 274 | // then start again from the restored version. |
| 275 | func TestUpgradeRollbackRestoresRunnerAndVersion(t *testing.T) { |
| 276 | r := newUpgradeRig(t) |
| 277 | if out, err := r.run(upgradeTo, "--execute"); err != nil { |
| 278 | t.Fatalf("upgrade: %v\n%s", err, out) |
| 279 | } |
| 280 | // Until the previous runner is the one running and connected, the |
| 281 | // rollback is not recorded, and it can be run again. |
| 282 | if out, err := r.run("rollback", "--execute"); err == nil || !strings.Contains(out, "is not the previous runner") || strings.Contains(r.state(), "rolled_back=1") { |
| 283 | t.Fatalf("rollback recorded without the previous runner running: %v\n%s", err, out) |
| 284 | } |
| 285 | exe := filepath.Join(r.root, "proc", "4242", "exe") |
| 286 | r.must(os.Remove(exe)) |
| 287 | r.must(os.Symlink(filepath.Join(r.runner, "releases", upgradeFrom, "equipo-runner"), exe)) |
| 288 | out, err := r.run("rollback", "--execute") |
| 289 | if err != nil { |
| 290 | t.Fatalf("rollback: %v\n%s", err, out) |
| 291 | } |
| 292 | if r.read(filepath.Join(r.runner, "service.env")) != r.serviceEnv || r.read(filepath.Join(r.runner, "equipo-runner.service")) != r.unit { |
| 293 | t.Fatal("runner configuration not restored exactly") |
| 294 | } |
| 295 | if strings.TrimSpace(r.read(filepath.Join(r.root, "served"))) != upgradeFrom { |
| 296 | t.Fatal("previous version not served") |
| 297 | } |
| 298 | inOrder(t, r.read(r.log), "systemctl --user stop", "daemon-reload", "promote --app equipo --account oficina --console https://console.carloku.com "+upgradeFrom+" edge", "systemctl --user start") |
| 299 | if !strings.Contains(out, "database is not rolled back") || !strings.Contains(out, "equipo-upgrade-") { |
| 300 | t.Fatalf("rollback does not say the database stays, or where the backup is:\n%s", out) |
| 301 | } |
| 302 | if out, err = r.run("rollback", "--execute"); err == nil { |
| 303 | t.Fatalf("second rollback allowed: %s", out) |
| 304 | } |
| 305 | time.Sleep(1100 * time.Millisecond) // a new stamp, so a new backup file |
| 306 | r.must(os.Remove(exe)) |
| 307 | r.must(os.Symlink(filepath.Join(r.runner, "releases", upgradeTo, "equipo-runner"), exe)) |
| 308 | if out, err = r.run(upgradeTo, "--execute"); err != nil { |
| 309 | t.Fatalf("upgrade after rollback: %v\n%s", err, out) |
| 310 | } |
| 311 | if kept, _ := filepath.Glob(filepath.Join(r.home, ".local/share/equipo-cutover/upgrade-"+upgradeTo+"-*.state")); len(kept) != 1 { |
| 312 | t.Fatalf("rolled-back state not kept: %v", kept) |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | // unitStubs writes stub systemctl and journalctl for the runner unit at |
| 317 | // unit: start and restart make it active (root/active) and stop ends it |
| 318 | // unless root/stuck exists; with root/killafter<verb> the script calling |
| 319 | // start or restart is killed right after (an interrupted run). It has an |
| 320 | // invocation id (fresh on every call while root/flap exists), main PID |
| 321 | // 4242 while active, the loaded ExecStart (root/loaded, else the unit |
| 322 | // file's) and the journal (root/journal, else a welcome). |
| 323 | func unitStubs(write func(string, string, os.FileMode), root, unit, log string) { |
| 324 | bin := filepath.Join(root, "bin") |
| 325 | logLine := "echo \"$(basename $0) $*\" >> " + log + "\n" |
| 326 | write(filepath.Join(bin, "systemctl"), "#!/bin/sh\n"+logLine+`case "$*" in |
| 327 | *FragmentPath*) cat `+filepath.Join(root, "fragment")+` 2>/dev/null || echo `+unit+` ;; |
| 328 | *InvocationID*) if [ -e `+filepath.Join(root, "flap")+` ]; then od -An -tx1 -N16 /dev/urandom | tr -d ' \n'; echo; else echo 0123456789abcdef0123456789abcdef; fi ;; |
| 329 | *MainPID*) if [ -e `+filepath.Join(root, "active")+` ]; then echo 4242; else echo 0; fi ;; |
| 330 | *" start "*|*" restart "*) touch `+filepath.Join(root, "active")+`; v=$2; if [ -e `+filepath.Join(root, "killafter")+`$v ]; then rm `+filepath.Join(root, "killafter")+`$v; kill -TERM $PPID; fi ;; |
| 331 | *" stop "*) [ -e `+filepath.Join(root, "stuck")+` ] || rm -f `+filepath.Join(root, "active")+` ;; |
| 332 | *ExecStart*) cat `+filepath.Join(root, "loaded")+` 2>/dev/null || sed -n 's/^ExecStart=\(.*\) run$/{ path=\1 ; argv[]=\1 run }/p' `+unit+` ;; |
| 333 | *is-active*) [ -e `+filepath.Join(root, "active")+` ] ;; |
| 334 | *daemon-reload*) if [ -e `+filepath.Join(root, "failreload")+` ]; then rm `+filepath.Join(root, "failreload")+`; exit 1; fi ;; |
| 335 | esac |
| 336 | `, 0700) |
| 337 | write(filepath.Join(bin, "journalctl"), "#!/bin/sh\n"+logLine+"cat "+filepath.Join(root, "journal")+" 2>/dev/null || echo 'level=INFO msg=welcome protocol=28 generation=1'\n", 0700) |
| 338 | } |
| 339 | |
| 340 | // Each key, ExecStart and RestartSec must be there exactly once: an edit |
| 341 | // that would match nothing, or twice, stops before the restart. |
| 342 | func TestUpgradeRefusesUnexpectedRunnerConfig(t *testing.T) { |
| 343 | for name, set := range map[string]func(r *upgradeRig){ |
| 344 | "tool proxy twice": func(r *upgradeRig) { |
| 345 | r.write(filepath.Join(r.runner, "service.env"), r.serviceEnv+"EQUIPO_TOOL_PROXY_PATH=\"/elsewhere\"\n", 0600) |
| 346 | }, |
| 347 | "no RestartSec": func(r *upgradeRig) { |
| 348 | r.write(filepath.Join(r.runner, "equipo-runner.service"), strings.Replace(r.unit, "RestartSec=5\n", "", 1), 0600) |
| 349 | }, |
| 350 | } { |
| 351 | t.Run(name, func(t *testing.T) { |
| 352 | r := newUpgradeRig(t) |
| 353 | r.t = t |
| 354 | set(r) |
| 355 | out, err := r.run(upgradeTo, "--execute") |
| 356 | if err == nil || !strings.Contains(out, "not one") || strings.Contains(r.read(r.log), "restart") || strings.Contains(r.read(r.log), "carlos deploy") { |
| 357 | t.Fatalf("went on with an unexpected runner config: %v\n%s", err, out) |
| 358 | } |
| 359 | }) |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // Connected means now, for this invocation: a welcome followed by a drop, |
| 364 | // or an invocation that changes between readings, is not enough. |
| 365 | func TestUpgradeVerifiesTheCurrentConnection(t *testing.T) { |
| 366 | for name, set := range map[string]func(r *upgradeRig){ |
| 367 | "dropped since": func(r *upgradeRig) { |
| 368 | r.write(filepath.Join(r.root, "journal"), "level=INFO msg=welcome generation=1\nlevel=INFO msg=\"runner stream ended\" retry_in=1s\n", 0600) |
| 369 | }, |
| 370 | "never welcomed": func(r *upgradeRig) { |
| 371 | r.write(filepath.Join(r.root, "journal"), "level=WARN msg=\"runner stream\" err=refused\n", 0600) |
| 372 | }, |
| 373 | "another invocation each time": func(r *upgradeRig) { r.write(filepath.Join(r.root, "flap"), "", 0600) }, |
| 374 | } { |
| 375 | t.Run(name, func(t *testing.T) { |
| 376 | r := newUpgradeRig(t) |
| 377 | r.t = t |
| 378 | set(r) |
| 379 | out, err := r.run(upgradeTo, "--execute") |
| 380 | if err == nil || !strings.Contains(out, "not connected") || strings.Contains(r.state(), "done_verify=1") { |
| 381 | t.Fatalf("verified without a current connection: %v\n%s", err, out) |
| 382 | } |
| 383 | }) |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | // The scripts a person runs are executable. |
| 388 | func TestOperatorScriptsAreExecutable(t *testing.T) { |
| 389 | for _, name := range []string{"cutover-thin.sh", "cutover-finish-thin.sh", "upgrade-thin.sh", "runner-config.py"} { |
| 390 | info, err := os.Stat("../../scripts/" + name) |
| 391 | if err != nil || info.Mode()&0111 != 0111 { |
| 392 | t.Errorf("scripts/%s is not executable: %v %v", name, info, err) |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | // A run cut off right after the restart resumes without restarting the |
| 398 | // staged runner again; a rollback cut off after the previous runner |
| 399 | // started does not stop it again. |
| 400 | func TestUpgradeResumesAfterTheRestart(t *testing.T) { |
| 401 | r := newUpgradeRig(t) |
| 402 | r.write(filepath.Join(r.root, "killafterrestart"), "", 0600) |
| 403 | if out, err := r.run(upgradeTo, "--execute"); err == nil { |
| 404 | t.Fatalf("the run was not interrupted:\n%s", out) |
| 405 | } |
| 406 | if out, err := r.run(upgradeTo, "--execute"); err != nil || strings.Count(r.read(r.log), "systemctl --user restart") != 1 || !strings.Contains(r.state(), "done_verify=1") { |
| 407 | t.Fatalf("did not resume without a second restart: %v\n%s\n%s", err, out, r.read(r.log)) |
| 408 | } |
| 409 | r.write(filepath.Join(r.root, "killafterstart"), "", 0600) |
| 410 | if out, err := r.run("rollback", "--execute"); err == nil { |
| 411 | t.Fatalf("the rollback was not interrupted:\n%s", out) |
| 412 | } |
| 413 | stops := strings.Count(r.read(r.log), "systemctl --user stop") |
| 414 | if stops != 1 { |
| 415 | t.Fatalf("the rollback stopped the staged runner %d times", stops) |
| 416 | } |
| 417 | // The previous runner is what the unit now runs. |
| 418 | exe := filepath.Join(r.root, "proc", "4242", "exe") |
| 419 | r.must(os.Remove(exe)) |
| 420 | r.must(os.Symlink(filepath.Join(r.runner, "releases", upgradeFrom, "equipo-runner"), exe)) |
| 421 | if out, err := r.run("rollback", "--execute"); err != nil || strings.Count(r.read(r.log), "systemctl --user stop") != stops || !strings.Contains(r.state(), "rolled_back=1") { |
| 422 | t.Fatalf("rollback retry stopped the previous runner again: %v\n%s\n%s", err, out, r.read(r.log)) |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | // An older release takes the work of an Engineering step that ran out of |
| 427 | // time (done, step_timeout) for finished work: a rollback refuses while |
| 428 | // an open task holds such work, before it changes anything, unless the |
| 429 | // operator says to go on (UPGRADE_ALLOW_UNFINISHED=1). |
| 430 | func TestUpgradeRollbackRefusesWithUnfinishedWork(t *testing.T) { |
| 431 | r := newUpgradeRig(t) |
| 432 | if out, err := r.run(upgradeTo, "--execute"); err != nil { |
| 433 | t.Fatalf("upgrade: %v\n%s", err, out) |
| 434 | } |
| 435 | exe := filepath.Join(r.root, "proc", "4242", "exe") |
| 436 | r.must(os.Remove(exe)) |
| 437 | r.must(os.Symlink(filepath.Join(r.runner, "releases", upgradeFrom, "equipo-runner"), exe)) |
| 438 | r.write(filepath.Join(r.root, "unfinished"), "2", 0600) |
| 439 | before := r.read(r.log) |
| 440 | out, err := r.run("rollback", "--execute") |
| 441 | if err == nil || !strings.Contains(out, "open tasks with unfinished work: 2") || !strings.Contains(out, "refused: open tasks hold unfinished work") || strings.Contains(r.state(), "rolled_back=1") { |
| 442 | t.Fatalf("rollback with unfinished work: %v\n%s", err, out) |
| 443 | } |
| 444 | if changed := strings.TrimPrefix(r.read(r.log), before); strings.Contains(changed, "systemctl") || strings.Contains(changed, "promote") { |
| 445 | t.Fatalf("changed something before refusing:\n%s", changed) |
| 446 | } |
| 447 | if plan, err := r.run("rollback"); err != nil || !strings.Contains(plan, "unfinished work") { |
| 448 | t.Fatalf("the plan does not name the check: %v\n%s", err, plan) |
| 449 | } |
| 450 | r.env = []string{"UPGRADE_ALLOW_UNFINISHED=1"} |
| 451 | if out, err := r.run("rollback", "--execute"); err != nil || !strings.Contains(r.state(), "rolled_back=1") { |
| 452 | t.Fatalf("rollback told to go on: %v\n%s", err, out) |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | // Unfinished work that comes in after the first reading, before the |
| 457 | // runner stopped, is found once nothing reports steps: the rollback |
| 458 | // starts the runner again and refuses, before the instance changes. |
| 459 | func TestUpgradeRollbackRefusesUnfinishedWorkThatCameIn(t *testing.T) { |
| 460 | r := newUpgradeRig(t) |
| 461 | if out, err := r.run(upgradeTo, "--execute"); err != nil { |
| 462 | t.Fatalf("upgrade: %v\n%s", err, out) |
| 463 | } |
| 464 | r.write(filepath.Join(r.root, "unfinished.seq"), "0\n0\n1\n", 0600) |
| 465 | before := r.read(r.log) |
| 466 | out, err := r.run("rollback", "--execute") |
| 467 | if err == nil || !strings.Contains(out, "the runner was started again") || strings.Contains(r.state(), "rolled_back=1") { |
| 468 | t.Fatalf("rollback with work that came in: %v\n%s", err, out) |
| 469 | } |
| 470 | changed := strings.TrimPrefix(r.read(r.log), before) |
| 471 | if strings.Contains(changed, "promote") || strings.Contains(changed, "daemon-reload") { |
| 472 | t.Fatalf("changed the instance or the unit:\n%s", changed) |
| 473 | } |
| 474 | inOrder(t, changed, "systemctl --user stop", "systemctl --user start") |
| 475 | } |
| 476 | |