package release import ( "crypto/sha256" "encoding/hex" "os" "os/exec" "path/filepath" "strings" "testing" "time" ) const ( upgradeFrom = "aaa1111" upgradeTo = "bbb2222" ) // upgradeRig runs scripts/upgrade-thin.sh against stub carlos, systemctl, // curl, git and make on a temporary HOME, after a finished cutover to // upgradeFrom. Nothing here reaches a real service. type upgradeRig struct { t *testing.T root, home, runner string script, log string serviceEnv, unit string // env is added to every run's environment. env []string } func newUpgradeRig(t *testing.T) *upgradeRig { t.Helper() root := t.TempDir() r := &upgradeRig{t: t, root: root, home: filepath.Join(root, "home"), log: filepath.Join(root, "calls.log")} r.runner = filepath.Join(r.home, ".local/share/equipo-runner") repo := filepath.Join(root, "repo") src, err := os.ReadFile("../../scripts/upgrade-thin.sh") r.must(err) r.script = filepath.Join(repo, "scripts", "upgrade-thin.sh") r.write(r.script, string(src), 0700) helper, err := os.ReadFile("../../scripts/runner-config.py") r.must(err) r.write(filepath.Join(repo, "scripts", "runner-config.py"), string(helper), 0700) r.write(filepath.Join(repo, "releases", "equipo-linux-arm64-"+upgradeTo), "instance", 0600) release := filepath.Join(repo, "releases", "equipo-runner-linux-amd64-"+upgradeTo) 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 r.write(filepath.Join(release, "equipo-runner"), runner, 0700) r.write(filepath.Join(release, RendererArchive), string(rendererArchiveBytes), 0600) r.write(filepath.Join(release, "equipo-tool-proxy"), "proxy", 0700) r.write(filepath.Join(release, "gitleaks"), scannerBytes, 0700) sum := sha256.Sum256([]byte(runner)) r.write(filepath.Join(repo, "releases", "staged", upgradeTo+".json"), `{"staging": "PASS", "sha256": {"runner": "`+hex.EncodeToString(sum[:])+`"}}`, 0600) old := filepath.Join(r.runner, "releases", upgradeFrom) 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" r.unit = "[Service]\nEnvironmentFile=" + filepath.Join(r.runner, "service.env") + "\nExecStart=" + old + "/equipo-runner run\nRestart=on-failure\nRestartSec=5\n" r.write(filepath.Join(r.runner, "service.env"), r.serviceEnv, 0600) r.write(filepath.Join(r.runner, "equipo-runner.service"), r.unit, 0600) r.write(filepath.Join(r.runner, "state", "credentials.json"), "enrolled", 0600) r.write(filepath.Join(r.home, ".local/share/equipo-cutover/cutover.state"), "version="+upgradeFrom+"\ndone_preflight=1\ndone_verify=1\n", 0600) r.write(filepath.Join(root, "served"), upgradeFrom+"\n", 0600) r.write(filepath.Join(root, "inflight"), "0/0/0", 0600) // The runner it replaces, which a rollback starts again. r.write(filepath.Join(old, "equipo-runner"), "#!/bin/sh\n# old runner\n", 0700) // The running runner's executable, as /proc shows it: the installed one. r.must(os.MkdirAll(filepath.Join(root, "proc", "4242"), 0700)) r.must(os.Symlink(filepath.Join(r.runner, "releases", upgradeTo, "equipo-runner"), filepath.Join(root, "proc", "4242", "exe"))) bin := filepath.Join(root, "bin") log := "echo \"$(basename $0) $*\" >> " + r.log + "\n" r.write(filepath.Join(bin, "make"), "#!/bin/sh\n"+log, 0700) r.write(filepath.Join(bin, "git"), "#!/bin/sh\n"+log, 0700) r.write(filepath.Join(bin, "curl"), "#!/bin/sh\ncat "+filepath.Join(root, "served")+"\n", 0700) unitStubs(r.write, root, filepath.Join(r.runner, "equipo-runner.service"), r.log) r.write(filepath.Join(bin, "carlos"), "#!/bin/sh\n"+log+`case "$1 $2" in "db "*) [ -e `+filepath.Join(root, "replicabroken")+` ] && { echo "the copy failed its integrity check" >&2; exit 1; } 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 ;; *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 ;; deploy*) for a; do case $prev in --version) echo "$a" > `+filepath.Join(root, "served")+` ;; esac; prev=$a; done ;; promote*) shift; for a; do last2=$last; last=$a; done; echo "$last2" > `+filepath.Join(root, "served")+` ;; esac `, 0700) return r } func (r *upgradeRig) must(err error) { r.t.Helper() if err != nil { r.t.Fatal(err) } } func (r *upgradeRig) write(path, body string, mode os.FileMode) { r.t.Helper() r.must(os.MkdirAll(filepath.Dir(path), 0700)) r.must(os.WriteFile(path, []byte(body), mode)) } func (r *upgradeRig) read(path string) string { raw, _ := os.ReadFile(path) return string(raw) } func (r *upgradeRig) run(args ...string) (string, error) { cmd := exec.Command("bash", append([]string{r.script}, args...)...) 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"} cmd.Env = append(cmd.Env, r.env...) out, err := cmd.CombinedOutput() return string(out), err } func (r *upgradeRig) state() string { return r.read(filepath.Join(r.home, ".local/share/equipo-cutover/upgrade.state")) } // inOrder fails unless each want appears in s after the one before it. func inOrder(t *testing.T, s string, want ...string) { t.Helper() at := 0 for _, w := range want { i := strings.Index(s[at:], w) if i < 0 { t.Fatalf("%q missing or out of order in:\n%s", w, s) } at += i + len(w) } } // The plan changes nothing and lists the staging release order: preflight, // backup, runners (after a second idle check), then the instance, then verify. func TestUpgradePlanChangesNothingAndRunsRunnersFirst(t *testing.T) { r := newUpgradeRig(t) out, err := r.run(upgradeTo) if err != nil { t.Fatalf("plan: %v\n%s", err, out) } inOrder(t, out, "from "+upgradeFrom+" to "+upgradeTo, "== 1 preflight", "staged-check RELEASE_VERSION="+upgradeTo, "must be 0/0", "== 2 backup", "carlos db pull", "pragma integrity_check", "chmod 0400", "== 3 runner", "verify-installed", "runner-config.py render", "daemon-reload", "short window, not a drain", "systemctl --user restart equipo-runner.service", "== 4 instance", "--swap hold --version "+upgradeTo, "== 5 verify", "_SYSTEMD_INVOCATION_ID=", "Plan only.") if strings.Contains(out, "would refuse") || strings.Contains(out, "env set") { t.Fatalf("plan refuses or changes the configuration:\n%s", out) } if r.read(r.log) != "" || r.state() != "" || r.read(filepath.Join(r.runner, "service.env")) != r.serviceEnv { t.Fatalf("the plan changed something:\n%s", r.read(r.log)) } } // The upgrade switches the runner first, then the instance, on the same // database, and checks the running runner's bytes; it is not run twice. func TestUpgradeSwitchesRunnerThenInstanceOnTheSameDatabase(t *testing.T) { r := newUpgradeRig(t) out, err := r.run(upgradeTo, "--execute") if err != nil { t.Fatalf("upgrade: %v\n%s", err, out) } calls := r.read(r.log) inOrder(t, calls, "make -C", "staged-check", "git -C", "merge-base --is-ancestor "+upgradeFrom+" "+upgradeTo, "db query", "db pull", "equipo-runner verify-installed "+filepath.Join(r.runner, "releases", upgradeTo, "equipo-tool-proxy"), "daemon-reload", "show -p ExecStart", "db query", "systemctl --user restart equipo-runner.service", "carlos deploy", "--swap hold --version "+upgradeTo, "journalctl --user -b _SYSTEMD_INVOCATION_ID=0123456789abcdef0123456789abcdef _PID=4242") // Idle counts dispatched turns that have not ended, as well as steps and merges. if !strings.Contains(calls, "(SELECT count(*) FROM chat_turns WHERE dispatched=1 AND state NOT IN ('replied','failed','cancelled'))") { t.Fatalf("the idle reading does not count unfinished turns:\n%s", calls) } for _, never := range []string{"env set", "env unset", "equipo-runner enrol", "promote"} { if strings.Contains(calls, never) { t.Fatalf("upgrade ran %q:\n%s", never, calls) } } env, unit := r.read(filepath.Join(r.runner, "service.env")), r.read(filepath.Join(r.runner, "equipo-runner.service")) if !strings.Contains(env, filepath.Join(r.runner, "releases", upgradeTo, "equipo-tool-proxy")) || !strings.Contains(env, "INSTANCE0000000000000000000") || !strings.Contains(unit, "ExecStart="+filepath.Join(r.runner, "releases", upgradeTo, "equipo-runner")+" run") { t.Fatalf("runner not switched:\n%s\n%s", env, unit) } if r.read(filepath.Join(r.runner, "state", "credentials.json")) != "enrolled" { t.Fatal("runner enrolment changed") } st := r.state() for _, want := range []string{"prev_version=" + upgradeFrom, "backup_sha256=", "done_verify=1"} { if !strings.Contains(st, want) { t.Fatalf("state lacks %q:\n%s", want, st) } } if out, err = r.run(upgradeTo, "--execute"); err == nil || !strings.Contains(out, "already on") { t.Fatalf("second upgrade allowed: %v %s", err, out) } } // Each refusal stops --execute before anything changes. func TestUpgradeRefusesBeforeChangingAnything(t *testing.T) { for name, set := range map[string]func(r *upgradeRig){ "cutover unfinished": func(r *upgradeRig) { r.write(filepath.Join(r.home, ".local/share/equipo-cutover/cutover.state"), "version="+upgradeFrom+"\ndone_deploy=1\n", 0600) }, "no cutover": func(r *upgradeRig) { r.must(os.Remove(filepath.Join(r.home, ".local/share/equipo-cutover/cutover.state"))) }, "steps in flight": func(r *upgradeRig) { r.write(filepath.Join(r.root, "inflight"), "1/0/0", 0600) }, "turns in flight": func(r *upgradeRig) { r.write(filepath.Join(r.root, "inflight"), "0/0/1", 0600) }, "another served": func(r *upgradeRig) { r.write(filepath.Join(r.root, "served"), "ccc3333\n", 0600) }, "staging unit": func(r *upgradeRig) { r.write(filepath.Join(r.root, "fragment"), filepath.Join(r.home, ".config/systemd/user/equipo-runner-staging.service"), 0600) }, "runner on the old origin": func(r *upgradeRig) { r.write(filepath.Join(r.runner, "service.env"), strings.Replace(r.serviceEnv, "equipo.oficina.works", "equipo.bds.oncarlos.com", 1), 0600) }, "replica unreadable": func(r *upgradeRig) { r.write(filepath.Join(r.root, "replicabroken"), "", 0600) }, "another upgrade unfinished": func(r *upgradeRig) { r.write(filepath.Join(r.home, ".local/share/equipo-cutover/upgrade.state"), "version=ddd4444\nprev_version="+upgradeFrom+"\ndone_preflight=1\n", 0600) }, } { t.Run(name, func(t *testing.T) { r := newUpgradeRig(t) r.t = t set(r) env := r.read(filepath.Join(r.runner, "service.env")) out, err := r.run(upgradeTo, "--execute") if err == nil || !strings.Contains(out, "refused") { t.Fatalf("not refused: %v\n%s", err, out) } calls := r.read(r.log) for _, change := range []string{"db pull", "restart", "deploy", "daemon-reload"} { if strings.Contains(calls, change) { t.Fatalf("changed something (%s) before refusing:\n%s", change, calls) } } if r.read(filepath.Join(r.runner, "service.env")) != env || strings.Contains(r.state(), "done_preflight=1\ndone") { t.Fatal("runner config or state changed") } }) } // An unreadable replica is named, with the check to run, and no way around it. r := newUpgradeRig(t) r.write(filepath.Join(r.root, "replicabroken"), "", 0600) out, _ := r.run(upgradeTo, "--execute") if !strings.Contains(out, "replica of equipo's database cannot be read") || !strings.Contains(out, "--sql 'PRAGMA integrity_check' (it must print ok)") { t.Fatalf("unclear refusal for an unreadable replica:\n%s", out) } // The plan still prints, and says what it would refuse. r = newUpgradeRig(t) r.write(filepath.Join(r.home, ".local/share/equipo-cutover/cutover.state"), "version="+upgradeFrom+"\ndone_deploy=1\n", 0600) if out, err := r.run(upgradeTo); err != nil || !strings.Contains(out, "would refuse: the cutover has not finished") || !strings.Contains(out, "== 5 verify") { t.Fatalf("plan: %v\n%s", err, out) } } // A running runner that is not the staged bytes, or programs that fail // verify-installed, stop the upgrade; once fixed, the same run resumes. func TestUpgradeVerifiesTheRunnerItRuns(t *testing.T) { r := newUpgradeRig(t) r.write(filepath.Join(r.root, "verifyfail"), "", 0600) if out, err := r.run(upgradeTo, "--execute"); err == nil || strings.Contains(r.read(r.log), "restart") { t.Fatalf("switched after a failed verify-installed: %v\n%s", err, out) } if r.read(filepath.Join(r.runner, "service.env")) != r.serviceEnv { t.Fatal("runner config changed before verify-installed passed") } r.must(os.Remove(filepath.Join(r.root, "verifyfail"))) exe := filepath.Join(r.root, "proc", "4242", "exe") r.must(os.Remove(exe)) r.must(os.Symlink("/bin/sh", exe)) if out, err := r.run(upgradeTo, "--execute"); err == nil || !strings.Contains(out, "not the staged") { t.Fatalf("accepted another running binary: %v\n%s", err, out) } r.must(os.Remove(exe)) r.must(os.Symlink(filepath.Join(r.runner, "releases", upgradeTo, "equipo-runner"), exe)) out, err := r.run(upgradeTo, "--execute") if err != nil || strings.Count(r.read(r.log), "db pull") != 1 || strings.Count(r.read(r.log), "carlos deploy") != 1 { t.Fatalf("did not resume past finished steps: %v\n%s\n%s", err, out, r.read(r.log)) } } // Rollback restores the previous runner and version exactly, says the // database is not rolled back and names the backup; the next upgrade can // then start again from the restored version. func TestUpgradeRollbackRestoresRunnerAndVersion(t *testing.T) { r := newUpgradeRig(t) if out, err := r.run(upgradeTo, "--execute"); err != nil { t.Fatalf("upgrade: %v\n%s", err, out) } // Until the previous runner is the one running and connected, the // rollback is not recorded, and it can be run again. if out, err := r.run("rollback", "--execute"); err == nil || !strings.Contains(out, "is not the previous runner") || strings.Contains(r.state(), "rolled_back=1") { t.Fatalf("rollback recorded without the previous runner running: %v\n%s", err, out) } exe := filepath.Join(r.root, "proc", "4242", "exe") r.must(os.Remove(exe)) r.must(os.Symlink(filepath.Join(r.runner, "releases", upgradeFrom, "equipo-runner"), exe)) out, err := r.run("rollback", "--execute") if err != nil { t.Fatalf("rollback: %v\n%s", err, out) } if r.read(filepath.Join(r.runner, "service.env")) != r.serviceEnv || r.read(filepath.Join(r.runner, "equipo-runner.service")) != r.unit { t.Fatal("runner configuration not restored exactly") } if strings.TrimSpace(r.read(filepath.Join(r.root, "served"))) != upgradeFrom { t.Fatal("previous version not served") } 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") if !strings.Contains(out, "database is not rolled back") || !strings.Contains(out, "equipo-upgrade-") { t.Fatalf("rollback does not say the database stays, or where the backup is:\n%s", out) } if out, err = r.run("rollback", "--execute"); err == nil { t.Fatalf("second rollback allowed: %s", out) } time.Sleep(1100 * time.Millisecond) // a new stamp, so a new backup file r.must(os.Remove(exe)) r.must(os.Symlink(filepath.Join(r.runner, "releases", upgradeTo, "equipo-runner"), exe)) if out, err = r.run(upgradeTo, "--execute"); err != nil { t.Fatalf("upgrade after rollback: %v\n%s", err, out) } if kept, _ := filepath.Glob(filepath.Join(r.home, ".local/share/equipo-cutover/upgrade-"+upgradeTo+"-*.state")); len(kept) != 1 { t.Fatalf("rolled-back state not kept: %v", kept) } } // unitStubs writes stub systemctl and journalctl for the runner unit at // unit: start and restart make it active (root/active) and stop ends it // unless root/stuck exists; with root/killafter the script calling // start or restart is killed right after (an interrupted run). It has an // invocation id (fresh on every call while root/flap exists), main PID // 4242 while active, the loaded ExecStart (root/loaded, else the unit // file's) and the journal (root/journal, else a welcome). func unitStubs(write func(string, string, os.FileMode), root, unit, log string) { bin := filepath.Join(root, "bin") logLine := "echo \"$(basename $0) $*\" >> " + log + "\n" write(filepath.Join(bin, "systemctl"), "#!/bin/sh\n"+logLine+`case "$*" in *FragmentPath*) cat `+filepath.Join(root, "fragment")+` 2>/dev/null || echo `+unit+` ;; *InvocationID*) if [ -e `+filepath.Join(root, "flap")+` ]; then od -An -tx1 -N16 /dev/urandom | tr -d ' \n'; echo; else echo 0123456789abcdef0123456789abcdef; fi ;; *MainPID*) if [ -e `+filepath.Join(root, "active")+` ]; then echo 4242; else echo 0; fi ;; *" 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 ;; *" stop "*) [ -e `+filepath.Join(root, "stuck")+` ] || rm -f `+filepath.Join(root, "active")+` ;; *ExecStart*) cat `+filepath.Join(root, "loaded")+` 2>/dev/null || sed -n 's/^ExecStart=\(.*\) run$/{ path=\1 ; argv[]=\1 run }/p' `+unit+` ;; *is-active*) [ -e `+filepath.Join(root, "active")+` ] ;; *daemon-reload*) if [ -e `+filepath.Join(root, "failreload")+` ]; then rm `+filepath.Join(root, "failreload")+`; exit 1; fi ;; esac `, 0700) 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) } // Each key, ExecStart and RestartSec must be there exactly once: an edit // that would match nothing, or twice, stops before the restart. func TestUpgradeRefusesUnexpectedRunnerConfig(t *testing.T) { for name, set := range map[string]func(r *upgradeRig){ "tool proxy twice": func(r *upgradeRig) { r.write(filepath.Join(r.runner, "service.env"), r.serviceEnv+"EQUIPO_TOOL_PROXY_PATH=\"/elsewhere\"\n", 0600) }, "no RestartSec": func(r *upgradeRig) { r.write(filepath.Join(r.runner, "equipo-runner.service"), strings.Replace(r.unit, "RestartSec=5\n", "", 1), 0600) }, } { t.Run(name, func(t *testing.T) { r := newUpgradeRig(t) r.t = t set(r) out, err := r.run(upgradeTo, "--execute") if err == nil || !strings.Contains(out, "not one") || strings.Contains(r.read(r.log), "restart") || strings.Contains(r.read(r.log), "carlos deploy") { t.Fatalf("went on with an unexpected runner config: %v\n%s", err, out) } }) } } // Connected means now, for this invocation: a welcome followed by a drop, // or an invocation that changes between readings, is not enough. func TestUpgradeVerifiesTheCurrentConnection(t *testing.T) { for name, set := range map[string]func(r *upgradeRig){ "dropped since": func(r *upgradeRig) { r.write(filepath.Join(r.root, "journal"), "level=INFO msg=welcome generation=1\nlevel=INFO msg=\"runner stream ended\" retry_in=1s\n", 0600) }, "never welcomed": func(r *upgradeRig) { r.write(filepath.Join(r.root, "journal"), "level=WARN msg=\"runner stream\" err=refused\n", 0600) }, "another invocation each time": func(r *upgradeRig) { r.write(filepath.Join(r.root, "flap"), "", 0600) }, } { t.Run(name, func(t *testing.T) { r := newUpgradeRig(t) r.t = t set(r) out, err := r.run(upgradeTo, "--execute") if err == nil || !strings.Contains(out, "not connected") || strings.Contains(r.state(), "done_verify=1") { t.Fatalf("verified without a current connection: %v\n%s", err, out) } }) } } // The scripts a person runs are executable. func TestOperatorScriptsAreExecutable(t *testing.T) { for _, name := range []string{"cutover-thin.sh", "cutover-finish-thin.sh", "upgrade-thin.sh", "runner-config.py"} { info, err := os.Stat("../../scripts/" + name) if err != nil || info.Mode()&0111 != 0111 { t.Errorf("scripts/%s is not executable: %v %v", name, info, err) } } } // A run cut off right after the restart resumes without restarting the // staged runner again; a rollback cut off after the previous runner // started does not stop it again. func TestUpgradeResumesAfterTheRestart(t *testing.T) { r := newUpgradeRig(t) r.write(filepath.Join(r.root, "killafterrestart"), "", 0600) if out, err := r.run(upgradeTo, "--execute"); err == nil { t.Fatalf("the run was not interrupted:\n%s", out) } 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") { t.Fatalf("did not resume without a second restart: %v\n%s\n%s", err, out, r.read(r.log)) } r.write(filepath.Join(r.root, "killafterstart"), "", 0600) if out, err := r.run("rollback", "--execute"); err == nil { t.Fatalf("the rollback was not interrupted:\n%s", out) } stops := strings.Count(r.read(r.log), "systemctl --user stop") if stops != 1 { t.Fatalf("the rollback stopped the staged runner %d times", stops) } // The previous runner is what the unit now runs. exe := filepath.Join(r.root, "proc", "4242", "exe") r.must(os.Remove(exe)) r.must(os.Symlink(filepath.Join(r.runner, "releases", upgradeFrom, "equipo-runner"), exe)) 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") { t.Fatalf("rollback retry stopped the previous runner again: %v\n%s\n%s", err, out, r.read(r.log)) } } // An older release takes the work of an Engineering step that ran out of // time (done, step_timeout) for finished work: a rollback refuses while // an open task holds such work, before it changes anything, unless the // operator says to go on (UPGRADE_ALLOW_UNFINISHED=1). func TestUpgradeRollbackRefusesWithUnfinishedWork(t *testing.T) { r := newUpgradeRig(t) if out, err := r.run(upgradeTo, "--execute"); err != nil { t.Fatalf("upgrade: %v\n%s", err, out) } exe := filepath.Join(r.root, "proc", "4242", "exe") r.must(os.Remove(exe)) r.must(os.Symlink(filepath.Join(r.runner, "releases", upgradeFrom, "equipo-runner"), exe)) r.write(filepath.Join(r.root, "unfinished"), "2", 0600) before := r.read(r.log) out, err := r.run("rollback", "--execute") 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") { t.Fatalf("rollback with unfinished work: %v\n%s", err, out) } if changed := strings.TrimPrefix(r.read(r.log), before); strings.Contains(changed, "systemctl") || strings.Contains(changed, "promote") { t.Fatalf("changed something before refusing:\n%s", changed) } if plan, err := r.run("rollback"); err != nil || !strings.Contains(plan, "unfinished work") { t.Fatalf("the plan does not name the check: %v\n%s", err, plan) } r.env = []string{"UPGRADE_ALLOW_UNFINISHED=1"} if out, err := r.run("rollback", "--execute"); err != nil || !strings.Contains(r.state(), "rolled_back=1") { t.Fatalf("rollback told to go on: %v\n%s", err, out) } } // Unfinished work that comes in after the first reading, before the // runner stopped, is found once nothing reports steps: the rollback // starts the runner again and refuses, before the instance changes. func TestUpgradeRollbackRefusesUnfinishedWorkThatCameIn(t *testing.T) { r := newUpgradeRig(t) if out, err := r.run(upgradeTo, "--execute"); err != nil { t.Fatalf("upgrade: %v\n%s", err, out) } r.write(filepath.Join(r.root, "unfinished.seq"), "0\n0\n1\n", 0600) before := r.read(r.log) out, err := r.run("rollback", "--execute") if err == nil || !strings.Contains(out, "the runner was started again") || strings.Contains(r.state(), "rolled_back=1") { t.Fatalf("rollback with work that came in: %v\n%s", err, out) } changed := strings.TrimPrefix(r.read(r.log), before) if strings.Contains(changed, "promote") || strings.Contains(changed, "daemon-reload") { t.Fatalf("changed the instance or the unit:\n%s", changed) } inOrder(t, changed, "systemctl --user stop", "systemctl --user start") }