package hostclaim import ( "errors" "reflect" "testing" ) // plan10J2Recovery is a source's recovery op: restoring (with its rollback id) // or reverting, the source executing at phase. func plan10J2Recovery(kind, phase string) *Claim { c := &Claim{V: schemaVersion, Host: "a.go.example.com", Gen: 12, Holder: &Holder{Family: "instance", Incarnation: "inc", Location: "instances/a.go.example.com", Generation: "g1", Stamp: 10, Session: 11, WriterBox: "ie-box"}, 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"}}} if kind == KindRestoring { c.Op.Intent["rollback"] = "rb_abcdefghijklmnopqrstuvwxyz" } if kind == KindReverting { c.Op.Journal = map[string]string{JournalRevPhase: phase} } return c } func TestPlan10J2RecoverySessionRowsAreForwardOnly(t *testing.T) { for _, kind := range []string{KindRestoring, KindReverting} { for _, tc := range []struct { from, to string want bool }{ {"probing", "probing", true}, {"probing", "probed", true}, {"probed", "opened", true}, {"opened", "opened", true}, {"probing", "opened", false}, {"probed", "probing", false}, {"opened", "probed", false}, {"opened", "probing", false}, {"claimed", "probing", false}, {"released", "claimed", false}, {"verify", "probing", false}, {"evidence", "verify", false}, {"probing", "committed", false}, {"probed", "committed", false}, } { if got := SessionContinues(kind, tc.from, tc.to); got != tc.want { t.Errorf("SessionContinues(%s, %q, %q) = %v, want %v", kind, tc.from, tc.to, got, tc.want) } } for _, phase := range []string{"probing", "probed", "opened"} { if !SessionPhase(kind, phase) { t.Errorf("%s %s is not a session phase", kind, phase) } } for _, phase := range []string{"", "claimed", "released", "releasing", "evidence", "verify", "committed", "recovered"} { if SessionPhase(kind, phase) { t.Errorf("%s %q is a session phase", kind, phase) } } // Observation and teardown reach forward along the row only. for _, tc := range []struct { from, to string want bool }{ {"probing", "opened", true}, {"probed", "opened", true}, {"opened", "probing", false}, {"probed", "probing", false}, {"claimed", "probing", false}, } { if got := SessionReaches(kind, tc.from, tc.to); got != tc.want { t.Errorf("SessionReaches(%s, %q, %q) = %v, want %v", kind, tc.from, tc.to, got, tc.want) } if got := SessionObserves(kind, tc.from, tc.to); got != tc.want { t.Errorf("SessionObserves(%s, %q, %q) = %v, want %v", kind, tc.from, tc.to, got, tc.want) } } } // A recovery row never lends its phases to another kind, and the landing // row stays as it was. for _, kind := range []string{KindMoving, KindServing, KindPurging} { if SessionContinues(kind, "probing", "probed") || SessionPhase(kind, "probing") { t.Errorf("%s gained a session row", kind) } } if SessionContinues(KindLanding, "probed", "opened") || SessionPhase(KindLanding, "opened") || !SessionContinues(KindLanding, "probed", "committed") { t.Error("the landing row changed") } } func TestPlan10J2KeepWriterSessionGuardsARecoveryStep(t *testing.T) { for _, tc := range []struct { kind string steps []func(*Claim) error }{ {KindRestoring, []func(*Claim) error{SetRestoringPhase("m1", PhaseProbing, PhaseProbed), SetRestoringPhase("m1", PhaseProbed, PhaseOpened)}}, {KindReverting, []func(*Claim) error{SetRevPhase("m1", PhaseProbing, PhaseProbed, false), SetRevPhase("m1", PhaseProbed, PhaseOpened, false)}}, } { t.Run(tc.kind, func(t *testing.T) { c := plan10J2Recovery(tc.kind, "probing") for i, step := range tc.steps { if err := KeepWriterSession("m1", step)(c); err != nil { t.Fatalf("step %d: %v", i, err) } } if c.Op.Phase != "opened" || c.Holder.Session != 11 || c.Holder.Stamp != 10 || c.Holder.Generation != "g1" || c.Holder.WriterBox != "ie-box" { t.Fatalf("the steps changed the session: %+v %+v", c.Op, *c.Holder) } }) } to := func(phase string) func(*Claim) error { return func(c *Claim) error { c.Op.Phase = phase; return nil } } for _, kind := range []string{KindRestoring, KindReverting} { other := KindReverting if kind == KindReverting { other = KindRestoring } for name, tc := range map[string]struct { from string fn func(*Claim) error }{ "skipped step": {"probing", to("opened")}, "backward step": {"probed", to("probing")}, "generation": {"probing", func(c *Claim) error { c.Holder.Generation = "fresh"; c.Op.Phase = "probed"; return nil }}, "session": {"probing", func(c *Claim) error { c.Holder.Session++; c.Op.Phase = "probed"; return nil }}, "stamp": {"probing", func(c *Claim) error { c.Holder.Stamp = c.Gen + 1; c.Op.Phase = "probed"; return nil }}, "incarnation": {"probing", func(c *Claim) error { c.Holder.Incarnation = "other"; c.Op.Phase = "probed"; return nil }}, "writer box": {"probing", func(c *Claim) error { c.Holder.WriterBox = "uk-box"; c.Op.Phase = "probed"; return nil }}, "executor": {"probing", func(c *Claim) error { c.Op.Exec = "uk-box"; c.Op.Phase = "probed"; return nil }}, "other recovery": {"probing", func(c *Claim) error { c.Op.Kind = other; c.Op.Phase = "probed"; return nil }}, "landing kind": {"probing", func(c *Claim) error { c.Op.Kind = KindLanding; c.Op.Phase = "probed"; return nil }}, "op id": {"probing", func(c *Claim) error { c.Op.ID = "m2"; c.Op.Phase = "probed"; return nil }}, "holder removed": {"probing", func(c *Claim) error { c.Holder = nil; return nil }}, } { t.Run(kind+"/"+name, func(t *testing.T) { c := plan10J2Recovery(kind, tc.from) if err := KeepWriterSession("m1", tc.fn)(c); !errors.Is(err, ErrWrongState) { t.Fatalf("kept the session: %v", err) } }) } } // claimed to probing starts the row. It is not a session-keeping step, // because no session was admitted before probing. c := plan10J2Recovery(KindRestoring, "claimed") before := *c.Holder if err := KeepWriterSession("m1", SetRestoringPhase("m1", PhaseClaimed, PhaseProbing))(c); !errors.Is(err, ErrWrongState) { t.Fatalf("claimed to probing kept a session: %v", err) } if !reflect.DeepEqual(*c.Holder, before) { t.Fatalf("holder changed: %+v", *c.Holder) } }