github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/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/network" 11 "github.com/juju/juju/core/watcher" 12 "github.com/juju/juju/core/watcher/watchertest" 13 "github.com/juju/juju/state" 14 ) 15 16 type APIAddresserTests struct { 17 ctrlSt *state.State 18 state *state.State 19 facade APIAddresserFacade 20 waitForModelWatchersIdle func(c *gc.C) 21 } 22 23 func NewAPIAddresserTests(facade APIAddresserFacade, ctrlSt, st *state.State, waitForModelWatchersIdle func(c *gc.C)) *APIAddresserTests { 24 return &APIAddresserTests{ 25 ctrlSt: ctrlSt, 26 state: st, 27 facade: facade, 28 waitForModelWatchersIdle: waitForModelWatchersIdle, 29 } 30 } 31 32 type APIAddresserFacade interface { 33 APIAddresses() ([]string, error) 34 APIHostPorts() ([]network.ProviderHostPorts, error) 35 WatchAPIHostPorts() (watcher.NotifyWatcher, error) 36 } 37 38 func (s *APIAddresserTests) TestAPIAddresses(c *gc.C) { 39 hostPorts := []network.SpaceHostPorts{ 40 network.NewSpaceHostPorts(1234, "0.1.2.3"), 41 } 42 43 err := s.state.SetAPIHostPorts(hostPorts) 44 c.Assert(err, jc.ErrorIsNil) 45 46 addresses, err := s.facade.APIAddresses() 47 c.Assert(err, jc.ErrorIsNil) 48 c.Assert(addresses, gc.DeepEquals, []string{"0.1.2.3:1234"}) 49 } 50 51 func (s *APIAddresserTests) TestAPIHostPorts(c *gc.C) { 52 ipv6Addr := network.NewSpaceAddress("2001:DB8::1", network.WithScope(network.ScopeCloudLocal)) 53 54 setServerAddrs := []network.SpaceHostPorts{ 55 network.NewSpaceHostPorts(999, "0.1.2.24"), 56 network.NewSpaceHostPorts(1234, "example.com"), 57 network.SpaceAddressesWithPort([]network.SpaceAddress{ipv6Addr}, 999), 58 } 59 err := s.state.SetAPIHostPorts(setServerAddrs) 60 c.Assert(err, jc.ErrorIsNil) 61 62 expectServerAddrs := []network.ProviderHostPorts{ 63 {network.ProviderHostPort{ProviderAddress: network.NewMachineAddress("0.1.2.24").AsProviderAddress(), NetPort: 999}}, 64 {network.ProviderHostPort{ProviderAddress: network.NewMachineAddress("example.com").AsProviderAddress(), NetPort: 1234}}, 65 {network.ProviderHostPort{ProviderAddress: network.NewMachineAddress(ipv6Addr.Value).AsProviderAddress(), NetPort: 999}}, 66 } 67 expectServerAddrs[2][0].Scope = network.ScopeCloudLocal 68 69 serverAddrs, err := s.facade.APIHostPorts() 70 c.Assert(err, jc.ErrorIsNil) 71 c.Assert(serverAddrs, gc.DeepEquals, expectServerAddrs) 72 } 73 74 func (s *APIAddresserTests) TestWatchAPIHostPorts(c *gc.C) { 75 hostports, err := s.ctrlSt.APIHostPortsForAgents() 76 c.Assert(err, jc.ErrorIsNil) 77 expectServerAddrs := []network.SpaceHostPorts{ 78 network.NewSpaceHostPorts(5678, "0.1.2.3"), 79 } 80 // Make sure we are changing the value 81 c.Assert(hostports, gc.Not(gc.DeepEquals), expectServerAddrs) 82 s.waitForModelWatchersIdle(c) 83 84 c.Logf("starting api host port watcher") 85 w, err := s.facade.WatchAPIHostPorts() 86 c.Assert(err, jc.ErrorIsNil) 87 wc := watchertest.NewNotifyWatcherC(c, w) 88 defer wc.AssertStops() 89 90 // Initial event. 91 wc.AssertOneChange() 92 c.Logf("got initial event") 93 94 // Change the state addresses and check that we get a notification 95 err = s.state.SetAPIHostPorts(expectServerAddrs) 96 c.Assert(err, jc.ErrorIsNil) 97 98 wc.AssertOneChange() 99 c.Logf("saw change event") 100 101 // And that we can change it again and see the notification 102 expectServerAddrs[0][0].Value = "0.1.99.99" 103 104 err = s.state.SetAPIHostPorts(expectServerAddrs) 105 c.Assert(err, jc.ErrorIsNil) 106 c.Logf("saw second change event") 107 108 wc.AssertOneChange() 109 }