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