blob: 1c6393f76acdb4549c3334b22fa8175a4763265d [file] [log] [blame]
Jon Perrittf050a4c2014-09-11 15:01:17 -05001// +build acceptance
2
3package v1
4
5import (
6 "strings"
7 "testing"
8
9 "github.com/rackspace/gophercloud/acceptance/tools"
Jon Perrittfdac6e52014-09-29 19:43:45 -050010 "github.com/rackspace/gophercloud/openstack/objectStorage/v1/containers"
Jon Perritt8c93a302014-09-28 22:35:57 -050011 "github.com/rackspace/gophercloud/pagination"
Jon Perrittf050a4c2014-09-11 15:01:17 -050012)
13
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050014// numContainers is the number of containers to create for testing.
Jon Perrittf050a4c2014-09-11 15:01:17 -050015var numContainers = 2
16
17func TestContainers(t *testing.T) {
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050018 // Create a new client to execute the HTTP requests. See common.go for newClient body.
Jon Perrittf050a4c2014-09-11 15:01:17 -050019 client, err := newClient()
20 if err != nil {
21 t.Error(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050022 }
23
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050024 // Create a slice of random container names.
Jon Perrittf050a4c2014-09-11 15:01:17 -050025 cNames := make([]string, numContainers)
26 for i := 0; i < numContainers; i++ {
27 cNames[i] = tools.RandomString("gophercloud-test-container-", 8)
28 }
29
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050030 // Create numContainers containers.
Jon Perrittf050a4c2014-09-11 15:01:17 -050031 for i := 0; i < len(cNames); i++ {
Jon Perritt8c93a302014-09-28 22:35:57 -050032 _, err := containers.Create(client, cNames[i], containers.CreateOpts{})
Jon Perrittf050a4c2014-09-11 15:01:17 -050033 if err != nil {
34 t.Error(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050035 }
36 }
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050037 // Delete the numContainers containers after function completion.
Jon Perrittf050a4c2014-09-11 15:01:17 -050038 defer func() {
39 for i := 0; i < len(cNames); i++ {
Jon Perritt8c93a302014-09-28 22:35:57 -050040 err = containers.Delete(client, cNames[i])
Jon Perrittf050a4c2014-09-11 15:01:17 -050041 if err != nil {
42 t.Error(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050043 }
44 }
45 }()
46
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050047 // List the numContainer names that were just created. To just list those,
48 // the 'prefix' parameter is used.
Jon Perritt8c93a302014-09-28 22:35:57 -050049 pager := containers.List(client, containers.ListOpts{Full: true, Prefix: "gophercloud-test-container-"})
50 if pager.Err != nil {
51 t.Error(err)
52 return
53 }
54 err = pager.EachPage(func(page pagination.Page) (bool, error) {
55 containerList, err := containers.ExtractInfo(page)
56 if err != nil {
57 t.Error(err)
58 }
59 for _, n := range containerList {
60 t.Logf("Container: Name [%s] Count [%d] Bytes [%d]",
Jon Perrittfdac6e52014-09-29 19:43:45 -050061 n.Name, n.Count, n.Bytes)
Jon Perritt8c93a302014-09-28 22:35:57 -050062 }
63
64 return true, nil
Jon Perrittf050a4c2014-09-11 15:01:17 -050065 })
66 if err != nil {
67 t.Error(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050068 }
69
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050070 // List the info for the numContainer containers that were created.
Jon Perritt8c93a302014-09-28 22:35:57 -050071 pager = containers.List(client, containers.ListOpts{Full: false, Prefix: "gophercloud-test-container-"})
72 err = pager.EachPage(func(page pagination.Page) (bool, error) {
73 containerList, err := containers.ExtractNames(page)
74 if err != nil {
75 return false, err
76 }
77 for _, n := range containerList {
78 t.Logf("Container: Name [%s]", n)
79 }
80
81 return true, nil
Jon Perrittf050a4c2014-09-11 15:01:17 -050082 })
83 if err != nil {
84 t.Error(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050085 }
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050086
87 // Update one of the numContainer container metadata.
Jon Perritt8c93a302014-09-28 22:35:57 -050088 err = containers.Update(client, cNames[0], containers.UpdateOpts{Metadata: metadata})
Jon Perrittf050a4c2014-09-11 15:01:17 -050089 if err != nil {
90 t.Error(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050091 }
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050092 // After the tests are done, delete the metadata that was set.
Jon Perrittf050a4c2014-09-11 15:01:17 -050093 defer func() {
94 tempMap := make(map[string]string)
95 for k := range metadata {
96 tempMap[k] = ""
97 }
Jon Perritt8c93a302014-09-28 22:35:57 -050098 err = containers.Update(client, cNames[0], containers.UpdateOpts{Metadata: tempMap})
Jon Perrittf050a4c2014-09-11 15:01:17 -050099 if err != nil {
100 t.Error(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -0500101 }
102 }()
103
Jon Perritt4cf2ecf2014-09-15 13:59:16 -0500104 // Retrieve a container's metadata.
Jon Perritt8c93a302014-09-28 22:35:57 -0500105 cm, err := containers.Get(client, cNames[0]).ExtractMetadata()
Jon Perrittf050a4c2014-09-11 15:01:17 -0500106 if err != nil {
107 t.Error(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -0500108 }
Jon Perrittf050a4c2014-09-11 15:01:17 -0500109 for k := range metadata {
110 if cm[k] != metadata[strings.Title(k)] {
111 t.Errorf("Expected custom metadata with key: %s", k)
Jon Perrittf050a4c2014-09-11 15:01:17 -0500112 }
113 }
114}