blob: b2c91c5f2e2b368dcfe27bcbfa6b08d5ac6fd84a [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 Perritt2a7797d2014-10-21 15:08:43 -050012 th "github.com/rackspace/gophercloud/testhelper"
Jon Perrittf050a4c2014-09-11 15:01:17 -050013)
14
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050015// numContainers is the number of containers to create for testing.
Jon Perrittf050a4c2014-09-11 15:01:17 -050016var numContainers = 2
17
18func TestContainers(t *testing.T) {
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050019 // Create a new client to execute the HTTP requests. See common.go for newClient body.
Jon Perrittf050a4c2014-09-11 15:01:17 -050020 client, err := newClient()
Jon Perritt2a7797d2014-10-21 15:08:43 -050021 th.AssertNoErr(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050022
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050023 // Create a slice of random container names.
Jon Perrittf050a4c2014-09-11 15:01:17 -050024 cNames := make([]string, numContainers)
25 for i := 0; i < numContainers; i++ {
26 cNames[i] = tools.RandomString("gophercloud-test-container-", 8)
27 }
28
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050029 // Create numContainers containers.
Jon Perrittf050a4c2014-09-11 15:01:17 -050030 for i := 0; i < len(cNames); i++ {
Jon Perritt2a7797d2014-10-21 15:08:43 -050031 res := containers.Create(client, cNames[i], nil)
32 th.AssertNoErr(res.Err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050033 }
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050034 // Delete the numContainers containers after function completion.
Jon Perrittf050a4c2014-09-11 15:01:17 -050035 defer func() {
36 for i := 0; i < len(cNames); i++ {
Jon Perritt2a7797d2014-10-21 15:08:43 -050037 res = containers.Delete(client, cNames[i])
38 th.AssertNoErr(res.Err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050039 }
40 }()
41
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050042 // List the numContainer names that were just created. To just list those,
43 // the 'prefix' parameter is used.
Jon Perrittde47eac2014-09-30 15:34:17 -050044 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 -050045 containerList, err := containers.ExtractInfo(page)
Jon Perritt2a7797d2014-10-21 15:08:43 -050046 th.AssertNoErr(err)
47
Jon Perritt8c93a302014-09-28 22:35:57 -050048 for _, n := range containerList {
49 t.Logf("Container: Name [%s] Count [%d] Bytes [%d]",
Jon Perrittfdac6e52014-09-29 19:43:45 -050050 n.Name, n.Count, n.Bytes)
Jon Perritt8c93a302014-09-28 22:35:57 -050051 }
52
53 return true, nil
Jon Perrittf050a4c2014-09-11 15:01:17 -050054 })
Jon Perritt2a7797d2014-10-21 15:08:43 -050055 th.AssertNoErr(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050056
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050057 // List the info for the numContainer containers that were created.
Jon Perrittde47eac2014-09-30 15:34:17 -050058 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 -050059 containerList, err := containers.ExtractNames(page)
Jon Perritt2a7797d2014-10-21 15:08:43 -050060 th.AssertNoErr(err)
Jon Perritt8c93a302014-09-28 22:35:57 -050061 for _, n := range containerList {
62 t.Logf("Container: Name [%s]", n)
63 }
64
65 return true, nil
Jon Perrittf050a4c2014-09-11 15:01:17 -050066 })
Jon Perritt2a7797d2014-10-21 15:08:43 -050067 th.AssertNoErr(err)
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050068
69 // Update one of the numContainer container metadata.
Jon Perritt2a7797d2014-10-21 15:08:43 -050070 res = containers.Update(client, cNames[0], &containers.UpdateOpts{Metadata: metadata})
71 th.AssertNoErr(res.Err)
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050072 // After the tests are done, delete the metadata that was set.
Jon Perrittf050a4c2014-09-11 15:01:17 -050073 defer func() {
74 tempMap := make(map[string]string)
75 for k := range metadata {
76 tempMap[k] = ""
77 }
Jon Perritt2a7797d2014-10-21 15:08:43 -050078 res = containers.Update(client, cNames[0], &containers.UpdateOpts{Metadata: tempMap})
79 th.AssertNoErr(res.Err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050080 }()
81
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050082 // Retrieve a container's metadata.
Jon Perritt8c93a302014-09-28 22:35:57 -050083 cm, err := containers.Get(client, cNames[0]).ExtractMetadata()
Jon Perritt2a7797d2014-10-21 15:08:43 -050084 th.AssertNoErr(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050085 for k := range metadata {
86 if cm[k] != metadata[strings.Title(k)] {
87 t.Errorf("Expected custom metadata with key: %s", k)
Jon Perrittf050a4c2014-09-11 15:01:17 -050088 }
89 }
90}