blob: 6f44c1caba755fcddce45835d527c036ce0df845 [file] [log] [blame]
Jon Perritt24270c42014-10-21 21:11:04 -05001package networks
Jon Perritt44b1ea22014-10-22 00:13:23 -05002
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
8 "github.com/rackspace/gophercloud/pagination"
9 th "github.com/rackspace/gophercloud/testhelper"
10 fake "github.com/rackspace/gophercloud/testhelper/client"
11)
12
13func TestList(t *testing.T) {
14 th.SetupHTTP()
15 defer th.TeardownHTTP()
16
17 th.Mux.HandleFunc("/os-networksv2", func(w http.ResponseWriter, r *http.Request) {
18 th.TestMethod(t, r, "GET")
19 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
20
21 w.Header().Add("Content-Type", "application/json")
22 w.WriteHeader(http.StatusOK)
23
24 fmt.Fprintf(w, `
25{
26 "networks": [
27 {
28 "label": "test-network-1",
29 "cidr": "192.168.100.0/24",
30 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22"
31 },
32 {
33 "label": "test-network-2",
34 "cidr": "192.30.250.00/18",
35 "id": "db193ab3-96e3-4cb3-8fc5-05f4296d0324"
36 }
37 ]
38}
39 `)
40 })
41
42 client := fake.ServiceClient()
43 count := 0
44
Jon Perritt004d9c32014-10-23 13:08:43 -050045 err := List(client).EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt44b1ea22014-10-22 00:13:23 -050046 count++
47 actual, err := ExtractNetworks(page)
48 if err != nil {
49 t.Errorf("Failed to extract networks: %v", err)
50 return false, err
51 }
52
53 expected := []Network{
54 Network{
55 Label: "test-network-1",
56 CIDR: "192.168.100.0/24",
57 ID: "d32019d3-bc6e-4319-9c1d-6722fc136a22",
58 },
59 Network{
60 Label: "test-network-2",
61 CIDR: "192.30.250.00/18",
62 ID: "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
63 },
64 }
65
66 th.CheckDeepEquals(t, expected, actual)
67
68 return true, nil
69 })
Jon Perritt004d9c32014-10-23 13:08:43 -050070 th.AssertNoErr(t, err)
71 th.CheckEquals(t, 1, count)
Jon Perritt44b1ea22014-10-22 00:13:23 -050072}
73
74func TestGet(t *testing.T) {
75 th.SetupHTTP()
76 defer th.TeardownHTTP()
77
78 th.Mux.HandleFunc("/os-networksv2/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
79 th.TestMethod(t, r, "GET")
80 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
81
82 w.Header().Add("Content-Type", "application/json")
83 w.WriteHeader(http.StatusOK)
84
85 fmt.Fprintf(w, `
86{
87 "network": {
88 "label": "test-network-1",
89 "cidr": "192.168.100.0/24",
90 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22"
91 }
92}
93 `)
94 })
95
96 n, err := Get(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
97 th.AssertNoErr(t, err)
98
99 th.AssertEquals(t, n.CIDR, "192.168.100.0/24")
100 th.AssertEquals(t, n.Label, "test-network-1")
101 th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
102}
103
104func TestCreate(t *testing.T) {
105 th.SetupHTTP()
106 defer th.TeardownHTTP()
107
108 th.Mux.HandleFunc("/os-networksv2", func(w http.ResponseWriter, r *http.Request) {
109 th.TestMethod(t, r, "POST")
110 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
111 th.TestHeader(t, r, "Content-Type", "application/json")
112 th.TestHeader(t, r, "Accept", "application/json")
113 th.TestJSONRequest(t, r, `
114{
115 "network": {
116 "label": "test-network-1",
117 "cidr": "192.168.100.0/24"
118 }
119}
120 `)
121
122 w.Header().Add("Content-Type", "application/json")
123 w.WriteHeader(http.StatusCreated)
124
125 fmt.Fprintf(w, `
126{
127 "network": {
128 "label": "test-network-1",
129 "cidr": "192.168.100.0/24",
130 "id": "4e8e5957-649f-477b-9e5b-f1f75b21c03c"
131 }
132}
133 `)
134 })
135
136 options := CreateOpts{Label: "test-network-1", CIDR: "192.168.100.0/24"}
137 n, err := Create(fake.ServiceClient(), options).Extract()
138 th.AssertNoErr(t, err)
139
140 th.AssertEquals(t, n.Label, "test-network-1")
141 th.AssertEquals(t, n.ID, "4e8e5957-649f-477b-9e5b-f1f75b21c03c")
142}
143
144func TestDelete(t *testing.T) {
145 th.SetupHTTP()
146 defer th.TeardownHTTP()
147
148 th.Mux.HandleFunc("/os-networksv2/4e8e5957-649f-477b-9e5b-f1f75b21c03c", func(w http.ResponseWriter, r *http.Request) {
149 th.TestMethod(t, r, "DELETE")
150 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
151 w.WriteHeader(http.StatusNoContent)
152 })
153
Jon Perritt26ff5b72014-10-23 11:54:39 -0500154 res := Delete(fake.ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c")
Jon Perritt44b1ea22014-10-22 00:13:23 -0500155 th.AssertNoErr(t, res.Err)
156}