| 1 | package hostclaim |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "reflect" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | // plan10J2Recovery is a source's recovery op: restoring (with its rollback id) |
| 10 | // or reverting, the source executing at phase. |
| 11 | func plan10J2Recovery(kind, phase string) *Claim { |
| 12 | c := &Claim{V: schemaVersion, Host: "a.go.example.com", Gen: 12, |
| 13 | Holder: &Holder{Family: "instance", Incarnation: "inc", Location: "instances/a.go.example.com", Generation: "g1", Stamp: 10, Session: 11, WriterBox: "ie-box"}, |
| 14 | Op: Op{Kind: kind, ID: "m1", Phase: phase, Exec: "ie-box", Participants: []string{"ie-box", "uk-box"}, Intent: map[string]string{"to": "uk", "source": "ie-box", "target": "uk-box"}}} |
| 15 | if kind == KindRestoring { |
| 16 | c.Op.Intent["rollback"] = "rb_abcdefghijklmnopqrstuvwxyz" |
| 17 | } |
| 18 | if kind == KindReverting { |
| 19 | c.Op.Journal = map[string]string{JournalRevPhase: phase} |
| 20 | } |
| 21 | return c |
| 22 | } |
| 23 | |
| 24 | func TestPlan10J2RecoverySessionRowsAreForwardOnly(t *testing.T) { |
| 25 | for _, kind := range []string{KindRestoring, KindReverting} { |
| 26 | for _, tc := range []struct { |
| 27 | from, to string |
| 28 | want bool |
| 29 | }{ |
| 30 | {"probing", "probing", true}, |
| 31 | {"probing", "probed", true}, |
| 32 | {"probed", "opened", true}, |
| 33 | {"opened", "opened", true}, |
| 34 | {"probing", "opened", false}, |
| 35 | {"probed", "probing", false}, |
| 36 | {"opened", "probed", false}, |
| 37 | {"opened", "probing", false}, |
| 38 | {"claimed", "probing", false}, |
| 39 | {"released", "claimed", false}, |
| 40 | {"verify", "probing", false}, |
| 41 | {"evidence", "verify", false}, |
| 42 | {"probing", "committed", false}, |
| 43 | {"probed", "committed", false}, |
| 44 | } { |
| 45 | if got := SessionContinues(kind, tc.from, tc.to); got != tc.want { |
| 46 | t.Errorf("SessionContinues(%s, %q, %q) = %v, want %v", kind, tc.from, tc.to, got, tc.want) |
| 47 | } |
| 48 | } |
| 49 | for _, phase := range []string{"probing", "probed", "opened"} { |
| 50 | if !SessionPhase(kind, phase) { |
| 51 | t.Errorf("%s %s is not a session phase", kind, phase) |
| 52 | } |
| 53 | } |
| 54 | for _, phase := range []string{"", "claimed", "released", "releasing", "evidence", "verify", "committed", "recovered"} { |
| 55 | if SessionPhase(kind, phase) { |
| 56 | t.Errorf("%s %q is a session phase", kind, phase) |
| 57 | } |
| 58 | } |
| 59 | // Observation and teardown reach forward along the row only. |
| 60 | for _, tc := range []struct { |
| 61 | from, to string |
| 62 | want bool |
| 63 | }{ |
| 64 | {"probing", "opened", true}, |
| 65 | {"probed", "opened", true}, |
| 66 | {"opened", "probing", false}, |
| 67 | {"probed", "probing", false}, |
| 68 | {"claimed", "probing", false}, |
| 69 | } { |
| 70 | if got := SessionReaches(kind, tc.from, tc.to); got != tc.want { |
| 71 | t.Errorf("SessionReaches(%s, %q, %q) = %v, want %v", kind, tc.from, tc.to, got, tc.want) |
| 72 | } |
| 73 | if got := SessionObserves(kind, tc.from, tc.to); got != tc.want { |
| 74 | t.Errorf("SessionObserves(%s, %q, %q) = %v, want %v", kind, tc.from, tc.to, got, tc.want) |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | // A recovery row never lends its phases to another kind, and the landing |
| 79 | // row stays as it was. |
| 80 | for _, kind := range []string{KindMoving, KindServing, KindPurging} { |
| 81 | if SessionContinues(kind, "probing", "probed") || SessionPhase(kind, "probing") { |
| 82 | t.Errorf("%s gained a session row", kind) |
| 83 | } |
| 84 | } |
| 85 | if SessionContinues(KindLanding, "probed", "opened") || SessionPhase(KindLanding, "opened") || !SessionContinues(KindLanding, "probed", "committed") { |
| 86 | t.Error("the landing row changed") |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func TestPlan10J2KeepWriterSessionGuardsARecoveryStep(t *testing.T) { |
| 91 | for _, tc := range []struct { |
| 92 | kind string |
| 93 | steps []func(*Claim) error |
| 94 | }{ |
| 95 | {KindRestoring, []func(*Claim) error{SetRestoringPhase("m1", PhaseProbing, PhaseProbed), SetRestoringPhase("m1", PhaseProbed, PhaseOpened)}}, |
| 96 | {KindReverting, []func(*Claim) error{SetRevPhase("m1", PhaseProbing, PhaseProbed, false), SetRevPhase("m1", PhaseProbed, PhaseOpened, false)}}, |
| 97 | } { |
| 98 | t.Run(tc.kind, func(t *testing.T) { |
| 99 | c := plan10J2Recovery(tc.kind, "probing") |
| 100 | for i, step := range tc.steps { |
| 101 | if err := KeepWriterSession("m1", step)(c); err != nil { |
| 102 | t.Fatalf("step %d: %v", i, err) |
| 103 | } |
| 104 | } |
| 105 | if c.Op.Phase != "opened" || c.Holder.Session != 11 || c.Holder.Stamp != 10 || c.Holder.Generation != "g1" || c.Holder.WriterBox != "ie-box" { |
| 106 | t.Fatalf("the steps changed the session: %+v %+v", c.Op, *c.Holder) |
| 107 | } |
| 108 | }) |
| 109 | } |
| 110 | to := func(phase string) func(*Claim) error { |
| 111 | return func(c *Claim) error { c.Op.Phase = phase; return nil } |
| 112 | } |
| 113 | for _, kind := range []string{KindRestoring, KindReverting} { |
| 114 | other := KindReverting |
| 115 | if kind == KindReverting { |
| 116 | other = KindRestoring |
| 117 | } |
| 118 | for name, tc := range map[string]struct { |
| 119 | from string |
| 120 | fn func(*Claim) error |
| 121 | }{ |
| 122 | "skipped step": {"probing", to("opened")}, |
| 123 | "backward step": {"probed", to("probing")}, |
| 124 | "generation": {"probing", func(c *Claim) error { c.Holder.Generation = "fresh"; c.Op.Phase = "probed"; return nil }}, |
| 125 | "session": {"probing", func(c *Claim) error { c.Holder.Session++; c.Op.Phase = "probed"; return nil }}, |
| 126 | "stamp": {"probing", func(c *Claim) error { c.Holder.Stamp = c.Gen + 1; c.Op.Phase = "probed"; return nil }}, |
| 127 | "incarnation": {"probing", func(c *Claim) error { c.Holder.Incarnation = "other"; c.Op.Phase = "probed"; return nil }}, |
| 128 | "writer box": {"probing", func(c *Claim) error { c.Holder.WriterBox = "uk-box"; c.Op.Phase = "probed"; return nil }}, |
| 129 | "executor": {"probing", func(c *Claim) error { c.Op.Exec = "uk-box"; c.Op.Phase = "probed"; return nil }}, |
| 130 | "other recovery": {"probing", func(c *Claim) error { c.Op.Kind = other; c.Op.Phase = "probed"; return nil }}, |
| 131 | "landing kind": {"probing", func(c *Claim) error { c.Op.Kind = KindLanding; c.Op.Phase = "probed"; return nil }}, |
| 132 | "op id": {"probing", func(c *Claim) error { c.Op.ID = "m2"; c.Op.Phase = "probed"; return nil }}, |
| 133 | "holder removed": {"probing", func(c *Claim) error { c.Holder = nil; return nil }}, |
| 134 | } { |
| 135 | t.Run(kind+"/"+name, func(t *testing.T) { |
| 136 | c := plan10J2Recovery(kind, tc.from) |
| 137 | if err := KeepWriterSession("m1", tc.fn)(c); !errors.Is(err, ErrWrongState) { |
| 138 | t.Fatalf("kept the session: %v", err) |
| 139 | } |
| 140 | }) |
| 141 | } |
| 142 | } |
| 143 | // claimed to probing starts the row. It is not a session-keeping step, |
| 144 | // because no session was admitted before probing. |
| 145 | c := plan10J2Recovery(KindRestoring, "claimed") |
| 146 | before := *c.Holder |
| 147 | if err := KeepWriterSession("m1", SetRestoringPhase("m1", PhaseClaimed, PhaseProbing))(c); !errors.Is(err, ErrWrongState) { |
| 148 | t.Fatalf("claimed to probing kept a session: %v", err) |
| 149 | } |
| 150 | if !reflect.DeepEqual(*c.Holder, before) { |
| 151 | t.Fatalf("holder changed: %+v", *c.Holder) |
| 152 | } |
| 153 | } |
| 154 | |