github.com/sacloud/iaas-api-go@v1.12.0/test/mobile_gateway_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 "os" 19 "testing" 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 TestMobileGatewayOpCRUD(t *testing.T) { 28 testutil.PreCheckEnvsFunc("SAKURACLOUD_SIM_ICCID", "SAKURACLOUD_SIM_PASSCODE")(t) 29 30 initMobileGatewayVariables() 31 32 testutil.RunCRUD(t, &testutil.CRUDTestCase{ 33 Parallel: true, 34 35 SetupAPICallerFunc: singletonAPICaller, 36 37 Create: &testutil.CRUDTestFunc{ 38 Func: testMobileGatewayCreate, 39 CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{ 40 ExpectValue: createMobileGatewayExpected, 41 IgnoreFields: ignoreMobileGatewayFields, 42 }), 43 }, 44 45 Read: &testutil.CRUDTestFunc{ 46 Func: testMobileGatewayRead, 47 CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{ 48 ExpectValue: createMobileGatewayExpected, 49 IgnoreFields: ignoreMobileGatewayFields, 50 }), 51 }, 52 53 Updates: []*testutil.CRUDTestFunc{ 54 { 55 Func: testMobileGatewayUpdate, 56 CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{ 57 ExpectValue: updateMobileGatewayExpected, 58 IgnoreFields: ignoreMobileGatewayFields, 59 }), 60 }, 61 { 62 Func: testMobileGatewayUpdateSettings, 63 CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{ 64 ExpectValue: updateMobileGatewaySettingsExpected, 65 IgnoreFields: ignoreMobileGatewayFields, 66 }), 67 }, 68 // shutdown(no check) 69 { 70 Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 71 mgwOp := iaas.NewMobileGatewayOp(caller) 72 // shutdown 73 if err := mgwOp.Shutdown(ctx, testZone, ctx.ID, &iaas.ShutdownOption{Force: true}); err != nil { 74 return nil, err 75 } 76 77 waiter := iaas.WaiterForDown(func() (interface{}, error) { 78 return mgwOp.Read(ctx, testZone, ctx.ID) 79 }) 80 81 return waiter.WaitForState(ctx) 82 }, 83 SkipExtractID: true, 84 }, 85 // connect to switch 86 { 87 Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 88 // prepare switch 89 swOp := iaas.NewSwitchOp(caller) 90 sw, err := swOp.Create(ctx, testZone, &iaas.SwitchCreateRequest{ 91 Name: testutil.ResourceName("switch-for-mobile-gateway"), 92 }) 93 if err != nil { 94 return nil, err 95 } 96 97 ctx.Values["mobile-gateway/switch"] = sw.ID 98 99 // connect 100 mgwOp := iaas.NewMobileGatewayOp(caller) 101 if err := mgwOp.ConnectToSwitch(ctx, testZone, ctx.ID, sw.ID); err != nil { 102 return nil, err 103 } 104 105 return mgwOp.Read(ctx, testZone, ctx.ID) 106 }, 107 CheckFunc: func(t testutil.TestT, ctx *testutil.CRUDTestContext, i interface{}) error { 108 mgw := i.(*iaas.MobileGateway) 109 return testutil.DoAsserts( 110 testutil.AssertLenFunc(t, mgw.Interfaces, 2, "len(MobileGateway.Interfaces)"), 111 ) 112 }, 113 SkipExtractID: true, 114 }, 115 // set IPAddress to eth1 116 { 117 Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 118 mgwOp := iaas.NewMobileGatewayOp(caller) 119 mgs, err := mgwOp.UpdateSettings(ctx, testZone, ctx.ID, &iaas.MobileGatewayUpdateSettingsRequest{ 120 InterfaceSettings: []*iaas.MobileGatewayInterfaceSetting{ 121 { 122 IPAddress: []string{"192.168.2.11"}, 123 NetworkMaskLen: 16, 124 Index: 1, 125 }, 126 }, 127 }) 128 if err != nil { 129 return nil, err 130 } 131 if err := mgwOp.Config(ctx, testZone, ctx.ID); err != nil { 132 return nil, err 133 } 134 return mgs, nil 135 }, 136 CheckFunc: func(t testutil.TestT, ctx *testutil.CRUDTestContext, i interface{}) error { 137 mgw := i.(*iaas.MobileGateway) 138 return testutil.DoAsserts( 139 testutil.AssertNotNilFunc(t, mgw.InterfaceSettings, "MobileGateway.Settings.Interfaces"), 140 testutil.AssertEqualFunc(t, 1, mgw.InterfaceSettings[0].Index, "MobileGateway.Settings.Interfaces.Index"), 141 testutil.AssertEqualFunc(t, "192.168.2.11", mgw.InterfaceSettings[0].IPAddress[0], "MobileGateway.Settings.Interfaces.IPAddress"), 142 testutil.AssertEqualFunc(t, 16, mgw.InterfaceSettings[0].NetworkMaskLen, "MobileGateway.Settings.Interfaces.NetworkMaskLen"), 143 ) 144 }, 145 SkipExtractID: true, 146 }, 147 148 // Get/Set DNS 149 { 150 Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 151 mgwOp := iaas.NewMobileGatewayOp(caller) 152 if err := mgwOp.SetDNS(ctx, testZone, ctx.ID, &iaas.MobileGatewayDNSSetting{ 153 DNS1: "8.8.8.8", 154 DNS2: "8.8.4.4", 155 }); err != nil { 156 return nil, err 157 } 158 return mgwOp.GetDNS(ctx, testZone, ctx.ID) 159 }, 160 CheckFunc: func(t testutil.TestT, ctx *testutil.CRUDTestContext, i interface{}) error { 161 dns := i.(*iaas.MobileGatewayDNSSetting) 162 return testutil.DoAsserts( 163 testutil.AssertEqualFunc(t, "8.8.8.8", dns.DNS1, "DNS1"), 164 testutil.AssertEqualFunc(t, "8.8.4.4", dns.DNS2, "DNS2"), 165 ) 166 }, 167 SkipExtractID: true, 168 }, 169 // Add/List SIM 170 { 171 Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 172 simOp := iaas.NewSIMOp(caller) 173 sim, err := simOp.Create(ctx, &iaas.SIMCreateRequest{ 174 Name: testutil.ResourceName("switch-for-mobile-gateway"), 175 ICCID: iccid, 176 PassCode: passcode, 177 }) 178 if err != nil { 179 return nil, err 180 } 181 182 mgwOp := iaas.NewMobileGatewayOp(caller) 183 if err := mgwOp.AddSIM(ctx, testZone, ctx.ID, &iaas.MobileGatewayAddSIMRequest{ 184 SIMID: sim.ID.String(), 185 }); err != nil { 186 return nil, err 187 } 188 189 ctx.Values["mobile-gateway/sim"] = sim.ID 190 return mgwOp.ListSIM(ctx, testZone, ctx.ID) 191 }, 192 CheckFunc: func(t testutil.TestT, ctx *testutil.CRUDTestContext, i interface{}) error { 193 sims := i.(iaas.MobileGatewaySIMs) 194 return testutil.DoAsserts( 195 testutil.AssertLenFunc(t, sims, 1, "len(SIM)"), 196 ) 197 }, 198 SkipExtractID: true, 199 }, 200 // SIMOp: Assign IP 201 { 202 Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 203 client := iaas.NewSIMOp(caller) 204 simID := ctx.Values["mobile-gateway/sim"].(types.ID) 205 if err := client.AssignIP(ctx, simID, &iaas.SIMAssignIPRequest{ 206 IP: "192.168.2.1", 207 }); err != nil { 208 return nil, err 209 } 210 return client.Status(ctx, simID) 211 }, 212 CheckFunc: func(t testutil.TestT, ctx *testutil.CRUDTestContext, v interface{}) error { 213 simInfo := v.(*iaas.SIMInfo) 214 return testutil.DoAsserts( 215 testutil.AssertEqualFunc(t, "192.168.2.1", simInfo.IP, "SIMInfo.IP"), 216 ) 217 }, 218 SkipExtractID: true, 219 }, 220 // SIMOp: clear IP 221 { 222 Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 223 client := iaas.NewSIMOp(caller) 224 simID := ctx.Values["mobile-gateway/sim"].(types.ID) 225 if err := client.ClearIP(ctx, simID); err != nil { 226 return nil, err 227 } 228 return client.Status(ctx, simID) 229 }, 230 CheckFunc: func(t testutil.TestT, ctx *testutil.CRUDTestContext, v interface{}) error { 231 simInfo := v.(*iaas.SIMInfo) 232 return testutil.DoAsserts( 233 testutil.AssertEmptyFunc(t, simInfo.IP, "SIMInfo.IP"), 234 ) 235 }, 236 SkipExtractID: true, 237 }, 238 239 // Get/Set SIMRoutes 240 { 241 Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 242 mgwOp := iaas.NewMobileGatewayOp(caller) 243 if err := mgwOp.SetSIMRoutes(ctx, testZone, ctx.ID, []*iaas.MobileGatewaySIMRouteParam{ 244 { 245 ResourceID: ctx.Values["mobile-gateway/sim"].(types.ID).String(), 246 Prefix: "192.168.3.0/24", 247 }, 248 }); err != nil { 249 return nil, err 250 } 251 return mgwOp.GetSIMRoutes(ctx, testZone, ctx.ID) 252 }, 253 CheckFunc: func(t testutil.TestT, ctx *testutil.CRUDTestContext, i interface{}) error { 254 routes := i.(iaas.MobileGatewaySIMRoutes) 255 simID := ctx.Values["mobile-gateway/sim"].(types.ID) 256 return testutil.DoAsserts( 257 testutil.AssertLenFunc(t, routes, 1, "len(SIMRoutes)"), 258 testutil.AssertEqualFunc(t, "192.168.3.0/24", routes[0].Prefix, "SIMRoute.Prefix"), 259 testutil.AssertEqualFunc(t, simID.String(), routes[0].ResourceID, "SIMRoute.ResourceID"), 260 ) 261 }, 262 SkipExtractID: true, 263 }, 264 // Delete SIMRoutes 265 { 266 Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 267 mgwOp := iaas.NewMobileGatewayOp(caller) 268 if err := mgwOp.SetSIMRoutes(ctx, testZone, ctx.ID, []*iaas.MobileGatewaySIMRouteParam{}); err != nil { 269 return nil, err 270 } 271 return mgwOp.GetSIMRoutes(ctx, testZone, ctx.ID) 272 }, 273 CheckFunc: func(t testutil.TestT, ctx *testutil.CRUDTestContext, i interface{}) error { 274 routes := i.(iaas.MobileGatewaySIMRoutes) 275 return testutil.DoAsserts( 276 testutil.AssertLenFunc(t, routes, 0, "len(SIMRoutes)"), 277 ) 278 }, 279 SkipExtractID: true, 280 }, 281 282 // Get/Set TrafficConfig 283 { 284 Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 285 mgwOp := iaas.NewMobileGatewayOp(caller) 286 if err := mgwOp.SetTrafficConfig(ctx, testZone, ctx.ID, &iaas.MobileGatewayTrafficControl{ 287 TrafficQuotaInMB: 10, 288 BandWidthLimitInKbps: 20, 289 EmailNotifyEnabled: true, 290 SlackNotifyEnabled: true, 291 SlackNotifyWebhooksURL: "https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX", 292 AutoTrafficShaping: true, 293 }); err != nil { 294 return nil, err 295 } 296 return mgwOp.GetTrafficConfig(ctx, testZone, ctx.ID) 297 }, 298 CheckFunc: func(t testutil.TestT, ctx *testutil.CRUDTestContext, i interface{}) error { 299 slackURL := "https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX" 300 config := i.(*iaas.MobileGatewayTrafficControl) 301 return testutil.DoAsserts( 302 testutil.AssertEqualFunc(t, 10, config.TrafficQuotaInMB, "TrafficConfig.TrafficQuotaInMB"), 303 testutil.AssertEqualFunc(t, 20, config.BandWidthLimitInKbps, "TrafficConfig.BandWidthLimitInKbps"), 304 testutil.AssertEqualFunc(t, true, config.EmailNotifyEnabled, "TrafficConfig.EmailNotifyEnabled"), 305 testutil.AssertEqualFunc(t, true, config.SlackNotifyEnabled, "TrafficConfig.SlackNotifyEnabled"), 306 testutil.AssertEqualFunc(t, slackURL, config.SlackNotifyWebhooksURL, "TrafficConfig.SlackNotifyWebhooksURL"), 307 testutil.AssertEqualFunc(t, true, config.AutoTrafficShaping, "TrafficConfig.AutoTrafficShaping"), 308 ) 309 }, 310 SkipExtractID: true, 311 }, 312 // Delete TrafficConfig(no check) 313 { 314 Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 315 mgwOp := iaas.NewMobileGatewayOp(caller) 316 return nil, mgwOp.DeleteTrafficConfig(ctx, testZone, ctx.ID) 317 }, 318 SkipExtractID: true, 319 }, 320 321 // Get TrafficStatus 322 { 323 Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 324 mgwOp := iaas.NewMobileGatewayOp(caller) 325 return mgwOp.TrafficStatus(ctx, testZone, ctx.ID) 326 }, 327 CheckFunc: func(t testutil.TestT, ctx *testutil.CRUDTestContext, i interface{}) error { 328 status := i.(*iaas.MobileGatewayTrafficStatus) 329 return testutil.DoAsserts( 330 testutil.AssertNotNilFunc(t, status, "TrafficStatus"), 331 testutil.AssertEqualFunc(t, types.StringNumber(0), status.UplinkBytes, "TrafficStatus.UplinkBytes"), 332 testutil.AssertEqualFunc(t, types.StringNumber(0), status.DownlinkBytes, "TrafficStatus.DownlinkBytes"), 333 ) 334 }, 335 SkipExtractID: true, 336 }, 337 338 // Delete SIM 339 { 340 Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 341 simID := ctx.Values["mobile-gateway/sim"].(types.ID) 342 mgwOp := iaas.NewMobileGatewayOp(caller) 343 if err := mgwOp.DeleteSIM(ctx, testZone, ctx.ID, simID); err != nil { 344 return nil, err 345 } 346 347 simOp := iaas.NewSIMOp(caller) 348 if err := simOp.Delete(ctx, simID); err != nil { 349 return nil, err 350 } 351 352 return mgwOp.ListSIM(ctx, testZone, ctx.ID) 353 }, 354 CheckFunc: func(t testutil.TestT, ctx *testutil.CRUDTestContext, i interface{}) error { 355 sims := i.(iaas.MobileGatewaySIMs) 356 return testutil.DoAsserts( 357 testutil.AssertLenFunc(t, sims, 0, "len(SIM)"), 358 ) 359 }, 360 SkipExtractID: true, 361 }, 362 // disconnect from switch 363 { 364 Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 365 mgwOp := iaas.NewMobileGatewayOp(caller) 366 if err := mgwOp.DisconnectFromSwitch(ctx, testZone, ctx.ID); err != nil { 367 return nil, err 368 } 369 370 swID := ctx.Values["mobile-gateway/switch"].(types.ID) 371 swOp := iaas.NewSwitchOp(caller) 372 if err := swOp.Delete(ctx, testZone, swID); err != nil { 373 return nil, err 374 } 375 376 return mgwOp.Read(ctx, testZone, ctx.ID) 377 }, 378 CheckFunc: func(t testutil.TestT, ctx *testutil.CRUDTestContext, i interface{}) error { 379 mgw := i.(*iaas.MobileGateway) 380 return testutil.DoAsserts( 381 testutil.AssertLenFunc(t, mgw.Interfaces, 1, "len(MobileGateway.Interfaces)"), 382 ) 383 }, 384 SkipExtractID: true, 385 }, 386 }, 387 Shutdown: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) error { 388 client := iaas.NewMobileGatewayOp(caller) 389 return power.ShutdownMobileGateway(ctx, client, testZone, ctx.ID, true) 390 }, 391 Delete: &testutil.CRUDTestDeleteFunc{ 392 Func: testMobileGatewayDelete, 393 }, 394 }) 395 } 396 397 func initMobileGatewayVariables() { 398 iccid = os.Getenv("SAKURACLOUD_SIM_ICCID") 399 passcode = os.Getenv("SAKURACLOUD_SIM_PASSCODE") 400 401 createMobileGatewayParam = &iaas.MobileGatewayCreateRequest{ 402 Name: testutil.ResourceName("mobile-gateway"), 403 Description: "desc", 404 Tags: []string{"tag1", "tag2"}, 405 InternetConnectionEnabled: true, 406 InterDeviceCommunicationEnabled: true, 407 } 408 createMobileGatewayExpected = &iaas.MobileGateway{ 409 Name: createMobileGatewayParam.Name, 410 Description: createMobileGatewayParam.Description, 411 Tags: createMobileGatewayParam.Tags, 412 Availability: types.Availabilities.Available, 413 InternetConnectionEnabled: true, 414 InterDeviceCommunicationEnabled: true, 415 } 416 updateMobileGatewayParam = &iaas.MobileGatewayUpdateRequest{ 417 Name: testutil.ResourceName("mobile-gateway-upd"), 418 Description: "desc-upd", 419 Tags: []string{"tag1-upd", "tag2-upd"}, 420 InternetConnectionEnabled: false, 421 InterDeviceCommunicationEnabled: false, 422 } 423 updateMobileGatewayExpected = &iaas.MobileGateway{ 424 Name: updateMobileGatewayParam.Name, 425 Description: updateMobileGatewayParam.Description, 426 Tags: updateMobileGatewayParam.Tags, 427 Availability: types.Availabilities.Available, 428 InternetConnectionEnabled: false, 429 InterDeviceCommunicationEnabled: false, 430 } 431 updateMobileGatewaySettingsParam = &iaas.MobileGatewayUpdateSettingsRequest{ 432 InternetConnectionEnabled: true, 433 InterDeviceCommunicationEnabled: true, 434 } 435 updateMobileGatewaySettingsExpected = &iaas.MobileGateway{ 436 Name: updateMobileGatewayParam.Name, 437 Description: updateMobileGatewayParam.Description, 438 Tags: updateMobileGatewayParam.Tags, 439 Availability: types.Availabilities.Available, 440 InternetConnectionEnabled: true, 441 InterDeviceCommunicationEnabled: true, 442 } 443 } 444 445 var ( 446 ignoreMobileGatewayFields = []string{ 447 "ID", 448 "Class", 449 "IconID", 450 "CreatedAt", 451 "Availability", 452 "InstanceHostName", 453 "InstanceHostInfoURL", 454 "InstanceStatus", 455 "InstanceStatusChangedAt", 456 "Interfaces", 457 "ZoneID", 458 "SettingsHash", 459 } 460 iccid string 461 passcode string 462 createMobileGatewayParam *iaas.MobileGatewayCreateRequest 463 createMobileGatewayExpected *iaas.MobileGateway 464 updateMobileGatewayParam *iaas.MobileGatewayUpdateRequest 465 updateMobileGatewayExpected *iaas.MobileGateway 466 updateMobileGatewaySettingsParam *iaas.MobileGatewayUpdateSettingsRequest 467 updateMobileGatewaySettingsExpected *iaas.MobileGateway 468 ) 469 470 func testMobileGatewayCreate(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 471 client := iaas.NewMobileGatewayOp(caller) 472 v, err := client.Create(ctx, testZone, createMobileGatewayParam) 473 if err != nil { 474 return nil, err 475 } 476 value, err := iaas.WaiterForReady(func() (interface{}, error) { 477 return client.Read(ctx, testZone, v.ID) 478 }).WaitForState(ctx) 479 if err != nil { 480 return nil, err 481 } 482 if err := client.Boot(ctx, testZone, v.ID); err != nil { 483 return nil, err 484 } 485 return value, nil 486 } 487 488 func testMobileGatewayRead(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 489 client := iaas.NewMobileGatewayOp(caller) 490 return client.Read(ctx, testZone, ctx.ID) 491 } 492 493 func testMobileGatewayUpdate(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 494 client := iaas.NewMobileGatewayOp(caller) 495 return client.Update(ctx, testZone, ctx.ID, updateMobileGatewayParam) 496 } 497 498 func testMobileGatewayUpdateSettings(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 499 client := iaas.NewMobileGatewayOp(caller) 500 return client.UpdateSettings(ctx, testZone, ctx.ID, updateMobileGatewaySettingsParam) 501 } 502 503 func testMobileGatewayDelete(ctx *testutil.CRUDTestContext, caller iaas.APICaller) error { 504 client := iaas.NewMobileGatewayOp(caller) 505 return client.Delete(ctx, testZone, ctx.ID) 506 }