forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application_leader_test.go
235 lines (197 loc) · 6.03 KB
/
application_leader_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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state_test
import (
"github.com/juju/errors"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/juju/state"
"github.com/juju/juju/state/testing"
"github.com/juju/juju/testing/factory"
)
type ApplicationLeaderSuite struct {
ConnSuite
application *state.Application
}
var _ = gc.Suite(&ApplicationLeaderSuite{})
func (s *ApplicationLeaderSuite) SetUpTest(c *gc.C) {
s.ConnSuite.SetUpTest(c)
s.application = s.Factory.MakeApplication(c, nil)
// Before we get into the tests, ensure that all the creation events have flowed through the system.
s.WaitForModelWatchersIdle(c, s.Model.UUID())
}
func (s *ApplicationLeaderSuite) TestReadEmpty(c *gc.C) {
s.checkSettings(c, map[string]string{})
}
func (s *ApplicationLeaderSuite) TestWrite(c *gc.C) {
s.writeSettings(c, map[string]string{
"foo": "bar",
"baz.qux": "ping",
"pong": "",
"$unset": "foo",
})
s.checkSettings(c, map[string]string{
"foo": "bar",
"baz.qux": "ping",
// pong: "" value is ignored
"$unset": "foo",
})
}
func (s *ApplicationLeaderSuite) TestOverwrite(c *gc.C) {
s.writeSettings(c, map[string]string{
"one": "foo",
"2.0": "bar",
"$three": "baz",
"fo-ur": "qux",
})
s.writeSettings(c, map[string]string{
"one": "",
"2.0": "ping",
"$three": "pong",
"$unset": "2.0",
})
s.checkSettings(c, map[string]string{
// one: "" value is cleared
"2.0": "ping",
"$three": "pong",
"fo-ur": "qux",
"$unset": "2.0",
})
}
func (s *ApplicationLeaderSuite) TestTxnRevnoChange(c *gc.C) {
defer state.SetBeforeHooks(c, s.State, func() {
s.writeSettings(c, map[string]string{
"other": "values",
"slipped": "in",
"before": "we",
"managed": "to",
})
}).Check()
s.writeSettings(c, map[string]string{
"but": "we",
"overwrite": "those",
"before": "",
})
s.checkSettings(c, map[string]string{
"other": "values",
"slipped": "in",
"but": "we",
"managed": "to",
"overwrite": "those",
})
}
func (s *ApplicationLeaderSuite) TestTokenError(c *gc.C) {
err := s.application.UpdateLeaderSettings(&failToken{}, map[string]string{"blah": "blah"})
c.Check(err, gc.ErrorMatches, `application "mysql": checking leadership continuity: something bad happened`)
}
func (s *ApplicationLeaderSuite) TestReadWriteDying(c *gc.C) {
s.preventRemove(c)
s.destroyApplication(c)
s.writeSettings(c, map[string]string{
"this": "should",
"still": "work",
})
s.checkSettings(c, map[string]string{
"this": "should",
"still": "work",
})
}
func (s *ApplicationLeaderSuite) TestReadRemoved(c *gc.C) {
s.destroyApplication(c)
actual, err := s.application.LeaderSettings()
c.Check(err, gc.ErrorMatches, `application "mysql" not found`)
c.Check(err, jc.Satisfies, errors.IsNotFound)
c.Check(actual, gc.IsNil)
}
func (s *ApplicationLeaderSuite) TestWriteRemoved(c *gc.C) {
s.destroyApplication(c)
err := s.application.UpdateLeaderSettings(&fakeToken{}, map[string]string{
"should": "fail",
})
c.Check(err, gc.ErrorMatches, `application "mysql" not found`)
c.Check(err, jc.Satisfies, errors.IsNotFound)
}
func (s *ApplicationLeaderSuite) TestWatchInitialEvent(c *gc.C) {
w := s.application.WatchLeaderSettings()
defer testing.AssertStop(c, w)
wc := testing.NewNotifyWatcherC(c, w)
wc.AssertOneChange()
}
func (s *ApplicationLeaderSuite) TestWatchDetectChange(c *gc.C) {
w := s.application.WatchLeaderSettings()
defer testing.AssertStop(c, w)
wc := testing.NewNotifyWatcherC(c, w)
wc.AssertOneChange()
err := s.application.UpdateLeaderSettings(&fakeToken{}, map[string]string{
"something": "changed",
})
c.Assert(err, jc.ErrorIsNil)
wc.AssertOneChange()
}
func (s *ApplicationLeaderSuite) TestWatchIgnoreNullChange(c *gc.C) {
w := s.application.WatchLeaderSettings()
defer testing.AssertStop(c, w)
wc := testing.NewNotifyWatcherC(c, w)
wc.AssertOneChange()
err := s.application.UpdateLeaderSettings(&fakeToken{}, map[string]string{
"something": "changed",
})
c.Assert(err, jc.ErrorIsNil)
wc.AssertOneChange()
err = s.application.UpdateLeaderSettings(&fakeToken{}, map[string]string{
"something": "changed",
})
c.Assert(err, jc.ErrorIsNil)
wc.AssertNoChange()
}
func (s *ApplicationLeaderSuite) TestWatchCoalesceChanges(c *gc.C) {
w := s.application.WatchLeaderSettings()
defer testing.AssertStop(c, w)
wc := testing.NewNotifyWatcherC(c, w)
wc.AssertOneChange()
err := s.application.UpdateLeaderSettings(&fakeToken{}, map[string]string{
"something": "changed",
})
c.Assert(err, jc.ErrorIsNil)
// TODO(quiescence): these two changes should be one event.
wc.AssertOneChange()
err = s.application.UpdateLeaderSettings(&fakeToken{}, map[string]string{
"very": "excitingly",
})
c.Assert(err, jc.ErrorIsNil)
wc.AssertOneChange()
}
func (s *ApplicationLeaderSuite) writeSettings(c *gc.C, update map[string]string) {
err := s.application.UpdateLeaderSettings(&fakeToken{}, update)
c.Check(err, jc.ErrorIsNil)
}
func (s *ApplicationLeaderSuite) checkSettings(c *gc.C, expect map[string]string) {
actual, err := s.application.LeaderSettings()
c.Check(err, jc.ErrorIsNil)
c.Check(actual, gc.DeepEquals, expect)
}
func (s *ApplicationLeaderSuite) preventRemove(c *gc.C) {
s.Factory.MakeUnit(c, &factory.UnitParams{Application: s.application})
}
func (s *ApplicationLeaderSuite) destroyApplication(c *gc.C) {
killApplication, err := s.State.Application(s.application.Name())
c.Assert(err, jc.ErrorIsNil)
err = killApplication.Destroy()
c.Assert(err, jc.ErrorIsNil)
}
// fakeToken implements leadership.Token.
type fakeToken struct {
err error
}
// Check is part of the leadership.Token interface. It returns its
// contained error (which defaults to nil), and never checks or writes
// the userdata.
func (t *fakeToken) Check() error {
return t.err
}
// failToken implements leadership.Token.
type failToken struct{}
// Check is part of the leadership.Token interface. It always returns an error.
func (*failToken) Check() error {
return errors.New("something bad happened")
}