blob: 987f733045edef20bdf6486827cc156875e9e794 [file] [log] [blame]
Jon Perrittf050a4c2014-09-11 15:01:17 -05001// +build acceptance
2
3package v1
4
5import (
6 "bytes"
7 "strings"
8 "testing"
9
10 "github.com/rackspace/gophercloud/acceptance/tools"
Jon Perritta9caabd2014-09-30 13:02:34 -050011 "github.com/rackspace/gophercloud/openstack/objectstorage/v1/containers"
12 "github.com/rackspace/gophercloud/openstack/objectstorage/v1/objects"
Jon Perrittf050a4c2014-09-11 15:01:17 -050013 "github.com/rackspace/gophercloud/pagination"
Jon Perritt2a7797d2014-10-21 15:08:43 -050014 th "github.com/rackspace/gophercloud/testhelper"
Jon Perrittf050a4c2014-09-11 15:01:17 -050015)
16
Jon Perrittb1eea512014-09-15 23:27:33 -050017// numObjects is the number of objects to create for testing.
Jon Perrittf050a4c2014-09-11 15:01:17 -050018var numObjects = 2
19
20func TestObjects(t *testing.T) {
Jon Perrittb1eea512014-09-15 23:27:33 -050021 // Create a provider client for executing the HTTP request.
22 // See common.go for more information.
Jon Perritt2b36fa32014-10-24 15:44:23 -050023 client := newClient(t)
Jon Perrittf050a4c2014-09-11 15:01:17 -050024
Jon Perrittb1eea512014-09-15 23:27:33 -050025 // Make a slice of length numObjects to hold the random object names.
Jon Perrittf050a4c2014-09-11 15:01:17 -050026 oNames := make([]string, numObjects)
27 for i := 0; i < len(oNames); i++ {
28 oNames[i] = tools.RandomString("test-object-", 8)
29 }
30
Jon Perrittb1eea512014-09-15 23:27:33 -050031 // Create a container to hold the test objects.
Jon Perrittf050a4c2014-09-11 15:01:17 -050032 cName := tools.RandomString("test-container-", 8)
Jon Perritt2b36fa32014-10-24 15:44:23 -050033 createres := containers.Create(client, cName, nil)
34 th.AssertNoErr(t, createres.Err)
Jon Perritt2a7797d2014-10-21 15:08:43 -050035
Jon Perrittb1eea512014-09-15 23:27:33 -050036 // Defer deletion of the container until after testing.
Jon Perrittf050a4c2014-09-11 15:01:17 -050037 defer func() {
Jon Perritt2b36fa32014-10-24 15:44:23 -050038 res := containers.Delete(client, cName)
39 th.AssertNoErr(t, res.Err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050040 }()
41
Jon Perrittb1eea512014-09-15 23:27:33 -050042 // Create a slice of buffers to hold the test object content.
Jon Perrittf050a4c2014-09-11 15:01:17 -050043 oContents := make([]*bytes.Buffer, numObjects)
44 for i := 0; i < numObjects; i++ {
45 oContents[i] = bytes.NewBuffer([]byte(tools.RandomString("", 10)))
Jon Perritt2b36fa32014-10-24 15:44:23 -050046 res := objects.Create(client, cName, oNames[i], oContents[i], nil)
47 th.AssertNoErr(t, res.Err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050048 }
Jon Perrittb1eea512014-09-15 23:27:33 -050049 // Delete the objects after testing.
Jon Perrittf050a4c2014-09-11 15:01:17 -050050 defer func() {
51 for i := 0; i < numObjects; i++ {
Jon Perritt2b36fa32014-10-24 15:44:23 -050052 res := objects.Delete(client, cName, oNames[i], nil)
53 th.AssertNoErr(t, res.Err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050054 }
55 }()
56
Jon Perrittf050a4c2014-09-11 15:01:17 -050057 ons := make([]string, 0, len(oNames))
Jon Perritt2b36fa32014-10-24 15:44:23 -050058 err := objects.List(client, cName, &objects.ListOpts{Full: false, Prefix: "test-object-"}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittf050a4c2014-09-11 15:01:17 -050059 names, err := objects.ExtractNames(page)
Jon Perritt2b36fa32014-10-24 15:44:23 -050060 th.AssertNoErr(t, err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050061 ons = append(ons, names...)
62
63 return true, nil
64 })
Jon Perritt2b36fa32014-10-24 15:44:23 -050065 th.AssertNoErr(t, err)
66 th.AssertEquals(t, len(ons), len(oNames))
Jon Perrittf050a4c2014-09-11 15:01:17 -050067
Jon Perrittf050a4c2014-09-11 15:01:17 -050068 ois := make([]objects.Object, 0, len(oNames))
Jon Perrittde47eac2014-09-30 15:34:17 -050069 err = objects.List(client, cName, &objects.ListOpts{Full: true, Prefix: "test-object-"}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittf050a4c2014-09-11 15:01:17 -050070 info, err := objects.ExtractInfo(page)
Jon Perritt2b36fa32014-10-24 15:44:23 -050071 th.AssertNoErr(t, err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050072
73 ois = append(ois, info...)
74
75 return true, nil
76 })
Jon Perritt2b36fa32014-10-24 15:44:23 -050077 th.AssertNoErr(t, err)
78 th.AssertEquals(t, len(ois), len(oNames))
Jon Perrittf050a4c2014-09-11 15:01:17 -050079
Jon Perrittb1eea512014-09-15 23:27:33 -050080 // Copy the contents of one object to another.
Jon Perritt2b36fa32014-10-24 15:44:23 -050081 copyres := objects.Copy(client, cName, oNames[0], &objects.CopyOpts{Destination: cName + "/" + oNames[1]})
82 th.AssertNoErr(t, copyres.Err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050083
Jon Perrittb1eea512014-09-15 23:27:33 -050084 // Download one of the objects that was created above.
Jon Perrittde47eac2014-09-30 15:34:17 -050085 o1Content, err := objects.Download(client, cName, oNames[0], nil).ExtractContent()
Jon Perritt2b36fa32014-10-24 15:44:23 -050086 th.AssertNoErr(t, err)
Jon Perritt2a7797d2014-10-21 15:08:43 -050087
Jon Perrittb1eea512014-09-15 23:27:33 -050088 // Download the another object that was create above.
Jon Perrittde47eac2014-09-30 15:34:17 -050089 o2Content, err := objects.Download(client, cName, oNames[1], nil).ExtractContent()
Jon Perritt2b36fa32014-10-24 15:44:23 -050090 th.AssertNoErr(t, err)
Jon Perritt2a7797d2014-10-21 15:08:43 -050091
Jon Perrittb1eea512014-09-15 23:27:33 -050092 // Compare the two object's contents to test that the copy worked.
Jon Perritt2b36fa32014-10-24 15:44:23 -050093 th.AssertEquals(t, string(o2Content), string(o1Content))
Jon Perrittf050a4c2014-09-11 15:01:17 -050094
Jon Perrittb1eea512014-09-15 23:27:33 -050095 // Update an object's metadata.
Jon Perritt2b36fa32014-10-24 15:44:23 -050096 updateres := objects.Update(client, cName, oNames[0], &objects.UpdateOpts{Metadata: metadata})
97 th.AssertNoErr(t, updateres.Err)
Jon Perritt2a7797d2014-10-21 15:08:43 -050098
Jon Perrittb1eea512014-09-15 23:27:33 -050099 // Delete the object's metadata after testing.
Jon Perrittf050a4c2014-09-11 15:01:17 -0500100 defer func() {
101 tempMap := make(map[string]string)
102 for k := range metadata {
103 tempMap[k] = ""
104 }
Jon Perritt2b36fa32014-10-24 15:44:23 -0500105 res := objects.Update(client, cName, oNames[0], &objects.UpdateOpts{Metadata: tempMap})
106 th.AssertNoErr(t, res.Err)
Jon Perrittf050a4c2014-09-11 15:01:17 -0500107 }()
108
Jon Perrittb1eea512014-09-15 23:27:33 -0500109 // Retrieve an object's metadata.
Jon Perrittde47eac2014-09-30 15:34:17 -0500110 om, err := objects.Get(client, cName, oNames[0], nil).ExtractMetadata()
Jon Perritt2b36fa32014-10-24 15:44:23 -0500111 th.AssertNoErr(t, err)
Jon Perrittf050a4c2014-09-11 15:01:17 -0500112 for k := range metadata {
113 if om[k] != metadata[strings.Title(k)] {
114 t.Errorf("Expected custom metadata with key: %s", k)
115 return
116 }
117 }
118}