blob: a8de338c3dd627696117224880dceb2fffb5e9bd [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 Perritt10a7ec12014-10-27 11:29:33 -050033 header, err := containers.Create(client, cName, nil).ExtractHeader()
34 th.AssertNoErr(t, err)
35 t.Logf("Create object headers: %+v\n", header)
Jon Perritt2a7797d2014-10-21 15:08:43 -050036
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 Perritt2b36fa32014-10-24 15:44:23 -050039 res := containers.Delete(client, cName)
40 th.AssertNoErr(t, 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 Perritt2b36fa32014-10-24 15:44:23 -050047 res := objects.Create(client, cName, oNames[i], oContents[i], nil)
48 th.AssertNoErr(t, 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 Perritt2b36fa32014-10-24 15:44:23 -050053 res := objects.Delete(client, cName, oNames[i], nil)
54 th.AssertNoErr(t, res.Err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050055 }
56 }()
57
Jon Perrittf050a4c2014-09-11 15:01:17 -050058 ons := make([]string, 0, len(oNames))
Jon Perritt10a7ec12014-10-27 11:29:33 -050059 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 -050060 names, err := objects.ExtractNames(page)
Jon Perritt2b36fa32014-10-24 15:44:23 -050061 th.AssertNoErr(t, err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050062 ons = append(ons, names...)
63
64 return true, nil
65 })
Jon Perritt2b36fa32014-10-24 15:44:23 -050066 th.AssertNoErr(t, err)
67 th.AssertEquals(t, len(ons), len(oNames))
Jon Perrittf050a4c2014-09-11 15:01:17 -050068
Jon Perrittf050a4c2014-09-11 15:01:17 -050069 ois := make([]objects.Object, 0, len(oNames))
Jon Perrittde47eac2014-09-30 15:34:17 -050070 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 -050071 info, err := objects.ExtractInfo(page)
Jon Perritt2b36fa32014-10-24 15:44:23 -050072 th.AssertNoErr(t, err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050073
74 ois = append(ois, info...)
75
76 return true, nil
77 })
Jon Perritt2b36fa32014-10-24 15:44:23 -050078 th.AssertNoErr(t, err)
79 th.AssertEquals(t, len(ois), len(oNames))
Jon Perrittf050a4c2014-09-11 15:01:17 -050080
Jon Perrittb1eea512014-09-15 23:27:33 -050081 // Copy the contents of one object to another.
Jon Perritt2b36fa32014-10-24 15:44:23 -050082 copyres := objects.Copy(client, cName, oNames[0], &objects.CopyOpts{Destination: cName + "/" + oNames[1]})
83 th.AssertNoErr(t, copyres.Err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050084
Jon Perrittb1eea512014-09-15 23:27:33 -050085 // Download one of the objects that was created above.
Jon Perrittde47eac2014-09-30 15:34:17 -050086 o1Content, err := objects.Download(client, cName, oNames[0], nil).ExtractContent()
Jon Perritt2b36fa32014-10-24 15:44:23 -050087 th.AssertNoErr(t, err)
Jon Perritt2a7797d2014-10-21 15:08:43 -050088
Jon Perrittb1eea512014-09-15 23:27:33 -050089 // Download the another object that was create above.
Jon Perrittde47eac2014-09-30 15:34:17 -050090 o2Content, err := objects.Download(client, cName, oNames[1], nil).ExtractContent()
Jon Perritt2b36fa32014-10-24 15:44:23 -050091 th.AssertNoErr(t, err)
Jon Perritt2a7797d2014-10-21 15:08:43 -050092
Jon Perrittb1eea512014-09-15 23:27:33 -050093 // Compare the two object's contents to test that the copy worked.
Jon Perritt2b36fa32014-10-24 15:44:23 -050094 th.AssertEquals(t, string(o2Content), string(o1Content))
Jon Perrittf050a4c2014-09-11 15:01:17 -050095
Jon Perrittb1eea512014-09-15 23:27:33 -050096 // Update an object's metadata.
Jon Perritt2b36fa32014-10-24 15:44:23 -050097 updateres := objects.Update(client, cName, oNames[0], &objects.UpdateOpts{Metadata: metadata})
98 th.AssertNoErr(t, updateres.Err)
Jon Perritt2a7797d2014-10-21 15:08:43 -050099
Jon Perrittb1eea512014-09-15 23:27:33 -0500100 // Delete the object's metadata after testing.
Jon Perrittf050a4c2014-09-11 15:01:17 -0500101 defer func() {
102 tempMap := make(map[string]string)
103 for k := range metadata {
104 tempMap[k] = ""
105 }
Jon Perritt2b36fa32014-10-24 15:44:23 -0500106 res := objects.Update(client, cName, oNames[0], &objects.UpdateOpts{Metadata: tempMap})
107 th.AssertNoErr(t, res.Err)
Jon Perrittf050a4c2014-09-11 15:01:17 -0500108 }()
109
Jon Perrittb1eea512014-09-15 23:27:33 -0500110 // Retrieve an object's metadata.
Jon Perrittde47eac2014-09-30 15:34:17 -0500111 om, err := objects.Get(client, cName, oNames[0], nil).ExtractMetadata()
Jon Perritt2b36fa32014-10-24 15:44:23 -0500112 th.AssertNoErr(t, err)
Jon Perrittf050a4c2014-09-11 15:01:17 -0500113 for k := range metadata {
114 if om[k] != metadata[strings.Title(k)] {
115 t.Errorf("Expected custom metadata with key: %s", k)
116 return
117 }
118 }
119}