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