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 | 10a7ec1 | 2014-10-27 11:29:33 -0500 | [diff] [blame] | 33 | header, err := containers.Create(client, cName, nil).ExtractHeader() |
| 34 | th.AssertNoErr(t, err) |
| 35 | t.Logf("Create object headers: %+v\n", header) |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 36 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 37 | // Defer deletion of the container until after testing. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 38 | defer func() { |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame] | 39 | res := containers.Delete(client, cName) |
| 40 | th.AssertNoErr(t, res.Err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 41 | }() |
| 42 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 43 | // Create a slice of buffers to hold the test object content. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 44 | oContents := make([]*bytes.Buffer, numObjects) |
| 45 | for i := 0; i < numObjects; i++ { |
| 46 | oContents[i] = bytes.NewBuffer([]byte(tools.RandomString("", 10))) |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame] | 47 | res := objects.Create(client, cName, oNames[i], oContents[i], nil) |
| 48 | th.AssertNoErr(t, res.Err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 49 | } |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 50 | // Delete the objects after testing. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 51 | defer func() { |
| 52 | for i := 0; i < numObjects; i++ { |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame] | 53 | res := objects.Delete(client, cName, oNames[i], nil) |
| 54 | th.AssertNoErr(t, res.Err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 55 | } |
| 56 | }() |
| 57 | |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 58 | ons := make([]string, 0, len(oNames)) |
Jon Perritt | 10a7ec1 | 2014-10-27 11:29:33 -0500 | [diff] [blame] | 59 | 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] | 60 | names, err := objects.ExtractNames(page) |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame] | 61 | th.AssertNoErr(t, err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 62 | ons = append(ons, names...) |
| 63 | |
| 64 | return true, nil |
| 65 | }) |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame] | 66 | th.AssertNoErr(t, err) |
| 67 | th.AssertEquals(t, len(ons), len(oNames)) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 68 | |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 69 | ois := make([]objects.Object, 0, len(oNames)) |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 70 | 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] | 71 | info, err := objects.ExtractInfo(page) |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame] | 72 | th.AssertNoErr(t, err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 73 | |
| 74 | ois = append(ois, info...) |
| 75 | |
| 76 | return true, nil |
| 77 | }) |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame] | 78 | th.AssertNoErr(t, err) |
| 79 | th.AssertEquals(t, len(ois), len(oNames)) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 80 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 81 | // Copy the contents of one object to another. |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame] | 82 | copyres := objects.Copy(client, cName, oNames[0], &objects.CopyOpts{Destination: cName + "/" + oNames[1]}) |
| 83 | th.AssertNoErr(t, copyres.Err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 84 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 85 | // Download one of the objects that was created above. |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 86 | o1Content, err := objects.Download(client, cName, oNames[0], nil).ExtractContent() |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame] | 87 | th.AssertNoErr(t, err) |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 88 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 89 | // Download the another object that was create above. |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 90 | o2Content, err := objects.Download(client, cName, oNames[1], nil).ExtractContent() |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame] | 91 | th.AssertNoErr(t, err) |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 92 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 93 | // Compare the two object's contents to test that the copy worked. |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame] | 94 | th.AssertEquals(t, string(o2Content), string(o1Content)) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 95 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 96 | // Update an object's metadata. |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame] | 97 | updateres := objects.Update(client, cName, oNames[0], &objects.UpdateOpts{Metadata: metadata}) |
| 98 | th.AssertNoErr(t, updateres.Err) |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 99 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 100 | // Delete the object's metadata after testing. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 101 | defer func() { |
| 102 | tempMap := make(map[string]string) |
| 103 | for k := range metadata { |
| 104 | tempMap[k] = "" |
| 105 | } |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame] | 106 | res := objects.Update(client, cName, oNames[0], &objects.UpdateOpts{Metadata: tempMap}) |
| 107 | th.AssertNoErr(t, res.Err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 108 | }() |
| 109 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 110 | // Retrieve an object's metadata. |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 111 | om, err := objects.Get(client, cName, oNames[0], nil).ExtractMetadata() |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame] | 112 | th.AssertNoErr(t, err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 113 | 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 | } |