~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/state/resources_persistence_test.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
 
34
34
        s.stub = &testing.Stub{}
35
35
        s.base = statetest.NewStubPersistence(s.stub)
 
36
        s.base.ReturnServiceExistsOps = []txn.Op{{
 
37
                C:      "service",
 
38
                Id:     "a-service",
 
39
                Assert: txn.DocExists,
 
40
        }}
36
41
}
37
42
 
38
43
func (s *ResourcePersistenceSuite) TestListResourcesOkay(c *gc.C) {
39
44
        expected, docs := newPersistenceResources(c, "a-service", "spam", "eggs")
40
45
        expected.CharmStoreResources[1].Revision += 1
41
46
        docs[3].Revision += 1
42
 
        unitRes, doc := newPersistenceUnitResource(c, "a-service", "a-service/0", "something")
 
47
        unitRes, unitDocs := newPersistenceUnitResources(c, "a-service", "a-service/0", expected.Resources)
 
48
        var progress int64 = 3
 
49
        unitDocs[1].DownloadProgress = &progress // the "eggs" doc
43
50
        expected.UnitResources = []resource.UnitResources{{
44
 
                Tag: names.NewUnitTag("a-service/0"),
45
 
                Resources: []resource.Resource{
46
 
                        unitRes,
 
51
                Tag:       names.NewUnitTag("a-service/0"),
 
52
                Resources: unitRes,
 
53
                DownloadProgress: map[string]int64{
 
54
                        "eggs": progress,
47
55
                },
48
56
        }}
49
 
        docs = append(docs, doc)
 
57
        docs = append(docs, unitDocs...)
50
58
        s.base.ReturnAll = docs
51
59
        p := NewResourcePersistence(s.base)
52
60
 
184
192
        doc.DocID += "#staged"
185
193
        p := NewResourcePersistence(s.base)
186
194
        ignoredErr := errors.New("<never reached>")
187
 
        s.stub.SetErrors(nil, nil, ignoredErr)
 
195
        s.stub.SetErrors(nil, nil, nil, ignoredErr)
188
196
 
189
197
        staged, err := p.StageResource(res.Resource, res.storagePath)
190
198
        c.Assert(err, jc.ErrorIsNil)
191
199
 
192
 
        s.stub.CheckCallNames(c, "Run", "RunTransaction")
193
 
        s.stub.CheckCall(c, 1, "RunTransaction", []txn.Op{{
 
200
        s.stub.CheckCallNames(c, "Run", "ServiceExistsOps", "RunTransaction")
 
201
        s.stub.CheckCall(c, 2, "RunTransaction", []txn.Op{{
194
202
                C:      "resources",
195
203
                Id:     "resource#a-service/spam#staged",
196
204
                Assert: txn.DocMissing,
197
205
                Insert: &doc,
 
206
        }, {
 
207
                C:      "service",
 
208
                Id:     "a-service",
 
209
                Assert: txn.DocExists,
198
210
        }})
199
211
        c.Check(staged, jc.DeepEquals, &StagedResource{
200
212
                base:   s.base,
232
244
        s.base.ReturnOne = doc
233
245
        p := NewResourcePersistence(s.base)
234
246
        ignoredErr := errors.New("<never reached>")
235
 
        s.stub.SetErrors(nil, nil, nil, ignoredErr)
 
247
        s.stub.SetErrors(nil, nil, nil, nil, ignoredErr)
236
248
 
237
249
        err := p.SetResource(res.Resource)
238
250
        c.Assert(err, jc.ErrorIsNil)
240
252
        s.stub.CheckCallNames(c,
241
253
                "One",
242
254
                "Run",
 
255
                "ServiceExistsOps",
243
256
                "RunTransaction",
244
257
        )
245
 
        s.stub.CheckCall(c, 2, "RunTransaction", []txn.Op{{
 
258
        s.stub.CheckCall(c, 3, "RunTransaction", []txn.Op{{
246
259
                C:      "resources",
247
260
                Id:     "resource#a-service/spam",
248
261
                Assert: txn.DocMissing,
249
262
                Insert: &doc,
 
263
        }, {
 
264
                C:      "service",
 
265
                Id:     "a-service",
 
266
                Assert: txn.DocExists,
250
267
        }})
251
268
}
252
269
 
259
276
        p := NewResourcePersistence(s.base)
260
277
        notFound := errors.NewNotFound(nil, "")
261
278
        ignoredErr := errors.New("<never reached>")
262
 
        s.stub.SetErrors(notFound, nil, nil, ignoredErr)
 
279
        s.stub.SetErrors(notFound, nil, nil, nil, ignoredErr)
263
280
 
264
281
        err := p.SetResource(res.Resource)
265
282
        c.Assert(err, jc.ErrorIsNil)
267
284
        s.stub.CheckCallNames(c,
268
285
                "One",
269
286
                "Run",
 
287
                "ServiceExistsOps",
270
288
                "RunTransaction",
271
289
        )
272
 
        s.stub.CheckCall(c, 2, "RunTransaction", []txn.Op{{
 
290
        s.stub.CheckCall(c, 3, "RunTransaction", []txn.Op{{
273
291
                C:      "resources",
274
292
                Id:     "resource#a-service/spam",
275
293
                Assert: txn.DocMissing,
276
294
                Insert: &expected,
 
295
        }, {
 
296
                C:      "service",
 
297
                Id:     "a-service",
 
298
                Assert: txn.DocExists,
277
299
        }})
278
300
}
279
301
 
296
318
 
297
319
        s.stub.CheckCallNames(c,
298
320
                "Run",
 
321
                "ServiceExistsOps",
299
322
                "RunTransaction",
300
323
        )
301
 
        s.stub.CheckCall(c, 1, "RunTransaction", []txn.Op{{
 
324
        s.stub.CheckCall(c, 2, "RunTransaction", []txn.Op{{
302
325
                C:      "resources",
303
326
                Id:     "resource#a-service/spam#charmstore",
304
327
                Assert: txn.DocMissing,
305
328
                Insert: &expected,
 
329
        }, {
 
330
                C:      "service",
 
331
                Id:     "a-service",
 
332
                Assert: txn.DocExists,
306
333
        }})
307
334
}
308
335
 
313
340
        s.base.ReturnOne = doc
314
341
        p := NewResourcePersistence(s.base)
315
342
        ignoredErr := errors.New("<never reached>")
316
 
        s.stub.SetErrors(nil, nil, nil, ignoredErr)
 
343
        s.stub.SetErrors(nil, nil, nil, nil, ignoredErr)
317
344
 
318
345
        err := p.SetUnitResource("a-service/0", res)
319
346
        c.Assert(err, jc.ErrorIsNil)
320
347
 
321
 
        s.stub.CheckCallNames(c, "One", "Run", "RunTransaction")
322
 
        s.stub.CheckCall(c, 2, "RunTransaction", []txn.Op{{
 
348
        s.stub.CheckCallNames(c, "One", "Run", "ServiceExistsOps", "RunTransaction")
 
349
        s.stub.CheckCall(c, 3, "RunTransaction", []txn.Op{{
323
350
                C:      "resources",
324
351
                Id:     "resource#a-service/eggs#unit-a-service/0",
325
352
                Assert: txn.DocMissing,
326
353
                Insert: &doc,
 
354
        }, {
 
355
                C:      "service",
 
356
                Id:     "a-service",
 
357
                Assert: txn.DocExists,
327
358
        }})
328
359
}
329
360
 
347
378
        s.base.ReturnOne = doc
348
379
        p := NewResourcePersistence(s.base)
349
380
        ignoredErr := errors.New("<never reached>")
350
 
        s.stub.SetErrors(nil, nil, txn.ErrAborted, nil, ignoredErr)
 
381
        s.stub.SetErrors(nil, nil, nil, txn.ErrAborted, nil, nil, ignoredErr)
351
382
 
352
383
        err := p.SetUnitResource("a-service/0", res)
353
384
        c.Assert(err, jc.ErrorIsNil)
354
385
 
355
 
        s.stub.CheckCallNames(c, "One", "Run", "RunTransaction", "RunTransaction")
356
 
        s.stub.CheckCall(c, 2, "RunTransaction", []txn.Op{{
357
 
                C:      "resources",
358
 
                Id:     "resource#a-service/spam#unit-a-service/0",
359
 
                Assert: txn.DocMissing,
360
 
                Insert: &doc,
361
 
        }})
 
386
        s.stub.CheckCallNames(c, "One", "Run", "ServiceExistsOps", "RunTransaction", "ServiceExistsOps", "RunTransaction")
362
387
        s.stub.CheckCall(c, 3, "RunTransaction", []txn.Op{{
363
388
                C:      "resources",
364
389
                Id:     "resource#a-service/spam#unit-a-service/0",
 
390
                Assert: txn.DocMissing,
 
391
                Insert: &doc,
 
392
        }, {
 
393
                C:      "service",
 
394
                Id:     "a-service",
 
395
                Assert: txn.DocExists,
 
396
        }})
 
397
        s.stub.CheckCall(c, 5, "RunTransaction", []txn.Op{{
 
398
                C:      "resources",
 
399
                Id:     "resource#a-service/spam#unit-a-service/0",
365
400
                Assert: txn.DocExists,
366
401
                Remove: true,
367
402
        }, {
369
404
                Id:     "resource#a-service/spam#unit-a-service/0",
370
405
                Assert: txn.DocMissing,
371
406
                Insert: &doc,
 
407
        }, {
 
408
                C:      "service",
 
409
                Id:     "a-service",
 
410
                Assert: txn.DocExists,
372
411
        }})
373
412
}
374
413
 
386
425
        s.stub.CheckCallNames(c, "One")
387
426
}
388
427
 
 
428
func (s *ResourcePersistenceSuite) TestSetUnitResourceProgress(c *gc.C) {
 
429
        servicename := "a-service"
 
430
        unitname := "a-service/0"
 
431
        res, doc := newPersistenceUnitResource(c, servicename, unitname, "eggs")
 
432
        s.base.ReturnOne = doc
 
433
        pendingID := "<a pending ID>"
 
434
        res.PendingID = pendingID
 
435
        expected := doc // a copy
 
436
        expected.PendingID = pendingID
 
437
        var progress int64 = 2
 
438
        expected.DownloadProgress = &progress
 
439
        p := NewResourcePersistence(s.base)
 
440
        ignoredErr := errors.New("<never reached>")
 
441
        s.stub.SetErrors(nil, nil, nil, nil, ignoredErr)
 
442
 
 
443
        err := p.SetUnitResourceProgress("a-service/0", res, progress)
 
444
        c.Assert(err, jc.ErrorIsNil)
 
445
 
 
446
        s.stub.CheckCallNames(c, "One", "Run", "ServiceExistsOps", "RunTransaction")
 
447
        s.stub.CheckCall(c, 3, "RunTransaction", []txn.Op{{
 
448
                C:      "resources",
 
449
                Id:     "resource#a-service/eggs#unit-a-service/0",
 
450
                Assert: txn.DocMissing,
 
451
                Insert: &expected,
 
452
        }, {
 
453
                C:      "service",
 
454
                Id:     "a-service",
 
455
                Assert: txn.DocExists,
 
456
        }})
 
457
}
 
458
 
389
459
func (s *ResourcePersistenceSuite) TestNewResourcePendingResourceOpsExists(c *gc.C) {
390
460
        pendingID := "some-unique-ID-001"
391
461
        stored, expected := newPersistenceResource(c, "a-service", "spam")
396
466
        s.base.ReturnOne = doc
397
467
        p := NewResourcePersistence(s.base)
398
468
 
 
469
        lastPolled := time.Now().UTC().Round(time.Second)
 
470
 
399
471
        ops, err := p.NewResolvePendingResourceOps(stored.ID, stored.PendingID)
400
472
        c.Assert(err, jc.ErrorIsNil)
401
473
 
 
474
        csresourceDoc := expected
 
475
        csresourceDoc.DocID = "resource#a-service/spam#charmstore"
 
476
        csresourceDoc.Username = ""
 
477
        csresourceDoc.Timestamp = time.Time{}
 
478
        csresourceDoc.StoragePath = ""
 
479
        csresourceDoc.LastPolled = lastPolled
 
480
 
 
481
        res := ops[4].Insert.(*resourceDoc)
 
482
        res.LastPolled = res.LastPolled.Round(time.Second)
 
483
 
402
484
        s.stub.CheckCallNames(c, "One", "One")
403
485
        s.stub.CheckCall(c, 0, "One", "resources", "resource#a-service/spam#pending-some-unique-ID-001", &doc)
404
 
        c.Check(ops, jc.DeepEquals, []txn.Op{{
405
 
                C:      "resources",
406
 
                Id:     doc.DocID,
407
 
                Assert: txn.DocExists,
408
 
                Remove: true,
409
 
        }, {
410
 
                C:      "resources",
411
 
                Id:     expected.DocID,
412
 
                Assert: txn.DocExists,
413
 
                Remove: true,
414
 
        }, {
415
 
                C:      "resources",
416
 
                Id:     expected.DocID,
417
 
                Assert: txn.DocMissing,
418
 
                Insert: &expected,
419
 
        }})
 
486
        c.Check(ops, jc.DeepEquals, []txn.Op{
 
487
                {
 
488
                        C:      "resources",
 
489
                        Id:     doc.DocID,
 
490
                        Assert: txn.DocExists,
 
491
                        Remove: true,
 
492
                }, {
 
493
                        C:      "resources",
 
494
                        Id:     expected.DocID,
 
495
                        Assert: txn.DocExists,
 
496
                        Remove: true,
 
497
                }, {
 
498
                        C:      "resources",
 
499
                        Id:     expected.DocID,
 
500
                        Assert: txn.DocMissing,
 
501
                        Insert: &expected,
 
502
                },
 
503
                {
 
504
                        C:      "resources",
 
505
                        Id:     csresourceDoc.DocID,
 
506
                        Assert: txn.DocExists,
 
507
                        Remove: true,
 
508
                },
 
509
                {
 
510
                        C:      "resources",
 
511
                        Id:     csresourceDoc.DocID,
 
512
                        Assert: txn.DocMissing,
 
513
                        Insert: &csresourceDoc,
 
514
                },
 
515
        })
420
516
}
421
517
 
422
518
func (s *ResourcePersistenceSuite) TestNewResourcePendingResourceOpsNotFound(c *gc.C) {
431
527
        s.stub.SetErrors(nil, notFound)
432
528
        p := NewResourcePersistence(s.base)
433
529
 
 
530
        lastPolled := time.Now().UTC().Round(time.Second)
434
531
        ops, err := p.NewResolvePendingResourceOps(stored.ID, stored.PendingID)
435
532
        c.Assert(err, jc.ErrorIsNil)
436
533
 
437
534
        s.stub.CheckCallNames(c, "One", "One")
438
535
        s.stub.CheckCall(c, 0, "One", "resources", "resource#a-service/spam#pending-some-unique-ID-001", &doc)
439
 
        c.Check(ops, jc.DeepEquals, []txn.Op{{
440
 
                C:      "resources",
441
 
                Id:     doc.DocID,
442
 
                Assert: txn.DocExists,
443
 
                Remove: true,
444
 
        }, {
445
 
                C:      "resources",
446
 
                Id:     expected.DocID,
447
 
                Assert: txn.DocMissing,
448
 
                Insert: &expected,
449
 
        }})
 
536
 
 
537
        csresourceDoc := expected
 
538
        csresourceDoc.DocID = "resource#a-service/spam#charmstore"
 
539
        csresourceDoc.Username = ""
 
540
        csresourceDoc.Timestamp = time.Time{}
 
541
        csresourceDoc.StoragePath = ""
 
542
        csresourceDoc.LastPolled = lastPolled
 
543
 
 
544
        res := ops[2].Insert.(*resourceDoc)
 
545
        res.LastPolled = res.LastPolled.Round(time.Second)
 
546
 
 
547
        c.Check(ops, jc.DeepEquals, []txn.Op{
 
548
                {
 
549
                        C:      "resources",
 
550
                        Id:     doc.DocID,
 
551
                        Assert: txn.DocExists,
 
552
                        Remove: true,
 
553
                }, {
 
554
                        C:      "resources",
 
555
                        Id:     expected.DocID,
 
556
                        Assert: txn.DocMissing,
 
557
                        Insert: &expected,
 
558
                },
 
559
                {
 
560
                        C:      "resources",
 
561
                        Id:     csresourceDoc.DocID,
 
562
                        Assert: txn.DocMissing,
 
563
                        Insert: &csresourceDoc,
 
564
                },
 
565
        })
 
566
}
 
567
 
 
568
func newPersistenceUnitResources(c *gc.C, serviceID, unitID string, resources []resource.Resource) ([]resource.Resource, []resourceDoc) {
 
569
        var unitResources []resource.Resource
 
570
        var docs []resourceDoc
 
571
        for _, res := range resources {
 
572
                res, doc := newPersistenceUnitResource(c, serviceID, unitID, res.Name)
 
573
                unitResources = append(unitResources, res)
 
574
                docs = append(docs, doc)
 
575
        }
 
576
        return unitResources, docs
 
577
}
 
578
 
 
579
func newPersistenceUnitResource(c *gc.C, serviceID, unitID, name string) (resource.Resource, resourceDoc) {
 
580
        res, doc := newPersistenceResource(c, serviceID, name)
 
581
        doc.DocID += "#unit-" + unitID
 
582
        doc.UnitID = unitID
 
583
        return res.Resource, doc
450
584
}
451
585
 
452
586
func newPersistenceResources(c *gc.C, serviceID string, names ...string) (resource.ServiceResources, []resourceDoc) {
468
602
        return svcResources, docs
469
603
}
470
604
 
471
 
func newPersistenceUnitResource(c *gc.C, serviceID, unitID, name string) (resource.Resource, resourceDoc) {
472
 
        res, doc := newPersistenceResource(c, serviceID, name)
473
 
        doc.DocID += "#unit-" + unitID
474
 
        doc.UnitID = unitID
475
 
        return res.Resource, doc
476
 
}
477
 
 
478
605
func newPersistenceResource(c *gc.C, serviceID, name string) (storedResource, resourceDoc) {
479
606
        content := name
480
607
        opened := resourcetesting.NewResource(c, nil, name, serviceID, content)