blob: 587c5e72886ccee723e9da6d584af8b3fb3ee76f [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 Ison53e997c2016-03-26 18:02:05 -0400120var (
121 // WebhookPolicy is a Policy corresponding to the first result in PolicyListBody.
122 WebhookPolicy = Policy{
123 ID: "2b48d247-0282-4b9d-8775-5c4b67e8e649",
124 Name: "webhook policy",
125 Type: Webhook,
126 Cooldown: 300,
Brad Isone7d6dfc2016-04-06 14:55:07 -0400127 ChangePercent: 3.3,
Brad Ison53e997c2016-03-26 18:02:05 -0400128 }
129
130 // OneTimePolicy is a Policy corresponding to the second result in PolicyListBody.
131 OneTimePolicy = Policy{
132 ID: "c175c31e-65f9-41de-8b15-918420d3253e",
133 Name: "one time",
134 Type: Schedule,
Brad Isone7d6dfc2016-04-06 14:55:07 -0400135 Change: float64(-1),
Brad Ison53e997c2016-03-26 18:02:05 -0400136 Args: map[string]interface{}{
137 "at": "2020-04-01T23:00:00.000Z",
138 },
139 }
140
141 // SundayAfternoonPolicy is a Policy corresponding to the third result in PolicyListBody.
142 SundayAfternoonPolicy = Policy{
Brad Isone7d6dfc2016-04-06 14:55:07 -0400143 ID: "e785e3e7-af9e-4f3c-99ae-b80a532e1663",
144 Name: "sunday afternoon",
145 Type: Schedule,
146 DesiredCapacity: float64(2),
Brad Ison53e997c2016-03-26 18:02:05 -0400147 Args: map[string]interface{}{
148 "cron": "59 15 * * 0",
149 },
150 }
151)
152
153// HandlePolicyListSuccessfully sets up the test server to respond to a policies List request.
154func HandlePolicyListSuccessfully(t *testing.T) {
Brad Ison55523e52016-04-06 19:25:20 -0400155 path := "/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies"
Brad Ison53e997c2016-03-26 18:02:05 -0400156
157 th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
158 th.TestMethod(t, r, "GET")
159 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
160
161 w.Header().Add("Content-Type", "application/json")
162
163 fmt.Fprintf(w, PolicyListBody)
164 })
165}
Brad Isone7d6dfc2016-04-06 14:55:07 -0400166
167// HandlePolicyCreateSuccessfully sets up the test server to respond to a policies Create request.
168func HandlePolicyCreateSuccessfully(t *testing.T) {
Brad Ison55523e52016-04-06 19:25:20 -0400169 path := "/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies"
Brad Isone7d6dfc2016-04-06 14:55:07 -0400170
171 th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
172 th.TestMethod(t, r, "POST")
173 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
174 th.TestHeader(t, r, "Content-Type", "application/json")
175 th.TestHeader(t, r, "Accept", "application/json")
176
177 th.TestJSONRequest(t, r, PolicyCreateRequest)
178
179 w.Header().Add("Content-Type", "application/json")
180 w.WriteHeader(http.StatusCreated)
181
182 fmt.Fprintf(w, PolicyCreateBody)
183 })
184}
Brad Ison55523e52016-04-06 19:25:20 -0400185
186// HandlePolicyGetSuccessfully sets up the test server to respond to a policies Get request.
187func HandlePolicyGetSuccessfully(t *testing.T) {
188 groupID := "60b15dad-5ea1-43fa-9a12-a1d737b4da07"
189 policyID := "2b48d247-0282-4b9d-8775-5c4b67e8e649"
190
191 path := fmt.Sprintf("/groups/%s/policies/%s", groupID, policyID)
192
193 th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
194 th.TestMethod(t, r, "GET")
195 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
196
197 w.Header().Add("Content-Type", "application/json")
198
199 fmt.Fprintf(w, PolicyGetBody)
200 })
201}