github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/edgegateway_unit_test.go (about) 1 //go:build unit || ALL 2 3 /* 4 * Copyright 2019 VMware, Inc. All rights reserved. Licensed under the Apache v2 License. 5 */ 6 7 package govcd 8 9 import ( 10 "encoding/xml" 11 "fmt" 12 "regexp" 13 "strings" 14 "testing" 15 16 "github.com/vmware/go-vcloud-director/v2/types/v56" 17 ) 18 19 func TestGetPseudoUUID(t *testing.T) { 20 21 var seen = make(map[string]int) 22 23 reUuid := regexp.MustCompile(`^[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}$`) 24 for N := 0; N < 1000; N++ { 25 uuid, _ := getPseudoUuid() 26 if !reUuid.MatchString(uuid) { 27 t.Logf("string %s doesn't look like a UUID", uuid) 28 t.Fail() 29 } 30 previous, found := seen[uuid] 31 if found { 32 t.Logf("uuid %s already in the generated list at position %d", uuid, previous) 33 t.Fail() 34 } 35 seen[uuid] = N 36 } 37 } 38 39 func Test_getVnicIndexFromNetworkNameType(t *testing.T) { 40 // Fake body - has record for network my-vdc-int-net2 twice. 41 sampleBody := []byte(` 42 <edgeInterfaces> 43 <edgeInterface> 44 <name>my-vdc-int-net</name> 45 <type>internal</type> 46 <index>1</index> 47 <networkReference> 48 <id>95bffe8e-7e67-452d-abf2-535ac298db2b</id> 49 <name>my-vdc-int-net</name> 50 <type>com.vmware.vcloud.entity.network</type> 51 </networkReference> 52 <addressGroups> 53 <addressGroup> 54 <primaryAddress>10.10.10.5</primaryAddress> 55 <subnetMask>255.255.255.0</subnetMask> 56 <subnetPrefixLength>24</subnetPrefixLength> 57 </addressGroup> 58 </addressGroups> 59 <portgroupId>95bffe8e-7e67-452d-abf2-535ac298db2b</portgroupId> 60 <portgroupName>my-vdc-int-net</portgroupName> 61 <isConnected>true</isConnected> 62 </edgeInterface> 63 <edgeInterface> 64 <name>my-ext-network</name> 65 <type>uplink</type> 66 <index>0</index> 67 <networkReference> 68 <id>f2547dd9-e0f7-4d81-97c1-dd33e5e0fbbf</id> 69 <name>my-ext-network</name> 70 <type>com.vmware.vcloud.entity.network</type> 71 </networkReference> 72 <addressGroups> 73 <addressGroup> 74 <primaryAddress>192.168.1.110</primaryAddress> 75 <secondaryAddresses> 76 <ipAddress>192.168.1.118</ipAddress> 77 <ipAddress>192.168.1.115</ipAddress> 78 <ipAddress>192.168.1.116</ipAddress> 79 <ipAddress>192.168.1.117</ipAddress> 80 </secondaryAddresses> 81 <subnetMask>255.255.255.0</subnetMask> 82 <subnetPrefixLength>24</subnetPrefixLength> 83 </addressGroup> 84 </addressGroups> 85 <portgroupId>f2547dd9-e0f7-4d81-97c1-dd33e5e0fbbf</portgroupId> 86 <portgroupName></portgroupName> 87 <isConnected>true</isConnected> 88 </edgeInterface> 89 <edgeInterface> 90 <name>Distributed Router Transit</name> 91 <type>internal</type> 92 <index>4</index> 93 <networkReference> 94 <id>00251e01-16ed-4367-a0cb-58195d21f367</id> 95 <name>Distributed Router Transit</name> 96 <type>com.vmware.vcloud.entity.network</type> 97 </networkReference> 98 <addressGroups> 99 <addressGroup> 100 <primaryAddress>10.255.255.249</primaryAddress> 101 <subnetMask>255.255.255.252</subnetMask> 102 <subnetPrefixLength>30</subnetPrefixLength> 103 </addressGroup> 104 </addressGroups> 105 <portgroupId>00251e01-16ed-4367-a0cb-58195d21f367</portgroupId> 106 <portgroupName>DLR_to_EDGE_my-edge-gw</portgroupName> 107 <isConnected>true</isConnected> 108 </edgeInterface> 109 <edgeInterface> 110 <name>my-vdc-int-net2</name> 111 <type>internal</type> 112 <index>3</index> 113 <networkReference> 114 <id>96a68fd4-c21a-41d6-98a4-fbf32c96480f</id> 115 <name>my-vdc-int-net2</name> 116 <type>com.vmware.vcloud.entity.network</type> 117 </networkReference> 118 <addressGroups> 119 <addressGroup> 120 <primaryAddress>13.13.1.1</primaryAddress> 121 <subnetMask>255.255.255.0</subnetMask> 122 <subnetPrefixLength>24</subnetPrefixLength> 123 </addressGroup> 124 </addressGroups> 125 <portgroupId>96a68fd4-c21a-41d6-98a4-fbf32c96480f</portgroupId> 126 <portgroupName>my-vdc-int-net2</portgroupName> 127 <isConnected>true</isConnected> 128 </edgeInterface> 129 <edgeInterface> 130 <name>my-vdc-int-net2</name> 131 <type>internal</type> 132 <index>3</index> 133 <networkReference> 134 <id>96a68fd4-c21a-41d6-98a4-fbf32c96480f</id> 135 <name>my-vdc-int-net2</name> 136 <type>com.vmware.vcloud.entity.network</type> 137 </networkReference> 138 <addressGroups> 139 <addressGroup> 140 <primaryAddress>13.13.1.1</primaryAddress> 141 <subnetMask>255.255.255.0</subnetMask> 142 <subnetPrefixLength>24</subnetPrefixLength> 143 </addressGroup> 144 </addressGroups> 145 <portgroupId>96a68fd4-c21a-41d6-98a4-fbf32c96480f</portgroupId> 146 <portgroupName>my-vdc-int-net2</portgroupName> 147 <isConnected>true</isConnected> 148 </edgeInterface> 149 <edgeInterface> 150 <name>subinterfaced-net</name> 151 <type>subinterface</type> 152 <index>10</index> 153 <networkReference> 154 <id>2d3e46cb-afe6-4725-9f0a-63514ebac840</id> 155 <name>subinterfaced-net</name> 156 <type>com.vmware.vcloud.entity.network</type> 157 </networkReference> 158 <addressGroups> 159 <addressGroup> 160 <primaryAddress>9.9.9.1</primaryAddress> 161 <subnetMask>255.255.255.0</subnetMask> 162 <subnetPrefixLength>24</subnetPrefixLength> 163 </addressGroup> 164 </addressGroups> 165 <portgroupId>2d3e46cb-afe6-4725-9f0a-63514ebac840</portgroupId> 166 <portgroupName>subinterfaced-net</portgroupName> 167 <isConnected>true</isConnected> 168 <tunnelId>1</tunnelId> 169 </edgeInterface> 170 <edgeInterface> 171 <name>subinterface2</name> 172 <type>subinterface</type> 173 <index>11</index> 174 <networkReference> 175 <id>3e10dd56-2a3a-47bd-aac8-07fc3f653baa</id> 176 <name>subinterface2</name> 177 <type>com.vmware.vcloud.entity.network</type> 178 </networkReference> 179 <addressGroups> 180 <addressGroup> 181 <primaryAddress>55.55.55.1</primaryAddress> 182 <subnetMask>255.255.255.0</subnetMask> 183 <subnetPrefixLength>24</subnetPrefixLength> 184 </addressGroup> 185 </addressGroups> 186 <portgroupId>3e10dd56-2a3a-47bd-aac8-07fc3f653baa</portgroupId> 187 <portgroupName>subinterface2</portgroupName> 188 <isConnected>true</isConnected> 189 <tunnelId>2</tunnelId> 190 </edgeInterface> 191 <edgeInterface> 192 <name>distributd-net</name> 193 <type>distributed</type> 194 <index>10</index> 195 <networkReference> 196 <id>0f1cc84b-517b-4435-9f7d-e42eacea1e19</id> 197 <name>distributd-net</name> 198 <type>com.vmware.vcloud.entity.network</type> 199 </networkReference> 200 <addressGroups> 201 <addressGroup> 202 <primaryAddress>65.65.65.1</primaryAddress> 203 <subnetMask>255.255.255.0</subnetMask> 204 <subnetPrefixLength>24</subnetPrefixLength> 205 </addressGroup> 206 </addressGroups> 207 <portgroupId>0f1cc84b-517b-4435-9f7d-e42eacea1e19</portgroupId> 208 <portgroupName>distributd-net</portgroupName> 209 <isConnected>true</isConnected> 210 </edgeInterface> 211 <edgeInterface> 212 <name>distri-2</name> 213 <type>distributed</type> 214 <index>11</index> 215 <networkReference> 216 <id>8d09d23c-fd08-4c34-9ad7-21afb629cd99</id> 217 <name>distri-2</name> 218 <type>com.vmware.vcloud.entity.network</type> 219 </networkReference> 220 <addressGroups> 221 <addressGroup> 222 <primaryAddress>77.77.77.1</primaryAddress> 223 <subnetMask>255.255.255.0</subnetMask> 224 <subnetPrefixLength>24</subnetPrefixLength> 225 </addressGroup> 226 </addressGroups> 227 <portgroupId>8d09d23c-fd08-4c34-9ad7-21afb629cd99</portgroupId> 228 <portgroupName>distri-2</portgroupName> 229 <isConnected>true</isConnected> 230 </edgeInterface> 231 </edgeInterfaces>`) 232 vnicObject := &types.EdgeGatewayInterfaces{} 233 _ = xml.Unmarshal(sampleBody, vnicObject) 234 235 tests := []struct { 236 name string 237 networkName string 238 networkType string 239 expectedVnicIndex *int 240 hasError bool 241 expectedError error 242 }{ 243 {"ExtNetwork", "my-ext-network", types.EdgeGatewayVnicTypeUplink, addrOf(0), false, nil}, 244 {"OrgNetwork", "my-vdc-int-net", types.EdgeGatewayVnicTypeInternal, addrOf(1), false, nil}, 245 {"WithSubinterfaces", "subinterfaced-net", types.EdgeGatewayVnicTypeSubinterface, addrOf(10), false, nil}, 246 {"WithSubinterfaces2", "subinterface2", types.EdgeGatewayVnicTypeSubinterface, addrOf(11), false, nil}, 247 {"NonExistingUplink", "invalid-network-name", types.EdgeGatewayVnicTypeUplink, nil, true, ErrorEntityNotFound}, 248 {"NonExistingInternal", "invalid-network-name", types.EdgeGatewayVnicTypeInternal, nil, true, ErrorEntityNotFound}, 249 {"NonExistingSubinterface", "invalid-network-name", types.EdgeGatewayVnicTypeSubinterface, nil, true, ErrorEntityNotFound}, 250 {"NonExistingTrunk", "invalid-network-name", types.EdgeGatewayVnicTypeTrunk, nil, true, ErrorEntityNotFound}, 251 {"MoreThanOne", "my-vdc-int-net2", types.EdgeGatewayVnicTypeInternal, nil, true, 252 fmt.Errorf("more than one (2) networks of type 'internal' with name 'my-vdc-int-net2' found")}, 253 } 254 for _, tt := range tests { 255 t.Run(tt.name, func(t *testing.T) { 256 vnicIndex, err := getVnicIndexByNetworkNameAndType(tt.networkName, tt.networkType, vnicObject) 257 258 if !tt.hasError && err != nil { 259 t.Errorf("error was not expected: %s", err) 260 } 261 262 if tt.hasError && !strings.Contains(err.Error(), tt.expectedError.Error()) { 263 t.Errorf("Got unexpected error: %s, expected: %s", err, tt.expectedError) 264 } 265 266 if vnicIndex != nil && tt.expectedVnicIndex != nil && *vnicIndex != *tt.expectedVnicIndex { 267 t.Errorf("Got unexpected vNic name: %d, expected: %d", vnicIndex, tt.expectedVnicIndex) 268 } 269 270 }) 271 } 272 273 } 274 275 func Test_getNetworkNameTypeFromVnicIndex(t *testing.T) { 276 // sample body for unit testing. The vNic 8 record is duplicated on purpose for testing. 277 sampleBody := []byte(` 278 <edgeInterfaces> 279 <edgeInterface> 280 <name>my-vdc-int-net</name> 281 <type>internal</type> 282 <index>1</index> 283 <networkReference> 284 <id>95bffe8e-7e67-452d-abf2-535ac298db2b</id> 285 <name>my-vdc-int-net</name> 286 <type>com.vmware.vcloud.entity.network</type> 287 </networkReference> 288 <addressGroups> 289 <addressGroup> 290 <primaryAddress>10.10.10.5</primaryAddress> 291 <subnetMask>255.255.255.0</subnetMask> 292 <subnetPrefixLength>24</subnetPrefixLength> 293 </addressGroup> 294 </addressGroups> 295 <portgroupId>95bffe8e-7e67-452d-abf2-535ac298db2b</portgroupId> 296 <portgroupName>my-vdc-int-net</portgroupName> 297 <isConnected>true</isConnected> 298 </edgeInterface> 299 <edgeInterface> 300 <name>my-ext-network</name> 301 <type>uplink</type> 302 <index>0</index> 303 <networkReference> 304 <id>f2547dd9-e0f7-4d81-97c1-dd33e5e0fbbf</id> 305 <name>my-ext-network</name> 306 <type>com.vmware.vcloud.entity.network</type> 307 </networkReference> 308 <addressGroups> 309 <addressGroup> 310 <primaryAddress>192.168.1.110</primaryAddress> 311 <secondaryAddresses> 312 <ipAddress>192.168.1.118</ipAddress> 313 <ipAddress>192.168.1.115</ipAddress> 314 <ipAddress>192.168.1.116</ipAddress> 315 <ipAddress>192.168.1.117</ipAddress> 316 </secondaryAddresses> 317 <subnetMask>255.255.255.0</subnetMask> 318 <subnetPrefixLength>24</subnetPrefixLength> 319 </addressGroup> 320 </addressGroups> 321 <portgroupId>f2547dd9-e0f7-4d81-97c1-dd33e5e0fbbf</portgroupId> 322 <portgroupName>my-ext-network</portgroupName> 323 <isConnected>true</isConnected> 324 </edgeInterface> 325 <edgeInterface> 326 <name>Distributed Router Transit</name> 327 <type>internal</type> 328 <index>4</index> 329 <networkReference> 330 <id>00251e01-16ed-4367-a0cb-58195d21f367</id> 331 <name>Distributed Router Transit</name> 332 <type>com.vmware.vcloud.entity.network</type> 333 </networkReference> 334 <addressGroups> 335 <addressGroup> 336 <primaryAddress>10.255.255.249</primaryAddress> 337 <subnetMask>255.255.255.252</subnetMask> 338 <subnetPrefixLength>30</subnetPrefixLength> 339 </addressGroup> 340 </addressGroups> 341 <portgroupId>00251e01-16ed-4367-a0cb-58195d21f367</portgroupId> 342 <portgroupName>DLR_to_EDGE_my-edge-gw</portgroupName> 343 <isConnected>true</isConnected> 344 </edgeInterface> 345 <edgeInterface> 346 <name>my-vdc-int-net2</name> 347 <type>internal</type> 348 <index>3</index> 349 <networkReference> 350 <id>96a68fd4-c21a-41d6-98a4-fbf32c96480f</id> 351 <name>my-vdc-int-net2</name> 352 <type>com.vmware.vcloud.entity.network</type> 353 </networkReference> 354 <addressGroups> 355 <addressGroup> 356 <primaryAddress>13.13.1.1</primaryAddress> 357 <subnetMask>255.255.255.0</subnetMask> 358 <subnetPrefixLength>24</subnetPrefixLength> 359 </addressGroup> 360 </addressGroups> 361 <portgroupId>96a68fd4-c21a-41d6-98a4-fbf32c96480f</portgroupId> 362 <portgroupName>my-vdc-int-net2</portgroupName> 363 <isConnected>true</isConnected> 364 </edgeInterface> 365 <edgeInterface> 366 <name>subinterfaced-net</name> 367 <type>subinterface</type> 368 <index>10</index> 369 <networkReference> 370 <id>2d3e46cb-afe6-4725-9f0a-63514ebac840</id> 371 <name>subinterfaced-net</name> 372 <type>com.vmware.vcloud.entity.network</type> 373 </networkReference> 374 <addressGroups> 375 <addressGroup> 376 <primaryAddress>9.9.9.1</primaryAddress> 377 <subnetMask>255.255.255.0</subnetMask> 378 <subnetPrefixLength>24</subnetPrefixLength> 379 </addressGroup> 380 </addressGroups> 381 <portgroupId>2d3e46cb-afe6-4725-9f0a-63514ebac840</portgroupId> 382 <portgroupName>subinterfaced-net</portgroupName> 383 <isConnected>true</isConnected> 384 <tunnelId>1</tunnelId> 385 </edgeInterface> 386 </edgeInterfaces>`) 387 vnicObject := &types.EdgeGatewayInterfaces{} 388 _ = xml.Unmarshal(sampleBody, vnicObject) 389 390 tests := []struct { 391 name string 392 vnicIndex int 393 expectednetworkName string 394 expectednetworkType string 395 hasError bool 396 expectedError error 397 }{ 398 {"ExternalUplink", 0, "my-ext-network", "uplink", false, nil}, 399 {"Internal", 1, "my-vdc-int-net", "internal", false, nil}, 400 {"Subinterface", 10, "subinterfaced-net", "subinterface", false, nil}, 401 {"NonExistent", 219, "", "", true, ErrorEntityNotFound}, 402 } 403 for _, tt := range tests { 404 t.Run(tt.name, func(t *testing.T) { 405 networkName, networkType, err := getNetworkNameAndTypeByVnicIndex(tt.vnicIndex, vnicObject) 406 407 if !tt.hasError && err != nil { 408 t.Errorf("error was not expected: %s", err) 409 } 410 411 if tt.hasError && !strings.Contains(err.Error(), tt.expectedError.Error()) { 412 t.Errorf("Got unexpected error: %s, expected: %s", err, tt.expectedError) 413 } 414 415 if networkName != tt.expectednetworkName { 416 t.Errorf("Got unexpected network name: %s, expected: %s", networkName, tt.expectednetworkName) 417 } 418 419 if networkType != tt.expectednetworkType { 420 t.Errorf("Got unexpected vNic name: %s, expected: %s", networkType, tt.expectednetworkType) 421 } 422 423 }) 424 } 425 }