PageRenderTime 81ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/unofficial-mirrors/openshift-origin
Go | 257 lines | 197 code | 38 blank | 22 comment | 42 complexity | 137fd6ca5cc31d9bb88d24d37aa3299e MD5 | raw file
  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. }
  117. func TestObjectsListSubdir(t *testing.T) {
  118. client, err := clients.NewObjectStorageV1Client()
  119. if err != nil {
  120. t.Fatalf("Unable to create client: %v", err)
  121. }
  122. // Create a random subdirectory name.
  123. cSubdir1 := tools.RandomString("test-subdir-", 8)
  124. cSubdir2 := tools.RandomString("test-subdir-", 8)
  125. // Make a slice of length numObjects to hold the random object names.
  126. oNames1 := make([]string, numObjects)
  127. for i := 0; i < len(oNames1); i++ {
  128. oNames1[i] = cSubdir1 + "/" + tools.RandomString("test-object-", 8)
  129. }
  130. oNames2 := make([]string, numObjects)
  131. for i := 0; i < len(oNames2); i++ {
  132. oNames2[i] = cSubdir2 + "/" + tools.RandomString("test-object-", 8)
  133. }
  134. // Create a container to hold the test objects.
  135. cName := tools.RandomString("test-container-", 8)
  136. _, err = containers.Create(client, cName, nil).Extract()
  137. th.AssertNoErr(t, err)
  138. // Defer deletion of the container until after testing.
  139. defer func() {
  140. t.Logf("Deleting container %s", cName)
  141. res := containers.Delete(client, cName)
  142. th.AssertNoErr(t, res.Err)
  143. }()
  144. // Create a slice of buffers to hold the test object content.
  145. oContents1 := make([]*bytes.Buffer, numObjects)
  146. for i := 0; i < numObjects; i++ {
  147. oContents1[i] = bytes.NewBuffer([]byte(tools.RandomString("", 10)))
  148. createOpts := objects.CreateOpts{
  149. Content: oContents1[i],
  150. }
  151. res := objects.Create(client, cName, oNames1[i], createOpts)
  152. th.AssertNoErr(t, res.Err)
  153. }
  154. // Delete the objects after testing.
  155. defer func() {
  156. for i := 0; i < numObjects; i++ {
  157. t.Logf("Deleting object %s", oNames1[i])
  158. res := objects.Delete(client, cName, oNames1[i], nil)
  159. th.AssertNoErr(t, res.Err)
  160. }
  161. }()
  162. oContents2 := make([]*bytes.Buffer, numObjects)
  163. for i := 0; i < numObjects; i++ {
  164. oContents2[i] = bytes.NewBuffer([]byte(tools.RandomString("", 10)))
  165. createOpts := objects.CreateOpts{
  166. Content: oContents2[i],
  167. }
  168. res := objects.Create(client, cName, oNames2[i], createOpts)
  169. th.AssertNoErr(t, res.Err)
  170. }
  171. // Delete the objects after testing.
  172. defer func() {
  173. for i := 0; i < numObjects; i++ {
  174. t.Logf("Deleting object %s", oNames2[i])
  175. res := objects.Delete(client, cName, oNames2[i], nil)
  176. th.AssertNoErr(t, res.Err)
  177. }
  178. }()
  179. listOpts := objects.ListOpts{
  180. Full: true,
  181. Delimiter: "/",
  182. }
  183. allPages, err := objects.List(client, cName, listOpts).AllPages()
  184. if err != nil {
  185. t.Fatal(err)
  186. }
  187. allObjects, err := objects.ExtractNames(allPages)
  188. if err != nil {
  189. t.Fatal(err)
  190. }
  191. t.Logf("%#v\n", allObjects)
  192. expected := []string{cSubdir1, cSubdir2}
  193. for _, e := range expected {
  194. var valid bool
  195. for _, a := range allObjects {
  196. if e+"/" == a {
  197. valid = true
  198. }
  199. }
  200. if !valid {
  201. t.Fatalf("could not find %s in results", e)
  202. }
  203. }
  204. listOpts = objects.ListOpts{
  205. Full: true,
  206. Delimiter: "/",
  207. Prefix: cSubdir2,
  208. }
  209. allPages, err = objects.List(client, cName, listOpts).AllPages()
  210. if err != nil {
  211. t.Fatal(err)
  212. }
  213. allObjects, err = objects.ExtractNames(allPages)
  214. if err != nil {
  215. t.Fatal(err)
  216. }
  217. th.AssertEquals(t, allObjects[0], cSubdir2+"/")
  218. t.Logf("%#v\n", allObjects)
  219. }