Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 1 | // +build acceptance |
| 2 | |
| 3 | package v1 |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/rackspace/gophercloud/acceptance/tools" |
Jon Perritt | a9caabd | 2014-09-30 13:02:34 -0500 | [diff] [blame] | 11 | "github.com/rackspace/gophercloud/openstack/objectstorage/v1/containers" |
| 12 | "github.com/rackspace/gophercloud/openstack/objectstorage/v1/objects" |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 13 | "github.com/rackspace/gophercloud/pagination" |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 14 | th "github.com/rackspace/gophercloud/testhelper" |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 15 | ) |
| 16 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 17 | // numObjects is the number of objects to create for testing. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 18 | var numObjects = 2 |
| 19 | |
| 20 | func TestObjects(t *testing.T) { |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 21 | // Create a provider client for executing the HTTP request. |
| 22 | // See common.go for more information. |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame^] | 23 | client := newClient(t) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 24 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 25 | // Make a slice of length numObjects to hold the random object names. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 26 | oNames := make([]string, numObjects) |
| 27 | for i := 0; i < len(oNames); i++ { |
| 28 | oNames[i] = tools.RandomString("test-object-", 8) |
| 29 | } |
| 30 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 31 | // Create a container to hold the test objects. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 32 | cName := tools.RandomString("test-container-", 8) |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame^] | 33 | createres := containers.Create(client, cName, nil) |
| 34 | th.AssertNoErr(t, createres.Err) |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 35 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 36 | // Defer deletion of the container until after testing. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 37 | defer func() { |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame^] | 38 | res := containers.Delete(client, cName) |
| 39 | th.AssertNoErr(t, res.Err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 40 | }() |
| 41 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 42 | // Create a slice of buffers to hold the test object content. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 43 | oContents := make([]*bytes.Buffer, numObjects) |
| 44 | for i := 0; i < numObjects; i++ { |
| 45 | oContents[i] = bytes.NewBuffer([]byte(tools.RandomString("", 10))) |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame^] | 46 | res := objects.Create(client, cName, oNames[i], oContents[i], nil) |
| 47 | th.AssertNoErr(t, res.Err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 48 | } |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 49 | // Delete the objects after testing. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 50 | defer func() { |
| 51 | for i := 0; i < numObjects; i++ { |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame^] | 52 | res := objects.Delete(client, cName, oNames[i], nil) |
| 53 | th.AssertNoErr(t, res.Err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 54 | } |
| 55 | }() |
| 56 | |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 57 | ons := make([]string, 0, len(oNames)) |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame^] | 58 | err := objects.List(client, cName, &objects.ListOpts{Full: false, Prefix: "test-object-"}).EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 59 | names, err := objects.ExtractNames(page) |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame^] | 60 | th.AssertNoErr(t, err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 61 | ons = append(ons, names...) |
| 62 | |
| 63 | return true, nil |
| 64 | }) |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame^] | 65 | th.AssertNoErr(t, err) |
| 66 | th.AssertEquals(t, len(ons), len(oNames)) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 67 | |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 68 | ois := make([]objects.Object, 0, len(oNames)) |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 69 | err = objects.List(client, cName, &objects.ListOpts{Full: true, Prefix: "test-object-"}).EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 70 | info, err := objects.ExtractInfo(page) |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame^] | 71 | th.AssertNoErr(t, err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 72 | |
| 73 | ois = append(ois, info...) |
| 74 | |
| 75 | return true, nil |
| 76 | }) |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame^] | 77 | th.AssertNoErr(t, err) |
| 78 | th.AssertEquals(t, len(ois), len(oNames)) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 79 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 80 | // Copy the contents of one object to another. |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame^] | 81 | copyres := objects.Copy(client, cName, oNames[0], &objects.CopyOpts{Destination: cName + "/" + oNames[1]}) |
| 82 | th.AssertNoErr(t, copyres.Err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 83 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 84 | // Download one of the objects that was created above. |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 85 | o1Content, err := objects.Download(client, cName, oNames[0], nil).ExtractContent() |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame^] | 86 | th.AssertNoErr(t, err) |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 87 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 88 | // Download the another object that was create above. |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 89 | o2Content, err := objects.Download(client, cName, oNames[1], nil).ExtractContent() |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame^] | 90 | th.AssertNoErr(t, err) |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 91 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 92 | // Compare the two object's contents to test that the copy worked. |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame^] | 93 | th.AssertEquals(t, string(o2Content), string(o1Content)) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 94 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 95 | // Update an object's metadata. |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame^] | 96 | updateres := objects.Update(client, cName, oNames[0], &objects.UpdateOpts{Metadata: metadata}) |
| 97 | th.AssertNoErr(t, updateres.Err) |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 98 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 99 | // Delete the object's metadata after testing. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 100 | defer func() { |
| 101 | tempMap := make(map[string]string) |
| 102 | for k := range metadata { |
| 103 | tempMap[k] = "" |
| 104 | } |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame^] | 105 | res := objects.Update(client, cName, oNames[0], &objects.UpdateOpts{Metadata: tempMap}) |
| 106 | th.AssertNoErr(t, res.Err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 107 | }() |
| 108 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 109 | // Retrieve an object's metadata. |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 110 | om, err := objects.Get(client, cName, oNames[0], nil).ExtractMetadata() |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame^] | 111 | th.AssertNoErr(t, err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 112 | 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 | } |