blob: ac54268bbd4e449379af1bf73c5f74a81681e60c [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{
133 ID: "2b48d247-0282-4b9d-8775-5c4b67e8e649",
134 Name: "webhook policy",
135 Type: Webhook,
136 Cooldown: 300,
Brad Isone7d6dfc2016-04-06 14:55:07 -0400137 ChangePercent: 3.3,
Brad Ison53e997c2016-03-26 18:02:05 -0400138 }
139
140 // OneTimePolicy is a Policy corresponding to the second result in PolicyListBody.
141 OneTimePolicy = Policy{
142 ID: "c175c31e-65f9-41de-8b15-918420d3253e",
143 Name: "one time",
144 Type: Schedule,
Brad Isone7d6dfc2016-04-06 14:55:07 -0400145 Change: float64(-1),
Brad Ison53e997c2016-03-26 18:02:05 -0400146 Args: map[string]interface{}{
147 "at": "2020-04-01T23:00:00.000Z",
148 },
149 }
150
151 // SundayAfternoonPolicy is a Policy corresponding to the third result in PolicyListBody.
152 SundayAfternoonPolicy = Policy{
Brad Isone7d6dfc2016-04-06 14:55:07 -0400153 ID: "e785e3e7-af9e-4f3c-99ae-b80a532e1663",
154 Name: "sunday afternoon",
155 Type: Schedule,
156 DesiredCapacity: float64(2),
Brad Ison53e997c2016-03-26 18:02:05 -0400157 Args: map[string]interface{}{
158 "cron": "59 15 * * 0",
159 },
160 }
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}