blob: 67967b0976f1374cc82c8ae9b176ea4993982bb5 [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"
9
10 th "github.com/rackspace/gophercloud/testhelper"
11 "github.com/rackspace/gophercloud/testhelper/client"
12)
13
14// PolicyListBody contains the canned body of a policies.List response.
15const PolicyListBody = `
16{
17 "policies_links": [],
18 "policies": [
19 {
20 "name": "webhook policy",
21 "links": [
22 {
23 "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/",
24 "rel": "self"
25 }
26 ],
Brad Isone7d6dfc2016-04-06 14:55:07 -040027 "changePercent": 3.3,
Brad Ison53e997c2016-03-26 18:02:05 -040028 "cooldown": 300,
29 "type": "webhook",
30 "id": "2b48d247-0282-4b9d-8775-5c4b67e8e649"
31 },
32 {
33 "cooldown": 0,
34 "name": "one time",
35 "links": [
36 {
37 "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/c175c31e-65f9-41de-8b15-918420d3253e/",
38 "rel": "self"
39 }
40 ],
41 "args": {
42 "at": "2020-04-01T23:00:00.000Z"
43 },
44 "type": "schedule",
45 "id": "c175c31e-65f9-41de-8b15-918420d3253e",
46 "change": -1
47 },
48 {
49 "cooldown": 0,
50 "name": "sunday afternoon",
51 "links": [
52 {
53 "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/e785e3e7-af9e-4f3c-99ae-b80a532e1663/",
54 "rel": "self"
55 }
56 ],
57 "args": {
58 "cron": "59 15 * * 0"
59 },
60 "type": "schedule",
61 "id": "e785e3e7-af9e-4f3c-99ae-b80a532e1663",
Brad Isone7d6dfc2016-04-06 14:55:07 -040062 "desiredCapacity": 2
Brad Ison53e997c2016-03-26 18:02:05 -040063 }
64 ]
65}
66`
67
Brad Isone7d6dfc2016-04-06 14:55:07 -040068// PolicyCreateBody contains the canned body of a policies.Create response.
69const PolicyCreateBody = PolicyListBody
70
71// PolicyCreateRequest contains the canned body of a policies.Create request.
72const PolicyCreateRequest = `
73[
74 {
75 "name": "webhook policy",
76 "changePercent": 3.3,
77 "cooldown": 300,
78 "type": "webhook"
79 },
80 {
81 "cooldown": 0,
82 "name": "one time",
83 "args": {
84 "at": "2020-04-01T23:00:00.000Z"
85 },
86 "type": "schedule",
87 "change": -1
88 },
89 {
90 "cooldown": 0,
91 "name": "sunday afternoon",
92 "args": {
93 "cron": "59 15 * * 0"
94 },
95 "type": "schedule",
96 "desiredCapacity": 2
97 }
98]
99`
100
Brad Ison55523e52016-04-06 19:25:20 -0400101// PolicyGetBody contains the canned body of a policies.Get response.
102const PolicyGetBody = `
103{
104 "policy": {
105 "name": "webhook policy",
106 "links": [
107 {
108 "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/",
109 "rel": "self"
110 }
111 ],
112 "changePercent": 3.3,
113 "cooldown": 300,
114 "type": "webhook",
115 "id": "2b48d247-0282-4b9d-8775-5c4b67e8e649"
116 }
117}
118`
119
Brad Isonac037d52016-04-07 19:41:29 -0400120// PolicyUpdateRequest contains the canned body of a policies.Update request.
121const PolicyUpdateRequest = `
122{
123 "name": "updated webhook policy",
124 "type": "webhook",
125 "cooldown": 600,
126 "changePercent": 6.6
127}
128`
129
Brad Ison53e997c2016-03-26 18:02:05 -0400130var (
131 // WebhookPolicy is a Policy corresponding to the first result in PolicyListBody.
132 WebhookPolicy = Policy{
Brad Ison2b739162016-04-11 12:40:11 -0400133 ID: "2b48d247-0282-4b9d-8775-5c4b67e8e649",
134 Name: "webhook policy",
135 Type: Webhook,
136 Cooldown: 300,
137 AdjustmentType: ChangePercent,
138 AdjustmentValue: 3.3,
Brad Ison53e997c2016-03-26 18:02:05 -0400139 }
140
141 // OneTimePolicy is a Policy corresponding to the second result in PolicyListBody.
142 OneTimePolicy = Policy{
Brad Ison2b739162016-04-11 12:40:11 -0400143 ID: "c175c31e-65f9-41de-8b15-918420d3253e",
144 Name: "one time",
145 Type: Schedule,
146 AdjustmentType: Change,
147 AdjustmentValue: float64(-1),
Brad Ison53e997c2016-03-26 18:02:05 -0400148 Args: map[string]interface{}{
149 "at": "2020-04-01T23:00:00.000Z",
150 },
151 }
152
153 // SundayAfternoonPolicy is a Policy corresponding to the third result in PolicyListBody.
154 SundayAfternoonPolicy = Policy{
Brad Isone7d6dfc2016-04-06 14:55:07 -0400155 ID: "e785e3e7-af9e-4f3c-99ae-b80a532e1663",
156 Name: "sunday afternoon",
157 Type: Schedule,
Brad Ison2b739162016-04-11 12:40:11 -0400158 AdjustmentType: DesiredCapacity,
159 AdjustmentValue: float64(2),
Brad Ison53e997c2016-03-26 18:02:05 -0400160 Args: map[string]interface{}{
161 "cron": "59 15 * * 0",
162 },
163 }
164)
165
166// HandlePolicyListSuccessfully sets up the test server to respond to a policies List request.
167func HandlePolicyListSuccessfully(t *testing.T) {
Brad Ison55523e52016-04-06 19:25:20 -0400168 path := "/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies"
Brad Ison53e997c2016-03-26 18:02:05 -0400169
170 th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
171 th.TestMethod(t, r, "GET")
172 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
173
174 w.Header().Add("Content-Type", "application/json")
175
176 fmt.Fprintf(w, PolicyListBody)
177 })
178}
Brad Isone7d6dfc2016-04-06 14:55:07 -0400179
180// HandlePolicyCreateSuccessfully sets up the test server to respond to a policies Create request.
181func HandlePolicyCreateSuccessfully(t *testing.T) {
Brad Ison55523e52016-04-06 19:25:20 -0400182 path := "/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies"
Brad Isone7d6dfc2016-04-06 14:55:07 -0400183
184 th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
185 th.TestMethod(t, r, "POST")
186 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
187 th.TestHeader(t, r, "Content-Type", "application/json")
188 th.TestHeader(t, r, "Accept", "application/json")
189
190 th.TestJSONRequest(t, r, PolicyCreateRequest)
191
192 w.Header().Add("Content-Type", "application/json")
193 w.WriteHeader(http.StatusCreated)
194
195 fmt.Fprintf(w, PolicyCreateBody)
196 })
197}
Brad Ison55523e52016-04-06 19:25:20 -0400198
199// HandlePolicyGetSuccessfully sets up the test server to respond to a policies Get request.
200func HandlePolicyGetSuccessfully(t *testing.T) {
201 groupID := "60b15dad-5ea1-43fa-9a12-a1d737b4da07"
202 policyID := "2b48d247-0282-4b9d-8775-5c4b67e8e649"
203
204 path := fmt.Sprintf("/groups/%s/policies/%s", groupID, policyID)
205
206 th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
207 th.TestMethod(t, r, "GET")
208 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
209
210 w.Header().Add("Content-Type", "application/json")
211
212 fmt.Fprintf(w, PolicyGetBody)
213 })
214}
Brad Isonac037d52016-04-07 19:41:29 -0400215
216// HandlePolicyUpdateSuccessfully sets up the test server to respond to a policies Update request.
217func HandlePolicyUpdateSuccessfully(t *testing.T) {
218 groupID := "60b15dad-5ea1-43fa-9a12-a1d737b4da07"
219 policyID := "2b48d247-0282-4b9d-8775-5c4b67e8e649"
220
221 path := fmt.Sprintf("/groups/%s/policies/%s", groupID, policyID)
222
223 th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
224 th.TestMethod(t, r, "PUT")
225 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
226
227 th.TestJSONRequest(t, r, PolicyUpdateRequest)
228
229 w.WriteHeader(http.StatusNoContent)
230 })
231}
Brad Ison124df8e2016-04-07 19:51:51 -0400232
233// HandlePolicyDeleteSuccessfully sets up the test server to respond to a policies Delete request.
234func HandlePolicyDeleteSuccessfully(t *testing.T) {
235 groupID := "60b15dad-5ea1-43fa-9a12-a1d737b4da07"
236 policyID := "2b48d247-0282-4b9d-8775-5c4b67e8e649"
237
238 path := fmt.Sprintf("/groups/%s/policies/%s", groupID, policyID)
239
240 th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
241 th.TestMethod(t, r, "DELETE")
242 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
243
244 w.WriteHeader(http.StatusNoContent)
245 })
246}
Brad Ison42f8dfb2016-04-07 20:26:06 -0400247
248// HandlePolicyExecuteSuccessfully sets up the test server to respond to a policies Execute request.
249func HandlePolicyExecuteSuccessfully(t *testing.T) {
250 groupID := "60b15dad-5ea1-43fa-9a12-a1d737b4da07"
251 policyID := "2b48d247-0282-4b9d-8775-5c4b67e8e649"
252
253 path := fmt.Sprintf("/groups/%s/policies/%s/execute", groupID, policyID)
254
255 th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
256 th.TestMethod(t, r, "POST")
257 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
258
259 w.WriteHeader(http.StatusAccepted)
260 fmt.Fprintf(w, "{}")
261 })
262}