| 1 | package moves |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/carlosframework/platform/internal/provision" |
| 7 | ) |
| 8 | |
| 9 | // Final review M1: a stuck entry names the participant that stopped. The |
| 10 | // target stops for an operator at its return (returning) and at its release |
| 11 | // (releasing, StuckReleaseTargetFiles); every other stuck is the source's. |
| 12 | func TestStuckNamesTheParticipantThatStopped(t *testing.T) { |
| 13 | rec := func(state, target, source, reason string, dirty bool) provision.Record { |
| 14 | return provision.Record{Host: "a.example.com", Move: &provision.MoveState{ID: "mv_abcdefghijklmnopqrstuvwxyz", Incarnation: "i", From: "ie", To: "uk", |
| 15 | State: state, StuckReason: reason, |
| 16 | Rollback: &provision.MoveRollback{ID: "rb_abcdefghijklmnopqrstuvwxyz", TargetPhase: target, SourcePhase: source, Dirty: dirty}}} |
| 17 | } |
| 18 | for _, c := range []struct { |
| 19 | name string |
| 20 | target, source string |
| 21 | reason string |
| 22 | dirty bool |
| 23 | want string |
| 24 | }{ |
| 25 | {"the target's release", provision.RbTargetReleasing, provision.RbSourceWaiting, provision.StuckReleaseTargetFiles, false, "target"}, |
| 26 | {"the target's return", provision.RbTargetReturning, provision.RbSourceWaiting, provision.StuckReturnTargetFiles, true, "target"}, |
| 27 | {"the source's verification", provision.RbTargetCleaned, provision.RbSourceVerified, provision.StuckBaselineMismatch, false, "source"}, |
| 28 | } { |
| 29 | before := rec(provision.MoveRollingBack, c.target, c.source, "", c.dirty) |
| 30 | after := rec(provision.MoveStuck, c.target, c.source, c.reason, c.dirty) |
| 31 | e, ok := TransitionOf(before, after) |
| 32 | if !ok || e.Event != "stuck" || e.Fields["role"] != c.want || e.Fields["reason"] != c.reason { |
| 33 | t.Fatalf("%s: %+v %v, want stuck with role %s", c.name, e, ok, c.want) |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // Final re-review 1, N1: DeclaredLost names a target loss only for a move on |
| 39 | // the rollback path a target loss is declared on: a landing (which the |
| 40 | // declaration rolls back) or a rollback. It fabricates none for any other |
| 41 | // state, a committed move's above all. |
| 42 | func TestDeclaredLostOnlyOnTheRollbackPath(t *testing.T) { |
| 43 | for state, want := range map[string]bool{ |
| 44 | provision.MoveLanding: true, provision.MoveRollingBack: true, |
| 45 | provision.MoveCommitted: false, provision.MovePreparing: false, provision.MoveCutting: false, provision.MoveReverting: false, provision.MoveStuck: false, |
| 46 | } { |
| 47 | e, ok := DeclaredLost(&provision.MoveState{ID: "mv_abcdefghijklmnopqrstuvwxyz", Incarnation: "i", From: "ie", To: "uk", State: state}) |
| 48 | if ok != want || ok && (e.Event != "declared_lost" || e.Identity != DeclaredLostIdentity) { |
| 49 | t.Errorf("%s: %+v %v, want %v", state, e, ok, want) |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |