1
// Copyright 2014 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
7
jc "github.com/juju/testing/checkers"
10
"github.com/juju/juju/network"
11
"github.com/juju/juju/testing"
14
type publishSuite struct {
18
var _ = gc.Suite(&publishSuite{})
20
type mockAPIHostPortsSetter struct {
22
apiHostPorts [][]network.HostPort
25
func (s *mockAPIHostPortsSetter) SetAPIHostPorts(apiHostPorts [][]network.HostPort) error {
27
s.apiHostPorts = apiHostPorts
31
func (s *publishSuite) TestPublisherSetsAPIHostPortsOnce(c *gc.C) {
32
var mock mockAPIHostPortsSetter
33
statePublish := newPublisher(&mock)
35
hostPorts1 := network.NewHostPorts(1234, "testing1.invalid", "127.0.0.1")
36
hostPorts2 := network.NewHostPorts(1234, "testing2.invalid", "127.0.0.2")
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)
45
c.Assert(mock.calls, gc.Equals, 1)
46
c.Assert(mock.apiHostPorts, gc.DeepEquals, apiServers)
48
apiServers = append(apiServers, hostPorts2)
49
for i := 0; i < 2; i++ {
50
err := statePublish.publishAPIServers(apiServers, nil)
51
c.Assert(err, jc.ErrorIsNil)
53
c.Assert(mock.calls, gc.Equals, 2)
54
c.Assert(mock.apiHostPorts, gc.DeepEquals, apiServers)
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")
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)
68
c.Assert(mock.calls, gc.Equals, 1)
69
c.Assert(mock.apiHostPorts, gc.DeepEquals, [][]network.HostPort{expect})
72
check(ipV6First, ipV4First)
73
check(ipV4First, ipV4First)
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")