forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration_test.go
399 lines (347 loc) · 11.9 KB
/
migration_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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package migration_test
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/url"
"time"
"github.com/juju/description"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
"github.com/juju/utils"
"github.com/juju/version"
gc "gopkg.in/check.v1"
"gopkg.in/juju/charm.v6"
"github.com/juju/juju/component/all"
"github.com/juju/juju/core/leadership"
"github.com/juju/juju/core/lease"
coremigration "github.com/juju/juju/core/migration"
"github.com/juju/juju/feature"
"github.com/juju/juju/migration"
"github.com/juju/juju/provider/dummy"
_ "github.com/juju/juju/provider/dummy"
"github.com/juju/juju/resource"
"github.com/juju/juju/resource/resourcetesting"
"github.com/juju/juju/state"
statetesting "github.com/juju/juju/state/testing"
coretesting "github.com/juju/juju/testing"
"github.com/juju/juju/testing/factory"
"github.com/juju/juju/tools"
)
func init() {
// Required for resources.
if err := all.RegisterForServer(); err != nil {
panic(err)
}
}
type ImportSuite struct {
statetesting.StateSuite
}
var _ = gc.Suite(&ImportSuite{})
func (s *ImportSuite) SetUpTest(c *gc.C) {
// Specify the config to use for the controller model before
// calling SetUpTest of the StateSuite, otherwise we get
// coretesting.ModelConfig(c). The default provider type
// specified in the coretesting.ModelConfig function is one that
// isn't registered as a valid provider. For our tests here we
// need a real registered provider, so we use the dummy provider.
// NOTE: make a better test provider.
s.InitialConfig = coretesting.CustomModelConfig(c, dummy.SampleConfig())
s.StateSuite.SetUpTest(c)
}
func (s *ImportSuite) TestBadBytes(c *gc.C) {
bytes := []byte("not a model")
controller := state.NewController(s.StatePool)
model, st, err := migration.ImportModel(controller, fakeGetClaimer, bytes)
c.Check(st, gc.IsNil)
c.Check(model, gc.IsNil)
c.Assert(err, gc.ErrorMatches, "yaml: unmarshal errors:\n.*")
}
func (s *ImportSuite) exportImport(c *gc.C, getClaimer migration.ClaimerFunc) *state.State {
model, err := s.State.Export()
c.Assert(err, jc.ErrorIsNil)
// Update the config values in the exported model for different values for
// "state-port", "api-port", and "ca-cert". Also give the model a new UUID
// and name so we can import it nicely.
uuid := utils.MustNewUUID().String()
model.UpdateConfig(map[string]interface{}{
"name": "new-model",
"uuid": uuid,
})
bytes, err := description.Serialize(model)
c.Check(err, jc.ErrorIsNil)
controller := state.NewController(s.StatePool)
dbModel, dbState, err := migration.ImportModel(controller, getClaimer, bytes)
c.Assert(err, jc.ErrorIsNil)
s.AddCleanup(func(*gc.C) { dbState.Close() })
dbConfig, err := dbModel.Config()
c.Assert(err, jc.ErrorIsNil)
c.Assert(dbConfig.UUID(), gc.Equals, uuid)
c.Assert(dbConfig.Name(), gc.Equals, "new-model")
return dbState
}
func (s *ImportSuite) TestImportModel(c *gc.C) {
s.exportImport(c, fakeGetClaimer)
}
func (s *ImportSuite) TestImportsLeadership(c *gc.C) {
s.makeApplicationWithUnits(c, "wordpress", 3)
s.makeUnitApplicationLeader(c, "wordpress/1", "wordpress")
s.makeApplicationWithUnits(c, "mysql", 2)
var (
claimer fakeClaimer
modelUUID string
)
dbState := s.exportImport(c, func(uuid string) (leadership.Claimer, error) {
modelUUID = uuid
return &claimer, nil
})
c.Assert(modelUUID, gc.Equals, dbState.ModelUUID())
c.Assert(claimer.stub.Calls(), gc.HasLen, 1)
claimer.stub.CheckCall(c, 0, "ClaimLeadership", "wordpress", "wordpress/1", time.Minute)
}
func (s *ImportSuite) TestImportsLeadershipLegacy(c *gc.C) {
err := s.State.UpdateControllerConfig(map[string]interface{}{
"features": []interface{}{feature.LegacyLeases},
}, nil)
c.Assert(err, jc.ErrorIsNil)
s.makeApplicationWithUnits(c, "wordpress", 3)
s.makeUnitApplicationLeaderLegacy(c, "wordpress/1", "wordpress")
s.makeApplicationWithUnits(c, "mysql", 2)
dbState := s.exportImport(c, nil)
leaders, err := dbState.ApplicationLeaders()
c.Assert(err, jc.ErrorIsNil)
c.Assert(leaders, gc.DeepEquals, map[string]string{"wordpress": "wordpress/1"})
}
func (s *ImportSuite) makeApplicationWithUnits(c *gc.C, applicationname string, count int) {
units := make([]*state.Unit, count)
application := s.Factory.MakeApplication(c, &factory.ApplicationParams{
Name: applicationname,
Charm: s.Factory.MakeCharm(c, &factory.CharmParams{
Name: applicationname,
}),
})
for i := 0; i < count; i++ {
units[i] = s.Factory.MakeUnit(c, &factory.UnitParams{
Application: application,
})
}
}
func (s *ImportSuite) makeUnitApplicationLeader(c *gc.C, unitName, applicationName string) {
target := s.State.LeaseNotifyTarget(
ioutil.Discard,
loggo.GetLogger("migration_import_test"),
)
target.Claimed(
lease.Key{"application-leadership", s.State.ModelUUID(), applicationName},
unitName,
)
}
func (s *ImportSuite) makeUnitApplicationLeaderLegacy(c *gc.C, unitName, applicationName string) {
err := s.State.LeadershipClaimer().ClaimLeadership(
applicationName,
unitName,
time.Minute)
c.Assert(err, jc.ErrorIsNil)
}
func (s *ImportSuite) TestUploadBinariesConfigValidate(c *gc.C) {
type T migration.UploadBinariesConfig // alias for brevity
check := func(modify func(*T), missing string) {
config := T{
CharmDownloader: struct{ migration.CharmDownloader }{},
CharmUploader: struct{ migration.CharmUploader }{},
ToolsDownloader: struct{ migration.ToolsDownloader }{},
ToolsUploader: struct{ migration.ToolsUploader }{},
ResourceDownloader: struct{ migration.ResourceDownloader }{},
ResourceUploader: struct{ migration.ResourceUploader }{},
}
modify(&config)
realConfig := migration.UploadBinariesConfig(config)
c.Check(realConfig.Validate(), gc.ErrorMatches, fmt.Sprintf("missing %s not valid", missing))
}
check(func(c *T) { c.CharmDownloader = nil }, "CharmDownloader")
check(func(c *T) { c.CharmUploader = nil }, "CharmUploader")
check(func(c *T) { c.ToolsDownloader = nil }, "ToolsDownloader")
check(func(c *T) { c.ToolsUploader = nil }, "ToolsUploader")
check(func(c *T) { c.ResourceDownloader = nil }, "ResourceDownloader")
check(func(c *T) { c.ResourceUploader = nil }, "ResourceUploader")
}
func (s *ImportSuite) TestBinariesMigration(c *gc.C) {
downloader := &fakeDownloader{}
uploader := &fakeUploader{
tools: make(map[version.Binary]string),
resources: make(map[string]string),
}
toolsMap := map[version.Binary]string{
version.MustParseBinary("2.1.0-trusty-amd64"): "/tools/0",
version.MustParseBinary("2.0.0-xenial-amd64"): "/tools/1",
}
app0Res := resourcetesting.NewResource(c, nil, "blob0", "app0", "blob0").Resource
app1Res := resourcetesting.NewResource(c, nil, "blob1", "app1", "blob1").Resource
app1UnitRes := app1Res
app1UnitRes.Revision = 1
app2Res := resourcetesting.NewPlaceholderResource(c, "blob2", "app2")
resources := []coremigration.SerializedModelResource{
{ApplicationRevision: app0Res},
{
ApplicationRevision: app1Res,
UnitRevisions: map[string]resource.Resource{"app1/99": app1UnitRes},
},
{ApplicationRevision: app2Res},
}
config := migration.UploadBinariesConfig{
Charms: []string{
// These 2 are out of order. Rev 2 must be uploaded first.
"local:trusty/magic-10",
"local:trusty/magic-2",
"cs:trusty/postgresql-42",
},
CharmDownloader: downloader,
CharmUploader: uploader,
Tools: toolsMap,
ToolsDownloader: downloader,
ToolsUploader: uploader,
Resources: resources,
ResourceDownloader: downloader,
ResourceUploader: uploader,
}
err := migration.UploadBinaries(config)
c.Assert(err, jc.ErrorIsNil)
expectedCharms := []string{
// Note ordering.
"cs:trusty/postgresql-42",
"local:trusty/magic-2",
"local:trusty/magic-10",
}
c.Assert(downloader.charms, jc.DeepEquals, expectedCharms)
c.Assert(uploader.charms, jc.DeepEquals, expectedCharms)
c.Assert(downloader.uris, jc.SameContents, []string{
"/tools/0",
"/tools/1",
})
c.Assert(uploader.tools, jc.DeepEquals, toolsMap)
c.Assert(downloader.resources, jc.SameContents, []string{
"app0/blob0",
"app1/blob1",
})
c.Assert(uploader.resources, jc.DeepEquals, map[string]string{
"app0/blob0": "blob0",
"app1/blob1": "blob1",
})
c.Assert(uploader.unitResources, jc.SameContents, []string{"app1/99-blob1"})
}
func (s *ImportSuite) TestWrongCharmURLAssigned(c *gc.C) {
downloader := &fakeDownloader{}
uploader := &fakeUploader{
reassignCharmURL: true,
}
config := migration.UploadBinariesConfig{
Charms: []string{"local:foo/bar-2"},
CharmDownloader: downloader,
CharmUploader: uploader,
ToolsDownloader: downloader,
ToolsUploader: uploader,
ResourceDownloader: downloader,
ResourceUploader: uploader,
}
err := migration.UploadBinaries(config)
c.Assert(err, gc.ErrorMatches,
"charm local:foo/bar-2 unexpectedly assigned local:foo/bar-1")
}
type fakeDownloader struct {
charms []string
uris []string
resources []string
}
func (d *fakeDownloader) OpenCharm(curl *charm.URL) (io.ReadCloser, error) {
urlStr := curl.String()
d.charms = append(d.charms, urlStr)
// Return the charm URL string as the fake charm content
return ioutil.NopCloser(bytes.NewReader([]byte(urlStr + " content"))), nil
}
func (d *fakeDownloader) OpenURI(uri string, query url.Values) (io.ReadCloser, error) {
if query != nil {
panic("query should be empty")
}
d.uris = append(d.uris, uri)
// Return the URI string as fake content
return ioutil.NopCloser(bytes.NewReader([]byte(uri))), nil
}
func (d *fakeDownloader) OpenResource(app, name string) (io.ReadCloser, error) {
d.resources = append(d.resources, app+"/"+name)
// Use the resource name as the content.
return ioutil.NopCloser(bytes.NewReader([]byte(name))), nil
}
type fakeUploader struct {
tools map[version.Binary]string
charms []string
resources map[string]string
unitResources []string
reassignCharmURL bool
}
func (f *fakeUploader) UploadTools(r io.ReadSeeker, v version.Binary, _ ...string) (tools.List, error) {
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, errors.Trace(err)
}
f.tools[v] = string(data)
return tools.List{&tools.Tools{Version: v}}, nil
}
func (f *fakeUploader) UploadCharm(u *charm.URL, r io.ReadSeeker) (*charm.URL, error) {
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, errors.Trace(err)
}
if string(data) != u.String()+" content" {
panic(fmt.Sprintf("unexpected charm body for %s: %s", u.String(), data))
}
f.charms = append(f.charms, u.String())
outU := *u
if f.reassignCharmURL {
outU.Revision--
}
return &outU, nil
}
func (f *fakeUploader) UploadResource(res resource.Resource, r io.ReadSeeker) error {
body, err := ioutil.ReadAll(r)
if err != nil {
return errors.Trace(err)
}
f.resources[res.ApplicationID+"/"+res.Name] = string(body)
return nil
}
func (f *fakeUploader) SetPlaceholderResource(res resource.Resource) error {
f.resources[res.ApplicationID+"/"+res.Name] = "<placeholder>"
return nil
}
func (f *fakeUploader) SetUnitResource(unit string, res resource.Resource) error {
f.unitResources = append(f.unitResources, unit+"-"+res.Name)
return nil
}
type ExportSuite struct {
statetesting.StateSuite
}
var _ = gc.Suite(&ExportSuite{})
func (s *ExportSuite) TestExportModel(c *gc.C) {
bytes, err := migration.ExportModel(s.State)
c.Assert(err, jc.ErrorIsNil)
// The bytes must be a valid model.
modelDesc, err := description.Deserialize(bytes)
c.Assert(err, jc.ErrorIsNil)
c.Assert(modelDesc.Validate(), jc.ErrorIsNil)
}
func fakeGetClaimer(string) (leadership.Claimer, error) {
return &fakeClaimer{}, nil
}
type fakeClaimer struct {
leadership.Claimer
stub testing.Stub
}
func (c *fakeClaimer) ClaimLeadership(application, unit string, duration time.Duration) error {
c.stub.AddCall("ClaimLeadership", application, unit, duration)
return c.stub.NextErr()
}