github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/uniter/operation/failaction_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package operation_test 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/testing" 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 "gopkg.in/juju/charm.v6/hooks" 12 13 "github.com/juju/juju/worker/uniter/hook" 14 "github.com/juju/juju/worker/uniter/operation" 15 ) 16 17 type FailActionSuite struct { 18 testing.IsolationSuite 19 } 20 21 var _ = gc.Suite(&FailActionSuite{}) 22 23 func (s *FailActionSuite) TestPrepare(c *gc.C) { 24 factory := operation.NewFactory(operation.FactoryParams{}) 25 op, err := factory.NewFailAction(someActionId) 26 c.Assert(err, jc.ErrorIsNil) 27 28 newState, err := op.Prepare(operation.State{}) 29 c.Assert(err, jc.ErrorIsNil) 30 c.Assert(newState, jc.DeepEquals, &operation.State{ 31 Kind: operation.RunAction, 32 Step: operation.Pending, 33 ActionId: &someActionId, 34 }) 35 } 36 37 func (s *FailActionSuite) TestExecuteSuccess(c *gc.C) { 38 var stateChangeTests = []struct { 39 description string 40 before operation.State 41 after operation.State 42 }{{ 43 description: "empty state", 44 after: operation.State{ 45 Kind: operation.RunAction, 46 Step: operation.Done, 47 ActionId: &someActionId, 48 }, 49 }, { 50 description: "preserves appropriate fields", 51 before: overwriteState, 52 after: operation.State{ 53 Kind: operation.RunAction, 54 Step: operation.Done, 55 ActionId: &someActionId, 56 Hook: &hook.Info{Kind: hooks.Install}, 57 Started: true, 58 }, 59 }} 60 61 for i, test := range stateChangeTests { 62 c.Logf("test %d: %s", i, test.description) 63 callbacks := &RunActionCallbacks{MockFailAction: &MockFailAction{}} 64 factory := operation.NewFactory(operation.FactoryParams{ 65 Callbacks: callbacks, 66 }) 67 op, err := factory.NewFailAction(someActionId) 68 c.Assert(err, jc.ErrorIsNil) 69 midState, err := op.Prepare(test.before) 70 c.Assert(midState, gc.NotNil) 71 c.Assert(err, jc.ErrorIsNil) 72 73 newState, err := op.Execute(*midState) 74 c.Assert(err, jc.ErrorIsNil) 75 c.Assert(newState, jc.DeepEquals, &test.after) 76 c.Assert(*callbacks.MockFailAction.gotMessage, gc.Equals, "action terminated") 77 c.Assert(*callbacks.MockFailAction.gotActionId, gc.Equals, someActionId) 78 } 79 } 80 81 func (s *FailActionSuite) TestExecuteFail(c *gc.C) { 82 st := operation.State{ 83 Kind: operation.RunAction, 84 Step: operation.Done, 85 ActionId: &someActionId, 86 } 87 callbacks := &RunActionCallbacks{MockFailAction: &MockFailAction{err: errors.New("squelch")}} 88 factory := operation.NewFactory(operation.FactoryParams{ 89 Callbacks: callbacks, 90 }) 91 op, err := factory.NewFailAction(someActionId) 92 c.Assert(err, jc.ErrorIsNil) 93 midState, err := op.Prepare(st) 94 c.Assert(midState, gc.NotNil) 95 c.Assert(err, jc.ErrorIsNil) 96 97 _, err = op.Execute(*midState) 98 c.Assert(err, gc.ErrorMatches, "squelch") 99 } 100 101 func (s *FailActionSuite) TestCommit(c *gc.C) { 102 var stateChangeTests = []struct { 103 description string 104 before operation.State 105 after operation.State 106 }{{ 107 description: "empty state", 108 after: operation.State{ 109 Kind: operation.Continue, 110 Step: operation.Pending, 111 }, 112 }, { 113 description: "preserves only appropriate fields, no hook", 114 before: operation.State{ 115 Kind: operation.Continue, 116 Step: operation.Pending, 117 Started: true, 118 CharmURL: curl("cs:quantal/wordpress-2"), 119 ActionId: &randomActionId, 120 }, 121 after: operation.State{ 122 Kind: operation.Continue, 123 Step: operation.Pending, 124 Started: true, 125 }, 126 }, { 127 description: "preserves only appropriate fields, with hook", 128 before: operation.State{ 129 Kind: operation.Continue, 130 Step: operation.Pending, 131 Started: true, 132 CharmURL: curl("cs:quantal/wordpress-2"), 133 ActionId: &randomActionId, 134 Hook: &hook.Info{Kind: hooks.Install}, 135 }, 136 after: operation.State{ 137 Kind: operation.RunHook, 138 Step: operation.Pending, 139 Hook: &hook.Info{Kind: hooks.Install}, 140 Started: true, 141 }, 142 }} 143 144 for i, test := range stateChangeTests { 145 c.Logf("test %d: %s", i, test.description) 146 factory := operation.NewFactory(operation.FactoryParams{}) 147 op, err := factory.NewFailAction(someActionId) 148 c.Assert(err, jc.ErrorIsNil) 149 150 newState, err := op.Commit(test.before) 151 c.Assert(newState, jc.DeepEquals, &test.after) 152 } 153 } 154 155 func (s *FailActionSuite) TestNeedsGlobalMachineLock(c *gc.C) { 156 factory := operation.NewFactory(operation.FactoryParams{}) 157 op, err := factory.NewFailAction(someActionId) 158 c.Assert(err, jc.ErrorIsNil) 159 c.Assert(op.NeedsGlobalMachineLock(), jc.IsTrue) 160 }