-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage_test.go
343 lines (296 loc) · 12.5 KB
/
storage_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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state_test
import (
"github.com/juju/errors"
"github.com/juju/names"
jc "github.com/juju/testing/checkers"
"github.com/juju/utils/featureflag"
gc "gopkg.in/check.v1"
"gopkg.in/mgo.v2"
"github.com/juju/juju/feature"
"github.com/juju/juju/juju/osenv"
"github.com/juju/juju/state"
"github.com/juju/juju/state/testing"
"github.com/juju/juju/storage"
"github.com/juju/juju/storage/poolmanager"
"github.com/juju/juju/storage/provider"
"github.com/juju/juju/storage/provider/registry"
)
type StorageStateSuite struct {
ConnSuite
}
var _ = gc.Suite(&StorageStateSuite{})
func (s *StorageStateSuite) SetUpTest(c *gc.C) {
s.ConnSuite.SetUpTest(c)
// This suite is all about storage, so enable the feature by default.
s.PatchEnvironment(osenv.JujuFeatureFlagEnvKey, feature.Storage)
featureflag.SetFlagsFromEnvironment(osenv.JujuFeatureFlagEnvKey)
// Create a default pool for block devices.
pm := poolmanager.New(state.NewStateSettings(s.State))
_, err := pm.Create("block", provider.LoopProviderType, map[string]interface{}{})
c.Assert(err, jc.ErrorIsNil)
registry.RegisterEnvironStorageProviders("someprovider", provider.LoopProviderType)
}
func (s *StorageStateSuite) storageInstanceExists(c *gc.C, tag names.StorageTag) bool {
_, err := state.TxnRevno(
s.State,
state.StorageInstancesC,
state.DocID(s.State, tag.Id()),
)
if err != nil {
c.Assert(err, gc.Equals, mgo.ErrNotFound)
return false
}
return true
}
func (s *StorageStateSuite) setupSingleStorage(c *gc.C) (*state.Service, *state.Unit, names.StorageTag) {
ch := s.AddTestingCharm(c, "storage-block")
storage := map[string]state.StorageConstraints{
"data": makeStorageCons("block", 1024, 1),
}
service := s.AddTestingServiceWithStorage(c, "storage-block", ch, storage)
unit, err := service.AddUnit()
c.Assert(err, jc.ErrorIsNil)
storageTag := names.NewStorageTag("data/0")
return service, unit, storageTag
}
func makeStorageCons(pool string, size, count uint64) state.StorageConstraints {
return state.StorageConstraints{Pool: pool, Size: size, Count: count}
}
func (s *StorageStateSuite) TestAddServiceStorageConstraintsWithoutFeature(c *gc.C) {
// Disable the storage feature, and ensure we can deploy a service from
// a charm that defines storage, without specifying the storage constraints.
s.PatchEnvironment(osenv.JujuFeatureFlagEnvKey, "")
featureflag.SetFlagsFromEnvironment(osenv.JujuFeatureFlagEnvKey)
ch := s.AddTestingCharm(c, "storage-block2")
service, err := s.State.AddService("storage-block2", "user-test-admin@local", ch, nil, nil)
c.Assert(err, jc.ErrorIsNil)
storageConstraints, err := service.StorageConstraints()
c.Assert(err, jc.ErrorIsNil)
c.Assert(storageConstraints, gc.HasLen, 0)
}
func (s *StorageStateSuite) TestAddServiceStorageConstraints(c *gc.C) {
ch := s.AddTestingCharm(c, "storage-block2")
addService := func(storage map[string]state.StorageConstraints) (*state.Service, error) {
return s.State.AddService("storage-block2", "user-test-admin@local", ch, nil, storage)
}
assertErr := func(storage map[string]state.StorageConstraints, expect string) {
_, err := addService(storage)
c.Assert(err, gc.ErrorMatches, expect)
}
assertErr(nil, `.*no constraints specified for store.*`)
defer func() {
registry.RegisterDefaultPool("someprovider", storage.StorageKindBlock, "")
}()
storageCons := map[string]state.StorageConstraints{
"multi1to10": makeStorageCons("", 1024, 1),
}
assertErr(storageCons, `cannot add service "storage-block2": no storage pool specified and no default available .*`)
registry.RegisterDefaultPool("someprovider", storage.StorageKindBlock, "block")
storageCons["multi2up"] = makeStorageCons("", 1024, 1)
assertErr(storageCons, `cannot add service "storage-block2": charm "storage-block2" store "multi2up": 2 instances required, 1 specified`)
storageCons["multi2up"] = makeStorageCons("block", 1024, 2)
storageCons["multi1to10"] = makeStorageCons("", 1024, 11)
assertErr(storageCons, `cannot add service "storage-block2": charm "storage-block2" store "multi1to10": at most 10 instances supported, 11 specified`)
storageCons["multi1to10"] = makeStorageCons("ebs", 1024, 10)
assertErr(storageCons, `cannot add service "storage-block2": pool "ebs" not found`)
storageCons["multi1to10"] = makeStorageCons("", 1024, 10)
_, err := addService(storageCons)
c.Assert(err, jc.ErrorIsNil)
// TODO(wallyworld) - test pool name stored in data model
}
func (s *StorageStateSuite) TestProviderFallbackToType(c *gc.C) {
ch := s.AddTestingCharm(c, "storage-block")
addService := func(storage map[string]state.StorageConstraints) (*state.Service, error) {
return s.State.AddService("storage-block", "user-test-admin@local", ch, nil, storage)
}
storageCons := map[string]state.StorageConstraints{
"data": makeStorageCons("loop", 1024, 1),
}
_, err := addService(storageCons)
c.Assert(err, jc.ErrorIsNil)
}
func (s *StorageStateSuite) TestAddUnit(c *gc.C) {
registry.RegisterDefaultPool("someprovider", storage.StorageKindBlock, "block")
defer func() {
registry.RegisterDefaultPool("someprovider", storage.StorageKindBlock, "")
}()
// Each unit added to the service will create storage instances
// to satisfy the service's storage constraints.
ch := s.AddTestingCharm(c, "storage-block2")
storage := map[string]state.StorageConstraints{
"multi1to10": makeStorageCons("", 1024, 1),
"multi2up": makeStorageCons("block", 1024, 2),
}
service := s.AddTestingServiceWithStorage(c, "storage-block2", ch, storage)
for i := 0; i < 2; i++ {
u, err := service.AddUnit()
c.Assert(err, jc.ErrorIsNil)
storageAttachments, err := s.State.StorageAttachments(u.UnitTag())
c.Assert(err, jc.ErrorIsNil)
count := make(map[string]int)
for _, att := range storageAttachments {
c.Assert(att.Unit(), gc.Equals, u.UnitTag())
storageInstance, err := s.State.StorageInstance(att.StorageInstance())
c.Assert(err, jc.ErrorIsNil)
count[storageInstance.StorageName()]++
c.Assert(storageInstance.Kind(), gc.Equals, state.StorageKindBlock)
_, err = storageInstance.Info()
c.Assert(err, jc.Satisfies, errors.IsNotProvisioned)
}
c.Assert(count, gc.DeepEquals, map[string]int{
"multi1to10": 1,
"multi2up": 2,
})
// TODO(wallyworld) - test pool name stored in data model
}
}
func (s *StorageStateSuite) TestUnitEnsureDead(c *gc.C) {
_, u, storageTag := s.setupSingleStorage(c)
// destroying a unit with storage attachments is fine; this is what
// will trigger the death and removal of storage attachments.
err := u.Destroy()
c.Assert(err, jc.ErrorIsNil)
// until all storage attachments are removed, the unit cannot be
// marked as being dead.
assertUnitEnsureDeadError := func() {
err = u.EnsureDead()
c.Assert(err, gc.ErrorMatches, "unit has storage attachments")
}
assertUnitEnsureDeadError()
err = s.State.EnsureStorageAttachmentDead(storageTag, u.UnitTag())
c.Assert(err, jc.ErrorIsNil)
assertUnitEnsureDeadError()
err = s.State.DestroyStorageInstance(storageTag)
c.Assert(err, jc.ErrorIsNil)
assertUnitEnsureDeadError()
err = s.State.RemoveStorageAttachment(storageTag, u.UnitTag())
c.Assert(err, jc.ErrorIsNil)
err = u.EnsureDead()
c.Assert(err, jc.ErrorIsNil)
}
func (s *StorageStateSuite) TestRemoveStorageAttachmentsRemovesDyingInstance(c *gc.C) {
_, u, storageTag := s.setupSingleStorage(c)
// Mark the storage instance as Dying, so that it will be removed
// when the last attachment is removed.
err := s.State.DestroyStorageInstance(storageTag)
c.Assert(err, jc.ErrorIsNil)
si, err := s.State.StorageInstance(storageTag)
c.Assert(err, jc.ErrorIsNil)
c.Assert(si.Life(), gc.Equals, state.Dying)
err = s.State.EnsureStorageAttachmentDead(storageTag, u.UnitTag())
c.Assert(err, jc.ErrorIsNil)
err = s.State.RemoveStorageAttachment(storageTag, u.UnitTag())
c.Assert(err, jc.ErrorIsNil)
exists := s.storageInstanceExists(c, storageTag)
c.Assert(exists, jc.IsFalse)
}
func (s *StorageStateSuite) TestConcurrentDestroyStorageInstanceRemoveStorageAttachmentsRemovesInstance(c *gc.C) {
_, u, storageTag := s.setupSingleStorage(c)
defer state.SetBeforeHooks(c, s.State, func() {
err := s.State.EnsureStorageAttachmentDead(storageTag, u.UnitTag())
c.Assert(err, jc.ErrorIsNil)
err = s.State.RemoveStorageAttachment(storageTag, u.UnitTag())
c.Assert(err, jc.ErrorIsNil)
}).Check()
// Destroying the instance should check that there are no concurrent
// changes to the storage instance's attachments, and recompute
// operations if there are.
err := s.State.DestroyStorageInstance(storageTag)
c.Assert(err, jc.ErrorIsNil)
exists := s.storageInstanceExists(c, storageTag)
c.Assert(exists, jc.IsFalse)
}
func (s *StorageStateSuite) TestConcurrentRemoveStorageAttachment(c *gc.C) {
_, u, storageTag := s.setupSingleStorage(c)
err := s.State.DestroyStorageInstance(storageTag)
c.Assert(err, jc.ErrorIsNil)
ensureDead := func() {
err = s.State.EnsureStorageAttachmentDead(storageTag, u.UnitTag())
c.Assert(err, jc.ErrorIsNil)
}
remove := func() {
err = s.State.RemoveStorageAttachment(storageTag, u.UnitTag())
c.Assert(err, jc.ErrorIsNil)
}
defer state.SetBeforeHooks(c, s.State, ensureDead, remove).Check()
ensureDead()
remove()
exists := s.storageInstanceExists(c, storageTag)
c.Assert(exists, jc.IsFalse)
}
func (s *StorageStateSuite) TestRemoveAliveStorageAttachmentError(c *gc.C) {
_, u, storageTag := s.setupSingleStorage(c)
err := s.State.RemoveStorageAttachment(storageTag, u.UnitTag())
c.Assert(err, gc.ErrorMatches, "cannot remove storage attachment data/0:storage-block/0: storage attachment is not dead")
attachments, err := s.State.StorageAttachments(u.UnitTag())
c.Assert(err, jc.ErrorIsNil)
c.Assert(attachments, gc.HasLen, 1)
c.Assert(attachments[0].StorageInstance(), gc.Equals, storageTag)
}
func (s *StorageStateSuite) TestConcurrentDestroyInstanceRemoveStorageAttachmentsRemovesInstance(c *gc.C) {
_, u, storageTag := s.setupSingleStorage(c)
defer state.SetBeforeHooks(c, s.State, func() {
// Concurrently mark the storage instance as Dying,
// so that it will be removed when the last attachment
// is removed.
err := s.State.DestroyStorageInstance(storageTag)
c.Assert(err, jc.ErrorIsNil)
}, nil).Check()
// Removing the attachment should check that there are no concurrent
// changes to the storage instance's life, and recompute operations
// if it does.
err := s.State.EnsureStorageAttachmentDead(storageTag, u.UnitTag())
c.Assert(err, jc.ErrorIsNil)
err = s.State.RemoveStorageAttachment(storageTag, u.UnitTag())
c.Assert(err, jc.ErrorIsNil)
exists := s.storageInstanceExists(c, storageTag)
c.Assert(exists, jc.IsFalse)
}
func (s *StorageStateSuite) TestConcurrentDestroyStorageInstance(c *gc.C) {
_, _, storageTag := s.setupSingleStorage(c)
defer state.SetBeforeHooks(c, s.State, func() {
err := s.State.DestroyStorageInstance(storageTag)
c.Assert(err, jc.ErrorIsNil)
}).Check()
err := s.State.DestroyStorageInstance(storageTag)
c.Assert(err, jc.ErrorIsNil)
si, err := s.State.StorageInstance(storageTag)
c.Assert(err, jc.ErrorIsNil)
c.Assert(si.Life(), gc.Equals, state.Dying)
}
func (s *StorageStateSuite) TestWatchStorageAttachments(c *gc.C) {
ch := s.AddTestingCharm(c, "storage-block2")
storage := map[string]state.StorageConstraints{
"multi1to10": makeStorageCons("block", 1024, 1),
"multi2up": makeStorageCons("block", 1024, 2),
}
service := s.AddTestingServiceWithStorage(c, "storage-block2", ch, storage)
u, err := service.AddUnit()
c.Assert(err, jc.ErrorIsNil)
w := s.State.WatchStorageAttachments(u.UnitTag())
defer testing.AssertStop(c, w)
wc := testing.NewStringsWatcherC(c, s.State, w)
wc.AssertChange("multi1to10/0", "multi2up/1", "multi2up/2")
wc.AssertNoChange()
err = s.State.DestroyStorageAttachment(names.NewStorageTag("multi2up/1"), u.UnitTag())
c.Assert(err, jc.ErrorIsNil)
wc.AssertChange("multi2up/1")
wc.AssertNoChange()
}
func (s *StorageStateSuite) TestWatchStorageAttachment(c *gc.C) {
_, u, storageTag := s.setupSingleStorage(c)
w := s.State.WatchStorageAttachment(storageTag, u.UnitTag())
defer testing.AssertStop(c, w)
wc := testing.NewNotifyWatcherC(c, s.State, w)
wc.AssertOneChange()
err := s.State.DestroyStorageAttachment(storageTag, u.UnitTag())
c.Assert(err, jc.ErrorIsNil)
wc.AssertOneChange()
}
// TODO(axw) StorageAttachments can't be added to Dying StorageInstance
// TODO(axw) StorageInstance without attachments is removed by Destroy
// TODO(axw) StorageInstance becomes Dying when Unit becomes Dying
// TODO(axw) concurrent add-unit and StorageAttachment removal does not
// remove storage instance.