~wallyworld/juju-core/fast-lxc-everywhere

« back to all changes in this revision

Viewing changes to state/service_test.go

  • Committer: Roger Peppe
  • Date: 2013-08-19 11:20:02 UTC
  • mto: (1666.1.9 368-environs-prepare)
  • mto: This revision was merged to the branch mainline in revision 1676.
  • Revision ID: roger.peppe@canonical.com-20130819112002-af52oz2bwgygi4gk
gocheck imports - s-w

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
        "sort"
9
9
 
10
10
        "labix.org/v2/mgo"
11
 
        . "launchpad.net/gocheck"
 
11
        gc "launchpad.net/gocheck"
12
12
 
13
13
        "launchpad.net/juju-core/charm"
14
14
        "launchpad.net/juju-core/constraints"
24
24
        mysql *state.Service
25
25
}
26
26
 
27
 
var _ = Suite(&ServiceSuite{})
 
27
var _ = gc.Suite(&ServiceSuite{})
28
28
 
29
 
func (s *ServiceSuite) SetUpTest(c *C) {
 
29
func (s *ServiceSuite) SetUpTest(c *gc.C) {
30
30
        s.ConnSuite.SetUpTest(c)
31
31
        s.charm = s.AddTestingCharm(c, "mysql")
32
32
        var err error
33
33
        s.mysql, err = s.State.AddService("mysql", s.charm)
34
 
        c.Assert(err, IsNil)
 
34
        c.Assert(err, gc.IsNil)
35
35
}
36
36
 
37
 
func (s *ServiceSuite) TestSetCharm(c *C) {
 
37
func (s *ServiceSuite) TestSetCharm(c *gc.C) {
38
38
        ch, force, err := s.mysql.Charm()
39
 
        c.Assert(err, IsNil)
40
 
        c.Assert(ch.URL(), DeepEquals, s.charm.URL())
41
 
        c.Assert(force, Equals, false)
 
39
        c.Assert(err, gc.IsNil)
 
40
        c.Assert(ch.URL(), gc.DeepEquals, s.charm.URL())
 
41
        c.Assert(force, gc.Equals, false)
42
42
        url, force := s.mysql.CharmURL()
43
 
        c.Assert(url, DeepEquals, s.charm.URL())
44
 
        c.Assert(force, Equals, false)
 
43
        c.Assert(url, gc.DeepEquals, s.charm.URL())
 
44
        c.Assert(force, gc.Equals, false)
45
45
 
46
46
        // Add a compatible charm and force it.
47
47
        sch := s.AddMetaCharm(c, "mysql", metaBase, 2) // revno 1 is used by SetUpSuite
48
48
        err = s.mysql.SetCharm(sch, true)
49
 
        c.Assert(err, IsNil)
 
49
        c.Assert(err, gc.IsNil)
50
50
        ch, force, err = s.mysql.Charm()
51
 
        c.Assert(err, IsNil)
52
 
        c.Assert(ch.URL(), DeepEquals, sch.URL())
53
 
        c.Assert(force, Equals, true)
 
51
        c.Assert(err, gc.IsNil)
 
52
        c.Assert(ch.URL(), gc.DeepEquals, sch.URL())
 
53
        c.Assert(force, gc.Equals, true)
54
54
        url, force = s.mysql.CharmURL()
55
 
        c.Assert(url, DeepEquals, sch.URL())
56
 
        c.Assert(force, Equals, true)
 
55
        c.Assert(url, gc.DeepEquals, sch.URL())
 
56
        c.Assert(force, gc.Equals, true)
57
57
 
58
58
        // SetCharm fails when the service is Dying.
59
59
        _, err = s.mysql.AddUnit()
60
 
        c.Assert(err, IsNil)
 
60
        c.Assert(err, gc.IsNil)
61
61
        err = s.mysql.Destroy()
62
 
        c.Assert(err, IsNil)
 
62
        c.Assert(err, gc.IsNil)
63
63
        err = s.mysql.SetCharm(sch, true)
64
 
        c.Assert(err, ErrorMatches, `service "mysql" is not alive`)
 
64
        c.Assert(err, gc.ErrorMatches, `service "mysql" is not alive`)
65
65
}
66
66
 
67
 
func (s *ServiceSuite) TestSetCharmErrors(c *C) {
 
67
func (s *ServiceSuite) TestSetCharmErrors(c *gc.C) {
68
68
        logging := s.AddTestingCharm(c, "logging")
69
69
        err := s.mysql.SetCharm(logging, false)
70
 
        c.Assert(err, ErrorMatches, "cannot change a service's subordinacy")
 
70
        c.Assert(err, gc.ErrorMatches, "cannot change a service's subordinacy")
71
71
 
72
72
        othermysql := s.AddSeriesCharm(c, "mysql", "otherseries")
73
73
        err = s.mysql.SetCharm(othermysql, false)
74
 
        c.Assert(err, ErrorMatches, "cannot change a service's series")
 
74
        c.Assert(err, gc.ErrorMatches, "cannot change a service's series")
75
75
}
76
76
 
77
77
var metaBase = `
157
157
        },
158
158
}
159
159
 
160
 
func (s *ServiceSuite) TestSetCharmChecksEndpointsWithoutRelations(c *C) {
 
160
func (s *ServiceSuite) TestSetCharmChecksEndpointsWithoutRelations(c *gc.C) {
161
161
        revno := 2 // 1 is used in SetUpSuite
162
162
        ms := s.AddMetaCharm(c, "mysql", metaBase, revno)
163
163
        svc, err := s.State.AddService("fakemysql", ms)
164
 
        c.Assert(err, IsNil)
 
164
        c.Assert(err, gc.IsNil)
165
165
        err = svc.SetCharm(ms, false)
166
 
        c.Assert(err, IsNil)
 
166
        c.Assert(err, gc.IsNil)
167
167
 
168
168
        for i, t := range setCharmEndpointsTests {
169
169
                c.Logf("test %d: %s", i, t.summary)
171
171
                newCh := s.AddMetaCharm(c, "mysql", t.meta, revno+i+1)
172
172
                err = svc.SetCharm(newCh, false)
173
173
                if t.err != "" {
174
 
                        c.Assert(err, ErrorMatches, t.err)
 
174
                        c.Assert(err, gc.ErrorMatches, t.err)
175
175
                } else {
176
 
                        c.Assert(err, IsNil)
 
176
                        c.Assert(err, gc.IsNil)
177
177
                }
178
178
        }
179
179
 
180
180
        err = svc.Destroy()
181
 
        c.Assert(err, IsNil)
 
181
        c.Assert(err, gc.IsNil)
182
182
}
183
183
 
184
 
func (s *ServiceSuite) TestSetCharmChecksEndpointsWithRelations(c *C) {
 
184
func (s *ServiceSuite) TestSetCharmChecksEndpointsWithRelations(c *gc.C) {
185
185
        revno := 2 // 1 is used by SetUpSuite
186
186
        providerCharm := s.AddMetaCharm(c, "mysql", metaDifferentProvider, revno)
187
187
        providerSvc, err := s.State.AddService("myprovider", providerCharm)
188
 
        c.Assert(err, IsNil)
 
188
        c.Assert(err, gc.IsNil)
189
189
        err = providerSvc.SetCharm(providerCharm, false)
190
 
        c.Assert(err, IsNil)
 
190
        c.Assert(err, gc.IsNil)
191
191
 
192
192
        revno++
193
193
        requirerCharm := s.AddMetaCharm(c, "mysql", metaDifferentRequirer, revno)
194
194
        requirerSvc, err := s.State.AddService("myrequirer", requirerCharm)
195
 
        c.Assert(err, IsNil)
 
195
        c.Assert(err, gc.IsNil)
196
196
        err = requirerSvc.SetCharm(requirerCharm, false)
197
 
        c.Assert(err, IsNil)
 
197
        c.Assert(err, gc.IsNil)
198
198
 
199
199
        eps, err := s.State.InferEndpoints([]string{"myprovider:kludge", "myrequirer:kludge"})
200
 
        c.Assert(err, IsNil)
 
200
        c.Assert(err, gc.IsNil)
201
201
        _, err = s.State.AddRelation(eps...)
202
 
        c.Assert(err, IsNil)
 
202
        c.Assert(err, gc.IsNil)
203
203
 
204
204
        revno++
205
205
        baseCharm := s.AddMetaCharm(c, "mysql", metaBase, revno)
206
206
        err = providerSvc.SetCharm(baseCharm, false)
207
 
        c.Assert(err, ErrorMatches, `cannot upgrade service "myprovider" to charm "local:series/series-mysql-4": would break relation "myrequirer:kludge myprovider:kludge"`)
 
207
        c.Assert(err, gc.ErrorMatches, `cannot upgrade service "myprovider" to charm "local:series/series-mysql-4": would break relation "myrequirer:kludge myprovider:kludge"`)
208
208
        err = requirerSvc.SetCharm(baseCharm, false)
209
 
        c.Assert(err, ErrorMatches, `cannot upgrade service "myrequirer" to charm "local:series/series-mysql-4": would break relation "myrequirer:kludge myprovider:kludge"`)
 
209
        c.Assert(err, gc.ErrorMatches, `cannot upgrade service "myrequirer" to charm "local:series/series-mysql-4": would break relation "myrequirer:kludge myprovider:kludge"`)
210
210
}
211
211
 
212
212
var stringConfig = `
270
270
        },
271
271
}
272
272
 
273
 
func (s *ServiceSuite) TestSetCharmConfig(c *C) {
 
273
func (s *ServiceSuite) TestSetCharmConfig(c *gc.C) {
274
274
        charms := map[string]*state.Charm{
275
275
                stringConfig:    s.AddConfigCharm(c, "wordpress", stringConfig, 1),
276
276
                emptyConfig:     s.AddConfigCharm(c, "wordpress", emptyConfig, 2),
283
283
 
284
284
                origCh := charms[t.startconfig]
285
285
                svc, err := s.State.AddService("wordpress", origCh)
286
 
                c.Assert(err, IsNil)
 
286
                c.Assert(err, gc.IsNil)
287
287
                err = svc.UpdateConfigSettings(t.startvalues)
288
 
                c.Assert(err, IsNil)
 
288
                c.Assert(err, gc.IsNil)
289
289
 
290
290
                newCh := charms[t.endconfig]
291
291
                err = svc.SetCharm(newCh, false)
292
292
                var expectVals charm.Settings
293
293
                var expectCh *state.Charm
294
294
                if t.err != "" {
295
 
                        c.Assert(err, ErrorMatches, t.err)
 
295
                        c.Assert(err, gc.ErrorMatches, t.err)
296
296
                        expectCh = origCh
297
297
                        expectVals = t.startvalues
298
298
                } else {
299
 
                        c.Assert(err, IsNil)
 
299
                        c.Assert(err, gc.IsNil)
300
300
                        expectCh = newCh
301
301
                        expectVals = t.endvalues
302
302
                }
303
303
 
304
304
                sch, _, err := svc.Charm()
305
 
                c.Assert(err, IsNil)
306
 
                c.Assert(sch.URL(), DeepEquals, expectCh.URL())
 
305
                c.Assert(err, gc.IsNil)
 
306
                c.Assert(sch.URL(), gc.DeepEquals, expectCh.URL())
307
307
                settings, err := svc.ConfigSettings()
308
 
                c.Assert(err, IsNil)
 
308
                c.Assert(err, gc.IsNil)
309
309
                if len(expectVals) == 0 {
310
 
                        c.Assert(settings, HasLen, 0)
 
310
                        c.Assert(settings, gc.HasLen, 0)
311
311
                } else {
312
 
                        c.Assert(settings, DeepEquals, expectVals)
 
312
                        c.Assert(settings, gc.DeepEquals, expectVals)
313
313
                }
314
314
 
315
315
                err = svc.Destroy()
316
 
                c.Assert(err, IsNil)
 
316
                c.Assert(err, gc.IsNil)
317
317
        }
318
318
}
319
319
 
367
367
        update:  charm.Settings{"skill-level": nil},
368
368
}}
369
369
 
370
 
func (s *ServiceSuite) TestUpdateConfigSettings(c *C) {
 
370
func (s *ServiceSuite) TestUpdateConfigSettings(c *gc.C) {
371
371
        sch := s.AddTestingCharm(c, "dummy")
372
372
        for i, t := range serviceUpdateConfigSettingsTests {
373
373
                c.Logf("test %d. %s", i, t.about)
374
374
                svc, err := s.State.AddService("dummy-service", sch)
375
 
                c.Assert(err, IsNil)
 
375
                c.Assert(err, gc.IsNil)
376
376
                if t.initial != nil {
377
377
                        err := svc.UpdateConfigSettings(t.initial)
378
 
                        c.Assert(err, IsNil)
 
378
                        c.Assert(err, gc.IsNil)
379
379
                }
380
380
                err = svc.UpdateConfigSettings(t.update)
381
381
                if t.err != "" {
382
 
                        c.Assert(err, ErrorMatches, t.err)
 
382
                        c.Assert(err, gc.ErrorMatches, t.err)
383
383
                } else {
384
 
                        c.Assert(err, IsNil)
 
384
                        c.Assert(err, gc.IsNil)
385
385
                        settings, err := svc.ConfigSettings()
386
 
                        c.Assert(err, IsNil)
 
386
                        c.Assert(err, gc.IsNil)
387
387
                        expect := t.expect
388
388
                        if expect == nil {
389
389
                                expect = charm.Settings{}
390
390
                        }
391
 
                        c.Assert(settings, DeepEquals, expect)
 
391
                        c.Assert(settings, gc.DeepEquals, expect)
392
392
                }
393
393
                err = svc.Destroy()
394
 
                c.Assert(err, IsNil)
 
394
                c.Assert(err, gc.IsNil)
395
395
        }
396
396
}
397
397
 
398
 
func (s *ServiceSuite) TestSettingsRefCountWorks(c *C) {
 
398
func (s *ServiceSuite) TestSettingsRefCountWorks(c *gc.C) {
399
399
        oldCh := s.AddConfigCharm(c, "wordpress", emptyConfig, 1)
400
400
        newCh := s.AddConfigCharm(c, "wordpress", emptyConfig, 2)
401
401
        svcName := "mywp"
402
402
 
403
403
        assertNoRef := func(sch *state.Charm) {
404
404
                _, err := state.ServiceSettingsRefCount(s.State, svcName, sch.URL())
405
 
                c.Assert(err, Equals, mgo.ErrNotFound)
 
405
                c.Assert(err, gc.Equals, mgo.ErrNotFound)
406
406
        }
407
407
        assertRef := func(sch *state.Charm, refcount int) {
408
408
                rc, err := state.ServiceSettingsRefCount(s.State, svcName, sch.URL())
409
 
                c.Assert(err, IsNil)
410
 
                c.Assert(rc, Equals, refcount)
 
409
                c.Assert(err, gc.IsNil)
 
410
                c.Assert(rc, gc.Equals, refcount)
411
411
        }
412
412
 
413
413
        assertNoRef(oldCh)
414
414
        assertNoRef(newCh)
415
415
 
416
416
        svc, err := s.State.AddService(svcName, oldCh)
417
 
        c.Assert(err, IsNil)
 
417
        c.Assert(err, gc.IsNil)
418
418
        assertRef(oldCh, 1)
419
419
        assertNoRef(newCh)
420
420
 
421
421
        err = svc.SetCharm(oldCh, false)
422
 
        c.Assert(err, IsNil)
 
422
        c.Assert(err, gc.IsNil)
423
423
        assertRef(oldCh, 1)
424
424
        assertNoRef(newCh)
425
425
 
426
426
        err = svc.SetCharm(newCh, false)
427
 
        c.Assert(err, IsNil)
 
427
        c.Assert(err, gc.IsNil)
428
428
        assertNoRef(oldCh)
429
429
        assertRef(newCh, 1)
430
430
 
431
431
        err = svc.SetCharm(oldCh, false)
432
 
        c.Assert(err, IsNil)
 
432
        c.Assert(err, gc.IsNil)
433
433
        assertRef(oldCh, 1)
434
434
        assertNoRef(newCh)
435
435
 
436
436
        u, err := svc.AddUnit()
437
 
        c.Assert(err, IsNil)
 
437
        c.Assert(err, gc.IsNil)
438
438
        curl, ok := u.CharmURL()
439
 
        c.Assert(ok, Equals, false)
 
439
        c.Assert(ok, gc.Equals, false)
440
440
        assertRef(oldCh, 1)
441
441
        assertNoRef(newCh)
442
442
 
443
443
        err = u.SetCharmURL(oldCh.URL())
444
 
        c.Assert(err, IsNil)
 
444
        c.Assert(err, gc.IsNil)
445
445
        curl, ok = u.CharmURL()
446
 
        c.Assert(ok, Equals, true)
447
 
        c.Assert(curl, DeepEquals, oldCh.URL())
 
446
        c.Assert(ok, gc.Equals, true)
 
447
        c.Assert(curl, gc.DeepEquals, oldCh.URL())
448
448
        assertRef(oldCh, 2)
449
449
        assertNoRef(newCh)
450
450
 
451
451
        err = u.EnsureDead()
452
 
        c.Assert(err, IsNil)
 
452
        c.Assert(err, gc.IsNil)
453
453
        assertRef(oldCh, 2)
454
454
        assertNoRef(newCh)
455
455
 
456
456
        err = u.Remove()
457
 
        c.Assert(err, IsNil)
 
457
        c.Assert(err, gc.IsNil)
458
458
        assertRef(oldCh, 1)
459
459
        assertNoRef(newCh)
460
460
 
461
461
        err = svc.Destroy()
462
 
        c.Assert(err, IsNil)
 
462
        c.Assert(err, gc.IsNil)
463
463
        assertNoRef(oldCh)
464
464
        assertNoRef(newCh)
465
465
}
481
481
  loadbalancer: phony
482
482
`
483
483
 
484
 
func (s *ServiceSuite) assertServiceRelations(c *C, svc *state.Service, expectedKeys ...string) []*state.Relation {
 
484
func (s *ServiceSuite) assertServiceRelations(c *gc.C, svc *state.Service, expectedKeys ...string) []*state.Relation {
485
485
        rels, err := svc.Relations()
486
 
        c.Assert(err, IsNil)
 
486
        c.Assert(err, gc.IsNil)
487
487
        if len(rels) == 0 {
488
488
                return nil
489
489
        }
492
492
                relKeys[i] = rel.String()
493
493
        }
494
494
        sort.Strings(relKeys)
495
 
        c.Assert(relKeys, DeepEquals, expectedKeys)
 
495
        c.Assert(relKeys, gc.DeepEquals, expectedKeys)
496
496
        return rels
497
497
}
498
498
 
499
 
func (s *ServiceSuite) TestNewPeerRelationsAddedOnUpgrade(c *C) {
 
499
func (s *ServiceSuite) TestNewPeerRelationsAddedOnUpgrade(c *gc.C) {
500
500
        // Original mysql charm has no peer relations.
501
501
        oldCh := s.AddMetaCharm(c, "mysql", mysqlBaseMeta+onePeerMeta, 2)
502
502
        newCh := s.AddMetaCharm(c, "mysql", mysqlBaseMeta+twoPeersMeta, 3)
505
505
        s.assertServiceRelations(c, s.mysql)
506
506
 
507
507
        err := s.mysql.SetCharm(oldCh, false)
508
 
        c.Assert(err, IsNil)
 
508
        c.Assert(err, gc.IsNil)
509
509
        s.assertServiceRelations(c, s.mysql, "mysql:cluster")
510
510
 
511
511
        err = s.mysql.SetCharm(newCh, false)
512
 
        c.Assert(err, IsNil)
 
512
        c.Assert(err, gc.IsNil)
513
513
        rels := s.assertServiceRelations(c, s.mysql, "mysql:cluster", "mysql:loadbalancer")
514
514
 
515
515
        // Check state consistency by attempting to destroy the service.
516
516
        err = s.mysql.Destroy()
517
 
        c.Assert(err, IsNil)
 
517
        c.Assert(err, gc.IsNil)
518
518
 
519
519
        // Check the peer relations got destroyed as well.
520
520
        for _, rel := range rels {
535
535
        }
536
536
}
537
537
 
538
 
func (s *ServiceSuite) TestTag(c *C) {
539
 
        c.Assert(s.mysql.Tag(), Equals, "service-mysql")
 
538
func (s *ServiceSuite) TestTag(c *gc.C) {
 
539
        c.Assert(s.mysql.Tag(), gc.Equals, "service-mysql")
540
540
}
541
541
 
542
 
func (s *ServiceSuite) TestMysqlEndpoints(c *C) {
 
542
func (s *ServiceSuite) TestMysqlEndpoints(c *gc.C) {
543
543
        _, err := s.mysql.Endpoint("mysql")
544
 
        c.Assert(err, ErrorMatches, `service "mysql" has no "mysql" relation`)
 
544
        c.Assert(err, gc.ErrorMatches, `service "mysql" has no "mysql" relation`)
545
545
 
546
546
        jiEP, err := s.mysql.Endpoint("juju-info")
547
 
        c.Assert(err, IsNil)
548
 
        c.Assert(jiEP, DeepEquals, jujuInfoEp("mysql"))
 
547
        c.Assert(err, gc.IsNil)
 
548
        c.Assert(jiEP, gc.DeepEquals, jujuInfoEp("mysql"))
549
549
 
550
550
        serverEP, err := s.mysql.Endpoint("server")
551
 
        c.Assert(err, IsNil)
552
 
        c.Assert(serverEP, DeepEquals, state.Endpoint{
 
551
        c.Assert(err, gc.IsNil)
 
552
        c.Assert(serverEP, gc.DeepEquals, state.Endpoint{
553
553
                ServiceName: "mysql",
554
554
                Relation: charm.Relation{
555
555
                        Interface: "mysql",
560
560
        })
561
561
 
562
562
        eps, err := s.mysql.Endpoints()
563
 
        c.Assert(err, IsNil)
564
 
        c.Assert(eps, DeepEquals, []state.Endpoint{jiEP, serverEP})
 
563
        c.Assert(err, gc.IsNil)
 
564
        c.Assert(eps, gc.DeepEquals, []state.Endpoint{jiEP, serverEP})
565
565
}
566
566
 
567
 
func (s *ServiceSuite) TestRiakEndpoints(c *C) {
 
567
func (s *ServiceSuite) TestRiakEndpoints(c *gc.C) {
568
568
        riak, err := s.State.AddService("myriak", s.AddTestingCharm(c, "riak"))
569
 
        c.Assert(err, IsNil)
 
569
        c.Assert(err, gc.IsNil)
570
570
 
571
571
        _, err = riak.Endpoint("garble")
572
 
        c.Assert(err, ErrorMatches, `service "myriak" has no "garble" relation`)
 
572
        c.Assert(err, gc.ErrorMatches, `service "myriak" has no "garble" relation`)
573
573
 
574
574
        jiEP, err := riak.Endpoint("juju-info")
575
 
        c.Assert(err, IsNil)
576
 
        c.Assert(jiEP, DeepEquals, jujuInfoEp("myriak"))
 
575
        c.Assert(err, gc.IsNil)
 
576
        c.Assert(jiEP, gc.DeepEquals, jujuInfoEp("myriak"))
577
577
 
578
578
        ringEP, err := riak.Endpoint("ring")
579
 
        c.Assert(err, IsNil)
580
 
        c.Assert(ringEP, DeepEquals, state.Endpoint{
 
579
        c.Assert(err, gc.IsNil)
 
580
        c.Assert(ringEP, gc.DeepEquals, state.Endpoint{
581
581
                ServiceName: "myriak",
582
582
                Relation: charm.Relation{
583
583
                        Interface: "riak",
589
589
        })
590
590
 
591
591
        adminEP, err := riak.Endpoint("admin")
592
 
        c.Assert(err, IsNil)
593
 
        c.Assert(adminEP, DeepEquals, state.Endpoint{
 
592
        c.Assert(err, gc.IsNil)
 
593
        c.Assert(adminEP, gc.DeepEquals, state.Endpoint{
594
594
                ServiceName: "myriak",
595
595
                Relation: charm.Relation{
596
596
                        Interface: "http",
601
601
        })
602
602
 
603
603
        endpointEP, err := riak.Endpoint("endpoint")
604
 
        c.Assert(err, IsNil)
605
 
        c.Assert(endpointEP, DeepEquals, state.Endpoint{
 
604
        c.Assert(err, gc.IsNil)
 
605
        c.Assert(endpointEP, gc.DeepEquals, state.Endpoint{
606
606
                ServiceName: "myriak",
607
607
                Relation: charm.Relation{
608
608
                        Interface: "http",
613
613
        })
614
614
 
615
615
        eps, err := riak.Endpoints()
616
 
        c.Assert(err, IsNil)
617
 
        c.Assert(eps, DeepEquals, []state.Endpoint{adminEP, endpointEP, jiEP, ringEP})
 
616
        c.Assert(err, gc.IsNil)
 
617
        c.Assert(eps, gc.DeepEquals, []state.Endpoint{adminEP, endpointEP, jiEP, ringEP})
618
618
}
619
619
 
620
 
func (s *ServiceSuite) TestWordpressEndpoints(c *C) {
 
620
func (s *ServiceSuite) TestWordpressEndpoints(c *gc.C) {
621
621
        wordpress, err := s.State.AddService("wordpress", s.AddTestingCharm(c, "wordpress"))
622
 
        c.Assert(err, IsNil)
 
622
        c.Assert(err, gc.IsNil)
623
623
 
624
624
        _, err = wordpress.Endpoint("nonsense")
625
 
        c.Assert(err, ErrorMatches, `service "wordpress" has no "nonsense" relation`)
 
625
        c.Assert(err, gc.ErrorMatches, `service "wordpress" has no "nonsense" relation`)
626
626
 
627
627
        jiEP, err := wordpress.Endpoint("juju-info")
628
 
        c.Assert(err, IsNil)
629
 
        c.Assert(jiEP, DeepEquals, jujuInfoEp("wordpress"))
 
628
        c.Assert(err, gc.IsNil)
 
629
        c.Assert(jiEP, gc.DeepEquals, jujuInfoEp("wordpress"))
630
630
 
631
631
        urlEP, err := wordpress.Endpoint("url")
632
 
        c.Assert(err, IsNil)
633
 
        c.Assert(urlEP, DeepEquals, state.Endpoint{
 
632
        c.Assert(err, gc.IsNil)
 
633
        c.Assert(urlEP, gc.DeepEquals, state.Endpoint{
634
634
                ServiceName: "wordpress",
635
635
                Relation: charm.Relation{
636
636
                        Interface: "http",
641
641
        })
642
642
 
643
643
        ldEP, err := wordpress.Endpoint("logging-dir")
644
 
        c.Assert(err, IsNil)
645
 
        c.Assert(ldEP, DeepEquals, state.Endpoint{
 
644
        c.Assert(err, gc.IsNil)
 
645
        c.Assert(ldEP, gc.DeepEquals, state.Endpoint{
646
646
                ServiceName: "wordpress",
647
647
                Relation: charm.Relation{
648
648
                        Interface: "logging",
653
653
        })
654
654
 
655
655
        mpEP, err := wordpress.Endpoint("monitoring-port")
656
 
        c.Assert(err, IsNil)
657
 
        c.Assert(mpEP, DeepEquals, state.Endpoint{
 
656
        c.Assert(err, gc.IsNil)
 
657
        c.Assert(mpEP, gc.DeepEquals, state.Endpoint{
658
658
                ServiceName: "wordpress",
659
659
                Relation: charm.Relation{
660
660
                        Interface: "monitoring",
665
665
        })
666
666
 
667
667
        dbEP, err := wordpress.Endpoint("db")
668
 
        c.Assert(err, IsNil)
669
 
        c.Assert(dbEP, DeepEquals, state.Endpoint{
 
668
        c.Assert(err, gc.IsNil)
 
669
        c.Assert(dbEP, gc.DeepEquals, state.Endpoint{
670
670
                ServiceName: "wordpress",
671
671
                Relation: charm.Relation{
672
672
                        Interface: "mysql",
678
678
        })
679
679
 
680
680
        cacheEP, err := wordpress.Endpoint("cache")
681
 
        c.Assert(err, IsNil)
682
 
        c.Assert(cacheEP, DeepEquals, state.Endpoint{
 
681
        c.Assert(err, gc.IsNil)
 
682
        c.Assert(cacheEP, gc.DeepEquals, state.Endpoint{
683
683
                ServiceName: "wordpress",
684
684
                Relation: charm.Relation{
685
685
                        Interface: "varnish",
692
692
        })
693
693
 
694
694
        eps, err := wordpress.Endpoints()
695
 
        c.Assert(err, IsNil)
696
 
        c.Assert(eps, DeepEquals, []state.Endpoint{cacheEP, dbEP, jiEP, ldEP, mpEP, urlEP})
 
695
        c.Assert(err, gc.IsNil)
 
696
        c.Assert(eps, gc.DeepEquals, []state.Endpoint{cacheEP, dbEP, jiEP, ldEP, mpEP, urlEP})
697
697
}
698
698
 
699
 
func (s *ServiceSuite) TestServiceRefresh(c *C) {
 
699
func (s *ServiceSuite) TestServiceRefresh(c *gc.C) {
700
700
        s1, err := s.State.Service(s.mysql.Name())
701
 
        c.Assert(err, IsNil)
 
701
        c.Assert(err, gc.IsNil)
702
702
 
703
703
        err = s.mysql.SetCharm(s.charm, true)
704
 
        c.Assert(err, IsNil)
 
704
        c.Assert(err, gc.IsNil)
705
705
 
706
706
        testch, force, err := s1.Charm()
707
 
        c.Assert(err, IsNil)
708
 
        c.Assert(force, Equals, false)
709
 
        c.Assert(testch.URL(), DeepEquals, s.charm.URL())
 
707
        c.Assert(err, gc.IsNil)
 
708
        c.Assert(force, gc.Equals, false)
 
709
        c.Assert(testch.URL(), gc.DeepEquals, s.charm.URL())
710
710
 
711
711
        err = s1.Refresh()
712
 
        c.Assert(err, IsNil)
 
712
        c.Assert(err, gc.IsNil)
713
713
        testch, force, err = s1.Charm()
714
 
        c.Assert(err, IsNil)
715
 
        c.Assert(force, Equals, true)
716
 
        c.Assert(testch.URL(), DeepEquals, s.charm.URL())
 
714
        c.Assert(err, gc.IsNil)
 
715
        c.Assert(force, gc.Equals, true)
 
716
        c.Assert(testch.URL(), gc.DeepEquals, s.charm.URL())
717
717
 
718
718
        err = s.mysql.Destroy()
719
 
        c.Assert(err, IsNil)
 
719
        c.Assert(err, gc.IsNil)
720
720
        err = s.mysql.Refresh()
721
721
        c.Assert(err, checkers.Satisfies, errors.IsNotFoundError)
722
722
}
723
723
 
724
 
func (s *ServiceSuite) TestServiceExposed(c *C) {
 
724
func (s *ServiceSuite) TestServiceExposed(c *gc.C) {
725
725
        // Check that querying for the exposed flag works correctly.
726
 
        c.Assert(s.mysql.IsExposed(), Equals, false)
 
726
        c.Assert(s.mysql.IsExposed(), gc.Equals, false)
727
727
 
728
728
        // Check that setting and clearing the exposed flag works correctly.
729
729
        err := s.mysql.SetExposed()
730
 
        c.Assert(err, IsNil)
731
 
        c.Assert(s.mysql.IsExposed(), Equals, true)
 
730
        c.Assert(err, gc.IsNil)
 
731
        c.Assert(s.mysql.IsExposed(), gc.Equals, true)
732
732
        err = s.mysql.ClearExposed()
733
 
        c.Assert(err, IsNil)
734
 
        c.Assert(s.mysql.IsExposed(), Equals, false)
 
733
        c.Assert(err, gc.IsNil)
 
734
        c.Assert(s.mysql.IsExposed(), gc.Equals, false)
735
735
 
736
736
        // Check that setting and clearing the exposed flag repeatedly does not fail.
737
737
        err = s.mysql.SetExposed()
738
 
        c.Assert(err, IsNil)
739
 
        err = s.mysql.SetExposed()
740
 
        c.Assert(err, IsNil)
741
 
        err = s.mysql.ClearExposed()
742
 
        c.Assert(err, IsNil)
743
 
        err = s.mysql.ClearExposed()
744
 
        c.Assert(err, IsNil)
745
 
        err = s.mysql.SetExposed()
746
 
        c.Assert(err, IsNil)
747
 
        c.Assert(s.mysql.IsExposed(), Equals, true)
 
738
        c.Assert(err, gc.IsNil)
 
739
        err = s.mysql.SetExposed()
 
740
        c.Assert(err, gc.IsNil)
 
741
        err = s.mysql.ClearExposed()
 
742
        c.Assert(err, gc.IsNil)
 
743
        err = s.mysql.ClearExposed()
 
744
        c.Assert(err, gc.IsNil)
 
745
        err = s.mysql.SetExposed()
 
746
        c.Assert(err, gc.IsNil)
 
747
        c.Assert(s.mysql.IsExposed(), gc.Equals, true)
748
748
 
749
749
        // Make the service Dying and check that ClearExposed and SetExposed fail.
750
750
        // TODO(fwereade): maybe service destruction should always unexpose?
751
751
        u, err := s.mysql.AddUnit()
752
 
        c.Assert(err, IsNil)
 
752
        c.Assert(err, gc.IsNil)
753
753
        err = s.mysql.Destroy()
754
 
        c.Assert(err, IsNil)
 
754
        c.Assert(err, gc.IsNil)
755
755
        err = s.mysql.ClearExposed()
756
 
        c.Assert(err, ErrorMatches, notAliveErr)
 
756
        c.Assert(err, gc.ErrorMatches, notAliveErr)
757
757
        err = s.mysql.SetExposed()
758
 
        c.Assert(err, ErrorMatches, notAliveErr)
 
758
        c.Assert(err, gc.ErrorMatches, notAliveErr)
759
759
 
760
760
        // Remove the service and check that both fail.
761
761
        err = u.EnsureDead()
762
 
        c.Assert(err, IsNil)
 
762
        c.Assert(err, gc.IsNil)
763
763
        err = u.Remove()
764
 
        c.Assert(err, IsNil)
 
764
        c.Assert(err, gc.IsNil)
765
765
        err = s.mysql.SetExposed()
766
 
        c.Assert(err, ErrorMatches, notAliveErr)
 
766
        c.Assert(err, gc.ErrorMatches, notAliveErr)
767
767
        err = s.mysql.ClearExposed()
768
 
        c.Assert(err, ErrorMatches, notAliveErr)
 
768
        c.Assert(err, gc.ErrorMatches, notAliveErr)
769
769
}
770
770
 
771
 
func (s *ServiceSuite) TestAddUnit(c *C) {
 
771
func (s *ServiceSuite) TestAddUnit(c *gc.C) {
772
772
        // Check that principal units can be added on their own.
773
773
        unitZero, err := s.mysql.AddUnit()
774
 
        c.Assert(err, IsNil)
775
 
        c.Assert(unitZero.Name(), Equals, "mysql/0")
776
 
        c.Assert(unitZero.IsPrincipal(), Equals, true)
777
 
        c.Assert(unitZero.SubordinateNames(), HasLen, 0)
 
774
        c.Assert(err, gc.IsNil)
 
775
        c.Assert(unitZero.Name(), gc.Equals, "mysql/0")
 
776
        c.Assert(unitZero.IsPrincipal(), gc.Equals, true)
 
777
        c.Assert(unitZero.SubordinateNames(), gc.HasLen, 0)
778
778
        unitOne, err := s.mysql.AddUnit()
779
 
        c.Assert(err, IsNil)
780
 
        c.Assert(unitOne.Name(), Equals, "mysql/1")
781
 
        c.Assert(unitOne.IsPrincipal(), Equals, true)
782
 
        c.Assert(unitOne.SubordinateNames(), HasLen, 0)
 
779
        c.Assert(err, gc.IsNil)
 
780
        c.Assert(unitOne.Name(), gc.Equals, "mysql/1")
 
781
        c.Assert(unitOne.IsPrincipal(), gc.Equals, true)
 
782
        c.Assert(unitOne.SubordinateNames(), gc.HasLen, 0)
783
783
 
784
784
        // Assign the principal unit to a machine.
785
785
        m, err := s.State.AddMachine("series", state.JobHostUnits)
786
 
        c.Assert(err, IsNil)
 
786
        c.Assert(err, gc.IsNil)
787
787
        err = unitZero.AssignToMachine(m)
788
 
        c.Assert(err, IsNil)
 
788
        c.Assert(err, gc.IsNil)
789
789
 
790
790
        // Add a subordinate service and check that units cannot be added directly.
791
791
        // to add a subordinate unit.
792
792
        subCharm := s.AddTestingCharm(c, "logging")
793
793
        logging, err := s.State.AddService("logging", subCharm)
794
 
        c.Assert(err, IsNil)
 
794
        c.Assert(err, gc.IsNil)
795
795
        _, err = logging.AddUnit()
796
 
        c.Assert(err, ErrorMatches, `cannot add unit to service "logging": service is a subordinate`)
 
796
        c.Assert(err, gc.ErrorMatches, `cannot add unit to service "logging": service is a subordinate`)
797
797
 
798
798
        // Indirectly create a subordinate unit by adding a relation and entering
799
799
        // scope as a principal.
800
800
        eps, err := s.State.InferEndpoints([]string{"logging", "mysql"})
801
 
        c.Assert(err, IsNil)
 
801
        c.Assert(err, gc.IsNil)
802
802
        rel, err := s.State.AddRelation(eps...)
803
 
        c.Assert(err, IsNil)
 
803
        c.Assert(err, gc.IsNil)
804
804
        ru, err := rel.Unit(unitZero)
805
 
        c.Assert(err, IsNil)
 
805
        c.Assert(err, gc.IsNil)
806
806
        err = ru.EnterScope(nil)
807
 
        c.Assert(err, IsNil)
 
807
        c.Assert(err, gc.IsNil)
808
808
        subZero, err := s.State.Unit("logging/0")
809
 
        c.Assert(err, IsNil)
 
809
        c.Assert(err, gc.IsNil)
810
810
 
811
811
        // Check that once it's refreshed unitZero has subordinates.
812
812
        err = unitZero.Refresh()
813
 
        c.Assert(err, IsNil)
814
 
        c.Assert(unitZero.SubordinateNames(), DeepEquals, []string{"logging/0"})
 
813
        c.Assert(err, gc.IsNil)
 
814
        c.Assert(unitZero.SubordinateNames(), gc.DeepEquals, []string{"logging/0"})
815
815
 
816
816
        // Check the subordinate unit has been assigned its principal's machine.
817
817
        id, err := subZero.AssignedMachineId()
818
 
        c.Assert(err, IsNil)
819
 
        c.Assert(id, Equals, m.Id())
 
818
        c.Assert(err, gc.IsNil)
 
819
        c.Assert(id, gc.Equals, m.Id())
820
820
}
821
821
 
822
 
func (s *ServiceSuite) TestAddUnitWhenNotAlive(c *C) {
 
822
func (s *ServiceSuite) TestAddUnitWhenNotAlive(c *gc.C) {
823
823
        u, err := s.mysql.AddUnit()
824
 
        c.Assert(err, IsNil)
 
824
        c.Assert(err, gc.IsNil)
825
825
        err = s.mysql.Destroy()
826
 
        c.Assert(err, IsNil)
 
826
        c.Assert(err, gc.IsNil)
827
827
        _, err = s.mysql.AddUnit()
828
 
        c.Assert(err, ErrorMatches, `cannot add unit to service "mysql": service is not alive`)
 
828
        c.Assert(err, gc.ErrorMatches, `cannot add unit to service "mysql": service is not alive`)
829
829
        err = u.EnsureDead()
830
 
        c.Assert(err, IsNil)
 
830
        c.Assert(err, gc.IsNil)
831
831
        err = u.Remove()
832
 
        c.Assert(err, IsNil)
 
832
        c.Assert(err, gc.IsNil)
833
833
        _, err = s.mysql.AddUnit()
834
 
        c.Assert(err, ErrorMatches, `cannot add unit to service "mysql": service "mysql" not found`)
 
834
        c.Assert(err, gc.ErrorMatches, `cannot add unit to service "mysql": service "mysql" not found`)
835
835
}
836
836
 
837
 
func (s *ServiceSuite) TestReadUnit(c *C) {
 
837
func (s *ServiceSuite) TestReadUnit(c *gc.C) {
838
838
        _, err := s.mysql.AddUnit()
839
 
        c.Assert(err, IsNil)
 
839
        c.Assert(err, gc.IsNil)
840
840
        _, err = s.mysql.AddUnit()
841
 
        c.Assert(err, IsNil)
 
841
        c.Assert(err, gc.IsNil)
842
842
 
843
843
        // Check that retrieving a unit from the service works correctly.
844
844
        unit, err := s.mysql.Unit("mysql/0")
845
 
        c.Assert(err, IsNil)
846
 
        c.Assert(unit.Name(), Equals, "mysql/0")
 
845
        c.Assert(err, gc.IsNil)
 
846
        c.Assert(unit.Name(), gc.Equals, "mysql/0")
847
847
 
848
848
        // Check that retrieving a unit from state works correctly.
849
849
        unit, err = s.State.Unit("mysql/0")
850
 
        c.Assert(err, IsNil)
851
 
        c.Assert(unit.Name(), Equals, "mysql/0")
 
850
        c.Assert(err, gc.IsNil)
 
851
        c.Assert(unit.Name(), gc.Equals, "mysql/0")
852
852
 
853
853
        // Check that retrieving a non-existent or an invalidly
854
854
        // named unit fail nicely.
855
855
        unit, err = s.mysql.Unit("mysql")
856
 
        c.Assert(err, ErrorMatches, `"mysql" is not a valid unit name`)
 
856
        c.Assert(err, gc.ErrorMatches, `"mysql" is not a valid unit name`)
857
857
        unit, err = s.mysql.Unit("mysql/0/0")
858
 
        c.Assert(err, ErrorMatches, `"mysql/0/0" is not a valid unit name`)
 
858
        c.Assert(err, gc.ErrorMatches, `"mysql/0/0" is not a valid unit name`)
859
859
        unit, err = s.mysql.Unit("pressword/0")
860
 
        c.Assert(err, ErrorMatches, `cannot get unit "pressword/0" from service "mysql": .*`)
 
860
        c.Assert(err, gc.ErrorMatches, `cannot get unit "pressword/0" from service "mysql": .*`)
861
861
 
862
862
        // Check direct state retrieval also fails nicely.
863
863
        unit, err = s.State.Unit("mysql")
864
 
        c.Assert(err, ErrorMatches, `"mysql" is not a valid unit name`)
 
864
        c.Assert(err, gc.ErrorMatches, `"mysql" is not a valid unit name`)
865
865
        unit, err = s.State.Unit("mysql/0/0")
866
 
        c.Assert(err, ErrorMatches, `"mysql/0/0" is not a valid unit name`)
 
866
        c.Assert(err, gc.ErrorMatches, `"mysql/0/0" is not a valid unit name`)
867
867
        unit, err = s.State.Unit("pressword/0")
868
 
        c.Assert(err, ErrorMatches, `unit "pressword/0" not found`)
 
868
        c.Assert(err, gc.ErrorMatches, `unit "pressword/0" not found`)
869
869
 
870
870
        // Add another service to check units are not misattributed.
871
871
        mysql, err := s.State.AddService("wordpress", s.charm)
872
 
        c.Assert(err, IsNil)
 
872
        c.Assert(err, gc.IsNil)
873
873
        _, err = mysql.AddUnit()
874
 
        c.Assert(err, IsNil)
 
874
        c.Assert(err, gc.IsNil)
875
875
 
876
876
        // BUG(aram): use error strings from state.
877
877
        unit, err = s.mysql.Unit("wordpress/0")
878
 
        c.Assert(err, ErrorMatches, `cannot get unit "wordpress/0" from service "mysql": .*`)
 
878
        c.Assert(err, gc.ErrorMatches, `cannot get unit "wordpress/0" from service "mysql": .*`)
879
879
 
880
880
        units, err := s.mysql.AllUnits()
881
 
        c.Assert(err, IsNil)
882
 
        c.Assert(sortedUnitNames(units), DeepEquals, []string{"mysql/0", "mysql/1"})
 
881
        c.Assert(err, gc.IsNil)
 
882
        c.Assert(sortedUnitNames(units), gc.DeepEquals, []string{"mysql/0", "mysql/1"})
883
883
}
884
884
 
885
 
func (s *ServiceSuite) TestReadUnitWhenDying(c *C) {
 
885
func (s *ServiceSuite) TestReadUnitWhenDying(c *gc.C) {
886
886
        // Test that we can still read units when the service is Dying...
887
887
        unit, err := s.mysql.AddUnit()
888
 
        c.Assert(err, IsNil)
 
888
        c.Assert(err, gc.IsNil)
889
889
        preventUnitDestroyRemove(c, unit)
890
890
        err = s.mysql.Destroy()
891
 
        c.Assert(err, IsNil)
 
891
        c.Assert(err, gc.IsNil)
892
892
        _, err = s.mysql.AllUnits()
893
 
        c.Assert(err, IsNil)
 
893
        c.Assert(err, gc.IsNil)
894
894
        _, err = s.mysql.Unit("mysql/0")
895
 
        c.Assert(err, IsNil)
 
895
        c.Assert(err, gc.IsNil)
896
896
 
897
897
        // ...and when those units are Dying or Dead...
898
898
        testWhenDying(c, unit, noErr, noErr, func() error {
906
906
        // ...and even, in a very limited way, when the service itself is removed.
907
907
        removeAllUnits(c, s.mysql)
908
908
        _, err = s.mysql.AllUnits()
909
 
        c.Assert(err, IsNil)
 
909
        c.Assert(err, gc.IsNil)
910
910
}
911
911
 
912
 
func (s *ServiceSuite) TestDestroySimple(c *C) {
 
912
func (s *ServiceSuite) TestDestroySimple(c *gc.C) {
913
913
        err := s.mysql.Destroy()
914
 
        c.Assert(err, IsNil)
915
 
        c.Assert(s.mysql.Life(), Equals, state.Dying)
916
 
        err = s.mysql.Refresh()
917
 
        c.Assert(err, checkers.Satisfies, errors.IsNotFoundError)
918
 
}
919
 
 
920
 
func (s *ServiceSuite) TestDestroyStillHasUnits(c *C) {
921
 
        unit, err := s.mysql.AddUnit()
922
 
        c.Assert(err, IsNil)
923
 
        err = s.mysql.Destroy()
924
 
        c.Assert(err, IsNil)
925
 
        c.Assert(s.mysql.Life(), Equals, state.Dying)
926
 
 
927
 
        err = unit.EnsureDead()
928
 
        c.Assert(err, IsNil)
929
 
        err = s.mysql.Refresh()
930
 
        c.Assert(err, IsNil)
931
 
        c.Assert(s.mysql.Life(), Equals, state.Dying)
932
 
 
933
 
        err = unit.Remove()
934
 
        c.Assert(err, IsNil)
935
 
        err = s.mysql.Refresh()
936
 
        c.Assert(err, checkers.Satisfies, errors.IsNotFoundError)
937
 
}
938
 
 
939
 
func (s *ServiceSuite) TestDestroyOnceHadUnits(c *C) {
940
 
        unit, err := s.mysql.AddUnit()
941
 
        c.Assert(err, IsNil)
942
 
        err = unit.EnsureDead()
943
 
        c.Assert(err, IsNil)
944
 
        err = unit.Remove()
945
 
        c.Assert(err, IsNil)
946
 
 
947
 
        err = s.mysql.Destroy()
948
 
        c.Assert(err, IsNil)
949
 
        c.Assert(s.mysql.Life(), Equals, state.Dying)
950
 
        err = s.mysql.Refresh()
951
 
        c.Assert(err, checkers.Satisfies, errors.IsNotFoundError)
952
 
}
953
 
 
954
 
func (s *ServiceSuite) TestDestroyStaleNonZeroUnitCount(c *C) {
955
 
        unit, err := s.mysql.AddUnit()
956
 
        c.Assert(err, IsNil)
957
 
        err = s.mysql.Refresh()
958
 
        c.Assert(err, IsNil)
959
 
        err = unit.EnsureDead()
960
 
        c.Assert(err, IsNil)
961
 
        err = unit.Remove()
962
 
        c.Assert(err, IsNil)
963
 
 
964
 
        err = s.mysql.Destroy()
965
 
        c.Assert(err, IsNil)
966
 
        c.Assert(s.mysql.Life(), Equals, state.Dying)
967
 
        err = s.mysql.Refresh()
968
 
        c.Assert(err, checkers.Satisfies, errors.IsNotFoundError)
969
 
}
970
 
 
971
 
func (s *ServiceSuite) TestDestroyStaleZeroUnitCount(c *C) {
972
 
        unit, err := s.mysql.AddUnit()
973
 
        c.Assert(err, IsNil)
974
 
 
975
 
        err = s.mysql.Destroy()
976
 
        c.Assert(err, IsNil)
977
 
        c.Assert(s.mysql.Life(), Equals, state.Dying)
978
 
 
979
 
        err = s.mysql.Refresh()
980
 
        c.Assert(err, IsNil)
981
 
        c.Assert(s.mysql.Life(), Equals, state.Dying)
982
 
 
983
 
        err = unit.EnsureDead()
984
 
        c.Assert(err, IsNil)
985
 
        err = s.mysql.Refresh()
986
 
        c.Assert(err, IsNil)
987
 
        c.Assert(s.mysql.Life(), Equals, state.Dying)
988
 
 
989
 
        err = unit.Remove()
990
 
        c.Assert(err, IsNil)
991
 
        err = s.mysql.Refresh()
992
 
        c.Assert(err, checkers.Satisfies, errors.IsNotFoundError)
993
 
}
994
 
 
995
 
func (s *ServiceSuite) TestDestroyWithRemovableRelation(c *C) {
 
914
        c.Assert(err, gc.IsNil)
 
915
        c.Assert(s.mysql.Life(), gc.Equals, state.Dying)
 
916
        err = s.mysql.Refresh()
 
917
        c.Assert(err, checkers.Satisfies, errors.IsNotFoundError)
 
918
}
 
919
 
 
920
func (s *ServiceSuite) TestDestroyStillHasUnits(c *gc.C) {
 
921
        unit, err := s.mysql.AddUnit()
 
922
        c.Assert(err, gc.IsNil)
 
923
        err = s.mysql.Destroy()
 
924
        c.Assert(err, gc.IsNil)
 
925
        c.Assert(s.mysql.Life(), gc.Equals, state.Dying)
 
926
 
 
927
        err = unit.EnsureDead()
 
928
        c.Assert(err, gc.IsNil)
 
929
        err = s.mysql.Refresh()
 
930
        c.Assert(err, gc.IsNil)
 
931
        c.Assert(s.mysql.Life(), gc.Equals, state.Dying)
 
932
 
 
933
        err = unit.Remove()
 
934
        c.Assert(err, gc.IsNil)
 
935
        err = s.mysql.Refresh()
 
936
        c.Assert(err, checkers.Satisfies, errors.IsNotFoundError)
 
937
}
 
938
 
 
939
func (s *ServiceSuite) TestDestroyOnceHadUnits(c *gc.C) {
 
940
        unit, err := s.mysql.AddUnit()
 
941
        c.Assert(err, gc.IsNil)
 
942
        err = unit.EnsureDead()
 
943
        c.Assert(err, gc.IsNil)
 
944
        err = unit.Remove()
 
945
        c.Assert(err, gc.IsNil)
 
946
 
 
947
        err = s.mysql.Destroy()
 
948
        c.Assert(err, gc.IsNil)
 
949
        c.Assert(s.mysql.Life(), gc.Equals, state.Dying)
 
950
        err = s.mysql.Refresh()
 
951
        c.Assert(err, checkers.Satisfies, errors.IsNotFoundError)
 
952
}
 
953
 
 
954
func (s *ServiceSuite) TestDestroyStaleNonZeroUnitCount(c *gc.C) {
 
955
        unit, err := s.mysql.AddUnit()
 
956
        c.Assert(err, gc.IsNil)
 
957
        err = s.mysql.Refresh()
 
958
        c.Assert(err, gc.IsNil)
 
959
        err = unit.EnsureDead()
 
960
        c.Assert(err, gc.IsNil)
 
961
        err = unit.Remove()
 
962
        c.Assert(err, gc.IsNil)
 
963
 
 
964
        err = s.mysql.Destroy()
 
965
        c.Assert(err, gc.IsNil)
 
966
        c.Assert(s.mysql.Life(), gc.Equals, state.Dying)
 
967
        err = s.mysql.Refresh()
 
968
        c.Assert(err, checkers.Satisfies, errors.IsNotFoundError)
 
969
}
 
970
 
 
971
func (s *ServiceSuite) TestDestroyStaleZeroUnitCount(c *gc.C) {
 
972
        unit, err := s.mysql.AddUnit()
 
973
        c.Assert(err, gc.IsNil)
 
974
 
 
975
        err = s.mysql.Destroy()
 
976
        c.Assert(err, gc.IsNil)
 
977
        c.Assert(s.mysql.Life(), gc.Equals, state.Dying)
 
978
 
 
979
        err = s.mysql.Refresh()
 
980
        c.Assert(err, gc.IsNil)
 
981
        c.Assert(s.mysql.Life(), gc.Equals, state.Dying)
 
982
 
 
983
        err = unit.EnsureDead()
 
984
        c.Assert(err, gc.IsNil)
 
985
        err = s.mysql.Refresh()
 
986
        c.Assert(err, gc.IsNil)
 
987
        c.Assert(s.mysql.Life(), gc.Equals, state.Dying)
 
988
 
 
989
        err = unit.Remove()
 
990
        c.Assert(err, gc.IsNil)
 
991
        err = s.mysql.Refresh()
 
992
        c.Assert(err, checkers.Satisfies, errors.IsNotFoundError)
 
993
}
 
994
 
 
995
func (s *ServiceSuite) TestDestroyWithRemovableRelation(c *gc.C) {
996
996
        wordpress, err := s.State.AddService("wordpress", s.AddTestingCharm(c, "wordpress"))
997
 
        c.Assert(err, IsNil)
 
997
        c.Assert(err, gc.IsNil)
998
998
        eps, err := s.State.InferEndpoints([]string{"wordpress", "mysql"})
999
 
        c.Assert(err, IsNil)
 
999
        c.Assert(err, gc.IsNil)
1000
1000
        rel, err := s.State.AddRelation(eps...)
1001
 
        c.Assert(err, IsNil)
 
1001
        c.Assert(err, gc.IsNil)
1002
1002
 
1003
1003
        // Destroy a service with no units in relation scope; check service and
1004
1004
        // unit removed.
1005
1005
        err = wordpress.Destroy()
1006
 
        c.Assert(err, IsNil)
 
1006
        c.Assert(err, gc.IsNil)
1007
1007
        err = wordpress.Refresh()
1008
1008
        c.Assert(err, checkers.Satisfies, errors.IsNotFoundError)
1009
1009
        err = rel.Refresh()
1010
1010
        c.Assert(err, checkers.Satisfies, errors.IsNotFoundError)
1011
1011
}
1012
1012
 
1013
 
func (s *ServiceSuite) TestDestroyWithReferencedRelation(c *C) {
 
1013
func (s *ServiceSuite) TestDestroyWithReferencedRelation(c *gc.C) {
1014
1014
        s.assertDestroyWithReferencedRelation(c, true)
1015
1015
}
1016
1016
 
1017
 
func (s *ServiceSuite) TestDestroyWithreferencedRelationStaleCount(c *C) {
 
1017
func (s *ServiceSuite) TestDestroyWithreferencedRelationStaleCount(c *gc.C) {
1018
1018
        s.assertDestroyWithReferencedRelation(c, false)
1019
1019
}
1020
1020
 
1021
 
func (s *ServiceSuite) assertDestroyWithReferencedRelation(c *C, refresh bool) {
 
1021
func (s *ServiceSuite) assertDestroyWithReferencedRelation(c *gc.C, refresh bool) {
1022
1022
        wordpress, err := s.State.AddService("wordpress", s.AddTestingCharm(c, "wordpress"))
1023
 
        c.Assert(err, IsNil)
 
1023
        c.Assert(err, gc.IsNil)
1024
1024
        eps, err := s.State.InferEndpoints([]string{"wordpress", "mysql"})
1025
 
        c.Assert(err, IsNil)
 
1025
        c.Assert(err, gc.IsNil)
1026
1026
        rel0, err := s.State.AddRelation(eps...)
1027
 
        c.Assert(err, IsNil)
 
1027
        c.Assert(err, gc.IsNil)
1028
1028
 
1029
1029
        _, err = s.State.AddService("logging", s.AddTestingCharm(c, "logging"))
1030
 
        c.Assert(err, IsNil)
 
1030
        c.Assert(err, gc.IsNil)
1031
1031
        eps, err = s.State.InferEndpoints([]string{"logging", "mysql"})
1032
 
        c.Assert(err, IsNil)
 
1032
        c.Assert(err, gc.IsNil)
1033
1033
        rel1, err := s.State.AddRelation(eps...)
1034
 
        c.Assert(err, IsNil)
 
1034
        c.Assert(err, gc.IsNil)
1035
1035
 
1036
1036
        // Add a separate reference to the first relation.
1037
1037
        unit, err := wordpress.AddUnit()
1038
 
        c.Assert(err, IsNil)
 
1038
        c.Assert(err, gc.IsNil)
1039
1039
        ru, err := rel0.Unit(unit)
1040
 
        c.Assert(err, IsNil)
 
1040
        c.Assert(err, gc.IsNil)
1041
1041
        err = ru.EnterScope(nil)
1042
 
        c.Assert(err, IsNil)
 
1042
        c.Assert(err, gc.IsNil)
1043
1043
 
1044
1044
        // Optionally update the service document to get correct relation counts.
1045
1045
        if refresh {
1046
1046
                err = s.mysql.Destroy()
1047
 
                c.Assert(err, IsNil)
 
1047
                c.Assert(err, gc.IsNil)
1048
1048
        }
1049
1049
 
1050
1050
        // Destroy, and check that the first relation becomes Dying...
1051
1051
        err = s.mysql.Destroy()
1052
 
        c.Assert(err, IsNil)
 
1052
        c.Assert(err, gc.IsNil)
1053
1053
        err = rel0.Refresh()
1054
 
        c.Assert(err, IsNil)
1055
 
        c.Assert(rel0.Life(), Equals, state.Dying)
 
1054
        c.Assert(err, gc.IsNil)
 
1055
        c.Assert(rel0.Life(), gc.Equals, state.Dying)
1056
1056
 
1057
1057
        // ...while the second is removed directly.
1058
1058
        err = rel1.Refresh()
1061
1061
        // Drop the last reference to the first relation; check the relation and
1062
1062
        // the service are are both removed.
1063
1063
        err = ru.LeaveScope()
1064
 
        c.Assert(err, IsNil)
 
1064
        c.Assert(err, gc.IsNil)
1065
1065
        err = s.mysql.Refresh()
1066
1066
        c.Assert(err, checkers.Satisfies, errors.IsNotFoundError)
1067
1067
        err = rel0.Refresh()
1068
1068
        c.Assert(err, checkers.Satisfies, errors.IsNotFoundError)
1069
1069
}
1070
1070
 
1071
 
func (s *ServiceSuite) TestDestroyQueuesUnitCleanup(c *C) {
 
1071
func (s *ServiceSuite) TestDestroyQueuesUnitCleanup(c *gc.C) {
1072
1072
        // Add 5 units; block quick-remove of mysql/1 and mysql/3
1073
1073
        units := make([]*state.Unit, 5)
1074
1074
        for i := range units {
1075
1075
                unit, err := s.mysql.AddUnit()
1076
 
                c.Assert(err, IsNil)
 
1076
                c.Assert(err, gc.IsNil)
1077
1077
                units[i] = unit
1078
1078
                if i%2 != 0 {
1079
1079
                        preventUnitDestroyRemove(c, unit)
1082
1082
 
1083
1083
        // Check state is clean.
1084
1084
        dirty, err := s.State.NeedsCleanup()
1085
 
        c.Assert(err, IsNil)
1086
 
        c.Assert(dirty, Equals, false)
 
1085
        c.Assert(err, gc.IsNil)
 
1086
        c.Assert(dirty, gc.Equals, false)
1087
1087
 
1088
1088
        // Destroy mysql, and check units are not touched.
1089
1089
        err = s.mysql.Destroy()
1090
 
        c.Assert(err, IsNil)
 
1090
        c.Assert(err, gc.IsNil)
1091
1091
        for _, unit := range units {
1092
1092
                assertUnitLife(c, unit, state.Alive)
1093
1093
        }
1094
1094
 
1095
1095
        // Check a cleanup doc was added.
1096
1096
        dirty, err = s.State.NeedsCleanup()
1097
 
        c.Assert(err, IsNil)
1098
 
        c.Assert(dirty, Equals, true)
 
1097
        c.Assert(err, gc.IsNil)
 
1098
        c.Assert(dirty, gc.Equals, true)
1099
1099
 
1100
1100
        // Run the cleanup and check the units.
1101
1101
        err = s.State.Cleanup()
1102
 
        c.Assert(err, IsNil)
 
1102
        c.Assert(err, gc.IsNil)
1103
1103
        for i, unit := range units {
1104
1104
                if i%2 != 0 {
1105
1105
                        assertUnitLife(c, unit, state.Dying)
1110
1110
 
1111
1111
        // Check we're now clean.
1112
1112
        dirty, err = s.State.NeedsCleanup()
1113
 
        c.Assert(err, IsNil)
1114
 
        c.Assert(dirty, Equals, false)
 
1113
        c.Assert(err, gc.IsNil)
 
1114
        c.Assert(dirty, gc.Equals, false)
1115
1115
}
1116
1116
 
1117
 
func (s *ServiceSuite) TestReadUnitWithChangingState(c *C) {
 
1117
func (s *ServiceSuite) TestReadUnitWithChangingState(c *gc.C) {
1118
1118
        // Check that reading a unit after removing the service
1119
1119
        // fails nicely.
1120
1120
        err := s.mysql.Destroy()
1121
 
        c.Assert(err, IsNil)
 
1121
        c.Assert(err, gc.IsNil)
1122
1122
        err = s.mysql.Refresh()
1123
1123
        c.Assert(err, checkers.Satisfies, errors.IsNotFoundError)
1124
1124
        _, err = s.State.Unit("mysql/0")
1125
 
        c.Assert(err, ErrorMatches, `unit "mysql/0" not found`)
 
1125
        c.Assert(err, gc.ErrorMatches, `unit "mysql/0" not found`)
1126
1126
}
1127
1127
 
1128
1128
func uint64p(val uint64) *uint64 {
1129
1129
        return &val
1130
1130
}
1131
1131
 
1132
 
func (s *ServiceSuite) TestConstraints(c *C) {
 
1132
func (s *ServiceSuite) TestConstraints(c *gc.C) {
1133
1133
        // Constraints are initially empty (for now).
1134
1134
        cons0 := constraints.Value{}
1135
1135
        cons1, err := s.mysql.Constraints()
1136
 
        c.Assert(err, IsNil)
1137
 
        c.Assert(cons1, DeepEquals, cons0)
 
1136
        c.Assert(err, gc.IsNil)
 
1137
        c.Assert(cons1, gc.DeepEquals, cons0)
1138
1138
 
1139
1139
        // Constraints can be set.
1140
1140
        cons2 := constraints.Value{Mem: uint64p(4096)}
1141
1141
        err = s.mysql.SetConstraints(cons2)
1142
1142
        cons3, err := s.mysql.Constraints()
1143
 
        c.Assert(err, IsNil)
1144
 
        c.Assert(cons3, DeepEquals, cons2)
 
1143
        c.Assert(err, gc.IsNil)
 
1144
        c.Assert(cons3, gc.DeepEquals, cons2)
1145
1145
 
1146
1146
        // Constraints are completely overwritten when re-set.
1147
1147
        cons4 := constraints.Value{CpuPower: uint64p(750)}
1148
1148
        err = s.mysql.SetConstraints(cons4)
1149
 
        c.Assert(err, IsNil)
 
1149
        c.Assert(err, gc.IsNil)
1150
1150
        cons5, err := s.mysql.Constraints()
1151
 
        c.Assert(err, IsNil)
1152
 
        c.Assert(cons5, DeepEquals, cons4)
 
1151
        c.Assert(err, gc.IsNil)
 
1152
        c.Assert(cons5, gc.DeepEquals, cons4)
1153
1153
 
1154
1154
        // Destroy the existing service; there's no way to directly assert
1155
1155
        // that the constraints are deleted...
1156
1156
        err = s.mysql.Destroy()
1157
 
        c.Assert(err, IsNil)
 
1157
        c.Assert(err, gc.IsNil)
1158
1158
        err = s.mysql.Refresh()
1159
1159
        c.Assert(err, checkers.Satisfies, errors.IsNotFoundError)
1160
1160
 
1161
1161
        // ...but we can check that old constraints do not affect new services
1162
1162
        // with matching names.
1163
1163
        ch, _, err := s.mysql.Charm()
1164
 
        c.Assert(err, IsNil)
 
1164
        c.Assert(err, gc.IsNil)
1165
1165
        mysql, err := s.State.AddService(s.mysql.Name(), ch)
1166
 
        c.Assert(err, IsNil)
 
1166
        c.Assert(err, gc.IsNil)
1167
1167
        cons6, err := mysql.Constraints()
1168
 
        c.Assert(err, IsNil)
1169
 
        c.Assert(cons6, DeepEquals, cons0)
 
1168
        c.Assert(err, gc.IsNil)
 
1169
        c.Assert(cons6, gc.DeepEquals, cons0)
1170
1170
}
1171
1171
 
1172
 
func (s *ServiceSuite) TestConstraintsLifecycle(c *C) {
 
1172
func (s *ServiceSuite) TestConstraintsLifecycle(c *gc.C) {
1173
1173
        // Dying.
1174
1174
        unit, err := s.mysql.AddUnit()
1175
 
        c.Assert(err, IsNil)
 
1175
        c.Assert(err, gc.IsNil)
1176
1176
        err = s.mysql.Destroy()
1177
 
        c.Assert(err, IsNil)
 
1177
        c.Assert(err, gc.IsNil)
1178
1178
        cons1 := constraints.MustParse("mem=1G")
1179
1179
        err = s.mysql.SetConstraints(cons1)
1180
 
        c.Assert(err, ErrorMatches, `cannot set constraints: not found or not alive`)
 
1180
        c.Assert(err, gc.ErrorMatches, `cannot set constraints: not found or not alive`)
1181
1181
        scons, err := s.mysql.Constraints()
1182
 
        c.Assert(err, IsNil)
1183
 
        c.Assert(scons, DeepEquals, constraints.Value{})
 
1182
        c.Assert(err, gc.IsNil)
 
1183
        c.Assert(scons, gc.DeepEquals, constraints.Value{})
1184
1184
 
1185
1185
        // Removed (== Dead, for a service).
1186
1186
        err = unit.EnsureDead()
1187
 
        c.Assert(err, IsNil)
 
1187
        c.Assert(err, gc.IsNil)
1188
1188
        err = unit.Remove()
1189
 
        c.Assert(err, IsNil)
 
1189
        c.Assert(err, gc.IsNil)
1190
1190
        err = s.mysql.SetConstraints(cons1)
1191
 
        c.Assert(err, ErrorMatches, `cannot set constraints: not found or not alive`)
 
1191
        c.Assert(err, gc.ErrorMatches, `cannot set constraints: not found or not alive`)
1192
1192
        _, err = s.mysql.Constraints()
1193
 
        c.Assert(err, ErrorMatches, `constraints not found`)
 
1193
        c.Assert(err, gc.ErrorMatches, `constraints not found`)
1194
1194
}
1195
1195
 
1196
 
func (s *ServiceSuite) TestSubordinateConstraints(c *C) {
 
1196
func (s *ServiceSuite) TestSubordinateConstraints(c *gc.C) {
1197
1197
        loggingCh := s.AddTestingCharm(c, "logging")
1198
1198
        logging, err := s.State.AddService("logging", loggingCh)
1199
 
        c.Assert(err, IsNil)
 
1199
        c.Assert(err, gc.IsNil)
1200
1200
 
1201
1201
        _, err = logging.Constraints()
1202
 
        c.Assert(err, Equals, state.ErrSubordinateConstraints)
 
1202
        c.Assert(err, gc.Equals, state.ErrSubordinateConstraints)
1203
1203
 
1204
1204
        err = logging.SetConstraints(constraints.Value{})
1205
 
        c.Assert(err, Equals, state.ErrSubordinateConstraints)
 
1205
        c.Assert(err, gc.Equals, state.ErrSubordinateConstraints)
1206
1206
}
1207
1207
 
1208
 
func (s *ServiceSuite) TestWatchUnitsBulkEvents(c *C) {
 
1208
func (s *ServiceSuite) TestWatchUnitsBulkEvents(c *gc.C) {
1209
1209
        // Alive unit...
1210
1210
        alive, err := s.mysql.AddUnit()
1211
 
        c.Assert(err, IsNil)
 
1211
        c.Assert(err, gc.IsNil)
1212
1212
 
1213
1213
        // Dying unit...
1214
1214
        dying, err := s.mysql.AddUnit()
1215
 
        c.Assert(err, IsNil)
 
1215
        c.Assert(err, gc.IsNil)
1216
1216
        preventUnitDestroyRemove(c, dying)
1217
1217
        err = dying.Destroy()
1218
 
        c.Assert(err, IsNil)
 
1218
        c.Assert(err, gc.IsNil)
1219
1219
 
1220
1220
        // Dead unit...
1221
1221
        dead, err := s.mysql.AddUnit()
1222
 
        c.Assert(err, IsNil)
 
1222
        c.Assert(err, gc.IsNil)
1223
1223
        preventUnitDestroyRemove(c, dead)
1224
1224
        err = dead.Destroy()
1225
 
        c.Assert(err, IsNil)
 
1225
        c.Assert(err, gc.IsNil)
1226
1226
        err = dead.EnsureDead()
1227
 
        c.Assert(err, IsNil)
 
1227
        c.Assert(err, gc.IsNil)
1228
1228
 
1229
1229
        // Gone unit.
1230
1230
        gone, err := s.mysql.AddUnit()
1231
 
        c.Assert(err, IsNil)
 
1231
        c.Assert(err, gc.IsNil)
1232
1232
        err = gone.Destroy()
1233
 
        c.Assert(err, IsNil)
 
1233
        c.Assert(err, gc.IsNil)
1234
1234
 
1235
1235
        // All except gone unit are reported in initial event.
1236
1236
        w := s.mysql.WatchUnits()
1241
1241
 
1242
1242
        // Remove them all; alive/dying changes reported; dead never mentioned again.
1243
1243
        err = alive.Destroy()
1244
 
        c.Assert(err, IsNil)
 
1244
        c.Assert(err, gc.IsNil)
1245
1245
        err = dying.EnsureDead()
1246
 
        c.Assert(err, IsNil)
 
1246
        c.Assert(err, gc.IsNil)
1247
1247
        err = dying.Remove()
1248
 
        c.Assert(err, IsNil)
 
1248
        c.Assert(err, gc.IsNil)
1249
1249
        err = dead.Remove()
1250
 
        c.Assert(err, IsNil)
 
1250
        c.Assert(err, gc.IsNil)
1251
1251
        wc.AssertChange(alive.Name(), dying.Name())
1252
1252
        wc.AssertNoChange()
1253
1253
}
1254
1254
 
1255
 
func (s *ServiceSuite) TestWatchUnitsLifecycle(c *C) {
 
1255
func (s *ServiceSuite) TestWatchUnitsLifecycle(c *gc.C) {
1256
1256
        // Empty initial event when no units.
1257
1257
        w := s.mysql.WatchUnits()
1258
1258
        defer testing.AssertStop(c, w)
1262
1262
 
1263
1263
        // Create one unit, check one change.
1264
1264
        quick, err := s.mysql.AddUnit()
1265
 
        c.Assert(err, IsNil)
 
1265
        c.Assert(err, gc.IsNil)
1266
1266
        wc.AssertChange(quick.Name())
1267
1267
        wc.AssertNoChange()
1268
1268
 
1269
1269
        // Destroy that unit (short-circuited to removal), check one change.
1270
1270
        err = quick.Destroy()
1271
 
        c.Assert(err, IsNil)
 
1271
        c.Assert(err, gc.IsNil)
1272
1272
        wc.AssertChange(quick.Name())
1273
1273
        wc.AssertNoChange()
1274
1274
 
1275
1275
        // Create another, check one change.
1276
1276
        slow, err := s.mysql.AddUnit()
1277
 
        c.Assert(err, IsNil)
 
1277
        c.Assert(err, gc.IsNil)
1278
1278
        wc.AssertChange(slow.Name())
1279
1279
        wc.AssertNoChange()
1280
1280
 
1284
1284
 
1285
1285
        // Make unit Dying, change detected.
1286
1286
        err = slow.Destroy()
1287
 
        c.Assert(err, IsNil)
 
1287
        c.Assert(err, gc.IsNil)
1288
1288
        wc.AssertChange(slow.Name())
1289
1289
        wc.AssertNoChange()
1290
1290
 
1291
1291
        // Make unit Dead, change detected.
1292
1292
        err = slow.EnsureDead()
1293
 
        c.Assert(err, IsNil)
 
1293
        c.Assert(err, gc.IsNil)
1294
1294
        wc.AssertChange(slow.Name())
1295
1295
        wc.AssertNoChange()
1296
1296
 
1297
1297
        // Remove unit, final change not detected.
1298
1298
        err = slow.Remove()
1299
 
        c.Assert(err, IsNil)
 
1299
        c.Assert(err, gc.IsNil)
1300
1300
        wc.AssertNoChange()
1301
1301
}
1302
1302
 
1303
 
func (s *ServiceSuite) TestWatchRelations(c *C) {
 
1303
func (s *ServiceSuite) TestWatchRelations(c *gc.C) {
1304
1304
        // TODO(fwereade) split this test up a bit.
1305
1305
        w := s.mysql.WatchRelations()
1306
1306
        defer testing.AssertStop(c, w)
1310
1310
 
1311
1311
        // Add a relation; check change.
1312
1312
        mysqlep, err := s.mysql.Endpoint("server")
1313
 
        c.Assert(err, IsNil)
 
1313
        c.Assert(err, gc.IsNil)
1314
1314
        wpch := s.AddTestingCharm(c, "wordpress")
1315
1315
        wpi := 0
1316
1316
        addRelation := func() *state.Relation {
1317
1317
                name := fmt.Sprintf("wp%d", wpi)
1318
1318
                wpi++
1319
1319
                wp, err := s.State.AddService(name, wpch)
1320
 
                c.Assert(err, IsNil)
 
1320
                c.Assert(err, gc.IsNil)
1321
1321
                wpep, err := wp.Endpoint("db")
1322
 
                c.Assert(err, IsNil)
 
1322
                c.Assert(err, gc.IsNil)
1323
1323
                rel, err := s.State.AddRelation(mysqlep, wpep)
1324
 
                c.Assert(err, IsNil)
 
1324
                c.Assert(err, gc.IsNil)
1325
1325
                return rel
1326
1326
        }
1327
1327
        rel0 := addRelation()
1335
1335
 
1336
1336
        // Destroy a relation; check change.
1337
1337
        err = rel0.Destroy()
1338
 
        c.Assert(err, IsNil)
 
1338
        c.Assert(err, gc.IsNil)
1339
1339
        wc.AssertChange(rel0.String())
1340
1340
        wc.AssertNoChange()
1341
1341
 
1353
1353
 
1354
1354
        // Add a unit to the new relation; check no change.
1355
1355
        unit, err := s.mysql.AddUnit()
1356
 
        c.Assert(err, IsNil)
 
1356
        c.Assert(err, gc.IsNil)
1357
1357
        ru2, err := rel2.Unit(unit)
1358
 
        c.Assert(err, IsNil)
 
1358
        c.Assert(err, gc.IsNil)
1359
1359
        err = ru2.EnterScope(nil)
1360
 
        c.Assert(err, IsNil)
 
1360
        c.Assert(err, gc.IsNil)
1361
1361
        wc.AssertNoChange()
1362
1362
 
1363
1363
        // Destroy the relation with the unit in scope, and add another; check
1364
1364
        // changes.
1365
1365
        err = rel2.Destroy()
1366
 
        c.Assert(err, IsNil)
 
1366
        c.Assert(err, gc.IsNil)
1367
1367
        rel3 := addRelation()
1368
1368
        wc.AssertChange(rel2.String(), rel3.String())
1369
1369
        wc.AssertNoChange()
1370
1370
 
1371
1371
        // Leave scope, destroying the relation, and check that change as well.
1372
1372
        err = ru2.LeaveScope()
1373
 
        c.Assert(err, IsNil)
 
1373
        c.Assert(err, gc.IsNil)
1374
1374
        wc.AssertChange(rel2.String())
1375
1375
        wc.AssertNoChange()
1376
1376
}
1377
1377
 
1378
 
func removeAllUnits(c *C, s *state.Service) {
 
1378
func removeAllUnits(c *gc.C, s *state.Service) {
1379
1379
        us, err := s.AllUnits()
1380
 
        c.Assert(err, IsNil)
 
1380
        c.Assert(err, gc.IsNil)
1381
1381
        for _, u := range us {
1382
1382
                err = u.EnsureDead()
1383
 
                c.Assert(err, IsNil)
 
1383
                c.Assert(err, gc.IsNil)
1384
1384
                err = u.Remove()
1385
 
                c.Assert(err, IsNil)
 
1385
                c.Assert(err, gc.IsNil)
1386
1386
        }
1387
1387
}
1388
1388
 
1389
 
func (s *ServiceSuite) TestWatchService(c *C) {
 
1389
func (s *ServiceSuite) TestWatchService(c *gc.C) {
1390
1390
        w := s.mysql.Watch()
1391
1391
        defer testing.AssertStop(c, w)
1392
1392
 
1396
1396
 
1397
1397
        // Make one change (to a separate instance), check one event.
1398
1398
        service, err := s.State.Service(s.mysql.Name())
1399
 
        c.Assert(err, IsNil)
 
1399
        c.Assert(err, gc.IsNil)
1400
1400
        err = service.SetExposed()
1401
 
        c.Assert(err, IsNil)
 
1401
        c.Assert(err, gc.IsNil)
1402
1402
        wc.AssertOneChange()
1403
1403
 
1404
1404
        // Make two changes, check one event.
1405
1405
        err = service.ClearExposed()
1406
 
        c.Assert(err, IsNil)
 
1406
        c.Assert(err, gc.IsNil)
1407
1407
        err = service.SetCharm(s.charm, true)
1408
 
        c.Assert(err, IsNil)
 
1408
        c.Assert(err, gc.IsNil)
1409
1409
        wc.AssertOneChange()
1410
1410
 
1411
1411
        // Stop, check closed.
1414
1414
 
1415
1415
        // Remove service, start new watch, check single event.
1416
1416
        err = service.Destroy()
1417
 
        c.Assert(err, IsNil)
 
1417
        c.Assert(err, gc.IsNil)
1418
1418
        w = s.mysql.Watch()
1419
1419
        defer testing.AssertStop(c, w)
1420
1420
        testing.NewNotifyWatcherC(c, s.State, w).AssertOneChange()
1421
1421
}
1422
1422
 
1423
 
func (s *ServiceSuite) TestAnnotatorForService(c *C) {
 
1423
func (s *ServiceSuite) TestAnnotatorForService(c *gc.C) {
1424
1424
        testAnnotator(c, func() (state.Annotator, error) {
1425
1425
                return s.State.Service("mysql")
1426
1426
        })
1427
1427
}
1428
1428
 
1429
 
func (s *ServiceSuite) TestAnnotationRemovalForService(c *C) {
 
1429
func (s *ServiceSuite) TestAnnotationRemovalForService(c *gc.C) {
1430
1430
        annotations := map[string]string{"mykey": "myvalue"}
1431
1431
        err := s.mysql.SetAnnotations(annotations)
1432
 
        c.Assert(err, IsNil)
 
1432
        c.Assert(err, gc.IsNil)
1433
1433
        err = s.mysql.Destroy()
1434
 
        c.Assert(err, IsNil)
 
1434
        c.Assert(err, gc.IsNil)
1435
1435
        ann, err := s.mysql.Annotations()
1436
 
        c.Assert(err, IsNil)
1437
 
        c.Assert(ann, DeepEquals, make(map[string]string))
 
1436
        c.Assert(err, gc.IsNil)
 
1437
        c.Assert(ann, gc.DeepEquals, make(map[string]string))
1438
1438
}