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" |
| 11 | "github.com/rackspace/gophercloud/openstack/storage/v1/containers" |
| 12 | "github.com/rackspace/gophercloud/openstack/storage/v1/objects" |
| 13 | "github.com/rackspace/gophercloud/pagination" |
| 14 | ) |
| 15 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame^] | 16 | // numObjects is the number of objects to create for testing. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 17 | var numObjects = 2 |
| 18 | |
| 19 | func TestObjects(t *testing.T) { |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame^] | 20 | // Create a provider client for executing the HTTP request. |
| 21 | // See common.go for more information. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 22 | client, err := newClient() |
| 23 | if err != nil { |
| 24 | t.Error(err) |
| 25 | return |
| 26 | } |
| 27 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame^] | 28 | // Make a slice of length numObjects to hold the random object names. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 29 | oNames := make([]string, numObjects) |
| 30 | for i := 0; i < len(oNames); i++ { |
| 31 | oNames[i] = tools.RandomString("test-object-", 8) |
| 32 | } |
| 33 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame^] | 34 | // Create a container to hold the test objects. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 35 | cName := tools.RandomString("test-container-", 8) |
| 36 | _, err = containers.Create(client, containers.CreateOpts{ |
| 37 | Name: cName, |
| 38 | }) |
| 39 | if err != nil { |
| 40 | t.Error(err) |
| 41 | return |
| 42 | } |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame^] | 43 | // Defer deletion of the container until after testing. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 44 | defer func() { |
| 45 | err = containers.Delete(client, containers.DeleteOpts{ |
| 46 | Name: cName, |
| 47 | }) |
| 48 | if err != nil { |
| 49 | t.Error(err) |
| 50 | return |
| 51 | } |
| 52 | }() |
| 53 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame^] | 54 | // Create a slice of buffers to hold the test object content. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 55 | oContents := make([]*bytes.Buffer, numObjects) |
| 56 | for i := 0; i < numObjects; i++ { |
| 57 | oContents[i] = bytes.NewBuffer([]byte(tools.RandomString("", 10))) |
| 58 | err = objects.Create(client, objects.CreateOpts{ |
| 59 | Container: cName, |
| 60 | Name: oNames[i], |
| 61 | Content: oContents[i], |
| 62 | }) |
| 63 | if err != nil { |
| 64 | t.Error(err) |
| 65 | return |
| 66 | } |
| 67 | } |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame^] | 68 | // Delete the objects after testing. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 69 | defer func() { |
| 70 | for i := 0; i < numObjects; i++ { |
| 71 | err = objects.Delete(client, objects.DeleteOpts{ |
| 72 | Container: cName, |
| 73 | Name: oNames[i], |
| 74 | }) |
| 75 | } |
| 76 | }() |
| 77 | |
| 78 | pager := objects.List(client, objects.ListOpts{Full: false, Container: cName}) |
| 79 | ons := make([]string, 0, len(oNames)) |
| 80 | err = pager.EachPage(func(page pagination.Page) (bool, error) { |
| 81 | names, err := objects.ExtractNames(page) |
| 82 | if err != nil { |
| 83 | return false, err |
| 84 | } |
| 85 | ons = append(ons, names...) |
| 86 | |
| 87 | return true, nil |
| 88 | }) |
| 89 | if err != nil { |
| 90 | t.Error(err) |
| 91 | return |
| 92 | } |
| 93 | if len(ons) != len(oNames) { |
| 94 | t.Errorf("Expected %d names and got %d", len(oNames), len(ons)) |
| 95 | return |
| 96 | } |
| 97 | |
| 98 | pager = objects.List(client, objects.ListOpts{Full: true, Container: cName}) |
| 99 | ois := make([]objects.Object, 0, len(oNames)) |
| 100 | err = pager.EachPage(func(page pagination.Page) (bool, error) { |
| 101 | info, err := objects.ExtractInfo(page) |
| 102 | if err != nil { |
| 103 | return false, nil |
| 104 | } |
| 105 | |
| 106 | ois = append(ois, info...) |
| 107 | |
| 108 | return true, nil |
| 109 | }) |
| 110 | if err != nil { |
| 111 | t.Error(err) |
| 112 | return |
| 113 | } |
| 114 | if len(ois) != len(oNames) { |
| 115 | t.Errorf("Expected %d containers and got %d", len(oNames), len(ois)) |
| 116 | return |
| 117 | } |
| 118 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame^] | 119 | // Copy the contents of one object to another. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 120 | err = objects.Copy(client, objects.CopyOpts{ |
| 121 | Container: cName, |
| 122 | Name: oNames[0], |
| 123 | NewContainer: cName, |
| 124 | NewName: oNames[1], |
| 125 | }) |
| 126 | if err != nil { |
| 127 | t.Error(err) |
| 128 | return |
| 129 | } |
| 130 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame^] | 131 | // Download one of the objects that was created above. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 132 | dr, err := objects.Download(client, objects.DownloadOpts{ |
| 133 | Container: cName, |
| 134 | Name: oNames[1], |
| 135 | }) |
| 136 | if err != nil { |
| 137 | t.Error(err) |
| 138 | return |
| 139 | } |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame^] | 140 | // Extract the content from the 'Download' response of object. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 141 | o2Content, err := objects.ExtractContent(dr) |
| 142 | if err != nil { |
| 143 | t.Error(err) |
| 144 | } |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame^] | 145 | // Download the another object that was create above. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 146 | dr, err = objects.Download(client, objects.DownloadOpts{ |
| 147 | Container: cName, |
| 148 | Name: oNames[0], |
| 149 | }) |
| 150 | if err != nil { |
| 151 | t.Error(err) |
| 152 | return |
| 153 | } |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame^] | 154 | // Extract the content from the 'Download' response of other object. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 155 | o1Content, err := objects.ExtractContent(dr) |
| 156 | if err != nil { |
| 157 | t.Error(err) |
| 158 | return |
| 159 | } |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame^] | 160 | // Compare the two object's contents to test that the copy worked. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 161 | if string(o2Content) != string(o1Content) { |
| 162 | t.Errorf("Copy failed. Expected\n%s\nand got\n%s", string(o1Content), string(o2Content)) |
| 163 | return |
| 164 | } |
| 165 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame^] | 166 | // Update an object's metadata. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 167 | err = objects.Update(client, objects.UpdateOpts{ |
| 168 | Container: cName, |
| 169 | Name: oNames[0], |
| 170 | Metadata: metadata, |
| 171 | }) |
| 172 | if err != nil { |
| 173 | t.Error(err) |
| 174 | return |
| 175 | } |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame^] | 176 | // Delete the object's metadata after testing. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 177 | defer func() { |
| 178 | tempMap := make(map[string]string) |
| 179 | for k := range metadata { |
| 180 | tempMap[k] = "" |
| 181 | } |
| 182 | err = objects.Update(client, objects.UpdateOpts{ |
| 183 | Container: cName, |
| 184 | Name: oNames[0], |
| 185 | Metadata: tempMap, |
| 186 | }) |
| 187 | if err != nil { |
| 188 | t.Error(err) |
| 189 | return |
| 190 | } |
| 191 | }() |
| 192 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame^] | 193 | // Retrieve an object's metadata. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 194 | gr, err := objects.Get(client, objects.GetOpts{}) |
| 195 | if err != nil { |
| 196 | t.Error(err) |
| 197 | return |
| 198 | } |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame^] | 199 | // Extract the custom metadata from the 'Get' response. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 200 | om := objects.ExtractMetadata(gr) |
| 201 | for k := range metadata { |
| 202 | if om[k] != metadata[strings.Title(k)] { |
| 203 | t.Errorf("Expected custom metadata with key: %s", k) |
| 204 | return |
| 205 | } |
| 206 | } |
| 207 | } |