Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 1 | // +build acceptance |
| 2 | |
| 3 | package openstack |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud/acceptance/tools" |
Jon Perritt | b6b1d02 | 2014-04-14 21:50:45 -0500 | [diff] [blame] | 8 | storage "github.com/rackspace/gophercloud/openstack/storage/v1" |
| 9 | "github.com/rackspace/gophercloud/openstack/storage/v1/accounts" |
| 10 | "github.com/rackspace/gophercloud/openstack/storage/v1/containers" |
| 11 | "github.com/rackspace/gophercloud/openstack/storage/v1/objects" |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 12 | "github.com/rackspace/gophercloud/openstack/utils" |
Jon Perritt | a8c3b81 | 2014-08-19 22:02:31 -0500 | [diff] [blame] | 13 | "os" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 14 | "strings" |
| 15 | "testing" |
| 16 | ) |
| 17 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 18 | var metadata = map[string]string{"gopher": "cloud"} |
| 19 | var numContainers = 2 |
| 20 | var numObjects = 2 |
| 21 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 22 | func newClient() (*storage.Client, error) { |
| 23 | ao, err := utils.AuthOptions() |
| 24 | if err != nil { |
| 25 | return nil, err |
| 26 | } |
| 27 | |
Jon Perritt | 5f4b5c2 | 2014-08-27 11:44:07 -0500 | [diff] [blame^] | 28 | client, err := utils.NewClient(ao, utils.EndpointOpts{ |
Jon Perritt | a8c3b81 | 2014-08-19 22:02:31 -0500 | [diff] [blame] | 29 | Region: os.Getenv("OS_REGION_NAME"), |
| 30 | Type: "object-store", |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 31 | }) |
| 32 | if err != nil { |
| 33 | return nil, err |
| 34 | } |
| 35 | |
| 36 | return storage.NewClient(client.Endpoint, client.Authority, client.Options), nil |
| 37 | } |
| 38 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 39 | func TestAccount(t *testing.T) { |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 40 | client, err := newClient() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 41 | if err != nil { |
| 42 | t.Error(err) |
| 43 | return |
| 44 | } |
| 45 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 46 | err = accounts.Update(client, accounts.UpdateOpts{ |
| 47 | Metadata: metadata, |
| 48 | }) |
| 49 | if err != nil { |
| 50 | t.Error(err) |
| 51 | return |
| 52 | } |
| 53 | defer func() { |
| 54 | tempMap := make(map[string]string) |
| 55 | for k := range metadata { |
| 56 | tempMap[k] = "" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 57 | } |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 58 | err = accounts.Update(client, accounts.UpdateOpts{ |
| 59 | Metadata: tempMap, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 60 | }) |
| 61 | if err != nil { |
| 62 | t.Error(err) |
| 63 | return |
| 64 | } |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 65 | }() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 66 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 67 | gr, err := accounts.Get(client, accounts.GetOpts{}) |
| 68 | if err != nil { |
| 69 | t.Error(err) |
| 70 | return |
| 71 | } |
| 72 | am := accounts.ExtractMetadata(gr) |
| 73 | for k := range metadata { |
| 74 | if am[k] != metadata[strings.Title(k)] { |
| 75 | t.Errorf("Expected custom metadata with key: %s", k) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 76 | return |
| 77 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestContainers(t *testing.T) { |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 82 | client, err := newClient() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 83 | if err != nil { |
| 84 | t.Error(err) |
| 85 | return |
| 86 | } |
| 87 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 88 | cNames := make([]string, numContainers) |
| 89 | for i := 0; i < numContainers; i++ { |
| 90 | cNames[i] = tools.RandomString("test-container-", 8) |
| 91 | } |
| 92 | |
| 93 | for i := 0; i < len(cNames); i++ { |
| 94 | _, err := containers.Create(client, containers.CreateOpts{ |
| 95 | Name: cNames[i], |
| 96 | }) |
| 97 | if err != nil { |
| 98 | t.Error(err) |
| 99 | return |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 100 | } |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 101 | } |
| 102 | defer func() { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 103 | for i := 0; i < len(cNames); i++ { |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 104 | err = containers.Delete(client, containers.DeleteOpts{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 105 | Name: cNames[i], |
| 106 | }) |
| 107 | if err != nil { |
| 108 | t.Error(err) |
| 109 | return |
| 110 | } |
| 111 | } |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 112 | }() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 113 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 114 | lr, err := containers.List(client, containers.ListOpts{ |
| 115 | Full: false, |
| 116 | }) |
| 117 | if err != nil { |
| 118 | t.Error(err) |
| 119 | return |
| 120 | } |
| 121 | cns, err := containers.ExtractNames(lr) |
| 122 | if err != nil { |
| 123 | t.Error(err) |
| 124 | return |
| 125 | } |
| 126 | if len(cns) != len(cNames) { |
| 127 | t.Errorf("Expected %d names and got %d", len(cNames), len(cns)) |
| 128 | return |
| 129 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 130 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 131 | lr, err = containers.List(client, containers.ListOpts{ |
| 132 | Full: true, |
| 133 | }) |
| 134 | if err != nil { |
| 135 | t.Error(err) |
| 136 | return |
| 137 | } |
| 138 | cis, err := containers.ExtractInfo(lr) |
| 139 | if err != nil { |
| 140 | t.Error(err) |
| 141 | return |
| 142 | } |
| 143 | if len(cis) != len(cNames) { |
| 144 | t.Errorf("Expected %d containers and got %d", len(cNames), len(cis)) |
| 145 | return |
| 146 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 147 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 148 | err = containers.Update(client, containers.UpdateOpts{ |
| 149 | Name: cNames[0], |
| 150 | Metadata: metadata, |
| 151 | }) |
| 152 | if err != nil { |
| 153 | t.Error(err) |
| 154 | return |
| 155 | } |
| 156 | defer func() { |
| 157 | tempMap := make(map[string]string) |
| 158 | for k := range metadata { |
| 159 | tempMap[k] = "" |
| 160 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 161 | err = containers.Update(client, containers.UpdateOpts{ |
| 162 | Name: cNames[0], |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 163 | Metadata: tempMap, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 164 | }) |
| 165 | if err != nil { |
| 166 | t.Error(err) |
| 167 | return |
| 168 | } |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 169 | }() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 170 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 171 | gr, err := containers.Get(client, containers.GetOpts{}) |
| 172 | if err != nil { |
| 173 | t.Error(err) |
| 174 | return |
| 175 | } |
| 176 | cm := containers.ExtractMetadata(gr) |
| 177 | for k := range metadata { |
| 178 | if cm[k] != metadata[strings.Title(k)] { |
| 179 | t.Errorf("Expected custom metadata with key: %s", k) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 180 | return |
| 181 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | func TestObjects(t *testing.T) { |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 186 | client, err := newClient() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 187 | if err != nil { |
| 188 | t.Error(err) |
| 189 | return |
| 190 | } |
| 191 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 192 | oNames := make([]string, numObjects) |
| 193 | for i := 0; i < len(oNames); i++ { |
| 194 | oNames[i] = tools.RandomString("test-object-", 8) |
| 195 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 196 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 197 | cName := tools.RandomString("test-container-", 8) |
| 198 | _, err = containers.Create(client, containers.CreateOpts{ |
| 199 | Name: cName, |
| 200 | }) |
| 201 | if err != nil { |
| 202 | t.Error(err) |
| 203 | return |
| 204 | } |
| 205 | defer func() { |
| 206 | err = containers.Delete(client, containers.DeleteOpts{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 207 | Name: cName, |
| 208 | }) |
| 209 | if err != nil { |
| 210 | t.Error(err) |
| 211 | return |
| 212 | } |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 213 | }() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 214 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 215 | oContents := make([]*bytes.Buffer, numObjects) |
| 216 | for i := 0; i < numObjects; i++ { |
| 217 | oContents[i] = bytes.NewBuffer([]byte(tools.RandomString("", 10))) |
| 218 | err = objects.Create(client, objects.CreateOpts{ |
| 219 | Container: cName, |
| 220 | Name: oNames[i], |
| 221 | Content: oContents[i], |
| 222 | }) |
| 223 | if err != nil { |
| 224 | t.Error(err) |
| 225 | return |
| 226 | } |
| 227 | } |
| 228 | defer func() { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 229 | for i := 0; i < numObjects; i++ { |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 230 | err = objects.Delete(client, objects.DeleteOpts{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 231 | Container: cName, |
| 232 | Name: oNames[i], |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 233 | }) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 234 | } |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 235 | }() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 236 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 237 | lr, err := objects.List(client, objects.ListOpts{ |
| 238 | Full: false, |
| 239 | Container: cName, |
| 240 | }) |
| 241 | if err != nil { |
| 242 | t.Error(err) |
| 243 | return |
| 244 | } |
| 245 | ons, err := objects.ExtractNames(lr) |
| 246 | if err != nil { |
| 247 | t.Error(err) |
| 248 | return |
| 249 | } |
| 250 | if len(ons) != len(oNames) { |
| 251 | t.Errorf("Expected %d names and got %d", len(oNames), len(ons)) |
| 252 | return |
| 253 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 254 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 255 | lr, err = objects.List(client, objects.ListOpts{ |
| 256 | Full: true, |
| 257 | Container: cName, |
| 258 | }) |
| 259 | if err != nil { |
| 260 | t.Error(err) |
| 261 | return |
| 262 | } |
| 263 | ois, err := objects.ExtractInfo(lr) |
| 264 | if err != nil { |
| 265 | t.Error(err) |
| 266 | return |
| 267 | } |
| 268 | if len(ois) != len(oNames) { |
| 269 | t.Errorf("Expected %d containers and got %d", len(oNames), len(ois)) |
| 270 | return |
| 271 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 272 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 273 | err = objects.Copy(client, objects.CopyOpts{ |
| 274 | Container: cName, |
| 275 | Name: oNames[0], |
| 276 | NewContainer: cName, |
| 277 | NewName: oNames[1], |
| 278 | }) |
| 279 | if err != nil { |
| 280 | t.Error(err) |
| 281 | return |
| 282 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 283 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 284 | dr, err := objects.Download(client, objects.DownloadOpts{ |
| 285 | Container: cName, |
| 286 | Name: oNames[1], |
| 287 | }) |
| 288 | if err != nil { |
| 289 | t.Error(err) |
| 290 | return |
| 291 | } |
| 292 | o2Content, err := objects.ExtractContent(dr) |
| 293 | if err != nil { |
| 294 | t.Error(err) |
| 295 | } |
| 296 | dr, err = objects.Download(client, objects.DownloadOpts{ |
| 297 | Container: cName, |
| 298 | Name: oNames[0], |
| 299 | }) |
| 300 | if err != nil { |
| 301 | t.Error(err) |
| 302 | return |
| 303 | } |
| 304 | o1Content, err := objects.ExtractContent(dr) |
| 305 | if err != nil { |
| 306 | t.Error(err) |
| 307 | return |
| 308 | } |
| 309 | if string(o2Content) != string(o1Content) { |
| 310 | t.Errorf("Copy failed. Expected\n%s\nand got\n%s", string(o1Content), string(o2Content)) |
| 311 | return |
| 312 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 313 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 314 | err = objects.Update(client, objects.UpdateOpts{ |
| 315 | Container: cName, |
| 316 | Name: oNames[0], |
| 317 | Metadata: metadata, |
| 318 | }) |
| 319 | if err != nil { |
| 320 | t.Error(err) |
| 321 | return |
| 322 | } |
| 323 | defer func() { |
| 324 | tempMap := make(map[string]string) |
| 325 | for k := range metadata { |
| 326 | tempMap[k] = "" |
| 327 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 328 | err = objects.Update(client, objects.UpdateOpts{ |
| 329 | Container: cName, |
| 330 | Name: oNames[0], |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 331 | Metadata: tempMap, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 332 | }) |
| 333 | if err != nil { |
| 334 | t.Error(err) |
| 335 | return |
| 336 | } |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 337 | }() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 338 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 339 | gr, err := objects.Get(client, objects.GetOpts{}) |
| 340 | if err != nil { |
| 341 | t.Error(err) |
| 342 | return |
| 343 | } |
| 344 | om := objects.ExtractMetadata(gr) |
| 345 | for k := range metadata { |
| 346 | if om[k] != metadata[strings.Title(k)] { |
| 347 | t.Errorf("Expected custom metadata with key: %s", k) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 348 | return |
| 349 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 350 | } |
| 351 | } |