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. |
Ash Wilson | 0036cbf | 2014-10-24 15:29:48 -0400 | [diff] [blame] | 23 | client := newClient() |
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 | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 33 | res = containers.Create(client, cName, nil) |
| 34 | th.AssertNoErr(res.Err) |
| 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 | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 38 | res = containers.Delete(client, cName) |
| 39 | th.AssertNoErr(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 | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 46 | res = objects.Create(client, cName, oNames[i], oContents[i], nil) |
| 47 | th.AssertNoErr(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 | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 52 | res = objects.Delete(client, cName, oNames[i], nil) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 53 | } |
| 54 | }() |
| 55 | |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 56 | ons := make([]string, 0, len(oNames)) |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 57 | 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] | 58 | names, err := objects.ExtractNames(page) |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 59 | th.AssertNoErr(err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 60 | ons = append(ons, names...) |
| 61 | |
| 62 | return true, nil |
| 63 | }) |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 64 | th.AssertNoErr(err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 65 | if len(ons) != len(oNames) { |
| 66 | t.Errorf("Expected %d names and got %d", len(oNames), len(ons)) |
| 67 | return |
| 68 | } |
| 69 | |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 70 | ois := make([]objects.Object, 0, len(oNames)) |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 71 | 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] | 72 | info, err := objects.ExtractInfo(page) |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 73 | th.AssertNoErr(err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 74 | |
| 75 | ois = append(ois, info...) |
| 76 | |
| 77 | return true, nil |
| 78 | }) |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 79 | th.AssertNoErr(err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 80 | if len(ois) != len(oNames) { |
| 81 | t.Errorf("Expected %d containers and got %d", len(oNames), len(ois)) |
| 82 | return |
| 83 | } |
| 84 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 85 | // Copy the contents of one object to another. |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 86 | res = objects.Copy(client, cName, oNames[0], &objects.CopyOpts{Destination: cName + "/" + oNames[1]}) |
| 87 | th.AssertNoErr(res.Err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 88 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 89 | // Download one of the objects that was created above. |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 90 | o1Content, err := objects.Download(client, cName, oNames[0], nil).ExtractContent() |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 91 | th.AssertNoErr(err) |
| 92 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 93 | // Download the another object that was create above. |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 94 | o2Content, err := objects.Download(client, cName, oNames[1], nil).ExtractContent() |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 95 | th.AssertNoErr(err) |
| 96 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 97 | // Compare the two object's contents to test that the copy worked. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 98 | if string(o2Content) != string(o1Content) { |
| 99 | t.Errorf("Copy failed. Expected\n%s\nand got\n%s", string(o1Content), string(o2Content)) |
| 100 | return |
| 101 | } |
| 102 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 103 | // Update an object's metadata. |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 104 | res = objects.Update(client, cName, oNames[0], &objects.UpdateOpts{Metadata: metadata}) |
| 105 | th.AssertNoErr(res.Err) |
| 106 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 107 | // Delete the object's metadata after testing. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 108 | defer func() { |
| 109 | tempMap := make(map[string]string) |
| 110 | for k := range metadata { |
| 111 | tempMap[k] = "" |
| 112 | } |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 113 | res = objects.Update(client, cName, oNames[0], &objects.UpdateOpts{Metadata: tempMap}) |
| 114 | th.AssertNoErr(res.Err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 115 | }() |
| 116 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 117 | // Retrieve an object's metadata. |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 118 | om, err := objects.Get(client, cName, oNames[0], nil).ExtractMetadata() |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 119 | th.AssertNoErr(err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 120 | for k := range metadata { |
| 121 | if om[k] != metadata[strings.Title(k)] { |
| 122 | t.Errorf("Expected custom metadata with key: %s", k) |
| 123 | return |
| 124 | } |
| 125 | } |
| 126 | } |