Debian Codegraph @

localservices/cloudapi/service_http_test.go

raw symbols SCIP
1 //
2 // gosdc - Go library to interact with the Joyent CloudAPI
3 //
4 // CloudAPI double testing service - HTTP API tests
5 //
6 // Copyright (c) Joyent Inc.
7 //
8
9 package cloudapi_test
10
11 import (
12 "bytes"
13 "encoding/json"
14 "fmt"
15 "io/ioutil"
16 "net/http"
17 "path"
18 "strconv"
19 "strings"
20
21 gc "launchpad.net/gocheck"
22
23 "github.com/joyent/gocommon/testing"
24 "github.com/joyent/gosdc/cloudapi"
25 lc "github.com/joyent/gosdc/localservices/cloudapi"
26 )
27
28 type CloudAPIHTTPSuite struct {
29 testing.HTTPSuite
30 service *lc.CloudAPI
31 }
32
33 var _ = gc.Suite(&CloudAPIHTTPSuite{})
34
35 type CloudAPIHTTPSSuite struct {
36 testing.HTTPSuite
37 service *lc.CloudAPI
38 }
39
40 var _ = gc.Suite(&CloudAPIHTTPSSuite{HTTPSuite: testing.HTTPSuite{UseTLS: true}})
41
42 func (s *CloudAPIHTTPSuite) SetUpSuite(c *gc.C) {
43 s.HTTPSuite.SetUpSuite(c)
44 c.Assert(s.Server.URL[:7], gc.Equals, "http://")
45 s.service = lc.New(s.Server.URL, testUserAccount)
46 }
47
48 func (s *CloudAPIHTTPSuite) TearDownSuite(c *gc.C) {
49 s.HTTPSuite.TearDownSuite(c)
50 }
51
52 func (s *CloudAPIHTTPSuite) SetUpTest(c *gc.C) {
53 s.HTTPSuite.SetUpTest(c)
54 s.service.SetupHTTP(s.Mux)
55 }
56
57 func (s *CloudAPIHTTPSuite) TearDownTest(c *gc.C) {
58 s.HTTPSuite.TearDownTest(c)
59 }
60
61 // assertJSON asserts the passed http.Response's body can be
62 // unmarshalled into the given expected object, populating it with the
63 // successfully parsed data.
64 func assertJSON(c *gc.C, resp *http.Response, expected interface{}) {
65 body, err := ioutil.ReadAll(resp.Body)
66 defer resp.Body.Close()
67 c.Assert(err, gc.IsNil)
68 err = json.Unmarshal(body, &expected)
69 c.Assert(err, gc.IsNil)
70 }
71
72 // assertBody asserts the passed http.Response's body matches the
73 // expected response, replacing any variables in the expected body.
74 func assertBody(c *gc.C, resp *http.Response, expected *lc.ErrorResponse) {
75 body, err := ioutil.ReadAll(resp.Body)
76 defer resp.Body.Close()
77 c.Assert(err, gc.IsNil)
78 expBody := expected.Body
79 // cast to string for easier asserts debugging
80 c.Assert(string(body), gc.Equals, string(expBody))
81 }
82
83 // sendRequest constructs an HTTP request from the parameters and
84 // sends it, returning the response or an error.
85 func (s *CloudAPIHTTPSuite) sendRequest(method, path string, body []byte, headers http.Header) (*http.Response, error) {
86 if headers == nil {
87 headers = make(http.Header)
88 }
89 requestURL := "http://" + s.service.Hostname + strings.TrimLeft(path, "/")
90 req, err := http.NewRequest(method, requestURL, bytes.NewReader(body))
91 if err != nil {
92 return nil, err
93 }
94 req.Close = true
95 for header, values := range headers {
96 for _, value := range values {
97 req.Header.Add(header, value)
98 }
99 }
100 // workaround for https://code.google.com/p/go/issues/detail?id=4454
101 req.Header.Set("Content-Length", strconv.Itoa(len(body)))
102 return http.DefaultClient.Do(req)
103 }
104
105 // jsonRequest serializes the passed body object to JSON and sends a
106 // the request with authRequest().
107 func (s *CloudAPIHTTPSuite) jsonRequest(method, path string, body interface{}, headers http.Header) (*http.Response, error) {
108 jsonBody, err := json.Marshal(body)
109 if err != nil {
110 return nil, err
111 }
112 return s.sendRequest(method, path, jsonBody, headers)
113 }
114
115 // Helpers
116 func (s *CloudAPIHTTPSuite) createKey(c *gc.C, keyName, key string) *cloudapi.Key {
117 opts := cloudapi.CreateKeyOpts{Name: keyName, Key: key}
118 optsByte, err := json.Marshal(opts)
119 c.Assert(err, gc.IsNil)
120 resp, err := s.sendRequest("POST", path.Join(testUserAccount, "keys"), optsByte, nil)
121 c.Assert(err, gc.IsNil)
122 c.Assert(resp.StatusCode, gc.Equals, http.StatusCreated)
123 k := &cloudapi.Key{}
124 assertJSON(c, resp, k)
125
126 return k
127 }
128
129 func (s *CloudAPIHTTPSuite) deleteKey(c *gc.C, keyName string) {
130 resp, err := s.sendRequest("DELETE", path.Join(testUserAccount, "keys", keyName), nil, nil)
131 c.Assert(err, gc.IsNil)
132 c.Assert(resp.StatusCode, gc.Equals, http.StatusNoContent)
133 }
134
135 func (s *CloudAPIHTTPSuite) createMachine(c *gc.C, name, pkg, image string, metadata, tags map[string]string) *cloudapi.Machine {
136 opts := cloudapi.CreateMachineOpts{Name: name, Image: image, Package: pkg, Tags: tags, Metadata: metadata}
137 optsByte, err := json.Marshal(opts)
138 c.Assert(err, gc.IsNil)
139 resp, err := s.sendRequest("POST", path.Join(testUserAccount, "machines"), optsByte, nil)
140 c.Assert(err, gc.IsNil)
141 c.Assert(resp.StatusCode, gc.Equals, http.StatusCreated)
142 m := &cloudapi.Machine{}
143 assertJSON(c, resp, m)
144
145 return m
146 }
147
148 func (s *CloudAPIHTTPSuite) getMachine(c *gc.C, machineId string, expected *cloudapi.Machine) {
149 resp, err := s.sendRequest("GET", path.Join(testUserAccount, "machines", machineId), nil, nil)
150 c.Assert(err, gc.IsNil)
151 c.Assert(resp.StatusCode, gc.Equals, http.StatusOK)
152 assertJSON(c, resp, expected)
153 }
154
155 func (s *CloudAPIHTTPSuite) deleteMachine(c *gc.C, machineId string) {
156 resp, err := s.sendRequest("POST", fmt.Sprintf("%s?action=stop", path.Join(testUserAccount, "machines", machineId)), nil, nil)
157 c.Assert(err, gc.IsNil)
158 c.Assert(resp.StatusCode, gc.Equals, http.StatusAccepted)
159 resp, err = s.sendRequest("DELETE", path.Join(testUserAccount, "machines", machineId), nil, nil)
160 c.Assert(err, gc.IsNil)
161 c.Assert(resp.StatusCode, gc.Equals, http.StatusNoContent)
162 }
163
164 // Helper method to create a test firewall rule
165 func (s *CloudAPIHTTPSuite) createFirewallRule(c *gc.C) *cloudapi.FirewallRule {
166 opts := cloudapi.CreateFwRuleOpts{Rule: testFwRule, Enabled: true}
167 optsByte, err := json.Marshal(opts)
168 c.Assert(err, gc.IsNil)
169 resp, err := s.sendRequest("POST", path.Join(testUserAccount, "fwrules"), optsByte, nil)
170 c.Assert(err, gc.IsNil)
171 c.Assert(resp.StatusCode, gc.Equals, http.StatusCreated)
172 r := &cloudapi.FirewallRule{}
173 assertJSON(c, resp, r)
174
175 return r
176 }
177
178 // Helper method to a test firewall rule
179 func (s *CloudAPIHTTPSuite) deleteFwRule(c *gc.C, fwRuleId string) {
180 resp, err := s.sendRequest("DELETE", path.Join(testUserAccount, "fwrules", fwRuleId), nil, nil)
181 c.Assert(err, gc.IsNil)
182 c.Assert(resp.StatusCode, gc.Equals, http.StatusNoContent)
183 }
184
185 // SimpleTest defines a simple request without a body and expected response.
186 type SimpleTest struct {
187 method string
188 url string
189 headers http.Header
190 expect *lc.ErrorResponse
191 }
192
193 func (s *CloudAPIHTTPSuite) simpleTests() []SimpleTest {
194 var simpleTests = []SimpleTest{
195 {
196 method: "GET",
197 url: "/",
198 headers: make(http.Header),
199 expect: lc.ErrNotFound,
200 },
201 {
202 method: "POST",
203 url: "/",
204 headers: make(http.Header),
205 expect: lc.ErrNotFound,
206 },
207 {
208 method: "DELETE",
209 url: "/",
210 headers: make(http.Header),
211 expect: lc.ErrNotFound,
212 },
213 {
214 method: "PUT",
215 url: "/",
216 headers: make(http.Header),
217 expect: lc.ErrNotFound,
218 },
219 {
220 method: "GET",
221 url: "/any",
222 headers: make(http.Header),
223 expect: lc.ErrNotFound,
224 },
225 {
226 method: "POST",
227 url: "/any",
228 headers: make(http.Header),
229 expect: lc.ErrNotFound,
230 },
231 {
232 method: "DELETE",
233 url: "/any",
234 headers: make(http.Header),
235 expect: lc.ErrNotFound,
236 },
237 {
238 method: "PUT",
239 url: "/any",
240 headers: make(http.Header),
241 expect: lc.ErrNotFound,
242 },
243 {
244 method: "PUT",
245 url: path.Join(testUserAccount, "keys"),
246 headers: make(http.Header),
247 expect: lc.ErrNotAllowed,
248 },
249 {
250 method: "PUT",
251 url: path.Join(testUserAccount, "images"),
252 headers: make(http.Header),
253 expect: lc.ErrNotAllowed,
254 },
255 {
256 method: "POST",
257 url: path.Join(testUserAccount, "packages"),
258 headers: make(http.Header),
259 expect: lc.ErrNotAllowed,
260 },
261 {
262 method: "PUT",
263 url: path.Join(testUserAccount, "packages"),
264 headers: make(http.Header),
265 expect: lc.ErrNotAllowed,
266 },
267 {
268 method: "DELETE",
269 url: path.Join(testUserAccount, "packages"),
270 headers: make(http.Header),
271 expect: lc.ErrNotAllowed,
272 },
273 {
274 method: "PUT",
275 url: path.Join(testUserAccount, "machines"),
276 headers: make(http.Header),
277 expect: lc.ErrNotAllowed,
278 },
279 {
280 method: "PUT",
281 url: path.Join(testUserAccount, "fwrules"),
282 headers: make(http.Header),
283 expect: lc.ErrNotAllowed,
284 },
285 {
286 method: "POST",
287 url: path.Join(testUserAccount, "networks"),
288 headers: make(http.Header),
289 expect: lc.ErrNotAllowed,
290 },
291 {
292 method: "PUT",
293 url: path.Join(testUserAccount, "networks"),
294 headers: make(http.Header),
295 expect: lc.ErrNotAllowed,
296 },
297 {
298 method: "DELETE",
299 url: path.Join(testUserAccount, "networks"),
300 headers: make(http.Header),
301 expect: lc.ErrNotAllowed,
302 },
303 }
304 return simpleTests
305 }
306
307 func (s *CloudAPIHTTPSuite) TestSimpleRequestTests(c *gc.C) {
308 simpleTests := s.simpleTests()
309 for i, t := range simpleTests {
310 c.Logf("#%d. %s %s -> %d", i, t.method, t.url, t.expect.Code)
311 if t.headers == nil {
312 t.headers = make(http.Header)
313 }
314 var (
315 resp *http.Response
316 err error
317 )
318 resp, err = s.sendRequest(t.method, t.url, nil, t.headers)
319 c.Assert(err, gc.IsNil)
320 c.Assert(resp.StatusCode, gc.Equals, t.expect.Code)
321 assertBody(c, resp, t.expect)
322 }
323 }
324
325 // Tests for Keys API
326 func (s *CloudAPIHTTPSuite) TestListKeys(c *gc.C) {
327 var expected []cloudapi.Key
328 k := s.createKey(c, testKeyName, testKey)
329 defer s.deleteKey(c, testKeyName)
330
331 resp, err := s.sendRequest("GET", path.Join(testUserAccount, "keys"), nil, nil)
332 c.Assert(err, gc.IsNil)
333 c.Assert(resp.StatusCode, gc.Equals, http.StatusOK)
334 assertJSON(c, resp, &expected)
335 for _, key := range expected {
336 if c.Check(&key, gc.DeepEquals, k) {
337 c.SucceedNow()
338 }
339 }
340 c.Fatalf("Obtained keys [%s] do not contain test key [%s]", expected, k)
341 }
342
343 func (s *CloudAPIHTTPSuite) TestGetKey(c *gc.C) {
344 var expected cloudapi.Key
345 k := s.createKey(c, testKeyName, testKey)
346 defer s.deleteKey(c, testKeyName)
347
348 resp, err := s.sendRequest("GET", path.Join(testUserAccount, "keys", testKeyName), nil, nil)
349 c.Assert(err, gc.IsNil)
350 c.Assert(resp.StatusCode, gc.Equals, http.StatusOK)
351 assertJSON(c, resp, &expected)
352 c.Assert(&expected, gc.DeepEquals, k)
353 }
354
355 func (s *CloudAPIHTTPSuite) TestCreateKey(c *gc.C) {
356 k := s.createKey(c, testKeyName, testKey)
357 defer s.deleteKey(c, testKeyName)
358
359 c.Assert(k.Name, gc.Equals, testKeyName)
360 c.Assert(k.Key, gc.Equals, testKey)
361 }
362
363 func (s *CloudAPIHTTPSuite) TestDeleteKey(c *gc.C) {
364 s.createKey(c, testKeyName, testKey)
365 s.deleteKey(c, testKeyName)
366 }
367
368 // Tests for Images API
369 func (s *CloudAPIHTTPSuite) TestListImages(c *gc.C) {
370 var expected []cloudapi.Image
371
372 resp, err := s.sendRequest("GET", path.Join(testUserAccount, "images"), nil, nil)
373 c.Assert(err, gc.IsNil)
374 c.Assert(resp.StatusCode, gc.Equals, http.StatusOK)
375 assertJSON(c, resp, &expected)
376 c.Assert(len(expected), gc.Equals, 6)
377 }
378
379 func (s *CloudAPIHTTPSuite) TestGetImage(c *gc.C) {
380 var expected cloudapi.Image
381
382 resp, err := s.sendRequest("GET", path.Join(testUserAccount, "images", testImage), nil, nil)
383 c.Assert(err, gc.IsNil)
384 c.Assert(resp.StatusCode, gc.Equals, http.StatusOK)
385 assertJSON(c, resp, &expected)
386 c.Assert(expected.Id, gc.Equals, "11223344-0a0a-ff99-11bb-0a1b2c3d4e5f")
387 c.Assert(expected.Name, gc.Equals, "ubuntu12.04")
388 c.Assert(expected.OS, gc.Equals, "linux")
389 c.Assert(expected.Version, gc.Equals, "2.3.1")
390 c.Assert(expected.Type, gc.Equals, "virtualmachine")
391 c.Assert(expected.Description, gc.Equals, "Test Ubuntu 12.04 image (64 bit)")
392 c.Assert(expected.PublishedAt, gc.Equals, "2014-01-20T16:12:31Z")
393 c.Assert(expected.Public, gc.Equals, true)
394 c.Assert(expected.State, gc.Equals, "active")
395 }
396
397 // Tests for Packages API
398 func (s *CloudAPIHTTPSuite) TestListPackages(c *gc.C) {
399 var expected []cloudapi.Package
400
401 resp, err := s.sendRequest("GET", path.Join(testUserAccount, "packages"), nil, nil)
402 c.Assert(err, gc.IsNil)
403 c.Assert(resp.StatusCode, gc.Equals, http.StatusOK)
404 assertJSON(c, resp, &expected)
405 c.Assert(len(expected), gc.Equals, 4)
406 }
407
408 func (s *CloudAPIHTTPSuite) TestGetPackage(c *gc.C) {
409 var expected cloudapi.Package
410
411 resp, err := s.sendRequest("GET", path.Join(testUserAccount, "packages", testPackage), nil, nil)
412 c.Assert(err, gc.IsNil)
413 c.Assert(resp.StatusCode, gc.Equals, http.StatusOK)
414 assertJSON(c, resp, &expected)
415 c.Assert(expected.Name, gc.Equals, "Small")
416 c.Assert(expected.Memory, gc.Equals, 1024)
417 c.Assert(expected.Disk, gc.Equals, 16384)
418 c.Assert(expected.Swap, gc.Equals, 2048)
419 c.Assert(expected.VCPUs, gc.Equals, 1)
420 c.Assert(expected.Default, gc.Equals, true)
421 c.Assert(expected.Id, gc.Equals, "11223344-1212-abab-3434-aabbccddeeff")
422 c.Assert(expected.Version, gc.Equals, "1.0.2")
423 }
424
425 // Tests for Machines API
426 func (s *CloudAPIHTTPSuite) TestListMachines(c *gc.C) {
427 var expected []cloudapi.Machine
428 m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil)
429 defer s.deleteMachine(c, m.Id)
430
431 resp, err := s.sendRequest("GET", path.Join(testUserAccount, "machines"), nil, nil)
432 c.Assert(err, gc.IsNil)
433 c.Assert(resp.StatusCode, gc.Equals, http.StatusOK)
434 assertJSON(c, resp, &expected)
435 for _, machine := range expected {
436 if machine.Id == m.Id {
437 c.SucceedNow()
438 }
439 }
440 c.Fatalf("Obtained machine [%s] do not contain test machine [%s]", expected, m)
441 }
442
443 /*func (s *CloudAPIHTTPSuite) TestCountMachines(c *gc.C) {
444 m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil)
445 defer s.deleteMachine(c, m.Id)
446
447 resp, err := s.sendRequest("HEAD", path.Join(testUserAccount, "machines"), nil, nil)
448 c.Assert(err, gc.IsNil)
449 c.Assert(resp.StatusCode, gc.Equals, http.StatusOK)
450 fmt.Printf("Got response %q\n", resp)
451 assertBody(c, resp, &lc.ErrorResponse{Body: "1"})
452 } */
453
454 func (s *CloudAPIHTTPSuite) TestGetMachine(c *gc.C) {
455 var expected cloudapi.Machine
456 m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil)
457 defer s.deleteMachine(c, m.Id)
458
459 s.getMachine(c, m.Id, &expected)
460 c.Assert(expected.Name, gc.Equals, testMachineName)
461 c.Assert(expected.Package, gc.Equals, testPackage)
462 c.Assert(expected.Image, gc.Equals, testImage)
463 }
464
465 func (s *CloudAPIHTTPSuite) TestCreateMachine(c *gc.C) {
466 m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil)
467 defer s.deleteMachine(c, m.Id)
468
469 c.Assert(m.Name, gc.Equals, testMachineName)
470 c.Assert(m.Package, gc.Equals, testPackage)
471 c.Assert(m.Image, gc.Equals, testImage)
472 c.Assert(m.Type, gc.Equals, "virtualmachine")
473 c.Assert(len(m.IPs), gc.Equals, 2)
474 c.Assert(m.State, gc.Equals, "running")
475 }
476
477 func (s *CloudAPIHTTPSuite) TestStartMachine(c *gc.C) {
478 var expected cloudapi.Machine
479 m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil)
480 defer s.deleteMachine(c, m.Id)
481
482 resp, err := s.sendRequest("POST", fmt.Sprintf("%s?action=start", path.Join(testUserAccount, "machines", m.Id)), nil, nil)
483 c.Assert(err, gc.IsNil)
484 c.Assert(resp.StatusCode, gc.Equals, http.StatusAccepted)
485
486 s.getMachine(c, m.Id, &expected)
487 c.Assert(expected.State, gc.Equals, "running")
488 }
489
490 func (s *CloudAPIHTTPSuite) TestStopMachine(c *gc.C) {
491 var expected cloudapi.Machine
492 m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil)
493 defer s.deleteMachine(c, m.Id)
494
495 resp, err := s.sendRequest("POST", fmt.Sprintf("%s?action=stop", path.Join(testUserAccount, "machines", m.Id)), nil, nil)
496 c.Assert(err, gc.IsNil)
497 c.Assert(resp.StatusCode, gc.Equals, http.StatusAccepted)
498
499 s.getMachine(c, m.Id, &expected)
500 c.Assert(expected.State, gc.Equals, "stopped")
501 }
502
503 func (s *CloudAPIHTTPSuite) TestRebootMachine(c *gc.C) {
504 var expected cloudapi.Machine
505 m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil)
506 defer s.deleteMachine(c, m.Id)
507
508 resp, err := s.sendRequest("POST", fmt.Sprintf("%s?action=reboot", path.Join(testUserAccount, "machines", m.Id)), nil, nil)
509 c.Assert(err, gc.IsNil)
510 c.Assert(resp.StatusCode, gc.Equals, http.StatusAccepted)
511
512 s.getMachine(c, m.Id, &expected)
513 c.Assert(expected.State, gc.Equals, "running")
514 }
515
516 func (s *CloudAPIHTTPSuite) TestResizeMachine(c *gc.C) {
517 var expected cloudapi.Machine
518 m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil)
519 defer s.deleteMachine(c, m.Id)
520
521 resp, err := s.sendRequest("POST", fmt.Sprintf("%s?action=resize&package=Medium", path.Join(testUserAccount, "machines", m.Id)), nil, nil)
522 c.Assert(err, gc.IsNil)
523 c.Assert(resp.StatusCode, gc.Equals, http.StatusAccepted)
524
525 s.getMachine(c, m.Id, &expected)
526 c.Assert(expected.Package, gc.Equals, "Medium")
527 c.Assert(expected.Memory, gc.Equals, 2048)
528 c.Assert(expected.Disk, gc.Equals, 32768)
529 }
530
531 func (s *CloudAPIHTTPSuite) TestRenameMachine(c *gc.C) {
532 var expected cloudapi.Machine
533 m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil)
534 defer s.deleteMachine(c, m.Id)
535
536 resp, err := s.sendRequest("POST", fmt.Sprintf("%s?action=rename&name=new-test-name", path.Join(testUserAccount, "machines", m.Id)), nil, nil)
537 c.Assert(err, gc.IsNil)
538 c.Assert(resp.StatusCode, gc.Equals, http.StatusAccepted)
539
540 s.getMachine(c, m.Id, &expected)
541 c.Assert(expected.Name, gc.Equals, "new-test-name")
542 }
543
544 func (s *CloudAPIHTTPSuite) TestListMachinesFirewallRules(c *gc.C) {
545 var expected []cloudapi.FirewallRule
546 m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil)
547 defer s.deleteMachine(c, m.Id)
548
549 resp, err := s.sendRequest("GET", path.Join(testUserAccount, "machines", m.Id, "fwrules"), nil, nil)
550 c.Assert(err, gc.IsNil)
551 c.Assert(resp.StatusCode, gc.Equals, http.StatusOK)
552 assertJSON(c, resp, &expected)
553 }
554
555 func (s *CloudAPIHTTPSuite) TestEnableFirewallMachine(c *gc.C) {
556 m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil)
557 defer s.deleteMachine(c, m.Id)
558
559 resp, err := s.sendRequest("POST", fmt.Sprintf("%s?action=enable_firewall", path.Join(testUserAccount, "machines", m.Id)), nil, nil)
560 c.Assert(err, gc.IsNil)
561 c.Assert(resp.StatusCode, gc.Equals, http.StatusAccepted)
562 }
563
564 func (s *CloudAPIHTTPSuite) TestDisableFirewallMachine(c *gc.C) {
565 m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil)
566 defer s.deleteMachine(c, m.Id)
567
568 resp, err := s.sendRequest("POST", fmt.Sprintf("%s?action=disable_firewall", path.Join(testUserAccount, "machines", m.Id)), nil, nil)
569 c.Assert(err, gc.IsNil)
570 c.Assert(resp.StatusCode, gc.Equals, http.StatusAccepted)
571 }
572
573 func (s *CloudAPIHTTPSuite) TestDeleteMachine(c *gc.C) {
574 m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil)
575 s.deleteMachine(c, m.Id)
576 }
577
578 // Tests for FirewallRules API
579 func (s *CloudAPIHTTPSuite) TestCreateFirewallRule(c *gc.C) {
580 testFwRule := s.createFirewallRule(c)
581
582 // cleanup
583 s.deleteFwRule(c, testFwRule.Id)
584 }
585
586 func (s *CloudAPIHTTPSuite) TestListFirewallRules(c *gc.C) {
587 var expected []cloudapi.FirewallRule
588 testFwRule := s.createFirewallRule(c)
589 defer s.deleteFwRule(c, testFwRule.Id)
590
591 resp, err := s.sendRequest("GET", path.Join(testUserAccount, "fwrules"), nil, nil)
592 c.Assert(err, gc.IsNil)
593 c.Assert(resp.StatusCode, gc.Equals, http.StatusOK)
594 assertJSON(c, resp, &expected)
595 }
596
597 func (s *CloudAPIHTTPSuite) TestGetFirewallRule(c *gc.C) {
598 var expected cloudapi.FirewallRule
599 testFwRule := s.createFirewallRule(c)
600 defer s.deleteFwRule(c, testFwRule.Id)
601
602 resp, err := s.sendRequest("GET", path.Join(testUserAccount, "fwrules", testFwRule.Id), nil, nil)
603 c.Assert(err, gc.IsNil)
604 c.Assert(resp.StatusCode, gc.Equals, http.StatusOK)
605 assertJSON(c, resp, &expected)
606 }
607
608 func (s *CloudAPIHTTPSuite) TestUpdateFirewallRule(c *gc.C) {
609 var expected cloudapi.FirewallRule
610 testFwRule := s.createFirewallRule(c)
611 defer s.deleteFwRule(c, testFwRule.Id)
612
613 opts := cloudapi.CreateFwRuleOpts{Rule: testUpdatedFwRule, Enabled: true}
614 optsByte, err := json.Marshal(opts)
615 c.Assert(err, gc.IsNil)
616 resp, err := s.sendRequest("POST", path.Join(testUserAccount, "fwrules", testFwRule.Id), optsByte, nil)
617 c.Assert(err, gc.IsNil)
618 c.Assert(resp.StatusCode, gc.Equals, http.StatusOK)
619 assertJSON(c, resp, &expected)
620 c.Assert(expected.Rule, gc.Equals, testUpdatedFwRule)
621 }
622
623 func (s *CloudAPIHTTPSuite) TestEnableFirewallRule(c *gc.C) {
624 var expected cloudapi.FirewallRule
625 testFwRule := s.createFirewallRule(c)
626 defer s.deleteFwRule(c, testFwRule.Id)
627
628 resp, err := s.sendRequest("POST", path.Join(testUserAccount, "fwrules", testFwRule.Id, "enable"), nil, nil)
629 c.Assert(err, gc.IsNil)
630 c.Assert(resp.StatusCode, gc.Equals, http.StatusOK)
631 assertJSON(c, resp, &expected)
632 c.Assert(expected.Enabled, gc.Equals, true)
633 }
634
635 func (s *CloudAPIHTTPSuite) TestDisableFirewallRule(c *gc.C) {
636 var expected cloudapi.FirewallRule
637 testFwRule := s.createFirewallRule(c)
638 defer s.deleteFwRule(c, testFwRule.Id)
639
640 resp, err := s.sendRequest("POST", path.Join(testUserAccount, "fwrules", testFwRule.Id, "disable"), nil, nil)
641 c.Assert(err, gc.IsNil)
642 c.Assert(resp.StatusCode, gc.Equals, http.StatusOK)
643 assertJSON(c, resp, &expected)
644 c.Assert(expected.Enabled, gc.Equals, false)
645 }
646
647 func (s *CloudAPIHTTPSuite) TestDeleteFirewallRule(c *gc.C) {
648 testFwRule := s.createFirewallRule(c)
649
650 s.deleteFwRule(c, testFwRule.Id)
651 }
652
653 // tests for Networks API
654 func (s *CloudAPIHTTPSuite) TestListNetworks(c *gc.C) {
655 var expected []cloudapi.Network
656
657 resp, err := s.sendRequest("GET", path.Join(testUserAccount, "networks"), nil, nil)
658 c.Assert(err, gc.IsNil)
659 c.Assert(resp.StatusCode, gc.Equals, http.StatusOK)
660 assertJSON(c, resp, &expected)
661 }
662
663 func (s *CloudAPIHTTPSuite) TestGetNetwork(c *gc.C) {
664 var expected cloudapi.Network
665
666 resp, err := s.sendRequest("GET", path.Join(testUserAccount, "networks", testNetworkID), nil, nil)
667 c.Assert(err, gc.IsNil)
668 c.Assert(resp.StatusCode, gc.Equals, http.StatusOK)
669 assertJSON(c, resp, &expected)
670 c.Assert(expected, gc.DeepEquals, cloudapi.Network{
671 Id: testNetworkID,
672 Name: "Test-Joyent-Public",
673 Public: true,
674 Description: "",
675 })
676 }
677
678 func (s *CloudAPIHTTPSuite) TestGetServices(c *gc.C) {
679 //var expected cloudapi.ServiceInstance
680 resp, err := s.sendRequest("GET", path.Join(testUserAccount, "services"), nil, nil)
681 c.Assert(err, gc.IsNil)
682 c.Assert(resp.StatusCode, gc.Equals, http.StatusOK)
683
684 }