Brad Ison | 53e997c | 2016-03-26 18:02:05 -0400 | [diff] [blame] | 1 | package policies |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
Brad Ison | a06e2ca | 2016-04-11 14:59:06 -0400 | [diff] [blame^] | 5 | "time" |
Brad Ison | 53e997c | 2016-03-26 18:02:05 -0400 | [diff] [blame] | 6 | |
| 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | th "github.com/rackspace/gophercloud/testhelper" |
| 9 | "github.com/rackspace/gophercloud/testhelper/client" |
| 10 | ) |
| 11 | |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame] | 12 | const ( |
Brad Ison | 55523e5 | 2016-04-06 19:25:20 -0400 | [diff] [blame] | 13 | groupID = "60b15dad-5ea1-43fa-9a12-a1d737b4da07" |
| 14 | webhookPolicyID = "2b48d247-0282-4b9d-8775-5c4b67e8e649" |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame] | 15 | ) |
| 16 | |
Brad Ison | 53e997c | 2016-03-26 18:02:05 -0400 | [diff] [blame] | 17 | func TestList(t *testing.T) { |
| 18 | th.SetupHTTP() |
| 19 | defer th.TeardownHTTP() |
| 20 | HandlePolicyListSuccessfully(t) |
| 21 | |
| 22 | pages := 0 |
Brad Ison | 55523e5 | 2016-04-06 19:25:20 -0400 | [diff] [blame] | 23 | pager := List(client.ServiceClient(), "60b15dad-5ea1-43fa-9a12-a1d737b4da07") |
Brad Ison | 53e997c | 2016-03-26 18:02:05 -0400 | [diff] [blame] | 24 | |
| 25 | err := pager.EachPage(func(page pagination.Page) (bool, error) { |
| 26 | pages++ |
| 27 | |
| 28 | policies, err := ExtractPolicies(page) |
| 29 | |
| 30 | if err != nil { |
| 31 | return false, err |
| 32 | } |
| 33 | |
| 34 | if len(policies) != 3 { |
| 35 | t.Fatalf("Expected 3 policies, got %d", len(policies)) |
| 36 | } |
| 37 | |
| 38 | th.CheckDeepEquals(t, WebhookPolicy, policies[0]) |
| 39 | th.CheckDeepEquals(t, OneTimePolicy, policies[1]) |
| 40 | th.CheckDeepEquals(t, SundayAfternoonPolicy, policies[2]) |
| 41 | |
| 42 | return true, nil |
| 43 | }) |
| 44 | |
| 45 | th.AssertNoErr(t, err) |
| 46 | |
| 47 | if pages != 1 { |
| 48 | t.Errorf("Expected 1 page, saw %d", pages) |
| 49 | } |
| 50 | } |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame] | 51 | |
| 52 | func TestCreate(t *testing.T) { |
| 53 | th.SetupHTTP() |
| 54 | defer th.TeardownHTTP() |
| 55 | HandlePolicyCreateSuccessfully(t) |
| 56 | |
Brad Ison | a06e2ca | 2016-04-11 14:59:06 -0400 | [diff] [blame^] | 57 | oneTime := time.Date(2020, time.April, 01, 23, 0, 0, 0, time.UTC) |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame] | 58 | client := client.ServiceClient() |
| 59 | opts := CreateOpts{ |
| 60 | { |
Brad Ison | b35ef6d | 2016-04-09 16:54:57 -0400 | [diff] [blame] | 61 | Name: "webhook policy", |
| 62 | Type: Webhook, |
| 63 | Cooldown: 300, |
| 64 | AdjustmentType: ChangePercent, |
| 65 | AdjustmentValue: 3.3, |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame] | 66 | }, |
| 67 | { |
Brad Ison | b35ef6d | 2016-04-09 16:54:57 -0400 | [diff] [blame] | 68 | Name: "one time", |
| 69 | Type: Schedule, |
| 70 | AdjustmentType: Change, |
| 71 | AdjustmentValue: -1, |
Brad Ison | a06e2ca | 2016-04-11 14:59:06 -0400 | [diff] [blame^] | 72 | Schedule: At(oneTime), |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame] | 73 | }, |
| 74 | { |
Brad Ison | b35ef6d | 2016-04-09 16:54:57 -0400 | [diff] [blame] | 75 | Name: "sunday afternoon", |
| 76 | Type: Schedule, |
| 77 | AdjustmentType: DesiredCapacity, |
| 78 | AdjustmentValue: 2, |
Brad Ison | a06e2ca | 2016-04-11 14:59:06 -0400 | [diff] [blame^] | 79 | Schedule: Cron("59 15 * * 0"), |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame] | 80 | }, |
| 81 | } |
| 82 | |
| 83 | policies, err := Create(client, groupID, opts).Extract() |
| 84 | |
| 85 | th.AssertNoErr(t, err) |
| 86 | th.CheckDeepEquals(t, WebhookPolicy, policies[0]) |
| 87 | th.CheckDeepEquals(t, OneTimePolicy, policies[1]) |
| 88 | th.CheckDeepEquals(t, SundayAfternoonPolicy, policies[2]) |
| 89 | } |
Brad Ison | 55523e5 | 2016-04-06 19:25:20 -0400 | [diff] [blame] | 90 | |
| 91 | func TestGet(t *testing.T) { |
| 92 | th.SetupHTTP() |
| 93 | defer th.TeardownHTTP() |
| 94 | HandlePolicyGetSuccessfully(t) |
| 95 | |
| 96 | client := client.ServiceClient() |
| 97 | |
| 98 | policy, err := Get(client, groupID, webhookPolicyID).Extract() |
| 99 | |
| 100 | th.AssertNoErr(t, err) |
| 101 | th.CheckDeepEquals(t, WebhookPolicy, *policy) |
| 102 | } |
Brad Ison | ac037d5 | 2016-04-07 19:41:29 -0400 | [diff] [blame] | 103 | |
| 104 | func TestUpdate(t *testing.T) { |
| 105 | th.SetupHTTP() |
| 106 | defer th.TeardownHTTP() |
| 107 | HandlePolicyUpdateSuccessfully(t) |
| 108 | |
| 109 | client := client.ServiceClient() |
| 110 | opts := UpdateOpts{ |
Brad Ison | b35ef6d | 2016-04-09 16:54:57 -0400 | [diff] [blame] | 111 | Name: "updated webhook policy", |
| 112 | Type: Webhook, |
| 113 | Cooldown: 600, |
| 114 | AdjustmentType: ChangePercent, |
| 115 | AdjustmentValue: 6.6, |
Brad Ison | ac037d5 | 2016-04-07 19:41:29 -0400 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | err := Update(client, groupID, webhookPolicyID, opts).ExtractErr() |
| 119 | |
| 120 | th.AssertNoErr(t, err) |
| 121 | } |
Brad Ison | 124df8e | 2016-04-07 19:51:51 -0400 | [diff] [blame] | 122 | |
| 123 | func TestDelete(t *testing.T) { |
| 124 | th.SetupHTTP() |
| 125 | defer th.TeardownHTTP() |
| 126 | HandlePolicyDeleteSuccessfully(t) |
| 127 | |
| 128 | client := client.ServiceClient() |
| 129 | err := Delete(client, groupID, webhookPolicyID).ExtractErr() |
| 130 | |
| 131 | th.AssertNoErr(t, err) |
| 132 | } |
Brad Ison | 42f8dfb | 2016-04-07 20:26:06 -0400 | [diff] [blame] | 133 | |
| 134 | func TestExecute(t *testing.T) { |
| 135 | th.SetupHTTP() |
| 136 | defer th.TeardownHTTP() |
| 137 | HandlePolicyExecuteSuccessfully(t) |
| 138 | |
| 139 | client := client.ServiceClient() |
| 140 | err := Execute(client, groupID, webhookPolicyID).ExtractErr() |
| 141 | |
| 142 | th.AssertNoErr(t, err) |
| 143 | } |
Brad Ison | eedc8d9 | 2016-04-11 13:15:16 -0400 | [diff] [blame] | 144 | |
| 145 | func TestValidateType(t *testing.T) { |
| 146 | ok := validateType(Schedule) |
| 147 | th.AssertEquals(t, true, ok) |
| 148 | |
| 149 | ok = validateType(Webhook) |
| 150 | th.AssertEquals(t, true, ok) |
| 151 | |
| 152 | ok = validateType("BAD") |
| 153 | th.AssertEquals(t, false, ok) |
| 154 | } |
| 155 | |
| 156 | func TestValidateCooldown(t *testing.T) { |
| 157 | ok := validateCooldown(0) |
| 158 | th.AssertEquals(t, true, ok) |
| 159 | |
| 160 | ok = validateCooldown(86400) |
| 161 | th.AssertEquals(t, true, ok) |
| 162 | |
| 163 | ok = validateCooldown(-1) |
| 164 | th.AssertEquals(t, false, ok) |
| 165 | |
| 166 | ok = validateCooldown(172800) |
| 167 | th.AssertEquals(t, false, ok) |
| 168 | } |