github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/acceptance/openstack/autoscaling/v1/policies_test.go (about) 1 package v1 2 3 import ( 4 "testing" 5 6 "github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients" 7 "github.com/opentelekomcloud/gophertelekomcloud/acceptance/openstack/autoscaling" 8 "github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools" 9 "github.com/opentelekomcloud/gophertelekomcloud/openstack/autoscaling/v1/policies" 10 th "github.com/opentelekomcloud/gophertelekomcloud/testhelper" 11 ) 12 13 func TestPolicyLifecycle(t *testing.T) { 14 client, err := clients.NewAutoscalingV1Client() 15 th.AssertNoErr(t, err) 16 17 networkID := clients.EnvOS.GetEnv("NETWORK_ID") 18 vpcID := clients.EnvOS.GetEnv("VPC_ID") 19 if networkID == "" || vpcID == "" { 20 t.Skip("OS_NETWORK_ID or OS_VPC_ID env vars are missing but AS Policy test requires") 21 } 22 23 groupID := autoscaling.CreateAutoScalingGroup(t, client, networkID, vpcID, tools.RandomString("as-group-create-", 3)) 24 t.Cleanup(func() { 25 autoscaling.DeleteAutoScalingGroup(t, client, groupID) 26 }) 27 28 asPolicyCreateName := tools.RandomString("as-policy-create-", 3) 29 t.Logf("Attempting to create AutoScaling Policy") 30 policyID, err := policies.Create(client, policies.CreateOpts{ 31 Name: asPolicyCreateName, 32 Type: "RECURRENCE", 33 ID: groupID, 34 SchedulePolicy: policies.SchedulePolicyOpts{ 35 LaunchTime: "10:30", 36 RecurrenceType: "Weekly", 37 RecurrenceValue: "1,3,5", 38 EndTime: "2040-12-31T10:30Z", 39 }, 40 Action: policies.Action{ 41 Operation: "ADD", 42 InstancePercentage: 15, 43 }, 44 }) 45 th.AssertNoErr(t, err) 46 t.Logf("Created AutoScaling Policy: %s", policyID) 47 48 t.Cleanup(func() { 49 t.Logf("Attempting to delete AutoScaling Policy") 50 err := policies.Delete(client, policyID) 51 th.AssertNoErr(t, err) 52 t.Logf("Deleted AutoScaling Policy: %s", policyID) 53 }) 54 55 policy, err := policies.Get(client, policyID) 56 th.AssertNoErr(t, err) 57 th.AssertEquals(t, asPolicyCreateName, policy.Name) 58 th.AssertEquals(t, 15, policy.Action.InstancePercentage) 59 th.AssertEquals(t, "ADD", policy.Action.Operation) 60 61 t.Logf("Attempting to update AutoScaling policy") 62 asPolicyUpdateName := tools.RandomString("as-policy-update-", 3) 63 64 updateOpts := policies.UpdateOpts{ 65 Name: asPolicyUpdateName, 66 Type: "RECURRENCE", 67 SchedulePolicy: policies.CreateOpts{ 68 Name: asPolicyCreateName, 69 Type: "RECURRENCE", 70 ID: groupID, 71 SchedulePolicy: policies.SchedulePolicyOpts{ 72 LaunchTime: "10:30", 73 RecurrenceType: "Weekly", 74 RecurrenceValue: "1,3,5", 75 EndTime: "2040-12-31T10:30Z", 76 }, 77 Action: policies.Action{ 78 Operation: "ADD", 79 InstancePercentage: 15, 80 }, 81 }.SchedulePolicy, 82 Action: policies.Action{ 83 InstancePercentage: 30, 84 }, 85 CoolDownTime: 0, 86 } 87 88 policyID, err = policies.Update(client, policyID, updateOpts) 89 th.AssertNoErr(t, err) 90 t.Logf("Updated AutoScaling Policy") 91 92 policy, err = policies.Get(client, policyID) 93 th.AssertNoErr(t, err) 94 tools.PrintResource(t, policy) 95 th.AssertEquals(t, asPolicyUpdateName, policy.Name) 96 th.AssertEquals(t, 30, policy.Action.InstancePercentage) 97 }