blob: b541307e30ca19e9bb85efdde4c966600630f62c [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 Perritta9caabd2014-09-30 13:02:34 -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 Perritt5db08922014-09-30 21:32:48 -050032 _, err := containers.Create(client, cNames[i], nil).ExtractHeaders()
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 Perritt5db08922014-09-30 21:32:48 -050040 _, err = containers.Delete(client, cNames[i]).ExtractHeaders()
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 Perrittde47eac2014-09-30 15:34:17 -050049 err = containers.List(client, &containers.ListOpts{Full: true, Prefix: "gophercloud-test-container-"}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt8c93a302014-09-28 22:35:57 -050050 containerList, err := containers.ExtractInfo(page)
51 if err != nil {
52 t.Error(err)
53 }
54 for _, n := range containerList {
55 t.Logf("Container: Name [%s] Count [%d] Bytes [%d]",
Jon Perrittfdac6e52014-09-29 19:43:45 -050056 n.Name, n.Count, n.Bytes)
Jon Perritt8c93a302014-09-28 22:35:57 -050057 }
58
59 return true, nil
Jon Perrittf050a4c2014-09-11 15:01:17 -050060 })
61 if err != nil {
62 t.Error(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050063 }
64
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050065 // List the info for the numContainer containers that were created.
Jon Perrittde47eac2014-09-30 15:34:17 -050066 err = containers.List(client, &containers.ListOpts{Full: false, Prefix: "gophercloud-test-container-"}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt8c93a302014-09-28 22:35:57 -050067 containerList, err := containers.ExtractNames(page)
68 if err != nil {
69 return false, err
70 }
71 for _, n := range containerList {
72 t.Logf("Container: Name [%s]", n)
73 }
74
75 return true, nil
Jon Perrittf050a4c2014-09-11 15:01:17 -050076 })
77 if err != nil {
78 t.Error(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050079 }
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050080
81 // Update one of the numContainer container metadata.
Jon Perritt5db08922014-09-30 21:32:48 -050082 _, err = containers.Update(client, cNames[0], &containers.UpdateOpts{Metadata: metadata}).ExtractHeaders()
Jon Perrittf050a4c2014-09-11 15:01:17 -050083 if err != nil {
84 t.Error(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050085 }
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050086 // After the tests are done, delete the metadata that was set.
Jon Perrittf050a4c2014-09-11 15:01:17 -050087 defer func() {
88 tempMap := make(map[string]string)
89 for k := range metadata {
90 tempMap[k] = ""
91 }
Jon Perritt5db08922014-09-30 21:32:48 -050092 _, err = containers.Update(client, cNames[0], &containers.UpdateOpts{Metadata: tempMap}).ExtractHeaders()
Jon Perrittf050a4c2014-09-11 15:01:17 -050093 if err != nil {
94 t.Error(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050095 }
96 }()
97
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050098 // Retrieve a container's metadata.
Jon Perritt8c93a302014-09-28 22:35:57 -050099 cm, err := containers.Get(client, cNames[0]).ExtractMetadata()
Jon Perrittf050a4c2014-09-11 15:01:17 -0500100 if err != nil {
101 t.Error(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -0500102 }
Jon Perrittf050a4c2014-09-11 15:01:17 -0500103 for k := range metadata {
104 if cm[k] != metadata[strings.Title(k)] {
105 t.Errorf("Expected custom metadata with key: %s", k)
Jon Perrittf050a4c2014-09-11 15:01:17 -0500106 }
107 }
108}