github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/worker/peergrouper/publish_test.go (about)

     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, false)
    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(preferIPv6 bool, publish, expect []network.HostPort) {
    62  		var mock mockAPIHostPortsSetter
    63  		statePublish := newPublisher(&mock, preferIPv6)
    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(false, ipV6First, ipV4First)
    73  	check(false, ipV4First, ipV4First)
    74  	check(true, ipV4First, ipV6First)
    75  	check(true, ipV6First, ipV6First)
    76  }
    77  
    78  func (s *publishSuite) TestPublisherRejectsNoServers(c *gc.C) {
    79  	check := func(preferIPv6 bool) {
    80  		var mock mockAPIHostPortsSetter
    81  		statePublish := newPublisher(&mock, preferIPv6)
    82  		err := statePublish.PublishAPIServers(nil, nil)
    83  		c.Assert(err, gc.ErrorMatches, "no api servers specified")
    84  	}
    85  	check(false)
    86  	check(true)
    87  }