PageRenderTime 45ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/github.com/gophercloud/gophercloud/acceptance/openstack/objectstorage/v1/containers_test.go

https://gitlab.com/unofficial-mirrors/openshift-origin
Go | 146 lines | 108 code | 21 blank | 17 comment | 21 complexity | 1722e33632a66334cb96f99ebdedb1c1 MD5 | raw file
  1. // +build acceptance
  2. package v1
  3. import (
  4. "strings"
  5. "testing"
  6. "github.com/gophercloud/gophercloud/acceptance/clients"
  7. "github.com/gophercloud/gophercloud/acceptance/tools"
  8. "github.com/gophercloud/gophercloud/openstack/objectstorage/v1/containers"
  9. "github.com/gophercloud/gophercloud/pagination"
  10. th "github.com/gophercloud/gophercloud/testhelper"
  11. )
  12. // numContainers is the number of containers to create for testing.
  13. var numContainers = 2
  14. func TestContainers(t *testing.T) {
  15. client, err := clients.NewObjectStorageV1Client()
  16. if err != nil {
  17. t.Fatalf("Unable to create client: %v", err)
  18. }
  19. // Create a slice of random container names.
  20. cNames := make([]string, numContainers)
  21. for i := 0; i < numContainers; i++ {
  22. cNames[i] = tools.RandomString("gophercloud-test-container-", 8)
  23. }
  24. // Create numContainers containers.
  25. for i := 0; i < len(cNames); i++ {
  26. res := containers.Create(client, cNames[i], nil)
  27. th.AssertNoErr(t, res.Err)
  28. }
  29. // Delete the numContainers containers after function completion.
  30. defer func() {
  31. for i := 0; i < len(cNames); i++ {
  32. res := containers.Delete(client, cNames[i])
  33. th.AssertNoErr(t, res.Err)
  34. }
  35. }()
  36. // List the numContainer names that were just created. To just list those,
  37. // the 'prefix' parameter is used.
  38. err = containers.List(client, &containers.ListOpts{Full: true, Prefix: "gophercloud-test-container-"}).EachPage(func(page pagination.Page) (bool, error) {
  39. containerList, err := containers.ExtractInfo(page)
  40. th.AssertNoErr(t, err)
  41. for _, n := range containerList {
  42. t.Logf("Container: Name [%s] Count [%d] Bytes [%d]",
  43. n.Name, n.Count, n.Bytes)
  44. }
  45. return true, nil
  46. })
  47. th.AssertNoErr(t, err)
  48. // List the info for the numContainer containers that were created.
  49. err = containers.List(client, &containers.ListOpts{Full: false, Prefix: "gophercloud-test-container-"}).EachPage(func(page pagination.Page) (bool, error) {
  50. containerList, err := containers.ExtractNames(page)
  51. th.AssertNoErr(t, err)
  52. for _, n := range containerList {
  53. t.Logf("Container: Name [%s]", n)
  54. }
  55. return true, nil
  56. })
  57. th.AssertNoErr(t, err)
  58. // Update one of the numContainer container metadata.
  59. metadata := map[string]string{
  60. "Gophercloud-Test": "containers",
  61. }
  62. updateres := containers.Update(client, cNames[0], &containers.UpdateOpts{Metadata: metadata})
  63. th.AssertNoErr(t, updateres.Err)
  64. // After the tests are done, delete the metadata that was set.
  65. defer func() {
  66. tempMap := make(map[string]string)
  67. for k := range metadata {
  68. tempMap[k] = ""
  69. }
  70. res := containers.Update(client, cNames[0], &containers.UpdateOpts{Metadata: tempMap})
  71. th.AssertNoErr(t, res.Err)
  72. }()
  73. // Retrieve a container's metadata.
  74. cm, err := containers.Get(client, cNames[0]).ExtractMetadata()
  75. th.AssertNoErr(t, err)
  76. for k := range metadata {
  77. if cm[k] != metadata[strings.Title(k)] {
  78. t.Errorf("Expected custom metadata with key: %s", k)
  79. }
  80. }
  81. }
  82. func TestListAllContainers(t *testing.T) {
  83. client, err := clients.NewObjectStorageV1Client()
  84. if err != nil {
  85. t.Fatalf("Unable to create client: %v", err)
  86. }
  87. numContainers := 20
  88. // Create a slice of random container names.
  89. cNames := make([]string, numContainers)
  90. for i := 0; i < numContainers; i++ {
  91. cNames[i] = tools.RandomString("gophercloud-test-container-", 8)
  92. }
  93. // Create numContainers containers.
  94. for i := 0; i < len(cNames); i++ {
  95. res := containers.Create(client, cNames[i], nil)
  96. th.AssertNoErr(t, res.Err)
  97. }
  98. // Delete the numContainers containers after function completion.
  99. defer func() {
  100. for i := 0; i < len(cNames); i++ {
  101. res := containers.Delete(client, cNames[i])
  102. th.AssertNoErr(t, res.Err)
  103. }
  104. }()
  105. // List all the numContainer names that were just created. To just list those,
  106. // the 'prefix' parameter is used.
  107. allPages, err := containers.List(client, &containers.ListOpts{Full: true, Limit: 5, Prefix: "gophercloud-test-container-"}).AllPages()
  108. th.AssertNoErr(t, err)
  109. containerInfoList, err := containers.ExtractInfo(allPages)
  110. th.AssertNoErr(t, err)
  111. for _, n := range containerInfoList {
  112. t.Logf("Container: Name [%s] Count [%d] Bytes [%d]",
  113. n.Name, n.Count, n.Bytes)
  114. }
  115. th.AssertEquals(t, numContainers, len(containerInfoList))
  116. // List the info for all the numContainer containers that were created.
  117. allPages, err = containers.List(client, &containers.ListOpts{Full: false, Limit: 2, Prefix: "gophercloud-test-container-"}).AllPages()
  118. th.AssertNoErr(t, err)
  119. containerNamesList, err := containers.ExtractNames(allPages)
  120. th.AssertNoErr(t, err)
  121. for _, n := range containerNamesList {
  122. t.Logf("Container: Name [%s]", n)
  123. }
  124. th.AssertEquals(t, numContainers, len(containerNamesList))
  125. }