github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/worker/machineactions/worker_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Copyright 2016 Cloudbase Solutions SRL
     3  // Licensed under the AGPLv3, see LICENCE file for details.
     4  
     5  package machineactions_test
     6  
     7  import (
     8  	"github.com/juju/errors"
     9  	"github.com/juju/juju/apiserver/params"
    10  	"github.com/juju/juju/worker/machineactions"
    11  	"github.com/juju/juju/worker/workertest"
    12  	"github.com/juju/testing"
    13  	jc "github.com/juju/testing/checkers"
    14  	gc "gopkg.in/check.v1"
    15  	"gopkg.in/juju/names.v2"
    16  )
    17  
    18  type WorkerSuite struct {
    19  	testing.IsolationSuite
    20  }
    21  
    22  var _ = gc.Suite(&WorkerSuite{})
    23  
    24  func (*WorkerSuite) TestInvalidFacade(c *gc.C) {
    25  	worker, err := machineactions.NewMachineActionsWorker(machineactions.WorkerConfig{
    26  		Facade: nil,
    27  	})
    28  	c.Assert(err, gc.ErrorMatches, "nil Facade not valid")
    29  	c.Assert(err, jc.Satisfies, errors.IsNotValid)
    30  	c.Assert(worker, gc.IsNil)
    31  }
    32  
    33  func (*WorkerSuite) TestInvalidMachineTag(c *gc.C) {
    34  	worker, err := machineactions.NewMachineActionsWorker(machineactions.WorkerConfig{
    35  		Facade:     &mockFacade{},
    36  		MachineTag: names.MachineTag{},
    37  	})
    38  	c.Assert(err, gc.ErrorMatches, "unspecified MachineTag not valid")
    39  	c.Assert(err, jc.Satisfies, errors.IsNotValid)
    40  	c.Assert(worker, gc.IsNil)
    41  }
    42  
    43  func (*WorkerSuite) TestInvalidHandleAction(c *gc.C) {
    44  	worker, err := machineactions.NewMachineActionsWorker(machineactions.WorkerConfig{
    45  		Facade:       &mockFacade{},
    46  		MachineTag:   fakeTag,
    47  		HandleAction: nil,
    48  	})
    49  	c.Assert(err, gc.ErrorMatches, "nil HandleAction not valid")
    50  	c.Assert(err, jc.Satisfies, errors.IsNotValid)
    51  	c.Assert(worker, gc.IsNil)
    52  }
    53  
    54  func defaultConfig(stub *testing.Stub) machineactions.WorkerConfig {
    55  	return machineactions.WorkerConfig{
    56  		Facade:       &mockFacade{stub: stub},
    57  		MachineTag:   fakeTag,
    58  		HandleAction: mockHandleAction(stub),
    59  	}
    60  }
    61  
    62  func (*WorkerSuite) TestRunningActionsError(c *gc.C) {
    63  	stub := &testing.Stub{}
    64  	stub.SetErrors(errors.New("splash"))
    65  	worker, err := machineactions.NewMachineActionsWorker(defaultConfig(stub))
    66  	c.Assert(err, jc.ErrorIsNil)
    67  	err = workertest.CheckKilled(c, worker)
    68  	c.Check(err, gc.ErrorMatches, "splash")
    69  
    70  	stub.CheckCalls(c, getSuccessfulCalls(1))
    71  }
    72  
    73  func (*WorkerSuite) TestInvalidActionId(c *gc.C) {
    74  	stub := &testing.Stub{}
    75  	facade := &mockFacade{
    76  		stub: stub,
    77  		watcherSendInvalidValues: true,
    78  	}
    79  	config := machineactions.WorkerConfig{
    80  		Facade:       facade,
    81  		MachineTag:   fakeTag,
    82  		HandleAction: mockHandleAction(stub),
    83  	}
    84  	worker, err := machineactions.NewMachineActionsWorker(config)
    85  	c.Assert(err, jc.ErrorIsNil)
    86  	err = workertest.CheckKilled(c, worker)
    87  	c.Check(err, gc.ErrorMatches, "got invalid action id invalid-action-id")
    88  
    89  	stub.CheckCalls(c, getSuccessfulCalls(allCalls))
    90  }
    91  
    92  func (*WorkerSuite) TestWatchErrorNonEmptyRunningActions(c *gc.C) {
    93  	stub := &testing.Stub{}
    94  	stub.SetErrors(nil, errors.New("ignored"), errors.New("kuso"))
    95  	facade := &mockFacade{
    96  		stub:           stub,
    97  		runningActions: fakeRunningActions,
    98  	}
    99  	config := machineactions.WorkerConfig{
   100  		Facade:       facade,
   101  		MachineTag:   fakeTag,
   102  		HandleAction: mockHandleAction(stub),
   103  	}
   104  	worker, err := machineactions.NewMachineActionsWorker(config)
   105  	c.Assert(err, jc.ErrorIsNil)
   106  	err = workertest.CheckKilled(c, worker)
   107  	c.Check(err, gc.ErrorMatches, "kuso")
   108  
   109  	stub.CheckCalls(c, []testing.StubCall{{
   110  		FuncName: "RunningActions",
   111  		Args:     []interface{}{fakeTag},
   112  	}, {
   113  		FuncName: "ActionFinish",
   114  		Args:     []interface{}{thirdActionTag, params.ActionFailed, "action cancelled"},
   115  	}, {
   116  		FuncName: "WatchActionNotifications",
   117  		Args:     []interface{}{fakeTag},
   118  	}})
   119  }
   120  
   121  func (*WorkerSuite) TestCannotRetrieveFirstAction(c *gc.C) {
   122  	stub := &testing.Stub{}
   123  	stub.SetErrors(nil, nil, errors.New("zbosh"))
   124  	worker, err := machineactions.NewMachineActionsWorker(defaultConfig(stub))
   125  	c.Assert(err, jc.ErrorIsNil)
   126  	err = workertest.CheckKilled(c, worker)
   127  	c.Check(errors.Cause(err), gc.ErrorMatches, "zbosh")
   128  
   129  	stub.CheckCalls(c, getSuccessfulCalls(3))
   130  }
   131  
   132  func (*WorkerSuite) TestCannotBeginAction(c *gc.C) {
   133  	stub := &testing.Stub{}
   134  	stub.SetErrors(nil, nil, nil, errors.New("kermack"))
   135  	worker, err := machineactions.NewMachineActionsWorker(defaultConfig(stub))
   136  	c.Assert(err, jc.ErrorIsNil)
   137  	err = workertest.CheckKilled(c, worker)
   138  	c.Check(errors.Cause(err), gc.ErrorMatches, "kermack")
   139  
   140  	stub.CheckCalls(c, getSuccessfulCalls(4))
   141  }
   142  
   143  func (*WorkerSuite) TestFirstActionHandleErrAndFinishErr(c *gc.C) {
   144  	stub := &testing.Stub{}
   145  	stub.SetErrors(nil, nil, nil, nil, errors.New("sentToActionFinish"), errors.New("slob"))
   146  	worker, err := machineactions.NewMachineActionsWorker(defaultConfig(stub))
   147  	c.Assert(err, jc.ErrorIsNil)
   148  	err = workertest.CheckKilled(c, worker)
   149  	c.Check(errors.Cause(err), gc.ErrorMatches, "slob")
   150  
   151  	successfulCalls := getSuccessfulCalls(6)
   152  	successfulCalls[5].Args = []interface{}{firstActionTag, params.ActionFailed, "sentToActionFinish"}
   153  	stub.CheckCalls(c, successfulCalls)
   154  }
   155  
   156  func (*WorkerSuite) TestFirstActionHandleErrButFinishErrCannotRetrieveSecond(c *gc.C) {
   157  	stub := &testing.Stub{}
   158  	stub.SetErrors(nil, nil, nil, nil, errors.New("sentToActionFinish"), nil, errors.New("gotcha"))
   159  	worker, err := machineactions.NewMachineActionsWorker(defaultConfig(stub))
   160  	c.Assert(err, jc.ErrorIsNil)
   161  	err = workertest.CheckKilled(c, worker)
   162  	c.Check(errors.Cause(err), gc.ErrorMatches, "gotcha")
   163  
   164  	successfulCalls := getSuccessfulCalls(7)
   165  	successfulCalls[5].Args = []interface{}{firstActionTag, params.ActionFailed, "sentToActionFinish"}
   166  	stub.CheckCalls(c, successfulCalls)
   167  }
   168  
   169  func (*WorkerSuite) TestFailHandlingSecondActionSendAllResults(c *gc.C) {
   170  	stub := &testing.Stub{}
   171  	stub.SetErrors(nil, nil, nil, nil, nil, nil, nil, nil, errors.New("kryptonite"))
   172  	worker, err := machineactions.NewMachineActionsWorker(defaultConfig(stub))
   173  	c.Assert(err, jc.ErrorIsNil)
   174  	workertest.CheckAlive(c, worker)
   175  	workertest.CleanKill(c, worker)
   176  
   177  	successfulCalls := getSuccessfulCalls(allCalls)
   178  	successfulCalls[9].Args = []interface{}{secondActionTag, params.ActionFailed, "kryptonite"}
   179  	stub.CheckCalls(c, successfulCalls)
   180  }
   181  
   182  func (*WorkerSuite) TestWorkerNoErr(c *gc.C) {
   183  	stub := &testing.Stub{}
   184  	worker, err := machineactions.NewMachineActionsWorker(defaultConfig(stub))
   185  	c.Assert(err, jc.ErrorIsNil)
   186  
   187  	workertest.CheckAlive(c, worker)
   188  	workertest.CleanKill(c, worker)
   189  	stub.CheckCalls(c, getSuccessfulCalls(allCalls))
   190  }
   191  
   192  const allCalls = 14
   193  
   194  func getSuccessfulCalls(index int) []testing.StubCall {
   195  	successfulCalls := []testing.StubCall{{
   196  		FuncName: "RunningActions",
   197  		Args:     []interface{}{fakeTag},
   198  	}, {
   199  		FuncName: "WatchActionNotifications",
   200  		Args:     []interface{}{fakeTag},
   201  	}, {
   202  		FuncName: "Action",
   203  		Args:     []interface{}{firstActionTag},
   204  	}, {
   205  		FuncName: "ActionBegin",
   206  		Args:     []interface{}{firstActionTag},
   207  	}, {
   208  		FuncName: "HandleAction",
   209  		Args:     []interface{}{firstAction.Name()},
   210  	}, {
   211  		FuncName: "ActionFinish",
   212  		Args:     []interface{}{firstActionTag, params.ActionCompleted, ""},
   213  	}, {
   214  		FuncName: "Action",
   215  		Args:     []interface{}{secondActionTag},
   216  	}, {
   217  		FuncName: "ActionBegin",
   218  		Args:     []interface{}{secondActionTag},
   219  	}, {
   220  		FuncName: "HandleAction",
   221  		Args:     []interface{}{secondAction.Name()},
   222  	}, {
   223  		FuncName: "ActionFinish",
   224  		Args:     []interface{}{secondActionTag, params.ActionCompleted, ""},
   225  	}, {
   226  		FuncName: "Action",
   227  		Args:     []interface{}{thirdActionTag},
   228  	}, {
   229  		FuncName: "ActionBegin",
   230  		Args:     []interface{}{thirdActionTag},
   231  	}, {
   232  		FuncName: "HandleAction",
   233  		Args:     []interface{}{thirdAction.Name()},
   234  	}, {
   235  		FuncName: "ActionFinish",
   236  		Args:     []interface{}{thirdActionTag, params.ActionCompleted, ""},
   237  	}}
   238  	return successfulCalls[:index]
   239  }