github.com/gophercloud/gophercloud@v1.11.0/openstack/imageservice/v2/images/testing/fixtures_test.go (about) 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 "hw_disk_bus": "scsi", 48 "hw_disk_bus_model": "virtio-scsi", 49 "hw_scsi_model": "virtio-scsi" 50 }`} 51 images[1] = imageEntry{"cirros-0.3.4-x86_64-uec-ramdisk", 52 `{ 53 "status": "active", 54 "name": "cirros-0.3.4-x86_64-uec-ramdisk", 55 "tags": [], 56 "container_format": "ari", 57 "created_at": "2015-07-15T11:43:32Z", 58 "size": 3740163, 59 "disk_format": "ari", 60 "updated_at": "2015-07-15T11:43:32Z", 61 "visibility": "public", 62 "self": "/v2/images/8c64f48a-45a3-4eaa-adff-a8106b6c005b", 63 "min_disk": 0, 64 "protected": false, 65 "id": "8c64f48a-45a3-4eaa-adff-a8106b6c005b", 66 "file": "/v2/images/8c64f48a-45a3-4eaa-adff-a8106b6c005b/file", 67 "checksum": "be575a2b939972276ef675752936977f", 68 "owner": "cba624273b8344e59dd1fd18685183b0", 69 "virtual_size": null, 70 "min_ram": 0, 71 "schema": "/v2/schemas/image", 72 "hw_disk_bus": "scsi", 73 "hw_disk_bus_model": "virtio-scsi", 74 "hw_scsi_model": "virtio-scsi" 75 }`} 76 images[2] = imageEntry{"cirros-0.3.4-x86_64-uec-kernel", 77 `{ 78 "status": "active", 79 "name": "cirros-0.3.4-x86_64-uec-kernel", 80 "tags": [], 81 "container_format": "aki", 82 "created_at": "2015-07-15T11:43:29Z", 83 "size": 4979632, 84 "disk_format": "aki", 85 "updated_at": "2015-07-15T11:43:30Z", 86 "visibility": "public", 87 "self": "/v2/images/e1b6edd4-bd9b-40ac-b010-8a6c16de4ba4", 88 "min_disk": 0, 89 "protected": false, 90 "id": "e1b6edd4-bd9b-40ac-b010-8a6c16de4ba4", 91 "file": "/v2/images/e1b6edd4-bd9b-40ac-b010-8a6c16de4ba4/file", 92 "checksum": "8a40c862b5735975d82605c1dd395796", 93 "owner": "cba624273b8344e59dd1fd18685183b0", 94 "virtual_size": null, 95 "min_ram": 0, 96 "schema": "/v2/schemas/image", 97 "hw_disk_bus": "scsi", 98 "hw_disk_bus_model": "virtio-scsi", 99 "hw_scsi_model": "virtio-scsi" 100 }`} 101 102 th.Mux.HandleFunc("/images", func(w http.ResponseWriter, r *http.Request) { 103 th.TestMethod(t, r, "GET") 104 th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID) 105 106 w.Header().Add("Content-Type", "application/json") 107 108 w.WriteHeader(http.StatusOK) 109 110 limit := 10 111 var err error 112 if r.FormValue("limit") != "" { 113 limit, err = strconv.Atoi(r.FormValue("limit")) 114 if err != nil { 115 t.Errorf("Error value for 'limit' parameter %v (error: %v)", r.FormValue("limit"), err) 116 } 117 118 } 119 120 marker := "" 121 newMarker := "" 122 123 if r.Form["marker"] != nil { 124 marker = r.Form["marker"][0] 125 } 126 127 t.Logf("limit = %v marker = %v", limit, marker) 128 129 selected := 0 130 addNext := false 131 var imageJSON []string 132 133 fmt.Fprintf(w, `{"images": [`) 134 135 for _, i := range images { 136 if marker == "" || addNext { 137 t.Logf("Adding image %v to page", i.ID) 138 imageJSON = append(imageJSON, i.JSON) 139 newMarker = i.ID 140 selected++ 141 } else { 142 if strings.Contains(i.JSON, marker) { 143 addNext = true 144 } 145 } 146 147 if selected == limit { 148 break 149 } 150 } 151 t.Logf("Writing out %v image(s)", len(imageJSON)) 152 fmt.Fprintf(w, strings.Join(imageJSON, ",")) 153 154 fmt.Fprintf(w, `], 155 "next": "/images?marker=%s&limit=%v", 156 "schema": "/schemas/images", 157 "first": "/images?limit=%v"}`, newMarker, limit, limit) 158 159 }) 160 } 161 162 // HandleImageCreationSuccessfully test setup 163 func HandleImageCreationSuccessfully(t *testing.T) { 164 th.Mux.HandleFunc("/images", func(w http.ResponseWriter, r *http.Request) { 165 th.TestMethod(t, r, "POST") 166 th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID) 167 th.TestJSONRequest(t, r, `{ 168 "id": "e7db3b45-8db7-47ad-8109-3fb55c2c24fd", 169 "name": "Ubuntu 12.10", 170 "architecture": "x86_64", 171 "tags": [ 172 "ubuntu", 173 "quantal" 174 ] 175 }`) 176 177 w.Header().Add("Content-Type", "application/json") 178 w.WriteHeader(http.StatusCreated) 179 fmt.Fprintf(w, `{ 180 "status": "queued", 181 "name": "Ubuntu 12.10", 182 "protected": false, 183 "tags": ["ubuntu","quantal"], 184 "container_format": "bare", 185 "created_at": "2014-11-11T20:47:55Z", 186 "disk_format": "qcow2", 187 "updated_at": "2014-11-11T20:47:55Z", 188 "visibility": "private", 189 "self": "/v2/images/e7db3b45-8db7-47ad-8109-3fb55c2c24fd", 190 "min_disk": 0, 191 "protected": false, 192 "id": "e7db3b45-8db7-47ad-8109-3fb55c2c24fd", 193 "file": "/v2/images/e7db3b45-8db7-47ad-8109-3fb55c2c24fd/file", 194 "owner": "b4eedccc6fb74fa8a7ad6b08382b852b", 195 "min_ram": 0, 196 "schema": "/v2/schemas/image", 197 "size": 0, 198 "checksum": "", 199 "virtual_size": 0, 200 "hw_disk_bus": "scsi", 201 "hw_disk_bus_model": "virtio-scsi", 202 "hw_scsi_model": "virtio-scsi" 203 }`) 204 }) 205 } 206 207 // HandleImageCreationSuccessfullyNulls test setup 208 // JSON null values could be also returned according to behaviour https://bugs.launchpad.net/glance/+bug/1481512 209 func HandleImageCreationSuccessfullyNulls(t *testing.T) { 210 th.Mux.HandleFunc("/images", func(w http.ResponseWriter, r *http.Request) { 211 th.TestMethod(t, r, "POST") 212 th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID) 213 th.TestJSONRequest(t, r, `{ 214 "id": "e7db3b45-8db7-47ad-8109-3fb55c2c24fd", 215 "architecture": "x86_64", 216 "name": "Ubuntu 12.10", 217 "tags": [ 218 "ubuntu", 219 "quantal" 220 ] 221 }`) 222 223 w.Header().Set("Content-Type", "application/json") 224 w.Header().Set("OpenStack-image-import-methods", "glance-direct,web-download") 225 w.Header().Set("OpenStack-image-store-ids", "123,456") 226 w.WriteHeader(http.StatusCreated) 227 fmt.Fprintf(w, `{ 228 "architecture": "x86_64", 229 "status": "queued", 230 "name": "Ubuntu 12.10", 231 "protected": false, 232 "tags": ["ubuntu","quantal"], 233 "container_format": "bare", 234 "created_at": "2014-11-11T20:47:55Z", 235 "disk_format": "qcow2", 236 "updated_at": "2014-11-11T20:47:55Z", 237 "visibility": "private", 238 "self": "/v2/images/e7db3b45-8db7-47ad-8109-3fb55c2c24fd", 239 "min_disk": 0, 240 "protected": false, 241 "id": "e7db3b45-8db7-47ad-8109-3fb55c2c24fd", 242 "file": "/v2/images/e7db3b45-8db7-47ad-8109-3fb55c2c24fd/file", 243 "owner": "b4eedccc6fb74fa8a7ad6b08382b852b", 244 "min_ram": 0, 245 "schema": "/v2/schemas/image", 246 "size": null, 247 "checksum": null, 248 "virtual_size": null 249 }`) 250 }) 251 } 252 253 // HandleImageGetSuccessfully test setup 254 func HandleImageGetSuccessfully(t *testing.T) { 255 th.Mux.HandleFunc("/images/1bea47ed-f6a9-463b-b423-14b9cca9ad27", func(w http.ResponseWriter, r *http.Request) { 256 th.TestMethod(t, r, "GET") 257 th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID) 258 259 w.Header().Add("Content-Type", "application/json") 260 w.WriteHeader(http.StatusOK) 261 fmt.Fprintf(w, `{ 262 "status": "active", 263 "name": "cirros-0.3.2-x86_64-disk", 264 "tags": [], 265 "container_format": "bare", 266 "created_at": "2014-05-05T17:15:10Z", 267 "disk_format": "qcow2", 268 "updated_at": "2014-05-05T17:15:11Z", 269 "visibility": "public", 270 "os_hidden": false, 271 "self": "/v2/images/1bea47ed-f6a9-463b-b423-14b9cca9ad27", 272 "min_disk": 0, 273 "protected": false, 274 "id": "1bea47ed-f6a9-463b-b423-14b9cca9ad27", 275 "file": "/v2/images/1bea47ed-f6a9-463b-b423-14b9cca9ad27/file", 276 "checksum": "64d7c1cd2b6f60c92c14662941cb7913", 277 "owner": "5ef70662f8b34079a6eddb8da9d75fe8", 278 "size": 13167616, 279 "min_ram": 0, 280 "schema": "/v2/schemas/image", 281 "virtual_size": null, 282 "hw_disk_bus": "scsi", 283 "hw_disk_bus_model": "virtio-scsi", 284 "hw_scsi_model": "virtio-scsi" 285 }`) 286 }) 287 } 288 289 // HandleImageDeleteSuccessfully test setup 290 func HandleImageDeleteSuccessfully(t *testing.T) { 291 th.Mux.HandleFunc("/images/1bea47ed-f6a9-463b-b423-14b9cca9ad27", func(w http.ResponseWriter, r *http.Request) { 292 th.TestMethod(t, r, "DELETE") 293 th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID) 294 295 w.WriteHeader(http.StatusNoContent) 296 }) 297 } 298 299 // HandleImageUpdateSuccessfully setup 300 func HandleImageUpdateSuccessfully(t *testing.T) { 301 th.Mux.HandleFunc("/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea", func(w http.ResponseWriter, r *http.Request) { 302 th.TestMethod(t, r, "PATCH") 303 th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID) 304 305 th.TestJSONRequest(t, r, `[ 306 { 307 "op": "replace", 308 "path": "/name", 309 "value": "Fedora 17" 310 }, 311 { 312 "op": "replace", 313 "path": "/tags", 314 "value": [ 315 "fedora", 316 "beefy" 317 ] 318 }, 319 { 320 "op": "replace", 321 "path": "/min_disk", 322 "value": 21 323 }, 324 { 325 "op": "replace", 326 "path": "/min_ram", 327 "value": 1024 328 }, 329 { 330 "op": "replace", 331 "path": "/os_hidden", 332 "value": false 333 }, 334 { 335 "op": "replace", 336 "path": "/protected", 337 "value": true 338 }, 339 { 340 "op": "add", 341 "path": "/empty_value", 342 "value": "" 343 } 344 ]`) 345 346 th.AssertEquals(t, "application/openstack-images-v2.1-json-patch", r.Header.Get("Content-Type")) 347 348 w.Header().Add("Content-Type", "application/json") 349 w.WriteHeader(http.StatusOK) 350 fmt.Fprintf(w, `{ 351 "id": "da3b75d9-3f4a-40e7-8a2c-bfab23927dea", 352 "name": "Fedora 17", 353 "status": "active", 354 "visibility": "public", 355 "os_hidden": false, 356 "protected": true, 357 "size": 2254249, 358 "checksum": "2cec138d7dae2aa59038ef8c9aec2390", 359 "tags": [ 360 "fedora", 361 "beefy" 362 ], 363 "created_at": "2012-08-10T19:23:50Z", 364 "updated_at": "2012-08-12T11:11:33Z", 365 "self": "/v2/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea", 366 "file": "/v2/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea/file", 367 "schema": "/v2/schemas/image", 368 "owner": "", 369 "min_ram": 1024, 370 "min_disk": 21, 371 "disk_format": "", 372 "virtual_size": 0, 373 "container_format": "", 374 "empty_value": "", 375 "hw_disk_bus": "scsi", 376 "hw_disk_bus_model": "virtio-scsi", 377 "hw_scsi_model": "virtio-scsi" 378 }`) 379 }) 380 } 381 382 // HandleImageListByTagsSuccessfully tests a list operation with tags. 383 func HandleImageListByTagsSuccessfully(t *testing.T) { 384 th.Mux.HandleFunc("/images", func(w http.ResponseWriter, r *http.Request) { 385 th.TestMethod(t, r, "GET") 386 th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID) 387 388 w.Header().Add("Content-Type", "application/json") 389 390 w.WriteHeader(http.StatusOK) 391 392 fmt.Fprintf(w, `{ 393 "images": [ 394 { 395 "status": "active", 396 "name": "cirros-0.3.2-x86_64-disk", 397 "tags": ["foo", "bar"], 398 "container_format": "bare", 399 "created_at": "2014-05-05T17:15:10Z", 400 "disk_format": "qcow2", 401 "updated_at": "2014-05-05T17:15:11Z", 402 "visibility": "public", 403 "os_hidden": false, 404 "self": "/v2/images/1bea47ed-f6a9-463b-b423-14b9cca9ad27", 405 "min_disk": 0, 406 "protected": false, 407 "id": "1bea47ed-f6a9-463b-b423-14b9cca9ad27", 408 "file": "/v2/images/1bea47ed-f6a9-463b-b423-14b9cca9ad27/file", 409 "checksum": "64d7c1cd2b6f60c92c14662941cb7913", 410 "owner": "5ef70662f8b34079a6eddb8da9d75fe8", 411 "size": 13167616, 412 "min_ram": 0, 413 "schema": "/v2/schemas/image", 414 "virtual_size": null, 415 "hw_disk_bus": "scsi", 416 "hw_disk_bus_model": "virtio-scsi", 417 "hw_scsi_model": "virtio-scsi" 418 } 419 ] 420 }`) 421 }) 422 } 423 424 // HandleImageUpdatePropertiesSuccessfully setup 425 func HandleImageUpdatePropertiesSuccessfully(t *testing.T) { 426 th.Mux.HandleFunc("/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea", func(w http.ResponseWriter, r *http.Request) { 427 th.TestMethod(t, r, "PATCH") 428 th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID) 429 430 th.TestJSONRequest(t, r, `[ 431 { 432 "op": "add", 433 "path": "/hw_disk_bus", 434 "value": "scsi" 435 }, 436 { 437 "op": "add", 438 "path": "/hw_disk_bus_model", 439 "value": "virtio-scsi" 440 }, 441 { 442 "op": "add", 443 "path": "/hw_scsi_model", 444 "value": "virtio-scsi" 445 } 446 ]`) 447 448 th.AssertEquals(t, "application/openstack-images-v2.1-json-patch", r.Header.Get("Content-Type")) 449 450 w.Header().Add("Content-Type", "application/json") 451 w.WriteHeader(http.StatusOK) 452 fmt.Fprintf(w, `{ 453 "id": "da3b75d9-3f4a-40e7-8a2c-bfab23927dea", 454 "name": "Fedora 17", 455 "status": "active", 456 "visibility": "public", 457 "size": 2254249, 458 "checksum": "2cec138d7dae2aa59038ef8c9aec2390", 459 "tags": [ 460 "fedora", 461 "beefy" 462 ], 463 "created_at": "2012-08-10T19:23:50Z", 464 "updated_at": "2012-08-12T11:11:33Z", 465 "self": "/v2/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea", 466 "file": "/v2/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea/file", 467 "schema": "/v2/schemas/image", 468 "owner": "", 469 "min_ram": 0, 470 "min_disk": 0, 471 "disk_format": "", 472 "virtual_size": 0, 473 "container_format": "", 474 "hw_disk_bus": "scsi", 475 "hw_disk_bus_model": "virtio-scsi", 476 "hw_scsi_model": "virtio-scsi" 477 }`) 478 }) 479 }