github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/apiserver/controller/destroy_test.go (about)

     1  // Copyright 2012-2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package controller_test
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  	"gopkg.in/juju/names.v2"
    10  
    11  	"github.com/juju/juju/apiserver/common"
    12  	commontesting "github.com/juju/juju/apiserver/common/testing"
    13  	"github.com/juju/juju/apiserver/controller"
    14  	"github.com/juju/juju/apiserver/params"
    15  	apiservertesting "github.com/juju/juju/apiserver/testing"
    16  	jujutesting "github.com/juju/juju/juju/testing"
    17  	"github.com/juju/juju/state"
    18  	"github.com/juju/juju/testing"
    19  	"github.com/juju/juju/testing/factory"
    20  )
    21  
    22  // NOTE: the testing of the general environment destruction code
    23  // is found in apiserver/common/environdestroy_test.go.
    24  //
    25  // The tests here are around the validation and behaviour of
    26  // the flags passed in to the destroy controller call.
    27  
    28  type destroyControllerSuite struct {
    29  	jujutesting.JujuConnSuite
    30  	commontesting.BlockHelper
    31  
    32  	controller *controller.ControllerAPI
    33  
    34  	otherState          *state.State
    35  	otherEnvOwner       names.UserTag
    36  	otherModelUUID      string
    37  	modelManagerBackend common.ModelManagerBackend
    38  }
    39  
    40  var _ = gc.Suite(&destroyControllerSuite{})
    41  
    42  func (s *destroyControllerSuite) SetUpTest(c *gc.C) {
    43  	s.JujuConnSuite.SetUpTest(c)
    44  
    45  	s.BlockHelper = commontesting.NewBlockHelper(s.APIState)
    46  	s.AddCleanup(func(*gc.C) { s.BlockHelper.Close() })
    47  
    48  	resources := common.NewResources()
    49  	s.AddCleanup(func(_ *gc.C) { resources.StopAll() })
    50  
    51  	authoriser := apiservertesting.FakeAuthorizer{
    52  		Tag: s.AdminUserTag(c),
    53  	}
    54  	controller, err := controller.NewControllerAPI(s.State, resources, authoriser)
    55  	c.Assert(err, jc.ErrorIsNil)
    56  	s.controller = controller
    57  
    58  	s.otherEnvOwner = names.NewUserTag("jess@dummy")
    59  	s.otherState = factory.NewFactory(s.State).MakeModel(c, &factory.ModelParams{
    60  		Name:  "dummytoo",
    61  		Owner: s.otherEnvOwner,
    62  		ConfigAttrs: testing.Attrs{
    63  			"controller": false,
    64  		},
    65  	})
    66  	s.AddCleanup(func(c *gc.C) { s.otherState.Close() })
    67  	s.otherModelUUID = s.otherState.ModelUUID()
    68  	s.modelManagerBackend = common.NewModelManagerBackend(s.State)
    69  }
    70  
    71  func (s *destroyControllerSuite) TestDestroyControllerKillErrsOnHostedEnvsWithBlocks(c *gc.C) {
    72  	s.BlockDestroyModel(c, "TestBlockDestroyModel")
    73  	s.BlockRemoveObject(c, "TestBlockRemoveObject")
    74  	s.otherState.SwitchBlockOn(state.DestroyBlock, "TestBlockDestroyModel")
    75  	s.otherState.SwitchBlockOn(state.ChangeBlock, "TestChangeBlock")
    76  
    77  	err := s.controller.DestroyController(params.DestroyControllerArgs{
    78  		DestroyModels: true,
    79  	})
    80  	c.Assert(err, gc.ErrorMatches, "found blocks in controller models")
    81  
    82  	env, err := s.State.Model()
    83  	c.Assert(err, jc.ErrorIsNil)
    84  	c.Assert(env.Life(), gc.Equals, state.Alive)
    85  }
    86  
    87  func (s *destroyControllerSuite) TestDestroyControllerReturnsBlockedEnvironmentsErr(c *gc.C) {
    88  	s.BlockDestroyModel(c, "TestBlockDestroyModel")
    89  	s.BlockRemoveObject(c, "TestBlockRemoveObject")
    90  	s.otherState.SwitchBlockOn(state.DestroyBlock, "TestBlockDestroyModel")
    91  	s.otherState.SwitchBlockOn(state.ChangeBlock, "TestChangeBlock")
    92  
    93  	err := s.controller.DestroyController(params.DestroyControllerArgs{
    94  		DestroyModels: true,
    95  	})
    96  	c.Assert(params.IsCodeOperationBlocked(err), jc.IsTrue)
    97  
    98  	numBlocks, err := s.State.AllBlocksForController()
    99  	c.Assert(err, jc.ErrorIsNil)
   100  	c.Assert(len(numBlocks), gc.Equals, 4)
   101  
   102  	_, err = s.otherState.Model()
   103  	c.Assert(err, jc.ErrorIsNil)
   104  }
   105  
   106  func (s *destroyControllerSuite) TestDestroyControllerKillsHostedEnvs(c *gc.C) {
   107  	err := s.controller.DestroyController(params.DestroyControllerArgs{
   108  		DestroyModels: true,
   109  	})
   110  	c.Assert(err, jc.ErrorIsNil)
   111  
   112  	env, err := s.State.Model()
   113  	c.Assert(err, jc.ErrorIsNil)
   114  	c.Assert(env.Life(), gc.Equals, state.Dying)
   115  }
   116  
   117  func (s *destroyControllerSuite) TestDestroyControllerLeavesBlocksIfNotKillAll(c *gc.C) {
   118  	s.BlockDestroyModel(c, "TestBlockDestroyModel")
   119  	s.BlockRemoveObject(c, "TestBlockRemoveObject")
   120  	s.otherState.SwitchBlockOn(state.DestroyBlock, "TestBlockDestroyModel")
   121  	s.otherState.SwitchBlockOn(state.ChangeBlock, "TestChangeBlock")
   122  
   123  	err := s.controller.DestroyController(params.DestroyControllerArgs{})
   124  	c.Assert(err, gc.ErrorMatches, "found blocks in controller models")
   125  
   126  	numBlocks, err := s.State.AllBlocksForController()
   127  	c.Assert(err, jc.ErrorIsNil)
   128  	c.Assert(len(numBlocks), gc.Equals, 4)
   129  }
   130  
   131  func (s *destroyControllerSuite) TestDestroyControllerNoHostedEnvs(c *gc.C) {
   132  	err := common.DestroyModel(s.modelManagerBackend, s.otherState.ModelTag())
   133  	c.Assert(err, jc.ErrorIsNil)
   134  
   135  	err = s.controller.DestroyController(params.DestroyControllerArgs{})
   136  	c.Assert(err, jc.ErrorIsNil)
   137  
   138  	env, err := s.State.Model()
   139  	c.Assert(err, jc.ErrorIsNil)
   140  	c.Assert(env.Life(), gc.Equals, state.Dying)
   141  }
   142  
   143  func (s *destroyControllerSuite) TestDestroyControllerErrsOnNoHostedEnvsWithBlock(c *gc.C) {
   144  	err := common.DestroyModel(s.modelManagerBackend, s.otherState.ModelTag())
   145  	c.Assert(err, jc.ErrorIsNil)
   146  
   147  	s.BlockDestroyModel(c, "TestBlockDestroyModel")
   148  	s.BlockRemoveObject(c, "TestBlockRemoveObject")
   149  
   150  	err = s.controller.DestroyController(params.DestroyControllerArgs{})
   151  	c.Assert(err, gc.ErrorMatches, "found blocks in controller models")
   152  	env, err := s.State.Model()
   153  	c.Assert(err, jc.ErrorIsNil)
   154  	c.Assert(env.Life(), gc.Equals, state.Alive)
   155  }
   156  
   157  func (s *destroyControllerSuite) TestDestroyControllerNoHostedEnvsWithBlockFail(c *gc.C) {
   158  	err := common.DestroyModel(s.modelManagerBackend, s.otherState.ModelTag())
   159  	c.Assert(err, jc.ErrorIsNil)
   160  
   161  	s.BlockDestroyModel(c, "TestBlockDestroyModel")
   162  	s.BlockRemoveObject(c, "TestBlockRemoveObject")
   163  
   164  	err = s.controller.DestroyController(params.DestroyControllerArgs{})
   165  	c.Assert(params.IsCodeOperationBlocked(err), jc.IsTrue)
   166  
   167  	numBlocks, err := s.State.AllBlocksForController()
   168  	c.Assert(err, jc.ErrorIsNil)
   169  	c.Assert(len(numBlocks), gc.Equals, 2)
   170  }