Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 1 | package containers |
| 2 | |
| 3 | import ( |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 4 | "testing" |
| 5 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud/pagination" |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame] | 7 | th "github.com/rackspace/gophercloud/testhelper" |
Jamie Hannaford | 4ff7996 | 2014-10-06 16:28:23 +0200 | [diff] [blame] | 8 | fake "github.com/rackspace/gophercloud/testhelper/client" |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 9 | ) |
| 10 | |
| 11 | var metadata = map[string]string{"gophercloud-test": "containers"} |
| 12 | |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 13 | func TestListContainerInfo(t *testing.T) { |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame] | 14 | th.SetupHTTP() |
| 15 | defer th.TeardownHTTP() |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 16 | HandleListContainerInfoSuccessfully(t) |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 17 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 18 | count := 0 |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 19 | err := List(fake.ServiceClient(), &ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 20 | count++ |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 21 | actual, err := ExtractInfo(page) |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 22 | th.AssertNoErr(t, err) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 23 | |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 24 | th.CheckDeepEquals(t, ExpectedListInfo, actual) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 25 | |
| 26 | return true, nil |
| 27 | }) |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 28 | th.AssertNoErr(t, err) |
| 29 | th.CheckEquals(t, count, 1) |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 30 | } |
| 31 | |
Jon Perritt | fe5e735 | 2015-02-18 13:51:01 -0700 | [diff] [blame^] | 32 | func 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 Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 44 | func TestListContainerNames(t *testing.T) { |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame] | 45 | th.SetupHTTP() |
| 46 | defer th.TeardownHTTP() |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 47 | HandleListContainerNamesSuccessfully(t) |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 48 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 49 | count := 0 |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 50 | err := List(fake.ServiceClient(), &ListOpts{Full: false}).EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 51 | count++ |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 52 | 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 Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 58 | th.CheckDeepEquals(t, ExpectedListNames, actual) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 59 | |
| 60 | return true, nil |
| 61 | }) |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 62 | th.AssertNoErr(t, err) |
| 63 | th.CheckEquals(t, count, 1) |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 64 | } |
| 65 | |
Jon Perritt | fe5e735 | 2015-02-18 13:51:01 -0700 | [diff] [blame^] | 66 | func 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 Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 78 | func TestCreateContainer(t *testing.T) { |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame] | 79 | th.SetupHTTP() |
| 80 | defer th.TeardownHTTP() |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 81 | HandleCreateContainerSuccessfully(t) |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 82 | |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame] | 83 | options := CreateOpts{ContentType: "application/json", Metadata: map[string]string{"foo": "bar"}} |
Jon Perritt | 0ce24ad | 2014-10-20 21:59:45 -0500 | [diff] [blame] | 84 | res := Create(fake.ServiceClient(), "testContainer", options) |
Jon Perritt | 59b0ea4 | 2015-01-31 20:23:33 -0700 | [diff] [blame] | 85 | c, err := res.Extract() |
| 86 | th.CheckNoErr(t, err) |
Jon Perritt | 0ce24ad | 2014-10-20 21:59:45 -0500 | [diff] [blame] | 87 | th.CheckEquals(t, "bar", res.Header["X-Container-Meta-Foo"][0]) |
Jon Perritt | 59b0ea4 | 2015-01-31 20:23:33 -0700 | [diff] [blame] | 88 | th.CheckEquals(t, "1234567", c.TransID) |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | func TestDeleteContainer(t *testing.T) { |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame] | 92 | th.SetupHTTP() |
| 93 | defer th.TeardownHTTP() |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 94 | HandleDeleteContainerSuccessfully(t) |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 95 | |
Jon Perritt | 0ce24ad | 2014-10-20 21:59:45 -0500 | [diff] [blame] | 96 | res := Delete(fake.ServiceClient(), "testContainer") |
| 97 | th.CheckNoErr(t, res.Err) |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | func TestUpateContainer(t *testing.T) { |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame] | 101 | th.SetupHTTP() |
| 102 | defer th.TeardownHTTP() |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 103 | HandleUpdateContainerSuccessfully(t) |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 104 | |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame] | 105 | options := &UpdateOpts{Metadata: map[string]string{"foo": "bar"}} |
Jon Perritt | 0ce24ad | 2014-10-20 21:59:45 -0500 | [diff] [blame] | 106 | res := Update(fake.ServiceClient(), "testContainer", options) |
| 107 | th.CheckNoErr(t, res.Err) |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | func TestGetContainer(t *testing.T) { |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame] | 111 | th.SetupHTTP() |
| 112 | defer th.TeardownHTTP() |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 113 | HandleGetContainerSuccessfully(t) |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 114 | |
Jamie Hannaford | 4ff7996 | 2014-10-06 16:28:23 +0200 | [diff] [blame] | 115 | _, err := Get(fake.ServiceClient(), "testContainer").ExtractMetadata() |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 116 | th.CheckNoErr(t, err) |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 117 | } |