package host import ( "context" "errors" "os" "testing" "github.com/carlosframework/platform/internal/registry" "github.com/carlosframework/platform/internal/release" ) type e8ExeUnits struct{ *plan14RestartUnits } func (u e8ExeUnits) Exe(pid int) (string, error) { if len(u.effects) > 0 { return "", os.ErrPermission } return u.plan14RestartUnits.Exe(pid) } func TestPlan14ERecoveryRestartOutcome(t *testing.T) { for _, fault := range []string{"binding", "final binding", "exe", "failed", "auto-restart", "not ready"} { t.Run(fault, func(t *testing.T) { e, u, c, source, pub := signedRecoveryFixture(t) if err := e.root.reg.SetVersions(observedHost, "v1", ""); err != nil { t.Fatal(err) } u.afterRestart = func() { u.props.NRestarts = 0 } u.afterEffect = func() { if len(u.effects) != 1 { return } if fault == "failed" { u.props.ActiveState, u.props.MainPID = "failed", 0 } if fault == "auto-restart" { u.props.ActiveState, u.props.SubState, u.props.NRestarts = "activating", "auto-restart", 1 } } c.legacy = func() (bool, error) { return true, nil } c.legacyReady = func(registry.Route) func() error { return func() error { if fault == "not ready" && len(u.effects) == 1 { return os.ErrDeadlineExceeded } return nil } } orig := c.host calls := 0 c.host = func(ctx context.Context, host string) (proofBinding, error) { if len(u.effects) == 1 { calls++ if fault != "final binding" || calls >= 2 { if fault == "failed" { u.props.ActiveState, u.props.MainPID = "failed", 0 } if fault == "auto-restart" { u.props.ActiveState, u.props.SubState, u.props.NRestarts = "activating", "auto-restart", 1 } return proofBinding{}, os.ErrPermission } } return orig(ctx, host) } if fault == "exe" { source.sys = e8ExeUnits{u} } n := namedUnitRecovery{source: source, pub: pub, readLive: os.Readlink} fallbacks := 0 err := release.Pass(t.Context(), e.root.reg, e.store, pub, u.opt, func(host string) error { rt, e := e.root.reg.Get(host) if e != nil { return e } fallbacks++ return legacyCheckedUnitRestart(source.sys, u.opt, func(string) string { return "5s" }, *rt, nil, c) }, release.BakeDir(e.bake), release.WithRestartFailurePropagation(), release.WithRecovery(func(ctx context.Context, rt registry.Route) (bool, error) { return n.recover(ctx, rt.Host, c, func(string) string { return "5s" }, e.root.reg) }, c.allowLegacy), release.WithRollbackRestart(func(host string) error { rt, err := e.root.reg.Get(host) if err != nil { return err } return legacyRollbackRestart(source.sys, u.opt, func(string) string { return "5s" }, *rt, c) })) failed := fault == "failed" || fault == "auto-restart" || fault == "not ready" want, count := "v2", 1 if failed { want, count = "v1", 3 } live, liveErr := release.LiveVersion(u.opt, observedHost) if liveErr != nil || live != want || len(u.effects) != count || fallbacks != 1 || (err != nil) != failed { t.Fatalf("outcome: live=%s/%v effects=%v fallback=%d err=%v", live, liveErr, u.effects, fallbacks, err) } stamp, stampErr := e.root.reg.Get(observedHost) if stampErr != nil || stamp.Version != want { t.Fatalf("accepted outcome not stamped: %+v %v", stamp, stampErr) } disk, _ := c.attempts() if names, err := disk.Names(); err != nil || len(names) != 0 { t.Fatalf("no-census recovery created authority: %v %v", names, err) } if calls != 0 { t.Fatalf("recovery selected binding: %d", calls) } }) } } func TestPlan14ERecoveryNotUpPrecedesBinding(t *testing.T) { err := errors.Join(errRestartBinding, &release.UnitNotUpError{Unit: "console.service", Reason: "failed"}) if !restartEffectFailed(err) { t.Fatal("binding masked real not-up result") } } func TestPlan14ERecoveryStrictNotUpPrecedesBinding(t *testing.T) { e, u, c, source, pub := signedRecoveryFixture(t) original := c.host c.host = func(ctx context.Context, host string) (proofBinding, error) { if len(u.effects) == 1 { u.props.ActiveState, u.props.MainPID = "failed", 0 return proofBinding{}, os.ErrPermission } return original(ctx, host) } n := namedUnitRecovery{source: source, pub: pub, readLive: os.Readlink} handled, err := n.recover(t.Context(), observedHost, c, func(string) string { return "5s" }, e.root.reg) var notUp *release.UnitNotUpError if !handled || !errors.As(err, ¬Up) || len(u.effects) != 3 { t.Fatalf("strict outcome lost: handled=%v err=%v effects=%v", handled, err, u.effects) } }