github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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/controller"
    12  	"github.com/juju/juju/network"
    13  	"github.com/juju/juju/state"
    14  	coretesting "github.com/juju/juju/testing"
    15  )
    16  
    17  type stateAddresserSuite struct {
    18  	addresser *common.StateAddresser
    19  }
    20  
    21  type apiAddresserSuite struct {
    22  	addresser *common.APIAddresser
    23  	fake      *fakeAddresses
    24  }
    25  
    26  var _ = gc.Suite(&stateAddresserSuite{})
    27  var _ = gc.Suite(&apiAddresserSuite{})
    28  
    29  func (s *stateAddresserSuite) SetUpTest(c *gc.C) {
    30  	s.addresser = common.NewStateAddresser(fakeAddresses{
    31  		hostPorts: [][]network.HostPort{
    32  			network.NewHostPorts(1, "apiaddresses"),
    33  			network.NewHostPorts(2, "apiaddresses"),
    34  		},
    35  	})
    36  }
    37  
    38  // Verify that AddressAndCertGetter is satisfied by *state.State.
    39  var _ common.AddressAndCertGetter = (*state.State)(nil)
    40  
    41  func (s *stateAddresserSuite) TestStateAddresses(c *gc.C) {
    42  	result, err := s.addresser.StateAddresses()
    43  	c.Assert(err, jc.ErrorIsNil)
    44  	c.Assert(result.Result, gc.DeepEquals, []string{"addresses:1", "addresses:2"})
    45  }
    46  
    47  func (s *apiAddresserSuite) SetUpTest(c *gc.C) {
    48  	s.fake = &fakeAddresses{
    49  		hostPorts: [][]network.HostPort{
    50  			network.NewHostPorts(1, "apiaddresses"),
    51  			network.NewHostPorts(2, "apiaddresses"),
    52  		},
    53  	}
    54  	s.addresser = common.NewAPIAddresser(s.fake, common.NewResources())
    55  }
    56  
    57  func (s *apiAddresserSuite) TestAPIAddresses(c *gc.C) {
    58  	result, err := s.addresser.APIAddresses()
    59  	c.Assert(err, jc.ErrorIsNil)
    60  	c.Assert(result.Result, gc.DeepEquals, []string{"apiaddresses:1", "apiaddresses:2"})
    61  }
    62  
    63  func (s *apiAddresserSuite) TestAPIAddressesPrivateFirst(c *gc.C) {
    64  	ctlr1, err := network.ParseHostPorts("52.7.1.1:17070", "10.0.2.1:17070")
    65  	c.Assert(err, jc.ErrorIsNil)
    66  	ctlr2, err := network.ParseHostPorts("53.51.121.17:17070", "10.0.1.17:17070")
    67  	c.Assert(err, jc.ErrorIsNil)
    68  	s.fake.hostPorts = [][]network.HostPort{
    69  		network.NewHostPorts(1, "apiaddresses"),
    70  		ctlr1,
    71  		ctlr2,
    72  		network.NewHostPorts(2, "apiaddresses"),
    73  	}
    74  	for _, hps := range s.fake.hostPorts {
    75  		for _, hp := range hps {
    76  			c.Logf("%s - %#v", hp.Scope, hp)
    77  		}
    78  	}
    79  
    80  	result, err := s.addresser.APIAddresses()
    81  	c.Assert(err, jc.ErrorIsNil)
    82  
    83  	c.Check(result.Result, gc.DeepEquals, []string{
    84  		"apiaddresses:1",
    85  		"10.0.2.1:17070",
    86  		"52.7.1.1:17070",
    87  		"10.0.1.17:17070",
    88  		"53.51.121.17:17070",
    89  		"apiaddresses:2",
    90  	})
    91  }
    92  
    93  func (s *apiAddresserSuite) TestModelUUID(c *gc.C) {
    94  	result := s.addresser.ModelUUID()
    95  	c.Assert(string(result.Result), gc.Equals, "the model uuid")
    96  }
    97  
    98  var _ common.AddressAndCertGetter = fakeAddresses{}
    99  
   100  type fakeAddresses struct {
   101  	hostPorts [][]network.HostPort
   102  }
   103  
   104  func (fakeAddresses) Addresses() ([]string, error) {
   105  	return []string{"addresses:1", "addresses:2"}, nil
   106  }
   107  
   108  func (fakeAddresses) ControllerConfig() (controller.Config, error) {
   109  	return coretesting.FakeControllerConfig(), nil
   110  }
   111  
   112  func (fakeAddresses) ModelUUID() string {
   113  	return "the model uuid"
   114  }
   115  
   116  func (f fakeAddresses) APIHostPortsForAgents() ([][]network.HostPort, error) {
   117  	return f.hostPorts, nil
   118  }
   119  
   120  func (fakeAddresses) WatchAPIHostPortsForAgents() state.NotifyWatcher {
   121  	panic("should never be called")
   122  }