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