github.com/sacloud/iaas-api-go@v1.12.0/test/package_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 "context" 19 "os" 20 "testing" 21 22 "github.com/sacloud/iaas-api-go" 23 "github.com/sacloud/iaas-api-go/accessor" 24 "github.com/sacloud/iaas-api-go/testutil" 25 "github.com/sacloud/iaas-api-go/types" 26 ) 27 28 func TestMain(m *testing.M) { 29 testZone = testutil.TestZone() 30 31 m.Run() 32 33 skipCleanup := os.Getenv("SKIP_CLEANUP") 34 if skipCleanup == "" { 35 if err := testutil.CleanupTestResources(context.Background(), singletonAPICaller()); err != nil { 36 panic(err) 37 } 38 } 39 } 40 41 var testZone string 42 var testIconID = types.ID(112901627749) // テスト用のアイコンID(shared icon) 43 44 func singletonAPICaller() iaas.APICaller { 45 return testutil.SingletonAPICaller() 46 } 47 48 func isAccTest() bool { 49 return testutil.IsAccTest() 50 } 51 52 func setupSwitchFunc(targetResource string, dests ...accessor.SwitchID) func(*testutil.CRUDTestContext, iaas.APICaller) error { 53 return func(testContext *testutil.CRUDTestContext, caller iaas.APICaller) error { 54 swClient := iaas.NewSwitchOp(caller) 55 sw, err := swClient.Create(context.Background(), testZone, &iaas.SwitchCreateRequest{ 56 Name: testutil.ResourceName("switch-for-" + targetResource), 57 }) 58 if err != nil { 59 return err 60 } 61 62 testContext.Values[targetResource+"/switch"] = sw.ID 63 for _, dest := range dests { 64 dest.SetSwitchID(sw.ID) 65 } 66 return nil 67 } 68 } 69 70 func cleanupSwitchFunc(targetResource string) func(*testutil.CRUDTestContext, iaas.APICaller) error { 71 return func(testContext *testutil.CRUDTestContext, caller iaas.APICaller) error { 72 switchID, ok := testContext.Values[targetResource+"/switch"] 73 if !ok { 74 return nil 75 } 76 77 swClient := iaas.NewSwitchOp(caller) 78 return swClient.Delete(context.Background(), testZone, switchID.(types.ID)) 79 } 80 }