github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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/core/watcher" 11 "github.com/juju/juju/core/watcher/watchertest" 12 "github.com/juju/juju/network" 13 "github.com/juju/juju/state" 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 APIHostPorts() ([][]network.HostPort, error) 31 WatchAPIHostPorts() (watcher.NotifyWatcher, error) 32 } 33 34 func (s *APIAddresserTests) TestAPIAddresses(c *gc.C) { 35 hostPorts := [][]network.HostPort{ 36 network.NewHostPorts(1234, "0.1.2.3"), 37 } 38 39 err := s.state.SetAPIHostPorts(hostPorts) 40 c.Assert(err, jc.ErrorIsNil) 41 42 addresses, err := s.facade.APIAddresses() 43 c.Assert(err, jc.ErrorIsNil) 44 c.Assert(addresses, gc.DeepEquals, []string{"0.1.2.3:1234"}) 45 } 46 47 func (s *APIAddresserTests) TestAPIHostPorts(c *gc.C) { 48 ipv6Addr := network.NewScopedAddress( 49 "2001:DB8::1", network.ScopeCloudLocal, 50 ) 51 expectServerAddrs := [][]network.HostPort{ 52 network.NewHostPorts(999, "0.1.2.24"), 53 network.NewHostPorts(1234, "example.com"), 54 network.AddressesWithPort([]network.Address{ipv6Addr}, 999), 55 } 56 57 err := s.state.SetAPIHostPorts(expectServerAddrs) 58 c.Assert(err, jc.ErrorIsNil) 59 60 serverAddrs, err := s.facade.APIHostPorts() 61 c.Assert(err, jc.ErrorIsNil) 62 c.Assert(serverAddrs, gc.DeepEquals, expectServerAddrs) 63 } 64 65 func (s *APIAddresserTests) TestWatchAPIHostPorts(c *gc.C) { 66 hostports, err := s.state.APIHostPortsForAgents() 67 c.Assert(err, jc.ErrorIsNil) 68 expectServerAddrs := [][]network.HostPort{ 69 network.NewHostPorts(5678, "0.1.2.3"), 70 } 71 // Make sure we are changing the value 72 c.Assert(hostports, gc.Not(gc.DeepEquals), expectServerAddrs) 73 74 c.Logf("starting api host port watcher") 75 w, err := s.facade.WatchAPIHostPorts() 76 c.Assert(err, jc.ErrorIsNil) 77 wc := watchertest.NewNotifyWatcherC(c, w, s.state.StartSync) 78 defer wc.AssertStops() 79 80 // Initial event. 81 wc.AssertOneChange() 82 c.Logf("got initial event") 83 84 // Change the state addresses and check that we get a notification 85 err = s.state.SetAPIHostPorts(expectServerAddrs) 86 c.Assert(err, jc.ErrorIsNil) 87 88 wc.AssertOneChange() 89 c.Logf("saw change event") 90 91 // And that we can change it again and see the notification 92 expectServerAddrs[0][0].Value = "0.1.99.99" 93 94 err = s.state.SetAPIHostPorts(expectServerAddrs) 95 c.Assert(err, jc.ErrorIsNil) 96 c.Logf("saw second change event") 97 98 wc.AssertOneChange() 99 }