package hostclaim_test import ( "crypto/rand" "errors" "testing" "github.com/carlosframework/platform/internal/hostbox" "github.com/carlosframework/platform/internal/hostclaim" "github.com/carlosframework/platform/internal/moves" ) // J1: the claim phases a recovery writes are the ones the wake policy keys // on. A recovery probe is admitted only at probing, and a serving wake only // at probed or opened, for both recovery kinds; no other phase of either // order starts code. func TestRecoveryPhasesMeetWakePhase(t *testing.T) { for _, kind := range []string{hostclaim.KindRestoring, hostclaim.KindReverting} { order := hostclaim.RecoveryPhaseOrder(kind) n := len(order) if n < 3 || order[n-3] != hostclaim.PhaseProbing || order[n-2] != hostclaim.PhaseProbed || order[n-1] != hostclaim.PhaseOpened { t.Fatalf("%s order %v does not end probing, probed, opened", kind, order) } phases := order if kind == hostclaim.KindReverting { // A revert inherits the moving phase it began from. phases = append([]string{hostclaim.PhaseMoving, hostclaim.PhaseSealed}, order...) } for _, phase := range phases { c := &hostclaim.Claim{Holder: &hostclaim.Holder{}, Op: hostclaim.Op{Kind: kind, ID: "mv_abcdefghijklmnopqrstuvwxyz", Phase: phase, Exec: "ie-box"}} probe := hostbox.WakePhase(c, "ie-box", true) == nil wake := hostbox.WakePhase(c, "ie-box", false) == nil if probe != (phase == hostclaim.PhaseProbing) { t.Errorf("%s/%q: probe admitted = %v", kind, phase, probe) } if wake != (phase == hostclaim.PhaseProbed || phase == hostclaim.PhaseOpened) { t.Errorf("%s/%q: wake admitted = %v", kind, phase, wake) } } } if hostclaim.RecoveryPhaseOrder(hostclaim.KindLanding) != nil { t.Fatal("landing is not a recovery kind") } } // The rollback id shape is a twin of moves.ValidOpID's rb_ form: a minted // rollback id is accepted, a minted move id is not. func TestRollBackTakesMintedRollbackIDs(t *testing.T) { landing := func() *hostclaim.Claim { return &hostclaim.Claim{Holder: &hostclaim.Holder{Stamp: 8}, Op: hostclaim.Op{Kind: hostclaim.KindLanding, ID: "mv_abcdefghijklmnopqrstuvwxyz", Phase: "probed", Exec: "uk-box", Intent: map[string]string{"to": "uk", "source": "ie-box", "target": "uk-box"}, Participants: []string{"ie-box", "uk-box"}}} } for i := 0; i < 32; i++ { rb, err := moves.NewRollbackID(rand.Reader) if err != nil { t.Fatal(err) } if err := hostclaim.RollBack("mv_abcdefghijklmnopqrstuvwxyz", rb)(landing()); err != nil { t.Fatalf("%s: %v", rb, err) } mv, err := moves.NewMoveID(rand.Reader) if err != nil { t.Fatal(err) } if err := hostclaim.RollBack("mv_abcdefghijklmnopqrstuvwxyz", mv)(landing()); !errors.Is(err, hostclaim.ErrMalformed) { t.Fatalf("%s: %v", mv, err) } } }