blob: f650696e485bc236854e153f7d42174e030437aa [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)
Jon Perritt59b0ea42015-01-31 20:23:33 -070061 c, err := res.Extract()
62 th.CheckNoErr(t, err)
Jon Perritt0ce24ad2014-10-20 21:59:45 -050063 th.CheckEquals(t, "bar", res.Header["X-Container-Meta-Foo"][0])
Jon Perritt59b0ea42015-01-31 20:23:33 -070064 th.CheckEquals(t, "1234567", c.TransID)
Jon Perritt50da9b42014-09-14 15:06:59 -050065}
66
67func TestDeleteContainer(t *testing.T) {
Jon Perritt27caa2f2014-10-13 21:08:36 -050068 th.SetupHTTP()
69 defer th.TeardownHTTP()
Jon Perritt457f8ca2014-10-15 00:28:23 -050070 HandleDeleteContainerSuccessfully(t)
Jon Perritt50da9b42014-09-14 15:06:59 -050071
Jon Perritt0ce24ad2014-10-20 21:59:45 -050072 res := Delete(fake.ServiceClient(), "testContainer")
73 th.CheckNoErr(t, res.Err)
Jon Perritt50da9b42014-09-14 15:06:59 -050074}
75
76func TestUpateContainer(t *testing.T) {
Jon Perritt27caa2f2014-10-13 21:08:36 -050077 th.SetupHTTP()
78 defer th.TeardownHTTP()
Jon Perritt457f8ca2014-10-15 00:28:23 -050079 HandleUpdateContainerSuccessfully(t)
Jon Perritt50da9b42014-09-14 15:06:59 -050080
Jon Perritt27caa2f2014-10-13 21:08:36 -050081 options := &UpdateOpts{Metadata: map[string]string{"foo": "bar"}}
Jon Perritt0ce24ad2014-10-20 21:59:45 -050082 res := Update(fake.ServiceClient(), "testContainer", options)
83 th.CheckNoErr(t, res.Err)
Jon Perritt50da9b42014-09-14 15:06:59 -050084}
85
86func TestGetContainer(t *testing.T) {
Jon Perritt27caa2f2014-10-13 21:08:36 -050087 th.SetupHTTP()
88 defer th.TeardownHTTP()
Jon Perritt457f8ca2014-10-15 00:28:23 -050089 HandleGetContainerSuccessfully(t)
Jon Perritt50da9b42014-09-14 15:06:59 -050090
Jamie Hannaford4ff79962014-10-06 16:28:23 +020091 _, err := Get(fake.ServiceClient(), "testContainer").ExtractMetadata()
Jon Perritt457f8ca2014-10-15 00:28:23 -050092 th.CheckNoErr(t, err)
Jon Perritt50da9b42014-09-14 15:06:59 -050093}