blob: 9334740402d53b7d9f3c92b8a42f2cc31e2f5b76 [file] [log] [blame]
Brad Ison53e997c2016-03-26 18:02:05 -04001// +build fixtures
2
3package policies
4
5import (
6 "fmt"
7 "net/http"
8 "testing"
Brad Isona06e2ca2016-04-11 14:59:06 -04009 "time"
Brad Ison53e997c2016-03-26 18:02:05 -040010
11 th "github.com/rackspace/gophercloud/testhelper"
12 "github.com/rackspace/gophercloud/testhelper/client"
13)
14
15// PolicyListBody contains the canned body of a policies.List response.
16const PolicyListBody = `
17{
18 "policies_links": [],
19 "policies": [
20 {
21 "name": "webhook policy",
22 "links": [
23 {
24 "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/",
25 "rel": "self"
26 }
27 ],
Brad Isone7d6dfc2016-04-06 14:55:07 -040028 "changePercent": 3.3,
Brad Ison53e997c2016-03-26 18:02:05 -040029 "cooldown": 300,
30 "type": "webhook",
31 "id": "2b48d247-0282-4b9d-8775-5c4b67e8e649"
32 },
33 {
34 "cooldown": 0,
35 "name": "one time",
36 "links": [
37 {
38 "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/c175c31e-65f9-41de-8b15-918420d3253e/",
39 "rel": "self"
40 }
41 ],
42 "args": {
43 "at": "2020-04-01T23:00:00.000Z"
44 },
45 "type": "schedule",
46 "id": "c175c31e-65f9-41de-8b15-918420d3253e",
47 "change": -1
48 },
49 {
50 "cooldown": 0,
51 "name": "sunday afternoon",
52 "links": [
53 {
54 "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/e785e3e7-af9e-4f3c-99ae-b80a532e1663/",
55 "rel": "self"
56 }
57 ],
58 "args": {
59 "cron": "59 15 * * 0"
60 },
61 "type": "schedule",
62 "id": "e785e3e7-af9e-4f3c-99ae-b80a532e1663",
Brad Isone7d6dfc2016-04-06 14:55:07 -040063 "desiredCapacity": 2
Brad Ison53e997c2016-03-26 18:02:05 -040064 }
65 ]
66}
67`
68
Brad Isone7d6dfc2016-04-06 14:55:07 -040069// PolicyCreateBody contains the canned body of a policies.Create response.
70const PolicyCreateBody = PolicyListBody
71
72// PolicyCreateRequest contains the canned body of a policies.Create request.
73const PolicyCreateRequest = `
74[
75 {
76 "name": "webhook policy",
77 "changePercent": 3.3,
78 "cooldown": 300,
79 "type": "webhook"
80 },
81 {
82 "cooldown": 0,
83 "name": "one time",
84 "args": {
Brad Isona06e2ca2016-04-11 14:59:06 -040085 "at": "2020-04-01T23:00:00Z"
Brad Isone7d6dfc2016-04-06 14:55:07 -040086 },
87 "type": "schedule",
88 "change": -1
89 },
90 {
91 "cooldown": 0,
92 "name": "sunday afternoon",
93 "args": {
94 "cron": "59 15 * * 0"
95 },
96 "type": "schedule",
97 "desiredCapacity": 2
98 }
99]
100`
101
Brad Ison55523e52016-04-06 19:25:20 -0400102// PolicyGetBody contains the canned body of a policies.Get response.
103const PolicyGetBody = `
104{
105 "policy": {
106 "name": "webhook policy",
107 "links": [
108 {
109 "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/",
110 "rel": "self"
111 }
112 ],
113 "changePercent": 3.3,
114 "cooldown": 300,
115 "type": "webhook",
116 "id": "2b48d247-0282-4b9d-8775-5c4b67e8e649"
117 }
118}
119`
120
Brad Isonac037d52016-04-07 19:41:29 -0400121// PolicyUpdateRequest contains the canned body of a policies.Update request.
122const PolicyUpdateRequest = `
123{
124 "name": "updated webhook policy",
125 "type": "webhook",
126 "cooldown": 600,
127 "changePercent": 6.6
128}
129`
130
Brad Ison53e997c2016-03-26 18:02:05 -0400131var (
132 // WebhookPolicy is a Policy corresponding to the first result in PolicyListBody.
133 WebhookPolicy = Policy{
Brad Ison2b739162016-04-11 12:40:11 -0400134 ID: "2b48d247-0282-4b9d-8775-5c4b67e8e649",
135 Name: "webhook policy",
136 Type: Webhook,
137 Cooldown: 300,
138 AdjustmentType: ChangePercent,
139 AdjustmentValue: 3.3,
Brad Ison53e997c2016-03-26 18:02:05 -0400140 }
141
142 // OneTimePolicy is a Policy corresponding to the second result in PolicyListBody.
143 OneTimePolicy = Policy{
Brad Ison2b739162016-04-11 12:40:11 -0400144 ID: "c175c31e-65f9-41de-8b15-918420d3253e",
145 Name: "one time",
146 Type: Schedule,
147 AdjustmentType: Change,
148 AdjustmentValue: float64(-1),
Brad Isona06e2ca2016-04-11 14:59:06 -0400149 Schedule: At(time.Date(2020, time.April, 01, 23, 0, 0, 0, time.UTC)),
Brad Ison53e997c2016-03-26 18:02:05 -0400150 }
151
152 // SundayAfternoonPolicy is a Policy corresponding to the third result in PolicyListBody.
153 SundayAfternoonPolicy = Policy{
Brad Isone7d6dfc2016-04-06 14:55:07 -0400154 ID: "e785e3e7-af9e-4f3c-99ae-b80a532e1663",
155 Name: "sunday afternoon",
156 Type: Schedule,
Brad Ison2b739162016-04-11 12:40:11 -0400157 AdjustmentType: DesiredCapacity,
158 AdjustmentValue: float64(2),
Brad Isona06e2ca2016-04-11 14:59:06 -0400159 Schedule: Cron("59 15 * * 0"),
Brad Ison53e997c2016-03-26 18:02:05 -0400160 }
161)
162
163// HandlePolicyListSuccessfully sets up the test server to respond to a policies List request.
164func HandlePolicyListSuccessfully(t *testing.T) {
Brad Ison55523e52016-04-06 19:25:20 -0400165 path := "/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies"
Brad Ison53e997c2016-03-26 18:02:05 -0400166
167 th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
168 th.TestMethod(t, r, "GET")
169 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
170
171 w.Header().Add("Content-Type", "application/json")
172
173 fmt.Fprintf(w, PolicyListBody)
174 })
175}
Brad Isone7d6dfc2016-04-06 14:55:07 -0400176
177// HandlePolicyCreateSuccessfully sets up the test server to respond to a policies Create request.
178func HandlePolicyCreateSuccessfully(t *testing.T) {
Brad Ison55523e52016-04-06 19:25:20 -0400179 path := "/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies"
Brad Isone7d6dfc2016-04-06 14:55:07 -0400180
181 th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
182 th.TestMethod(t, r, "POST")
183 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
184 th.TestHeader(t, r, "Content-Type", "application/json")
185 th.TestHeader(t, r, "Accept", "application/json")
186
187 th.TestJSONRequest(t, r, PolicyCreateRequest)
188
189 w.Header().Add("Content-Type", "application/json")
190 w.WriteHeader(http.StatusCreated)
191
192 fmt.Fprintf(w, PolicyCreateBody)
193 })
194}
Brad Ison55523e52016-04-06 19:25:20 -0400195
196// HandlePolicyGetSuccessfully sets up the test server to respond to a policies Get request.
197func HandlePolicyGetSuccessfully(t *testing.T) {
198 groupID := "60b15dad-5ea1-43fa-9a12-a1d737b4da07"
199 policyID := "2b48d247-0282-4b9d-8775-5c4b67e8e649"
200
201 path := fmt.Sprintf("/groups/%s/policies/%s", groupID, policyID)
202
203 th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
204 th.TestMethod(t, r, "GET")
205 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
206
207 w.Header().Add("Content-Type", "application/json")
208
209 fmt.Fprintf(w, PolicyGetBody)
210 })
211}
Brad Isonac037d52016-04-07 19:41:29 -0400212
213// HandlePolicyUpdateSuccessfully sets up the test server to respond to a policies Update request.
214func HandlePolicyUpdateSuccessfully(t *testing.T) {
215 groupID := "60b15dad-5ea1-43fa-9a12-a1d737b4da07"
216 policyID := "2b48d247-0282-4b9d-8775-5c4b67e8e649"
217
218 path := fmt.Sprintf("/groups/%s/policies/%s", groupID, policyID)
219
220 th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
221 th.TestMethod(t, r, "PUT")
222 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
223
224 th.TestJSONRequest(t, r, PolicyUpdateRequest)
225
226 w.WriteHeader(http.StatusNoContent)
227 })
228}
Brad Ison124df8e2016-04-07 19:51:51 -0400229
230// HandlePolicyDeleteSuccessfully sets up the test server to respond to a policies Delete request.
231func HandlePolicyDeleteSuccessfully(t *testing.T) {
232 groupID := "60b15dad-5ea1-43fa-9a12-a1d737b4da07"
233 policyID := "2b48d247-0282-4b9d-8775-5c4b67e8e649"
234
235 path := fmt.Sprintf("/groups/%s/policies/%s", groupID, policyID)
236
237 th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
238 th.TestMethod(t, r, "DELETE")
239 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
240
241 w.WriteHeader(http.StatusNoContent)
242 })
243}
Brad Ison42f8dfb2016-04-07 20:26:06 -0400244
245// HandlePolicyExecuteSuccessfully sets up the test server to respond to a policies Execute request.
246func HandlePolicyExecuteSuccessfully(t *testing.T) {
247 groupID := "60b15dad-5ea1-43fa-9a12-a1d737b4da07"
248 policyID := "2b48d247-0282-4b9d-8775-5c4b67e8e649"
249
250 path := fmt.Sprintf("/groups/%s/policies/%s/execute", groupID, policyID)
251
252 th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
253 th.TestMethod(t, r, "POST")
254 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
255
256 w.WriteHeader(http.StatusAccepted)
257 fmt.Fprintf(w, "{}")
258 })
259}