| 1 | package hostguard |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/carlosframework/platform/internal/hostclaim" |
| 7 | ) |
| 8 | |
| 9 | // plan9D4Writer is a landing whose current writer, session 12, box-1's |
| 10 | // restarted probe, was admitted cold in uk into generation g12. Session 9 was |
| 11 | // box-1's first probe over the move's source on box-0. |
| 12 | func plan9D4Writer(phase string) (*hostclaim.Claim, FenceTarget) { |
| 13 | c := &hostclaim.Claim{V: 2, Host: "shop.bab.oncarlos.com", Gen: 12, |
| 14 | Holder: &hostclaim.Holder{Incarnation: "inc", Generation: "g12", Stamp: 12, Session: 12, WriterBox: "box-1", |
| 15 | Admissions: []hostclaim.AdmissionRecord{ |
| 16 | {Session: 9, Box: "box-1", Generation: "m1", Predecessor: "src", Region: "uk", PredecessorRegion: "ie", Mode: "cold", Coverage: "sealed"}, |
| 17 | {Session: 12, Box: "box-1", Generation: "g12", Predecessor: "m1", Region: "uk", PredecessorRegion: "uk", Mode: "cold"}, |
| 18 | }}, |
| 19 | Op: hostclaim.Op{Kind: hostclaim.KindLanding, ID: "m1", Phase: phase, Exec: "box-1", Participants: []string{"box-0", "box-1"}}} |
| 20 | t := FenceTarget{Region: "uk", Host: c.Host, Incarnation: "inc", Stamp: 12, Session: 12, Op: "m1", Phase: phase, ObligationID: WriterStopObligation(12), Box: "box-1"} |
| 21 | return c, t |
| 22 | } |
| 23 | |
| 24 | func TestPlan9D4WriterStopIsOnlyTheLandingsOwnCurrentWriter(t *testing.T) { |
| 25 | for _, phase := range []string{hostclaim.PhaseLandingProbing, hostclaim.PhaseLandingProbed, hostclaim.PhaseLandingCommitted} { |
| 26 | c, target := plan9D4Writer(phase) |
| 27 | if !StopAuthorityMatches(c, target) { |
| 28 | t.Fatalf("%s: the landing's own writer stop was refused", phase) |
| 29 | } |
| 30 | if id, ok := LandingWriterStop(c, "box-1", "uk"); !ok || id != "writer-session-12" { |
| 31 | t.Fatalf("%s: writer stop %q %v", phase, id, ok) |
| 32 | } |
| 33 | } |
| 34 | for name, mutate := range map[string]func(*hostclaim.Claim, *FenceTarget){ |
| 35 | "another session's label": func(_ *hostclaim.Claim, t *FenceTarget) { t.ObligationID = WriterStopObligation(9) }, |
| 36 | "another region": func(_ *hostclaim.Claim, t *FenceTarget) { t.Region = "ie" }, |
| 37 | "no region": func(_ *hostclaim.Claim, t *FenceTarget) { t.Region = "" }, |
| 38 | "the source's writer": func(c *hostclaim.Claim, _ *FenceTarget) { |
| 39 | c.Holder.WriterBox = "box-0" |
| 40 | }, |
| 41 | "another executor": func(c *hostclaim.Claim, t *FenceTarget) { c.Op.Exec, t.Box = "box-2", "box-2" }, |
| 42 | "before the probe": func(c *hostclaim.Claim, t *FenceTarget) { c.Op.Phase, t.Phase = "restored", "restored" }, |
| 43 | "a restoring probe": func(c *hostclaim.Claim, _ *FenceTarget) { c.Op.Kind = hostclaim.KindRestoring }, |
| 44 | "a moving source": func(c *hostclaim.Claim, _ *FenceTarget) { c.Op.Kind = hostclaim.KindMoving }, |
| 45 | "admitted elsewhere": func(c *hostclaim.Claim, _ *FenceTarget) { c.Holder.Admissions[1].Box = "box-0" }, |
| 46 | "admitted twice": func(c *hostclaim.Claim, _ *FenceTarget) { c.Holder.Admissions[0].Session = 12 }, |
| 47 | "never admitted": func(c *hostclaim.Claim, _ *FenceTarget) { c.Holder.Admissions = c.Holder.Admissions[:1] }, |
| 48 | "another generation": func(c *hostclaim.Claim, _ *FenceTarget) { c.Holder.Admissions[1].Generation = "g11" }, |
| 49 | "a warm admission": func(c *hostclaim.Claim, _ *FenceTarget) { c.Holder.Admissions[1].Mode = "warm" }, |
| 50 | "a chore with the name": func(c *hostclaim.Claim, _ *FenceTarget) { |
| 51 | c.Obligations = []hostclaim.Obligation{{ID: "writer-session-12", Op: "m1", Kind: "park-local", Box: "box-1", Owner: "box:box-1", Retirement: "r"}} |
| 52 | }, |
| 53 | } { |
| 54 | c, target := plan9D4Writer(hostclaim.PhaseLandingProbing) |
| 55 | mutate(c, &target) |
| 56 | if StopAuthorityMatches(c, target) { |
| 57 | t.Fatalf("%s: a writer-session stop was accepted", name) |
| 58 | } |
| 59 | } |
| 60 | // Main's chore-named operation stops are unchanged. |
| 61 | c, target := plan9D4Writer(hostclaim.PhaseLandingProbing) |
| 62 | target.ObligationID = "local" |
| 63 | if !StopAuthorityMatches(c, target) { |
| 64 | t.Fatal("a chore-named landing stop by the executor was refused") |
| 65 | } |
| 66 | // Serving never carries an operation tuple, writer-session or otherwise. |
| 67 | c.Op = hostclaim.Op{Kind: hostclaim.KindServing, Exec: "box-1"} |
| 68 | target.Op, target.Phase = "", "" |
| 69 | target.ObligationID = WriterStopObligation(12) |
| 70 | if StopAuthorityMatches(c, target) { |
| 71 | t.Fatal("a serving stop carried a writer-session obligation") |
| 72 | } |
| 73 | } |
| 74 | |