jrperritt | c5c590a | 2016-11-04 14:41:15 -0500 | [diff] [blame] | 1 | package testing |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | th "github.com/gophercloud/gophercloud/testhelper" |
| 11 | fakeclient "github.com/gophercloud/gophercloud/testhelper/client" |
| 12 | ) |
| 13 | |
| 14 | type imageEntry struct { |
| 15 | ID string |
| 16 | JSON string |
| 17 | } |
| 18 | |
| 19 | // HandleImageListSuccessfully test setup |
| 20 | func HandleImageListSuccessfully(t *testing.T) { |
| 21 | |
| 22 | images := make([]imageEntry, 3) |
| 23 | |
| 24 | images[0] = imageEntry{"cirros-0.3.4-x86_64-uec", |
| 25 | `{ |
| 26 | "status": "active", |
| 27 | "name": "cirros-0.3.4-x86_64-uec", |
| 28 | "tags": [], |
| 29 | "kernel_id": "e1b6edd4-bd9b-40ac-b010-8a6c16de4ba4", |
| 30 | "container_format": "ami", |
| 31 | "created_at": "2015-07-15T11:43:35Z", |
| 32 | "ramdisk_id": "8c64f48a-45a3-4eaa-adff-a8106b6c005b", |
| 33 | "disk_format": "ami", |
| 34 | "updated_at": "2015-07-15T11:43:35Z", |
| 35 | "visibility": "public", |
| 36 | "self": "/v2/images/07aa21a9-fa1a-430e-9a33-185be5982431", |
| 37 | "min_disk": 0, |
| 38 | "protected": false, |
| 39 | "id": "07aa21a9-fa1a-430e-9a33-185be5982431", |
| 40 | "size": 25165824, |
| 41 | "file": "/v2/images/07aa21a9-fa1a-430e-9a33-185be5982431/file", |
| 42 | "checksum": "eb9139e4942121f22bbc2afc0400b2a4", |
| 43 | "owner": "cba624273b8344e59dd1fd18685183b0", |
| 44 | "virtual_size": null, |
| 45 | "min_ram": 0, |
| 46 | "schema": "/v2/schemas/image" |
| 47 | }`} |
| 48 | images[1] = imageEntry{"cirros-0.3.4-x86_64-uec-ramdisk", |
| 49 | `{ |
| 50 | "status": "active", |
| 51 | "name": "cirros-0.3.4-x86_64-uec-ramdisk", |
| 52 | "tags": [], |
| 53 | "container_format": "ari", |
| 54 | "created_at": "2015-07-15T11:43:32Z", |
| 55 | "size": 3740163, |
| 56 | "disk_format": "ari", |
| 57 | "updated_at": "2015-07-15T11:43:32Z", |
| 58 | "visibility": "public", |
| 59 | "self": "/v2/images/8c64f48a-45a3-4eaa-adff-a8106b6c005b", |
| 60 | "min_disk": 0, |
| 61 | "protected": false, |
| 62 | "id": "8c64f48a-45a3-4eaa-adff-a8106b6c005b", |
| 63 | "file": "/v2/images/8c64f48a-45a3-4eaa-adff-a8106b6c005b/file", |
| 64 | "checksum": "be575a2b939972276ef675752936977f", |
| 65 | "owner": "cba624273b8344e59dd1fd18685183b0", |
| 66 | "virtual_size": null, |
| 67 | "min_ram": 0, |
| 68 | "schema": "/v2/schemas/image" |
| 69 | }`} |
| 70 | images[2] = imageEntry{"cirros-0.3.4-x86_64-uec-kernel", |
| 71 | `{ |
| 72 | "status": "active", |
| 73 | "name": "cirros-0.3.4-x86_64-uec-kernel", |
| 74 | "tags": [], |
| 75 | "container_format": "aki", |
| 76 | "created_at": "2015-07-15T11:43:29Z", |
| 77 | "size": 4979632, |
| 78 | "disk_format": "aki", |
| 79 | "updated_at": "2015-07-15T11:43:30Z", |
| 80 | "visibility": "public", |
| 81 | "self": "/v2/images/e1b6edd4-bd9b-40ac-b010-8a6c16de4ba4", |
| 82 | "min_disk": 0, |
| 83 | "protected": false, |
| 84 | "id": "e1b6edd4-bd9b-40ac-b010-8a6c16de4ba4", |
| 85 | "file": "/v2/images/e1b6edd4-bd9b-40ac-b010-8a6c16de4ba4/file", |
| 86 | "checksum": "8a40c862b5735975d82605c1dd395796", |
| 87 | "owner": "cba624273b8344e59dd1fd18685183b0", |
| 88 | "virtual_size": null, |
| 89 | "min_ram": 0, |
| 90 | "schema": "/v2/schemas/image" |
| 91 | }`} |
| 92 | |
| 93 | th.Mux.HandleFunc("/images", func(w http.ResponseWriter, r *http.Request) { |
| 94 | th.TestMethod(t, r, "GET") |
| 95 | th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID) |
| 96 | |
| 97 | w.Header().Add("Content-Type", "application/json") |
| 98 | |
| 99 | w.WriteHeader(http.StatusOK) |
| 100 | |
| 101 | limit := 10 |
| 102 | var err error |
| 103 | if r.FormValue("limit") != "" { |
| 104 | limit, err = strconv.Atoi(r.FormValue("limit")) |
| 105 | if err != nil { |
| 106 | t.Errorf("Error value for 'limit' parameter %v (error: %v)", r.FormValue("limit"), err) |
| 107 | } |
| 108 | |
| 109 | } |
| 110 | |
| 111 | marker := "" |
| 112 | newMarker := "" |
| 113 | |
| 114 | if r.Form["marker"] != nil { |
| 115 | marker = r.Form["marker"][0] |
| 116 | } |
| 117 | |
| 118 | t.Logf("limit = %v marker = %v", limit, marker) |
| 119 | |
| 120 | selected := 0 |
| 121 | addNext := false |
| 122 | var imageJSON []string |
| 123 | |
| 124 | fmt.Fprintf(w, `{"images": [`) |
| 125 | |
| 126 | for _, i := range images { |
| 127 | if marker == "" || addNext { |
| 128 | t.Logf("Adding image %v to page", i.ID) |
| 129 | imageJSON = append(imageJSON, i.JSON) |
| 130 | newMarker = i.ID |
| 131 | selected++ |
| 132 | } else { |
| 133 | if strings.Contains(i.JSON, marker) { |
| 134 | addNext = true |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | if selected == limit { |
| 139 | break |
| 140 | } |
| 141 | } |
| 142 | t.Logf("Writing out %v image(s)", len(imageJSON)) |
| 143 | fmt.Fprintf(w, strings.Join(imageJSON, ",")) |
| 144 | |
| 145 | fmt.Fprintf(w, `], |
| 146 | "next": "/images?marker=%s&limit=%v", |
| 147 | "schema": "/schemas/images", |
| 148 | "first": "/images?limit=%v"}`, newMarker, limit, limit) |
| 149 | |
| 150 | }) |
| 151 | } |
| 152 | |
| 153 | // HandleImageCreationSuccessfully test setup |
| 154 | func HandleImageCreationSuccessfully(t *testing.T) { |
| 155 | th.Mux.HandleFunc("/images", func(w http.ResponseWriter, r *http.Request) { |
| 156 | th.TestMethod(t, r, "POST") |
| 157 | th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID) |
| 158 | th.TestJSONRequest(t, r, `{ |
| 159 | "id": "e7db3b45-8db7-47ad-8109-3fb55c2c24fd", |
| 160 | "name": "Ubuntu 12.10", |
| 161 | "tags": [ |
| 162 | "ubuntu", |
| 163 | "quantal" |
| 164 | ] |
| 165 | }`) |
| 166 | |
| 167 | w.WriteHeader(http.StatusCreated) |
| 168 | w.Header().Add("Content-Type", "application/json") |
| 169 | fmt.Fprintf(w, `{ |
| 170 | "status": "queued", |
| 171 | "name": "Ubuntu 12.10", |
| 172 | "protected": false, |
| 173 | "tags": ["ubuntu","quantal"], |
| 174 | "container_format": "bare", |
| 175 | "created_at": "2014-11-11T20:47:55Z", |
| 176 | "disk_format": "qcow2", |
| 177 | "updated_at": "2014-11-11T20:47:55Z", |
| 178 | "visibility": "private", |
| 179 | "self": "/v2/images/e7db3b45-8db7-47ad-8109-3fb55c2c24fd", |
| 180 | "min_disk": 0, |
| 181 | "protected": false, |
| 182 | "id": "e7db3b45-8db7-47ad-8109-3fb55c2c24fd", |
| 183 | "file": "/v2/images/e7db3b45-8db7-47ad-8109-3fb55c2c24fd/file", |
| 184 | "owner": "b4eedccc6fb74fa8a7ad6b08382b852b", |
| 185 | "min_ram": 0, |
| 186 | "schema": "/v2/schemas/image", |
| 187 | "size": 0, |
| 188 | "checksum": "", |
| 189 | "virtual_size": 0 |
| 190 | }`) |
| 191 | }) |
| 192 | } |
| 193 | |
| 194 | // HandleImageCreationSuccessfullyNulls test setup |
| 195 | // JSON null values could be also returned according to behaviour https://bugs.launchpad.net/glance/+bug/1481512 |
| 196 | func HandleImageCreationSuccessfullyNulls(t *testing.T) { |
| 197 | th.Mux.HandleFunc("/images", func(w http.ResponseWriter, r *http.Request) { |
| 198 | th.TestMethod(t, r, "POST") |
| 199 | th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID) |
| 200 | th.TestJSONRequest(t, r, `{ |
| 201 | "id": "e7db3b45-8db7-47ad-8109-3fb55c2c24fd", |
| 202 | "name": "Ubuntu 12.10", |
| 203 | "tags": [ |
| 204 | "ubuntu", |
| 205 | "quantal" |
| 206 | ] |
| 207 | }`) |
| 208 | |
| 209 | w.WriteHeader(http.StatusCreated) |
| 210 | w.Header().Add("Content-Type", "application/json") |
| 211 | fmt.Fprintf(w, `{ |
| 212 | "status": "queued", |
| 213 | "name": "Ubuntu 12.10", |
| 214 | "protected": false, |
| 215 | "tags": ["ubuntu","quantal"], |
| 216 | "container_format": "bare", |
| 217 | "created_at": "2014-11-11T20:47:55Z", |
| 218 | "disk_format": "qcow2", |
| 219 | "updated_at": "2014-11-11T20:47:55Z", |
| 220 | "visibility": "private", |
| 221 | "self": "/v2/images/e7db3b45-8db7-47ad-8109-3fb55c2c24fd", |
| 222 | "min_disk": 0, |
| 223 | "protected": false, |
| 224 | "id": "e7db3b45-8db7-47ad-8109-3fb55c2c24fd", |
| 225 | "file": "/v2/images/e7db3b45-8db7-47ad-8109-3fb55c2c24fd/file", |
| 226 | "owner": "b4eedccc6fb74fa8a7ad6b08382b852b", |
| 227 | "min_ram": 0, |
| 228 | "schema": "/v2/schemas/image", |
| 229 | "size": null, |
| 230 | "checksum": null, |
| 231 | "virtual_size": null |
| 232 | }`) |
| 233 | }) |
| 234 | } |
| 235 | |
| 236 | // HandleImageGetSuccessfully test setup |
| 237 | func HandleImageGetSuccessfully(t *testing.T) { |
| 238 | th.Mux.HandleFunc("/images/1bea47ed-f6a9-463b-b423-14b9cca9ad27", func(w http.ResponseWriter, r *http.Request) { |
| 239 | th.TestMethod(t, r, "GET") |
| 240 | th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID) |
| 241 | |
| 242 | w.WriteHeader(http.StatusOK) |
| 243 | w.Header().Add("Content-Type", "application/json") |
| 244 | fmt.Fprintf(w, `{ |
| 245 | "status": "active", |
| 246 | "name": "cirros-0.3.2-x86_64-disk", |
| 247 | "tags": [], |
| 248 | "container_format": "bare", |
| 249 | "created_at": "2014-05-05T17:15:10Z", |
| 250 | "disk_format": "qcow2", |
| 251 | "updated_at": "2014-05-05T17:15:11Z", |
| 252 | "visibility": "public", |
| 253 | "self": "/v2/images/1bea47ed-f6a9-463b-b423-14b9cca9ad27", |
| 254 | "min_disk": 0, |
| 255 | "protected": false, |
| 256 | "id": "1bea47ed-f6a9-463b-b423-14b9cca9ad27", |
| 257 | "file": "/v2/images/1bea47ed-f6a9-463b-b423-14b9cca9ad27/file", |
| 258 | "checksum": "64d7c1cd2b6f60c92c14662941cb7913", |
| 259 | "owner": "5ef70662f8b34079a6eddb8da9d75fe8", |
| 260 | "size": 13167616, |
| 261 | "min_ram": 0, |
| 262 | "schema": "/v2/schemas/image", |
| 263 | "virtual_size": "None" |
| 264 | }`) |
| 265 | }) |
| 266 | } |
| 267 | |
| 268 | // HandleImageDeleteSuccessfully test setup |
| 269 | func HandleImageDeleteSuccessfully(t *testing.T) { |
| 270 | th.Mux.HandleFunc("/images/1bea47ed-f6a9-463b-b423-14b9cca9ad27", func(w http.ResponseWriter, r *http.Request) { |
| 271 | th.TestMethod(t, r, "DELETE") |
| 272 | th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID) |
| 273 | |
| 274 | w.WriteHeader(http.StatusNoContent) |
| 275 | }) |
| 276 | } |
| 277 | |
| 278 | // HandleImageUpdateSuccessfully setup |
| 279 | func HandleImageUpdateSuccessfully(t *testing.T) { |
| 280 | th.Mux.HandleFunc("/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea", func(w http.ResponseWriter, r *http.Request) { |
| 281 | th.TestMethod(t, r, "PATCH") |
| 282 | th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID) |
| 283 | |
| 284 | th.TestJSONRequest(t, r, `[ |
| 285 | { |
| 286 | "op": "replace", |
| 287 | "path": "/name", |
| 288 | "value": "Fedora 17" |
| 289 | }, |
| 290 | { |
| 291 | "op": "replace", |
| 292 | "path": "/tags", |
| 293 | "value": [ |
| 294 | "fedora", |
| 295 | "beefy" |
| 296 | ] |
| 297 | } |
| 298 | ]`) |
| 299 | |
| 300 | th.AssertEquals(t, "application/openstack-images-v2.1-json-patch", r.Header.Get("Content-Type")) |
| 301 | |
| 302 | w.WriteHeader(http.StatusOK) |
| 303 | w.Header().Add("Content-Type", "application/json") |
| 304 | fmt.Fprintf(w, `{ |
| 305 | "id": "da3b75d9-3f4a-40e7-8a2c-bfab23927dea", |
| 306 | "name": "Fedora 17", |
| 307 | "status": "active", |
| 308 | "visibility": "public", |
| 309 | "size": 2254249, |
| 310 | "checksum": "2cec138d7dae2aa59038ef8c9aec2390", |
| 311 | "tags": [ |
| 312 | "fedora", |
| 313 | "beefy" |
| 314 | ], |
| 315 | "created_at": "2012-08-10T19:23:50Z", |
| 316 | "updated_at": "2012-08-12T11:11:33Z", |
| 317 | "self": "/v2/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea", |
| 318 | "file": "/v2/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea/file", |
| 319 | "schema": "/v2/schemas/image", |
| 320 | "owner": "", |
| 321 | "min_ram": 0, |
| 322 | "min_disk": 0, |
| 323 | "disk_format": "", |
| 324 | "virtual_size": 0, |
| 325 | "container_format": "" |
| 326 | }`) |
| 327 | }) |
| 328 | } |