blob: 442a891c61d4df9b2e7dc9119f3112904475a497 [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 Ison53e997c2016-03-26 18:02:05 -0400101var (
102 // WebhookPolicy is a Policy corresponding to the first result in PolicyListBody.
103 WebhookPolicy = Policy{
104 ID: "2b48d247-0282-4b9d-8775-5c4b67e8e649",
105 Name: "webhook policy",
106 Type: Webhook,
107 Cooldown: 300,
Brad Isone7d6dfc2016-04-06 14:55:07 -0400108 ChangePercent: 3.3,
Brad Ison53e997c2016-03-26 18:02:05 -0400109 }
110
111 // OneTimePolicy is a Policy corresponding to the second result in PolicyListBody.
112 OneTimePolicy = Policy{
113 ID: "c175c31e-65f9-41de-8b15-918420d3253e",
114 Name: "one time",
115 Type: Schedule,
Brad Isone7d6dfc2016-04-06 14:55:07 -0400116 Change: float64(-1),
Brad Ison53e997c2016-03-26 18:02:05 -0400117 Args: map[string]interface{}{
118 "at": "2020-04-01T23:00:00.000Z",
119 },
120 }
121
122 // SundayAfternoonPolicy is a Policy corresponding to the third result in PolicyListBody.
123 SundayAfternoonPolicy = Policy{
Brad Isone7d6dfc2016-04-06 14:55:07 -0400124 ID: "e785e3e7-af9e-4f3c-99ae-b80a532e1663",
125 Name: "sunday afternoon",
126 Type: Schedule,
127 DesiredCapacity: float64(2),
Brad Ison53e997c2016-03-26 18:02:05 -0400128 Args: map[string]interface{}{
129 "cron": "59 15 * * 0",
130 },
131 }
132)
133
134// HandlePolicyListSuccessfully sets up the test server to respond to a policies List request.
135func HandlePolicyListSuccessfully(t *testing.T) {
136 path := "/groups/10eb3219-1b12-4b34-b1e4-e10ee4f24c65/policies"
137
138 th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
139 th.TestMethod(t, r, "GET")
140 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
141
142 w.Header().Add("Content-Type", "application/json")
143
144 fmt.Fprintf(w, PolicyListBody)
145 })
146}
Brad Isone7d6dfc2016-04-06 14:55:07 -0400147
148// HandlePolicyCreateSuccessfully sets up the test server to respond to a policies Create request.
149func HandlePolicyCreateSuccessfully(t *testing.T) {
150 path := "/groups/10eb3219-1b12-4b34-b1e4-e10ee4f24c65/policies"
151
152 th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
153 th.TestMethod(t, r, "POST")
154 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
155 th.TestHeader(t, r, "Content-Type", "application/json")
156 th.TestHeader(t, r, "Accept", "application/json")
157
158 th.TestJSONRequest(t, r, PolicyCreateRequest)
159
160 w.Header().Add("Content-Type", "application/json")
161 w.WriteHeader(http.StatusCreated)
162
163 fmt.Fprintf(w, PolicyCreateBody)
164 })
165}