package hostguard import ( "testing" "github.com/carlosframework/platform/internal/hostclaim" ) // plan9D4Writer is a landing whose current writer, session 12, box-1's // restarted probe, was admitted cold in uk into generation g12. Session 9 was // box-1's first probe over the move's source on box-0. func plan9D4Writer(phase string) (*hostclaim.Claim, FenceTarget) { c := &hostclaim.Claim{V: 2, Host: "shop.bab.oncarlos.com", Gen: 12, Holder: &hostclaim.Holder{Incarnation: "inc", Generation: "g12", Stamp: 12, Session: 12, WriterBox: "box-1", Admissions: []hostclaim.AdmissionRecord{ {Session: 9, Box: "box-1", Generation: "m1", Predecessor: "src", Region: "uk", PredecessorRegion: "ie", Mode: "cold", Coverage: "sealed"}, {Session: 12, Box: "box-1", Generation: "g12", Predecessor: "m1", Region: "uk", PredecessorRegion: "uk", Mode: "cold"}, }}, Op: hostclaim.Op{Kind: hostclaim.KindLanding, ID: "m1", Phase: phase, Exec: "box-1", Participants: []string{"box-0", "box-1"}}} t := FenceTarget{Region: "uk", Host: c.Host, Incarnation: "inc", Stamp: 12, Session: 12, Op: "m1", Phase: phase, ObligationID: WriterStopObligation(12), Box: "box-1"} return c, t } func TestPlan9D4WriterStopIsOnlyTheLandingsOwnCurrentWriter(t *testing.T) { for _, phase := range []string{hostclaim.PhaseLandingProbing, hostclaim.PhaseLandingProbed, hostclaim.PhaseLandingCommitted} { c, target := plan9D4Writer(phase) if !StopAuthorityMatches(c, target) { t.Fatalf("%s: the landing's own writer stop was refused", phase) } if id, ok := LandingWriterStop(c, "box-1", "uk"); !ok || id != "writer-session-12" { t.Fatalf("%s: writer stop %q %v", phase, id, ok) } } for name, mutate := range map[string]func(*hostclaim.Claim, *FenceTarget){ "another session's label": func(_ *hostclaim.Claim, t *FenceTarget) { t.ObligationID = WriterStopObligation(9) }, "another region": func(_ *hostclaim.Claim, t *FenceTarget) { t.Region = "ie" }, "no region": func(_ *hostclaim.Claim, t *FenceTarget) { t.Region = "" }, "the source's writer": func(c *hostclaim.Claim, _ *FenceTarget) { c.Holder.WriterBox = "box-0" }, "another executor": func(c *hostclaim.Claim, t *FenceTarget) { c.Op.Exec, t.Box = "box-2", "box-2" }, "before the probe": func(c *hostclaim.Claim, t *FenceTarget) { c.Op.Phase, t.Phase = "restored", "restored" }, "a restoring probe": func(c *hostclaim.Claim, _ *FenceTarget) { c.Op.Kind = hostclaim.KindRestoring }, "a moving source": func(c *hostclaim.Claim, _ *FenceTarget) { c.Op.Kind = hostclaim.KindMoving }, "admitted elsewhere": func(c *hostclaim.Claim, _ *FenceTarget) { c.Holder.Admissions[1].Box = "box-0" }, "admitted twice": func(c *hostclaim.Claim, _ *FenceTarget) { c.Holder.Admissions[0].Session = 12 }, "never admitted": func(c *hostclaim.Claim, _ *FenceTarget) { c.Holder.Admissions = c.Holder.Admissions[:1] }, "another generation": func(c *hostclaim.Claim, _ *FenceTarget) { c.Holder.Admissions[1].Generation = "g11" }, "a warm admission": func(c *hostclaim.Claim, _ *FenceTarget) { c.Holder.Admissions[1].Mode = "warm" }, "a chore with the name": func(c *hostclaim.Claim, _ *FenceTarget) { c.Obligations = []hostclaim.Obligation{{ID: "writer-session-12", Op: "m1", Kind: "park-local", Box: "box-1", Owner: "box:box-1", Retirement: "r"}} }, } { c, target := plan9D4Writer(hostclaim.PhaseLandingProbing) mutate(c, &target) if StopAuthorityMatches(c, target) { t.Fatalf("%s: a writer-session stop was accepted", name) } } // Main's chore-named operation stops are unchanged. c, target := plan9D4Writer(hostclaim.PhaseLandingProbing) target.ObligationID = "local" if !StopAuthorityMatches(c, target) { t.Fatal("a chore-named landing stop by the executor was refused") } // Serving never carries an operation tuple, writer-session or otherwise. c.Op = hostclaim.Op{Kind: hostclaim.KindServing, Exec: "box-1"} target.Op, target.Phase = "", "" target.ObligationID = WriterStopObligation(12) if StopAuthorityMatches(c, target) { t.Fatal("a serving stop carried a writer-session obligation") } }