github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/state/address_internal_test.go (about) 1 // Copyright 2017 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package state 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 ) 12 13 type GetOpsForHostPortsSuite struct { 14 internalStateSuite 15 } 16 17 var _ = gc.Suite(&GetOpsForHostPortsSuite{}) 18 19 func (s *GetOpsForHostPortsSuite) TestGetOpsForHostPortsChangeWithSpaces(c *gc.C) { 20 addresses := map[string]network.SpaceHostPorts{ 21 "0": { 22 { 23 SpaceAddress: network.SpaceAddress{ 24 MachineAddress: network.MachineAddress{ 25 Value: "10.144.9.113", 26 Type: "ipv4", 27 Scope: "local-cloud", 28 }, 29 SpaceID: "foo", 30 }, 31 NetPort: 17070, 32 }, { 33 SpaceAddress: network.SpaceAddress{ 34 MachineAddress: network.MachineAddress{ 35 Value: "127.0.0.1", 36 Type: "ipv4", 37 Scope: "local-machine", 38 }, 39 }, 40 NetPort: 17070, 41 }, 42 }, 43 "1": { 44 { 45 SpaceAddress: network.SpaceAddress{ 46 MachineAddress: network.MachineAddress{ 47 Value: "10.144.9.62", 48 Type: "ipv4", 49 Scope: "local-cloud", 50 }, 51 SpaceID: "foo", 52 }, 53 NetPort: 17070, 54 }, { 55 SpaceAddress: network.SpaceAddress{ 56 MachineAddress: network.MachineAddress{ 57 Value: "127.0.0.1", 58 Type: "ipv4", 59 Scope: "local-machine", 60 }, 61 }, 62 NetPort: 17070, 63 }, 64 }, 65 "2": { 66 { 67 SpaceAddress: network.SpaceAddress{ 68 MachineAddress: network.MachineAddress{ 69 Value: "10.144.9.56", 70 Type: "ipv4", 71 Scope: "local-cloud", 72 }, 73 SpaceID: "foo", 74 }, 75 NetPort: 17070, 76 }, { 77 SpaceAddress: network.SpaceAddress{ 78 MachineAddress: network.MachineAddress{ 79 Value: "127.0.0.1", 80 Type: "ipv4", 81 Scope: "local-machine", 82 }, 83 }, 84 NetPort: 17070, 85 }, 86 }, 87 } 88 // Iterate the map each time to generate the slice with the machines 89 // in a different order. 90 addressSlice := func() []network.SpaceHostPorts { 91 var result []network.SpaceHostPorts 92 for _, value := range addresses { 93 result = append(result, value) 94 } 95 return result 96 } 97 98 controllers, closer := s.state.db().GetCollection(controllersC) 99 defer closer() 100 101 ops, err := s.state.getOpsForHostPortsChange(controllers, apiHostPortsKey, addressSlice()) 102 c.Assert(err, jc.ErrorIsNil) 103 c.Assert(len(ops), jc.GreaterThan, 0) 104 // Run the ops. 105 err = s.state.db().RunTransaction(ops) 106 c.Assert(err, jc.ErrorIsNil) 107 108 // Now iterate over the map a few times to get different ordering, and assert the 109 // ops to update the host ports is empty. 110 for i := 0; i < 5; i++ { 111 ops, err := s.state.getOpsForHostPortsChange(controllers, apiHostPortsKey, addressSlice()) 112 c.Assert(err, jc.ErrorIsNil) 113 c.Assert(ops, gc.HasLen, 0) 114 } 115 } 116 117 type AddressEqualitySuite struct{} 118 119 var _ = gc.Suite(&AddressEqualitySuite{}) 120 121 func (*AddressEqualitySuite) TestHostPortsEqual(c *gc.C) { 122 first := []network.SpaceHostPorts{ 123 { 124 { 125 SpaceAddress: network.SpaceAddress{ 126 MachineAddress: network.MachineAddress{ 127 Value: "10.144.9.113", 128 Type: "ipv4", 129 Scope: "local-cloud", 130 }, 131 }, 132 NetPort: 17070, 133 }, { 134 SpaceAddress: network.SpaceAddress{ 135 MachineAddress: network.MachineAddress{ 136 Value: "127.0.0.1", 137 Type: "ipv4", 138 Scope: "local-machine", 139 }, 140 }, 141 NetPort: 17070, 142 }, 143 }, { 144 { 145 SpaceAddress: network.SpaceAddress{ 146 MachineAddress: network.MachineAddress{ 147 Value: "10.144.9.62", 148 Type: "ipv4", 149 Scope: "local-cloud", 150 }, 151 }, 152 NetPort: 17070, 153 }, { 154 SpaceAddress: network.SpaceAddress{ 155 MachineAddress: network.MachineAddress{ 156 Value: "127.0.0.1", 157 Type: "ipv4", 158 Scope: "local-machine", 159 }, 160 }, 161 NetPort: 17070, 162 }, 163 }, { 164 { 165 SpaceAddress: network.SpaceAddress{ 166 MachineAddress: network.MachineAddress{ 167 Value: "10.144.9.56", 168 Type: "ipv4", 169 Scope: "local-cloud", 170 }, 171 }, 172 NetPort: 17070, 173 }, { 174 SpaceAddress: network.SpaceAddress{ 175 MachineAddress: network.MachineAddress{ 176 Value: "127.0.0.1", 177 Type: "ipv4", 178 Scope: "local-machine", 179 }, 180 }, 181 NetPort: 17070, 182 }, 183 }, 184 } 185 // second is the same as first with the first set of machines at the 186 // end rather than the start. 187 second := []network.SpaceHostPorts{ 188 { 189 { 190 SpaceAddress: network.SpaceAddress{ 191 MachineAddress: network.MachineAddress{ 192 Value: "10.144.9.62", 193 Type: "ipv4", 194 Scope: "local-cloud", 195 }, 196 }, 197 NetPort: 17070, 198 }, { 199 SpaceAddress: network.SpaceAddress{ 200 MachineAddress: network.MachineAddress{ 201 Value: "127.0.0.1", 202 Type: "ipv4", 203 Scope: "local-machine", 204 }, 205 }, 206 NetPort: 17070, 207 }, 208 }, { 209 { 210 SpaceAddress: network.SpaceAddress{ 211 MachineAddress: network.MachineAddress{ 212 Value: "10.144.9.56", 213 Type: "ipv4", 214 Scope: "local-cloud", 215 }, 216 }, 217 NetPort: 17070, 218 }, { 219 SpaceAddress: network.SpaceAddress{ 220 MachineAddress: network.MachineAddress{ 221 Value: "127.0.0.1", 222 Type: "ipv4", 223 Scope: "local-machine", 224 }, 225 }, 226 NetPort: 17070, 227 }, 228 }, { 229 { 230 SpaceAddress: network.SpaceAddress{ 231 MachineAddress: network.MachineAddress{ 232 Value: "10.144.9.113", 233 Type: "ipv4", 234 Scope: "local-cloud", 235 }, 236 }, 237 NetPort: 17070, 238 }, { 239 SpaceAddress: network.SpaceAddress{ 240 MachineAddress: network.MachineAddress{ 241 Value: "127.0.0.1", 242 Type: "ipv4", 243 Scope: "local-machine", 244 }, 245 }, 246 NetPort: 17070, 247 }, 248 }, 249 } 250 c.Assert(hostsPortsEqual(first, second), jc.IsTrue) 251 } 252 253 func (s *AddressEqualitySuite) TestAddressConversion(c *gc.C) { 254 machineAddress := network.SpaceAddress{ 255 MachineAddress: network.MachineAddress{ 256 Value: "foo", 257 Type: network.IPv4Address, 258 Scope: network.ScopePublic, 259 }, 260 } 261 stateAddress := fromNetworkAddress(machineAddress, "machine") 262 c.Assert(machineAddress, jc.DeepEquals, stateAddress.networkAddress()) 263 264 providerAddress := network.SpaceAddress{ 265 MachineAddress: network.MachineAddress{ 266 Value: "bar", 267 Type: network.IPv4Address, 268 Scope: network.ScopePublic, 269 }, 270 SpaceID: "666", 271 } 272 stateAddress = fromNetworkAddress(providerAddress, "provider") 273 c.Assert(providerAddress, jc.DeepEquals, stateAddress.networkAddress()) 274 }