1
// Copyright 2012-2015 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
4
package controller_test
7
jc "github.com/juju/testing/checkers"
9
"gopkg.in/juju/names.v2"
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"
22
// NOTE: the testing of the general environment destruction code
23
// is found in apiserver/common/environdestroy_test.go.
25
// The tests here are around the validation and behaviour of
26
// the flags passed in to the destroy controller call.
28
type destroyControllerSuite struct {
29
jujutesting.JujuConnSuite
30
commontesting.BlockHelper
32
controller *controller.ControllerAPI
34
otherState *state.State
35
otherEnvOwner names.UserTag
37
modelManagerBackend common.ModelManagerBackend
40
var _ = gc.Suite(&destroyControllerSuite{})
42
func (s *destroyControllerSuite) SetUpTest(c *gc.C) {
43
s.JujuConnSuite.SetUpTest(c)
45
s.BlockHelper = commontesting.NewBlockHelper(s.APIState)
46
s.AddCleanup(func(*gc.C) { s.BlockHelper.Close() })
48
resources := common.NewResources()
49
s.AddCleanup(func(_ *gc.C) { resources.StopAll() })
51
authoriser := apiservertesting.FakeAuthorizer{
52
Tag: s.AdminUserTag(c),
54
controller, err := controller.NewControllerAPI(s.State, resources, authoriser)
55
c.Assert(err, jc.ErrorIsNil)
56
s.controller = controller
58
s.otherEnvOwner = names.NewUserTag("jess@dummy")
59
s.otherState = factory.NewFactory(s.State).MakeModel(c, &factory.ModelParams{
61
Owner: s.otherEnvOwner,
62
ConfigAttrs: testing.Attrs{
66
s.AddCleanup(func(c *gc.C) { s.otherState.Close() })
67
s.otherModelUUID = s.otherState.ModelUUID()
68
s.modelManagerBackend = common.NewModelManagerBackend(s.State)
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")
77
err := s.controller.DestroyController(params.DestroyControllerArgs{
80
c.Assert(err, gc.ErrorMatches, "found blocks in controller models")
82
env, err := s.State.Model()
83
c.Assert(err, jc.ErrorIsNil)
84
c.Assert(env.Life(), gc.Equals, state.Alive)
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")
93
err := s.controller.DestroyController(params.DestroyControllerArgs{
96
c.Assert(params.IsCodeOperationBlocked(err), jc.IsTrue)
98
numBlocks, err := s.State.AllBlocksForController()
99
c.Assert(err, jc.ErrorIsNil)
100
c.Assert(len(numBlocks), gc.Equals, 4)
102
_, err = s.otherState.Model()
103
c.Assert(err, jc.ErrorIsNil)
106
func (s *destroyControllerSuite) TestDestroyControllerKillsHostedEnvs(c *gc.C) {
107
err := s.controller.DestroyController(params.DestroyControllerArgs{
110
c.Assert(err, jc.ErrorIsNil)
112
env, err := s.State.Model()
113
c.Assert(err, jc.ErrorIsNil)
114
c.Assert(env.Life(), gc.Equals, state.Dying)
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")
123
err := s.controller.DestroyController(params.DestroyControllerArgs{})
124
c.Assert(err, gc.ErrorMatches, "found blocks in controller models")
126
numBlocks, err := s.State.AllBlocksForController()
127
c.Assert(err, jc.ErrorIsNil)
128
c.Assert(len(numBlocks), gc.Equals, 4)
131
func (s *destroyControllerSuite) TestDestroyControllerNoHostedEnvs(c *gc.C) {
132
err := common.DestroyModel(s.modelManagerBackend, s.otherState.ModelTag())
133
c.Assert(err, jc.ErrorIsNil)
135
err = s.controller.DestroyController(params.DestroyControllerArgs{})
136
c.Assert(err, jc.ErrorIsNil)
138
env, err := s.State.Model()
139
c.Assert(err, jc.ErrorIsNil)
140
c.Assert(env.Life(), gc.Equals, state.Dying)
143
func (s *destroyControllerSuite) TestDestroyControllerErrsOnNoHostedEnvsWithBlock(c *gc.C) {
144
err := common.DestroyModel(s.modelManagerBackend, s.otherState.ModelTag())
145
c.Assert(err, jc.ErrorIsNil)
147
s.BlockDestroyModel(c, "TestBlockDestroyModel")
148
s.BlockRemoveObject(c, "TestBlockRemoveObject")
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)
157
func (s *destroyControllerSuite) TestDestroyControllerNoHostedEnvsWithBlockFail(c *gc.C) {
158
err := common.DestroyModel(s.modelManagerBackend, s.otherState.ModelTag())
159
c.Assert(err, jc.ErrorIsNil)
161
s.BlockDestroyModel(c, "TestBlockDestroyModel")
162
s.BlockRemoveObject(c, "TestBlockRemoveObject")
164
err = s.controller.DestroyController(params.DestroyControllerArgs{})
165
c.Assert(params.IsCodeOperationBlocked(err), jc.IsTrue)
167
numBlocks, err := s.State.AllBlocksForController()
168
c.Assert(err, jc.ErrorIsNil)
169
c.Assert(len(numBlocks), gc.Equals, 2)