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