github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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/api/watcher" 11 "github.com/juju/juju/network" 12 "github.com/juju/juju/state" 13 statetesting "github.com/juju/juju/state/testing" 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 ipv6Addr.NetworkName = "someNetwork" 53 expectServerAddrs := [][]network.HostPort{ 54 network.NewHostPorts(999, "0.1.2.24"), 55 network.NewHostPorts(1234, "example.com"), 56 network.AddressesWithPort([]network.Address{ipv6Addr}, 999), 57 } 58 59 err := s.state.SetAPIHostPorts(expectServerAddrs) 60 c.Assert(err, jc.ErrorIsNil) 61 62 serverAddrs, err := s.facade.APIHostPorts() 63 c.Assert(err, jc.ErrorIsNil) 64 c.Assert(serverAddrs, gc.DeepEquals, expectServerAddrs) 65 } 66 67 func (s *APIAddresserTests) TestCACert(c *gc.C) { 68 caCert, err := s.facade.CACert() 69 c.Assert(err, jc.ErrorIsNil) 70 c.Assert(caCert, gc.DeepEquals, s.state.CACert()) 71 } 72 73 func (s *APIAddresserTests) TestWatchAPIHostPorts(c *gc.C) { 74 expectServerAddrs := [][]network.HostPort{ 75 network.NewHostPorts(1234, "0.1.2.3"), 76 } 77 err := s.state.SetAPIHostPorts(expectServerAddrs) 78 c.Assert(err, jc.ErrorIsNil) 79 80 w, err := s.facade.WatchAPIHostPorts() 81 c.Assert(err, jc.ErrorIsNil) 82 defer statetesting.AssertStop(c, w) 83 84 wc := statetesting.NewNotifyWatcherC(c, s.state, w) 85 86 // Initial event. 87 wc.AssertOneChange() 88 89 // Change the state addresses and check that we get a notification 90 expectServerAddrs[0][0].Value = "0.1.99.99" 91 92 err = s.state.SetAPIHostPorts(expectServerAddrs) 93 c.Assert(err, jc.ErrorIsNil) 94 95 wc.AssertOneChange() 96 97 statetesting.AssertStop(c, w) 98 wc.AssertClosed() 99 }