github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/migrationflag/worker_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package migrationflag_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/worker.v1/workertest"
    12  
    13  	"github.com/juju/juju/core/migration"
    14  	"github.com/juju/juju/worker/migrationflag"
    15  )
    16  
    17  type WorkerSuite struct {
    18  	testing.IsolationSuite
    19  }
    20  
    21  var _ = gc.Suite(&WorkerSuite{})
    22  
    23  func (*WorkerSuite) TestPhaseErrorOnStartup(c *gc.C) {
    24  	stub := &testing.Stub{}
    25  	stub.SetErrors(errors.New("gaah"))
    26  	facade := newMockFacade(stub)
    27  	config := migrationflag.Config{
    28  		Facade: facade,
    29  		Model:  validUUID,
    30  		Check:  panicCheck,
    31  	}
    32  	worker, err := migrationflag.New(config)
    33  	c.Check(worker, gc.IsNil)
    34  	c.Check(err, gc.ErrorMatches, "gaah")
    35  	checkCalls(c, stub, "Phase")
    36  }
    37  
    38  func (*WorkerSuite) TestWatchError(c *gc.C) {
    39  	stub := &testing.Stub{}
    40  	stub.SetErrors(nil, errors.New("boff"))
    41  	facade := newMockFacade(stub, migration.REAP)
    42  	config := migrationflag.Config{
    43  		Facade: facade,
    44  		Model:  validUUID,
    45  		Check:  neverCheck,
    46  	}
    47  	worker, err := migrationflag.New(config)
    48  	c.Assert(err, jc.ErrorIsNil)
    49  	c.Check(worker.Check(), jc.IsFalse)
    50  
    51  	err = workertest.CheckKilled(c, worker)
    52  	c.Check(err, gc.ErrorMatches, "boff")
    53  	checkCalls(c, stub, "Phase", "Watch")
    54  }
    55  
    56  func (*WorkerSuite) TestPhaseErrorWhileRunning(c *gc.C) {
    57  	stub := &testing.Stub{}
    58  	stub.SetErrors(nil, nil, errors.New("glug"))
    59  	facade := newMockFacade(stub, migration.QUIESCE)
    60  	config := migrationflag.Config{
    61  		Facade: facade,
    62  		Model:  validUUID,
    63  		Check:  neverCheck,
    64  	}
    65  	worker, err := migrationflag.New(config)
    66  	c.Assert(err, jc.ErrorIsNil)
    67  	c.Check(worker.Check(), jc.IsFalse)
    68  
    69  	err = workertest.CheckKilled(c, worker)
    70  	c.Check(err, gc.ErrorMatches, "glug")
    71  	checkCalls(c, stub, "Phase", "Watch", "Phase")
    72  }
    73  
    74  func (*WorkerSuite) TestImmediatePhaseChange(c *gc.C) {
    75  	stub := &testing.Stub{}
    76  	facade := newMockFacade(stub,
    77  		migration.QUIESCE, migration.REAP,
    78  	)
    79  	config := migrationflag.Config{
    80  		Facade: facade,
    81  		Model:  validUUID,
    82  		Check:  isQuiesce,
    83  	}
    84  	worker, err := migrationflag.New(config)
    85  	c.Assert(err, jc.ErrorIsNil)
    86  	c.Check(worker.Check(), jc.IsTrue)
    87  
    88  	err = workertest.CheckKilled(c, worker)
    89  	c.Check(err, gc.Equals, migrationflag.ErrChanged)
    90  	checkCalls(c, stub, "Phase", "Watch", "Phase")
    91  }
    92  
    93  func (*WorkerSuite) TestSubsequentPhaseChange(c *gc.C) {
    94  	stub := &testing.Stub{}
    95  	facade := newMockFacade(stub,
    96  		migration.ABORT, migration.REAP, migration.QUIESCE,
    97  	)
    98  	config := migrationflag.Config{
    99  		Facade: facade,
   100  		Model:  validUUID,
   101  		Check:  isQuiesce,
   102  	}
   103  	worker, err := migrationflag.New(config)
   104  	c.Assert(err, jc.ErrorIsNil)
   105  	c.Check(worker.Check(), jc.IsFalse)
   106  
   107  	err = workertest.CheckKilled(c, worker)
   108  	c.Check(err, gc.Equals, migrationflag.ErrChanged)
   109  	checkCalls(c, stub, "Phase", "Watch", "Phase", "Phase")
   110  }
   111  
   112  func (*WorkerSuite) TestNoRelevantPhaseChange(c *gc.C) {
   113  	stub := &testing.Stub{}
   114  	facade := newMockFacade(stub,
   115  		migration.REAPFAILED,
   116  		migration.DONE,
   117  		migration.ABORT,
   118  		migration.IMPORT,
   119  	)
   120  	config := migrationflag.Config{
   121  		Facade: facade,
   122  		Model:  validUUID,
   123  		Check:  isQuiesce,
   124  	}
   125  	worker, err := migrationflag.New(config)
   126  	c.Assert(err, jc.ErrorIsNil)
   127  	c.Check(worker.Check(), jc.IsFalse)
   128  
   129  	workertest.CheckAlive(c, worker)
   130  	workertest.CleanKill(c, worker)
   131  	checkCalls(c, stub, "Phase", "Watch", "Phase", "Phase", "Phase")
   132  }
   133  
   134  func (*WorkerSuite) TestIsTerminal(c *gc.C) {
   135  	tests := []struct {
   136  		phase    migration.Phase
   137  		expected bool
   138  	}{
   139  		{migration.QUIESCE, false},
   140  		{migration.SUCCESS, false},
   141  		{migration.ABORT, false},
   142  		{migration.NONE, true},
   143  		{migration.UNKNOWN, true},
   144  		{migration.ABORTDONE, true},
   145  		{migration.DONE, true},
   146  	}
   147  	for _, t := range tests {
   148  		c.Check(migrationflag.IsTerminal(t.phase), gc.Equals, t.expected,
   149  			gc.Commentf("for %s", t.phase))
   150  	}
   151  }