~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/worker/peergrouper/publish_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2014 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package peergrouper
 
5
 
 
6
import (
 
7
        jc "github.com/juju/testing/checkers"
 
8
        gc "gopkg.in/check.v1"
 
9
 
 
10
        "github.com/juju/juju/network"
 
11
        "github.com/juju/juju/testing"
 
12
)
 
13
 
 
14
type publishSuite struct {
 
15
        testing.BaseSuite
 
16
}
 
17
 
 
18
var _ = gc.Suite(&publishSuite{})
 
19
 
 
20
type mockAPIHostPortsSetter struct {
 
21
        calls        int
 
22
        apiHostPorts [][]network.HostPort
 
23
}
 
24
 
 
25
func (s *mockAPIHostPortsSetter) SetAPIHostPorts(apiHostPorts [][]network.HostPort) error {
 
26
        s.calls++
 
27
        s.apiHostPorts = apiHostPorts
 
28
        return nil
 
29
}
 
30
 
 
31
func (s *publishSuite) TestPublisherSetsAPIHostPortsOnce(c *gc.C) {
 
32
        var mock mockAPIHostPortsSetter
 
33
        statePublish := newPublisher(&mock)
 
34
 
 
35
        hostPorts1 := network.NewHostPorts(1234, "testing1.invalid", "127.0.0.1")
 
36
        hostPorts2 := network.NewHostPorts(1234, "testing2.invalid", "127.0.0.2")
 
37
 
 
38
        // statePublish.publishAPIServers should not update state a second time.
 
39
        apiServers := [][]network.HostPort{hostPorts1}
 
40
        for i := 0; i < 2; i++ {
 
41
                err := statePublish.publishAPIServers(apiServers, nil)
 
42
                c.Assert(err, jc.ErrorIsNil)
 
43
        }
 
44
 
 
45
        c.Assert(mock.calls, gc.Equals, 1)
 
46
        c.Assert(mock.apiHostPorts, gc.DeepEquals, apiServers)
 
47
 
 
48
        apiServers = append(apiServers, hostPorts2)
 
49
        for i := 0; i < 2; i++ {
 
50
                err := statePublish.publishAPIServers(apiServers, nil)
 
51
                c.Assert(err, jc.ErrorIsNil)
 
52
        }
 
53
        c.Assert(mock.calls, gc.Equals, 2)
 
54
        c.Assert(mock.apiHostPorts, gc.DeepEquals, apiServers)
 
55
}
 
56
 
 
57
func (s *publishSuite) TestPublisherSortsHostPorts(c *gc.C) {
 
58
        ipV4First := network.NewHostPorts(1234, "testing1.invalid", "127.0.0.1", "::1")
 
59
        ipV6First := network.NewHostPorts(1234, "testing1.invalid", "::1", "127.0.0.1")
 
60
 
 
61
        check := func(publish, expect []network.HostPort) {
 
62
                var mock mockAPIHostPortsSetter
 
63
                statePublish := newPublisher(&mock)
 
64
                for i := 0; i < 2; i++ {
 
65
                        err := statePublish.publishAPIServers([][]network.HostPort{publish}, nil)
 
66
                        c.Assert(err, jc.ErrorIsNil)
 
67
                }
 
68
                c.Assert(mock.calls, gc.Equals, 1)
 
69
                c.Assert(mock.apiHostPorts, gc.DeepEquals, [][]network.HostPort{expect})
 
70
        }
 
71
 
 
72
        check(ipV6First, ipV4First)
 
73
        check(ipV4First, ipV4First)
 
74
}
 
75
 
 
76
func (s *publishSuite) TestPublisherRejectsNoServers(c *gc.C) {
 
77
        var mock mockAPIHostPortsSetter
 
78
        statePublish := newPublisher(&mock)
 
79
        err := statePublish.PublishAPIServers(nil, nil)
 
80
        c.Assert(err, gc.ErrorMatches, "no api servers specified")
 
81
}