blob: bf49816560ca70b032e8e48f6fc9548ac01356cf [file] [log] [blame]
Brad Ison53e997c2016-03-26 18:02:05 -04001package policies
2
3import (
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 Isone7d6dfc2016-04-06 14:55:07 -040011const (
12 groupID = "10eb3219-1b12-4b34-b1e4-e10ee4f24c65"
13)
14
Brad Ison53e997c2016-03-26 18:02:05 -040015func 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 Isone7d6dfc2016-04-06 14:55:07 -040049
50func 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}