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