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