PageRenderTime 152ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/JamesClonk/machine
Go | 119 lines | 82 code | 21 blank | 16 comment | 10 complexity | e0b8c24e78d199d301d9eae7d44e0066 MD5 | raw file
  1. // +build acceptance
  2. package v1
  3. import (
  4. "bytes"
  5. "strings"
  6. "testing"
  7. "github.com/rackspace/gophercloud/acceptance/tools"
  8. "github.com/rackspace/gophercloud/openstack/objectstorage/v1/containers"
  9. "github.com/rackspace/gophercloud/openstack/objectstorage/v1/objects"
  10. "github.com/rackspace/gophercloud/pagination"
  11. th "github.com/rackspace/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. // Create a provider client for executing the HTTP request.
  17. // See common.go for more information.
  18. client := newClient(t)
  19. // Make a slice of length numObjects to hold the random object names.
  20. oNames := make([]string, numObjects)
  21. for i := 0; i < len(oNames); i++ {
  22. oNames[i] = tools.RandomString("test-object-", 8)
  23. }
  24. // Create a container to hold the test objects.
  25. cName := tools.RandomString("test-container-", 8)
  26. header, err := containers.Create(client, cName, nil).ExtractHeader()
  27. th.AssertNoErr(t, err)
  28. t.Logf("Create object headers: %+v\n", header)
  29. // Defer deletion of the container until after testing.
  30. defer func() {
  31. res := containers.Delete(client, cName)
  32. th.AssertNoErr(t, res.Err)
  33. }()
  34. // Create a slice of buffers to hold the test object content.
  35. oContents := make([]*bytes.Buffer, numObjects)
  36. for i := 0; i < numObjects; i++ {
  37. oContents[i] = bytes.NewBuffer([]byte(tools.RandomString("", 10)))
  38. res := objects.Create(client, cName, oNames[i], oContents[i], nil)
  39. th.AssertNoErr(t, res.Err)
  40. }
  41. // Delete the objects after testing.
  42. defer func() {
  43. for i := 0; i < numObjects; i++ {
  44. res := objects.Delete(client, cName, oNames[i], nil)
  45. th.AssertNoErr(t, res.Err)
  46. }
  47. }()
  48. ons := make([]string, 0, len(oNames))
  49. err = objects.List(client, cName, &objects.ListOpts{Full: false, Prefix: "test-object-"}).EachPage(func(page pagination.Page) (bool, error) {
  50. names, err := objects.ExtractNames(page)
  51. th.AssertNoErr(t, err)
  52. ons = append(ons, names...)
  53. return true, nil
  54. })
  55. th.AssertNoErr(t, err)
  56. th.AssertEquals(t, len(ons), len(oNames))
  57. ois := make([]objects.Object, 0, len(oNames))
  58. err = objects.List(client, cName, &objects.ListOpts{Full: true, Prefix: "test-object-"}).EachPage(func(page pagination.Page) (bool, error) {
  59. info, err := objects.ExtractInfo(page)
  60. th.AssertNoErr(t, err)
  61. ois = append(ois, info...)
  62. return true, nil
  63. })
  64. th.AssertNoErr(t, err)
  65. th.AssertEquals(t, len(ois), len(oNames))
  66. // Copy the contents of one object to another.
  67. copyres := objects.Copy(client, cName, oNames[0], &objects.CopyOpts{Destination: cName + "/" + oNames[1]})
  68. th.AssertNoErr(t, copyres.Err)
  69. // Download one of the objects that was created above.
  70. o1Content, err := objects.Download(client, cName, oNames[0], nil).ExtractContent()
  71. th.AssertNoErr(t, err)
  72. // Download the another object that was create above.
  73. o2Content, err := objects.Download(client, cName, oNames[1], nil).ExtractContent()
  74. th.AssertNoErr(t, err)
  75. // Compare the two object's contents to test that the copy worked.
  76. th.AssertEquals(t, string(o2Content), string(o1Content))
  77. // Update an object's metadata.
  78. updateres := objects.Update(client, cName, oNames[0], &objects.UpdateOpts{Metadata: metadata})
  79. th.AssertNoErr(t, updateres.Err)
  80. // Delete the object's metadata after testing.
  81. defer func() {
  82. tempMap := make(map[string]string)
  83. for k := range metadata {
  84. tempMap[k] = ""
  85. }
  86. res := objects.Update(client, cName, oNames[0], &objects.UpdateOpts{Metadata: tempMap})
  87. th.AssertNoErr(t, res.Err)
  88. }()
  89. // Retrieve an object's metadata.
  90. om, err := objects.Get(client, cName, oNames[0], nil).ExtractMetadata()
  91. th.AssertNoErr(t, err)
  92. for k := range metadata {
  93. if om[k] != metadata[strings.Title(k)] {
  94. t.Errorf("Expected custom metadata with key: %s", k)
  95. return
  96. }
  97. }
  98. }