github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/api/testing/apiaddresser.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package testing
     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/state"
    12  	"github.com/juju/juju/watcher"
    13  	"github.com/juju/juju/watcher/watchertest"
    14  )
    15  
    16  type APIAddresserTests struct {
    17  	state  *state.State
    18  	facade APIAddresserFacade
    19  }
    20  
    21  func NewAPIAddresserTests(facade APIAddresserFacade, st *state.State) *APIAddresserTests {
    22  	return &APIAddresserTests{
    23  		state:  st,
    24  		facade: facade,
    25  	}
    26  }
    27  
    28  type APIAddresserFacade interface {
    29  	APIAddresses() ([]string, error)
    30  	CACert() (string, error)
    31  	APIHostPorts() ([][]network.HostPort, error)
    32  	WatchAPIHostPorts() (watcher.NotifyWatcher, error)
    33  }
    34  
    35  func (s *APIAddresserTests) TestAPIAddresses(c *gc.C) {
    36  	hostPorts := [][]network.HostPort{
    37  		network.NewHostPorts(1234, "0.1.2.3"),
    38  	}
    39  
    40  	err := s.state.SetAPIHostPorts(hostPorts)
    41  	c.Assert(err, jc.ErrorIsNil)
    42  
    43  	addresses, err := s.facade.APIAddresses()
    44  	c.Assert(err, jc.ErrorIsNil)
    45  	c.Assert(addresses, gc.DeepEquals, []string{"0.1.2.3:1234"})
    46  }
    47  
    48  func (s *APIAddresserTests) TestAPIHostPorts(c *gc.C) {
    49  	ipv6Addr := network.NewScopedAddress(
    50  		"2001:DB8::1", network.ScopeCloudLocal,
    51  	)
    52  	expectServerAddrs := [][]network.HostPort{
    53  		network.NewHostPorts(999, "0.1.2.24"),
    54  		network.NewHostPorts(1234, "example.com"),
    55  		network.AddressesWithPort([]network.Address{ipv6Addr}, 999),
    56  	}
    57  
    58  	err := s.state.SetAPIHostPorts(expectServerAddrs)
    59  	c.Assert(err, jc.ErrorIsNil)
    60  
    61  	serverAddrs, err := s.facade.APIHostPorts()
    62  	c.Assert(err, jc.ErrorIsNil)
    63  	c.Assert(serverAddrs, gc.DeepEquals, expectServerAddrs)
    64  }
    65  
    66  func (s *APIAddresserTests) TestCACert(c *gc.C) {
    67  	caCert, err := s.facade.CACert()
    68  	c.Assert(err, jc.ErrorIsNil)
    69  	c.Assert(caCert, gc.DeepEquals, s.state.CACert())
    70  }
    71  
    72  func (s *APIAddresserTests) TestWatchAPIHostPorts(c *gc.C) {
    73  	expectServerAddrs := [][]network.HostPort{
    74  		network.NewHostPorts(1234, "0.1.2.3"),
    75  	}
    76  	err := s.state.SetAPIHostPorts(expectServerAddrs)
    77  	c.Assert(err, jc.ErrorIsNil)
    78  
    79  	w, err := s.facade.WatchAPIHostPorts()
    80  	c.Assert(err, jc.ErrorIsNil)
    81  	wc := watchertest.NewNotifyWatcherC(c, w, s.state.StartSync)
    82  	defer wc.AssertStops()
    83  
    84  	// Initial event.
    85  	wc.AssertOneChange()
    86  
    87  	// Change the state addresses and check that we get a notification
    88  	expectServerAddrs[0][0].Value = "0.1.99.99"
    89  
    90  	err = s.state.SetAPIHostPorts(expectServerAddrs)
    91  	c.Assert(err, jc.ErrorIsNil)
    92  
    93  	wc.AssertOneChange()
    94  }