423
func hostPortInSpace(address, spaceName string) network.HostPort {
424
netAddress := network.Address{
426
Type: network.IPv4Address,
428
Scope: network.ScopeUnknown,
429
SpaceName: network.SpaceName(spaceName),
431
return network.HostPort{netAddress, 4711}
434
func mongoSpaceTestCommonSetup(c *gc.C, ipVersion TestIPVersion, noSpaces bool) (*fakeState, []string, []network.HostPort) {
436
InitState(c, st, 3, ipVersion)
437
var hostPorts []network.HostPort
440
hostPorts = []network.HostPort{
441
hostPortInSpace(fmt.Sprintf(ipVersion.machineFormatHost, 1), ""),
442
hostPortInSpace(fmt.Sprintf(ipVersion.machineFormatHost, 2), ""),
443
hostPortInSpace(fmt.Sprintf(ipVersion.machineFormatHost, 3), ""),
446
hostPorts = []network.HostPort{
447
hostPortInSpace(fmt.Sprintf(ipVersion.machineFormatHost, 1), "one"),
448
hostPortInSpace(fmt.Sprintf(ipVersion.machineFormatHost, 2), "two"),
449
hostPortInSpace(fmt.Sprintf(ipVersion.machineFormatHost, 3), "three"),
453
machines := []string{"10", "11", "12"}
454
for _, machine := range machines {
455
st.machine(machine).SetHasVote(true)
456
st.machine(machine).setWantsVote(true)
459
st.session.Set(mkMembers("0v 1v 2v", ipVersion))
461
return st, machines, hostPorts
464
func startWorkerSupportingSpaces(st *fakeState, ipVersion TestIPVersion) *pgWorker {
465
return newWorker(st, noPublisher{}, true).(*pgWorker)
468
func runWorkerUntilMongoStateIs(c *gc.C, st *fakeState, w *pgWorker, mss state.MongoSpaceStates) {
469
changes := st.controllers.Watch()
471
for st.getMongoSpaceState() != mss {
474
c.Check(worker.Stop(w), gc.IsNil)
477
func (s *workerSuite) TestMongoFindAndUseSpace(c *gc.C) {
478
DoTestForIPv4AndIPv6(func(ipVersion TestIPVersion) {
479
st, machines, hostPorts := mongoSpaceTestCommonSetup(c, ipVersion, false)
481
for i, machine := range machines {
482
// machine 10 gets a host port in space one
483
// machine 11 gets host ports in spaces one and two
484
// machine 12 gets host ports in spaces one, two and three
485
st.machine(machine).setMongoHostPorts(hostPorts[0 : i+1])
488
w := startWorkerSupportingSpaces(st, ipVersion)
489
runWorkerUntilMongoStateIs(c, st, w, state.MongoSpaceValid)
491
// Only space one has all three servers in it
492
c.Assert(st.getMongoSpaceName(), gc.Equals, "one")
494
// All machines have the same address in this test for simplicity. The
495
// space three address is 0.0.0.3 giving us the host port of 0.0.0.3:4711
496
members := st.session.members.Get().([]replicaset.Member)
497
c.Assert(members, gc.HasLen, 3)
498
for i := 0; i < 3; i++ {
499
c.Assert(members[i].Address, gc.Equals, fmt.Sprintf(ipVersion.formatHostPort, 1, 4711))
504
func (s *workerSuite) TestMongoErrorNoCommonSpace(c *gc.C) {
505
DoTestForIPv4AndIPv6(func(ipVersion TestIPVersion) {
506
st, machines, hostPorts := mongoSpaceTestCommonSetup(c, ipVersion, false)
508
for i, machine := range machines {
509
// machine 10 gets a host port in space one
510
// machine 11 gets a host port in space two
511
// machine 12 gets a host port in space three
512
st.machine(machine).setMongoHostPorts(hostPorts[i : i+1])
515
w := startWorkerSupportingSpaces(st, ipVersion)
516
done := make(chan error)
522
c.Assert(err, gc.ErrorMatches, ".*couldn't find a space containing all peer group machines")
523
case <-time.After(coretesting.LongWait):
524
c.Fatalf("timed out waiting for worker to exit")
527
// Each machine is in a unique space, so the Mongo space should be empty
528
c.Assert(st.getMongoSpaceName(), gc.Equals, "")
529
c.Assert(st.getMongoSpaceState(), gc.Equals, state.MongoSpaceInvalid)
533
func (s *workerSuite) TestMongoNoSpaces(c *gc.C) {
534
DoTestForIPv4AndIPv6(func(ipVersion TestIPVersion) {
535
st, machines, hostPorts := mongoSpaceTestCommonSetup(c, ipVersion, true)
537
for i, machine := range machines {
538
st.machine(machine).setMongoHostPorts(hostPorts[i : i+1])
541
w := startWorkerSupportingSpaces(st, ipVersion)
542
runWorkerUntilMongoStateIs(c, st, w, state.MongoSpaceValid)
544
// Only space one has all three servers in it
545
c.Assert(st.getMongoSpaceName(), gc.Equals, "")
549
func (s *workerSuite) TestMongoSpaceNotOverwritten(c *gc.C) {
550
DoTestForIPv4AndIPv6(func(ipVersion TestIPVersion) {
551
st, machines, hostPorts := mongoSpaceTestCommonSetup(c, ipVersion, false)
553
for i, machine := range machines {
554
// machine 10 gets a host port in space one
555
// machine 11 gets host ports in spaces one and two
556
// machine 12 gets host ports in spaces one, two and three
557
st.machine(machine).setMongoHostPorts(hostPorts[0 : i+1])
560
w := startWorkerSupportingSpaces(st, ipVersion)
561
runWorkerUntilMongoStateIs(c, st, w, state.MongoSpaceValid)
563
// Only space one has all three servers in it
564
c.Assert(st.getMongoSpaceName(), gc.Equals, "one")
566
// Set st.mongoSpaceName to something different
568
st.SetMongoSpaceState(state.MongoSpaceUnknown)
569
st.SetOrGetMongoSpaceName("testing")
571
// Manually run getMongoSpace - it should do nothing because we already have
572
// a space. If it did re-calculate the space name it will change back to "one".
573
w.getMongoSpace(&peerGroupInfo{})
575
// Only space one has all three servers in it
576
c.Assert(st.getMongoSpaceName(), gc.Equals, "testing")
577
c.Assert(st.getMongoSpaceState(), gc.Equals, state.MongoSpaceValid)
581
func (s *workerSuite) TestMongoSpaceNotCalculatedWhenSpacesNotSupported(c *gc.C) {
582
DoTestForIPv4AndIPv6(func(ipVersion TestIPVersion) {
583
st, machines, hostPorts := mongoSpaceTestCommonSetup(c, ipVersion, false)
585
for i, machine := range machines {
586
// machine 10 gets a host port in space one
587
// machine 11 gets host ports in spaces one and two
588
// machine 12 gets host ports in spaces one, two and three
589
st.machine(machine).setMongoHostPorts(hostPorts[0 : i+1])
592
// Set some garbage up to check that it isn't overwritten
593
st.SetOrGetMongoSpaceName("garbage")
594
st.SetMongoSpaceState(state.MongoSpaceUnknown)
596
// Start a worker that doesn't support spaces
597
w := newWorker(st, noPublisher{}, false).(*pgWorker)
598
runWorkerUntilMongoStateIs(c, st, w, state.MongoSpaceUnsupported)
600
// Only space one has all three servers in it
601
c.Assert(st.getMongoSpaceName(), gc.Equals, "garbage")
602
c.Assert(st.getMongoSpaceState(), gc.Equals, state.MongoSpaceUnsupported)
421
606
func (s *workerSuite) TestWorkerRetriesOnPublishError(c *gc.C) {
422
607
DoTestForIPv4AndIPv6(func(ipVersion TestIPVersion) {
423
608
s.PatchValue(&pollInterval, coretesting.LongWait+time.Second)