blob: 5a63a4ca5e5edccbfeab62189d1583935a45c81f [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"
14)
15
Jon Perrittb1eea512014-09-15 23:27:33 -050016// numObjects is the number of objects to create for testing.
Jon Perrittf050a4c2014-09-11 15:01:17 -050017var numObjects = 2
18
19func TestObjects(t *testing.T) {
Jon Perrittb1eea512014-09-15 23:27:33 -050020 // Create a provider client for executing the HTTP request.
21 // See common.go for more information.
Jon Perrittf050a4c2014-09-11 15:01:17 -050022 client, err := newClient()
23 if err != nil {
24 t.Error(err)
25 return
26 }
27
Jon Perrittb1eea512014-09-15 23:27:33 -050028 // Make a slice of length numObjects to hold the random object names.
Jon Perrittf050a4c2014-09-11 15:01:17 -050029 oNames := make([]string, numObjects)
30 for i := 0; i < len(oNames); i++ {
31 oNames[i] = tools.RandomString("test-object-", 8)
32 }
33
Jon Perrittb1eea512014-09-15 23:27:33 -050034 // Create a container to hold the test objects.
Jon Perrittf050a4c2014-09-11 15:01:17 -050035 cName := tools.RandomString("test-container-", 8)
Jon Perritt5db08922014-09-30 21:32:48 -050036 _, err = containers.Create(client, cName, nil).ExtractHeaders()
Jon Perrittf050a4c2014-09-11 15:01:17 -050037 if err != nil {
38 t.Error(err)
39 return
40 }
Jon Perrittb1eea512014-09-15 23:27:33 -050041 // Defer deletion of the container until after testing.
Jon Perrittf050a4c2014-09-11 15:01:17 -050042 defer func() {
Jon Perritt5db08922014-09-30 21:32:48 -050043 _, err = containers.Delete(client, cName).ExtractHeaders()
Jon Perrittf050a4c2014-09-11 15:01:17 -050044 if err != nil {
45 t.Error(err)
46 return
47 }
48 }()
49
Jon Perrittb1eea512014-09-15 23:27:33 -050050 // Create a slice of buffers to hold the test object content.
Jon Perrittf050a4c2014-09-11 15:01:17 -050051 oContents := make([]*bytes.Buffer, numObjects)
52 for i := 0; i < numObjects; i++ {
53 oContents[i] = bytes.NewBuffer([]byte(tools.RandomString("", 10)))
Jon Perritt5db08922014-09-30 21:32:48 -050054 _, err = objects.Create(client, cName, oNames[i], oContents[i], nil).ExtractHeaders()
Jon Perrittf050a4c2014-09-11 15:01:17 -050055 if err != nil {
56 t.Error(err)
57 return
58 }
59 }
Jon Perrittb1eea512014-09-15 23:27:33 -050060 // Delete the objects after testing.
Jon Perrittf050a4c2014-09-11 15:01:17 -050061 defer func() {
62 for i := 0; i < numObjects; i++ {
Jon Perritt5db08922014-09-30 21:32:48 -050063 _, err = objects.Delete(client, cName, oNames[i], nil).ExtractHeaders()
Jon Perrittf050a4c2014-09-11 15:01:17 -050064 }
65 }()
66
Jon Perrittf050a4c2014-09-11 15:01:17 -050067 ons := make([]string, 0, len(oNames))
Jon Perrittde47eac2014-09-30 15:34:17 -050068 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 -050069 names, err := objects.ExtractNames(page)
70 if err != nil {
71 return false, err
72 }
73 ons = append(ons, names...)
74
75 return true, nil
76 })
77 if err != nil {
78 t.Error(err)
79 return
80 }
81 if len(ons) != len(oNames) {
82 t.Errorf("Expected %d names and got %d", len(oNames), len(ons))
83 return
84 }
85
Jon Perrittf050a4c2014-09-11 15:01:17 -050086 ois := make([]objects.Object, 0, len(oNames))
Jon Perrittde47eac2014-09-30 15:34:17 -050087 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 -050088 info, err := objects.ExtractInfo(page)
89 if err != nil {
90 return false, nil
91 }
92
93 ois = append(ois, info...)
94
95 return true, nil
96 })
97 if err != nil {
98 t.Error(err)
99 return
100 }
101 if len(ois) != len(oNames) {
102 t.Errorf("Expected %d containers and got %d", len(oNames), len(ois))
103 return
104 }
105
Jon Perrittb1eea512014-09-15 23:27:33 -0500106 // Copy the contents of one object to another.
Jon Perritt5db08922014-09-30 21:32:48 -0500107 _, err = objects.Copy(client, cName, oNames[0], &objects.CopyOpts{Destination: cName + "/" + oNames[1]}).ExtractHeaders()
Jon Perrittf050a4c2014-09-11 15:01:17 -0500108 if err != nil {
109 t.Error(err)
110 return
111 }
112
Jon Perrittb1eea512014-09-15 23:27:33 -0500113 // Download one of the objects that was created above.
Jon Perrittde47eac2014-09-30 15:34:17 -0500114 o1Content, err := objects.Download(client, cName, oNames[0], nil).ExtractContent()
Jon Perrittf050a4c2014-09-11 15:01:17 -0500115 if err != nil {
116 t.Error(err)
117 return
118 }
Jon Perrittb1eea512014-09-15 23:27:33 -0500119 // Download the another object that was create above.
Jon Perrittde47eac2014-09-30 15:34:17 -0500120 o2Content, err := objects.Download(client, cName, oNames[1], nil).ExtractContent()
Jon Perrittf050a4c2014-09-11 15:01:17 -0500121 if err != nil {
122 t.Error(err)
123 return
124 }
Jon Perrittb1eea512014-09-15 23:27:33 -0500125 // Compare the two object's contents to test that the copy worked.
Jon Perrittf050a4c2014-09-11 15:01:17 -0500126 if string(o2Content) != string(o1Content) {
127 t.Errorf("Copy failed. Expected\n%s\nand got\n%s", string(o1Content), string(o2Content))
128 return
129 }
130
Jon Perrittb1eea512014-09-15 23:27:33 -0500131 // Update an object's metadata.
Jon Perritt5db08922014-09-30 21:32:48 -0500132 _, err = objects.Update(client, cName, oNames[0], &objects.UpdateOpts{Metadata: metadata}).ExtractHeaders()
Jon Perrittf050a4c2014-09-11 15:01:17 -0500133 if err != nil {
134 t.Error(err)
135 return
136 }
Jon Perrittb1eea512014-09-15 23:27:33 -0500137 // Delete the object's metadata after testing.
Jon Perrittf050a4c2014-09-11 15:01:17 -0500138 defer func() {
139 tempMap := make(map[string]string)
140 for k := range metadata {
141 tempMap[k] = ""
142 }
Jon Perritt5db08922014-09-30 21:32:48 -0500143 _, err = objects.Update(client, cName, oNames[0], &objects.UpdateOpts{Metadata: tempMap}).ExtractHeaders()
Jon Perrittf050a4c2014-09-11 15:01:17 -0500144 if err != nil {
145 t.Error(err)
146 return
147 }
148 }()
149
Jon Perrittb1eea512014-09-15 23:27:33 -0500150 // Retrieve an object's metadata.
Jon Perrittde47eac2014-09-30 15:34:17 -0500151 om, err := objects.Get(client, cName, oNames[0], nil).ExtractMetadata()
Jon Perrittf050a4c2014-09-11 15:01:17 -0500152 if err != nil {
153 t.Error(err)
154 return
155 }
Jon Perrittf050a4c2014-09-11 15:01:17 -0500156 for k := range metadata {
157 if om[k] != metadata[strings.Title(k)] {
158 t.Errorf("Expected custom metadata with key: %s", k)
159 return
160 }
161 }
162}