1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
|
package testing
import (
"strings"
"testing"
"github.com/gophercloud/gophercloud/openstack/clustering/v1/clusters"
"github.com/gophercloud/gophercloud/pagination"
th "github.com/gophercloud/gophercloud/testhelper"
fake "github.com/gophercloud/gophercloud/testhelper/client"
)
func TestCreateCluster(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleCreateClusterSuccessfully(t)
minSize := 1
opts := clusters.CreateOpts{
Name: "cluster1",
DesiredCapacity: 3,
ProfileID: "d8a48377-f6a3-4af4-bbbb-6e8bcaa0cbc0",
MinSize: &minSize,
MaxSize: 20,
Timeout: 3600,
Metadata: map[string]interface{}{},
Config: map[string]interface{}{},
}
res := clusters.Create(fake.ServiceClient(), opts)
th.AssertNoErr(t, res.Err)
location := res.Header.Get("Location")
th.AssertEquals(t, "http://senlin.cloud.blizzard.net:8778/v1/actions/625628cd-f877-44be-bde0-fec79f84e13d", location)
locationFields := strings.Split(location, "actions/")
actionID := locationFields[1]
th.AssertEquals(t, "625628cd-f877-44be-bde0-fec79f84e13d", actionID)
actual, err := res.Extract()
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, ExpectedCluster, *actual)
}
func TestCreateClusterEmptyTime(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleCreateClusterEmptyTimeSuccessfully(t)
minSize := 1
opts := clusters.CreateOpts{
Name: "cluster1",
DesiredCapacity: 3,
ProfileID: "d8a48377-f6a3-4af4-bbbb-6e8bcaa0cbc0",
MinSize: &minSize,
MaxSize: 20,
Timeout: 3600,
Metadata: map[string]interface{}{},
Config: map[string]interface{}{},
}
actual, err := clusters.Create(fake.ServiceClient(), opts).Extract()
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, ExpectedCluster_EmptyTime, *actual)
}
func TestCreateClusterMetadata(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleCreateClusterMetadataSuccessfully(t)
minSize := 1
opts := clusters.CreateOpts{
Name: "cluster1",
DesiredCapacity: 3,
ProfileID: "d8a48377-f6a3-4af4-bbbb-6e8bcaa0cbc0",
MinSize: &minSize,
MaxSize: 20,
Timeout: 3600,
Metadata: map[string]interface{}{
"foo": "bar",
"test": map[string]interface{}{
"nil_interface": interface{}(nil),
"float_value": float64(123.3),
"string_value": "test_string",
"bool_value": false,
},
},
Config: map[string]interface{}{},
}
actual, err := clusters.Create(fake.ServiceClient(), opts).Extract()
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, ExpectedCluster_Metadata, *actual)
}
func TestGetCluster(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleGetClusterSuccessfully(t)
actual, err := clusters.Get(fake.ServiceClient(), "7d85f602-a948-4a30-afd4-e84f47471c15").Extract()
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, ExpectedCluster, *actual)
}
func TestGetClusterEmptyTime(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleGetClusterEmptyTimeSuccessfully(t)
actual, err := clusters.Get(fake.ServiceClient(), "7d85f602-a948-4a30-afd4-e84f47471c15").Extract()
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, ExpectedCluster_EmptyTime, *actual)
}
func TestListClusters(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleListClusterSuccessfully(t)
count := 0
clusters.List(fake.ServiceClient(), clusters.ListOpts{GlobalProject: new(bool)}).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := clusters.ExtractClusters(page)
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, ExpectedClusters, actual)
return true, nil
})
if count != 1 {
t.Errorf("Expected 1 page, got %d", count)
}
}
func TestUpdateCluster(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleUpdateClusterSuccessfully(t)
updateOpts := clusters.UpdateOpts{
Name: "cluster1",
ProfileID: "edc63d0a-2ca4-48fa-9854-27926da76a4a",
}
actual, err := clusters.Update(fake.ServiceClient(), ExpectedCluster.ID, updateOpts).Extract()
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, ExpectedCluster, *actual)
}
func TestUpdateClusterEmptyTime(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleUpdateClusterEmptyTimeSuccessfully(t)
updateOpts := clusters.UpdateOpts{
Name: "cluster1",
ProfileID: "edc63d0a-2ca4-48fa-9854-27926da76a4a",
}
actual, err := clusters.Update(fake.ServiceClient(), ExpectedCluster_EmptyTime.ID, updateOpts).Extract()
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, ExpectedCluster_EmptyTime, *actual)
}
func TestDeleteCluster(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleDeleteClusterSuccessfully(t)
err := clusters.Delete(fake.ServiceClient(), "6dc6d336e3fc4c0a951b5698cd1236ee").ExtractErr()
th.AssertNoErr(t, err)
}
func TestResizeCluster(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleResizeSuccessfully(t)
maxSize := 5
minSize := 1
number := -2
strict := true
opts := clusters.ResizeOpts{
AdjustmentType: "CHANGE_IN_CAPACITY",
MaxSize: &maxSize,
MinSize: &minSize,
Number: number,
Strict: &strict,
}
actionID, err := clusters.Resize(fake.ServiceClient(), "7d85f602-a948-4a30-afd4-e84f47471c15", opts).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, ExpectedActionID, actionID)
}
// Test case for Number field having a float value
func TestResizeClusterNumberFloat(t *testing.T) {
maxSize := 5
minSize := 1
number := 100.0
strict := true
opts := clusters.ResizeOpts{
AdjustmentType: "CHANGE_IN_PERCENTAGE",
MaxSize: &maxSize,
MinSize: &minSize,
Number: number,
Strict: &strict,
}
_, err := opts.ToClusterResizeMap()
th.AssertNoErr(t, err)
}
// Test case for missing Number field.
func TestResizeClusterMissingNumber(t *testing.T) {
maxSize := 5
minSize := 1
strict := true
opts := clusters.ResizeOpts{
MaxSize: &maxSize,
MinSize: &minSize,
Strict: &strict,
}
_, err := opts.ToClusterResizeMap()
th.AssertNoErr(t, err)
}
// Test case for missing Number field which is required when AdjustmentType is specified
func TestResizeClusterInvalidParamsMissingNumber(t *testing.T) {
maxSize := 5
minSize := 1
strict := true
opts := clusters.ResizeOpts{
AdjustmentType: "CHANGE_IN_CAPACITY",
MaxSize: &maxSize,
MinSize: &minSize,
Strict: &strict,
}
_, err := opts.ToClusterResizeMap()
isValid := err == nil
th.AssertEquals(t, false, isValid)
}
// Test case for float Number field which is only valid for CHANGE_IN_PERCENTAGE.
func TestResizeClusterInvalidParamsNumberFloat(t *testing.T) {
maxSize := 5
minSize := 1
number := 100.0
strict := true
opts := clusters.ResizeOpts{
AdjustmentType: "CHANGE_IN_CAPACITY",
MaxSize: &maxSize,
MinSize: &minSize,
Number: number,
Strict: &strict,
}
_, err := opts.ToClusterResizeMap()
isValid := err == nil
th.AssertEquals(t, false, isValid)
}
func TestClusterScaleIn(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleScaleInSuccessfully(t)
count := 5
scaleOpts := clusters.ScaleInOpts{
Count: &count,
}
actionID, err := clusters.ScaleIn(fake.ServiceClient(), "edce3528-864f-41fb-8759-f4707925cc09", scaleOpts).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, ExpectedActionID, actionID)
}
func TestListClusterPolicies(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleListPoliciesSuccessfully(t)
pageCount := 0
err := clusters.ListPolicies(fake.ServiceClient(), ExpectedClusterPolicy.ClusterID, clusters.ListPoliciesOpts{Name: "Test"}).EachPage(func(page pagination.Page) (bool, error) {
pageCount++
actual, err := clusters.ExtractClusterPolicies(page)
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, ExpectedListPolicies, actual)
return true, nil
})
th.AssertNoErr(t, err)
th.AssertEquals(t, pageCount, 1)
}
func TestGetClusterPolicies(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleGetPolicySuccessfully(t)
actual, err := clusters.GetPolicy(fake.ServiceClient(), "7d85f602-a948-4a30-afd4-e84f47471c15", "714fe676-a08f-4196-b7af-61d52eeded15").Extract()
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, ExpectedClusterPolicy, *actual)
}
func TestClusterRecover(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleRecoverSuccessfully(t)
recoverOpts := clusters.RecoverOpts{
Operation: clusters.RebuildRecovery,
Check: new(bool),
CheckCapacity: new(bool),
}
actionID, err := clusters.Recover(fake.ServiceClient(), "edce3528-864f-41fb-8759-f4707925cc09", recoverOpts).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, ExpectedActionID, actionID)
}
func TestAttachPolicy(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleAttachPolicySuccessfully(t)
enabled := true
opts := clusters.AttachPolicyOpts{
PolicyID: "policy1",
Enabled: &enabled,
}
actionID, err := clusters.AttachPolicy(fake.ServiceClient(), "7d85f602-a948-4a30-afd4-e84f47471c15", opts).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, ExpectedActionID, actionID)
}
func TestDetachPolicy(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleDetachPolicySuccessfully(t)
opts := clusters.DetachPolicyOpts{
PolicyID: "policy1",
}
actionID, err := clusters.DetachPolicy(fake.ServiceClient(), "7d85f602-a948-4a30-afd4-e84f47471c15", opts).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, ExpectedActionID, actionID)
}
func TestUpdatePolicy(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleUpdatePolicySuccessfully(t)
enabled := true
opts := clusters.UpdatePolicyOpts{
PolicyID: "policy1",
Enabled: &enabled,
}
actionID, err := clusters.UpdatePolicy(fake.ServiceClient(), "7d85f602-a948-4a30-afd4-e84f47471c15", opts).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, ExpectedActionID, actionID)
}
func TestClusterScaleOut(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleScaleOutSuccessfully(t)
scaleOutOpts := clusters.ScaleOutOpts{
Count: 5,
}
actionID, err := clusters.ScaleOut(fake.ServiceClient(), "edce3528-864f-41fb-8759-f4707925cc09", scaleOutOpts).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, ExpectedActionID, actionID)
}
func TestClusterCheck(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleCheckSuccessfully(t)
actionID, err := clusters.Check(fake.ServiceClient(), "edce3528-864f-41fb-8759-f4707925cc09").Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, ExpectedActionID, actionID)
}
func TestLifecycle(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleLifecycleSuccessfully(t)
opts := clusters.CompleteLifecycleOpts{
LifecycleActionTokenID: "976528c6-dcf6-4d8d-9f4c-588f4e675f29",
}
res := clusters.CompleteLifecycle(fake.ServiceClient(), "edce3528-864f-41fb-8759-f4707925cc09", opts)
location := res.Header.Get("Location")
th.AssertEquals(t, "http://senlin.cloud.blizzard.net:8778/v1/actions/2a0ff107-e789-4660-a122-3816c43af703", location)
actionID, err := res.Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, "2a0ff107-e789-4660-a122-3816c43af703", actionID)
}
func TestAddNodes(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleAddNodesSuccessfully(t)
opts := clusters.AddNodesOpts{
Nodes: []string{"node1", "node2", "node3"},
}
result, err := clusters.AddNodes(fake.ServiceClient(), "7d85f602-a948-4a30-afd4-e84f47471c15", opts).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, result, "2a0ff107-e789-4660-a122-3816c43af703")
}
func TestRemoveNodes(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleRemoveNodesSuccessfully(t)
opts := clusters.RemoveNodesOpts{
Nodes: []string{"node1", "node2", "node3"},
}
result, err := clusters.RemoveNodes(fake.ServiceClient(), "7d85f602-a948-4a30-afd4-e84f47471c15", opts).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, result, "2a0ff107-e789-4660-a122-3816c43af703")
}
func TestReplaceNodes(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleReplaceNodeSuccessfully(t)
opts := clusters.ReplaceNodesOpts{
Nodes: map[string]string{"node-1234": "node-5678"},
}
actionID, err := clusters.ReplaceNodes(fake.ServiceClient(), "7d85f602-a948-4a30-afd4-e84f47471c15", opts).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, actionID, "2a0ff107-e789-4660-a122-3816c43af703")
}
func TestClusterCollect(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleClusterCollectSuccessfully(t)
opts := clusters.CollectOpts{
Path: "foo.bar",
}
attributes, err := clusters.Collect(fake.ServiceClient(), "7d85f602-a948-4a30-afd4-e84f47471c15", opts).Extract()
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, ExpectedCollectAttributes, attributes)
}
func TestOperation(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleOpsSuccessfully(t)
clusterOpts := clusters.OperationOpts{
Operation: clusters.PauseOperation,
Filters: clusters.OperationFilters{"role": "slave"},
Params: clusters.OperationParams{"type": "soft"},
}
actual, err := clusters.Ops(fake.ServiceClient(), "7d85f602-a948-4a30-afd4-e84f47471c15", clusterOpts).Extract()
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, OperationExpectedActionID, actual)
}
|