github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/apiserver/common/addresses_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package common_test
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/apiserver/common"
    11  	"github.com/juju/juju/network"
    12  	"github.com/juju/juju/state"
    13  )
    14  
    15  type stateAddresserSuite struct {
    16  	addresser *common.StateAddresser
    17  }
    18  
    19  type apiAddresserSuite struct {
    20  	addresser *common.APIAddresser
    21  	fake      *fakeAddresses
    22  }
    23  
    24  var _ = gc.Suite(&stateAddresserSuite{})
    25  var _ = gc.Suite(&apiAddresserSuite{})
    26  
    27  func (s *stateAddresserSuite) SetUpTest(c *gc.C) {
    28  	s.addresser = common.NewStateAddresser(fakeAddresses{
    29  		hostPorts: [][]network.HostPort{
    30  			network.NewHostPorts(1, "apiaddresses"),
    31  			network.NewHostPorts(2, "apiaddresses"),
    32  		},
    33  	})
    34  }
    35  
    36  // Verify that AddressAndCertGetter is satisfied by *state.State.
    37  var _ common.AddressAndCertGetter = (*state.State)(nil)
    38  
    39  func (s *stateAddresserSuite) TestStateAddresses(c *gc.C) {
    40  	result, err := s.addresser.StateAddresses()
    41  	c.Assert(err, jc.ErrorIsNil)
    42  	c.Assert(result.Result, gc.DeepEquals, []string{"addresses:1", "addresses:2"})
    43  }
    44  
    45  func (s *apiAddresserSuite) SetUpTest(c *gc.C) {
    46  	s.fake = &fakeAddresses{
    47  		hostPorts: [][]network.HostPort{
    48  			network.NewHostPorts(1, "apiaddresses"),
    49  			network.NewHostPorts(2, "apiaddresses"),
    50  		},
    51  	}
    52  	s.addresser = common.NewAPIAddresser(s.fake, common.NewResources())
    53  }
    54  
    55  func (s *apiAddresserSuite) TestAPIAddresses(c *gc.C) {
    56  	result, err := s.addresser.APIAddresses()
    57  	c.Assert(err, jc.ErrorIsNil)
    58  	c.Assert(result.Result, gc.DeepEquals, []string{"apiaddresses:1", "apiaddresses:2"})
    59  }
    60  
    61  func (s *apiAddresserSuite) TestAPIAddressesPrivateFirst(c *gc.C) {
    62  	ctlr1, err := network.ParseHostPorts("52.7.1.1:17070", "10.0.2.1:17070")
    63  	c.Assert(err, jc.ErrorIsNil)
    64  	ctlr2, err := network.ParseHostPorts("53.51.121.17:17070", "10.0.1.17:17070")
    65  	c.Assert(err, jc.ErrorIsNil)
    66  	s.fake.hostPorts = [][]network.HostPort{
    67  		network.NewHostPorts(1, "apiaddresses"),
    68  		ctlr1,
    69  		ctlr2,
    70  		network.NewHostPorts(2, "apiaddresses"),
    71  	}
    72  	for _, hps := range s.fake.hostPorts {
    73  		for _, hp := range hps {
    74  			c.Logf("%s - %#v", hp.Scope, hp)
    75  		}
    76  	}
    77  
    78  	result, err := s.addresser.APIAddresses()
    79  	c.Assert(err, jc.ErrorIsNil)
    80  
    81  	c.Check(result.Result, gc.DeepEquals, []string{
    82  		"apiaddresses:1",
    83  		"10.0.2.1:17070",
    84  		"52.7.1.1:17070",
    85  		"10.0.1.17:17070",
    86  		"53.51.121.17:17070",
    87  		"apiaddresses:2",
    88  	})
    89  }
    90  
    91  func (s *apiAddresserSuite) TestCACert(c *gc.C) {
    92  	result := s.addresser.CACert()
    93  	c.Assert(string(result.Result), gc.Equals, "a cert")
    94  }
    95  
    96  func (s *apiAddresserSuite) TestEnvironUUID(c *gc.C) {
    97  	result := s.addresser.ModelUUID()
    98  	c.Assert(string(result.Result), gc.Equals, "the environ uuid")
    99  }
   100  
   101  var _ common.AddressAndCertGetter = fakeAddresses{}
   102  
   103  type fakeAddresses struct {
   104  	hostPorts [][]network.HostPort
   105  }
   106  
   107  func (fakeAddresses) Addresses() ([]string, error) {
   108  	return []string{"addresses:1", "addresses:2"}, nil
   109  }
   110  
   111  func (fakeAddresses) APIAddressesFromMachines() ([]string, error) {
   112  	panic("should never be called")
   113  }
   114  
   115  func (fakeAddresses) CACert() string {
   116  	return "a cert"
   117  }
   118  
   119  func (fakeAddresses) ModelUUID() string {
   120  	return "the environ uuid"
   121  }
   122  
   123  func (f fakeAddresses) APIHostPorts() ([][]network.HostPort, error) {
   124  	return f.hostPorts, nil
   125  }
   126  
   127  func (fakeAddresses) WatchAPIHostPorts() state.NotifyWatcher {
   128  	panic("should never be called")
   129  }