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