-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_test.go
161 lines (133 loc) · 4.21 KB
/
block_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state_test
import (
"fmt"
"github.com/juju/errors"
"github.com/juju/names"
jc "github.com/juju/testing/checkers"
"github.com/juju/utils"
gc "gopkg.in/check.v1"
"github.com/juju/juju/state"
"github.com/juju/juju/testing"
)
type blockSuite struct {
ConnSuite
}
var _ = gc.Suite(&blockSuite{})
func (s *blockSuite) SetUpTest(c *gc.C) {
s.ConnSuite.SetUpTest(c)
}
func assertNoEnvBlock(c *gc.C, st *state.State) {
all, err := st.AllBlocks()
c.Assert(err, jc.ErrorIsNil)
c.Assert(all, gc.HasLen, 0)
}
func (s *blockSuite) assertNoTypedBlock(c *gc.C, t state.BlockType) {
one, found, err := s.State.GetBlockForType(t)
c.Assert(err, jc.ErrorIsNil)
c.Assert(found, jc.IsFalse)
c.Assert(one, gc.IsNil)
}
func assertEnvHasBlock(c *gc.C, st *state.State, t state.BlockType, msg string) {
dBlock, found, err := st.GetBlockForType(t)
c.Assert(err, jc.ErrorIsNil)
c.Assert(found, jc.IsTrue)
c.Assert(dBlock, gc.NotNil)
c.Assert(dBlock.Type(), gc.DeepEquals, t)
tag, err := dBlock.Tag()
c.Assert(err, jc.ErrorIsNil)
c.Assert(tag, gc.DeepEquals, st.EnvironTag())
c.Assert(dBlock.Message(), gc.DeepEquals, msg)
}
func (s *blockSuite) assertSwitchedOn(c *gc.C, t state.BlockType) string {
msg := ""
err := s.State.SwitchBlockOn(t, msg)
c.Assert(err, jc.ErrorIsNil)
assertEnvHasBlock(c, s.State, t, msg)
return msg
}
func (s *blockSuite) assertSwitchedOff(c *gc.C, t state.BlockType) {
err := s.State.SwitchBlockOff(t)
c.Assert(err, jc.ErrorIsNil)
assertNoEnvBlock(c, s.State)
s.assertNoTypedBlock(c, t)
}
func (s *blockSuite) assertBlocked(c *gc.C, t state.BlockType) {
msg := s.assertSwitchedOn(c, t)
expectedErr := fmt.Sprintf(".*block %v is already ON.*", t.String())
// cannot duplicate
err := s.State.SwitchBlockOn(t, msg)
c.Assert(errors.Cause(err), gc.ErrorMatches, expectedErr)
// cannot update
err = s.State.SwitchBlockOn(t, "Test block update")
c.Assert(errors.Cause(err), gc.ErrorMatches, expectedErr)
s.assertSwitchedOff(c, t)
err = s.State.SwitchBlockOff(t)
expectedErr = fmt.Sprintf(".*block %v is already OFF.*", t.String())
c.Assert(errors.Cause(err), gc.ErrorMatches, expectedErr)
}
func (s *blockSuite) TestNewEnvironmentNotBlocked(c *gc.C) {
assertNoEnvBlock(c, s.State)
s.assertNoTypedBlock(c, state.DestroyBlock)
s.assertNoTypedBlock(c, state.RemoveBlock)
s.assertNoTypedBlock(c, state.ChangeBlock)
}
func (s *blockSuite) TestDestroyBlocked(c *gc.C) {
s.assertBlocked(c, state.DestroyBlock)
}
func (s *blockSuite) TestRemoveBlocked(c *gc.C) {
s.assertBlocked(c, state.RemoveBlock)
}
func (s *blockSuite) TestChangeBlocked(c *gc.C) {
s.assertBlocked(c, state.ChangeBlock)
}
func (s *blockSuite) TestNonsenseBlocked(c *gc.C) {
bType := state.BlockType(42)
// This could be useful for entity blocks...
s.assertSwitchedOn(c, bType)
s.assertSwitchedOff(c, bType)
// but for multiwatcher, it should panic.
c.Assert(func() { bType.ToParams() }, gc.PanicMatches, ".*unknown block type.*")
}
func (s *blockSuite) TestMultiEnvBlocked(c *gc.C) {
// create another env
_, st2 := s.createTestEnv(c)
defer st2.Close()
// switch one block type on
t := state.ChangeBlock
msg := "another env tst"
err := st2.SwitchBlockOn(t, msg)
c.Assert(err, jc.ErrorIsNil)
assertEnvHasBlock(c, st2, t, msg)
//check correct env has it
assertNoEnvBlock(c, s.State)
s.assertNoTypedBlock(c, t)
}
func (s *blockSuite) createTestEnv(c *gc.C) (*state.Environment, *state.State) {
uuid, err := utils.NewUUID()
c.Assert(err, jc.ErrorIsNil)
cfg := testing.CustomEnvironConfig(c, testing.Attrs{
"name": "testing",
"uuid": uuid.String(),
})
owner := names.NewUserTag("test@remote")
env, st, err := s.State.NewEnvironment(cfg, owner)
c.Assert(err, jc.ErrorIsNil)
return env, st
}
func (s *blockSuite) TestConcurrentBlocked(c *gc.C) {
switchBlockOn := func() {
msg := ""
t := state.DestroyBlock
err := s.State.SwitchBlockOn(t, msg)
c.Assert(err, jc.ErrorIsNil)
assertEnvHasBlock(c, s.State, t, msg)
}
defer state.SetBeforeHooks(c, s.State, switchBlockOn).Check()
msg := "concurrency tst"
t := state.RemoveBlock
err := s.State.SwitchBlockOn(t, msg)
c.Assert(err, jc.ErrorIsNil)
assertEnvHasBlock(c, s.State, t, msg)
}