blob: 88569418eb626fd915bd9a5483747c39a091b22a [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.
Ash Wilson0036cbf2014-10-24 15:29:48 -040020 client := newClient(t)
Jon Perrittf050a4c2014-09-11 15:01:17 -050021
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050022 // Create a slice of random container names.
Jon Perrittf050a4c2014-09-11 15:01:17 -050023 cNames := make([]string, numContainers)
24 for i := 0; i < numContainers; i++ {
25 cNames[i] = tools.RandomString("gophercloud-test-container-", 8)
26 }
27
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050028 // Create numContainers containers.
Jon Perrittf050a4c2014-09-11 15:01:17 -050029 for i := 0; i < len(cNames); i++ {
Jon Perritt2a7797d2014-10-21 15:08:43 -050030 res := containers.Create(client, cNames[i], nil)
31 th.AssertNoErr(res.Err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050032 }
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050033 // Delete the numContainers containers after function completion.
Jon Perrittf050a4c2014-09-11 15:01:17 -050034 defer func() {
35 for i := 0; i < len(cNames); i++ {
Jon Perritt2a7797d2014-10-21 15:08:43 -050036 res = containers.Delete(client, cNames[i])
37 th.AssertNoErr(res.Err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050038 }
39 }()
40
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050041 // List the numContainer names that were just created. To just list those,
42 // the 'prefix' parameter is used.
Jon Perrittde47eac2014-09-30 15:34:17 -050043 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 -050044 containerList, err := containers.ExtractInfo(page)
Jon Perritt2a7797d2014-10-21 15:08:43 -050045 th.AssertNoErr(err)
46
Jon Perritt8c93a302014-09-28 22:35:57 -050047 for _, n := range containerList {
48 t.Logf("Container: Name [%s] Count [%d] Bytes [%d]",
Jon Perrittfdac6e52014-09-29 19:43:45 -050049 n.Name, n.Count, n.Bytes)
Jon Perritt8c93a302014-09-28 22:35:57 -050050 }
51
52 return true, nil
Jon Perrittf050a4c2014-09-11 15:01:17 -050053 })
Jon Perritt2a7797d2014-10-21 15:08:43 -050054 th.AssertNoErr(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050055
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050056 // List the info for the numContainer containers that were created.
Jon Perrittde47eac2014-09-30 15:34:17 -050057 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 -050058 containerList, err := containers.ExtractNames(page)
Jon Perritt2a7797d2014-10-21 15:08:43 -050059 th.AssertNoErr(err)
Jon Perritt8c93a302014-09-28 22:35:57 -050060 for _, n := range containerList {
61 t.Logf("Container: Name [%s]", n)
62 }
63
64 return true, nil
Jon Perrittf050a4c2014-09-11 15:01:17 -050065 })
Jon Perritt2a7797d2014-10-21 15:08:43 -050066 th.AssertNoErr(err)
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050067
68 // Update one of the numContainer container metadata.
Jon Perritt2a7797d2014-10-21 15:08:43 -050069 res = containers.Update(client, cNames[0], &containers.UpdateOpts{Metadata: metadata})
70 th.AssertNoErr(res.Err)
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050071 // After the tests are done, delete the metadata that was set.
Jon Perrittf050a4c2014-09-11 15:01:17 -050072 defer func() {
73 tempMap := make(map[string]string)
74 for k := range metadata {
75 tempMap[k] = ""
76 }
Jon Perritt2a7797d2014-10-21 15:08:43 -050077 res = containers.Update(client, cNames[0], &containers.UpdateOpts{Metadata: tempMap})
78 th.AssertNoErr(res.Err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050079 }()
80
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050081 // Retrieve a container's metadata.
Jon Perritt8c93a302014-09-28 22:35:57 -050082 cm, err := containers.Get(client, cNames[0]).ExtractMetadata()
Jon Perritt2a7797d2014-10-21 15:08:43 -050083 th.AssertNoErr(err)
Jon Perrittf050a4c2014-09-11 15:01:17 -050084 for k := range metadata {
85 if cm[k] != metadata[strings.Title(k)] {
86 t.Errorf("Expected custom metadata with key: %s", k)
Jon Perrittf050a4c2014-09-11 15:01:17 -050087 }
88 }
89}