blob: 0ccd5a7786816a9e6ad445635e284301cb3b5523 [file] [log] [blame]
Jon Perritt50da9b42014-09-14 15:06:59 -05001package containers
2
3import (
Jon Perritt50da9b42014-09-14 15:06:59 -05004 "testing"
5
Jon Perritt8c93a302014-09-28 22:35:57 -05006 "github.com/rackspace/gophercloud/pagination"
Jon Perritt27caa2f2014-10-13 21:08:36 -05007 th "github.com/rackspace/gophercloud/testhelper"
Jamie Hannaford4ff79962014-10-06 16:28:23 +02008 fake "github.com/rackspace/gophercloud/testhelper/client"
Jon Perritt50da9b42014-09-14 15:06:59 -05009)
10
11var metadata = map[string]string{"gophercloud-test": "containers"}
12
Jon Perritt50da9b42014-09-14 15:06:59 -050013func TestListContainerInfo(t *testing.T) {
Jon Perritt27caa2f2014-10-13 21:08:36 -050014 th.SetupHTTP()
15 defer th.TeardownHTTP()
Jon Perritt457f8ca2014-10-15 00:28:23 -050016 HandleListContainerInfoSuccessfully(t)
Jon Perritt50da9b42014-09-14 15:06:59 -050017
Jon Perrittde47eac2014-09-30 15:34:17 -050018 count := 0
Jon Perritt457f8ca2014-10-15 00:28:23 -050019 err := List(fake.ServiceClient(), &ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittde47eac2014-09-30 15:34:17 -050020 count++
Jon Perritt8c93a302014-09-28 22:35:57 -050021 actual, err := ExtractInfo(page)
Jon Perritt457f8ca2014-10-15 00:28:23 -050022 th.AssertNoErr(t, err)
Jon Perritt8c93a302014-09-28 22:35:57 -050023
Jon Perritt457f8ca2014-10-15 00:28:23 -050024 th.CheckDeepEquals(t, ExpectedListInfo, actual)
Jon Perritt8c93a302014-09-28 22:35:57 -050025
26 return true, nil
27 })
Jon Perritt457f8ca2014-10-15 00:28:23 -050028 th.AssertNoErr(t, err)
29 th.CheckEquals(t, count, 1)
Jon Perritt50da9b42014-09-14 15:06:59 -050030}
31
Jon Perrittfe5e7352015-02-18 13:51:01 -070032func TestListAllContainerInfo(t *testing.T) {
33 th.SetupHTTP()
34 defer th.TeardownHTTP()
35 HandleListContainerInfoSuccessfully(t)
36
37 allPages, err := List(fake.ServiceClient(), &ListOpts{Full: true}).AllPages()
38 th.AssertNoErr(t, err)
39 actual, err := ExtractInfo(allPages)
40 th.AssertNoErr(t, err)
41 th.CheckDeepEquals(t, ExpectedListInfo, actual)
42}
43
Jon Perritt50da9b42014-09-14 15:06:59 -050044func TestListContainerNames(t *testing.T) {
Jon Perritt27caa2f2014-10-13 21:08:36 -050045 th.SetupHTTP()
46 defer th.TeardownHTTP()
Jon Perritt457f8ca2014-10-15 00:28:23 -050047 HandleListContainerNamesSuccessfully(t)
Jon Perritt50da9b42014-09-14 15:06:59 -050048
Jon Perrittde47eac2014-09-30 15:34:17 -050049 count := 0
Jon Perritt457f8ca2014-10-15 00:28:23 -050050 err := List(fake.ServiceClient(), &ListOpts{Full: false}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittde47eac2014-09-30 15:34:17 -050051 count++
Jon Perritt8c93a302014-09-28 22:35:57 -050052 actual, err := ExtractNames(page)
53 if err != nil {
54 t.Errorf("Failed to extract container names: %v", err)
55 return false, err
56 }
57
Jon Perritt457f8ca2014-10-15 00:28:23 -050058 th.CheckDeepEquals(t, ExpectedListNames, actual)
Jon Perritt8c93a302014-09-28 22:35:57 -050059
60 return true, nil
61 })
Jon Perritt457f8ca2014-10-15 00:28:23 -050062 th.AssertNoErr(t, err)
63 th.CheckEquals(t, count, 1)
Jon Perritt50da9b42014-09-14 15:06:59 -050064}
65
Jon Perrittfe5e7352015-02-18 13:51:01 -070066func TestListAllContainerNames(t *testing.T) {
67 th.SetupHTTP()
68 defer th.TeardownHTTP()
69 HandleListContainerNamesSuccessfully(t)
70
71 allPages, err := List(fake.ServiceClient(), &ListOpts{Full: false}).AllPages()
72 th.AssertNoErr(t, err)
73 actual, err := ExtractNames(allPages)
74 th.AssertNoErr(t, err)
75 th.CheckDeepEquals(t, ExpectedListNames, actual)
76}
77
Jon Perritt50da9b42014-09-14 15:06:59 -050078func TestCreateContainer(t *testing.T) {
Jon Perritt27caa2f2014-10-13 21:08:36 -050079 th.SetupHTTP()
80 defer th.TeardownHTTP()
Jon Perritt457f8ca2014-10-15 00:28:23 -050081 HandleCreateContainerSuccessfully(t)
Jon Perritt50da9b42014-09-14 15:06:59 -050082
Jon Perritt27caa2f2014-10-13 21:08:36 -050083 options := CreateOpts{ContentType: "application/json", Metadata: map[string]string{"foo": "bar"}}
Jon Perritt0ce24ad2014-10-20 21:59:45 -050084 res := Create(fake.ServiceClient(), "testContainer", options)
Jon Perritt59b0ea42015-01-31 20:23:33 -070085 c, err := res.Extract()
86 th.CheckNoErr(t, err)
Jon Perritt0ce24ad2014-10-20 21:59:45 -050087 th.CheckEquals(t, "bar", res.Header["X-Container-Meta-Foo"][0])
Jon Perritt59b0ea42015-01-31 20:23:33 -070088 th.CheckEquals(t, "1234567", c.TransID)
Jon Perritt50da9b42014-09-14 15:06:59 -050089}
90
91func TestDeleteContainer(t *testing.T) {
Jon Perritt27caa2f2014-10-13 21:08:36 -050092 th.SetupHTTP()
93 defer th.TeardownHTTP()
Jon Perritt457f8ca2014-10-15 00:28:23 -050094 HandleDeleteContainerSuccessfully(t)
Jon Perritt50da9b42014-09-14 15:06:59 -050095
Jon Perritt0ce24ad2014-10-20 21:59:45 -050096 res := Delete(fake.ServiceClient(), "testContainer")
97 th.CheckNoErr(t, res.Err)
Jon Perritt50da9b42014-09-14 15:06:59 -050098}
99
100func TestUpateContainer(t *testing.T) {
Jon Perritt27caa2f2014-10-13 21:08:36 -0500101 th.SetupHTTP()
102 defer th.TeardownHTTP()
Jon Perritt457f8ca2014-10-15 00:28:23 -0500103 HandleUpdateContainerSuccessfully(t)
Jon Perritt50da9b42014-09-14 15:06:59 -0500104
Jon Perritt27caa2f2014-10-13 21:08:36 -0500105 options := &UpdateOpts{Metadata: map[string]string{"foo": "bar"}}
Jon Perritt0ce24ad2014-10-20 21:59:45 -0500106 res := Update(fake.ServiceClient(), "testContainer", options)
107 th.CheckNoErr(t, res.Err)
Jon Perritt50da9b42014-09-14 15:06:59 -0500108}
109
110func TestGetContainer(t *testing.T) {
Jon Perritt27caa2f2014-10-13 21:08:36 -0500111 th.SetupHTTP()
112 defer th.TeardownHTTP()
Jon Perritt457f8ca2014-10-15 00:28:23 -0500113 HandleGetContainerSuccessfully(t)
Jon Perritt50da9b42014-09-14 15:06:59 -0500114
Jamie Hannaford4ff79962014-10-06 16:28:23 +0200115 _, err := Get(fake.ServiceClient(), "testContainer").ExtractMetadata()
Jon Perritt457f8ca2014-10-15 00:28:23 -0500116 th.CheckNoErr(t, err)
Jon Perritt50da9b42014-09-14 15:06:59 -0500117}