blob: 88a52a7e3213cd5d9ef47af6e45e1769d134352f [file] [log] [blame]
Brad Ison53e997c2016-03-26 18:02:05 -04001package policies
2
3import (
4 "testing"
Brad Isona06e2ca2016-04-11 14:59:06 -04005 "time"
Brad Ison53e997c2016-03-26 18:02:05 -04006
7 "github.com/rackspace/gophercloud/pagination"
8 th "github.com/rackspace/gophercloud/testhelper"
9 "github.com/rackspace/gophercloud/testhelper/client"
10)
11
Brad Isone7d6dfc2016-04-06 14:55:07 -040012const (
Brad Ison55523e52016-04-06 19:25:20 -040013 groupID = "60b15dad-5ea1-43fa-9a12-a1d737b4da07"
14 webhookPolicyID = "2b48d247-0282-4b9d-8775-5c4b67e8e649"
Brad Isone7d6dfc2016-04-06 14:55:07 -040015)
16
Brad Ison53e997c2016-03-26 18:02:05 -040017func TestList(t *testing.T) {
18 th.SetupHTTP()
19 defer th.TeardownHTTP()
20 HandlePolicyListSuccessfully(t)
21
22 pages := 0
Brad Ison55523e52016-04-06 19:25:20 -040023 pager := List(client.ServiceClient(), "60b15dad-5ea1-43fa-9a12-a1d737b4da07")
Brad Ison53e997c2016-03-26 18:02:05 -040024
25 err := pager.EachPage(func(page pagination.Page) (bool, error) {
26 pages++
27
28 policies, err := ExtractPolicies(page)
29
30 if err != nil {
31 return false, err
32 }
33
34 if len(policies) != 3 {
35 t.Fatalf("Expected 3 policies, got %d", len(policies))
36 }
37
38 th.CheckDeepEquals(t, WebhookPolicy, policies[0])
39 th.CheckDeepEquals(t, OneTimePolicy, policies[1])
40 th.CheckDeepEquals(t, SundayAfternoonPolicy, policies[2])
41
42 return true, nil
43 })
44
45 th.AssertNoErr(t, err)
46
47 if pages != 1 {
48 t.Errorf("Expected 1 page, saw %d", pages)
49 }
50}
Brad Isone7d6dfc2016-04-06 14:55:07 -040051
52func TestCreate(t *testing.T) {
53 th.SetupHTTP()
54 defer th.TeardownHTTP()
55 HandlePolicyCreateSuccessfully(t)
56
Brad Isona06e2ca2016-04-11 14:59:06 -040057 oneTime := time.Date(2020, time.April, 01, 23, 0, 0, 0, time.UTC)
Brad Isone7d6dfc2016-04-06 14:55:07 -040058 client := client.ServiceClient()
59 opts := CreateOpts{
60 {
Brad Isonb35ef6d2016-04-09 16:54:57 -040061 Name: "webhook policy",
62 Type: Webhook,
63 Cooldown: 300,
64 AdjustmentType: ChangePercent,
65 AdjustmentValue: 3.3,
Brad Isone7d6dfc2016-04-06 14:55:07 -040066 },
67 {
Brad Isonb35ef6d2016-04-09 16:54:57 -040068 Name: "one time",
69 Type: Schedule,
70 AdjustmentType: Change,
71 AdjustmentValue: -1,
Brad Isona06e2ca2016-04-11 14:59:06 -040072 Schedule: At(oneTime),
Brad Isone7d6dfc2016-04-06 14:55:07 -040073 },
74 {
Brad Isonb35ef6d2016-04-09 16:54:57 -040075 Name: "sunday afternoon",
76 Type: Schedule,
77 AdjustmentType: DesiredCapacity,
78 AdjustmentValue: 2,
Brad Isona06e2ca2016-04-11 14:59:06 -040079 Schedule: Cron("59 15 * * 0"),
Brad Isone7d6dfc2016-04-06 14:55:07 -040080 },
81 }
82
83 policies, err := Create(client, groupID, opts).Extract()
84
85 th.AssertNoErr(t, err)
86 th.CheckDeepEquals(t, WebhookPolicy, policies[0])
87 th.CheckDeepEquals(t, OneTimePolicy, policies[1])
88 th.CheckDeepEquals(t, SundayAfternoonPolicy, policies[2])
89}
Brad Ison55523e52016-04-06 19:25:20 -040090
91func TestGet(t *testing.T) {
92 th.SetupHTTP()
93 defer th.TeardownHTTP()
94 HandlePolicyGetSuccessfully(t)
95
96 client := client.ServiceClient()
97
98 policy, err := Get(client, groupID, webhookPolicyID).Extract()
99
100 th.AssertNoErr(t, err)
101 th.CheckDeepEquals(t, WebhookPolicy, *policy)
102}
Brad Isonac037d52016-04-07 19:41:29 -0400103
104func TestUpdate(t *testing.T) {
105 th.SetupHTTP()
106 defer th.TeardownHTTP()
107 HandlePolicyUpdateSuccessfully(t)
108
109 client := client.ServiceClient()
110 opts := UpdateOpts{
Brad Isonb35ef6d2016-04-09 16:54:57 -0400111 Name: "updated webhook policy",
112 Type: Webhook,
113 Cooldown: 600,
114 AdjustmentType: ChangePercent,
115 AdjustmentValue: 6.6,
Brad Isonac037d52016-04-07 19:41:29 -0400116 }
117
118 err := Update(client, groupID, webhookPolicyID, opts).ExtractErr()
119
120 th.AssertNoErr(t, err)
121}
Brad Ison124df8e2016-04-07 19:51:51 -0400122
123func TestDelete(t *testing.T) {
124 th.SetupHTTP()
125 defer th.TeardownHTTP()
126 HandlePolicyDeleteSuccessfully(t)
127
128 client := client.ServiceClient()
129 err := Delete(client, groupID, webhookPolicyID).ExtractErr()
130
131 th.AssertNoErr(t, err)
132}
Brad Ison42f8dfb2016-04-07 20:26:06 -0400133
134func TestExecute(t *testing.T) {
135 th.SetupHTTP()
136 defer th.TeardownHTTP()
137 HandlePolicyExecuteSuccessfully(t)
138
139 client := client.ServiceClient()
140 err := Execute(client, groupID, webhookPolicyID).ExtractErr()
141
142 th.AssertNoErr(t, err)
143}
Brad Isoneedc8d92016-04-11 13:15:16 -0400144
145func TestValidateType(t *testing.T) {
146 ok := validateType(Schedule)
147 th.AssertEquals(t, true, ok)
148
149 ok = validateType(Webhook)
150 th.AssertEquals(t, true, ok)
151
152 ok = validateType("BAD")
153 th.AssertEquals(t, false, ok)
154}
155
156func TestValidateCooldown(t *testing.T) {
157 ok := validateCooldown(0)
158 th.AssertEquals(t, true, ok)
159
160 ok = validateCooldown(86400)
161 th.AssertEquals(t, true, ok)
162
163 ok = validateCooldown(-1)
164 th.AssertEquals(t, false, ok)
165
166 ok = validateCooldown(172800)
167 th.AssertEquals(t, false, ok)
168}