github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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  	"github.com/juju/names"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    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  }
    38  
    39  var _ = gc.Suite(&destroyControllerSuite{})
    40  
    41  func (s *destroyControllerSuite) SetUpTest(c *gc.C) {
    42  	s.JujuConnSuite.SetUpTest(c)
    43  
    44  	s.BlockHelper = commontesting.NewBlockHelper(s.APIState)
    45  	s.AddCleanup(func(*gc.C) { s.BlockHelper.Close() })
    46  
    47  	resources := common.NewResources()
    48  	s.AddCleanup(func(_ *gc.C) { resources.StopAll() })
    49  
    50  	authoriser := apiservertesting.FakeAuthorizer{
    51  		Tag: s.AdminUserTag(c),
    52  	}
    53  	controller, err := controller.NewControllerAPI(s.State, resources, authoriser)
    54  	c.Assert(err, jc.ErrorIsNil)
    55  	s.controller = controller
    56  
    57  	s.otherEnvOwner = names.NewUserTag("jess@dummy")
    58  	s.otherState = factory.NewFactory(s.State).MakeModel(c, &factory.ModelParams{
    59  		Name:  "dummytoo",
    60  		Owner: s.otherEnvOwner,
    61  		ConfigAttrs: testing.Attrs{
    62  			"controller": false,
    63  		},
    64  	})
    65  	s.AddCleanup(func(c *gc.C) { s.otherState.Close() })
    66  	s.otherModelUUID = s.otherState.ModelUUID()
    67  }
    68  
    69  func (s *destroyControllerSuite) TestDestroyControllerKillErrsOnHostedEnvsWithBlocks(c *gc.C) {
    70  	s.BlockDestroyModel(c, "TestBlockDestroyModel")
    71  	s.BlockRemoveObject(c, "TestBlockRemoveObject")
    72  	s.otherState.SwitchBlockOn(state.DestroyBlock, "TestBlockDestroyModel")
    73  	s.otherState.SwitchBlockOn(state.ChangeBlock, "TestChangeBlock")
    74  
    75  	err := s.controller.DestroyController(params.DestroyControllerArgs{
    76  		DestroyModels: true,
    77  	})
    78  	c.Assert(err, gc.ErrorMatches, "found blocks in controller models")
    79  
    80  	env, err := s.State.Model()
    81  	c.Assert(err, jc.ErrorIsNil)
    82  	c.Assert(env.Life(), gc.Equals, state.Alive)
    83  }
    84  
    85  func (s *destroyControllerSuite) TestDestroyControllerReturnsBlockedEnvironmentsErr(c *gc.C) {
    86  	s.BlockDestroyModel(c, "TestBlockDestroyModel")
    87  	s.BlockRemoveObject(c, "TestBlockRemoveObject")
    88  	s.otherState.SwitchBlockOn(state.DestroyBlock, "TestBlockDestroyModel")
    89  	s.otherState.SwitchBlockOn(state.ChangeBlock, "TestChangeBlock")
    90  
    91  	err := s.controller.DestroyController(params.DestroyControllerArgs{
    92  		DestroyModels: true,
    93  	})
    94  	c.Assert(params.IsCodeOperationBlocked(err), jc.IsTrue)
    95  
    96  	numBlocks, err := s.State.AllBlocksForController()
    97  	c.Assert(err, jc.ErrorIsNil)
    98  	c.Assert(len(numBlocks), gc.Equals, 4)
    99  
   100  	_, err = s.otherState.Model()
   101  	c.Assert(err, jc.ErrorIsNil)
   102  }
   103  
   104  func (s *destroyControllerSuite) TestDestroyControllerKillsHostedEnvs(c *gc.C) {
   105  	err := s.controller.DestroyController(params.DestroyControllerArgs{
   106  		DestroyModels: true,
   107  	})
   108  	c.Assert(err, jc.ErrorIsNil)
   109  
   110  	env, err := s.State.Model()
   111  	c.Assert(err, jc.ErrorIsNil)
   112  	c.Assert(env.Life(), gc.Equals, state.Dying)
   113  }
   114  
   115  func (s *destroyControllerSuite) TestDestroyControllerLeavesBlocksIfNotKillAll(c *gc.C) {
   116  	s.BlockDestroyModel(c, "TestBlockDestroyModel")
   117  	s.BlockRemoveObject(c, "TestBlockRemoveObject")
   118  	s.otherState.SwitchBlockOn(state.DestroyBlock, "TestBlockDestroyModel")
   119  	s.otherState.SwitchBlockOn(state.ChangeBlock, "TestChangeBlock")
   120  
   121  	err := s.controller.DestroyController(params.DestroyControllerArgs{})
   122  	c.Assert(err, gc.ErrorMatches, "found blocks in controller models")
   123  
   124  	numBlocks, err := s.State.AllBlocksForController()
   125  	c.Assert(err, jc.ErrorIsNil)
   126  	c.Assert(len(numBlocks), gc.Equals, 4)
   127  }
   128  
   129  func (s *destroyControllerSuite) TestDestroyControllerNoHostedEnvs(c *gc.C) {
   130  	err := common.DestroyModel(s.State, s.otherState.ModelTag())
   131  	c.Assert(err, jc.ErrorIsNil)
   132  
   133  	err = s.controller.DestroyController(params.DestroyControllerArgs{})
   134  	c.Assert(err, jc.ErrorIsNil)
   135  
   136  	env, err := s.State.Model()
   137  	c.Assert(err, jc.ErrorIsNil)
   138  	c.Assert(env.Life(), gc.Equals, state.Dying)
   139  }
   140  
   141  func (s *destroyControllerSuite) TestDestroyControllerErrsOnNoHostedEnvsWithBlock(c *gc.C) {
   142  	err := common.DestroyModel(s.State, s.otherState.ModelTag())
   143  	c.Assert(err, jc.ErrorIsNil)
   144  
   145  	s.BlockDestroyModel(c, "TestBlockDestroyModel")
   146  	s.BlockRemoveObject(c, "TestBlockRemoveObject")
   147  
   148  	err = s.controller.DestroyController(params.DestroyControllerArgs{})
   149  	c.Assert(err, gc.ErrorMatches, "found blocks in controller models")
   150  	env, err := s.State.Model()
   151  	c.Assert(err, jc.ErrorIsNil)
   152  	c.Assert(env.Life(), gc.Equals, state.Alive)
   153  }
   154  
   155  func (s *destroyControllerSuite) TestDestroyControllerNoHostedEnvsWithBlockFail(c *gc.C) {
   156  	err := common.DestroyModel(s.State, s.otherState.ModelTag())
   157  	c.Assert(err, jc.ErrorIsNil)
   158  
   159  	s.BlockDestroyModel(c, "TestBlockDestroyModel")
   160  	s.BlockRemoveObject(c, "TestBlockRemoveObject")
   161  
   162  	err = s.controller.DestroyController(params.DestroyControllerArgs{})
   163  	c.Assert(params.IsCodeOperationBlocked(err), jc.IsTrue)
   164  
   165  	numBlocks, err := s.State.AllBlocksForController()
   166  	c.Assert(err, jc.ErrorIsNil)
   167  	c.Assert(len(numBlocks), gc.Equals, 2)
   168  }