blob: d0ce7f1e585cf88dd909583d5cb9a93215a5ba53 [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
32func TestListContainerNames(t *testing.T) {
Jon Perritt27caa2f2014-10-13 21:08:36 -050033 th.SetupHTTP()
34 defer th.TeardownHTTP()
Jon Perritt457f8ca2014-10-15 00:28:23 -050035 HandleListContainerNamesSuccessfully(t)
Jon Perritt50da9b42014-09-14 15:06:59 -050036
Jon Perrittde47eac2014-09-30 15:34:17 -050037 count := 0
Jon Perritt457f8ca2014-10-15 00:28:23 -050038 err := List(fake.ServiceClient(), &ListOpts{Full: false}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittde47eac2014-09-30 15:34:17 -050039 count++
Jon Perritt8c93a302014-09-28 22:35:57 -050040 actual, err := ExtractNames(page)
41 if err != nil {
42 t.Errorf("Failed to extract container names: %v", err)
43 return false, err
44 }
45
Jon Perritt457f8ca2014-10-15 00:28:23 -050046 th.CheckDeepEquals(t, ExpectedListNames, actual)
Jon Perritt8c93a302014-09-28 22:35:57 -050047
48 return true, nil
49 })
Jon Perritt457f8ca2014-10-15 00:28:23 -050050 th.AssertNoErr(t, err)
51 th.CheckEquals(t, count, 1)
Jon Perritt50da9b42014-09-14 15:06:59 -050052}
53
54func TestCreateContainer(t *testing.T) {
Jon Perritt27caa2f2014-10-13 21:08:36 -050055 th.SetupHTTP()
56 defer th.TeardownHTTP()
Jon Perritt457f8ca2014-10-15 00:28:23 -050057 HandleCreateContainerSuccessfully(t)
Jon Perritt50da9b42014-09-14 15:06:59 -050058
Jon Perritt27caa2f2014-10-13 21:08:36 -050059 options := CreateOpts{ContentType: "application/json", Metadata: map[string]string{"foo": "bar"}}
Jon Perritt0ce24ad2014-10-20 21:59:45 -050060 res := Create(fake.ServiceClient(), "testContainer", options)
61 th.CheckNoErr(t, res.Err)
62 th.CheckEquals(t, "bar", res.Header["X-Container-Meta-Foo"][0])
Jon Perritt50da9b42014-09-14 15:06:59 -050063}
64
65func TestDeleteContainer(t *testing.T) {
Jon Perritt27caa2f2014-10-13 21:08:36 -050066 th.SetupHTTP()
67 defer th.TeardownHTTP()
Jon Perritt457f8ca2014-10-15 00:28:23 -050068 HandleDeleteContainerSuccessfully(t)
Jon Perritt50da9b42014-09-14 15:06:59 -050069
Jon Perritt0ce24ad2014-10-20 21:59:45 -050070 res := Delete(fake.ServiceClient(), "testContainer")
71 th.CheckNoErr(t, res.Err)
Jon Perritt50da9b42014-09-14 15:06:59 -050072}
73
74func TestUpateContainer(t *testing.T) {
Jon Perritt27caa2f2014-10-13 21:08:36 -050075 th.SetupHTTP()
76 defer th.TeardownHTTP()
Jon Perritt457f8ca2014-10-15 00:28:23 -050077 HandleUpdateContainerSuccessfully(t)
Jon Perritt50da9b42014-09-14 15:06:59 -050078
Jon Perritt27caa2f2014-10-13 21:08:36 -050079 options := &UpdateOpts{Metadata: map[string]string{"foo": "bar"}}
Jon Perritt0ce24ad2014-10-20 21:59:45 -050080 res := Update(fake.ServiceClient(), "testContainer", options)
81 th.CheckNoErr(t, res.Err)
Jon Perritt50da9b42014-09-14 15:06:59 -050082}
83
84func TestGetContainer(t *testing.T) {
Jon Perritt27caa2f2014-10-13 21:08:36 -050085 th.SetupHTTP()
86 defer th.TeardownHTTP()
Jon Perritt457f8ca2014-10-15 00:28:23 -050087 HandleGetContainerSuccessfully(t)
Jon Perritt50da9b42014-09-14 15:06:59 -050088
Jamie Hannaford4ff79962014-10-06 16:28:23 +020089 _, err := Get(fake.ServiceClient(), "testContainer").ExtractMetadata()
Jon Perritt457f8ca2014-10-15 00:28:23 -050090 th.CheckNoErr(t, err)
Jon Perritt50da9b42014-09-14 15:06:59 -050091}