github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/state/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 gc "launchpad.net/gocheck" 8 9 "github.com/juju/juju/instance" 10 "github.com/juju/juju/state" 11 "github.com/juju/juju/state/api/watcher" 12 statetesting "github.com/juju/juju/state/testing" 13 ) 14 15 type APIAddresserTests struct { 16 state *state.State 17 facade APIAddresserFacade 18 } 19 20 func NewAPIAddresserTests(facade APIAddresserFacade, st *state.State) *APIAddresserTests { 21 return &APIAddresserTests{ 22 state: st, 23 facade: facade, 24 } 25 } 26 27 type APIAddresserFacade interface { 28 APIAddresses() ([]string, error) 29 CACert() (string, error) 30 APIHostPorts() ([][]instance.HostPort, error) 31 WatchAPIHostPorts() (watcher.NotifyWatcher, error) 32 } 33 34 func (s *APIAddresserTests) TestAPIAddresses(c *gc.C) { 35 hostPorts := [][]instance.HostPort{{{ 36 Address: instance.NewAddress("0.1.2.3", instance.NetworkUnknown), 37 Port: 1234, 38 }}} 39 40 err := s.state.SetAPIHostPorts(hostPorts) 41 c.Assert(err, gc.IsNil) 42 43 addresses, err := s.facade.APIAddresses() 44 c.Assert(err, gc.IsNil) 45 c.Assert(addresses, gc.DeepEquals, []string{"0.1.2.3:1234"}) 46 } 47 48 func (s *APIAddresserTests) TestAPIHostPorts(c *gc.C) { 49 expectServerAddrs := [][]instance.HostPort{{{ 50 Address: instance.NewAddress("0.1.2.24", instance.NetworkUnknown), 51 Port: 999, 52 }, { 53 Address: instance.NewAddress("example.com", instance.NetworkUnknown), 54 Port: 1234, 55 }}, {{ 56 Address: instance.Address{ 57 Value: "2001:DB8::1", 58 Type: instance.Ipv6Address, 59 NetworkName: "someNetwork", 60 NetworkScope: instance.NetworkCloudLocal, 61 }, 62 Port: 999, 63 }}} 64 65 err := s.state.SetAPIHostPorts(expectServerAddrs) 66 c.Assert(err, gc.IsNil) 67 68 serverAddrs, err := s.facade.APIHostPorts() 69 c.Assert(err, gc.IsNil) 70 c.Assert(serverAddrs, gc.DeepEquals, expectServerAddrs) 71 } 72 73 func (s *APIAddresserTests) TestCACert(c *gc.C) { 74 caCert, err := s.facade.CACert() 75 c.Assert(err, gc.IsNil) 76 c.Assert(caCert, gc.DeepEquals, s.state.CACert()) 77 } 78 79 func (s *APIAddresserTests) TestWatchAPIHostPorts(c *gc.C) { 80 expectServerAddrs := [][]instance.HostPort{{{ 81 Address: instance.NewAddress("0.1.2.3", instance.NetworkUnknown), 82 Port: 1234, 83 }}} 84 err := s.state.SetAPIHostPorts(expectServerAddrs) 85 c.Assert(err, gc.IsNil) 86 87 w, err := s.facade.WatchAPIHostPorts() 88 c.Assert(err, gc.IsNil) 89 defer statetesting.AssertStop(c, w) 90 91 wc := statetesting.NewNotifyWatcherC(c, s.state, w) 92 93 // Initial event. 94 wc.AssertOneChange() 95 96 // Change the state addresses and check that we get a notification 97 expectServerAddrs[0][0].Value = "0.1.99.99" 98 99 err = s.state.SetAPIHostPorts(expectServerAddrs) 100 c.Assert(err, gc.IsNil) 101 102 wc.AssertOneChange() 103 104 statetesting.AssertStop(c, w) 105 wc.AssertClosed() 106 }