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