| new file mode 100644 |
| index 000000000..1a2b70aaa |
| --- /dev/null |
| +++ b/internal/activator/rootfs_swap_release_test.go |
| @@ -0,0 +1,1105 @@ |
| +package activator |
| + |
| +// Rootfs apps Plan 3, Group R, R2: the release before an overlap swap. A |
| +// rootfs build that declares `release` runs it from the new image under |
| +// the incumbent's lease before the swap is admitted (releaseBeforeSwap); |
| +// the swap is admitted only on that attempt's proven pass (INV-R5); a |
| +// failure is the build's and the incumbent keeps serving (C8); an open |
| +// release record blocks every transition of the host (INV-R4). Not |
| +// parallel: some tests shrink the release's package vars. |
| + |
| +import ( |
| + "context" |
| + "crypto/sha256" |
| + "encoding/hex" |
| + "errors" |
| + "fmt" |
| + "io" |
| + "os" |
| + "path/filepath" |
| + "regexp" |
| + "slices" |
| + "strings" |
| + "sync" |
| + "sync/atomic" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/lease" |
| + "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/release" |
| + "github.com/carlosframework/platform/internal/rolloutwords" |
| +) |
| + |
| +// relSwapBackend is the swap fake (fakeSwapBackend) with the release seam |
| +// and the process read. Processes answers procs by build version (a build |
| +// not there declares web alone) and counts every read. Each StartRelease |
| +// asks the exec gate, records release:start:<build> in the order log and |
| +// hands out a fresh releaseUnit on units; a settle of it fails while stuck |
| +// and records release:proven:<build> when it proves the unit gone. |
| +type relSwapBackend struct { |
| + *fakeSwapBackend |
| + rmu sync.Mutex |
| + procs map[string]map[string]release.Process |
| + reads int |
| + onProcesses func(id release.StagedIdentity) |
| + onEnv func() |
| + units chan *releaseUnit |
| + starts []string |
| + stuck bool |
| + onSettle func() |
| + settling int |
| + maxSettling int |
| +} |
| + |
| +func (b *relSwapBackend) Processes(_ registry.Route, id release.StagedIdentity) (map[string]release.Process, error) { |
| + b.rmu.Lock() |
| + b.reads++ |
| + hook := b.onProcesses |
| + p, ok := b.procs[id.Version] |
| + b.rmu.Unlock() |
| + if hook != nil { |
| + hook(id) |
| + } |
| + if !ok { |
| + return map[string]release.Process{release.ProcessWeb: {Command: []string{"/app/serve"}}}, nil |
| + } |
| + return p, nil |
| +} |
| + |
| +func (b *relSwapBackend) ReleaseEnv(_ context.Context, rt registry.Route) (map[string]string, error) { |
| + b.rmu.Lock() |
| + hook := b.onEnv |
| + b.rmu.Unlock() |
| + if hook != nil { |
| + hook() |
| + } |
| + return map[string]string{"CARLOS_SOCKET": rt.Socket}, nil |
| +} |
| + |
| +func (b *relSwapBackend) StartRelease(ctx context.Context, _ registry.Route, id release.StagedIdentity, _ map[string]string, _ io.Writer) (CmdUnit, error) { |
| + // The exec gate, as startRootfsCmd asks it right before root. |
| + if err := runExecGate(ctx); err != nil { |
| + return nil, err |
| + } |
| + b.record("release:start:" + id.Version) |
| + u := newReleaseUnit() |
| + u.settle = func(int) error { return b.settle(id.Version) } |
| + b.rmu.Lock() |
| + b.starts = append(b.starts, id.Version) |
| + b.rmu.Unlock() |
| + b.units <- u |
| + return u, nil |
| +} |
| + |
| +func (b *relSwapBackend) settle(build string) error { |
| + b.rmu.Lock() |
| + b.settling++ |
| + b.maxSettling = max(b.maxSettling, b.settling) |
| + stuck, hook := b.stuck, b.onSettle |
| + b.rmu.Unlock() |
| + if hook != nil { |
| + hook() |
| + } |
| + b.rmu.Lock() |
| + b.settling-- |
| + b.rmu.Unlock() |
| + if stuck { |
| + return errors.New("processes remained in the release command's cgroup") |
| + } |
| + b.record("release:proven:" + build) |
| + return nil |
| +} |
| + |
| +func (b *relSwapBackend) setStuck(stuck bool) { |
| + b.rmu.Lock() |
| + b.stuck = stuck |
| + b.rmu.Unlock() |
| +} |
| + |
| +func (b *relSwapBackend) readCount() int { |
| + b.rmu.Lock() |
| + defer b.rmu.Unlock() |
| + return b.reads |
| +} |
| + |
| +func (b *relSwapBackend) startList() []string { |
| + b.rmu.Lock() |
| + defer b.rmu.Unlock() |
| + return slices.Clone(b.starts) |
| +} |
| + |
| +var ( |
| + _ ReleaseBackend = (*relSwapBackend)(nil) |
| + _ ProcessReader = (*relSwapBackend)(nil) |
| + _ SwapBackend = (*relSwapBackend)(nil) |
| +) |
| + |
| +// relSwapFixture is the swap fixture over relSwapBackend: an awake |
| +// incumbent on build v1 (made a rootfs row good on v1, its replica an |
| +// ownedRep with the lease's own signal), build v2 staged (f.staged) |
| +// declaring a release, build v3 staged declaring one too, the events |
| +// captured and the generations file on disk. |
| +type relSwapFixture struct { |
| + *swapFixture |
| + rb *relSwapBackend |
| + sink *rootfsSink |
| + rep *ownedRep |
| + binRoute registry.Route // the row as it woke, before it was made rootfs |
| + v3, v3d string // v3's staged path and digest |
| +} |
| + |
| +func newRelSwapFixture(t *testing.T, rootfs bool) *relSwapFixture { |
| + t.Helper() |
| + var rb *relSwapBackend |
| + f := newSwapFixtureOn(t, func(b *fakeSwapBackend) Backend { |
| + rb = &relSwapBackend{fakeSwapBackend: b, units: make(chan *releaseUnit, 16), |
| + procs: map[string]map[string]release.Process{"v2": releaseDeclared(), "v3": releaseDeclared()}} |
| + return rb |
| + }) |
| + rf := &relSwapFixture{swapFixture: f, rb: rb, sink: &rootfsSink{}} |
| + f.a.Logs = rf.sink |
| + f.a.GenerationsFile = filepath.Join(t.TempDir(), "generations.json") |
| + f.a.mu.Lock() |
| + rf.binRoute = f.a.entries[f.host].route |
| + f.a.mu.Unlock() |
| + rf.v3, rf.v3d = stageTestBuild(t, "v3") |
| + if rootfs { |
| + rf.rep = makeRootfsIncumbent(t, f.a, f.host) |
| + } |
| + return rf |
| +} |
| + |
| +// makeRootfsIncumbent makes host's awake entry a hibernating rootfs row |
| +// of acme/shop, good on v1, its replica an ownedRep an hour ahead, |
| +// installed through installProcLocked. |
| +func makeRootfsIncumbent(t *testing.T, a *Service, host string) *ownedRep { |
| + t.Helper() |
| + a.mu.Lock() |
| + defer a.mu.Unlock() |
| + e := a.entries[host] |
| + e.route.Runtime, e.route.App, e.route.Account = registry.RuntimeRootfsV1, "shop", "acme" |
| + e.route.GoodVersion, e.route.GoodDigest = "v1", "aa11" |
| + fr, ok := e.rep.(*fakeRep) |
| + if !ok { |
| + t.Fatalf("the incumbent's replica is %T", e.rep) |
| + } |
| + rep := newOwnedRep(fr, time.Now().Add(time.Hour)) |
| + t.Cleanup(func() { rep.timer.Stop() }) |
| + a.installProcLocked(e, rep, e.proc, e.gen) |
| + return rep |
| +} |
| + |
| +// stageTestBuild writes build ver's artifact and answers its path and |
| +// digest. |
| +func stageTestBuild(t *testing.T, ver string) (string, string) { |
| + t.Helper() |
| + dir := filepath.Join(t.TempDir(), "versions", ver) |
| + if err := os.MkdirAll(dir, 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + path := filepath.Join(dir, "app") |
| + body := []byte("build " + ver) |
| + if err := writeExecutable(path, body, 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + sum := sha256.Sum256(body) |
| + return path, hex.EncodeToString(sum[:]) |
| +} |
| + |
| +func (f *relSwapFixture) attemptV3(mode SwapMode) Attempt { |
| + at := f.attempt(mode) |
| + at.Build, at.StagedPath, at.Digest = "v3", f.v3, f.v3d |
| + return at |
| +} |
| + |
| +// swapAsync runs Swap in the background. |
| +func (f *relSwapFixture) swapAsync(at Attempt) <-chan SwapResult { |
| + ch := make(chan SwapResult, 1) |
| + go func() { ch <- f.a.Swap(context.Background(), at, f.newest) }() |
| + return ch |
| +} |
| + |
| +func waitSwap(t *testing.T, ch <-chan SwapResult) SwapResult { |
| + t.Helper() |
| + select { |
| + case res := <-ch: |
| + return res |
| + case <-time.After(30 * time.Second): |
| + t.Fatal("Swap did not return") |
| + } |
| + return SwapResult{} |
| +} |
| + |
| +// nextUnit waits for the next release unit StartRelease hands out. |
| +func (f *relSwapFixture) nextUnit(t *testing.T) *releaseUnit { |
| + t.Helper() |
| + select { |
| + case u := <-f.rb.units: |
| + return u |
| + case <-time.After(10 * time.Second): |
| + t.Fatal("no release started") |
| + } |
| + return nil |
| +} |
| + |
| +// noUnit fails t if a release unit was handed out. |
| +func (f *relSwapFixture) noUnit(t *testing.T, when string) { |
| + t.Helper() |
| + select { |
| + case <-f.rb.units: |
| + t.Fatalf("%s: a release started", when) |
| + default: |
| + } |
| +} |
| + |
| +func (f *relSwapFixture) record() *releaseOp { |
| + f.a.mu.Lock() |
| + defer f.a.mu.Unlock() |
| + return f.a.releaseOps[f.host] |
| +} |
| + |
| +// incumbentServing fails t unless the incumbent is still current, awake, |
| +// unstopped and the host's socket, with no member in its operation set. |
| +func (f *relSwapFixture) incumbentServing(t *testing.T, proc *fakeProc, when string) { |
| + t.Helper() |
| + cur, members := f.a.genStateForTest(f.host) |
| + if cur.N != f.incumbent.N || len(members) != 0 { |
| + t.Errorf("%s: current generation %d, members %v; want the incumbent %d alone", when, cur.N, members, f.incumbent.N) |
| + } |
| + if proc.isStopped() { |
| + t.Errorf("%s: the incumbent was stopped", when) |
| + } |
| + if got := f.appSocket(t); got != f.incumbent.Socket { |
| + t.Errorf("%s: AppSocket %q, want the incumbent's %q", when, got, f.incumbent.Socket) |
| + } |
| + f.a.mu.Lock() |
| + e := f.a.entries[f.host] |
| + awake := e != nil && e.state == actAwake && e.stopWhy == "" |
| + f.a.mu.Unlock() |
| + if !awake { |
| + t.Errorf("%s: the incumbent is no longer awake", when) |
| + } |
| +} |
| + |
| +// nothingAdmitted fails t if any successor was admitted or started. |
| +func (f *relSwapFixture) nothingAdmitted(t *testing.T, when string) { |
| + t.Helper() |
| + if gens, raises, _ := f.b.calls(); gens != 0 || raises != 0 { |
| + t.Errorf("%s: StartStaged %d, raises %d; want neither", when, gens, raises) |
| + } |
| +} |
| + |
| +// proveRecord ends the stuck settle and has the sweep's pass prove the |
| +// record (retrySettle is paced by actStopTimeout of the fake clock). |
| +func (f *relSwapFixture) proveRecord(t *testing.T) { |
| + t.Helper() |
| + f.rb.setStuck(false) |
| + f.clk.advance(actStopTimeout + time.Second) |
| + f.a.mu.Lock() |
| + f.a.finishPendingStops() |
| + f.a.mu.Unlock() |
| + waitFor(t, "the reaper to prove the release record", func() bool { return f.record() == nil }) |
| +} |
| + |
| +func eventsLike(msgs []string, head string) int { |
| + n := 0 |
| + for _, m := range msgs { |
| + if strings.HasPrefix(m, head) { |
| + n++ |
| + } |
| + } |
| + return n |
| +} |
| + |
| +func TestOverlapReleaseFailureKeepsTheIncumbent(t *testing.T) { |
| + f := newRelSwapFixture(t, true) |
| + incumbent := f.b.lastProc() |
| + ch := f.swapAsync(f.attempt(ModeOverlap)) |
| + f.nextUnit(t).done("1", "1", "exit-code") |
| + res := waitSwap(t, ch) |
| + if res.Outcome != SwapFailed || !strings.HasPrefix(res.Reason, "release: ") || !res.Executed || res.Mode != ModeOverlap { |
| + t.Fatalf("Swap = %+v; want failed, executed, overlap, reason \"release: …\"", res) |
| + } |
| + if want := "release: " + (&ReleaseError{Build: "v2", Exit: 1}).Error(); res.Reason != want { |
| + t.Errorf("Reason = %q, want %q", res.Reason, want) |
| + } |
| + f.nothingAdmitted(t, "after the release failed") |
| + f.incumbentServing(t, incumbent, "after the release failed") |
| + if op := f.record(); op != nil { |
| + t.Error("the release record outlived its proof") |
| + } |
| + if n := eventsLike(f.sink.msgs(), "the release command of build v2 exited with status 1"); n != 1 { |
| + t.Errorf("C05 emitted %d times: %q", n, f.sink.msgs()) |
| + } |
| +} |
| + |
| +func TestOverlapReleaseTimeoutKeepsTheIncumbent(t *testing.T) { |
| + f := newRelSwapFixture(t, true) |
| + procs := releaseDeclared() |
| + p := procs[release.ProcessRelease] |
| + p.TimeoutSec = 1 |
| + procs[release.ProcessRelease] = p |
| + f.rb.procs["v2"] = procs |
| + incumbent := f.b.lastProc() |
| + ch := f.swapAsync(f.attempt(ModeOverlap)) |
| + u := f.nextUnit(t) // never answers: the release runs into its timeout |
| + res := waitSwap(t, ch) |
| + if res.Outcome != SwapFailed || !strings.HasPrefix(res.Reason, "release timed out: ") || !res.Executed { |
| + t.Fatalf("Swap = %+v; want failed, executed, reason \"release timed out: …\"", res) |
| + } |
| + if !u.isProven() { |
| + t.Error("the timed-out release's unit was not proven gone before the failure") |
| + } |
| + f.nothingAdmitted(t, "after the timeout") |
| + f.incumbentServing(t, incumbent, "after the timeout") |
| + if n := eventsLike(f.sink.msgs(), "the release command of build v2 did not finish within 1s"); n != 1 { |
| + t.Errorf("C07 emitted %d times: %q", n, f.sink.msgs()) |
| + } |
| +} |
| + |
| +func TestOverlapRunsReleaseBeforeTheSwapIsAdmitted(t *testing.T) { |
| + f := newRelSwapFixture(t, true) |
| + var admittedClean atomic.Bool |
| + f.b.onRaise = func() { |
| + // The raise follows the admission at once: the member is in the |
| + // set and the release record is already gone. |
| + f.a.mu.Lock() |
| + e := f.a.entries[f.host] |
| + admittedClean.Store(len(e.ops.members) == 1 && f.a.releaseOps[f.host] == nil) |
| + f.a.mu.Unlock() |
| + f.b.record("admitted") |
| + } |
| + ch := f.swapAsync(f.attempt(ModeOverlap)) |
| + u := f.nextUnit(t) |
| + if f.record() == nil { |
| + t.Fatal("no release record while the release runs") |
| + } |
| + if _, members := f.a.genStateForTest(f.host); len(members) != 0 { |
| + t.Fatalf("a member was admitted while the release ran: %v", members) |
| + } |
| + u.done("1", "0", "success") |
| + res := waitSwap(t, ch) |
| + if res.Outcome != SwapDone || res.Gen.Build != "v2" || !res.Executed { |
| + t.Fatalf("Swap = %+v", res) |
| + } |
| + ev := f.b.events() |
| + order := []int{ |
| + indexPrefix(ev, "release:start:v2"), |
| + indexPrefix(ev, "release:proven:v2"), |
| + indexPrefix(ev, "admitted"), |
| + indexPrefix(ev, "startGen:"+f.staged), |
| + indexPrefix(ev, "switch:"), |
| + } |
| + for i := range order { |
| + if order[i] < 0 || (i > 0 && order[i] < order[i-1]) { |
| + t.Fatalf("event order %v (indexes %v): want release start, release proven, admission, StartStaged, switch", ev, order) |
| + } |
| + } |
| + if !admittedClean.Load() { |
| + t.Error("the member was admitted beside an open release record") |
| + } |
| + if msgs := f.sink.msgs(); eventsLike(msgs, "running the release command of build v2") != 1 || |
| + eventsLike(msgs, "the release command of build v2 finished in") != 1 { |
| + t.Errorf("events %q: want C03 and C04 once each", msgs) |
| + } |
| +} |
| + |
| +// INV-R5: an overlap of a build that needs a release is admitted only with |
| +// this attempt's pass for this build, with no record open, and with |
| +// ownership rechecked at the admission and at the exec. |
| +func TestSwapAdmittedOnlyOnAProvenPass(t *testing.T) { |
| + overlap := func(f *relSwapFixture, pass *releasePass, due bool) SwapResult { |
| + return f.a.swapOverlap(context.Background(), f.a.backend.(SwapBackend), f.attempt(ModeOverlap), f.newest, pass, due) |
| + } |
| + passOf := func(f *relSwapFixture) *releasePass { |
| + at := f.attempt(ModeOverlap) |
| + return &releasePass{host: at.Host, schedRev: at.SchedRev, |
| + build: release.StagedIdentity{Version: at.Build, StagedPath: at.StagedPath, Digest: at.Digest, Demand: at.Demand}} |
| + } |
| + for _, tc := range []struct { |
| + name string |
| + pass func(f *relSwapFixture) *releasePass |
| + }{ |
| + {"none", func(*relSwapFixture) *releasePass { return nil }}, |
| + {"another SchedRev", func(f *relSwapFixture) *releasePass { p := passOf(f); p.schedRev++; return p }}, |
| + {"another digest of the build", func(f *relSwapFixture) *releasePass { |
| + p := passOf(f) |
| + p.build.Digest = strings.Repeat("f", 64) |
| + return p |
| + }}, |
| + {"another staged path", func(f *relSwapFixture) *releasePass { p := passOf(f); p.build.StagedPath += ".old"; return p }}, |
| + {"another demand", func(f *relSwapFixture) *releasePass { p := passOf(f); p.build.Demand = "hold"; return p }}, |
| + {"another build", func(f *relSwapFixture) *releasePass { p := passOf(f); p.build.Version = "v3"; return p }}, |
| + {"another host", func(f *relSwapFixture) *releasePass { p := passOf(f); p.host = "other.example.com"; return p }}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + f := newRelSwapFixture(t, true) |
| + incumbent := f.b.lastProc() |
| + res := overlap(f, tc.pass(f), true) |
| + if res.Outcome != SwapAbandoned || res.Reason != errReleaseNotPassed.Error() || res.Executed { |
| + t.Fatalf("swapOverlap = %+v; want abandoned with errReleaseNotPassed, not executed", res) |
| + } |
| + f.nothingAdmitted(t, tc.name) |
| + f.incumbentServing(t, incumbent, tc.name) |
| + }) |
| + } |
| + t.Run("the attempt's own pass", func(t *testing.T) { |
| + f := newRelSwapFixture(t, true) |
| + if res := overlap(f, passOf(f), true); res.Outcome != SwapDone { |
| + t.Fatalf("swapOverlap with the matching pass = %+v", res) |
| + } |
| + }) |
| + t.Run("no release due", func(t *testing.T) { |
| + f := newRelSwapFixture(t, true) |
| + if res := overlap(f, nil, false); res.Outcome != SwapDone { |
| + t.Fatalf("swapOverlap with nothing due = %+v", res) |
| + } |
| + }) |
| + t.Run("ownership at the admission", func(t *testing.T) { |
| + f := newRelSwapFixture(t, true) |
| + f.rep.lose(lease.ErrLost) |
| + if res := overlap(f, passOf(f), true); res.Outcome != SwapAbandoned || !strings.Contains(res.Reason, errSwapLeaseLost.Error()) { |
| + t.Fatalf("swapOverlap after the lease was lost = %+v", res) |
| + } |
| + f.nothingAdmitted(t, "the lease lost before the admission") |
| + }) |
| + t.Run("ownership at the exec", func(t *testing.T) { |
| + f := newRelSwapFixture(t, true) |
| + f.b.onRaise = func() { f.rep.lose(lease.ErrLost) } |
| + if res := overlap(f, passOf(f), true); res.Outcome != SwapAbandoned || !strings.Contains(res.Reason, errSwapLeaseLost.Error()) { |
| + t.Fatalf("swapOverlap with the lease lost after the admission = %+v", res) |
| + } |
| + if gens, _, _ := f.b.calls(); gens != 0 { |
| + t.Errorf("StartStaged %d times after the lease was lost", gens) |
| + } |
| + }) |
| + t.Run("an unproven release issues no pass", func(t *testing.T) { |
| + f := newRelSwapFixture(t, true) |
| + f.rb.setStuck(true) |
| + type answer struct { |
| + pass *releasePass |
| + due bool |
| + res SwapResult |
| + passed bool |
| + } |
| + ch := make(chan answer, 1) |
| + go func() { |
| + p, due, res, ok := f.a.releaseBeforeSwap(context.Background(), f.attempt(ModeOverlap), f.newest) |
| + ch <- answer{p, due, res, ok} |
| + }() |
| + f.nextUnit(t).done("1", "0", "success") |
| + var got answer |
| + select { |
| + case got = <-ch: |
| + case <-time.After(30 * time.Second): |
| + t.Fatal("releaseBeforeSwap did not return") |
| + } |
| + if got.pass != nil || got.passed || got.res.Outcome != SwapAbandoned || !got.res.Executed || |
| + !strings.Contains(got.res.Reason, errReleaseUnproven.Error()) { |
| + t.Fatalf("releaseBeforeSwap with the unit unproven = pass %v, ok %v, %+v", got.pass, got.passed, got.res) |
| + } |
| + if f.record() == nil { |
| + t.Fatal("the unproven release left no record") |
| + } |
| + f.nothingAdmitted(t, "after an unproven release") |
| + f.proveRecord(t) |
| + }) |
| +} |
| + |
| +// INV-R4: an open release record blocks every transition of the host, |
| +// checked first: before anything reads the build or decides whether it |
| +// needs a release, and again at the admission and at the exec. |
| +func TestOpenReleaseBlocksEveryTransition(t *testing.T) { |
| + f := newRelSwapFixture(t, true) |
| + incumbent := f.b.lastProc() |
| + f.rb.setStuck(true) |
| + ch := f.swapAsync(f.attempt(ModeOverlap)) |
| + f.nextUnit(t).done("1", "0", "success") |
| + if res := waitSwap(t, ch); res.Outcome != SwapAbandoned || !res.Executed { |
| + t.Fatalf("B's unproven release = %+v; want abandoned, executed", res) |
| + } |
| + op := f.record() |
| + if op == nil || op.state != releaseNotProven { |
| + t.Fatal("B's release left no unproven record") |
| + } |
| + reads := f.rb.readCount() |
| + good := f.attempt(ModeOverlap) |
| + // A config or restart attempt re-runs the good build. Its staged copy |
| + // here is not on disk: an attempt that read anything before the |
| + // record would be refused as staging instead. |
| + good.Build, good.StagedPath, good.Digest = "v1", f.incumbent.StagedPath, strings.Repeat("a", 64) |
| + config, restart := good, good |
| + config.Cause, config.ConfigRev = CauseConfig, "cfg-2" |
| + restart.Cause, restart.RestartID = CauseRestart, "rs-1" |
| + hold := f.attempt(ModeHold) |
| + noRelease := f.attemptV3(ModeOverlap) |
| + delete(f.rb.procs, "v3") // C declares web alone |
| + for name, at := range map[string]Attempt{ |
| + "a config attempt onto the good build": config, |
| + "a restart attempt": restart, |
| + "a hold attempt": hold, |
| + "an overlap of a build with no release": noRelease, |
| + } { |
| + res := f.a.Swap(context.Background(), at, f.newest) |
| + if res.Outcome != SwapAbandoned || res.Reason != errReleaseOpen.Error() || res.Executed { |
| + t.Errorf("%s: Swap = %+v; want abandoned with errReleaseOpen, not executed", name, res) |
| + } |
| + } |
| + if n := f.rb.readCount(); n != reads { |
| + t.Errorf("the declared processes were read %d times beside the open record", n-reads) |
| + } |
| + f.nothingAdmitted(t, "beside the open record") |
| + f.noUnit(t, "beside the open record") |
| + f.incumbentServing(t, incumbent, "beside the open record") |
| + |
| + // Both hold admissions ask the record (holdAdmitLocked), and so does |
| + // the overlap's own release half (releaseAdmitsSwapLocked). |
| + f.a.mu.Lock() |
| + holdErr := f.a.holdAdmitLocked(f.a.entries[f.host], hold, f.newest) |
| + overlapErr := f.a.releaseAdmitsSwapLocked(noRelease, Gen{Build: "v3", StagedPath: f.v3, Digest: f.v3d}, nil, false) |
| + f.a.mu.Unlock() |
| + if !errors.Is(holdErr, errReleaseOpen) || !errors.Is(overlapErr, errReleaseOpen) { |
| + t.Errorf("with the record open: holdAdmitLocked = %v, the overlap's release check = %v", holdErr, overlapErr) |
| + } |
| + |
| + f.proveRecord(t) |
| + ch = f.swapAsync(f.attempt(ModeOverlap)) |
| + f.nextUnit(t).done("1", "0", "success") |
| + if res := waitSwap(t, ch); res.Outcome != SwapDone || res.Gen.Build != "v2" { |
| + t.Fatalf("the next attempt once the record is proven = %+v", res) |
| + } |
| + if got := f.rb.startList(); !slices.Equal(got, []string{"v2", "v2"}) { |
| + t.Errorf("releases started %v; want B's twice (the unproven one, then the next attempt's)", got) |
| + } |
| +} |
| + |
| +// A record that opens after the top of Swap is still refused at the |
| +// admission, at Step 3a and at the exec, and each is an abandonment, |
| +// never the build's failure. (Only a stand-in record can appear there: |
| +// the incumbent's own release refuses beside a member.) |
| +func TestOpenReleaseIsAskedAtTheAdmissionAndTheExec(t *testing.T) { |
| + standIn := func(f *relSwapFixture) func() { |
| + return func() { |
| + f.a.mu.Lock() |
| + defer f.a.mu.Unlock() |
| + if f.a.releaseOps == nil { |
| + f.a.releaseOps = map[string]*releaseOp{} |
| + } |
| + f.a.releaseOps[f.host] = &releaseOp{host: f.host, state: releaseRunning, done: make(chan struct{})} |
| + } |
| + } |
| + dropStandIn := func(t *testing.T, f *relSwapFixture) { |
| + t.Cleanup(func() { |
| + f.a.mu.Lock() |
| + delete(f.a.releaseOps, f.host) |
| + f.a.mu.Unlock() |
| + }) |
| + } |
| + t.Run("the admission", func(t *testing.T) { |
| + f := newRelSwapFixture(t, true) |
| + dropStandIn(t, f) |
| + delete(f.rb.procs, "v3") |
| + f.rb.onProcesses = func(release.StagedIdentity) { standIn(f)() } |
| + res := f.a.Swap(context.Background(), f.attemptV3(ModeOverlap), f.newest) |
| + if res.Outcome != SwapAbandoned || res.Reason != errReleaseOpen.Error() || res.Executed { |
| + t.Fatalf("Swap = %+v", res) |
| + } |
| + f.nothingAdmitted(t, "a record opened before the admission") |
| + }) |
| + t.Run("Step 3a", func(t *testing.T) { |
| + f := newRelSwapFixture(t, true) |
| + dropStandIn(t, f) |
| + delete(f.rb.procs, "v3") |
| + f.b.onRaise = standIn(f) |
| + res := f.a.Swap(context.Background(), f.attemptV3(ModeOverlap), f.newest) |
| + if res.Outcome != SwapAbandoned || res.Reason != errReleaseOpen.Error() || res.Executed { |
| + t.Fatalf("Swap = %+v", res) |
| + } |
| + if gens, _, _ := f.b.calls(); gens != 0 { |
| + t.Errorf("StartStaged %d times beside the record", gens) |
| + } |
| + }) |
| + t.Run("the exec", func(t *testing.T) { |
| + f := newRelSwapFixture(t, true) |
| + dropStandIn(t, f) |
| + delete(f.rb.procs, "v3") |
| + gate := make(chan struct{}) |
| + f.b.genGate = gate |
| + ch := f.swapAsync(f.attemptV3(ModeOverlap)) |
| + waitFor(t, "StartStaged", func() bool { return indexPrefix(f.b.events(), "startGen:") >= 0 }) |
| + standIn(f)() |
| + close(gate) |
| + res := waitSwap(t, ch) |
| + if res.Outcome != SwapAbandoned || !strings.Contains(res.Reason, errReleaseOpen.Error()) || res.Executed { |
| + t.Fatalf("Swap = %+v; want abandoned at the exec, never failed", res) |
| + } |
| + if _, p := f.b.lastGen(); p != nil { |
| + t.Error("a successor was launched beside the record") |
| + } |
| + }) |
| +} |
| + |
| +func TestOverlapAbandonsWhileARunIsOpen(t *testing.T) { |
| + f := newRelSwapFixture(t, true) |
| + incumbent := f.b.lastProc() |
| + f.a.mu.Lock() |
| + e := f.a.entries[f.host] |
| + e.cmd = newCmdHold("run_a", func() {}, f.a.now) |
| + f.a.mu.Unlock() |
| + t.Cleanup(func() { |
| + f.a.mu.Lock() |
| + e.cmd = nil |
| + f.a.mu.Unlock() |
| + }) |
| + res := f.a.Swap(context.Background(), f.attempt(ModeOverlap), f.newest) |
| + if res.Outcome != SwapAbandoned || !strings.Contains(res.Reason, errSwapBusy.Error()) || res.Executed { |
| + t.Fatalf("Swap beside an open run = %+v; want abandoned busy, not executed", res) |
| + } |
| + f.noUnit(t, "beside an open run") |
| + f.nothingAdmitted(t, "beside an open run") |
| + f.incumbentServing(t, incumbent, "beside an open run") |
| + if f.record() != nil { |
| + t.Error("a release record was left beside an open run") |
| + } |
| +} |
| + |
| +// Review Focus 6: a lease loss while the release runs stops it and proves |
| +// its unit gone before the safety deadline; the attempt is abandoned, |
| +// never the build's failure. |
| +func TestSwapReleaseStopsOnLeaseLoss(t *testing.T) { |
| + shrinkRelease(t, 200*time.Millisecond, 50*time.Millisecond) |
| + f := newRelSwapFixture(t, true) |
| + until := time.Now().Add(10 * time.Second) |
| + f.rep.mu.Lock() |
| + f.rep.until = until |
| + f.rep.mu.Unlock() |
| + ch := f.swapAsync(f.attempt(ModeOverlap)) |
| + u := f.nextUnit(t) |
| + f.rep.lose(lease.ErrLost) |
| + res := waitSwap(t, ch) |
| + if res.Outcome != SwapAbandoned || !res.Executed || !strings.Contains(res.Reason, errReleaseLeaseLost.Error()) { |
| + t.Fatalf("Swap after the lease was lost = %+v; want abandoned, executed, the lease lost", res) |
| + } |
| + stops := u.stopTimes() |
| + if !u.isProven() || len(stops) == 0 || !stops[len(stops)-1].Before(until) { |
| + t.Fatalf("the release unit: proven %v, stops %v, deadline %v", u.isProven(), stops, until) |
| + } |
| + f.nothingAdmitted(t, "after the lease loss") |
| + msgs := f.sink.msgs() |
| + for _, head := range []string{"the release command of build v2 exited", "the release command of build v2 was ended by signal", "the release command of build v2 did not finish"} { |
| + if eventsLike(msgs, head) != 0 { |
| + t.Errorf("a failure event %q after a lease loss: %q", head, msgs) |
| + } |
| + } |
| + if n := eventsLike(msgs, "the release command of build v2 was stopped because this box no longer holds the instance"); n != 1 { |
| + t.Errorf("C29 emitted %d times: %q", n, msgs) |
| + } |
| +} |
| + |
| +// A passed release ran the build's release process against the database, |
| +// so an attempt abandoned after it (here superseded between the pass and |
| +// the admission) is executed: the scheduler must not count it as having |
| +// spent nothing (SwapResult.Executed). |
| +func TestPassedReleaseCountsAsExecuted(t *testing.T) { |
| + f := newRelSwapFixture(t, true) |
| + incumbent := f.b.lastProc() |
| + f.rb.onSettle = func() { f.rev.Add(1) } // a newer attempt arrives as the release is proven |
| + ch := f.swapAsync(f.attempt(ModeOverlap)) |
| + f.nextUnit(t).done("1", "0", "success") |
| + res := waitSwap(t, ch) |
| + if res.Outcome != SwapAbandoned || res.Reason != errSwapSuperseded.Error() || !res.Executed { |
| + t.Fatalf("Swap superseded after its release passed = %+v; want abandoned, executed", res) |
| + } |
| + f.nothingAdmitted(t, "after the pass, superseded") |
| + f.incumbentServing(t, incumbent, "after the pass, superseded") |
| +} |
| + |
| +// [pf R2.6]: the release runs under the attempt's own context, so the |
| +// agent's life ending (Shutdown's first step, lifeStop) ends it: its unit |
| +// is settled and proven, and the attempt is abandoned, never failed. |
| +func TestShutdownCancelsAReleaseBeforeASwap(t *testing.T) { |
| + f := newRelSwapFixture(t, true) |
| + ch := f.swapAsync(f.attempt(ModeOverlap)) |
| + u := f.nextUnit(t) |
| + f.a.lifeStop() |
| + res := waitSwap(t, ch) |
| + if res.Outcome != SwapAbandoned || !res.Executed { |
| + t.Fatalf("Swap after the agent's life ended = %+v; want abandoned, executed", res) |
| + } |
| + if !u.isProven() || f.record() != nil { |
| + t.Fatalf("the release unit proven %v, record left %v", u.isProven(), f.record() != nil) |
| + } |
| + f.nothingAdmitted(t, "after the agent's life ended") |
| +} |
| + |
| +// RACE: the lease's loss and the sweep's lease check at once. One settle |
| +// at a time on the record, the incumbent stopped once (one stop begun; the |
| +// pending stop's proof asks the exited process's Stop again, which is not |
| +// a second stop), and its replica closed (the lease given back) only after |
| +// the record is gone. |
| +func TestSwapReleaseLeaseLossRacesTheSweep(t *testing.T) { |
| + shrinkRelease(t, 200*time.Millisecond, 50*time.Millisecond) |
| + logs := captureLockedLog(t) |
| + for round := 0; round < 5; round++ { |
| + f := newRelSwapFixture(t, true) |
| + incumbent := f.b.lastProc() |
| + f.rb.onSettle = func() { time.Sleep(5 * time.Millisecond) } |
| + var closedOverRecord atomic.Bool |
| + f.rep.setOnClose(func() { |
| + if f.record() != nil { |
| + closedOverRecord.Store(true) |
| + } |
| + }) |
| + ch := f.swapAsync(f.attempt(ModeOverlap)) |
| + f.nextUnit(t) |
| + start := make(chan struct{}) |
| + var wg sync.WaitGroup |
| + wg.Add(2) |
| + go func() { defer wg.Done(); <-start; f.rep.lose(lease.ErrLost) }() |
| + go func() { defer wg.Done(); <-start; f.a.sweepOnce() }() |
| + close(start) |
| + wg.Wait() |
| + res := waitSwap(t, ch) |
| + if res.Outcome != SwapAbandoned || !res.Executed { |
| + t.Fatalf("round %d: Swap = %+v; want abandoned, executed", round, res) |
| + } |
| + for i := 0; i < 200 && f.a.entryCount() > 0; i++ { |
| + f.a.sweepOnce() |
| + time.Sleep(10 * time.Millisecond) |
| + } |
| + if f.a.entryCount() != 0 { |
| + t.Fatalf("round %d: the incumbent's stop never finished", round) |
| + } |
| + f.rb.rmu.Lock() |
| + most := f.rb.maxSettling |
| + f.rb.rmu.Unlock() |
| + if most != 1 { |
| + t.Errorf("round %d: %d settles of the record ran at once", round, most) |
| + } |
| + if n := strings.Count(logs.String(), "LEASE LOST for "+f.host); n != round+1 { |
| + t.Errorf("round %d: %d stops begun for the lease's loss over %d rounds", round, n, round+1) |
| + } |
| + // The stop's last step logs it, after the generations file's write. |
| + waitFor(t, "the stop's end", func() bool { return strings.Count(logs.String(), f.host+" hibernated") >= round+1 }) |
| + if n := strings.Count(logs.String(), f.host+" hibernated"); n != round+1 || !incumbent.isStopped() { |
| + t.Errorf("round %d: %d stops finished over %d rounds, the incumbent stopped %v", round, n, round+1, incumbent.isStopped()) |
| + } |
| + if closedOverRecord.Load() || f.rep.closeCount() != 1 { |
| + t.Errorf("round %d: replica closed %d times, over an open record %v", round, f.rep.closeCount(), closedOverRecord.Load()) |
| + } |
| + f.nothingAdmitted(t, "after the race") |
| + } |
| +} |
| + |
| +// INV-R3: a release that exits 1 and whose settle fails is unproven, not |
| +// the build's failure; once proven, the next attempt runs it again. |
| +func TestUnprovenSwapReleaseIsNotABuildFailure(t *testing.T) { |
| + f := newRelSwapFixture(t, true) |
| + incumbent := f.b.lastProc() |
| + f.rb.setStuck(true) |
| + ch := f.swapAsync(f.attempt(ModeOverlap)) |
| + f.nextUnit(t).done("1", "1", "exit-code") |
| + res := waitSwap(t, ch) |
| + if res.Outcome != SwapAbandoned || !res.Executed || !strings.Contains(res.Reason, errReleaseUnproven.Error()) { |
| + t.Fatalf("Swap = %+v; want abandoned (unproven), executed", res) |
| + } |
| + if eventsLike(f.sink.msgs(), "the release command of build v2 exited") != 0 { |
| + t.Errorf("a failure event for an unproven release: %q", f.sink.msgs()) |
| + } |
| + f.nothingAdmitted(t, "after the unproven release") |
| + f.incumbentServing(t, incumbent, "after the unproven release") |
| + f.proveRecord(t) |
| + ch = f.swapAsync(f.attempt(ModeOverlap)) |
| + f.nextUnit(t).done("1", "1", "exit-code") |
| + if res := waitSwap(t, ch); res.Outcome != SwapFailed { |
| + t.Fatalf("the next attempt = %+v; want the release run again, and failed", res) |
| + } |
| + if got := f.rb.startList(); !slices.Equal(got, []string{"v2", "v2"}) { |
| + t.Errorf("releases started %v", got) |
| + } |
| +} |
| + |
| +// C8: a hibernating incumbent is neither idled nor ceilinged while the |
| +// release runs; the ceiling fires on the first sweep after it ends. |
| +func TestIncumbentNeverIdlesThroughAMigration(t *testing.T) { |
| + logs := captureLockedLog(t) |
| + f := newRelSwapFixture(t, true) |
| + incumbent := f.b.lastProc() |
| + f.a.SetMaxRunFor(func(registry.Route) time.Duration { return 3 * time.Hour }) |
| + ch := f.swapAsync(f.attempt(ModeOverlap)) |
| + u := f.nextUnit(t) |
| + stopping := func() bool { |
| + f.a.mu.Lock() |
| + defer f.a.mu.Unlock() |
| + e := f.a.entries[f.host] |
| + return e == nil || e.state != actAwake || e.stopWhy != "" |
| + } |
| + // Past the idle window (an hour), below the ceiling. |
| + f.clk.advance(2 * time.Hour) |
| + f.a.sweepOnce() |
| + if stopping() || incumbent.isStopped() { |
| + t.Fatal("the incumbent was idled while its successor's release ran") |
| + } |
| + // Past the ceiling too, with a request in flight so only the ceiling |
| + // could stop it. |
| + f.a.mu.Lock() |
| + f.a.entries[f.host].inflight++ |
| + f.a.mu.Unlock() |
| + f.clk.advance(2 * time.Hour) |
| + f.a.sweepOnce() |
| + if stopping() || incumbent.isStopped() { |
| + t.Fatal("the incumbent was stopped by the ceiling while its successor's release ran") |
| + } |
| + u.done("1", "1", "exit-code") |
| + if res := waitSwap(t, ch); res.Outcome != SwapFailed { |
| + t.Fatalf("Swap = %+v", res) |
| + } |
| + f.a.sweepOnce() |
| + if !stopping() || !strings.Contains(logs.String(), "past its plan's ceiling") { |
| + t.Fatalf("the ceiling did not fire on the first sweep after the release (log %q)", logs.String()) |
| + } |
| + f.a.mu.Lock() |
| + if e := f.a.entries[f.host]; e != nil { |
| + e.inflight-- |
| + } |
| + f.a.mu.Unlock() |
| + for i := 0; i < 200 && f.a.entryCount() > 0; i++ { |
| + f.a.sweepOnce() |
| + time.Sleep(10 * time.Millisecond) |
| + } |
| + if f.a.entryCount() != 0 { |
| + t.Fatal("the ceiling's stop never finished") |
| + } |
| + waitFor(t, "the stop's end", func() bool { return strings.Contains(logs.String(), f.host+" hibernated") }) |
| +} |
| + |
| +// swapNoRelease runs at and fails t at once if a release starts for it. |
| +func (f *relSwapFixture) swapNoRelease(t *testing.T, at Attempt) SwapResult { |
| + t.Helper() |
| + ch := f.swapAsync(at) |
| + select { |
| + case res := <-ch: |
| + return res |
| + case <-f.rb.units: |
| + t.Fatal("a release started") |
| + case <-time.After(30 * time.Second): |
| + t.Fatal("Swap did not return") |
| + } |
| + return SwapResult{} |
| +} |
| + |
| +func TestNoReleaseForADisabledRoute(t *testing.T) { |
| + f := newRelSwapFixture(t, true) |
| + f.a.SetDisabledFor(func(rt registry.Route) bool { return rt.Host == f.host }) |
| + res := f.swapNoRelease(t, f.attempt(ModeOverlap)) |
| + if res.Outcome != SwapAbandoned || res.Reason != errReleaseTurnedOff.Error() || res.Executed { |
| + t.Fatalf("Swap of a plan-disabled route = %+v", res) |
| + } |
| + if verdictOf(errReleaseTurnedOff) != gateNoOwnership { |
| + t.Error("errReleaseTurnedOff is not a refusal") |
| + } |
| + f.noUnit(t, "for a disabled route") |
| + if n := f.rb.readCount(); n != 0 { |
| + t.Errorf("the declared processes were read %d times for a disabled route", n) |
| + } |
| + f.nothingAdmitted(t, "for a disabled route") |
| +} |
| + |
| +// [pf R2.3]: the release's gate is the swap's own checks, asked before |
| +// anything is read and again at the release's exec: a superseded attempt |
| +// starts no release. |
| +func TestNoReleaseForASupersededAttempt(t *testing.T) { |
| + t.Run("before the release", func(t *testing.T) { |
| + f := newRelSwapFixture(t, true) |
| + at := f.attempt(ModeOverlap) |
| + f.rev.Add(1) |
| + res := f.swapNoRelease(t, at) |
| + if res.Outcome != SwapAbandoned || res.Reason != errSwapSuperseded.Error() || res.Executed { |
| + t.Fatalf("Swap of a superseded attempt = %+v", res) |
| + } |
| + if n := f.rb.readCount(); n != 0 { |
| + t.Errorf("the declared processes were read %d times for a superseded attempt", n) |
| + } |
| + f.noUnit(t, "for a superseded attempt") |
| + f.nothingAdmitted(t, "for a superseded attempt") |
| + }) |
| + t.Run("at the release's exec", func(t *testing.T) { |
| + f := newRelSwapFixture(t, true) |
| + f.rb.onEnv = func() { f.rev.Add(1) } |
| + res := f.swapNoRelease(t, f.attempt(ModeOverlap)) |
| + if res.Outcome != SwapAbandoned || !strings.Contains(res.Reason, errSwapSuperseded.Error()) || res.Executed { |
| + t.Fatalf("Swap superseded before the release's exec = %+v", res) |
| + } |
| + f.noUnit(t, "superseded at the exec") |
| + f.nothingAdmitted(t, "superseded at the exec") |
| + if f.record() != nil { |
| + t.Error("a release record outlived the refused start") |
| + } |
| + }) |
| +} |
| + |
| +func TestNoReleaseBesideAnUnfinishedOperation(t *testing.T) { |
| + f := newRelSwapFixture(t, true) |
| + delete(f.rb.procs, "v2") // B declares no release |
| + incumbent := f.b.lastProc() |
| + incumbent.mu.Lock() |
| + incumbent.stuck = true |
| + incumbent.mu.Unlock() |
| + res := f.a.Swap(context.Background(), f.attempt(ModeOverlap), f.newest) |
| + if res.Outcome != SwapDone || !strings.Contains(res.Reason, "not yet proven stopped") { |
| + t.Fatalf("the swap to B = %+v; want done with its retiring generation unproven", res) |
| + } |
| + f.a.mu.Lock() |
| + e := f.a.entries[f.host] |
| + outstanding, raised := len(e.ops.members), e.ops.raised |
| + f.a.mu.Unlock() |
| + if outstanding != 1 || !raised { |
| + t.Fatalf("after B: members %d, raised %v", outstanding, raised) |
| + } |
| + f.rev.Add(1) |
| + res = f.a.Swap(context.Background(), f.attemptV3(ModeOverlap), f.newest) |
| + if res.Outcome != SwapAbandoned || !strings.Contains(res.Reason, errSwapBusy.Error()) || res.Executed { |
| + t.Fatalf("C beside the unfinished operation = %+v; want abandoned busy, not executed", res) |
| + } |
| + f.noUnit(t, "beside the unfinished operation") |
| + if f.record() != nil { |
| + t.Fatal("a release record beside the unfinished operation") |
| + } |
| + incumbent.exit() |
| + waitFor(t, "the retiring generation's proof and the parent's restore", func() bool { |
| + f.a.mu.Lock() |
| + f.a.finishPendingStops() |
| + e := f.a.entries[f.host] |
| + done := len(e.ops.members) == 0 && !e.ops.raised |
| + f.a.mu.Unlock() |
| + return done |
| + }) |
| + ch := f.swapAsync(f.attemptV3(ModeOverlap)) |
| + f.nextUnit(t).done("1", "0", "success") |
| + if res := waitSwap(t, ch); res.Outcome != SwapDone || res.Gen.Build != "v3" { |
| + t.Fatalf("C once the operation finished = %+v", res) |
| + } |
| + if got := f.rb.startList(); !slices.Equal(got, []string{"v3"}) { |
| + t.Errorf("releases started %v", got) |
| + } |
| +} |
| + |
| +// D3: the pass lives only on the Swap call's stack. A Service replaced |
| +// between the pass and the swap runs the release again before admitting |
| +// the build. |
| +func TestReleaseRerunsAfterAnAgentRestart(t *testing.T) { |
| + f := newRelSwapFixture(t, true) |
| + type answer struct { |
| + pass *releasePass |
| + ok bool |
| + } |
| + ch := make(chan answer, 1) |
| + go func() { |
| + p, _, _, ok := f.a.releaseBeforeSwap(context.Background(), f.attempt(ModeOverlap), f.newest) |
| + ch <- answer{p, ok} |
| + }() |
| + f.nextUnit(t).done("1", "0", "success") |
| + got := <-ch |
| + if got.pass == nil || !got.ok { |
| + t.Fatalf("the first Service's release = %+v; want a pass", got) |
| + } |
| + // The seam between the pass and swapOverlap: the agent restarts. A new |
| + // Service over the same registry and backend wakes the incumbent. |
| + b := New(Config{Bucket: "test-bucket", Idle: time.Hour}, f.rb, f.reg) |
| + b.gens, b.now = true, f.clk.now |
| + b.genHealth = func(context.Context, Gen) error { return nil } |
| + b.Logs = f.sink |
| + b.GenerationsFile = f.a.GenerationsFile |
| + arrangeAwake(t, b, f.binRoute) |
| + setCurrentGen(t, b, f.host, f.incumbent) |
| + makeRootfsIncumbent(t, b, f.host) |
| + if b.releaseOps[f.host] != nil { |
| + t.Fatal("the new Service starts with a release record") |
| + } |
| + sw := make(chan SwapResult, 1) |
| + go func() { sw <- b.Swap(context.Background(), f.attempt(ModeOverlap), f.newest) }() |
| + f.nextUnit(t).done("1", "0", "success") |
| + res := waitSwap(t, sw) |
| + if res.Outcome != SwapDone || res.Gen.Build != "v2" { |
| + t.Fatalf("the new Service's swap = %+v", res) |
| + } |
| + if got := f.rb.startList(); !slices.Equal(got, []string{"v2", "v2"}) { |
| + t.Errorf("releases started %v; want B's release again on the new Service", got) |
| + } |
| + ev := f.b.events() |
| + second := slices.Index(ev, "release:start:v2") |
| + second += 1 + slices.Index(ev[second+1:], "release:start:v2") |
| + if second <= 0 || indexPrefix(ev[second:], "startGen:"+f.staged) < 0 { |
| + t.Errorf("events %v: the new Service's StartStaged does not follow its own release", ev) |
| + } |
| +} |
| + |
| +// INV-6: a binary overlap and hold run as on main: no process read, no |
| +// release, and the same order of backend calls as a fixture without the |
| +// release seam. |
| +func TestBinarySwapUnchangedByTheReleaseCheck(t *testing.T) { |
| + norm := regexp.MustCompile(`(switch|gen):\d+`) |
| + normalize := func(ev []string, staged string) []string { |
| + out := make([]string, len(ev)) |
| + for i, e := range ev { |
| + out[i] = norm.ReplaceAllString(strings.ReplaceAll(e, staged, "<staged>"), "$1:N") |
| + } |
| + return out |
| + } |
| + for _, mode := range []SwapMode{ModeOverlap, ModeHold} { |
| + t.Run(string(mode), func(t *testing.T) { |
| + plain := newSwapFixture(t) |
| + want := plain.a.Swap(context.Background(), plain.attempt(mode), plain.newest) |
| + f := newRelSwapFixture(t, false) |
| + got := f.a.Swap(context.Background(), f.attempt(mode), f.newest) |
| + if got.Outcome != SwapDone || got.Outcome != want.Outcome || got.Executed != want.Executed || got.Mode != want.Mode || got.Reason != want.Reason { |
| + t.Fatalf("Swap = %+v, without the release seam %+v", got, want) |
| + } |
| + if n := f.rb.readCount(); n != 0 { |
| + t.Errorf("a binary %s read the declared processes %d times", mode, n) |
| + } |
| + f.noUnit(t, "a binary "+string(mode)) |
| + if a, b := normalize(f.b.events(), f.staged), normalize(plain.b.events(), plain.staged); !slices.Equal(a, b) { |
| + t.Errorf("backend calls %v, without the release seam %v", a, b) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// The release-word phrases reach the swap's reason through the approved |
| +// rows (RFS-3-C01, C02 are rendered by rollout; the reason text here is |
| +// what reasonWord reads). |
| +func TestReleaseSwapResultIsTheOverlapColumn(t *testing.T) { |
| + failed := &ReleaseError{Build: "v2", Exit: 3} |
| + timedOut := &ReleaseError{Build: "v2", TimedOut: true, Took: time.Minute} |
| + for _, tc := range []struct { |
| + name string |
| + err error |
| + executed bool |
| + open bool |
| + outcome SwapOutcome |
| + exec bool |
| + prefix string |
| + }{ |
| + {"failed", failed, true, false, SwapFailed, true, "release: "}, |
| + {"timed out", timedOut, true, false, SwapFailed, true, "release timed out: "}, |
| + {"unproven", errors.Join(errReleaseUnproven, errors.New("cgroup")), true, true, SwapAbandoned, true, ""}, |
| + {"cancelled, ran", errReleaseVanished, true, false, SwapAbandoned, true, ""}, |
| + {"cancelled, the lease", fmt.Errorf("%w: %w", errSwapLeaseLost, errReleaseLeaseLost), true, false, SwapAbandoned, true, ""}, |
| + {"refused", errReleaseOpen, false, true, SwapAbandoned, false, ""}, |
| + {"refused by root", refuseRootfs(errors.New("root said no")), false, false, SwapAbandoned, false, ""}, |
| + {"refused, not published", errStagedBuildNotPublished, false, false, SwapAbandoned, false, ""}, |
| + } { |
| + res := releaseSwapResult(tc.err, tc.executed, tc.open) |
| + if res.Outcome != tc.outcome || res.Executed != tc.exec || !strings.HasPrefix(res.Reason, tc.prefix) { |
| + t.Errorf("%s: %+v; want %s, executed %v, reason prefix %q", tc.name, res, tc.outcome, tc.exec, tc.prefix) |
| + } |
| + if res.Outcome == SwapDown { |
| + t.Errorf("%s: a release before an overlap answered SwapDown", tc.name) |
| + } |
| + } |
| + if got := rolloutwords.EventReleaseFailed; !strings.Contains("release: "+failed.Error(), strings.SplitN(got, "%", 2)[0]) { |
| + t.Errorf("the failure's reason does not carry C05 (%q)", got) |
| + } |
| +} |