github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/undertaker/undertaker_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package undertaker_test
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/testing"
     9  	gc "gopkg.in/check.v1"
    10  
    11  	"github.com/juju/juju/apiserver/params"
    12  	"github.com/juju/juju/status"
    13  	"github.com/juju/juju/worker"
    14  	"github.com/juju/juju/worker/workertest"
    15  )
    16  
    17  // UndertakerSuite is *not* complete. But it's a lot more so
    18  // than it was before, and should be much easier to extend.
    19  type UndertakerSuite struct {
    20  	testing.IsolationSuite
    21  	fix fixture
    22  }
    23  
    24  var _ = gc.Suite(&UndertakerSuite{})
    25  
    26  func (s *UndertakerSuite) SetUpTest(c *gc.C) {
    27  	s.IsolationSuite.SetUpTest(c)
    28  	s.fix = fixture{
    29  		info: params.UndertakerModelInfoResult{
    30  			Result: params.UndertakerModelInfo{
    31  				Life: "dying",
    32  			},
    33  		},
    34  	}
    35  }
    36  
    37  func (s *UndertakerSuite) TestAliveError(c *gc.C) {
    38  	s.fix.info.Result.Life = "alive"
    39  	s.fix.dirty = true
    40  	stub := s.fix.run(c, func(w worker.Worker) {
    41  		err := workertest.CheckKilled(c, w)
    42  		c.Check(err, gc.ErrorMatches, "model still alive")
    43  	})
    44  	stub.CheckCallNames(c, "ModelInfo")
    45  }
    46  
    47  func (s *UndertakerSuite) TestAlreadyDeadRemoves(c *gc.C) {
    48  	s.fix.info.Result.Life = "dead"
    49  	stub := s.fix.run(c, func(w worker.Worker) {
    50  		workertest.CheckKilled(c, w)
    51  	})
    52  	stub.CheckCallNames(c, "ModelInfo", "SetStatus", "Destroy", "RemoveModel")
    53  }
    54  
    55  func (s *UndertakerSuite) TestDyingDeadRemoved(c *gc.C) {
    56  	stub := s.fix.run(c, func(w worker.Worker) {
    57  		workertest.CheckKilled(c, w)
    58  	})
    59  	stub.CheckCallNames(c,
    60  		"ModelInfo",
    61  		"SetStatus",
    62  		"WatchModelResources",
    63  		"ProcessDyingModel",
    64  		"SetStatus",
    65  		"Destroy",
    66  		"RemoveModel",
    67  	)
    68  }
    69  
    70  func (s *UndertakerSuite) TestSetStatusDestroying(c *gc.C) {
    71  	stub := s.fix.run(c, func(w worker.Worker) {
    72  		workertest.CheckKilled(c, w)
    73  	})
    74  	stub.CheckCall(
    75  		c, 1, "SetStatus", status.Destroying,
    76  		"cleaning up cloud resources", map[string]interface{}(nil),
    77  	)
    78  	stub.CheckCall(
    79  		c, 4, "SetStatus", status.Destroying,
    80  		"tearing down cloud environment", map[string]interface{}(nil),
    81  	)
    82  }
    83  
    84  func (s *UndertakerSuite) TestControllerStopsWhenModelDead(c *gc.C) {
    85  	s.fix.info.Result.IsSystem = true
    86  	stub := s.fix.run(c, func(w worker.Worker) {
    87  		workertest.CheckKilled(c, w)
    88  	})
    89  	stub.CheckCallNames(c,
    90  		"ModelInfo",
    91  		"SetStatus",
    92  		"WatchModelResources",
    93  		"ProcessDyingModel",
    94  	)
    95  }
    96  
    97  func (s *UndertakerSuite) TestModelInfoErrorFatal(c *gc.C) {
    98  	s.fix.errors = []error{errors.New("pow")}
    99  	s.fix.dirty = true
   100  	stub := s.fix.run(c, func(w worker.Worker) {
   101  		err := workertest.CheckKilled(c, w)
   102  		c.Check(err, gc.ErrorMatches, "pow")
   103  	})
   104  	stub.CheckCallNames(c, "ModelInfo")
   105  }
   106  
   107  func (s *UndertakerSuite) TestWatchModelResourcesErrorFatal(c *gc.C) {
   108  	s.fix.errors = []error{nil, nil, errors.New("pow")}
   109  	s.fix.dirty = true
   110  	stub := s.fix.run(c, func(w worker.Worker) {
   111  		err := workertest.CheckKilled(c, w)
   112  		c.Check(err, gc.ErrorMatches, "pow")
   113  	})
   114  	stub.CheckCallNames(c, "ModelInfo", "SetStatus", "WatchModelResources")
   115  }
   116  
   117  func (s *UndertakerSuite) TestProcessDyingModelErrorRetried(c *gc.C) {
   118  	s.fix.errors = []error{
   119  		nil, // ModelInfo
   120  		nil, // SetStatus
   121  		nil, // WatchModelResources,
   122  		errors.New("meh, will retry"),  // ProcessDyingModel,
   123  		errors.New("will retry again"), // ProcessDyingModel,
   124  		nil, // ProcessDyingModel,
   125  		nil, // SetStatus
   126  		nil, // Destroy,
   127  		nil, // RemoveModel
   128  	}
   129  	stub := s.fix.run(c, func(w worker.Worker) {
   130  		workertest.CheckKilled(c, w)
   131  	})
   132  	stub.CheckCallNames(c,
   133  		"ModelInfo",
   134  		"SetStatus",
   135  		"WatchModelResources",
   136  		"ProcessDyingModel",
   137  		"ProcessDyingModel",
   138  		"ProcessDyingModel",
   139  		"SetStatus",
   140  		"Destroy",
   141  		"RemoveModel",
   142  	)
   143  }
   144  
   145  func (s *UndertakerSuite) TestDestroyErrorFatal(c *gc.C) {
   146  	s.fix.errors = []error{nil, nil, errors.New("pow")}
   147  	s.fix.info.Result.Life = "dead"
   148  	s.fix.dirty = true
   149  	stub := s.fix.run(c, func(w worker.Worker) {
   150  		err := workertest.CheckKilled(c, w)
   151  		c.Check(err, gc.ErrorMatches, "pow")
   152  	})
   153  	stub.CheckCallNames(c, "ModelInfo", "SetStatus", "Destroy")
   154  }
   155  
   156  func (s *UndertakerSuite) TestRemoveModelErrorFatal(c *gc.C) {
   157  	s.fix.errors = []error{nil, nil, nil, errors.New("pow")}
   158  	s.fix.info.Result.Life = "dead"
   159  	s.fix.dirty = true
   160  	stub := s.fix.run(c, func(w worker.Worker) {
   161  		err := workertest.CheckKilled(c, w)
   162  		c.Check(err, gc.ErrorMatches, "cannot remove model: pow")
   163  	})
   164  	stub.CheckCallNames(c, "ModelInfo", "SetStatus", "Destroy", "RemoveModel")
   165  }