blob: 3af9435a1daf0c657f7a8c5593752af96b3bb13a [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 (
Brad Ison55523e52016-04-06 19:25:20 -040012 groupID = "60b15dad-5ea1-43fa-9a12-a1d737b4da07"
13 webhookPolicyID = "2b48d247-0282-4b9d-8775-5c4b67e8e649"
Brad Isone7d6dfc2016-04-06 14:55:07 -040014)
15
Brad Ison53e997c2016-03-26 18:02:05 -040016func TestList(t *testing.T) {
17 th.SetupHTTP()
18 defer th.TeardownHTTP()
19 HandlePolicyListSuccessfully(t)
20
21 pages := 0
Brad Ison55523e52016-04-06 19:25:20 -040022 pager := List(client.ServiceClient(), "60b15dad-5ea1-43fa-9a12-a1d737b4da07")
Brad Ison53e997c2016-03-26 18:02:05 -040023
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 Isone7d6dfc2016-04-06 14:55:07 -040050
51func TestCreate(t *testing.T) {
52 th.SetupHTTP()
53 defer th.TeardownHTTP()
54 HandlePolicyCreateSuccessfully(t)
55
56 client := client.ServiceClient()
57 opts := CreateOpts{
58 {
Brad Isonb35ef6d2016-04-09 16:54:57 -040059 Name: "webhook policy",
60 Type: Webhook,
61 Cooldown: 300,
62 AdjustmentType: ChangePercent,
63 AdjustmentValue: 3.3,
Brad Isone7d6dfc2016-04-06 14:55:07 -040064 },
65 {
Brad Isonb35ef6d2016-04-09 16:54:57 -040066 Name: "one time",
67 Type: Schedule,
68 AdjustmentType: Change,
69 AdjustmentValue: -1,
Brad Isone7d6dfc2016-04-06 14:55:07 -040070 Args: map[string]interface{}{
71 "at": "2020-04-01T23:00:00.000Z",
72 },
73 },
74 {
Brad Isonb35ef6d2016-04-09 16:54:57 -040075 Name: "sunday afternoon",
76 Type: Schedule,
77 AdjustmentType: DesiredCapacity,
78 AdjustmentValue: 2,
Brad Isone7d6dfc2016-04-06 14:55:07 -040079 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 Ison55523e52016-04-06 19:25:20 -040092
93func 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 Isonac037d52016-04-07 19:41:29 -0400105
106func TestUpdate(t *testing.T) {
107 th.SetupHTTP()
108 defer th.TeardownHTTP()
109 HandlePolicyUpdateSuccessfully(t)
110
111 client := client.ServiceClient()
112 opts := UpdateOpts{
Brad Isonb35ef6d2016-04-09 16:54:57 -0400113 Name: "updated webhook policy",
114 Type: Webhook,
115 Cooldown: 600,
116 AdjustmentType: ChangePercent,
117 AdjustmentValue: 6.6,
Brad Isonac037d52016-04-07 19:41:29 -0400118 }
119
120 err := Update(client, groupID, webhookPolicyID, opts).ExtractErr()
121
122 th.AssertNoErr(t, err)
123}
Brad Ison124df8e2016-04-07 19:51:51 -0400124
125func 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 Ison42f8dfb2016-04-07 20:26:06 -0400135
136func 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}