blob: fafc6fb302059f9923cc6a66bfebae108122d81c [file] [log] [blame]
Jon Perritt53c8a3a2014-11-24 07:46:35 -07001package subnets
2
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
8 os "github.com/rackspace/gophercloud/openstack/networking/v2/subnets"
9 "github.com/rackspace/gophercloud/pagination"
10 fake "github.com/rackspace/gophercloud/rackspace/networking/v2/common"
11 th "github.com/rackspace/gophercloud/testhelper"
12)
13
14func TestList(t *testing.T) {
15 th.SetupHTTP()
16 defer th.TeardownHTTP()
17
18 th.Mux.HandleFunc("/subnets", func(w http.ResponseWriter, r *http.Request) {
19 th.TestMethod(t, r, "GET")
20 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
21
22 w.Header().Add("Content-Type", "application/json")
23 w.WriteHeader(http.StatusOK)
24
25 fmt.Fprintf(w, `
26{
27 "subnets": [
28 {
29 "name": "private-subnet",
30 "enable_dhcp": true,
31 "network_id": "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
32 "tenant_id": "26a7980765d0414dbc1fc1f88cdb7e6e",
33 "dns_nameservers": [],
34 "allocation_pools": [
35 {
36 "start": "10.0.0.2",
37 "end": "10.0.0.254"
38 }
39 ],
40 "host_routes": [],
41 "ip_version": 4,
42 "gateway_ip": "10.0.0.1",
43 "cidr": "10.0.0.0/24",
44 "id": "08eae331-0402-425a-923c-34f7cfe39c1b"
45 },
46 {
47 "name": "my_subnet",
48 "enable_dhcp": true,
49 "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
50 "tenant_id": "4fd44f30292945e481c7b8a0c8908869",
51 "dns_nameservers": [],
52 "allocation_pools": [
53 {
54 "start": "192.0.0.2",
55 "end": "192.255.255.254"
56 }
57 ],
58 "host_routes": [],
59 "ip_version": 4,
60 "gateway_ip": "192.0.0.1",
61 "cidr": "192.0.0.0/8",
62 "id": "54d6f61d-db07-451c-9ab3-b9609b6b6f0b"
63 }
64 ]
65}
66 `)
67 })
68
69 count := 0
70
71 List(fake.ServiceClient(), os.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
72 count++
73 actual, err := os.ExtractSubnets(page)
74 if err != nil {
75 t.Errorf("Failed to extract subnets: %v", err)
76 return false, nil
77 }
78
79 expected := []os.Subnet{
80 os.Subnet{
81 Name: "private-subnet",
82 EnableDHCP: true,
83 NetworkID: "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
84 TenantID: "26a7980765d0414dbc1fc1f88cdb7e6e",
85 DNSNameservers: []string{},
86 AllocationPools: []os.AllocationPool{
87 os.AllocationPool{
88 Start: "10.0.0.2",
89 End: "10.0.0.254",
90 },
91 },
92 HostRoutes: []os.HostRoute{},
93 IPVersion: 4,
94 GatewayIP: "10.0.0.1",
95 CIDR: "10.0.0.0/24",
96 ID: "08eae331-0402-425a-923c-34f7cfe39c1b",
97 },
98 os.Subnet{
99 Name: "my_subnet",
100 EnableDHCP: true,
101 NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22",
102 TenantID: "4fd44f30292945e481c7b8a0c8908869",
103 DNSNameservers: []string{},
104 AllocationPools: []os.AllocationPool{
105 os.AllocationPool{
106 Start: "192.0.0.2",
107 End: "192.255.255.254",
108 },
109 },
110 HostRoutes: []os.HostRoute{},
111 IPVersion: 4,
112 GatewayIP: "192.0.0.1",
113 CIDR: "192.0.0.0/8",
114 ID: "54d6f61d-db07-451c-9ab3-b9609b6b6f0b",
115 },
116 }
117
118 th.CheckDeepEquals(t, expected, actual)
119
120 return true, nil
121 })
122
123 if count != 1 {
124 t.Errorf("Expected 1 page, got %d", count)
125 }
126}
127
128func TestGet(t *testing.T) {
129 th.SetupHTTP()
130 defer th.TeardownHTTP()
131
132 th.Mux.HandleFunc("/subnets/54d6f61d-db07-451c-9ab3-b9609b6b6f0b", func(w http.ResponseWriter, r *http.Request) {
133 th.TestMethod(t, r, "GET")
134 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
135
136 w.Header().Add("Content-Type", "application/json")
137 w.WriteHeader(http.StatusOK)
138
139 fmt.Fprintf(w, `
140{
141 "subnet": {
142 "name": "my_subnet",
143 "enable_dhcp": true,
144 "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
145 "tenant_id": "4fd44f30292945e481c7b8a0c8908869",
146 "dns_nameservers": [],
147 "allocation_pools": [
148 {
149 "start": "192.0.0.2",
150 "end": "192.255.255.254"
151 }
152 ],
153 "host_routes": [],
154 "ip_version": 4,
155 "gateway_ip": "192.0.0.1",
156 "cidr": "192.0.0.0/8",
157 "id": "54d6f61d-db07-451c-9ab3-b9609b6b6f0b"
158 }
159}
160 `)
161 })
162
163 s, err := Get(fake.ServiceClient(), "54d6f61d-db07-451c-9ab3-b9609b6b6f0b").Extract()
164 th.AssertNoErr(t, err)
165
166 th.AssertEquals(t, s.Name, "my_subnet")
167 th.AssertEquals(t, s.EnableDHCP, true)
168 th.AssertEquals(t, s.NetworkID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
169 th.AssertEquals(t, s.TenantID, "4fd44f30292945e481c7b8a0c8908869")
170 th.AssertDeepEquals(t, s.DNSNameservers, []string{})
171 th.AssertDeepEquals(t, s.AllocationPools, []os.AllocationPool{
172 os.AllocationPool{
173 Start: "192.0.0.2",
174 End: "192.255.255.254",
175 },
176 })
177 th.AssertDeepEquals(t, s.HostRoutes, []os.HostRoute{})
178 th.AssertEquals(t, s.IPVersion, 4)
179 th.AssertEquals(t, s.GatewayIP, "192.0.0.1")
180 th.AssertEquals(t, s.CIDR, "192.0.0.0/8")
181 th.AssertEquals(t, s.ID, "54d6f61d-db07-451c-9ab3-b9609b6b6f0b")
182}
183
184func TestCreate(t *testing.T) {
185 th.SetupHTTP()
186 defer th.TeardownHTTP()
187
188 th.Mux.HandleFunc("/subnets", func(w http.ResponseWriter, r *http.Request) {
189 th.TestMethod(t, r, "POST")
190 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
191 th.TestHeader(t, r, "Content-Type", "application/json")
192 th.TestHeader(t, r, "Accept", "application/json")
193 th.TestJSONRequest(t, r, `
194{
195 "subnet": {
196 "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
197 "ip_version": 4,
198 "cidr": "192.168.199.0/24",
199 "dns_nameservers": ["foo"],
200 "allocation_pools": [
201 {
202 "start": "192.168.199.2",
203 "end": "192.168.199.254"
204 }
205 ],
206 "host_routes": [{"destination":"","nexthop": "bar"}]
207 }
208}
209 `)
210
211 w.Header().Add("Content-Type", "application/json")
212 w.WriteHeader(http.StatusCreated)
213
214 fmt.Fprintf(w, `
215{
216 "subnet": {
217 "name": "",
218 "enable_dhcp": true,
219 "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
220 "tenant_id": "4fd44f30292945e481c7b8a0c8908869",
221 "dns_nameservers": [],
222 "allocation_pools": [
223 {
224 "start": "192.168.199.2",
225 "end": "192.168.199.254"
226 }
227 ],
228 "host_routes": [],
229 "ip_version": 4,
230 "gateway_ip": "192.168.199.1",
231 "cidr": "192.168.199.0/24",
232 "id": "3b80198d-4f7b-4f77-9ef5-774d54e17126"
233 }
234}
235 `)
236 })
237
238 opts := os.CreateOpts{
239 NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22",
240 IPVersion: 4,
241 CIDR: "192.168.199.0/24",
242 AllocationPools: []os.AllocationPool{
243 os.AllocationPool{
244 Start: "192.168.199.2",
245 End: "192.168.199.254",
246 },
247 },
248 DNSNameservers: []string{"foo"},
249 HostRoutes: []os.HostRoute{
250 os.HostRoute{NextHop: "bar"},
251 },
252 }
253 s, err := Create(fake.ServiceClient(), opts).Extract()
254 th.AssertNoErr(t, err)
255
256 th.AssertEquals(t, s.Name, "")
257 th.AssertEquals(t, s.EnableDHCP, true)
258 th.AssertEquals(t, s.NetworkID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
259 th.AssertEquals(t, s.TenantID, "4fd44f30292945e481c7b8a0c8908869")
260 th.AssertDeepEquals(t, s.DNSNameservers, []string{})
261 th.AssertDeepEquals(t, s.AllocationPools, []os.AllocationPool{
262 os.AllocationPool{
263 Start: "192.168.199.2",
264 End: "192.168.199.254",
265 },
266 })
267 th.AssertDeepEquals(t, s.HostRoutes, []os.HostRoute{})
268 th.AssertEquals(t, s.IPVersion, 4)
269 th.AssertEquals(t, s.GatewayIP, "192.168.199.1")
270 th.AssertEquals(t, s.CIDR, "192.168.199.0/24")
271 th.AssertEquals(t, s.ID, "3b80198d-4f7b-4f77-9ef5-774d54e17126")
272}
273
274func TestRequiredCreateOpts(t *testing.T) {
275 res := Create(fake.ServiceClient(), os.CreateOpts{})
276 if res.Err == nil {
277 t.Fatalf("Expected error, got none")
278 }
279
280 res = Create(fake.ServiceClient(), os.CreateOpts{NetworkID: "foo"})
281 if res.Err == nil {
282 t.Fatalf("Expected error, got none")
283 }
284
285 res = Create(fake.ServiceClient(), os.CreateOpts{NetworkID: "foo", CIDR: "bar", IPVersion: 40})
286 if res.Err == nil {
287 t.Fatalf("Expected error, got none")
288 }
289}
290
291func TestUpdate(t *testing.T) {
292 th.SetupHTTP()
293 defer th.TeardownHTTP()
294
295 th.Mux.HandleFunc("/subnets/08eae331-0402-425a-923c-34f7cfe39c1b", func(w http.ResponseWriter, r *http.Request) {
296 th.TestMethod(t, r, "PUT")
297 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
298 th.TestHeader(t, r, "Content-Type", "application/json")
299 th.TestHeader(t, r, "Accept", "application/json")
300 th.TestJSONRequest(t, r, `
301{
302 "subnet": {
303 "name": "my_new_subnet",
304 "dns_nameservers": ["foo"],
305 "host_routes": [{"destination":"","nexthop": "bar"}]
306 }
307}
308 `)
309
310 w.Header().Add("Content-Type", "application/json")
311 w.WriteHeader(http.StatusCreated)
312
313 fmt.Fprintf(w, `
314{
315 "subnet": {
316 "name": "my_new_subnet",
317 "enable_dhcp": true,
318 "network_id": "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
319 "tenant_id": "26a7980765d0414dbc1fc1f88cdb7e6e",
320 "dns_nameservers": [],
321 "allocation_pools": [
322 {
323 "start": "10.0.0.2",
324 "end": "10.0.0.254"
325 }
326 ],
327 "host_routes": [],
328 "ip_version": 4,
329 "gateway_ip": "10.0.0.1",
330 "cidr": "10.0.0.0/24",
331 "id": "08eae331-0402-425a-923c-34f7cfe39c1b"
332 }
333}
334 `)
335 })
336
337 opts := os.UpdateOpts{
338 Name: "my_new_subnet",
339 DNSNameservers: []string{"foo"},
340 HostRoutes: []os.HostRoute{
341 os.HostRoute{NextHop: "bar"},
342 },
343 }
344 s, err := Update(fake.ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b", opts).Extract()
345 th.AssertNoErr(t, err)
346
347 th.AssertEquals(t, s.Name, "my_new_subnet")
348 th.AssertEquals(t, s.ID, "08eae331-0402-425a-923c-34f7cfe39c1b")
349}
350
351func TestDelete(t *testing.T) {
352 th.SetupHTTP()
353 defer th.TeardownHTTP()
354
355 th.Mux.HandleFunc("/subnets/08eae331-0402-425a-923c-34f7cfe39c1b", func(w http.ResponseWriter, r *http.Request) {
356 th.TestMethod(t, r, "DELETE")
357 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
358 w.WriteHeader(http.StatusNoContent)
359 })
360
361 res := Delete(fake.ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b")
362 th.AssertNoErr(t, res.Err)
363}