Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 1 | package servers |
| 2 | |
| 3 | import ( |
Kevin Pike | 92e10b5 | 2015-04-10 15:16:57 -0700 | [diff] [blame^] | 4 | "encoding/base64" |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 5 | "net/http" |
| 6 | "testing" |
| 7 | |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/pagination" |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 9 | th "github.com/rackspace/gophercloud/testhelper" |
| 10 | "github.com/rackspace/gophercloud/testhelper/client" |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 11 | ) |
| 12 | |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 13 | func TestListServers(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 14 | th.SetupHTTP() |
| 15 | defer th.TeardownHTTP() |
Ash Wilson | a70510a | 2014-10-23 11:54:03 -0400 | [diff] [blame] | 16 | HandleServerListSuccessfully(t) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 17 | |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 18 | pages := 0 |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 19 | err := List(client.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 20 | pages++ |
| 21 | |
| 22 | actual, err := ExtractServers(page) |
| 23 | if err != nil { |
| 24 | return false, err |
| 25 | } |
| 26 | |
| 27 | if len(actual) != 2 { |
| 28 | t.Fatalf("Expected 2 servers, got %d", len(actual)) |
| 29 | } |
Ash Wilson | d3532cd | 2014-10-21 14:37:47 -0400 | [diff] [blame] | 30 | th.CheckDeepEquals(t, ServerHerp, actual[0]) |
| 31 | th.CheckDeepEquals(t, ServerDerp, actual[1]) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 32 | |
| 33 | return true, nil |
| 34 | }) |
| 35 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 36 | th.AssertNoErr(t, err) |
Jamie Hannaford | cf00172 | 2014-10-16 12:54:07 +0200 | [diff] [blame] | 37 | |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 38 | if pages != 1 { |
| 39 | t.Errorf("Expected 1 page, saw %d", pages) |
| 40 | } |
| 41 | } |
| 42 | |
Jon Perritt | d27a9c7 | 2015-02-18 11:33:28 -0700 | [diff] [blame] | 43 | func TestListAllServers(t *testing.T) { |
| 44 | th.SetupHTTP() |
| 45 | defer th.TeardownHTTP() |
| 46 | HandleServerListSuccessfully(t) |
| 47 | |
| 48 | allPages, err := List(client.ServiceClient(), ListOpts{}).AllPages() |
| 49 | th.AssertNoErr(t, err) |
| 50 | actual, err := ExtractServers(allPages) |
| 51 | th.AssertNoErr(t, err) |
| 52 | th.CheckDeepEquals(t, ServerHerp, actual[0]) |
| 53 | th.CheckDeepEquals(t, ServerDerp, actual[1]) |
| 54 | } |
| 55 | |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 56 | func TestCreateServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 57 | th.SetupHTTP() |
| 58 | defer th.TeardownHTTP() |
Ash Wilson | 664fe33 | 2014-10-21 17:47:49 -0400 | [diff] [blame] | 59 | HandleServerCreationSuccessfully(t, SingleServerBody) |
Ash Wilson | 3204d0d | 2014-09-25 10:37:44 -0400 | [diff] [blame] | 60 | |
Ash Wilson | 664fe33 | 2014-10-21 17:47:49 -0400 | [diff] [blame] | 61 | actual, err := Create(client.ServiceClient(), CreateOpts{ |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 62 | Name: "derp", |
| 63 | ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb", |
| 64 | FlavorRef: "1", |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 65 | }).Extract() |
Ash Wilson | 664fe33 | 2014-10-21 17:47:49 -0400 | [diff] [blame] | 66 | th.AssertNoErr(t, err) |
Ash Wilson | 3204d0d | 2014-09-25 10:37:44 -0400 | [diff] [blame] | 67 | |
Ash Wilson | d3532cd | 2014-10-21 14:37:47 -0400 | [diff] [blame] | 68 | th.CheckDeepEquals(t, ServerDerp, *actual) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | func TestDeleteServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 72 | th.SetupHTTP() |
| 73 | defer th.TeardownHTTP() |
Ash Wilson | 664fe33 | 2014-10-21 17:47:49 -0400 | [diff] [blame] | 74 | HandleServerDeletionSuccessfully(t) |
Ash Wilson | aff3627 | 2014-09-25 10:40:05 -0400 | [diff] [blame] | 75 | |
Jamie Hannaford | 34732fe | 2014-10-27 11:29:36 +0100 | [diff] [blame] | 76 | res := Delete(client.ServiceClient(), "asdfasdfasdf") |
| 77 | th.AssertNoErr(t, res.Err) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | func TestGetServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 81 | th.SetupHTTP() |
| 82 | defer th.TeardownHTTP() |
Ash Wilson | 189c95c | 2014-10-23 11:41:35 -0400 | [diff] [blame] | 83 | HandleServerGetSuccessfully(t) |
Ash Wilson | a612f1f | 2014-09-25 10:42:40 -0400 | [diff] [blame] | 84 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 85 | client := client.ServiceClient() |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 86 | actual, err := Get(client, "1234asdf").Extract() |
Ash Wilson | a612f1f | 2014-09-25 10:42:40 -0400 | [diff] [blame] | 87 | if err != nil { |
| 88 | t.Fatalf("Unexpected Get error: %v", err) |
| 89 | } |
| 90 | |
Ash Wilson | d3532cd | 2014-10-21 14:37:47 -0400 | [diff] [blame] | 91 | th.CheckDeepEquals(t, ServerDerp, *actual) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | func TestUpdateServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 95 | th.SetupHTTP() |
| 96 | defer th.TeardownHTTP() |
Ash Wilson | 189c95c | 2014-10-23 11:41:35 -0400 | [diff] [blame] | 97 | HandleServerUpdateSuccessfully(t) |
Ash Wilson | 0aac3a8 | 2014-09-25 10:45:03 -0400 | [diff] [blame] | 98 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 99 | client := client.ServiceClient() |
Ash Wilson | dcbc8fb | 2014-09-29 09:05:44 -0400 | [diff] [blame] | 100 | actual, err := Update(client, "1234asdf", UpdateOpts{Name: "new-name"}).Extract() |
Ash Wilson | 0aac3a8 | 2014-09-25 10:45:03 -0400 | [diff] [blame] | 101 | if err != nil { |
| 102 | t.Fatalf("Unexpected Update error: %v", err) |
| 103 | } |
| 104 | |
Ash Wilson | d3532cd | 2014-10-21 14:37:47 -0400 | [diff] [blame] | 105 | th.CheckDeepEquals(t, ServerDerp, *actual) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | func TestChangeServerAdminPassword(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 109 | th.SetupHTTP() |
| 110 | defer th.TeardownHTTP() |
Ash Wilson | 1c1eb88 | 2014-10-21 18:14:31 -0400 | [diff] [blame] | 111 | HandleAdminPasswordChangeSuccessfully(t) |
Ash Wilson | fb99ec7 | 2014-09-25 10:48:51 -0400 | [diff] [blame] | 112 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 113 | res := ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password") |
| 114 | th.AssertNoErr(t, res.Err) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | func TestRebootServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 118 | th.SetupHTTP() |
| 119 | defer th.TeardownHTTP() |
Ash Wilson | 2295ba5 | 2014-10-21 18:19:28 -0400 | [diff] [blame] | 120 | HandleRebootSuccessfully(t) |
Ash Wilson | 8d368e9 | 2014-09-25 10:49:07 -0400 | [diff] [blame] | 121 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 122 | res := Reboot(client.ServiceClient(), "1234asdf", SoftReboot) |
| 123 | th.AssertNoErr(t, res.Err) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | func TestRebuildServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 127 | th.SetupHTTP() |
| 128 | defer th.TeardownHTTP() |
Ash Wilson | acf49c6 | 2014-10-21 18:25:11 -0400 | [diff] [blame] | 129 | HandleRebuildSuccessfully(t, SingleServerBody) |
Ash Wilson | 077f877 | 2014-09-25 10:57:13 -0400 | [diff] [blame] | 130 | |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 131 | opts := RebuildOpts{ |
| 132 | Name: "new-name", |
| 133 | AdminPass: "swordfish", |
| 134 | ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb", |
| 135 | AccessIPv4: "1.2.3.4", |
Ash Wilson | 077f877 | 2014-09-25 10:57:13 -0400 | [diff] [blame] | 136 | } |
| 137 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 138 | actual, err := Rebuild(client.ServiceClient(), "1234asdf", opts).Extract() |
| 139 | th.AssertNoErr(t, err) |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 140 | |
Ash Wilson | d3532cd | 2014-10-21 14:37:47 -0400 | [diff] [blame] | 141 | th.CheckDeepEquals(t, ServerDerp, *actual) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | func TestResizeServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 145 | th.SetupHTTP() |
| 146 | defer th.TeardownHTTP() |
Ash Wilson | 45181f4 | 2014-09-25 11:00:16 -0400 | [diff] [blame] | 147 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 148 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 149 | th.TestMethod(t, r, "POST") |
| 150 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 151 | th.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`) |
Ash Wilson | 45181f4 | 2014-09-25 11:00:16 -0400 | [diff] [blame] | 152 | |
| 153 | w.WriteHeader(http.StatusAccepted) |
| 154 | }) |
| 155 | |
Ash Wilson | 5f7cf18 | 2014-10-23 08:35:24 -0400 | [diff] [blame] | 156 | res := Resize(client.ServiceClient(), "1234asdf", ResizeOpts{FlavorRef: "2"}) |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 157 | th.AssertNoErr(t, res.Err) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | func TestConfirmResize(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 161 | th.SetupHTTP() |
| 162 | defer th.TeardownHTTP() |
Ash Wilson | e2bffd5 | 2014-09-25 11:11:43 -0400 | [diff] [blame] | 163 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 164 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 165 | th.TestMethod(t, r, "POST") |
| 166 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 167 | th.TestJSONRequest(t, r, `{ "confirmResize": null }`) |
Ash Wilson | e2bffd5 | 2014-09-25 11:11:43 -0400 | [diff] [blame] | 168 | |
| 169 | w.WriteHeader(http.StatusNoContent) |
| 170 | }) |
| 171 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 172 | res := ConfirmResize(client.ServiceClient(), "1234asdf") |
| 173 | th.AssertNoErr(t, res.Err) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | func TestRevertResize(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 177 | th.SetupHTTP() |
| 178 | defer th.TeardownHTTP() |
Ash Wilson | 8deb38c | 2014-09-25 11:11:53 -0400 | [diff] [blame] | 179 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 180 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 181 | th.TestMethod(t, r, "POST") |
| 182 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 183 | th.TestJSONRequest(t, r, `{ "revertResize": null }`) |
Ash Wilson | 8deb38c | 2014-09-25 11:11:53 -0400 | [diff] [blame] | 184 | |
| 185 | w.WriteHeader(http.StatusAccepted) |
| 186 | }) |
| 187 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 188 | res := RevertResize(client.ServiceClient(), "1234asdf") |
| 189 | th.AssertNoErr(t, res.Err) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 190 | } |
Alex Gaynor | 810d489 | 2014-11-12 15:43:36 -0800 | [diff] [blame] | 191 | |
| 192 | func TestRescue(t *testing.T) { |
| 193 | th.SetupHTTP() |
| 194 | defer th.TeardownHTTP() |
| 195 | |
Alex Gaynor | 6c003d2 | 2014-11-13 13:52:05 -0800 | [diff] [blame] | 196 | HandleServerRescueSuccessfully(t) |
Alex Gaynor | 810d489 | 2014-11-12 15:43:36 -0800 | [diff] [blame] | 197 | |
Alex Gaynor | 40449ed | 2014-11-12 16:28:06 -0800 | [diff] [blame] | 198 | res := Rescue(client.ServiceClient(), "1234asdf", RescueOpts{ |
Alex Gaynor | 3116817 | 2014-11-12 16:27:47 -0800 | [diff] [blame] | 199 | AdminPass: "1234567890", |
Alex Gaynor | 810d489 | 2014-11-12 15:43:36 -0800 | [diff] [blame] | 200 | }) |
| 201 | th.AssertNoErr(t, res.Err) |
Alex Gaynor | 7f3b06e | 2014-11-13 09:54:03 -0800 | [diff] [blame] | 202 | adminPass, _ := res.Extract() |
Alex Gaynor | 0160cff | 2014-11-13 10:17:48 -0800 | [diff] [blame] | 203 | th.AssertEquals(t, "1234567890", adminPass) |
Alex Gaynor | 810d489 | 2014-11-12 15:43:36 -0800 | [diff] [blame] | 204 | } |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 205 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 206 | func TestGetMetadatum(t *testing.T) { |
| 207 | th.SetupHTTP() |
| 208 | defer th.TeardownHTTP() |
| 209 | |
| 210 | HandleMetadatumGetSuccessfully(t) |
| 211 | |
| 212 | expected := map[string]string{"foo": "bar"} |
| 213 | actual, err := Metadatum(client.ServiceClient(), "1234asdf", "foo").Extract() |
| 214 | th.AssertNoErr(t, err) |
| 215 | th.AssertDeepEquals(t, expected, actual) |
| 216 | } |
| 217 | |
| 218 | func TestCreateMetadatum(t *testing.T) { |
| 219 | th.SetupHTTP() |
| 220 | defer th.TeardownHTTP() |
| 221 | |
| 222 | HandleMetadatumCreateSuccessfully(t) |
| 223 | |
| 224 | expected := map[string]string{"foo": "bar"} |
| 225 | actual, err := CreateMetadatum(client.ServiceClient(), "1234asdf", MetadatumOpts{"foo": "bar"}).Extract() |
| 226 | th.AssertNoErr(t, err) |
| 227 | th.AssertDeepEquals(t, expected, actual) |
| 228 | } |
| 229 | |
| 230 | func TestDeleteMetadatum(t *testing.T) { |
| 231 | th.SetupHTTP() |
| 232 | defer th.TeardownHTTP() |
| 233 | |
| 234 | HandleMetadatumDeleteSuccessfully(t) |
| 235 | |
| 236 | err := DeleteMetadatum(client.ServiceClient(), "1234asdf", "foo").ExtractErr() |
| 237 | th.AssertNoErr(t, err) |
| 238 | } |
| 239 | |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 240 | func TestGetMetadata(t *testing.T) { |
| 241 | th.SetupHTTP() |
| 242 | defer th.TeardownHTTP() |
| 243 | |
| 244 | HandleMetadataGetSuccessfully(t) |
| 245 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 246 | expected := map[string]string{"foo": "bar", "this": "that"} |
| 247 | actual, err := Metadata(client.ServiceClient(), "1234asdf").Extract() |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 248 | th.AssertNoErr(t, err) |
| 249 | th.AssertDeepEquals(t, expected, actual) |
| 250 | } |
| 251 | |
Jon Perritt | 789f832 | 2014-11-21 08:20:04 -0700 | [diff] [blame] | 252 | func TestResetMetadata(t *testing.T) { |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 253 | th.SetupHTTP() |
| 254 | defer th.TeardownHTTP() |
| 255 | |
Jon Perritt | 789f832 | 2014-11-21 08:20:04 -0700 | [diff] [blame] | 256 | HandleMetadataResetSuccessfully(t) |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 257 | |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 258 | expected := map[string]string{"foo": "bar", "this": "that"} |
Jon Perritt | 789f832 | 2014-11-21 08:20:04 -0700 | [diff] [blame] | 259 | actual, err := ResetMetadata(client.ServiceClient(), "1234asdf", MetadataOpts{ |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 260 | "foo": "bar", |
| 261 | "this": "that", |
| 262 | }).Extract() |
| 263 | th.AssertNoErr(t, err) |
| 264 | th.AssertDeepEquals(t, expected, actual) |
| 265 | } |
| 266 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 267 | func TestUpdateMetadata(t *testing.T) { |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 268 | th.SetupHTTP() |
| 269 | defer th.TeardownHTTP() |
| 270 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 271 | HandleMetadataUpdateSuccessfully(t) |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 272 | |
| 273 | expected := map[string]string{"foo": "baz", "this": "those"} |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 274 | actual, err := UpdateMetadata(client.ServiceClient(), "1234asdf", MetadataOpts{ |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 275 | "foo": "baz", |
| 276 | "this": "those", |
| 277 | }).Extract() |
| 278 | th.AssertNoErr(t, err) |
| 279 | th.AssertDeepEquals(t, expected, actual) |
| 280 | } |
Jon Perritt | 5cb4948 | 2015-02-19 12:19:58 -0700 | [diff] [blame] | 281 | |
| 282 | func TestListAddresses(t *testing.T) { |
| 283 | th.SetupHTTP() |
| 284 | defer th.TeardownHTTP() |
| 285 | HandleAddressListSuccessfully(t) |
| 286 | |
| 287 | expected := ListAddressesExpected |
| 288 | pages := 0 |
| 289 | err := ListAddresses(client.ServiceClient(), "asdfasdfasdf").EachPage(func(page pagination.Page) (bool, error) { |
| 290 | pages++ |
| 291 | |
| 292 | actual, err := ExtractAddresses(page) |
| 293 | th.AssertNoErr(t, err) |
| 294 | |
| 295 | if len(actual) != 2 { |
Jon Perritt | 04d073c | 2015-02-19 21:46:23 -0700 | [diff] [blame] | 296 | t.Fatalf("Expected 2 networks, got %d", len(actual)) |
| 297 | } |
| 298 | th.CheckDeepEquals(t, expected, actual) |
| 299 | |
| 300 | return true, nil |
| 301 | }) |
| 302 | th.AssertNoErr(t, err) |
| 303 | th.CheckEquals(t, 1, pages) |
| 304 | } |
| 305 | |
| 306 | func TestListAddressesByNetwork(t *testing.T) { |
| 307 | th.SetupHTTP() |
| 308 | defer th.TeardownHTTP() |
| 309 | HandleNetworkAddressListSuccessfully(t) |
| 310 | |
| 311 | expected := ListNetworkAddressesExpected |
| 312 | pages := 0 |
| 313 | err := ListAddressesByNetwork(client.ServiceClient(), "asdfasdfasdf", "public").EachPage(func(page pagination.Page) (bool, error) { |
| 314 | pages++ |
| 315 | |
| 316 | actual, err := ExtractNetworkAddresses(page) |
| 317 | th.AssertNoErr(t, err) |
| 318 | |
Jon Perritt | b51ba9c | 2015-02-23 10:56:35 -0700 | [diff] [blame] | 319 | if len(actual) != 2 { |
| 320 | t.Fatalf("Expected 2 addresses, got %d", len(actual)) |
Jon Perritt | 5cb4948 | 2015-02-19 12:19:58 -0700 | [diff] [blame] | 321 | } |
| 322 | th.CheckDeepEquals(t, expected, actual) |
| 323 | |
| 324 | return true, nil |
| 325 | }) |
| 326 | th.AssertNoErr(t, err) |
| 327 | th.CheckEquals(t, 1, pages) |
| 328 | } |
Kevin Pike | 92e10b5 | 2015-04-10 15:16:57 -0700 | [diff] [blame^] | 329 | |
| 330 | func TestMarshalPersonality(t *testing.T) { |
| 331 | name := "test" |
| 332 | contents := []byte("asdfasdf") |
| 333 | |
| 334 | personality := Personality{ |
| 335 | File{ |
| 336 | Path: name, |
| 337 | Contents: contents, |
| 338 | }, |
| 339 | } |
| 340 | |
| 341 | actual := personality.Marshal() |
| 342 | |
| 343 | if actual[0]["path"] != name { |
| 344 | t.Fatal("file path incorrect") |
| 345 | } |
| 346 | |
| 347 | if actual[0]["contents"] != base64.StdEncoding.EncodeToString(contents) { |
| 348 | t.Fatal("file contents incorrect") |
| 349 | } |
| 350 | } |