github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/worker/uniter/operation/runcommands_test.go (about) 1 // Copyright 2014 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 utilexec "github.com/juju/utils/exec" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/worker/uniter/operation" 14 "github.com/juju/juju/worker/uniter/runner" 15 ) 16 17 type RunCommandsSuite struct { 18 testing.IsolationSuite 19 } 20 21 var _ = gc.Suite(&RunCommandsSuite{}) 22 23 func (s *RunCommandsSuite) TestPrepareError(c *gc.C) { 24 runnerFactory := &MockRunnerFactory{ 25 MockNewCommandRunner: &MockNewCommandRunner{err: errors.New("blooey")}, 26 } 27 factory := operation.NewFactory(nil, runnerFactory, nil, nil) 28 sendResponse := func(*utilexec.ExecResponse, error) { panic("not expected") } 29 op, err := factory.NewCommands(someCommandArgs, sendResponse) 30 c.Assert(err, jc.ErrorIsNil) 31 32 newState, err := op.Prepare(operation.State{}) 33 c.Assert(err, gc.ErrorMatches, "blooey") 34 c.Assert(newState, gc.IsNil) 35 c.Assert(*runnerFactory.MockNewCommandRunner.gotInfo, gc.Equals, runner.CommandInfo{ 36 RelationId: 123, 37 RemoteUnitName: "foo/456", 38 ForceRemoteUnit: true, 39 }) 40 } 41 42 func (s *RunCommandsSuite) TestPrepareSuccess(c *gc.C) { 43 runnerFactory := &MockRunnerFactory{ 44 MockNewCommandRunner: &MockNewCommandRunner{}, 45 } 46 factory := operation.NewFactory(nil, runnerFactory, nil, nil) 47 sendResponse := func(*utilexec.ExecResponse, error) { panic("not expected") } 48 op, err := factory.NewCommands(someCommandArgs, sendResponse) 49 c.Assert(err, jc.ErrorIsNil) 50 51 newState, err := op.Prepare(operation.State{}) 52 c.Assert(err, jc.ErrorIsNil) 53 c.Assert(newState, gc.IsNil) 54 c.Assert(*runnerFactory.MockNewCommandRunner.gotInfo, gc.Equals, runner.CommandInfo{ 55 RelationId: 123, 56 RemoteUnitName: "foo/456", 57 ForceRemoteUnit: true, 58 }) 59 } 60 61 func (s *RunCommandsSuite) TestExecuteLockError(c *gc.C) { 62 runnerFactory := &MockRunnerFactory{ 63 MockNewCommandRunner: &MockNewCommandRunner{}, 64 } 65 callbacks := &RunCommandsCallbacks{ 66 MockAcquireExecutionLock: &MockAcquireExecutionLock{err: errors.New("sneh")}, 67 } 68 factory := operation.NewFactory(nil, runnerFactory, callbacks, nil) 69 sendResponse := func(*utilexec.ExecResponse, error) { panic("not expected") } 70 op, err := factory.NewCommands(someCommandArgs, sendResponse) 71 c.Assert(err, jc.ErrorIsNil) 72 _, err = op.Prepare(operation.State{}) 73 c.Assert(err, jc.ErrorIsNil) 74 75 newState, err := op.Execute(operation.State{}) 76 c.Assert(newState, gc.IsNil) 77 c.Assert(err, gc.ErrorMatches, "sneh") 78 c.Assert(*callbacks.MockAcquireExecutionLock.gotMessage, gc.Equals, "run commands") 79 } 80 81 func (s *RunCommandsSuite) TestExecuteRebootErrors(c *gc.C) { 82 for _, sendErr := range []error{runner.ErrRequeueAndReboot, runner.ErrReboot} { 83 runnerFactory := NewRunCommandsRunnerFactory( 84 &utilexec.ExecResponse{Code: 101}, sendErr, 85 ) 86 callbacks := &RunCommandsCallbacks{ 87 MockAcquireExecutionLock: &MockAcquireExecutionLock{}, 88 } 89 factory := operation.NewFactory(nil, runnerFactory, callbacks, nil) 90 sendResponse := &MockSendResponse{} 91 op, err := factory.NewCommands(someCommandArgs, sendResponse.Call) 92 c.Assert(err, jc.ErrorIsNil) 93 _, err = op.Prepare(operation.State{}) 94 c.Assert(err, jc.ErrorIsNil) 95 96 newState, err := op.Execute(operation.State{}) 97 c.Assert(newState, gc.IsNil) 98 c.Assert(err, jc.ErrorIsNil) 99 c.Assert(*callbacks.MockAcquireExecutionLock.gotMessage, gc.Equals, "run commands") 100 c.Assert(callbacks.MockAcquireExecutionLock.didUnlock, jc.IsTrue) 101 c.Assert(*runnerFactory.MockNewCommandRunner.runner.MockRunCommands.gotCommands, gc.Equals, "do something") 102 c.Assert(*sendResponse.gotResponse, gc.DeepEquals, &utilexec.ExecResponse{Code: 101}) 103 c.Assert(*sendResponse.gotErr, gc.Equals, operation.ErrNeedsReboot) 104 } 105 } 106 107 func (s *RunCommandsSuite) TestExecuteOtherError(c *gc.C) { 108 runnerFactory := NewRunCommandsRunnerFactory( 109 nil, errors.New("sneh"), 110 ) 111 callbacks := &RunCommandsCallbacks{ 112 MockAcquireExecutionLock: &MockAcquireExecutionLock{}, 113 } 114 factory := operation.NewFactory(nil, runnerFactory, callbacks, nil) 115 sendResponse := &MockSendResponse{} 116 op, err := factory.NewCommands(someCommandArgs, sendResponse.Call) 117 c.Assert(err, jc.ErrorIsNil) 118 _, err = op.Prepare(operation.State{}) 119 c.Assert(err, jc.ErrorIsNil) 120 121 newState, err := op.Execute(operation.State{}) 122 c.Assert(newState, gc.IsNil) 123 c.Assert(err, jc.ErrorIsNil) 124 c.Assert(*callbacks.MockAcquireExecutionLock.gotMessage, gc.Equals, "run commands") 125 c.Assert(callbacks.MockAcquireExecutionLock.didUnlock, jc.IsTrue) 126 c.Assert(*runnerFactory.MockNewCommandRunner.runner.MockRunCommands.gotCommands, gc.Equals, "do something") 127 c.Assert(*sendResponse.gotResponse, gc.IsNil) 128 c.Assert(*sendResponse.gotErr, gc.ErrorMatches, "sneh") 129 } 130 131 func (s *RunCommandsSuite) TestExecuteSuccess(c *gc.C) { 132 runnerFactory := NewRunCommandsRunnerFactory( 133 &utilexec.ExecResponse{Code: 222}, nil, 134 ) 135 callbacks := &RunCommandsCallbacks{ 136 MockAcquireExecutionLock: &MockAcquireExecutionLock{}, 137 } 138 factory := operation.NewFactory(nil, runnerFactory, callbacks, nil) 139 sendResponse := &MockSendResponse{} 140 op, err := factory.NewCommands(someCommandArgs, sendResponse.Call) 141 c.Assert(err, jc.ErrorIsNil) 142 _, err = op.Prepare(operation.State{}) 143 c.Assert(err, jc.ErrorIsNil) 144 145 newState, err := op.Execute(operation.State{}) 146 c.Assert(newState, gc.IsNil) 147 c.Assert(err, jc.ErrorIsNil) 148 c.Assert(*callbacks.MockAcquireExecutionLock.gotMessage, gc.Equals, "run commands") 149 c.Assert(callbacks.MockAcquireExecutionLock.didUnlock, jc.IsTrue) 150 c.Assert(*runnerFactory.MockNewCommandRunner.runner.MockRunCommands.gotCommands, gc.Equals, "do something") 151 c.Assert(*sendResponse.gotResponse, gc.DeepEquals, &utilexec.ExecResponse{Code: 222}) 152 c.Assert(*sendResponse.gotErr, jc.ErrorIsNil) 153 } 154 155 func (s *RunCommandsSuite) TestCommit(c *gc.C) { 156 factory := operation.NewFactory(nil, nil, nil, nil) 157 sendResponse := func(*utilexec.ExecResponse, error) { panic("not expected") } 158 op, err := factory.NewCommands(someCommandArgs, sendResponse) 159 c.Assert(err, jc.ErrorIsNil) 160 newState, err := op.Commit(operation.State{}) 161 c.Assert(newState, gc.IsNil) 162 c.Assert(err, jc.ErrorIsNil) 163 }