LEFT | RIGHT |
1 package state_test | 1 package state_test |
2 | 2 |
3 import ( | 3 import ( |
4 "fmt" | 4 "fmt" |
5 . "launchpad.net/gocheck" | 5 . "launchpad.net/gocheck" |
6 "launchpad.net/juju-core/charm" | 6 "launchpad.net/juju-core/charm" |
7 "launchpad.net/juju-core/state" | 7 "launchpad.net/juju-core/state" |
8 "sort" | 8 "sort" |
9 "time" | 9 "time" |
10 ) | 10 ) |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 func jujuInfoEp(serviceName string) state.Endpoint { | 57 func jujuInfoEp(serviceName string) state.Endpoint { |
58 return state.Endpoint{ | 58 return state.Endpoint{ |
59 ServiceName: serviceName, | 59 ServiceName: serviceName, |
60 Interface: "juju-info", | 60 Interface: "juju-info", |
61 RelationName: "juju-info", | 61 RelationName: "juju-info", |
62 RelationRole: state.RoleProvider, | 62 RelationRole: state.RoleProvider, |
63 RelationScope: charm.ScopeGlobal, | 63 RelationScope: charm.ScopeGlobal, |
64 } | 64 } |
65 } | 65 } |
66 | 66 |
| 67 func (s *ServiceSuite) TestEntityName(c *C) { |
| 68 c.Assert(s.mysql.EntityName(), Equals, "service-mysql") |
| 69 } |
| 70 |
67 func (s *ServiceSuite) TestMysqlEndpoints(c *C) { | 71 func (s *ServiceSuite) TestMysqlEndpoints(c *C) { |
68 _, err := s.mysql.Endpoint("mysql") | 72 _, err := s.mysql.Endpoint("mysql") |
69 c.Assert(err, ErrorMatches, `service "mysql" has no "mysql" relation`) | 73 c.Assert(err, ErrorMatches, `service "mysql" has no "mysql" relation`) |
70 | 74 |
71 jiEP, err := s.mysql.Endpoint("juju-info") | 75 jiEP, err := s.mysql.Endpoint("juju-info") |
72 c.Assert(err, IsNil) | 76 c.Assert(err, IsNil) |
73 c.Assert(jiEP, DeepEquals, jujuInfoEp("mysql")) | 77 c.Assert(jiEP, DeepEquals, jujuInfoEp("mysql")) |
74 | 78 |
75 serverEP, err := s.mysql.Endpoint("server") | 79 serverEP, err := s.mysql.Endpoint("server") |
76 c.Assert(err, IsNil) | 80 c.Assert(err, IsNil) |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 _, err := s.mysql.Unit("mysql/0") | 398 _, err := s.mysql.Unit("mysql/0") |
395 return err | 399 return err |
396 }) | 400 }) |
397 | 401 |
398 // ...and even, in a very limited way, when the service itself is remove
d. | 402 // ...and even, in a very limited way, when the service itself is remove
d. |
399 removeAllUnits(c, s.mysql) | 403 removeAllUnits(c, s.mysql) |
400 _, err = s.mysql.AllUnits() | 404 _, err = s.mysql.AllUnits() |
401 c.Assert(err, IsNil) | 405 c.Assert(err, IsNil) |
402 } | 406 } |
403 | 407 |
404 func (s *ServiceSuite) TestLifeWithUnits(c *C) { | 408 func (s *ServiceSuite) TestDestroySimple(c *C) { |
| 409 » err := s.mysql.Destroy() |
| 410 » c.Assert(err, IsNil) |
| 411 » c.Assert(s.mysql.Life(), Equals, state.Dying) |
| 412 » err = s.mysql.Refresh() |
| 413 » c.Assert(state.IsNotFound(err), Equals, true) |
| 414 } |
| 415 |
| 416 func (s *ServiceSuite) TestDestroyStillHasUnits(c *C) { |
405 unit, err := s.mysql.AddUnit() | 417 unit, err := s.mysql.AddUnit() |
406 c.Assert(err, IsNil) | 418 c.Assert(err, IsNil) |
407 err = s.mysql.Destroy() | 419 err = s.mysql.Destroy() |
408 c.Assert(err, IsNil) | 420 c.Assert(err, IsNil) |
| 421 c.Assert(s.mysql.Life(), Equals, state.Dying) |
| 422 |
409 err = unit.EnsureDead() | 423 err = unit.EnsureDead() |
410 c.Assert(err, IsNil) | 424 c.Assert(err, IsNil) |
411 err = s.mysql.Refresh() | 425 err = s.mysql.Refresh() |
412 c.Assert(err, IsNil) | 426 c.Assert(err, IsNil) |
| 427 c.Assert(s.mysql.Life(), Equals, state.Dying) |
| 428 |
413 err = unit.Remove() | 429 err = unit.Remove() |
414 c.Assert(err, IsNil) | 430 c.Assert(err, IsNil) |
415 err = s.mysql.Refresh() | 431 err = s.mysql.Refresh() |
416 c.Assert(state.IsNotFound(err), Equals, true) | 432 c.Assert(state.IsNotFound(err), Equals, true) |
417 } | 433 } |
418 | 434 |
419 func (s *ServiceSuite) TestLifeWithRemovableRelations(c *C) { | 435 func (s *ServiceSuite) TestDestroyOnceHadUnits(c *C) { |
| 436 » unit, err := s.mysql.AddUnit() |
| 437 » c.Assert(err, IsNil) |
| 438 » err = unit.EnsureDead() |
| 439 » c.Assert(err, IsNil) |
| 440 » err = unit.Remove() |
| 441 » c.Assert(err, IsNil) |
| 442 |
| 443 » err = s.mysql.Destroy() |
| 444 » c.Assert(err, IsNil) |
| 445 » c.Assert(s.mysql.Life(), Equals, state.Dying) |
| 446 » err = s.mysql.Refresh() |
| 447 » c.Assert(state.IsNotFound(err), Equals, true) |
| 448 } |
| 449 |
| 450 func (s *ServiceSuite) TestDestroyStaleNonZeroUnitCount(c *C) { |
| 451 » unit, err := s.mysql.AddUnit() |
| 452 » c.Assert(err, IsNil) |
| 453 » err = s.mysql.Refresh() |
| 454 » c.Assert(err, IsNil) |
| 455 » err = unit.EnsureDead() |
| 456 » c.Assert(err, IsNil) |
| 457 » err = unit.Remove() |
| 458 » c.Assert(err, IsNil) |
| 459 |
| 460 » err = s.mysql.Destroy() |
| 461 » c.Assert(err, IsNil) |
| 462 » c.Assert(s.mysql.Life(), Equals, state.Dying) |
| 463 » err = s.mysql.Refresh() |
| 464 » c.Assert(state.IsNotFound(err), Equals, true) |
| 465 } |
| 466 |
| 467 func (s *ServiceSuite) TestDestroyStaleZeroUnitCount(c *C) { |
| 468 » unit, err := s.mysql.AddUnit() |
| 469 » c.Assert(err, IsNil) |
| 470 |
| 471 » err = s.mysql.Destroy() |
| 472 » c.Assert(err, IsNil) |
| 473 » c.Assert(s.mysql.Life(), Equals, state.Dying) |
| 474 |
| 475 » err = s.mysql.Refresh() |
| 476 » c.Assert(err, IsNil) |
| 477 » c.Assert(s.mysql.Life(), Equals, state.Dying) |
| 478 |
| 479 » err = unit.EnsureDead() |
| 480 » c.Assert(err, IsNil) |
| 481 » err = s.mysql.Refresh() |
| 482 » c.Assert(err, IsNil) |
| 483 » c.Assert(s.mysql.Life(), Equals, state.Dying) |
| 484 |
| 485 » err = unit.Remove() |
| 486 » c.Assert(err, IsNil) |
| 487 » err = s.mysql.Refresh() |
| 488 » c.Assert(state.IsNotFound(err), Equals, true) |
| 489 } |
| 490 |
| 491 func (s *ServiceSuite) TestDestroyWithRemovableRelation(c *C) { |
420 wordpress, err := s.State.AddService("wordpress", s.AddTestingCharm(c, "
wordpress")) | 492 wordpress, err := s.State.AddService("wordpress", s.AddTestingCharm(c, "
wordpress")) |
421 c.Assert(err, IsNil) | 493 c.Assert(err, IsNil) |
422 eps, err := s.State.InferEndpoints([]string{"wordpress", "mysql"}) | 494 eps, err := s.State.InferEndpoints([]string{"wordpress", "mysql"}) |
423 c.Assert(err, IsNil) | 495 c.Assert(err, IsNil) |
424 rel, err := s.State.AddRelation(eps...) | 496 rel, err := s.State.AddRelation(eps...) |
425 c.Assert(err, IsNil) | 497 c.Assert(err, IsNil) |
426 | 498 |
427 // Destroy a service with no units in relation scope; check service and | 499 // Destroy a service with no units in relation scope; check service and |
428 // unit removed. | 500 // unit removed. |
429 err = wordpress.Destroy() | 501 err = wordpress.Destroy() |
430 c.Assert(err, IsNil) | 502 c.Assert(err, IsNil) |
431 err = wordpress.Refresh() | 503 err = wordpress.Refresh() |
432 c.Assert(state.IsNotFound(err), Equals, true) | 504 c.Assert(state.IsNotFound(err), Equals, true) |
433 err = rel.Refresh() | 505 err = rel.Refresh() |
434 c.Assert(state.IsNotFound(err), Equals, true) | 506 c.Assert(state.IsNotFound(err), Equals, true) |
435 } | 507 } |
436 | 508 |
437 func (s *ServiceSuite) TestLifeWithReferencedRelations(c *C) { | 509 func (s *ServiceSuite) TestDestroyWithReferencedRelation(c *C) { |
| 510 » s.assertDestroyWithReferencedRelation(c, true) |
| 511 } |
| 512 |
| 513 func (s *ServiceSuite) TestDestroyWithreferencedRelationStaleCount(c *C) { |
| 514 » s.assertDestroyWithReferencedRelation(c, false) |
| 515 } |
| 516 |
| 517 func (s *ServiceSuite) assertDestroyWithReferencedRelation(c *C, refresh bool) { |
438 wordpress, err := s.State.AddService("wordpress", s.AddTestingCharm(c, "
wordpress")) | 518 wordpress, err := s.State.AddService("wordpress", s.AddTestingCharm(c, "
wordpress")) |
439 c.Assert(err, IsNil) | 519 c.Assert(err, IsNil) |
440 eps, err := s.State.InferEndpoints([]string{"wordpress", "mysql"}) | 520 eps, err := s.State.InferEndpoints([]string{"wordpress", "mysql"}) |
441 c.Assert(err, IsNil) | 521 c.Assert(err, IsNil) |
442 » rel, err := s.State.AddRelation(eps...) | 522 » rel0, err := s.State.AddRelation(eps...) |
443 » c.Assert(err, IsNil) | 523 » c.Assert(err, IsNil) |
444 | 524 |
445 » // Join a unit to the wordpress side to keep the relation alive. | 525 » _, err = s.State.AddService("logging", s.AddTestingCharm(c, "logging")) |
| 526 » c.Assert(err, IsNil) |
| 527 » eps, err = s.State.InferEndpoints([]string{"logging", "mysql"}) |
| 528 » c.Assert(err, IsNil) |
| 529 » rel1, err := s.State.AddRelation(eps...) |
| 530 » c.Assert(err, IsNil) |
| 531 |
| 532 » // Add a separate reference to the first relation. |
446 unit, err := wordpress.AddUnit() | 533 unit, err := wordpress.AddUnit() |
447 c.Assert(err, IsNil) | 534 c.Assert(err, IsNil) |
448 » ru, err := rel.Unit(unit) | 535 » ru, err := rel0.Unit(unit) |
449 c.Assert(err, IsNil) | 536 c.Assert(err, IsNil) |
450 err = ru.EnterScope(nil) | 537 err = ru.EnterScope(nil) |
451 c.Assert(err, IsNil) | 538 c.Assert(err, IsNil) |
452 | 539 |
453 » // Set Dying, and check that the relation also becomes Dying. | 540 » // Optionally update the service document to get correct relation counts
. |
| 541 » if refresh { |
| 542 » » err = s.mysql.Destroy() |
| 543 » » c.Assert(err, IsNil) |
| 544 » } |
| 545 |
| 546 » // Destroy, and check that the first relation becomes Dying... |
454 err = s.mysql.Destroy() | 547 err = s.mysql.Destroy() |
455 c.Assert(err, IsNil) | 548 c.Assert(err, IsNil) |
456 » err = rel.Refresh() | 549 » err = rel0.Refresh() |
457 » c.Assert(err, IsNil) | 550 » c.Assert(err, IsNil) |
458 » c.Assert(rel.Life(), Equals, state.Dying) | 551 » c.Assert(rel0.Life(), Equals, state.Dying) |
459 | 552 |
460 » // Check that no new relations can be added. | 553 » // ...while the second is removed directly. |
461 » _, err = s.State.AddService("logging", s.AddTestingCharm(c, "logging")) | 554 » err = rel1.Refresh() |
462 » c.Assert(err, IsNil) | 555 » c.Assert(state.IsNotFound(err), Equals, true) |
463 » eps, err = s.State.InferEndpoints([]string{"logging", "mysql"}) | 556 |
464 » c.Assert(err, IsNil) | 557 » // Drop the last reference to the first relation; check the relation and |
465 » _, err = s.State.AddRelation(eps...) | 558 » // the service are are both removed. |
466 » c.Assert(err, ErrorMatches, `cannot add relation "logging:info mysql:juj
u-info": service "mysql" is not alive`) | |
467 | |
468 » // Leave scope on the counterpart side; check the service and relation | |
469 » // are both removed. | |
470 err = ru.LeaveScope() | 559 err = ru.LeaveScope() |
471 c.Assert(err, IsNil) | 560 c.Assert(err, IsNil) |
472 err = s.mysql.Refresh() | 561 err = s.mysql.Refresh() |
473 c.Assert(state.IsNotFound(err), Equals, true) | 562 c.Assert(state.IsNotFound(err), Equals, true) |
474 » err = rel.Refresh() | 563 » err = rel0.Refresh() |
475 c.Assert(state.IsNotFound(err), Equals, true) | 564 c.Assert(state.IsNotFound(err), Equals, true) |
476 } | 565 } |
477 | 566 |
478 func (s *ServiceSuite) TestReadUnitWithChangingState(c *C) { | 567 func (s *ServiceSuite) TestReadUnitWithChangingState(c *C) { |
479 // Check that reading a unit after removing the service | 568 // Check that reading a unit after removing the service |
480 // fails nicely. | 569 // fails nicely. |
481 err := s.mysql.Destroy() | 570 err := s.mysql.Destroy() |
482 c.Assert(err, IsNil) | 571 c.Assert(err, IsNil) |
483 err = s.mysql.Refresh() | 572 err = s.mysql.Refresh() |
484 c.Assert(state.IsNotFound(err), Equals, true) | 573 c.Assert(state.IsNotFound(err), Equals, true) |
(...skipping 573 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1058 case <-time.After(500 * time.Millisecond): | 1147 case <-time.After(500 * time.Millisecond): |
1059 c.Fatalf("did not get change") | 1148 c.Fatalf("did not get change") |
1060 } | 1149 } |
1061 | 1150 |
1062 select { | 1151 select { |
1063 case got := <-configWatcher.Changes(): | 1152 case got := <-configWatcher.Changes(): |
1064 c.Fatalf("got unexpected change: %#v", got) | 1153 c.Fatalf("got unexpected change: %#v", got) |
1065 case <-time.After(100 * time.Millisecond): | 1154 case <-time.After(100 * time.Millisecond): |
1066 } | 1155 } |
1067 } | 1156 } |
| 1157 |
| 1158 func (s *ServiceSuite) TestAnnotatorForService(c *C) { |
| 1159 testAnnotator(c, func() (annotator, error) { |
| 1160 return s.State.Service("mysql") |
| 1161 }) |
| 1162 } |
LEFT | RIGHT |