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

/Godeps/_workspace/src/github.com/rackspace/gophercloud/acceptance/openstack/objectstorage/v1/containers_test.go

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