blob: 14540635458af58e7e36c038de8d625900f9f555 [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 Perrittf050a4c2014-09-11 15:01:17 -050023 client, err := newClient()
Jon Perritt2a7797d2014-10-21 15:08:43 -050024 th.AssertNoErr(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050025
Jon Perrittb1eea512014-09-15 23:27:33 -050026 // Make a slice of length numObjects to hold the random object names.
Jon Perrittf050a4c2014-09-11 15:01:17 -050027 oNames := make([]string, numObjects)
28 for i := 0; i < len(oNames); i++ {
29 oNames[i] = tools.RandomString("test-object-", 8)
30 }
31
Jon Perrittb1eea512014-09-15 23:27:33 -050032 // Create a container to hold the test objects.
Jon Perrittf050a4c2014-09-11 15:01:17 -050033 cName := tools.RandomString("test-container-", 8)
Jon Perritt2a7797d2014-10-21 15:08:43 -050034 res = containers.Create(client, cName, nil)
35 th.AssertNoErr(res.Err)
36
Jon Perrittb1eea512014-09-15 23:27:33 -050037 // Defer deletion of the container until after testing.
Jon Perrittf050a4c2014-09-11 15:01:17 -050038 defer func() {
Jon Perritt2a7797d2014-10-21 15:08:43 -050039 res = containers.Delete(client, cName)
40 th.AssertNoErr(res.Err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050041 }()
42
Jon Perrittb1eea512014-09-15 23:27:33 -050043 // Create a slice of buffers to hold the test object content.
Jon Perrittf050a4c2014-09-11 15:01:17 -050044 oContents := make([]*bytes.Buffer, numObjects)
45 for i := 0; i < numObjects; i++ {
46 oContents[i] = bytes.NewBuffer([]byte(tools.RandomString("", 10)))
Jon Perritt2a7797d2014-10-21 15:08:43 -050047 res = objects.Create(client, cName, oNames[i], oContents[i], nil)
48 th.AssertNoErr(res.Err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050049 }
Jon Perrittb1eea512014-09-15 23:27:33 -050050 // Delete the objects after testing.
Jon Perrittf050a4c2014-09-11 15:01:17 -050051 defer func() {
52 for i := 0; i < numObjects; i++ {
Jon Perritt2a7797d2014-10-21 15:08:43 -050053 res = objects.Delete(client, cName, oNames[i], nil)
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 Perrittde47eac2014-09-30 15:34:17 -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 Perritt2a7797d2014-10-21 15:08:43 -050060 th.AssertNoErr(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050061 ons = append(ons, names...)
62
63 return true, nil
64 })
Jon Perritt2a7797d2014-10-21 15:08:43 -050065 th.AssertNoErr(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050066 if len(ons) != len(oNames) {
67 t.Errorf("Expected %d names and got %d", len(oNames), len(ons))
68 return
69 }
70
Jon Perrittf050a4c2014-09-11 15:01:17 -050071 ois := make([]objects.Object, 0, len(oNames))
Jon Perrittde47eac2014-09-30 15:34:17 -050072 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 -050073 info, err := objects.ExtractInfo(page)
Jon Perritt2a7797d2014-10-21 15:08:43 -050074 th.AssertNoErr(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050075
76 ois = append(ois, info...)
77
78 return true, nil
79 })
Jon Perritt2a7797d2014-10-21 15:08:43 -050080 th.AssertNoErr(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050081 if len(ois) != len(oNames) {
82 t.Errorf("Expected %d containers and got %d", len(oNames), len(ois))
83 return
84 }
85
Jon Perrittb1eea512014-09-15 23:27:33 -050086 // Copy the contents of one object to another.
Jon Perritt2a7797d2014-10-21 15:08:43 -050087 res = objects.Copy(client, cName, oNames[0], &objects.CopyOpts{Destination: cName + "/" + oNames[1]})
88 th.AssertNoErr(res.Err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050089
Jon Perrittb1eea512014-09-15 23:27:33 -050090 // Download one of the objects that was created above.
Jon Perrittde47eac2014-09-30 15:34:17 -050091 o1Content, err := objects.Download(client, cName, oNames[0], nil).ExtractContent()
Jon Perritt2a7797d2014-10-21 15:08:43 -050092 th.AssertNoErr(err)
93
Jon Perrittb1eea512014-09-15 23:27:33 -050094 // Download the another object that was create above.
Jon Perrittde47eac2014-09-30 15:34:17 -050095 o2Content, err := objects.Download(client, cName, oNames[1], nil).ExtractContent()
Jon Perritt2a7797d2014-10-21 15:08:43 -050096 th.AssertNoErr(err)
97
Jon Perrittb1eea512014-09-15 23:27:33 -050098 // Compare the two object's contents to test that the copy worked.
Jon Perrittf050a4c2014-09-11 15:01:17 -050099 if string(o2Content) != string(o1Content) {
100 t.Errorf("Copy failed. Expected\n%s\nand got\n%s", string(o1Content), string(o2Content))
101 return
102 }
103
Jon Perrittb1eea512014-09-15 23:27:33 -0500104 // Update an object's metadata.
Jon Perritt2a7797d2014-10-21 15:08:43 -0500105 res = objects.Update(client, cName, oNames[0], &objects.UpdateOpts{Metadata: metadata})
106 th.AssertNoErr(res.Err)
107
Jon Perrittb1eea512014-09-15 23:27:33 -0500108 // Delete the object's metadata after testing.
Jon Perrittf050a4c2014-09-11 15:01:17 -0500109 defer func() {
110 tempMap := make(map[string]string)
111 for k := range metadata {
112 tempMap[k] = ""
113 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500114 res = objects.Update(client, cName, oNames[0], &objects.UpdateOpts{Metadata: tempMap})
115 th.AssertNoErr(res.Err)
Jon Perrittf050a4c2014-09-11 15:01:17 -0500116 }()
117
Jon Perrittb1eea512014-09-15 23:27:33 -0500118 // Retrieve an object's metadata.
Jon Perrittde47eac2014-09-30 15:34:17 -0500119 om, err := objects.Get(client, cName, oNames[0], nil).ExtractMetadata()
Jon Perritt2a7797d2014-10-21 15:08:43 -0500120 th.AssertNoErr(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -0500121 for k := range metadata {
122 if om[k] != metadata[strings.Title(k)] {
123 t.Errorf("Expected custom metadata with key: %s", k)
124 return
125 }
126 }
127}