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 | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 23 | client, err := newClient() |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 24 | th.AssertNoErr(err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 25 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 26 | // Make a slice of length numObjects to hold the random object names. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 27 | oNames := make([]string, numObjects) |
| 28 | for i := 0; i < len(oNames); i++ { |
| 29 | oNames[i] = tools.RandomString("test-object-", 8) |
| 30 | } |
| 31 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 32 | // Create a container to hold the test objects. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 33 | cName := tools.RandomString("test-container-", 8) |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 34 | res = containers.Create(client, cName, nil) |
| 35 | th.AssertNoErr(res.Err) |
| 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 | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 39 | res = containers.Delete(client, cName) |
| 40 | th.AssertNoErr(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 | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 47 | res = objects.Create(client, cName, oNames[i], oContents[i], nil) |
| 48 | th.AssertNoErr(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 | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 53 | res = objects.Delete(client, cName, oNames[i], nil) |
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 | de47eac | 2014-09-30 15:34:17 -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 | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 60 | th.AssertNoErr(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 | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 65 | th.AssertNoErr(err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 66 | if len(ons) != len(oNames) { |
| 67 | t.Errorf("Expected %d names and got %d", len(oNames), len(ons)) |
| 68 | return |
| 69 | } |
| 70 | |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 71 | ois := make([]objects.Object, 0, len(oNames)) |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 72 | 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] | 73 | info, err := objects.ExtractInfo(page) |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 74 | th.AssertNoErr(err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 75 | |
| 76 | ois = append(ois, info...) |
| 77 | |
| 78 | return true, nil |
| 79 | }) |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 80 | th.AssertNoErr(err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 81 | if len(ois) != len(oNames) { |
| 82 | t.Errorf("Expected %d containers and got %d", len(oNames), len(ois)) |
| 83 | return |
| 84 | } |
| 85 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 86 | // Copy the contents of one object to another. |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 87 | res = objects.Copy(client, cName, oNames[0], &objects.CopyOpts{Destination: cName + "/" + oNames[1]}) |
| 88 | th.AssertNoErr(res.Err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 89 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 90 | // Download one of the objects that was created above. |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 91 | o1Content, err := objects.Download(client, cName, oNames[0], nil).ExtractContent() |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 92 | th.AssertNoErr(err) |
| 93 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 94 | // Download the another object that was create above. |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 95 | o2Content, err := objects.Download(client, cName, oNames[1], nil).ExtractContent() |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 96 | th.AssertNoErr(err) |
| 97 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 98 | // Compare the two object's contents to test that the copy worked. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 99 | 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 Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 104 | // Update an object's metadata. |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 105 | res = objects.Update(client, cName, oNames[0], &objects.UpdateOpts{Metadata: metadata}) |
| 106 | th.AssertNoErr(res.Err) |
| 107 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 108 | // Delete the object's metadata after testing. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 109 | defer func() { |
| 110 | tempMap := make(map[string]string) |
| 111 | for k := range metadata { |
| 112 | tempMap[k] = "" |
| 113 | } |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 114 | res = objects.Update(client, cName, oNames[0], &objects.UpdateOpts{Metadata: tempMap}) |
| 115 | th.AssertNoErr(res.Err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 116 | }() |
| 117 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 118 | // Retrieve an object's metadata. |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 119 | om, err := objects.Get(client, cName, oNames[0], nil).ExtractMetadata() |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 120 | th.AssertNoErr(err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 121 | 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 | } |