github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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/context"
    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(operation.FactoryParams{
    28  		RunnerFactory: runnerFactory,
    29  	})
    30  	sendResponse := func(*utilexec.ExecResponse, error) { panic("not expected") }
    31  	op, err := factory.NewCommands(someCommandArgs, sendResponse)
    32  	c.Assert(err, jc.ErrorIsNil)
    33  
    34  	newState, err := op.Prepare(operation.State{})
    35  	c.Assert(err, gc.ErrorMatches, "blooey")
    36  	c.Assert(newState, gc.IsNil)
    37  	c.Assert(*runnerFactory.MockNewCommandRunner.gotInfo, gc.Equals, context.CommandInfo{
    38  		RelationId:      123,
    39  		RemoteUnitName:  "foo/456",
    40  		ForceRemoteUnit: true,
    41  	})
    42  }
    43  
    44  func (s *RunCommandsSuite) TestPrepareSuccess(c *gc.C) {
    45  	ctx := &MockContext{}
    46  	runnerFactory := &MockRunnerFactory{
    47  		MockNewCommandRunner: &MockNewCommandRunner{
    48  			runner: &MockRunner{
    49  				context: ctx,
    50  			},
    51  		},
    52  	}
    53  	factory := operation.NewFactory(operation.FactoryParams{
    54  		RunnerFactory: runnerFactory,
    55  	})
    56  	sendResponse := func(*utilexec.ExecResponse, error) { panic("not expected") }
    57  	op, err := factory.NewCommands(someCommandArgs, sendResponse)
    58  	c.Assert(err, jc.ErrorIsNil)
    59  
    60  	newState, err := op.Prepare(operation.State{})
    61  	c.Assert(err, jc.ErrorIsNil)
    62  	c.Assert(newState, gc.IsNil)
    63  	c.Assert(*runnerFactory.MockNewCommandRunner.gotInfo, gc.Equals, context.CommandInfo{
    64  		RelationId:      123,
    65  		RemoteUnitName:  "foo/456",
    66  		ForceRemoteUnit: true,
    67  	})
    68  	ctx.CheckCall(c, 0, "Prepare")
    69  }
    70  
    71  func (s *RunCommandsSuite) TestPrepareCtxError(c *gc.C) {
    72  	ctx := &MockContext{}
    73  	ctx.SetErrors(errors.New("ctx prepare error"))
    74  	runnerFactory := &MockRunnerFactory{
    75  		MockNewCommandRunner: &MockNewCommandRunner{
    76  			runner: &MockRunner{
    77  				context: ctx,
    78  			},
    79  		},
    80  	}
    81  	factory := operation.NewFactory(operation.FactoryParams{
    82  		RunnerFactory: runnerFactory,
    83  	})
    84  	sendResponse := func(*utilexec.ExecResponse, error) { panic("not expected") }
    85  	op, err := factory.NewCommands(someCommandArgs, sendResponse)
    86  	c.Assert(err, jc.ErrorIsNil)
    87  
    88  	newState, err := op.Prepare(operation.State{})
    89  	c.Assert(err, gc.ErrorMatches, "ctx prepare error")
    90  	c.Assert(newState, gc.IsNil)
    91  	ctx.CheckCall(c, 0, "Prepare")
    92  }
    93  
    94  func (s *RunCommandsSuite) TestExecuteRebootErrors(c *gc.C) {
    95  	for _, sendErr := range []error{context.ErrRequeueAndReboot, context.ErrReboot} {
    96  		runnerFactory := NewRunCommandsRunnerFactory(
    97  			&utilexec.ExecResponse{Code: 101}, sendErr,
    98  		)
    99  		callbacks := &RunCommandsCallbacks{}
   100  		factory := operation.NewFactory(operation.FactoryParams{
   101  			RunnerFactory: runnerFactory,
   102  			Callbacks:     callbacks,
   103  		})
   104  		sendResponse := &MockSendResponse{}
   105  		op, err := factory.NewCommands(someCommandArgs, sendResponse.Call)
   106  		c.Assert(err, jc.ErrorIsNil)
   107  		_, err = op.Prepare(operation.State{})
   108  		c.Assert(err, jc.ErrorIsNil)
   109  
   110  		newState, err := op.Execute(operation.State{})
   111  		c.Assert(newState, gc.IsNil)
   112  		c.Assert(err, gc.Equals, operation.ErrNeedsReboot)
   113  		c.Assert(*runnerFactory.MockNewCommandRunner.runner.MockRunCommands.gotCommands, gc.Equals, "do something")
   114  		c.Assert(*sendResponse.gotResponse, gc.DeepEquals, &utilexec.ExecResponse{Code: 101})
   115  		c.Assert(*sendResponse.gotErr, jc.ErrorIsNil)
   116  	}
   117  }
   118  
   119  func (s *RunCommandsSuite) TestExecuteOtherError(c *gc.C) {
   120  	runnerFactory := NewRunCommandsRunnerFactory(
   121  		nil, errors.New("sneh"),
   122  	)
   123  	callbacks := &RunCommandsCallbacks{}
   124  	factory := operation.NewFactory(operation.FactoryParams{
   125  		RunnerFactory: runnerFactory,
   126  		Callbacks:     callbacks,
   127  	})
   128  	sendResponse := &MockSendResponse{}
   129  	op, err := factory.NewCommands(someCommandArgs, sendResponse.Call)
   130  	c.Assert(err, jc.ErrorIsNil)
   131  	_, err = op.Prepare(operation.State{})
   132  	c.Assert(err, jc.ErrorIsNil)
   133  
   134  	newState, err := op.Execute(operation.State{})
   135  	c.Assert(newState, gc.IsNil)
   136  	c.Assert(err, gc.ErrorMatches, "sneh")
   137  	c.Assert(*runnerFactory.MockNewCommandRunner.runner.MockRunCommands.gotCommands, gc.Equals, "do something")
   138  	c.Assert(*sendResponse.gotResponse, gc.IsNil)
   139  	c.Assert(*sendResponse.gotErr, gc.ErrorMatches, "sneh")
   140  }
   141  
   142  func (s *RunCommandsSuite) TestExecuteSuccess(c *gc.C) {
   143  	runnerFactory := NewRunCommandsRunnerFactory(
   144  		&utilexec.ExecResponse{Code: 222}, nil,
   145  	)
   146  	callbacks := &RunCommandsCallbacks{}
   147  	factory := operation.NewFactory(operation.FactoryParams{
   148  		RunnerFactory: runnerFactory,
   149  		Callbacks:     callbacks,
   150  	})
   151  	sendResponse := &MockSendResponse{}
   152  	op, err := factory.NewCommands(someCommandArgs, sendResponse.Call)
   153  	c.Assert(err, jc.ErrorIsNil)
   154  	_, err = op.Prepare(operation.State{})
   155  	c.Assert(err, jc.ErrorIsNil)
   156  
   157  	newState, err := op.Execute(operation.State{})
   158  	c.Assert(newState, gc.IsNil)
   159  	c.Assert(err, jc.ErrorIsNil)
   160  	c.Assert(*runnerFactory.MockNewCommandRunner.runner.MockRunCommands.gotCommands, gc.Equals, "do something")
   161  	c.Assert(*sendResponse.gotResponse, gc.DeepEquals, &utilexec.ExecResponse{Code: 222})
   162  	c.Assert(*sendResponse.gotErr, jc.ErrorIsNil)
   163  }
   164  
   165  func (s *RunCommandsSuite) TestCommit(c *gc.C) {
   166  	factory := operation.NewFactory(operation.FactoryParams{})
   167  	sendResponse := func(*utilexec.ExecResponse, error) { panic("not expected") }
   168  	op, err := factory.NewCommands(someCommandArgs, sendResponse)
   169  	c.Assert(err, jc.ErrorIsNil)
   170  	newState, err := op.Commit(operation.State{})
   171  	c.Assert(newState, gc.IsNil)
   172  	c.Assert(err, jc.ErrorIsNil)
   173  }
   174  
   175  func (s *RunCommandsSuite) TestNeedsGlobalMachineLock(c *gc.C) {
   176  	factory := operation.NewFactory(operation.FactoryParams{})
   177  	sendResponse := &MockSendResponse{}
   178  	op, err := factory.NewCommands(someCommandArgs, sendResponse.Call)
   179  	c.Assert(err, jc.ErrorIsNil)
   180  	c.Assert(op.NeedsGlobalMachineLock(), jc.IsTrue)
   181  }