blob: 4241c63f91aeb0fef1c5628c2ce8345ec4d772f3 [file] [log] [blame]
Jamie Hannaford89f9af22014-09-17 12:21:48 +02001package subnets
2
Jamie Hannaford0708c002014-09-17 16:08:49 +02003import (
4 "fmt"
5 "net/http"
6 "testing"
7
Jon Perritt27249f42016-02-18 10:35:59 -06008 fake "github.com/gophercloud/gophercloud/openstack/networking/v2/common"
9 "github.com/gophercloud/gophercloud/pagination"
10 th "github.com/gophercloud/gophercloud/testhelper"
Jamie Hannaford0708c002014-09-17 16:08:49 +020011)
12
Jamie Hannaford89f9af22014-09-17 12:21:48 +020013func TestList(t *testing.T) {
Jamie Hannaford0708c002014-09-17 16:08:49 +020014 th.SetupHTTP()
15 defer th.TeardownHTTP()
Jamie Hannaford89f9af22014-09-17 12:21:48 +020016
Jamie Hannaforda2976ab2014-10-09 10:32:58 +020017 th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford0708c002014-09-17 16:08:49 +020018 th.TestMethod(t, r, "GET")
Jamie Hannaford58b008f2014-10-06 10:07:47 +020019 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jamie Hannaford0708c002014-09-17 16:08:49 +020020
21 w.Header().Add("Content-Type", "application/json")
22 w.WriteHeader(http.StatusOK)
23
24 fmt.Fprintf(w, `
25{
26 "subnets": [
27 {
28 "name": "private-subnet",
29 "enable_dhcp": true,
30 "network_id": "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
31 "tenant_id": "26a7980765d0414dbc1fc1f88cdb7e6e",
32 "dns_nameservers": [],
33 "allocation_pools": [
34 {
35 "start": "10.0.0.2",
36 "end": "10.0.0.254"
37 }
38 ],
39 "host_routes": [],
40 "ip_version": 4,
41 "gateway_ip": "10.0.0.1",
42 "cidr": "10.0.0.0/24",
43 "id": "08eae331-0402-425a-923c-34f7cfe39c1b"
44 },
45 {
46 "name": "my_subnet",
47 "enable_dhcp": true,
48 "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
49 "tenant_id": "4fd44f30292945e481c7b8a0c8908869",
50 "dns_nameservers": [],
51 "allocation_pools": [
52 {
53 "start": "192.0.0.2",
54 "end": "192.255.255.254"
55 }
56 ],
57 "host_routes": [],
58 "ip_version": 4,
59 "gateway_ip": "192.0.0.1",
60 "cidr": "192.0.0.0/8",
61 "id": "54d6f61d-db07-451c-9ab3-b9609b6b6f0b"
Joe Topjianf92ae6c2016-04-06 21:24:43 -060062 },
63 {
64 "name": "my_gatewayless_subnet",
65 "enable_dhcp": true,
66 "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a23",
67 "tenant_id": "4fd44f30292945e481c7b8a0c8908869",
68 "dns_nameservers": [],
69 "allocation_pools": [
70 {
71 "start": "192.168.1.2",
72 "end": "192.168.1.254"
73 }
74 ],
75 "host_routes": [],
76 "ip_version": 4,
77 "gateway_ip": null,
78 "cidr": "192.168.1.0/24",
79 "id": "54d6f61d-db07-451c-9ab3-b9609b6b6f0c"
Jamie Hannaford0708c002014-09-17 16:08:49 +020080 }
81 ]
82}
83 `)
84 })
85
86 count := 0
87
Jamie Hannaford58b008f2014-10-06 10:07:47 +020088 List(fake.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Jamie Hannaford0708c002014-09-17 16:08:49 +020089 count++
90 actual, err := ExtractSubnets(page)
91 if err != nil {
92 t.Errorf("Failed to extract subnets: %v", err)
93 return false, nil
94 }
95
96 expected := []Subnet{
97 Subnet{
98 Name: "private-subnet",
99 EnableDHCP: true,
100 NetworkID: "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
101 TenantID: "26a7980765d0414dbc1fc1f88cdb7e6e",
Jamie Hannaford965ae702014-09-22 14:58:19 +0200102 DNSNameservers: []string{},
Jamie Hannaford0708c002014-09-17 16:08:49 +0200103 AllocationPools: []AllocationPool{
104 AllocationPool{
105 Start: "10.0.0.2",
106 End: "10.0.0.254",
107 },
108 },
Jamie Hannafordf2835402014-09-23 11:01:21 +0200109 HostRoutes: []HostRoute{},
Jamie Hannaford0708c002014-09-17 16:08:49 +0200110 IPVersion: 4,
111 GatewayIP: "10.0.0.1",
112 CIDR: "10.0.0.0/24",
113 ID: "08eae331-0402-425a-923c-34f7cfe39c1b",
114 },
115 Subnet{
116 Name: "my_subnet",
117 EnableDHCP: true,
118 NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22",
119 TenantID: "4fd44f30292945e481c7b8a0c8908869",
Jamie Hannaford965ae702014-09-22 14:58:19 +0200120 DNSNameservers: []string{},
Jamie Hannaford0708c002014-09-17 16:08:49 +0200121 AllocationPools: []AllocationPool{
122 AllocationPool{
123 Start: "192.0.0.2",
124 End: "192.255.255.254",
125 },
126 },
Jamie Hannafordf2835402014-09-23 11:01:21 +0200127 HostRoutes: []HostRoute{},
Jamie Hannaford0708c002014-09-17 16:08:49 +0200128 IPVersion: 4,
129 GatewayIP: "192.0.0.1",
130 CIDR: "192.0.0.0/8",
131 ID: "54d6f61d-db07-451c-9ab3-b9609b6b6f0b",
132 },
Joe Topjianf92ae6c2016-04-06 21:24:43 -0600133 Subnet{
134 Name: "my_gatewayless_subnet",
135 EnableDHCP: true,
136 NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a23",
137 TenantID: "4fd44f30292945e481c7b8a0c8908869",
138 DNSNameservers: []string{},
139 AllocationPools: []AllocationPool{
140 AllocationPool{
141 Start: "192.168.1.2",
142 End: "192.168.1.254",
143 },
144 },
145 HostRoutes: []HostRoute{},
146 IPVersion: 4,
147 GatewayIP: "",
148 CIDR: "192.168.1.0/24",
149 ID: "54d6f61d-db07-451c-9ab3-b9609b6b6f0c",
150 },
Jamie Hannaford0708c002014-09-17 16:08:49 +0200151 }
152
153 th.CheckDeepEquals(t, expected, actual)
154
155 return true, nil
156 })
157
158 if count != 1 {
159 t.Errorf("Expected 1 page, got %d", count)
160 }
161}
162
163func TestGet(t *testing.T) {
164 th.SetupHTTP()
165 defer th.TeardownHTTP()
166
Jamie Hannaforda2976ab2014-10-09 10:32:58 +0200167 th.Mux.HandleFunc("/v2.0/subnets/54d6f61d-db07-451c-9ab3-b9609b6b6f0b", func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford0708c002014-09-17 16:08:49 +0200168 th.TestMethod(t, r, "GET")
Jamie Hannaford58b008f2014-10-06 10:07:47 +0200169 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jamie Hannaford0708c002014-09-17 16:08:49 +0200170
171 w.Header().Add("Content-Type", "application/json")
172 w.WriteHeader(http.StatusOK)
173
174 fmt.Fprintf(w, `
175{
176 "subnet": {
177 "name": "my_subnet",
178 "enable_dhcp": true,
179 "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
180 "tenant_id": "4fd44f30292945e481c7b8a0c8908869",
181 "dns_nameservers": [],
182 "allocation_pools": [
183 {
184 "start": "192.0.0.2",
185 "end": "192.255.255.254"
186 }
187 ],
188 "host_routes": [],
189 "ip_version": 4,
190 "gateway_ip": "192.0.0.1",
191 "cidr": "192.0.0.0/8",
192 "id": "54d6f61d-db07-451c-9ab3-b9609b6b6f0b"
193 }
194}
195 `)
196 })
197
Jamie Hannaford58b008f2014-10-06 10:07:47 +0200198 s, err := Get(fake.ServiceClient(), "54d6f61d-db07-451c-9ab3-b9609b6b6f0b").Extract()
Jamie Hannaford0708c002014-09-17 16:08:49 +0200199 th.AssertNoErr(t, err)
200
201 th.AssertEquals(t, s.Name, "my_subnet")
202 th.AssertEquals(t, s.EnableDHCP, true)
203 th.AssertEquals(t, s.NetworkID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
204 th.AssertEquals(t, s.TenantID, "4fd44f30292945e481c7b8a0c8908869")
Jamie Hannaford965ae702014-09-22 14:58:19 +0200205 th.AssertDeepEquals(t, s.DNSNameservers, []string{})
Jamie Hannaford0708c002014-09-17 16:08:49 +0200206 th.AssertDeepEquals(t, s.AllocationPools, []AllocationPool{
207 AllocationPool{
208 Start: "192.0.0.2",
209 End: "192.255.255.254",
210 },
211 })
Jamie Hannafordf2835402014-09-23 11:01:21 +0200212 th.AssertDeepEquals(t, s.HostRoutes, []HostRoute{})
Jamie Hannaford0708c002014-09-17 16:08:49 +0200213 th.AssertEquals(t, s.IPVersion, 4)
214 th.AssertEquals(t, s.GatewayIP, "192.0.0.1")
215 th.AssertEquals(t, s.CIDR, "192.0.0.0/8")
216 th.AssertEquals(t, s.ID, "54d6f61d-db07-451c-9ab3-b9609b6b6f0b")
Jamie Hannaford89f9af22014-09-17 12:21:48 +0200217}
Jamie Hannaford63631432014-09-18 11:40:09 +0200218
219func TestCreate(t *testing.T) {
220 th.SetupHTTP()
221 defer th.TeardownHTTP()
222
Jamie Hannaforda2976ab2014-10-09 10:32:58 +0200223 th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford63631432014-09-18 11:40:09 +0200224 th.TestMethod(t, r, "POST")
Jamie Hannaford58b008f2014-10-06 10:07:47 +0200225 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jamie Hannaford63631432014-09-18 11:40:09 +0200226 th.TestHeader(t, r, "Content-Type", "application/json")
227 th.TestHeader(t, r, "Accept", "application/json")
228 th.TestJSONRequest(t, r, `
229{
230 "subnet": {
231 "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
232 "ip_version": 4,
jrperrittbc548612016-04-13 17:03:59 -0500233 "gateway_ip": null,
Jamie Hannaford5d93f562014-10-09 10:53:32 +0200234 "cidr": "192.168.199.0/24",
235 "dns_nameservers": ["foo"],
236 "allocation_pools": [
237 {
238 "start": "192.168.199.2",
239 "end": "192.168.199.254"
240 }
241 ],
242 "host_routes": [{"destination":"","nexthop": "bar"}]
Jamie Hannaford63631432014-09-18 11:40:09 +0200243 }
244}
245 `)
246
247 w.Header().Add("Content-Type", "application/json")
248 w.WriteHeader(http.StatusCreated)
249
250 fmt.Fprintf(w, `
251{
252 "subnet": {
253 "name": "",
254 "enable_dhcp": true,
255 "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
256 "tenant_id": "4fd44f30292945e481c7b8a0c8908869",
257 "dns_nameservers": [],
258 "allocation_pools": [
259 {
260 "start": "192.168.199.2",
261 "end": "192.168.199.254"
262 }
263 ],
264 "host_routes": [],
265 "ip_version": 4,
266 "gateway_ip": "192.168.199.1",
267 "cidr": "192.168.199.0/24",
268 "id": "3b80198d-4f7b-4f77-9ef5-774d54e17126"
269 }
270}
271 `)
272 })
273
Jamie Hannaford5d93f562014-10-09 10:53:32 +0200274 opts := CreateOpts{
275 NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22",
276 IPVersion: 4,
277 CIDR: "192.168.199.0/24",
278 AllocationPools: []AllocationPool{
279 AllocationPool{
280 Start: "192.168.199.2",
281 End: "192.168.199.254",
282 },
283 },
284 DNSNameservers: []string{"foo"},
285 HostRoutes: []HostRoute{
286 HostRoute{NextHop: "bar"},
287 },
288 }
Jamie Hannaford58b008f2014-10-06 10:07:47 +0200289 s, err := Create(fake.ServiceClient(), opts).Extract()
Jamie Hannaford63631432014-09-18 11:40:09 +0200290 th.AssertNoErr(t, err)
291
292 th.AssertEquals(t, s.Name, "")
293 th.AssertEquals(t, s.EnableDHCP, true)
294 th.AssertEquals(t, s.NetworkID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
295 th.AssertEquals(t, s.TenantID, "4fd44f30292945e481c7b8a0c8908869")
Jamie Hannaford965ae702014-09-22 14:58:19 +0200296 th.AssertDeepEquals(t, s.DNSNameservers, []string{})
Jamie Hannaford63631432014-09-18 11:40:09 +0200297 th.AssertDeepEquals(t, s.AllocationPools, []AllocationPool{
298 AllocationPool{
299 Start: "192.168.199.2",
300 End: "192.168.199.254",
301 },
302 })
Jamie Hannafordf2835402014-09-23 11:01:21 +0200303 th.AssertDeepEquals(t, s.HostRoutes, []HostRoute{})
Jamie Hannaford63631432014-09-18 11:40:09 +0200304 th.AssertEquals(t, s.IPVersion, 4)
305 th.AssertEquals(t, s.GatewayIP, "192.168.199.1")
306 th.AssertEquals(t, s.CIDR, "192.168.199.0/24")
307 th.AssertEquals(t, s.ID, "3b80198d-4f7b-4f77-9ef5-774d54e17126")
308}
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200309
Joe Topjianf92ae6c2016-04-06 21:24:43 -0600310func TestCreateNoGateway(t *testing.T) {
311 th.SetupHTTP()
312 defer th.TeardownHTTP()
313
314 th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) {
315 th.TestMethod(t, r, "POST")
316 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
317 th.TestHeader(t, r, "Content-Type", "application/json")
318 th.TestHeader(t, r, "Accept", "application/json")
319 th.TestJSONRequest(t, r, `
320{
321 "subnet": {
322 "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a23",
323 "ip_version": 4,
324 "cidr": "192.168.1.0/24",
325 "gateway_ip": null,
326 "allocation_pools": [
327 {
328 "start": "192.168.1.2",
329 "end": "192.168.1.254"
330 }
331 ]
332 }
333}
334 `)
335
336 w.Header().Add("Content-Type", "application/json")
337 w.WriteHeader(http.StatusCreated)
338
339 fmt.Fprintf(w, `
340{
341 "subnet": {
342 "name": "",
343 "enable_dhcp": true,
344 "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a23",
345 "tenant_id": "4fd44f30292945e481c7b8a0c8908869",
346 "allocation_pools": [
347 {
348 "start": "192.168.1.2",
349 "end": "192.168.1.254"
350 }
351 ],
352 "host_routes": [],
353 "ip_version": 4,
354 "gateway_ip": null,
355 "cidr": "192.168.1.0/24",
356 "id": "54d6f61d-db07-451c-9ab3-b9609b6b6f0c"
357 }
358}
359 `)
360 })
361
362 opts := CreateOpts{
363 NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a23",
364 IPVersion: 4,
365 CIDR: "192.168.1.0/24",
Joe Topjianf92ae6c2016-04-06 21:24:43 -0600366 AllocationPools: []AllocationPool{
367 AllocationPool{
368 Start: "192.168.1.2",
369 End: "192.168.1.254",
370 },
371 },
372 DNSNameservers: []string{},
373 }
374 s, err := Create(fake.ServiceClient(), opts).Extract()
375 th.AssertNoErr(t, err)
376
377 th.AssertEquals(t, s.Name, "")
378 th.AssertEquals(t, s.EnableDHCP, true)
379 th.AssertEquals(t, s.NetworkID, "d32019d3-bc6e-4319-9c1d-6722fc136a23")
380 th.AssertEquals(t, s.TenantID, "4fd44f30292945e481c7b8a0c8908869")
381 th.AssertDeepEquals(t, s.AllocationPools, []AllocationPool{
382 AllocationPool{
383 Start: "192.168.1.2",
384 End: "192.168.1.254",
385 },
386 })
387 th.AssertDeepEquals(t, s.HostRoutes, []HostRoute{})
388 th.AssertEquals(t, s.IPVersion, 4)
389 th.AssertEquals(t, s.GatewayIP, "")
390 th.AssertEquals(t, s.CIDR, "192.168.1.0/24")
391 th.AssertEquals(t, s.ID, "54d6f61d-db07-451c-9ab3-b9609b6b6f0c")
392}
393
Jamie Hannaford5d93f562014-10-09 10:53:32 +0200394func TestRequiredCreateOpts(t *testing.T) {
395 res := Create(fake.ServiceClient(), CreateOpts{})
396 if res.Err == nil {
397 t.Fatalf("Expected error, got none")
398 }
399
400 res = Create(fake.ServiceClient(), CreateOpts{NetworkID: "foo"})
401 if res.Err == nil {
402 t.Fatalf("Expected error, got none")
403 }
404
405 res = Create(fake.ServiceClient(), CreateOpts{NetworkID: "foo", CIDR: "bar", IPVersion: 40})
406 if res.Err == nil {
407 t.Fatalf("Expected error, got none")
408 }
409}
410
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200411func TestUpdate(t *testing.T) {
412 th.SetupHTTP()
413 defer th.TeardownHTTP()
414
Jamie Hannaforda2976ab2014-10-09 10:32:58 +0200415 th.Mux.HandleFunc("/v2.0/subnets/08eae331-0402-425a-923c-34f7cfe39c1b", func(w http.ResponseWriter, r *http.Request) {
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200416 th.TestMethod(t, r, "PUT")
Jamie Hannaford58b008f2014-10-06 10:07:47 +0200417 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200418 th.TestHeader(t, r, "Content-Type", "application/json")
419 th.TestHeader(t, r, "Accept", "application/json")
420 th.TestJSONRequest(t, r, `
421{
422 "subnet": {
Jamie Hannaford5d93f562014-10-09 10:53:32 +0200423 "name": "my_new_subnet",
424 "dns_nameservers": ["foo"],
425 "host_routes": [{"destination":"","nexthop": "bar"}]
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200426 }
427}
428 `)
429
430 w.Header().Add("Content-Type", "application/json")
431 w.WriteHeader(http.StatusCreated)
432
433 fmt.Fprintf(w, `
434{
435 "subnet": {
436 "name": "my_new_subnet",
437 "enable_dhcp": true,
438 "network_id": "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
439 "tenant_id": "26a7980765d0414dbc1fc1f88cdb7e6e",
440 "dns_nameservers": [],
441 "allocation_pools": [
442 {
443 "start": "10.0.0.2",
444 "end": "10.0.0.254"
445 }
446 ],
447 "host_routes": [],
448 "ip_version": 4,
449 "gateway_ip": "10.0.0.1",
450 "cidr": "10.0.0.0/24",
451 "id": "08eae331-0402-425a-923c-34f7cfe39c1b"
452 }
453}
454 `)
455 })
456
Jamie Hannaford5d93f562014-10-09 10:53:32 +0200457 opts := UpdateOpts{
458 Name: "my_new_subnet",
459 DNSNameservers: []string{"foo"},
460 HostRoutes: []HostRoute{
461 HostRoute{NextHop: "bar"},
462 },
463 }
Jamie Hannaford58b008f2014-10-06 10:07:47 +0200464 s, err := Update(fake.ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b", opts).Extract()
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200465 th.AssertNoErr(t, err)
466
467 th.AssertEquals(t, s.Name, "my_new_subnet")
468 th.AssertEquals(t, s.ID, "08eae331-0402-425a-923c-34f7cfe39c1b")
469}
470
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200471func TestDelete(t *testing.T) {
472 th.SetupHTTP()
473 defer th.TeardownHTTP()
474
Jamie Hannaforda2976ab2014-10-09 10:32:58 +0200475 th.Mux.HandleFunc("/v2.0/subnets/08eae331-0402-425a-923c-34f7cfe39c1b", func(w http.ResponseWriter, r *http.Request) {
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200476 th.TestMethod(t, r, "DELETE")
Jamie Hannaford58b008f2014-10-06 10:07:47 +0200477 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200478 w.WriteHeader(http.StatusNoContent)
479 })
480
Jamie Hannaford58b008f2014-10-06 10:07:47 +0200481 res := Delete(fake.ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b")
Jamie Hannafordd9036422014-09-23 17:50:24 +0200482 th.AssertNoErr(t, res.Err)
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200483}