| 1 | package hostclaim_test |
| 2 | |
| 3 | import ( |
| 4 | "crypto/rand" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/carlosframework/platform/internal/hostbox" |
| 9 | "github.com/carlosframework/platform/internal/hostclaim" |
| 10 | "github.com/carlosframework/platform/internal/moves" |
| 11 | ) |
| 12 | |
| 13 | // J1: the claim phases a recovery writes are the ones the wake policy keys |
| 14 | // on. A recovery probe is admitted only at probing, and a serving wake only |
| 15 | // at probed or opened, for both recovery kinds; no other phase of either |
| 16 | // order starts code. |
| 17 | func TestRecoveryPhasesMeetWakePhase(t *testing.T) { |
| 18 | for _, kind := range []string{hostclaim.KindRestoring, hostclaim.KindReverting} { |
| 19 | order := hostclaim.RecoveryPhaseOrder(kind) |
| 20 | n := len(order) |
| 21 | if n < 3 || order[n-3] != hostclaim.PhaseProbing || order[n-2] != hostclaim.PhaseProbed || order[n-1] != hostclaim.PhaseOpened { |
| 22 | t.Fatalf("%s order %v does not end probing, probed, opened", kind, order) |
| 23 | } |
| 24 | phases := order |
| 25 | if kind == hostclaim.KindReverting { |
| 26 | // A revert inherits the moving phase it began from. |
| 27 | phases = append([]string{hostclaim.PhaseMoving, hostclaim.PhaseSealed}, order...) |
| 28 | } |
| 29 | for _, phase := range phases { |
| 30 | c := &hostclaim.Claim{Holder: &hostclaim.Holder{}, Op: hostclaim.Op{Kind: kind, ID: "mv_abcdefghijklmnopqrstuvwxyz", Phase: phase, Exec: "ie-box"}} |
| 31 | probe := hostbox.WakePhase(c, "ie-box", true) == nil |
| 32 | wake := hostbox.WakePhase(c, "ie-box", false) == nil |
| 33 | if probe != (phase == hostclaim.PhaseProbing) { |
| 34 | t.Errorf("%s/%q: probe admitted = %v", kind, phase, probe) |
| 35 | } |
| 36 | if wake != (phase == hostclaim.PhaseProbed || phase == hostclaim.PhaseOpened) { |
| 37 | t.Errorf("%s/%q: wake admitted = %v", kind, phase, wake) |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | if hostclaim.RecoveryPhaseOrder(hostclaim.KindLanding) != nil { |
| 42 | t.Fatal("landing is not a recovery kind") |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // The rollback id shape is a twin of moves.ValidOpID's rb_ form: a minted |
| 47 | // rollback id is accepted, a minted move id is not. |
| 48 | func TestRollBackTakesMintedRollbackIDs(t *testing.T) { |
| 49 | landing := func() *hostclaim.Claim { |
| 50 | return &hostclaim.Claim{Holder: &hostclaim.Holder{Stamp: 8}, Op: hostclaim.Op{Kind: hostclaim.KindLanding, ID: "mv_abcdefghijklmnopqrstuvwxyz", Phase: "probed", Exec: "uk-box", |
| 51 | Intent: map[string]string{"to": "uk", "source": "ie-box", "target": "uk-box"}, Participants: []string{"ie-box", "uk-box"}}} |
| 52 | } |
| 53 | for i := 0; i < 32; i++ { |
| 54 | rb, err := moves.NewRollbackID(rand.Reader) |
| 55 | if err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | if err := hostclaim.RollBack("mv_abcdefghijklmnopqrstuvwxyz", rb)(landing()); err != nil { |
| 59 | t.Fatalf("%s: %v", rb, err) |
| 60 | } |
| 61 | mv, err := moves.NewMoveID(rand.Reader) |
| 62 | if err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | if err := hostclaim.RollBack("mv_abcdefghijklmnopqrstuvwxyz", mv)(landing()); !errors.Is(err, hostclaim.ErrMalformed) { |
| 66 | t.Fatalf("%s: %v", mv, err) |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |