github.com/gophercloud/gophercloud@v1.11.0/openstack/imageservice/v2/images/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/gophercloud/gophercloud/openstack/imageservice/v2/images" 8 "github.com/gophercloud/gophercloud/pagination" 9 th "github.com/gophercloud/gophercloud/testhelper" 10 fakeclient "github.com/gophercloud/gophercloud/testhelper/client" 11 ) 12 13 func TestListImage(t *testing.T) { 14 th.SetupHTTP() 15 defer th.TeardownHTTP() 16 17 HandleImageListSuccessfully(t) 18 19 t.Logf("Id\tName\tOwner\tChecksum\tSizeBytes") 20 21 pager := images.List(fakeclient.ServiceClient(), images.ListOpts{Limit: 1}) 22 t.Logf("Pager state %v", pager) 23 count, pages := 0, 0 24 err := pager.EachPage(func(page pagination.Page) (bool, error) { 25 pages++ 26 t.Logf("Page %v", page) 27 images, err := images.ExtractImages(page) 28 if err != nil { 29 return false, err 30 } 31 32 for _, i := range images { 33 t.Logf("%s\t%s\t%s\t%s\t%v\t\n", i.ID, i.Name, i.Owner, i.Checksum, i.SizeBytes) 34 count++ 35 } 36 37 return true, nil 38 }) 39 th.AssertNoErr(t, err) 40 41 t.Logf("--------\n%d images listed on %d pages.\n", count, pages) 42 th.AssertEquals(t, 3, pages) 43 th.AssertEquals(t, 3, count) 44 } 45 46 func TestAllPagesImage(t *testing.T) { 47 th.SetupHTTP() 48 defer th.TeardownHTTP() 49 50 HandleImageListSuccessfully(t) 51 52 pages, err := images.List(fakeclient.ServiceClient(), nil).AllPages() 53 th.AssertNoErr(t, err) 54 images, err := images.ExtractImages(pages) 55 th.AssertNoErr(t, err) 56 th.AssertEquals(t, 3, len(images)) 57 } 58 59 func TestCreateImage(t *testing.T) { 60 th.SetupHTTP() 61 defer th.TeardownHTTP() 62 63 HandleImageCreationSuccessfully(t) 64 65 id := "e7db3b45-8db7-47ad-8109-3fb55c2c24fd" 66 name := "Ubuntu 12.10" 67 68 actualImage, err := images.Create(fakeclient.ServiceClient(), images.CreateOpts{ 69 ID: id, 70 Name: name, 71 Properties: map[string]string{ 72 "architecture": "x86_64", 73 }, 74 Tags: []string{"ubuntu", "quantal"}, 75 }).Extract() 76 77 th.AssertNoErr(t, err) 78 79 containerFormat := "bare" 80 diskFormat := "qcow2" 81 owner := "b4eedccc6fb74fa8a7ad6b08382b852b" 82 minDiskGigabytes := 0 83 minRAMMegabytes := 0 84 file := actualImage.File 85 createdDate := actualImage.CreatedAt 86 lastUpdate := actualImage.UpdatedAt 87 schema := "/v2/schemas/image" 88 89 expectedImage := images.Image{ 90 ID: "e7db3b45-8db7-47ad-8109-3fb55c2c24fd", 91 Name: "Ubuntu 12.10", 92 Tags: []string{"ubuntu", "quantal"}, 93 94 Status: images.ImageStatusQueued, 95 96 ContainerFormat: containerFormat, 97 DiskFormat: diskFormat, 98 99 MinDiskGigabytes: minDiskGigabytes, 100 MinRAMMegabytes: minRAMMegabytes, 101 102 Owner: owner, 103 104 Visibility: images.ImageVisibilityPrivate, 105 File: file, 106 CreatedAt: createdDate, 107 UpdatedAt: lastUpdate, 108 Schema: schema, 109 VirtualSize: 0, 110 Properties: map[string]interface{}{ 111 "hw_disk_bus": "scsi", 112 "hw_disk_bus_model": "virtio-scsi", 113 "hw_scsi_model": "virtio-scsi", 114 }, 115 } 116 117 th.AssertDeepEquals(t, &expectedImage, actualImage) 118 } 119 120 func TestCreateImageNulls(t *testing.T) { 121 th.SetupHTTP() 122 defer th.TeardownHTTP() 123 124 HandleImageCreationSuccessfullyNulls(t) 125 126 id := "e7db3b45-8db7-47ad-8109-3fb55c2c24fd" 127 name := "Ubuntu 12.10" 128 129 actualImage, err := images.Create(fakeclient.ServiceClient(), images.CreateOpts{ 130 ID: id, 131 Name: name, 132 Tags: []string{"ubuntu", "quantal"}, 133 Properties: map[string]string{ 134 "architecture": "x86_64", 135 }, 136 }).Extract() 137 138 th.AssertNoErr(t, err) 139 140 containerFormat := "bare" 141 diskFormat := "qcow2" 142 owner := "b4eedccc6fb74fa8a7ad6b08382b852b" 143 minDiskGigabytes := 0 144 minRAMMegabytes := 0 145 file := actualImage.File 146 createdDate := actualImage.CreatedAt 147 lastUpdate := actualImage.UpdatedAt 148 schema := "/v2/schemas/image" 149 properties := map[string]interface{}{ 150 "architecture": "x86_64", 151 } 152 sizeBytes := int64(0) 153 154 expectedImage := images.Image{ 155 ID: "e7db3b45-8db7-47ad-8109-3fb55c2c24fd", 156 Name: "Ubuntu 12.10", 157 Tags: []string{"ubuntu", "quantal"}, 158 159 Status: images.ImageStatusQueued, 160 161 ContainerFormat: containerFormat, 162 DiskFormat: diskFormat, 163 164 MinDiskGigabytes: minDiskGigabytes, 165 MinRAMMegabytes: minRAMMegabytes, 166 167 Owner: owner, 168 169 Visibility: images.ImageVisibilityPrivate, 170 File: file, 171 CreatedAt: createdDate, 172 UpdatedAt: lastUpdate, 173 Schema: schema, 174 Properties: properties, 175 SizeBytes: sizeBytes, 176 OpenStackImageImportMethods: []string{ 177 "glance-direct", 178 "web-download", 179 }, 180 OpenStackImageStoreIDs: []string{ 181 "123", 182 "456", 183 }, 184 } 185 186 th.AssertDeepEquals(t, &expectedImage, actualImage) 187 } 188 189 func TestGetImage(t *testing.T) { 190 th.SetupHTTP() 191 defer th.TeardownHTTP() 192 193 HandleImageGetSuccessfully(t) 194 195 actualImage, err := images.Get(fakeclient.ServiceClient(), "1bea47ed-f6a9-463b-b423-14b9cca9ad27").Extract() 196 197 th.AssertNoErr(t, err) 198 199 checksum := "64d7c1cd2b6f60c92c14662941cb7913" 200 sizeBytes := int64(13167616) 201 containerFormat := "bare" 202 diskFormat := "qcow2" 203 minDiskGigabytes := 0 204 minRAMMegabytes := 0 205 owner := "5ef70662f8b34079a6eddb8da9d75fe8" 206 file := actualImage.File 207 createdDate := actualImage.CreatedAt 208 lastUpdate := actualImage.UpdatedAt 209 schema := "/v2/schemas/image" 210 211 expectedImage := images.Image{ 212 ID: "1bea47ed-f6a9-463b-b423-14b9cca9ad27", 213 Name: "cirros-0.3.2-x86_64-disk", 214 Tags: []string{}, 215 216 Status: images.ImageStatusActive, 217 218 ContainerFormat: containerFormat, 219 DiskFormat: diskFormat, 220 221 MinDiskGigabytes: minDiskGigabytes, 222 MinRAMMegabytes: minRAMMegabytes, 223 224 Owner: owner, 225 226 Protected: false, 227 Visibility: images.ImageVisibilityPublic, 228 Hidden: false, 229 230 Checksum: checksum, 231 SizeBytes: sizeBytes, 232 File: file, 233 CreatedAt: createdDate, 234 UpdatedAt: lastUpdate, 235 Schema: schema, 236 VirtualSize: 0, 237 Properties: map[string]interface{}{ 238 "hw_disk_bus": "scsi", 239 "hw_disk_bus_model": "virtio-scsi", 240 "hw_scsi_model": "virtio-scsi", 241 }, 242 } 243 244 th.AssertDeepEquals(t, &expectedImage, actualImage) 245 } 246 247 func TestDeleteImage(t *testing.T) { 248 th.SetupHTTP() 249 defer th.TeardownHTTP() 250 251 HandleImageDeleteSuccessfully(t) 252 253 result := images.Delete(fakeclient.ServiceClient(), "1bea47ed-f6a9-463b-b423-14b9cca9ad27") 254 th.AssertNoErr(t, result.Err) 255 } 256 257 func TestUpdateImage(t *testing.T) { 258 th.SetupHTTP() 259 defer th.TeardownHTTP() 260 261 HandleImageUpdateSuccessfully(t) 262 263 actualImage, err := images.Update(fakeclient.ServiceClient(), "da3b75d9-3f4a-40e7-8a2c-bfab23927dea", images.UpdateOpts{ 264 images.ReplaceImageName{NewName: "Fedora 17"}, 265 images.ReplaceImageTags{NewTags: []string{"fedora", "beefy"}}, 266 images.ReplaceImageMinDisk{NewMinDisk: 21}, 267 images.ReplaceImageMinRam{NewMinRam: 1024}, 268 images.ReplaceImageHidden{NewHidden: false}, 269 images.ReplaceImageProtected{NewProtected: true}, 270 images.UpdateImageProperty{ 271 Op: images.AddOp, 272 Name: "empty_value", 273 Value: "", 274 }, 275 }).Extract() 276 277 th.AssertNoErr(t, err) 278 279 sizebytes := int64(2254249) 280 checksum := "2cec138d7dae2aa59038ef8c9aec2390" 281 file := actualImage.File 282 createdDate := actualImage.CreatedAt 283 lastUpdate := actualImage.UpdatedAt 284 schema := "/v2/schemas/image" 285 286 expectedImage := images.Image{ 287 ID: "da3b75d9-3f4a-40e7-8a2c-bfab23927dea", 288 Name: "Fedora 17", 289 Status: images.ImageStatusActive, 290 Visibility: images.ImageVisibilityPublic, 291 Hidden: false, 292 Protected: true, 293 294 SizeBytes: sizebytes, 295 Checksum: checksum, 296 297 Tags: []string{ 298 "fedora", 299 "beefy", 300 }, 301 302 Owner: "", 303 MinRAMMegabytes: 1024, 304 MinDiskGigabytes: 21, 305 306 DiskFormat: "", 307 ContainerFormat: "", 308 File: file, 309 CreatedAt: createdDate, 310 UpdatedAt: lastUpdate, 311 Schema: schema, 312 VirtualSize: 0, 313 Properties: map[string]interface{}{ 314 "hw_disk_bus": "scsi", 315 "hw_disk_bus_model": "virtio-scsi", 316 "hw_scsi_model": "virtio-scsi", 317 "empty_value": "", 318 }, 319 } 320 321 th.AssertDeepEquals(t, &expectedImage, actualImage) 322 } 323 324 func TestImageDateQuery(t *testing.T) { 325 date := time.Date(2014, 1, 1, 1, 1, 1, 0, time.UTC) 326 327 listOpts := images.ListOpts{ 328 CreatedAtQuery: &images.ImageDateQuery{ 329 Date: date, 330 Filter: images.FilterGTE, 331 }, 332 UpdatedAtQuery: &images.ImageDateQuery{ 333 Date: date, 334 }, 335 } 336 337 expectedQueryString := "?created_at=gte%3A2014-01-01T01%3A01%3A01Z&updated_at=2014-01-01T01%3A01%3A01Z" 338 actualQueryString, err := listOpts.ToImageListQuery() 339 th.AssertNoErr(t, err) 340 th.AssertEquals(t, expectedQueryString, actualQueryString) 341 } 342 343 func TestImageListByTags(t *testing.T) { 344 th.SetupHTTP() 345 defer th.TeardownHTTP() 346 347 HandleImageListByTagsSuccessfully(t) 348 349 listOpts := images.ListOpts{ 350 Tags: []string{"foo", "bar"}, 351 } 352 353 expectedQueryString := "?tag=foo&tag=bar" 354 actualQueryString, err := listOpts.ToImageListQuery() 355 th.AssertNoErr(t, err) 356 th.AssertEquals(t, expectedQueryString, actualQueryString) 357 358 pages, err := images.List(fakeclient.ServiceClient(), listOpts).AllPages() 359 th.AssertNoErr(t, err) 360 allImages, err := images.ExtractImages(pages) 361 th.AssertNoErr(t, err) 362 363 checksum := "64d7c1cd2b6f60c92c14662941cb7913" 364 sizeBytes := int64(13167616) 365 containerFormat := "bare" 366 diskFormat := "qcow2" 367 minDiskGigabytes := 0 368 minRAMMegabytes := 0 369 owner := "5ef70662f8b34079a6eddb8da9d75fe8" 370 file := allImages[0].File 371 createdDate := allImages[0].CreatedAt 372 lastUpdate := allImages[0].UpdatedAt 373 schema := "/v2/schemas/image" 374 tags := []string{"foo", "bar"} 375 376 expectedImage := images.Image{ 377 ID: "1bea47ed-f6a9-463b-b423-14b9cca9ad27", 378 Name: "cirros-0.3.2-x86_64-disk", 379 Tags: tags, 380 381 Status: images.ImageStatusActive, 382 383 ContainerFormat: containerFormat, 384 DiskFormat: diskFormat, 385 386 MinDiskGigabytes: minDiskGigabytes, 387 MinRAMMegabytes: minRAMMegabytes, 388 389 Owner: owner, 390 391 Protected: false, 392 Visibility: images.ImageVisibilityPublic, 393 394 Checksum: checksum, 395 SizeBytes: sizeBytes, 396 File: file, 397 CreatedAt: createdDate, 398 UpdatedAt: lastUpdate, 399 Schema: schema, 400 VirtualSize: 0, 401 Properties: map[string]interface{}{ 402 "hw_disk_bus": "scsi", 403 "hw_disk_bus_model": "virtio-scsi", 404 "hw_scsi_model": "virtio-scsi", 405 }, 406 } 407 408 th.AssertDeepEquals(t, expectedImage, allImages[0]) 409 } 410 411 func TestUpdateImageProperties(t *testing.T) { 412 th.SetupHTTP() 413 defer th.TeardownHTTP() 414 415 HandleImageUpdatePropertiesSuccessfully(t) 416 417 actualImage, err := images.Update(fakeclient.ServiceClient(), "da3b75d9-3f4a-40e7-8a2c-bfab23927dea", images.UpdateOpts{ 418 images.UpdateImageProperty{ 419 Op: images.AddOp, 420 Name: "hw_disk_bus", 421 Value: "scsi", 422 }, 423 images.UpdateImageProperty{ 424 Op: images.AddOp, 425 Name: "hw_disk_bus_model", 426 Value: "virtio-scsi", 427 }, 428 images.UpdateImageProperty{ 429 Op: images.AddOp, 430 Name: "hw_scsi_model", 431 Value: "virtio-scsi", 432 }, 433 }).Extract() 434 435 th.AssertNoErr(t, err) 436 437 sizebytes := int64(2254249) 438 checksum := "2cec138d7dae2aa59038ef8c9aec2390" 439 file := actualImage.File 440 createdDate := actualImage.CreatedAt 441 lastUpdate := actualImage.UpdatedAt 442 schema := "/v2/schemas/image" 443 444 expectedImage := images.Image{ 445 ID: "da3b75d9-3f4a-40e7-8a2c-bfab23927dea", 446 Name: "Fedora 17", 447 Status: images.ImageStatusActive, 448 Visibility: images.ImageVisibilityPublic, 449 450 SizeBytes: sizebytes, 451 Checksum: checksum, 452 453 Tags: []string{ 454 "fedora", 455 "beefy", 456 }, 457 458 Owner: "", 459 MinRAMMegabytes: 0, 460 MinDiskGigabytes: 0, 461 462 DiskFormat: "", 463 ContainerFormat: "", 464 File: file, 465 CreatedAt: createdDate, 466 UpdatedAt: lastUpdate, 467 Schema: schema, 468 VirtualSize: 0, 469 Properties: map[string]interface{}{ 470 "hw_disk_bus": "scsi", 471 "hw_disk_bus_model": "virtio-scsi", 472 "hw_scsi_model": "virtio-scsi", 473 }, 474 } 475 476 th.AssertDeepEquals(t, &expectedImage, actualImage) 477 }