requests_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package testing
  2. import (
  3. "testing"
  4. "time"
  5. "devel.mephi.ru/iacherepanov/openstack-gophercloud/openstack/blockstorage/v1/volumes"
  6. "devel.mephi.ru/iacherepanov/openstack-gophercloud/pagination"
  7. th "devel.mephi.ru/iacherepanov/openstack-gophercloud/testhelper"
  8. "devel.mephi.ru/iacherepanov/openstack-gophercloud/testhelper/client"
  9. )
  10. func TestList(t *testing.T) {
  11. th.SetupHTTP()
  12. defer th.TeardownHTTP()
  13. MockListResponse(t)
  14. count := 0
  15. volumes.List(client.ServiceClient(), &volumes.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
  16. count++
  17. actual, err := volumes.ExtractVolumes(page)
  18. if err != nil {
  19. t.Errorf("Failed to extract volumes: %v", err)
  20. return false, err
  21. }
  22. expected := []volumes.Volume{
  23. {
  24. ID: "289da7f8-6440-407c-9fb4-7db01ec49164",
  25. Name: "vol-001",
  26. },
  27. {
  28. ID: "96c3bda7-c82a-4f50-be73-ca7621794835",
  29. Name: "vol-002",
  30. },
  31. }
  32. th.CheckDeepEquals(t, expected, actual)
  33. return true, nil
  34. })
  35. if count != 1 {
  36. t.Errorf("Expected 1 page, got %d", count)
  37. }
  38. }
  39. func TestListAll(t *testing.T) {
  40. th.SetupHTTP()
  41. defer th.TeardownHTTP()
  42. MockListResponse(t)
  43. allPages, err := volumes.List(client.ServiceClient(), &volumes.ListOpts{}).AllPages()
  44. th.AssertNoErr(t, err)
  45. actual, err := volumes.ExtractVolumes(allPages)
  46. th.AssertNoErr(t, err)
  47. expected := []volumes.Volume{
  48. {
  49. ID: "289da7f8-6440-407c-9fb4-7db01ec49164",
  50. Name: "vol-001",
  51. },
  52. {
  53. ID: "96c3bda7-c82a-4f50-be73-ca7621794835",
  54. Name: "vol-002",
  55. },
  56. }
  57. th.CheckDeepEquals(t, expected, actual)
  58. }
  59. func TestGet(t *testing.T) {
  60. th.SetupHTTP()
  61. defer th.TeardownHTTP()
  62. MockGetResponse(t)
  63. actual, err := volumes.Get(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
  64. th.AssertNoErr(t, err)
  65. expected := &volumes.Volume{
  66. Status: "active",
  67. Name: "vol-001",
  68. Attachments: []map[string]interface{}{
  69. {
  70. "attachment_id": "03987cd1-0ad5-40d1-9b2a-7cc48295d4fa",
  71. "id": "47e9ecc5-4045-4ee3-9a4b-d859d546a0cf",
  72. "volume_id": "6c80f8ac-e3e2-480c-8e6e-f1db92fe4bfe",
  73. "server_id": "d1c4788b-9435-42e2-9b81-29f3be1cd01f",
  74. "host_name": "mitaka",
  75. "device": "/",
  76. },
  77. },
  78. AvailabilityZone: "us-east1",
  79. Bootable: "false",
  80. CreatedAt: time.Date(2012, 2, 14, 20, 53, 07, 0, time.UTC),
  81. Description: "Another volume.",
  82. VolumeType: "289da7f8-6440-407c-9fb4-7db01ec49164",
  83. SnapshotID: "",
  84. SourceVolID: "",
  85. Metadata: map[string]string{
  86. "contents": "junk",
  87. },
  88. ID: "521752a6-acf6-4b2d-bc7a-119f9148cd8c",
  89. Size: 30,
  90. }
  91. th.AssertDeepEquals(t, expected, actual)
  92. }
  93. func TestCreate(t *testing.T) {
  94. th.SetupHTTP()
  95. defer th.TeardownHTTP()
  96. MockCreateResponse(t)
  97. options := &volumes.CreateOpts{
  98. Size: 75,
  99. AvailabilityZone: "us-east1",
  100. }
  101. n, err := volumes.Create(client.ServiceClient(), options).Extract()
  102. th.AssertNoErr(t, err)
  103. th.AssertEquals(t, n.Size, 4)
  104. th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
  105. }
  106. func TestDelete(t *testing.T) {
  107. th.SetupHTTP()
  108. defer th.TeardownHTTP()
  109. MockDeleteResponse(t)
  110. res := volumes.Delete(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
  111. th.AssertNoErr(t, res.Err)
  112. }
  113. func TestUpdate(t *testing.T) {
  114. th.SetupHTTP()
  115. defer th.TeardownHTTP()
  116. MockUpdateResponse(t)
  117. options := volumes.UpdateOpts{Name: "vol-002"}
  118. v, err := volumes.Update(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", options).Extract()
  119. th.AssertNoErr(t, err)
  120. th.CheckEquals(t, "vol-002", v.Name)
  121. }