github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/apiserver/common/block_test.go (about) 1 // Copyright 2012-2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package common_test 5 6 import ( 7 jc "github.com/juju/testing/checkers" 8 gc "gopkg.in/check.v1" 9 10 "github.com/juju/errors" 11 12 "github.com/juju/juju/apiserver/common" 13 "github.com/juju/juju/environs/config" 14 "github.com/juju/juju/testing" 15 ) 16 17 type blocksSuite struct { 18 testing.FakeJujuHomeSuite 19 destroy, remove, change bool 20 cfg *config.Config 21 } 22 23 var _ = gc.Suite(&blocksSuite{}) 24 25 func (s *blocksSuite) TearDownTest(c *gc.C) { 26 s.destroy, s.remove, s.change = false, false, false 27 } 28 29 func (s *blocksSuite) SetUpTest(c *gc.C) { 30 s.FakeJujuHomeSuite.SetUpTest(c) 31 cfg, err := config.New( 32 config.UseDefaults, 33 map[string]interface{}{ 34 "name": "block-env", 35 "type": "any-type", 36 }, 37 ) 38 c.Assert(err, jc.ErrorIsNil) 39 s.cfg = cfg 40 } 41 42 func (s *blocksSuite) TestBlockOperationErrorDestroy(c *gc.C) { 43 // prevent destroy-environment 44 s.blockDestroys(c) 45 s.assertDestroyOperationBlocked(c, true) 46 47 // prevent remove-object 48 s.blockRemoves(c) 49 s.assertDestroyOperationBlocked(c, true) 50 51 // prevent all-changes 52 s.blockAllChanges(c) 53 s.assertDestroyOperationBlocked(c, true) 54 } 55 56 func (s *blocksSuite) TestBlockOperationErrorRemove(c *gc.C) { 57 // prevent destroy-environment 58 s.blockDestroys(c) 59 s.assertRemoveOperationBlocked(c, false) 60 61 // prevent remove-object 62 s.blockRemoves(c) 63 s.assertRemoveOperationBlocked(c, true) 64 65 // prevent all-changes 66 s.blockAllChanges(c) 67 s.assertRemoveOperationBlocked(c, true) 68 } 69 70 func (s *blocksSuite) TestBlockOperationErrorChange(c *gc.C) { 71 // prevent destroy-environment 72 s.blockDestroys(c) 73 s.assertChangeOperationBlocked(c, false) 74 75 // prevent remove-object 76 s.blockRemoves(c) 77 s.assertChangeOperationBlocked(c, false) 78 79 // prevent all-changes 80 s.blockAllChanges(c) 81 s.assertChangeOperationBlocked(c, true) 82 } 83 84 func (s *blocksSuite) blockDestroys(c *gc.C) { 85 s.destroy, s.remove, s.change = true, false, false 86 } 87 88 func (s *blocksSuite) blockRemoves(c *gc.C) { 89 s.remove, s.destroy, s.change = true, false, false 90 } 91 92 func (s *blocksSuite) blockAllChanges(c *gc.C) { 93 s.change, s.destroy, s.remove = true, false, false 94 } 95 96 func (s *blocksSuite) assertDestroyOperationBlocked(c *gc.C, value bool) { 97 s.assertOperationBlocked(c, common.DestroyOperation, value) 98 } 99 100 func (s *blocksSuite) assertRemoveOperationBlocked(c *gc.C, value bool) { 101 s.assertOperationBlocked(c, common.RemoveOperation, value) 102 } 103 104 func (s *blocksSuite) assertChangeOperationBlocked(c *gc.C, value bool) { 105 s.assertOperationBlocked(c, common.ChangeOperation, value) 106 } 107 108 func (s *blocksSuite) assertOperationBlocked(c *gc.C, operation common.Operation, value bool) { 109 c.Assert(common.IsOperationBlocked(operation, s.getCurrentConfig(c)), gc.Equals, value) 110 } 111 112 func (s *blocksSuite) getCurrentConfig(c *gc.C) *config.Config { 113 cfg, err := s.cfg.Apply(map[string]interface{}{ 114 "block-destroy-environment": s.destroy, 115 "block-remove-object": s.remove, 116 "block-all-changes": s.change, 117 }) 118 c.Assert(err, jc.ErrorIsNil) 119 return cfg 120 } 121 122 type blockCheckerSuite struct { 123 blocksSuite 124 getter *mockGetter 125 blockchecker *common.BlockChecker 126 } 127 128 var _ = gc.Suite(&blockCheckerSuite{}) 129 130 func (s *blockCheckerSuite) SetUpTest(c *gc.C) { 131 s.blocksSuite.SetUpTest(c) 132 s.getter = &mockGetter{ 133 suite: s, 134 c: c, 135 } 136 s.blockchecker = common.NewBlockChecker(s.getter) 137 } 138 139 type mockGetter struct { 140 suite *blockCheckerSuite 141 c *gc.C 142 } 143 144 func (mock *mockGetter) EnvironConfig() (*config.Config, error) { 145 return mock.suite.getCurrentConfig(mock.c), nil 146 } 147 148 func (s *blockCheckerSuite) TestDestroyBlockChecker(c *gc.C) { 149 s.blockDestroys(c) 150 s.assertDestroyBlocked(c) 151 152 s.blockRemoves(c) 153 s.assertDestroyBlocked(c) 154 155 s.blockAllChanges(c) 156 s.assertDestroyBlocked(c) 157 } 158 159 func (s *blockCheckerSuite) TestRemoveBlockChecker(c *gc.C) { 160 s.blockDestroys(c) 161 s.assertRemoveBlocked(c, false) 162 163 s.blockRemoves(c) 164 s.assertRemoveBlocked(c, true) 165 166 s.blockAllChanges(c) 167 s.assertRemoveBlocked(c, true) 168 } 169 170 func (s *blockCheckerSuite) TestChangeBlockChecker(c *gc.C) { 171 s.blockDestroys(c) 172 s.assertChangeBlocked(c, false) 173 174 s.blockRemoves(c) 175 s.assertChangeBlocked(c, false) 176 177 s.blockAllChanges(c) 178 s.assertChangeBlocked(c, true) 179 } 180 181 func (s *blockCheckerSuite) assertDestroyBlocked(c *gc.C) { 182 c.Assert(errors.Cause(s.blockchecker.DestroyAllowed()), gc.Equals, common.ErrOperationBlocked) 183 } 184 185 func (s *blockCheckerSuite) assertRemoveBlocked(c *gc.C, blocked bool) { 186 if blocked { 187 c.Assert(errors.Cause(s.blockchecker.RemoveAllowed()), gc.Equals, common.ErrOperationBlocked) 188 } else { 189 c.Assert(errors.Cause(s.blockchecker.RemoveAllowed()), jc.ErrorIsNil) 190 } 191 } 192 193 func (s *blockCheckerSuite) assertChangeBlocked(c *gc.C, blocked bool) { 194 if blocked { 195 c.Assert(errors.Cause(s.blockchecker.ChangeAllowed()), gc.Equals, common.ErrOperationBlocked) 196 } else { 197 c.Assert(errors.Cause(s.blockchecker.ChangeAllowed()), jc.ErrorIsNil) 198 } 199 }