PageRenderTime 79ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/JamesClonk/machine
Go | 89 lines | 63 code | 14 blank | 12 comment | 11 complexity | 1420467aadedcfd93b04ccf4af2a99ab 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. }