PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/cmd/cluster-capacity/go/src/github.com/kubernetes-incubator/cluster-capacity/vendor/github.com/gophercloud/gophercloud/acceptance/openstack/objectstorage/v1/objects_test.go

https://bitbucket.org/enterstudiosbiz/origin
Go | 138 lines | 101 code | 22 blank | 15 comment | 18 complexity | 91095f6d6112138996dbe25ee68e22ac MD5 | raw file
Possible License(s): Apache-2.0, 0BSD, GPL-2.0, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-3.0
  1. // +build acceptance
  2. package v1
  3. import (
  4. "bytes"
  5. "strings"
  6. "testing"
  7. "github.com/gophercloud/gophercloud/acceptance/clients"
  8. "github.com/gophercloud/gophercloud/acceptance/tools"
  9. "github.com/gophercloud/gophercloud/openstack/objectstorage/v1/containers"
  10. "github.com/gophercloud/gophercloud/openstack/objectstorage/v1/objects"
  11. th "github.com/gophercloud/gophercloud/testhelper"
  12. )
  13. // numObjects is the number of objects to create for testing.
  14. var numObjects = 2
  15. func TestObjects(t *testing.T) {
  16. client, err := clients.NewObjectStorageV1Client()
  17. if err != nil {
  18. t.Fatalf("Unable to create client: %v", err)
  19. }
  20. // Make a slice of length numObjects to hold the random object names.
  21. oNames := make([]string, numObjects)
  22. for i := 0; i < len(oNames); i++ {
  23. oNames[i] = tools.RandomString("test-object-", 8)
  24. }
  25. // Create a container to hold the test objects.
  26. cName := tools.RandomString("test-container-", 8)
  27. header, err := containers.Create(client, cName, nil).Extract()
  28. th.AssertNoErr(t, err)
  29. t.Logf("Create object headers: %+v\n", header)
  30. // Defer deletion of the container until after testing.
  31. defer func() {
  32. res := containers.Delete(client, cName)
  33. th.AssertNoErr(t, res.Err)
  34. }()
  35. // Create a slice of buffers to hold the test object content.
  36. oContents := make([]*bytes.Buffer, numObjects)
  37. for i := 0; i < numObjects; i++ {
  38. oContents[i] = bytes.NewBuffer([]byte(tools.RandomString("", 10)))
  39. createOpts := objects.CreateOpts{
  40. Content: oContents[i],
  41. }
  42. res := objects.Create(client, cName, oNames[i], createOpts)
  43. th.AssertNoErr(t, res.Err)
  44. }
  45. // Delete the objects after testing.
  46. defer func() {
  47. for i := 0; i < numObjects; i++ {
  48. res := objects.Delete(client, cName, oNames[i], nil)
  49. th.AssertNoErr(t, res.Err)
  50. }
  51. }()
  52. // List all created objects
  53. listOpts := objects.ListOpts{
  54. Full: true,
  55. Prefix: "test-object-",
  56. }
  57. allPages, err := objects.List(client, cName, listOpts).AllPages()
  58. if err != nil {
  59. t.Fatalf("Unable to list objects: %v", err)
  60. }
  61. ons, err := objects.ExtractNames(allPages)
  62. if err != nil {
  63. t.Fatalf("Unable to extract objects: %v", err)
  64. }
  65. th.AssertEquals(t, len(ons), len(oNames))
  66. ois, err := objects.ExtractInfo(allPages)
  67. if err != nil {
  68. t.Fatalf("Unable to extract object info: %v", err)
  69. }
  70. th.AssertEquals(t, len(ois), len(oNames))
  71. // Copy the contents of one object to another.
  72. copyOpts := objects.CopyOpts{
  73. Destination: cName + "/" + oNames[1],
  74. }
  75. copyres := objects.Copy(client, cName, oNames[0], copyOpts)
  76. th.AssertNoErr(t, copyres.Err)
  77. // Download one of the objects that was created above.
  78. downloadres := objects.Download(client, cName, oNames[0], nil)
  79. th.AssertNoErr(t, downloadres.Err)
  80. o1Content, err := downloadres.ExtractContent()
  81. th.AssertNoErr(t, err)
  82. // Download the another object that was create above.
  83. downloadres = objects.Download(client, cName, oNames[1], nil)
  84. th.AssertNoErr(t, downloadres.Err)
  85. o2Content, err := downloadres.ExtractContent()
  86. th.AssertNoErr(t, err)
  87. // Compare the two object's contents to test that the copy worked.
  88. th.AssertEquals(t, string(o2Content), string(o1Content))
  89. // Update an object's metadata.
  90. metadata := map[string]string{
  91. "Gophercloud-Test": "objects",
  92. }
  93. updateOpts := objects.UpdateOpts{
  94. Metadata: metadata,
  95. }
  96. updateres := objects.Update(client, cName, oNames[0], updateOpts)
  97. th.AssertNoErr(t, updateres.Err)
  98. // Delete the object's metadata after testing.
  99. defer func() {
  100. tempMap := make(map[string]string)
  101. for k := range metadata {
  102. tempMap[k] = ""
  103. }
  104. res := objects.Update(client, cName, oNames[0], &objects.UpdateOpts{Metadata: tempMap})
  105. th.AssertNoErr(t, res.Err)
  106. }()
  107. // Retrieve an object's metadata.
  108. om, err := objects.Get(client, cName, oNames[0], nil).ExtractMetadata()
  109. th.AssertNoErr(t, err)
  110. for k := range metadata {
  111. if om[k] != metadata[strings.Title(k)] {
  112. t.Errorf("Expected custom metadata with key: %s", k)
  113. return
  114. }
  115. }
  116. }