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 ( |
| 12 | groupID = "10eb3219-1b12-4b34-b1e4-e10ee4f24c65" |
| 13 | ) |
| 14 | |
Brad Ison | 53e997c | 2016-03-26 18:02:05 -0400 | [diff] [blame] | 15 | func TestList(t *testing.T) { |
| 16 | th.SetupHTTP() |
| 17 | defer th.TeardownHTTP() |
| 18 | HandlePolicyListSuccessfully(t) |
| 19 | |
| 20 | pages := 0 |
| 21 | pager := List(client.ServiceClient(), "10eb3219-1b12-4b34-b1e4-e10ee4f24c65") |
| 22 | |
| 23 | err := pager.EachPage(func(page pagination.Page) (bool, error) { |
| 24 | pages++ |
| 25 | |
| 26 | policies, err := ExtractPolicies(page) |
| 27 | |
| 28 | if err != nil { |
| 29 | return false, err |
| 30 | } |
| 31 | |
| 32 | if len(policies) != 3 { |
| 33 | t.Fatalf("Expected 3 policies, got %d", len(policies)) |
| 34 | } |
| 35 | |
| 36 | th.CheckDeepEquals(t, WebhookPolicy, policies[0]) |
| 37 | th.CheckDeepEquals(t, OneTimePolicy, policies[1]) |
| 38 | th.CheckDeepEquals(t, SundayAfternoonPolicy, policies[2]) |
| 39 | |
| 40 | return true, nil |
| 41 | }) |
| 42 | |
| 43 | th.AssertNoErr(t, err) |
| 44 | |
| 45 | if pages != 1 { |
| 46 | t.Errorf("Expected 1 page, saw %d", pages) |
| 47 | } |
| 48 | } |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame^] | 49 | |
| 50 | func TestCreate(t *testing.T) { |
| 51 | th.SetupHTTP() |
| 52 | defer th.TeardownHTTP() |
| 53 | HandlePolicyCreateSuccessfully(t) |
| 54 | |
| 55 | client := client.ServiceClient() |
| 56 | opts := CreateOpts{ |
| 57 | { |
| 58 | Name: "webhook policy", |
| 59 | Type: Webhook, |
| 60 | Cooldown: 300, |
| 61 | Adjustment: Adjustment{ |
| 62 | Type: ChangePercent, |
| 63 | Value: 3.3, |
| 64 | }, |
| 65 | }, |
| 66 | { |
| 67 | Name: "one time", |
| 68 | Type: Schedule, |
| 69 | Adjustment: Adjustment{ |
| 70 | Type: Change, |
| 71 | Value: -1, |
| 72 | }, |
| 73 | Args: map[string]interface{}{ |
| 74 | "at": "2020-04-01T23:00:00.000Z", |
| 75 | }, |
| 76 | }, |
| 77 | { |
| 78 | Name: "sunday afternoon", |
| 79 | Type: Schedule, |
| 80 | Adjustment: Adjustment{ |
| 81 | Type: DesiredCapacity, |
| 82 | Value: 2, |
| 83 | }, |
| 84 | Args: map[string]interface{}{ |
| 85 | "cron": "59 15 * * 0", |
| 86 | }, |
| 87 | }, |
| 88 | } |
| 89 | |
| 90 | policies, err := Create(client, groupID, opts).Extract() |
| 91 | |
| 92 | th.AssertNoErr(t, err) |
| 93 | th.CheckDeepEquals(t, WebhookPolicy, policies[0]) |
| 94 | th.CheckDeepEquals(t, OneTimePolicy, policies[1]) |
| 95 | th.CheckDeepEquals(t, SundayAfternoonPolicy, policies[2]) |
| 96 | } |