blob: 4c4736737d825dacb23c8c6d469310c0dd350d94 [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.
Ash Wilson0036cbf2014-10-24 15:29:48 -040023 client := newClient()
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 Perritt2a7797d2014-10-21 15:08:43 -050033 res = containers.Create(client, cName, nil)
34 th.AssertNoErr(res.Err)
35
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 Perritt2a7797d2014-10-21 15:08:43 -050038 res = containers.Delete(client, cName)
39 th.AssertNoErr(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 Perritt2a7797d2014-10-21 15:08:43 -050046 res = objects.Create(client, cName, oNames[i], oContents[i], nil)
47 th.AssertNoErr(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 Perritt2a7797d2014-10-21 15:08:43 -050052 res = objects.Delete(client, cName, oNames[i], nil)
Jon Perrittf050a4c2014-09-11 15:01:17 -050053 }
54 }()
55
Jon Perrittf050a4c2014-09-11 15:01:17 -050056 ons := make([]string, 0, len(oNames))
Jon Perrittde47eac2014-09-30 15:34:17 -050057 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 -050058 names, err := objects.ExtractNames(page)
Jon Perritt2a7797d2014-10-21 15:08:43 -050059 th.AssertNoErr(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050060 ons = append(ons, names...)
61
62 return true, nil
63 })
Jon Perritt2a7797d2014-10-21 15:08:43 -050064 th.AssertNoErr(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050065 if len(ons) != len(oNames) {
66 t.Errorf("Expected %d names and got %d", len(oNames), len(ons))
67 return
68 }
69
Jon Perrittf050a4c2014-09-11 15:01:17 -050070 ois := make([]objects.Object, 0, len(oNames))
Jon Perrittde47eac2014-09-30 15:34:17 -050071 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 -050072 info, err := objects.ExtractInfo(page)
Jon Perritt2a7797d2014-10-21 15:08:43 -050073 th.AssertNoErr(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050074
75 ois = append(ois, info...)
76
77 return true, nil
78 })
Jon Perritt2a7797d2014-10-21 15:08:43 -050079 th.AssertNoErr(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050080 if len(ois) != len(oNames) {
81 t.Errorf("Expected %d containers and got %d", len(oNames), len(ois))
82 return
83 }
84
Jon Perrittb1eea512014-09-15 23:27:33 -050085 // Copy the contents of one object to another.
Jon Perritt2a7797d2014-10-21 15:08:43 -050086 res = objects.Copy(client, cName, oNames[0], &objects.CopyOpts{Destination: cName + "/" + oNames[1]})
87 th.AssertNoErr(res.Err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050088
Jon Perrittb1eea512014-09-15 23:27:33 -050089 // Download one of the objects that was created above.
Jon Perrittde47eac2014-09-30 15:34:17 -050090 o1Content, err := objects.Download(client, cName, oNames[0], nil).ExtractContent()
Jon Perritt2a7797d2014-10-21 15:08:43 -050091 th.AssertNoErr(err)
92
Jon Perrittb1eea512014-09-15 23:27:33 -050093 // Download the another object that was create above.
Jon Perrittde47eac2014-09-30 15:34:17 -050094 o2Content, err := objects.Download(client, cName, oNames[1], nil).ExtractContent()
Jon Perritt2a7797d2014-10-21 15:08:43 -050095 th.AssertNoErr(err)
96
Jon Perrittb1eea512014-09-15 23:27:33 -050097 // Compare the two object's contents to test that the copy worked.
Jon Perrittf050a4c2014-09-11 15:01:17 -050098 if string(o2Content) != string(o1Content) {
99 t.Errorf("Copy failed. Expected\n%s\nand got\n%s", string(o1Content), string(o2Content))
100 return
101 }
102
Jon Perrittb1eea512014-09-15 23:27:33 -0500103 // Update an object's metadata.
Jon Perritt2a7797d2014-10-21 15:08:43 -0500104 res = objects.Update(client, cName, oNames[0], &objects.UpdateOpts{Metadata: metadata})
105 th.AssertNoErr(res.Err)
106
Jon Perrittb1eea512014-09-15 23:27:33 -0500107 // Delete the object's metadata after testing.
Jon Perrittf050a4c2014-09-11 15:01:17 -0500108 defer func() {
109 tempMap := make(map[string]string)
110 for k := range metadata {
111 tempMap[k] = ""
112 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500113 res = objects.Update(client, cName, oNames[0], &objects.UpdateOpts{Metadata: tempMap})
114 th.AssertNoErr(res.Err)
Jon Perrittf050a4c2014-09-11 15:01:17 -0500115 }()
116
Jon Perrittb1eea512014-09-15 23:27:33 -0500117 // Retrieve an object's metadata.
Jon Perrittde47eac2014-09-30 15:34:17 -0500118 om, err := objects.Get(client, cName, oNames[0], nil).ExtractMetadata()
Jon Perritt2a7797d2014-10-21 15:08:43 -0500119 th.AssertNoErr(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -0500120 for k := range metadata {
121 if om[k] != metadata[strings.Title(k)] {
122 t.Errorf("Expected custom metadata with key: %s", k)
123 return
124 }
125 }
126}