github.com/sacloud/iaas-api-go@v1.12.0/test/auto_scale_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 "fmt" 19 "os" 20 "testing" 21 22 "github.com/sacloud/iaas-api-go" 23 "github.com/sacloud/iaas-api-go/testutil" 24 "github.com/sacloud/iaas-api-go/types" 25 "github.com/sacloud/packages-go/size" 26 ) 27 28 var autoScaleTestServerName = testutil.ResourceName("auto-scale") 29 30 func TestAutoScaleOp_CRUD(t *testing.T) { 31 if testutil.IsAccTest() && os.Getenv("SAKURACLOUD_API_KEY_ID") == "" { 32 t.Skip("SAKURACLOUD_API_KEY_ID is required when running the acceptance test") 33 } 34 35 testutil.RunCRUD(t, &testutil.CRUDTestCase{ 36 Parallel: true, 37 SetupAPICallerFunc: singletonAPICaller, 38 Setup: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) error { 39 serverOp := iaas.NewServerOp(caller) 40 // ディスクレスサーバを作成 41 _, err := serverOp.Create(ctx, testutil.TestZone(), &iaas.ServerCreateRequest{ 42 CPU: 1, 43 MemoryMB: 2 * size.GiB, 44 Name: autoScaleTestServerName, 45 }) 46 return err 47 }, 48 Create: &testutil.CRUDTestFunc{ 49 Func: testAutoScaleCreate, 50 CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{ 51 ExpectValue: createAutoScaleExpected, 52 IgnoreFields: ignoreAutoScaleFields, 53 }), 54 }, 55 56 Read: &testutil.CRUDTestFunc{ 57 Func: testAutoScaleRead, 58 CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{ 59 ExpectValue: createAutoScaleExpected, 60 IgnoreFields: ignoreAutoScaleFields, 61 }), 62 }, 63 64 Updates: []*testutil.CRUDTestFunc{ 65 { 66 Func: testAutoScaleUpdate, 67 CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{ 68 ExpectValue: updateAutoScaleExpected, 69 IgnoreFields: ignoreAutoScaleFields, 70 }), 71 }, 72 { 73 Func: testAutoScaleUpdateTriggerType, 74 CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{ 75 ExpectValue: updateAutoScaleTriggerTypeExpected, 76 IgnoreFields: ignoreAutoScaleFields, 77 }), 78 }, 79 }, 80 Delete: &testutil.CRUDTestDeleteFunc{ 81 Func: testAutoScaleDelete, 82 }, 83 }) 84 } 85 86 var ( 87 ignoreAutoScaleFields = []string{ 88 "ID", 89 "Class", 90 "SettingsHash", 91 "CreatedAt", 92 "ModifiedAt", 93 "ScheduleScaling", 94 } 95 createAutoScaleParam = &iaas.AutoScaleCreateRequest{ 96 Name: testutil.ResourceName("auto-scale"), 97 Description: "desc", 98 Tags: []string{"tag1", "tag2"}, 99 100 Config: fmt.Sprintf(autoScaleConfigTemplate, autoScaleTestServerName, testutil.TestZone()), 101 Zones: []string{testutil.TestZone()}, 102 TriggerType: types.AutoScaleTriggerTypes.CPU, 103 Disabled: true, 104 CPUThresholdScaling: &iaas.AutoScaleCPUThresholdScaling{ 105 ServerPrefix: autoScaleTestServerName, 106 Up: 80, 107 Down: 50, 108 }, 109 APIKeyID: os.Getenv("SAKURACLOUD_API_KEY_ID"), 110 } 111 createAutoScaleExpected = &iaas.AutoScale{ 112 Name: createAutoScaleParam.Name, 113 Description: createAutoScaleParam.Description, 114 Tags: createAutoScaleParam.Tags, 115 Availability: types.Availabilities.Available, 116 117 TriggerType: types.AutoScaleTriggerTypes.CPU, 118 Disabled: true, 119 Config: fmt.Sprintf(autoScaleConfigTemplate, autoScaleTestServerName, testutil.TestZone()), 120 Zones: []string{testutil.TestZone()}, 121 CPUThresholdScaling: &iaas.AutoScaleCPUThresholdScaling{ 122 ServerPrefix: autoScaleTestServerName, 123 Up: 80, 124 Down: 50, 125 }, 126 APIKeyID: os.Getenv("SAKURACLOUD_API_KEY_ID"), 127 } 128 updateAutoScaleParam = &iaas.AutoScaleUpdateRequest{ 129 Name: testutil.ResourceName("auto-scale-upd"), 130 Description: "desc-upd", 131 Tags: []string{"tag1-upd", "tag2-upd"}, 132 IconID: testIconID, 133 134 Config: fmt.Sprintf(autoScaleConfigTemplateUpd, autoScaleTestServerName, testutil.TestZone()), 135 Zones: []string{testutil.TestZone()}, 136 TriggerType: types.AutoScaleTriggerTypes.CPU, 137 Disabled: false, 138 CPUThresholdScaling: &iaas.AutoScaleCPUThresholdScaling{ 139 ServerPrefix: autoScaleTestServerName, 140 Up: 81, 141 Down: 51, 142 }, 143 } 144 updateAutoScaleExpected = &iaas.AutoScale{ 145 Name: updateAutoScaleParam.Name, 146 Description: updateAutoScaleParam.Description, 147 Tags: updateAutoScaleParam.Tags, 148 Availability: types.Availabilities.Available, 149 IconID: testIconID, 150 151 Config: fmt.Sprintf(autoScaleConfigTemplateUpd, autoScaleTestServerName, testutil.TestZone()), 152 Zones: []string{testutil.TestZone()}, 153 TriggerType: types.AutoScaleTriggerTypes.CPU, 154 Disabled: false, 155 CPUThresholdScaling: &iaas.AutoScaleCPUThresholdScaling{ 156 ServerPrefix: autoScaleTestServerName, 157 Up: 81, 158 Down: 51, 159 }, 160 APIKeyID: os.Getenv("SAKURACLOUD_API_KEY_ID"), 161 } 162 updateAutoScaleTriggerTypeParam = &iaas.AutoScaleUpdateRequest{ 163 Name: testutil.ResourceName("auto-scale-upd"), 164 Description: "desc-upd", 165 Tags: []string{"tag1-upd", "tag2-upd"}, 166 IconID: testIconID, 167 168 Config: fmt.Sprintf(autoScaleConfigTemplateUpd, autoScaleTestServerName, testutil.TestZone()), 169 Zones: []string{testutil.TestZone()}, 170 TriggerType: types.AutoScaleTriggerTypes.Schedule, 171 Disabled: false, 172 ScheduleScaling: []*iaas.AutoScaleScheduleScaling{ 173 { 174 Action: types.AutoScaleActions.Up, 175 Hour: 10, 176 Minute: 15, 177 DayOfWeek: []types.EDayOfTheWeek{types.DaysOfTheWeek.Monday}, 178 }, 179 { 180 Action: types.AutoScaleActions.Down, 181 Hour: 18, 182 Minute: 15, 183 DayOfWeek: []types.EDayOfTheWeek{types.DaysOfTheWeek.Monday}, 184 }, 185 }, 186 } 187 updateAutoScaleTriggerTypeExpected = &iaas.AutoScale{ 188 Name: updateAutoScaleParam.Name, 189 Description: updateAutoScaleParam.Description, 190 Tags: updateAutoScaleParam.Tags, 191 Availability: types.Availabilities.Available, 192 IconID: testIconID, 193 194 Config: fmt.Sprintf(autoScaleConfigTemplateUpd, autoScaleTestServerName, testutil.TestZone()), 195 Zones: []string{testutil.TestZone()}, 196 TriggerType: types.AutoScaleTriggerTypes.Schedule, 197 Disabled: false, 198 ScheduleScaling: []*iaas.AutoScaleScheduleScaling{ 199 { 200 Action: types.AutoScaleActions.Up, 201 Hour: 10, 202 Minute: 15, 203 DayOfWeek: []types.EDayOfTheWeek{types.DaysOfTheWeek.Monday}, 204 }, 205 { 206 Action: types.AutoScaleActions.Down, 207 Hour: 18, 208 Minute: 15, 209 DayOfWeek: []types.EDayOfTheWeek{types.DaysOfTheWeek.Monday}, 210 }, 211 }, 212 APIKeyID: os.Getenv("SAKURACLOUD_API_KEY_ID"), 213 } 214 ) 215 216 func testAutoScaleCreate(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 217 client := iaas.NewAutoScaleOp(caller) 218 return client.Create(ctx, createAutoScaleParam) 219 } 220 221 func testAutoScaleRead(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 222 client := iaas.NewAutoScaleOp(caller) 223 return client.Read(ctx, ctx.ID) 224 } 225 226 func testAutoScaleUpdate(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 227 client := iaas.NewAutoScaleOp(caller) 228 return client.Update(ctx, ctx.ID, updateAutoScaleParam) 229 } 230 231 func testAutoScaleUpdateTriggerType(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 232 client := iaas.NewAutoScaleOp(caller) 233 return client.Update(ctx, ctx.ID, updateAutoScaleTriggerTypeParam) 234 } 235 236 func testAutoScaleDelete(ctx *testutil.CRUDTestContext, caller iaas.APICaller) error { 237 client := iaas.NewAutoScaleOp(caller) 238 return client.Delete(ctx, ctx.ID) 239 } 240 241 var autoScaleConfigTemplate = ` 242 resources: 243 - type: Server 244 selector: 245 names: ["%s"] 246 zones: ["%s"] 247 248 shutdown_force: true 249 ` 250 251 var autoScaleConfigTemplateUpd = ` 252 resources: 253 - type: Server 254 selector: 255 names: ["%s"] 256 zones: ["%s"] 257 258 shutdown_force: true 259 260 autoscaler: 261 cooldown: 300 262 `