github.com/sacloud/iaas-api-go@v1.12.0/test/simple_monitor_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 "errors" 19 "testing" 20 "time" 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/stretchr/testify/assert" 26 ) 27 28 func TestSimpleMonitorOp_CRUD(t *testing.T) { 29 testutil.RunCRUD(t, &testutil.CRUDTestCase{ 30 Parallel: true, 31 32 SetupAPICallerFunc: singletonAPICaller, 33 34 Create: &testutil.CRUDTestFunc{ 35 Func: testSimpleMonitorCreate, 36 CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{ 37 ExpectValue: createSimpleMonitorExpected, 38 IgnoreFields: ignoreSimpleMonitorFields, 39 }), 40 }, 41 42 Read: &testutil.CRUDTestFunc{ 43 Func: testSimpleMonitorRead, 44 CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{ 45 ExpectValue: createSimpleMonitorExpected, 46 IgnoreFields: ignoreSimpleMonitorFields, 47 }), 48 }, 49 50 Updates: []*testutil.CRUDTestFunc{ 51 { 52 Func: testSimpleMonitorUpdate, 53 CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{ 54 ExpectValue: updateSimpleMonitorExpected, 55 IgnoreFields: ignoreSimpleMonitorFields, 56 }), 57 }, 58 { 59 Func: testSimpleMonitorUpdateSettings, 60 CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{ 61 ExpectValue: updateSimpleMonitorSettingsExpected, 62 IgnoreFields: ignoreSimpleMonitorFields, 63 }), 64 }, 65 { 66 Func: testSimpleMonitorUpdateToMin, 67 CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{ 68 ExpectValue: updateSimpleMonitorToMinExpected, 69 IgnoreFields: ignoreSimpleMonitorFields, 70 }), 71 }, 72 }, 73 74 Delete: &testutil.CRUDTestDeleteFunc{ 75 Func: testSimpleMonitorDelete, 76 }, 77 }) 78 } 79 80 var ( 81 ignoreSimpleMonitorFields = []string{ 82 "ID", 83 "CreatedAt", 84 "ModifiedAt", 85 "Class", 86 "SettingsHash", 87 } 88 createSimpleMonitorParam = &iaas.SimpleMonitorCreateRequest{ 89 Target: testutil.ResourceName("simple-monitor.usacloud.jp"), 90 Description: "desc", 91 Tags: []string{"tag1", "tag2"}, 92 MaxCheckAttempts: 5, 93 RetryInterval: 20, 94 DelayLoop: 60, 95 Enabled: types.StringTrue, 96 HealthCheck: &iaas.SimpleMonitorHealthCheck{ 97 Protocol: types.SimpleMonitorProtocols.HTTP, 98 Port: types.StringNumber(80), 99 Path: "/index.html", 100 Status: types.StringNumber(200), 101 SNI: types.StringTrue, 102 VerifySNI: types.StringTrue, 103 Host: "libsacloud-test.usacloud.jp", 104 BasicAuthUsername: "username", 105 BasicAuthPassword: "password", 106 ContainsString: "foobar", 107 }, 108 NotifyEmailEnabled: types.StringTrue, 109 NotifyEmailHTML: types.StringTrue, 110 NotifySlackEnabled: types.StringFalse, 111 NotifyInterval: 2 * 60 * 60, 112 Timeout: 10, 113 SlackWebhooksURL: "", 114 } 115 createSimpleMonitorExpected = &iaas.SimpleMonitor{ 116 Name: createSimpleMonitorParam.Target, 117 Description: createSimpleMonitorParam.Description, 118 Tags: createSimpleMonitorParam.Tags, 119 Target: createSimpleMonitorParam.Target, 120 MaxCheckAttempts: createSimpleMonitorParam.MaxCheckAttempts, 121 RetryInterval: createSimpleMonitorParam.RetryInterval, 122 DelayLoop: createSimpleMonitorParam.DelayLoop, 123 Enabled: createSimpleMonitorParam.Enabled, 124 HealthCheck: createSimpleMonitorParam.HealthCheck, 125 NotifyEmailEnabled: createSimpleMonitorParam.NotifyEmailEnabled, 126 NotifyEmailHTML: createSimpleMonitorParam.NotifyEmailHTML, 127 NotifySlackEnabled: createSimpleMonitorParam.NotifySlackEnabled, 128 NotifyInterval: createSimpleMonitorParam.NotifyInterval, 129 SlackWebhooksURL: createSimpleMonitorParam.SlackWebhooksURL, 130 Timeout: createSimpleMonitorParam.Timeout, 131 Availability: types.Availabilities.Available, 132 } 133 updateSimpleMonitorParam = &iaas.SimpleMonitorUpdateRequest{ 134 Description: "desc-upd", 135 Tags: []string{"tag1-upd", "tag2-upd"}, 136 MaxCheckAttempts: 10, 137 RetryInterval: 100, 138 DelayLoop: 120, 139 HealthCheck: &iaas.SimpleMonitorHealthCheck{ 140 Protocol: types.SimpleMonitorProtocols.HTTPS, 141 Port: types.StringNumber(443), 142 Path: "/index2.html", 143 Status: types.StringNumber(201), 144 SNI: types.StringFalse, 145 VerifySNI: types.StringFalse, 146 Host: "libsacloud-test-upd.usacloud.jp", 147 BasicAuthUsername: "username-upd", 148 BasicAuthPassword: "password-upd", 149 HTTP2: true, 150 }, 151 NotifyEmailEnabled: types.StringFalse, 152 NotifyEmailHTML: types.StringFalse, 153 NotifySlackEnabled: types.StringTrue, 154 SlackWebhooksURL: "https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX", 155 NotifyInterval: 3 * 60 * 60, 156 Timeout: 20, 157 IconID: testIconID, 158 } 159 updateSimpleMonitorExpected = &iaas.SimpleMonitor{ 160 Name: createSimpleMonitorParam.Target, 161 Description: updateSimpleMonitorParam.Description, 162 Tags: updateSimpleMonitorParam.Tags, 163 Target: createSimpleMonitorParam.Target, 164 MaxCheckAttempts: updateSimpleMonitorParam.MaxCheckAttempts, 165 RetryInterval: updateSimpleMonitorParam.RetryInterval, 166 DelayLoop: updateSimpleMonitorParam.DelayLoop, 167 Enabled: updateSimpleMonitorParam.Enabled, 168 HealthCheck: updateSimpleMonitorParam.HealthCheck, 169 NotifyEmailEnabled: updateSimpleMonitorParam.NotifyEmailEnabled, 170 NotifyEmailHTML: updateSimpleMonitorParam.NotifyEmailHTML, 171 NotifySlackEnabled: updateSimpleMonitorParam.NotifySlackEnabled, 172 NotifyInterval: updateSimpleMonitorParam.NotifyInterval, 173 Timeout: updateSimpleMonitorParam.Timeout, 174 SlackWebhooksURL: updateSimpleMonitorParam.SlackWebhooksURL, 175 Availability: types.Availabilities.Available, 176 IconID: testIconID, 177 } 178 updateSimpleMonitorSettingsParam = &iaas.SimpleMonitorUpdateSettingsRequest{ 179 MaxCheckAttempts: 10, 180 RetryInterval: 100, 181 DelayLoop: 120, 182 HealthCheck: &iaas.SimpleMonitorHealthCheck{ 183 Protocol: types.SimpleMonitorProtocols.HTTP, 184 Port: types.StringNumber(80), 185 Path: "/index3.html", 186 Status: types.StringNumber(202), 187 SNI: types.StringFalse, 188 VerifySNI: types.StringFalse, 189 Host: "libsacloud-test.usacloud.jp", 190 BasicAuthUsername: "username-upd2", 191 BasicAuthPassword: "password-upd2", 192 }, 193 NotifyEmailEnabled: types.StringFalse, 194 NotifyEmailHTML: types.StringFalse, 195 NotifySlackEnabled: types.StringTrue, 196 SlackWebhooksURL: "https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX", 197 NotifyInterval: 3 * 60 * 60, 198 Timeout: 30, 199 } 200 updateSimpleMonitorSettingsExpected = &iaas.SimpleMonitor{ 201 Name: createSimpleMonitorParam.Target, 202 Description: updateSimpleMonitorParam.Description, 203 Tags: updateSimpleMonitorParam.Tags, 204 Target: createSimpleMonitorParam.Target, 205 MaxCheckAttempts: updateSimpleMonitorSettingsParam.MaxCheckAttempts, 206 RetryInterval: updateSimpleMonitorSettingsParam.RetryInterval, 207 DelayLoop: updateSimpleMonitorSettingsParam.DelayLoop, 208 Enabled: updateSimpleMonitorSettingsParam.Enabled, 209 HealthCheck: updateSimpleMonitorSettingsParam.HealthCheck, 210 NotifyEmailEnabled: updateSimpleMonitorSettingsParam.NotifyEmailEnabled, 211 NotifyEmailHTML: updateSimpleMonitorSettingsParam.NotifyEmailHTML, 212 NotifySlackEnabled: updateSimpleMonitorSettingsParam.NotifySlackEnabled, 213 NotifyInterval: updateSimpleMonitorSettingsParam.NotifyInterval, 214 Timeout: updateSimpleMonitorSettingsParam.Timeout, 215 SlackWebhooksURL: updateSimpleMonitorSettingsParam.SlackWebhooksURL, 216 Availability: types.Availabilities.Available, 217 IconID: testIconID, 218 } 219 220 updateSimpleMonitorToMinParam = &iaas.SimpleMonitorUpdateRequest{ 221 HealthCheck: &iaas.SimpleMonitorHealthCheck{ 222 Protocol: types.SimpleMonitorProtocols.Ping, 223 Host: "libsacloud-test-upd.usacloud.jp", 224 }, 225 NotifyEmailEnabled: types.StringTrue, 226 Enabled: true, 227 } 228 updateSimpleMonitorToMinExpected = &iaas.SimpleMonitor{ 229 Name: createSimpleMonitorParam.Target, 230 Target: createSimpleMonitorParam.Target, 231 MaxCheckAttempts: 3, // default value 232 RetryInterval: 10, // default value 233 DelayLoop: 60, // default value 234 Enabled: updateSimpleMonitorToMinParam.Enabled, 235 HealthCheck: updateSimpleMonitorToMinParam.HealthCheck, 236 NotifyEmailEnabled: updateSimpleMonitorToMinParam.NotifyEmailEnabled, 237 Availability: types.Availabilities.Available, 238 NotifyInterval: 2 * 60 * 60, // default value 239 } 240 ) 241 242 func testSimpleMonitorCreate(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 243 client := iaas.NewSimpleMonitorOp(caller) 244 return client.Create(ctx, createSimpleMonitorParam) 245 } 246 247 func testSimpleMonitorRead(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 248 client := iaas.NewSimpleMonitorOp(caller) 249 return client.Read(ctx, ctx.ID) 250 } 251 252 func testSimpleMonitorUpdate(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 253 client := iaas.NewSimpleMonitorOp(caller) 254 return client.Update(ctx, ctx.ID, updateSimpleMonitorParam) 255 } 256 257 func testSimpleMonitorUpdateSettings(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 258 client := iaas.NewSimpleMonitorOp(caller) 259 return client.UpdateSettings(ctx, ctx.ID, updateSimpleMonitorSettingsParam) 260 } 261 262 func testSimpleMonitorUpdateToMin(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 263 client := iaas.NewSimpleMonitorOp(caller) 264 return client.Update(ctx, ctx.ID, updateSimpleMonitorToMinParam) 265 } 266 267 func testSimpleMonitorDelete(ctx *testutil.CRUDTestContext, caller iaas.APICaller) error { 268 client := iaas.NewSimpleMonitorOp(caller) 269 return client.Delete(ctx, ctx.ID) 270 } 271 272 func TestSimpleMonitorOp_StatusAndHealth(t *testing.T) { 273 client := iaas.NewSimpleMonitorOp(singletonAPICaller()) 274 testutil.RunCRUD(t, &testutil.CRUDTestCase{ 275 Parallel: true, 276 277 SetupAPICallerFunc: singletonAPICaller, 278 279 Create: &testutil.CRUDTestFunc{ 280 Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 281 sm, err := client.Create(ctx, simpleMonitorStatusAndHealthTargetParam) 282 if err != nil { 283 return nil, err 284 } 285 if isAccTest() { 286 time.Sleep(2 * time.Minute) // Statusの戻り値を確認するために数分待つ 287 } 288 return sm, nil 289 }, 290 }, 291 292 Read: &testutil.CRUDTestFunc{ 293 Func: testSimpleMonitorRead, 294 }, 295 296 Updates: []*testutil.CRUDTestFunc{ 297 { 298 Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 299 return client.HealthStatus(ctx, ctx.ID) 300 }, 301 CheckFunc: func(t testutil.TestT, ctx *testutil.CRUDTestContext, v interface{}) error { 302 healthStatus := v.(*iaas.SimpleMonitorHealthStatus) 303 if !assert.NotNil(t, healthStatus) { 304 return errors.New("unexpected state: SimpleMonitorHealthStatus") 305 } 306 if isAccTest() { 307 if !assert.NotEmpty(t, healthStatus.LatestLogs) { 308 return errors.New("unexpected state: SimpleMonitorHealthStatus.LatestLogs") 309 } 310 } 311 return nil 312 }, 313 }, 314 { 315 Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) { 316 return client.MonitorResponseTime(ctx, ctx.ID, &iaas.MonitorCondition{}) 317 }, 318 CheckFunc: func(t testutil.TestT, ctx *testutil.CRUDTestContext, v interface{}) error { 319 monitor := v.(*iaas.ResponseTimeSecActivity) 320 if !assert.NotNil(t, monitor) { 321 return errors.New("unexpected state: ResponseTimeSecActivity") 322 } 323 return nil 324 }, 325 }, 326 }, 327 328 Delete: &testutil.CRUDTestDeleteFunc{ 329 Func: testSimpleMonitorDelete, 330 }, 331 }) 332 } 333 334 var simpleMonitorStatusAndHealthTargetParam = &iaas.SimpleMonitorCreateRequest{ 335 Target: testutil.ResourceName("simple-monitor.usacloud.jp"), 336 DelayLoop: 60, 337 Enabled: true, 338 HealthCheck: &iaas.SimpleMonitorHealthCheck{ 339 Protocol: types.SimpleMonitorProtocols.HTTPS, 340 Port: 443, 341 Path: "/", 342 Status: 200, 343 }, 344 NotifySlackEnabled: true, 345 SlackWebhooksURL: "https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX", 346 }