github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/state/block_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package state_test 5 6 import ( 7 "strings" 8 9 jc "github.com/juju/testing/checkers" 10 "github.com/juju/utils" 11 gc "gopkg.in/check.v1" 12 "gopkg.in/juju/names.v2" 13 14 "github.com/juju/juju/state" 15 "github.com/juju/juju/storage" 16 "github.com/juju/juju/testing" 17 ) 18 19 type blockSuite struct { 20 ConnSuite 21 } 22 23 var _ = gc.Suite(&blockSuite{}) 24 25 func (s *blockSuite) SetUpTest(c *gc.C) { 26 s.ConnSuite.SetUpTest(c) 27 } 28 29 func assertNoEnvBlock(c *gc.C, st *state.State) { 30 all, err := st.AllBlocks() 31 c.Assert(err, jc.ErrorIsNil) 32 c.Assert(all, gc.HasLen, 0) 33 } 34 35 func (s *blockSuite) TestNoInitialBlocks(c *gc.C) { 36 assertNoEnvBlock(c, s.State) 37 } 38 39 func (s *blockSuite) assertNoTypedBlock(c *gc.C, t state.BlockType) { 40 one, found, err := s.State.GetBlockForType(t) 41 c.Assert(err, jc.ErrorIsNil) 42 c.Assert(found, jc.IsFalse) 43 c.Assert(one, gc.IsNil) 44 } 45 46 func (s *blockSuite) assertModelHasBlock(c *gc.C, st *state.State, t state.BlockType, msg string) { 47 block, found, err := st.GetBlockForType(t) 48 c.Assert(err, jc.ErrorIsNil) 49 c.Assert(found, jc.IsTrue) 50 c.Assert(block, gc.NotNil) 51 c.Assert(block.Type(), gc.Equals, t) 52 tag, err := block.Tag() 53 c.Assert(err, jc.ErrorIsNil) 54 c.Assert(tag, gc.Equals, st.ModelTag()) 55 c.Assert(block.Message(), gc.Equals, msg) 56 } 57 58 func (s *blockSuite) switchOnBlock(c *gc.C, t state.BlockType, message ...string) { 59 m := strings.Join(message, " ") 60 err := s.State.SwitchBlockOn(state.DestroyBlock, m) 61 c.Assert(err, jc.ErrorIsNil) 62 } 63 64 func (s *blockSuite) TestSwitchOnBlock(c *gc.C) { 65 s.switchOnBlock(c, state.DestroyBlock, "some message") 66 s.assertModelHasBlock(c, s.State, state.DestroyBlock, "some message") 67 } 68 69 func (s *blockSuite) TestSwitchOnBlockAlreadyOn(c *gc.C) { 70 s.switchOnBlock(c, state.DestroyBlock, "first message") 71 s.switchOnBlock(c, state.DestroyBlock, "second message") 72 s.assertModelHasBlock(c, s.State, state.DestroyBlock, "second message") 73 } 74 75 func (s *blockSuite) switchOffBlock(c *gc.C, t state.BlockType) { 76 err := s.State.SwitchBlockOff(t) 77 c.Assert(err, jc.ErrorIsNil) 78 } 79 80 func (s *blockSuite) TestSwitchOffBlockNoBlock(c *gc.C) { 81 s.switchOffBlock(c, state.DestroyBlock) 82 assertNoEnvBlock(c, s.State) 83 s.assertNoTypedBlock(c, state.DestroyBlock) 84 } 85 86 func (s *blockSuite) TestSwitchOffBlock(c *gc.C) { 87 s.switchOnBlock(c, state.DestroyBlock) 88 s.switchOffBlock(c, state.DestroyBlock) 89 assertNoEnvBlock(c, s.State) 90 s.assertNoTypedBlock(c, state.DestroyBlock) 91 } 92 93 func (s *blockSuite) TestNonsenseBlocked(c *gc.C) { 94 bType := state.BlockType(42) 95 // This could be useful for entity blocks... 96 s.switchOnBlock(c, bType) 97 s.switchOffBlock(c, bType) 98 // but for multiwatcher, it should panic. 99 c.Assert(func() { bType.ToParams() }, gc.PanicMatches, ".*unknown block type.*") 100 } 101 102 func (s *blockSuite) TestMultiEnvBlocked(c *gc.C) { 103 // create another env 104 _, st2 := s.createTestModel(c) 105 defer st2.Close() 106 107 // switch one block type on 108 t := state.ChangeBlock 109 msg := "another env tst" 110 err := st2.SwitchBlockOn(t, msg) 111 c.Assert(err, jc.ErrorIsNil) 112 s.assertModelHasBlock(c, st2, t, msg) 113 114 //check correct env has it 115 assertNoEnvBlock(c, s.State) 116 s.assertNoTypedBlock(c, t) 117 } 118 119 func (s *blockSuite) TestAllBlocksForController(c *gc.C) { 120 _, st2 := s.createTestModel(c) 121 defer st2.Close() 122 123 err := st2.SwitchBlockOn(state.ChangeBlock, "block test") 124 c.Assert(err, jc.ErrorIsNil) 125 err = s.State.SwitchBlockOn(state.ChangeBlock, "block test") 126 c.Assert(err, jc.ErrorIsNil) 127 128 blocks, err := s.State.AllBlocksForController() 129 c.Assert(err, jc.ErrorIsNil) 130 c.Assert(len(blocks), gc.Equals, 2) 131 } 132 133 func (s *blockSuite) TestRemoveAllBlocksForController(c *gc.C) { 134 _, st2 := s.createTestModel(c) 135 defer st2.Close() 136 137 err := st2.SwitchBlockOn(state.ChangeBlock, "block test") 138 c.Assert(err, jc.ErrorIsNil) 139 err = s.State.SwitchBlockOn(state.ChangeBlock, "block test") 140 c.Assert(err, jc.ErrorIsNil) 141 142 err = s.State.RemoveAllBlocksForController() 143 c.Assert(err, jc.ErrorIsNil) 144 145 blocks, err := s.State.AllBlocksForController() 146 c.Assert(err, jc.ErrorIsNil) 147 c.Assert(len(blocks), gc.Equals, 0) 148 } 149 150 func (s *blockSuite) TestRemoveAllBlocksForControllerNoBlocks(c *gc.C) { 151 _, st2 := s.createTestModel(c) 152 defer st2.Close() 153 154 err := st2.RemoveAllBlocksForController() 155 c.Assert(err, jc.ErrorIsNil) 156 157 blocks, err := st2.AllBlocksForController() 158 c.Assert(err, jc.ErrorIsNil) 159 c.Assert(len(blocks), gc.Equals, 0) 160 } 161 162 func (s *blockSuite) TestModelUUID(c *gc.C) { 163 st := s.Factory.MakeModel(c, nil) 164 defer st.Close() 165 err := st.SwitchBlockOn(state.ChangeBlock, "blocktest") 166 c.Assert(err, jc.ErrorIsNil) 167 168 blocks, err := st.AllBlocks() 169 c.Assert(err, jc.ErrorIsNil) 170 c.Assert(len(blocks), gc.Equals, 1) 171 c.Assert(blocks[0].ModelUUID(), gc.Equals, st.ModelUUID()) 172 } 173 174 func (s *blockSuite) createTestModel(c *gc.C) (*state.Model, *state.State) { 175 uuid, err := utils.NewUUID() 176 c.Assert(err, jc.ErrorIsNil) 177 cfg := testing.CustomModelConfig(c, testing.Attrs{ 178 "name": "testing", 179 "uuid": uuid.String(), 180 }) 181 owner := names.NewUserTag("test@remote") 182 env, st, err := s.State.NewModel(state.ModelArgs{ 183 CloudName: "dummy", CloudRegion: "dummy-region", Config: cfg, Owner: owner, 184 StorageProviderRegistry: storage.StaticProviderRegistry{}, 185 }) 186 c.Assert(err, jc.ErrorIsNil) 187 return env, st 188 } 189 190 func (s *blockSuite) TestConcurrentBlocked(c *gc.C) { 191 switchBlockOn := func() { 192 msg := "" 193 t := state.DestroyBlock 194 err := s.State.SwitchBlockOn(t, msg) 195 c.Assert(err, jc.ErrorIsNil) 196 s.assertModelHasBlock(c, s.State, t, msg) 197 } 198 defer state.SetBeforeHooks(c, s.State, switchBlockOn).Check() 199 msg := "concurrency tst" 200 t := state.RemoveBlock 201 err := s.State.SwitchBlockOn(t, msg) 202 c.Assert(err, jc.ErrorIsNil) 203 s.assertModelHasBlock(c, s.State, t, msg) 204 }