github.com/sacloud/iaas-api-go@v1.12.0/test/vpc_router_op_test.go (about) 1 // Copyright 2022-2023 The sacloud/iaas-api-go Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package test 16 17 import ( 18 "testing" 19 "time" 20 21 "github.com/sacloud/iaas-api-go" 22 "github.com/sacloud/iaas-api-go/helper/power" 23 "github.com/sacloud/iaas-api-go/testutil" 24 "github.com/sacloud/iaas-api-go/types" 25 ) 26 27 func TestVPCRouterOp_CRUD(t *testing.T) { 28 testutil.RunCRUD(t, &testutil.CRUDTestCase{ 29 Parallel: true, 30 SetupAPICallerFunc: singletonAPICaller, 31 Create: &testutil.CRUDTestFunc{ 32 Func: testVPCRouterCreate(createVPCRouterParam), 33 CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{ 34 ExpectValue: createVPCRouterExpected, 35 IgnoreFields: ignoreVPCRouterFields, 36 }), 37 }, 38 39 Read: &testutil.CRUDTestFunc{ 40 Func: testVPCRouterRead, 41 CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{ 42 ExpectValue: createVPCRouterExpected, 43 IgnoreFields: ignoreVPCRouterFields, 44 }), 45 }, 46 47 Updates: []*testutil.CRUDTestFunc{ 48 { 49 Func: testVPCRouterUpdate(updateVPCRouterParam), 50 CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{ 51 ExpectValue: updateVPCRouterExpected, 52 IgnoreFields: ignoreVPCRouterFields, 53 }), 54 }, 55 }, 56 57 Shutdown: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) error { 58 client := iaas.NewVPCRouterOp(caller) 59 return power.ShutdownVPCRouter(ctx, client, testZone, ctx.ID, true) 60 }, 61 62 Delete: &testutil.CRUDTestDeleteFunc{ 63 Func: testVPCRouterDelete, 64 }, 65 }) 66 } 67 68 var ( 69 ignoreVPCRouterFields = []string{ 70 "ID", 71 "Availability", 72 "Class", 73 "CreatedAt", 74 "SettingsHash", 75 "Settings", 76 "InstanceHostName", 77 "InstanceHostInfoURL", 78 "InstanceStatus", 79 "InstanceStatusChangedAt", 80 "Interfaces", 81 "ZoneID", 82 } 83 84 createVPCRouterParam = &iaas.VPCRouterCreateRequest{ 85 PlanID: types.VPCRouterPlans.Standard, 86 Switch: &iaas.ApplianceConnectedSwitch{ 87 Scope: types.Scopes.Shared, 88 }, 89 Name: testutil.ResourceName("vpc-router"), 90 Description: "desc", 91 Tags: []string{"tag1", "tag2"}, 92 Settings: &iaas.VPCRouterSetting{}, 93 } 94 createVPCRouterExpected = &iaas.VPCRouter{ 95 Class: "vpcrouter", 96 Name: createVPCRouterParam.Name, 97 Description: createVPCRouterParam.Description, 98 Tags: createVPCRouterParam.Tags, 99 Availability: types.Availabilities.Available, 100 InstanceStatus: types.ServerInstanceStatuses.Up, 101 PlanID: createVPCRouterParam.PlanID, 102 Version: 2, 103 Settings: createVPCRouterParam.Settings, 104 } 105 updateVPCRouterParam = &iaas.VPCRouterUpdateRequest{ 106 Name: testutil.ResourceName("vpc-router-upd"), 107 Tags: []string{"tag1-upd", "tag2-upd"}, 108 Description: "desc-upd", 109 } 110 updateVPCRouterExpected = &iaas.VPCRouter{ 111 Class: "vpcrouter", 112 Name: updateVPCRouterParam.Name, 113 Description: updateVPCRouterParam.Description, 114 Tags: updateVPCRouterParam.Tags, 115 Availability: types.Availabilities.Available, 116 InstanceStatus: types.ServerInstanceStatuses.Up, 117 PlanID: createVPCRouterParam.PlanID, 118 Version: 2, 119 } 120 ) 121 122 func testVPCRouterCreate(createParam *iaas.VPCRouterCreateRequest) func(*testutil.CRUDTestContext, iaas.APICaller) (interface{}, error) { 123 return func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 124 client := iaas.NewVPCRouterOp(caller) 125 vpcRouter, err := client.Create(ctx, testZone, createParam) 126 if err != nil { 127 return nil, err 128 } 129 130 n, err := iaas.WaiterForReady(func() (interface{}, error) { 131 return client.Read(ctx, testZone, vpcRouter.ID) 132 }).WaitForState(ctx) 133 if err != nil { 134 return nil, err 135 } 136 137 if err := client.Boot(ctx, testZone, vpcRouter.ID); err != nil { 138 return nil, err 139 } 140 141 return n.(*iaas.VPCRouter), nil 142 } 143 } 144 145 func testVPCRouterRead(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 146 client := iaas.NewVPCRouterOp(caller) 147 return client.Read(ctx, testZone, ctx.ID) 148 } 149 150 func testVPCRouterUpdate(updateParam *iaas.VPCRouterUpdateRequest) func(*testutil.CRUDTestContext, iaas.APICaller) (interface{}, error) { 151 return func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 152 client := iaas.NewVPCRouterOp(caller) 153 return client.Update(ctx, testZone, ctx.ID, updateParam) 154 } 155 } 156 157 func testVPCRouterDelete(ctx *testutil.CRUDTestContext, caller iaas.APICaller) error { 158 client := iaas.NewVPCRouterOp(caller) 159 return client.Delete(ctx, testZone, ctx.ID) 160 } 161 162 var fakeWireGuardPublicKey = `fqxOlS2X0Jtg4P9zVf8D3BAUtJmrp+z2mjzUmgxxxxx=` 163 164 func TestVPCRouterOp_WithRouterCRUD(t *testing.T) { 165 testutil.RunCRUD(t, &testutil.CRUDTestCase{ 166 Parallel: true, 167 SetupAPICallerFunc: singletonAPICaller, 168 169 Setup: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) error { 170 routerOp := iaas.NewInternetOp(caller) 171 created, err := routerOp.Create(ctx, testZone, &iaas.InternetCreateRequest{ 172 Name: testutil.ResourceName("internet-for-vpc-router"), 173 BandWidthMbps: 100, 174 NetworkMaskLen: 28, 175 }) 176 if err != nil { 177 return err 178 } 179 180 ctx.Values["vpcrouter/internet"] = created.ID 181 max := 30 182 for { 183 if max == 0 { 184 break 185 } 186 _, err := routerOp.Read(ctx, testZone, created.ID) 187 if err != nil || iaas.IsNotFoundError(err) { 188 max-- 189 time.Sleep(3 * time.Second) 190 continue 191 } 192 break 193 } 194 195 swOp := iaas.NewSwitchOp(caller) 196 sw, err := swOp.Read(ctx, testZone, created.Switch.ID) 197 if err != nil { 198 return err 199 } 200 201 ipaddresses := sw.Subnets[0].GetAssignedIPAddresses() 202 p := withRouterCreateVPCRouterParam 203 p.Switch = &iaas.ApplianceConnectedSwitch{ 204 ID: sw.ID, 205 } 206 p.IPAddresses = []string{ipaddresses[1], ipaddresses[2]} 207 p.Settings = &iaas.VPCRouterSetting{ 208 VRID: 100, 209 InternetConnectionEnabled: true, 210 Interfaces: []*iaas.VPCRouterInterfaceSetting{ 211 { 212 VirtualIPAddress: ipaddresses[0], 213 IPAddress: []string{ipaddresses[1], ipaddresses[2]}, 214 IPAliases: []string{ipaddresses[3]}, 215 NetworkMaskLen: sw.Subnets[0].NetworkMaskLen, 216 }, 217 }, 218 } 219 220 withRouterCreateVPCRouterExpected.Settings = p.Settings 221 return nil 222 }, 223 Create: &testutil.CRUDTestFunc{ 224 Func: testVPCRouterCreate(withRouterCreateVPCRouterParam), 225 CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{ 226 ExpectValue: withRouterCreateVPCRouterExpected, 227 IgnoreFields: ignoreVPCRouterFields, 228 }), 229 }, 230 231 Read: &testutil.CRUDTestFunc{ 232 Func: testVPCRouterRead, 233 CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{ 234 ExpectValue: withRouterCreateVPCRouterExpected, 235 IgnoreFields: ignoreVPCRouterFields, 236 }), 237 }, 238 239 Updates: []*testutil.CRUDTestFunc{ 240 { 241 Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 242 if isAccTest() { 243 // 起動直後だとシャットダウンできない場合があるため20秒ほど待つ 244 time.Sleep(20 * time.Second) 245 } 246 247 vpcOp := iaas.NewVPCRouterOp(caller) 248 // shutdown 249 if err := vpcOp.Shutdown(ctx, testZone, ctx.ID, &iaas.ShutdownOption{Force: true}); err != nil { 250 return nil, err 251 } 252 _, err := iaas.WaiterForDown(func() (interface{}, error) { 253 return vpcOp.Read(ctx, testZone, ctx.ID) 254 }).WaitForState(ctx) 255 if err != nil { 256 return nil, err 257 } 258 259 swOp := iaas.NewSwitchOp(caller) 260 sw, err := swOp.Create(ctx, testZone, &iaas.SwitchCreateRequest{ 261 Name: testutil.ResourceName("switch-for-vpc-router"), 262 }) 263 if err != nil { 264 return nil, err 265 } 266 ctx.Values["vpcrouter/switch"] = sw.ID 267 268 // connect to switch 269 if err := vpcOp.ConnectToSwitch(ctx, testZone, ctx.ID, 2, sw.ID); err != nil { 270 return nil, err 271 } 272 273 // setup update param 274 p := withRouterUpdateVPCRouterParam 275 p.Settings = &iaas.VPCRouterSetting{ 276 VRID: 10, 277 SyslogHost: "192.168.2.199", 278 InternetConnectionEnabled: true, 279 Interfaces: []*iaas.VPCRouterInterfaceSetting{ 280 withRouterCreateVPCRouterParam.Settings.Interfaces[0], 281 { 282 VirtualIPAddress: "192.168.2.1", 283 IPAddress: []string{"192.168.2.11", "192.168.2.12"}, 284 NetworkMaskLen: 24, 285 Index: 2, 286 }, 287 }, 288 StaticNAT: []*iaas.VPCRouterStaticNAT{ 289 { 290 GlobalAddress: withRouterCreateVPCRouterParam.Settings.Interfaces[0].IPAliases[0], 291 PrivateAddress: "192.168.2.1", 292 }, 293 }, 294 PortForwarding: []*iaas.VPCRouterPortForwarding{ 295 { 296 Protocol: types.VPCRouterPortForwardingProtocols.TCP, 297 GlobalPort: 22, 298 PrivateAddress: "192.168.2.2", 299 PrivatePort: 10022, 300 Description: "port forwarding", 301 }, 302 }, 303 DHCPServer: []*iaas.VPCRouterDHCPServer{ 304 { 305 Interface: "eth2", 306 RangeStart: "192.168.2.51", 307 RangeStop: "192.168.2.60", 308 }, 309 }, 310 DHCPStaticMapping: []*iaas.VPCRouterDHCPStaticMapping{ 311 { 312 MACAddress: "aa:bb:cc:dd:ee:ff", 313 IPAddress: "192.168.2.21", 314 }, 315 }, 316 DNSForwarding: &iaas.VPCRouterDNSForwarding{ 317 Interface: "eth2", 318 DNSServers: []string{"133.242.0.3", "133.242.0.4"}, 319 }, 320 PPTPServer: &iaas.VPCRouterPPTPServer{ 321 RangeStart: "192.168.2.61", 322 RangeStop: "192.168.2.70", 323 }, 324 PPTPServerEnabled: true, 325 L2TPIPsecServer: &iaas.VPCRouterL2TPIPsecServer{ 326 RangeStart: "192.168.2.71", 327 RangeStop: "192.168.2.80", 328 PreSharedSecret: "presharedsecret", 329 }, 330 L2TPIPsecServerEnabled: true, 331 WireGuard: &iaas.VPCRouterWireGuard{ 332 IPAddress: "192.168.3.1/24", 333 Peers: []*iaas.VPCRouterWireGuardPeer{ 334 { 335 Name: "foobar", 336 IPAddress: "192.168.3.11", 337 PublicKey: fakeWireGuardPublicKey, 338 }, 339 }, 340 }, 341 WireGuardEnabled: true, 342 RemoteAccessUsers: []*iaas.VPCRouterRemoteAccessUser{ 343 { 344 UserName: "user1", 345 Password: "password1", 346 }, 347 }, 348 SiteToSiteIPsecVPN: &iaas.VPCRouterSiteToSiteIPsecVPN{ 349 Config: []*iaas.VPCRouterSiteToSiteIPsecVPNConfig{ 350 { 351 Peer: "1.2.3.4", 352 PreSharedSecret: "presharedsecret", 353 RemoteID: "1.2.3.4", 354 Routes: []string{"10.0.0.0/24"}, 355 LocalPrefix: []string{"192.168.2.0/24"}, 356 }, 357 }, 358 IKE: &iaas.VPCRouterSiteToSiteIPsecVPNIKE{ 359 Lifetime: 28801, 360 DPD: &iaas.VPCRouterSiteToSiteIPsecVPNIKEDPD{ 361 Interval: 16, 362 Timeout: 31, 363 }, 364 }, 365 ESP: &iaas.VPCRouterSiteToSiteIPsecVPNESP{ 366 Lifetime: 1801, 367 }, 368 EncryptionAlgo: types.VPCRouterSiteToSiteVPNEncryptionAlgoAES256, 369 HashAlgo: types.VPCRouterSiteToSiteVPNHashAlgoSHA256, 370 DHGroup: types.VPCRouterSiteToSiteVPNDHGroupModp2048, 371 }, 372 StaticRoute: []*iaas.VPCRouterStaticRoute{ 373 { 374 Prefix: "172.16.0.0/16", 375 NextHop: "192.168.2.11", 376 }, 377 }, 378 ScheduledMaintenance: &iaas.VPCRouterScheduledMaintenance{ 379 DayOfWeek: 1, 380 Hour: 2, 381 }, 382 } 383 384 withRouterUpdateVPCRouterExpected.Settings = p.Settings 385 return testVPCRouterUpdate(withRouterUpdateVPCRouterParam)(ctx, caller) 386 }, 387 CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{ 388 ExpectValue: withRouterUpdateVPCRouterExpected, 389 IgnoreFields: ignoreVPCRouterFields, 390 }), 391 }, 392 { 393 Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 394 // setup update param 395 p := withRouterUpdateVPCRouterToMinParam 396 p.Settings = &iaas.VPCRouterSetting{ 397 VRID: 10, 398 InternetConnectionEnabled: false, 399 Interfaces: []*iaas.VPCRouterInterfaceSetting{ 400 withRouterCreateVPCRouterParam.Settings.Interfaces[0], 401 }, 402 } 403 404 withRouterUpdateVPCRouterToMinExpected.Settings = p.Settings 405 return testVPCRouterUpdate(withRouterUpdateVPCRouterToMinParam)(ctx, caller) 406 }, 407 CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{ 408 ExpectValue: withRouterUpdateVPCRouterToMinExpected, 409 IgnoreFields: ignoreVPCRouterFields, 410 }), 411 }, 412 }, 413 414 Shutdown: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) error { 415 client := iaas.NewVPCRouterOp(caller) 416 return power.ShutdownVPCRouter(ctx, client, testZone, ctx.ID, true) 417 }, 418 419 Delete: &testutil.CRUDTestDeleteFunc{ 420 Func: testVPCRouterDelete, 421 }, 422 423 Cleanup: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) error { 424 routerOp := iaas.NewInternetOp(caller) 425 routerID, ok := ctx.Values["vpcrouter/internet"] 426 if ok { 427 if err := routerOp.Delete(ctx, testZone, routerID.(types.ID)); err != nil { 428 return err 429 } 430 } 431 432 swOp := iaas.NewSwitchOp(caller) 433 switchID, ok := ctx.Values["vpcrouter/switch"] 434 if ok { 435 if err := swOp.Delete(ctx, testZone, switchID.(types.ID)); err != nil { 436 return err 437 } 438 } 439 return nil 440 }, 441 }) 442 } 443 444 var ( 445 withRouterCreateVPCRouterParam = &iaas.VPCRouterCreateRequest{ 446 PlanID: types.VPCRouterPlans.Premium, 447 Name: testutil.ResourceName("vpc-router"), 448 Description: "desc", 449 Tags: []string{"tag1", "tag2"}, 450 } 451 withRouterCreateVPCRouterExpected = &iaas.VPCRouter{ 452 Class: "vpcrouter", 453 Name: withRouterCreateVPCRouterParam.Name, 454 Description: withRouterCreateVPCRouterParam.Description, 455 Tags: withRouterCreateVPCRouterParam.Tags, 456 Availability: types.Availabilities.Available, 457 InstanceStatus: types.ServerInstanceStatuses.Up, 458 PlanID: withRouterCreateVPCRouterParam.PlanID, 459 Version: 2, 460 Settings: withRouterCreateVPCRouterParam.Settings, 461 } 462 withRouterUpdateVPCRouterParam = &iaas.VPCRouterUpdateRequest{ 463 Name: testutil.ResourceName("vpc-router-upd"), 464 Tags: []string{"tag1-upd", "tag2-upd"}, 465 Description: "desc-upd", 466 IconID: testIconID, 467 } 468 withRouterUpdateVPCRouterExpected = &iaas.VPCRouter{ 469 Class: "vpcrouter", 470 Name: withRouterUpdateVPCRouterParam.Name, 471 Description: withRouterUpdateVPCRouterParam.Description, 472 Tags: withRouterUpdateVPCRouterParam.Tags, 473 Availability: types.Availabilities.Available, 474 InstanceStatus: types.ServerInstanceStatuses.Up, 475 PlanID: withRouterCreateVPCRouterParam.PlanID, 476 Version: 2, 477 Settings: withRouterUpdateVPCRouterParam.Settings, 478 IconID: testIconID, 479 } 480 withRouterUpdateVPCRouterToMinParam = &iaas.VPCRouterUpdateRequest{ 481 Name: testutil.ResourceName("vpc-router-to-min"), 482 } 483 withRouterUpdateVPCRouterToMinExpected = &iaas.VPCRouter{ 484 Class: "vpcrouter", 485 Name: withRouterUpdateVPCRouterToMinParam.Name, 486 Availability: types.Availabilities.Available, 487 InstanceStatus: types.ServerInstanceStatuses.Up, 488 PlanID: withRouterCreateVPCRouterParam.PlanID, 489 Version: 2, 490 Settings: withRouterUpdateVPCRouterToMinParam.Settings, 491 } 492 )