github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/worker/certupdater/certupdater_test.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package certupdater_test 5 6 import ( 7 "net" 8 stdtesting "testing" 9 10 jc "github.com/juju/testing/checkers" 11 "github.com/juju/worker/v3/workertest" 12 gc "gopkg.in/check.v1" 13 14 jujucontroller "github.com/juju/juju/controller" 15 "github.com/juju/juju/core/network" 16 "github.com/juju/juju/pki" 17 pkitest "github.com/juju/juju/pki/test" 18 "github.com/juju/juju/state" 19 coretesting "github.com/juju/juju/testing" 20 "github.com/juju/juju/worker/certupdater" 21 ) 22 23 func TestPackage(t *stdtesting.T) { 24 coretesting.MgoTestPackage(t) 25 } 26 27 type CertUpdaterSuite struct { 28 coretesting.BaseSuite 29 stateServingInfo jujucontroller.StateServingInfo 30 } 31 32 var _ = gc.Suite(&CertUpdaterSuite{}) 33 34 func (s *CertUpdaterSuite) SetUpTest(c *gc.C) { 35 s.BaseSuite.SetUpTest(c) 36 37 s.stateServingInfo = jujucontroller.StateServingInfo{ 38 Cert: coretesting.ServerCert, 39 PrivateKey: coretesting.ServerKey, 40 CAPrivateKey: coretesting.CAKey, 41 StatePort: 123, 42 APIPort: 456, 43 } 44 } 45 46 type mockNotifyWatcher struct { 47 changes <-chan struct{} 48 } 49 50 func (w *mockNotifyWatcher) Changes() <-chan struct{} { 51 return w.changes 52 } 53 54 func (*mockNotifyWatcher) Stop() error { 55 return nil 56 } 57 58 func (*mockNotifyWatcher) Kill() {} 59 60 func (*mockNotifyWatcher) Wait() error { 61 return nil 62 } 63 64 func (*mockNotifyWatcher) Err() error { 65 return nil 66 } 67 68 func newMockNotifyWatcher(changes <-chan struct{}) state.NotifyWatcher { 69 return &mockNotifyWatcher{changes} 70 } 71 72 type mockMachine struct { 73 changes chan struct{} 74 } 75 76 func (m *mockMachine) WatchAddresses() state.NotifyWatcher { 77 return newMockNotifyWatcher(m.changes) 78 } 79 80 func (m *mockMachine) Addresses() (addresses network.SpaceAddresses) { 81 return network.NewSpaceAddresses("0.1.2.3") 82 } 83 84 func (s *CertUpdaterSuite) StateServingInfo() (jujucontroller.StateServingInfo, bool) { 85 return s.stateServingInfo, true 86 } 87 88 type mockAPIHostGetter struct{} 89 90 func (g *mockAPIHostGetter) APIHostPortsForClients() ([]network.SpaceHostPorts, error) { 91 return []network.SpaceHostPorts{{ 92 {SpaceAddress: network.NewSpaceAddress("192.168.1.1", network.WithScope(network.ScopeCloudLocal)), NetPort: 17070}, 93 {SpaceAddress: network.NewSpaceAddress("10.1.1.1", network.WithScope(network.ScopeMachineLocal)), NetPort: 17070}, 94 }}, nil 95 } 96 97 func (s *CertUpdaterSuite) TestStartStop(c *gc.C) { 98 authority, err := pkitest.NewTestAuthority() 99 c.Assert(err, jc.ErrorIsNil) 100 101 changes := make(chan struct{}) 102 worker := certupdater.NewCertificateUpdater(certupdater.Config{ 103 AddressWatcher: &mockMachine{changes}, 104 APIHostPortsGetter: &mockAPIHostGetter{}, 105 Authority: authority, 106 }) 107 workertest.CleanKill(c, worker) 108 109 leaf, err := authority.LeafForGroup(pki.ControllerIPLeafGroup) 110 c.Assert(err, jc.ErrorIsNil) 111 c.Assert(leaf.Certificate().IPAddresses, coretesting.IPsEqual, 112 []net.IP{net.ParseIP("192.168.1.1")}) 113 } 114 115 func (s *CertUpdaterSuite) TestAddressChange(c *gc.C) { 116 authority, err := pkitest.NewTestAuthority() 117 c.Assert(err, jc.ErrorIsNil) 118 119 changes := make(chan struct{}) 120 worker := certupdater.NewCertificateUpdater(certupdater.Config{ 121 AddressWatcher: &mockMachine{changes}, 122 APIHostPortsGetter: &mockAPIHostGetter{}, 123 Authority: authority, 124 }) 125 126 changes <- struct{}{} 127 // Certificate should be updated with the address value. 128 129 workertest.CleanKill(c, worker) 130 leaf, err := authority.LeafForGroup(pki.ControllerIPLeafGroup) 131 c.Assert(err, jc.ErrorIsNil) 132 c.Assert(leaf.Certificate().IPAddresses, coretesting.IPsEqual, 133 []net.IP{net.ParseIP("0.1.2.3")}) 134 }