Brad Ison | 53e997c | 2016-03-26 18:02:05 -0400 | [diff] [blame] | 1 | // +build fixtures |
| 2 | |
| 3 | package policies |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | "testing" |
Brad Ison | a06e2ca | 2016-04-11 14:59:06 -0400 | [diff] [blame] | 9 | "time" |
Brad Ison | 53e997c | 2016-03-26 18:02:05 -0400 | [diff] [blame] | 10 | |
| 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. |
| 16 | const 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 Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame] | 28 | "changePercent": 3.3, |
Brad Ison | 53e997c | 2016-03-26 18:02:05 -0400 | [diff] [blame] | 29 | "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 Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame] | 63 | "desiredCapacity": 2 |
Brad Ison | 53e997c | 2016-03-26 18:02:05 -0400 | [diff] [blame] | 64 | } |
| 65 | ] |
| 66 | } |
| 67 | ` |
| 68 | |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame] | 69 | // PolicyCreateBody contains the canned body of a policies.Create response. |
| 70 | const PolicyCreateBody = PolicyListBody |
| 71 | |
| 72 | // PolicyCreateRequest contains the canned body of a policies.Create request. |
| 73 | const 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 Ison | a06e2ca | 2016-04-11 14:59:06 -0400 | [diff] [blame] | 85 | "at": "2020-04-01T23:00:00Z" |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame] | 86 | }, |
| 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 Ison | 55523e5 | 2016-04-06 19:25:20 -0400 | [diff] [blame] | 102 | // PolicyGetBody contains the canned body of a policies.Get response. |
| 103 | const 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 Ison | ac037d5 | 2016-04-07 19:41:29 -0400 | [diff] [blame] | 121 | // PolicyUpdateRequest contains the canned body of a policies.Update request. |
| 122 | const PolicyUpdateRequest = ` |
| 123 | { |
| 124 | "name": "updated webhook policy", |
| 125 | "type": "webhook", |
| 126 | "cooldown": 600, |
| 127 | "changePercent": 6.6 |
| 128 | } |
| 129 | ` |
| 130 | |
Brad Ison | 53e997c | 2016-03-26 18:02:05 -0400 | [diff] [blame] | 131 | var ( |
| 132 | // WebhookPolicy is a Policy corresponding to the first result in PolicyListBody. |
| 133 | WebhookPolicy = Policy{ |
Brad Ison | 2b73916 | 2016-04-11 12:40:11 -0400 | [diff] [blame] | 134 | ID: "2b48d247-0282-4b9d-8775-5c4b67e8e649", |
| 135 | Name: "webhook policy", |
| 136 | Type: Webhook, |
| 137 | Cooldown: 300, |
| 138 | AdjustmentType: ChangePercent, |
| 139 | AdjustmentValue: 3.3, |
Brad Ison | 53e997c | 2016-03-26 18:02:05 -0400 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | // OneTimePolicy is a Policy corresponding to the second result in PolicyListBody. |
| 143 | OneTimePolicy = Policy{ |
Brad Ison | 2b73916 | 2016-04-11 12:40:11 -0400 | [diff] [blame] | 144 | ID: "c175c31e-65f9-41de-8b15-918420d3253e", |
| 145 | Name: "one time", |
| 146 | Type: Schedule, |
| 147 | AdjustmentType: Change, |
| 148 | AdjustmentValue: float64(-1), |
Brad Ison | a06e2ca | 2016-04-11 14:59:06 -0400 | [diff] [blame] | 149 | Schedule: At(time.Date(2020, time.April, 01, 23, 0, 0, 0, time.UTC)), |
Brad Ison | 53e997c | 2016-03-26 18:02:05 -0400 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | // SundayAfternoonPolicy is a Policy corresponding to the third result in PolicyListBody. |
| 153 | SundayAfternoonPolicy = Policy{ |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame] | 154 | ID: "e785e3e7-af9e-4f3c-99ae-b80a532e1663", |
| 155 | Name: "sunday afternoon", |
| 156 | Type: Schedule, |
Brad Ison | 2b73916 | 2016-04-11 12:40:11 -0400 | [diff] [blame] | 157 | AdjustmentType: DesiredCapacity, |
| 158 | AdjustmentValue: float64(2), |
Brad Ison | a06e2ca | 2016-04-11 14:59:06 -0400 | [diff] [blame] | 159 | Schedule: Cron("59 15 * * 0"), |
Brad Ison | 53e997c | 2016-03-26 18:02:05 -0400 | [diff] [blame] | 160 | } |
| 161 | ) |
| 162 | |
| 163 | // HandlePolicyListSuccessfully sets up the test server to respond to a policies List request. |
| 164 | func HandlePolicyListSuccessfully(t *testing.T) { |
Brad Ison | 55523e5 | 2016-04-06 19:25:20 -0400 | [diff] [blame] | 165 | path := "/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies" |
Brad Ison | 53e997c | 2016-03-26 18:02:05 -0400 | [diff] [blame] | 166 | |
| 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 Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame] | 176 | |
| 177 | // HandlePolicyCreateSuccessfully sets up the test server to respond to a policies Create request. |
| 178 | func HandlePolicyCreateSuccessfully(t *testing.T) { |
Brad Ison | 55523e5 | 2016-04-06 19:25:20 -0400 | [diff] [blame] | 179 | path := "/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies" |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame] | 180 | |
| 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 Ison | 55523e5 | 2016-04-06 19:25:20 -0400 | [diff] [blame] | 195 | |
| 196 | // HandlePolicyGetSuccessfully sets up the test server to respond to a policies Get request. |
| 197 | func 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 Ison | ac037d5 | 2016-04-07 19:41:29 -0400 | [diff] [blame] | 212 | |
| 213 | // HandlePolicyUpdateSuccessfully sets up the test server to respond to a policies Update request. |
| 214 | func 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 Ison | 124df8e | 2016-04-07 19:51:51 -0400 | [diff] [blame] | 229 | |
| 230 | // HandlePolicyDeleteSuccessfully sets up the test server to respond to a policies Delete request. |
| 231 | func 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 Ison | 42f8dfb | 2016-04-07 20:26:06 -0400 | [diff] [blame] | 244 | |
| 245 | // HandlePolicyExecuteSuccessfully sets up the test server to respond to a policies Execute request. |
| 246 | func 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 | } |