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 | { |
| 59 | Name: "webhook policy", |
| 60 | Type: Webhook, |
| 61 | Cooldown: 300, |
| 62 | Adjustment: Adjustment{ |
| 63 | Type: ChangePercent, |
| 64 | Value: 3.3, |
| 65 | }, |
| 66 | }, |
| 67 | { |
| 68 | Name: "one time", |
| 69 | Type: Schedule, |
| 70 | Adjustment: Adjustment{ |
| 71 | Type: Change, |
| 72 | Value: -1, |
| 73 | }, |
| 74 | Args: map[string]interface{}{ |
| 75 | "at": "2020-04-01T23:00:00.000Z", |
| 76 | }, |
| 77 | }, |
| 78 | { |
| 79 | Name: "sunday afternoon", |
| 80 | Type: Schedule, |
| 81 | Adjustment: Adjustment{ |
| 82 | Type: DesiredCapacity, |
| 83 | Value: 2, |
| 84 | }, |
| 85 | Args: map[string]interface{}{ |
| 86 | "cron": "59 15 * * 0", |
| 87 | }, |
| 88 | }, |
| 89 | } |
| 90 | |
| 91 | policies, err := Create(client, groupID, opts).Extract() |
| 92 | |
| 93 | th.AssertNoErr(t, err) |
| 94 | th.CheckDeepEquals(t, WebhookPolicy, policies[0]) |
| 95 | th.CheckDeepEquals(t, OneTimePolicy, policies[1]) |
| 96 | th.CheckDeepEquals(t, SundayAfternoonPolicy, policies[2]) |
| 97 | } |
Brad Ison | 55523e5 | 2016-04-06 19:25:20 -0400 | [diff] [blame] | 98 | |
| 99 | func TestGet(t *testing.T) { |
| 100 | th.SetupHTTP() |
| 101 | defer th.TeardownHTTP() |
| 102 | HandlePolicyGetSuccessfully(t) |
| 103 | |
| 104 | client := client.ServiceClient() |
| 105 | |
| 106 | policy, err := Get(client, groupID, webhookPolicyID).Extract() |
| 107 | |
| 108 | th.AssertNoErr(t, err) |
| 109 | th.CheckDeepEquals(t, WebhookPolicy, *policy) |
| 110 | } |
Brad Ison | ac037d5 | 2016-04-07 19:41:29 -0400 | [diff] [blame] | 111 | |
| 112 | func TestUpdate(t *testing.T) { |
| 113 | th.SetupHTTP() |
| 114 | defer th.TeardownHTTP() |
| 115 | HandlePolicyUpdateSuccessfully(t) |
| 116 | |
| 117 | client := client.ServiceClient() |
| 118 | opts := UpdateOpts{ |
| 119 | Name: "updated webhook policy", |
| 120 | Type: Webhook, |
| 121 | Cooldown: 600, |
| 122 | Adjustment: Adjustment{ |
| 123 | Type: ChangePercent, |
| 124 | Value: 6.6, |
| 125 | }, |
| 126 | } |
| 127 | |
| 128 | err := Update(client, groupID, webhookPolicyID, opts).ExtractErr() |
| 129 | |
| 130 | th.AssertNoErr(t, err) |
| 131 | } |
Brad Ison | 124df8e | 2016-04-07 19:51:51 -0400 | [diff] [blame^] | 132 | |
| 133 | func TestDelete(t *testing.T) { |
| 134 | th.SetupHTTP() |
| 135 | defer th.TeardownHTTP() |
| 136 | HandlePolicyDeleteSuccessfully(t) |
| 137 | |
| 138 | client := client.ServiceClient() |
| 139 | err := Delete(client, groupID, webhookPolicyID).ExtractErr() |
| 140 | |
| 141 | th.AssertNoErr(t, err) |
| 142 | } |