@@ -59,6 +59,24 @@ func TestList(t *testing.T) {
5959 "gateway_ip": "192.0.0.1",
6060 "cidr": "192.0.0.0/8",
6161 "id": "54d6f61d-db07-451c-9ab3-b9609b6b6f0b"
62+ },
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"
6280 }
6381 ]
6482}
@@ -112,6 +130,24 @@ func TestList(t *testing.T) {
112130 CIDR : "192.0.0.0/8" ,
113131 ID : "54d6f61d-db07-451c-9ab3-b9609b6b6f0b" ,
114132 },
133+ 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+ },
115151 }
116152
117153 th .CheckDeepEquals (t , expected , actual )
@@ -270,6 +306,145 @@ func TestCreate(t *testing.T) {
270306 th .AssertEquals (t , s .ID , "3b80198d-4f7b-4f77-9ef5-774d54e17126" )
271307}
272308
309+ func TestCreateNoGateway (t * testing.T ) {
310+ th .SetupHTTP ()
311+ defer th .TeardownHTTP ()
312+
313+ th .Mux .HandleFunc ("/v2.0/subnets" , func (w http.ResponseWriter , r * http.Request ) {
314+ th .TestMethod (t , r , "POST" )
315+ th .TestHeader (t , r , "X-Auth-Token" , fake .TokenID )
316+ th .TestHeader (t , r , "Content-Type" , "application/json" )
317+ th .TestHeader (t , r , "Accept" , "application/json" )
318+ th .TestJSONRequest (t , r , `
319+ {
320+ "subnet": {
321+ "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a23",
322+ "ip_version": 4,
323+ "cidr": "192.168.1.0/24",
324+ "gateway_ip": null,
325+ "allocation_pools": [
326+ {
327+ "start": "192.168.1.2",
328+ "end": "192.168.1.254"
329+ }
330+ ]
331+ }
332+ }
333+ ` )
334+
335+ w .Header ().Add ("Content-Type" , "application/json" )
336+ w .WriteHeader (http .StatusCreated )
337+
338+ fmt .Fprintf (w , `
339+ {
340+ "subnet": {
341+ "name": "",
342+ "enable_dhcp": true,
343+ "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a23",
344+ "tenant_id": "4fd44f30292945e481c7b8a0c8908869",
345+ "allocation_pools": [
346+ {
347+ "start": "192.168.1.2",
348+ "end": "192.168.1.254"
349+ }
350+ ],
351+ "host_routes": [],
352+ "ip_version": 4,
353+ "gateway_ip": null,
354+ "cidr": "192.168.1.0/24",
355+ "id": "54d6f61d-db07-451c-9ab3-b9609b6b6f0c"
356+ }
357+ }
358+ ` )
359+ })
360+
361+ opts := CreateOpts {
362+ NetworkID : "d32019d3-bc6e-4319-9c1d-6722fc136a23" ,
363+ IPVersion : 4 ,
364+ CIDR : "192.168.1.0/24" ,
365+ NoGateway : true ,
366+ 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+
394+ func TestCreateInvalidGatewayConfig (t * testing.T ) {
395+ th .SetupHTTP ()
396+ defer th .TeardownHTTP ()
397+
398+ th .Mux .HandleFunc ("/v2.0/subnets" , func (w http.ResponseWriter , r * http.Request ) {
399+ th .TestMethod (t , r , "POST" )
400+ th .TestHeader (t , r , "X-Auth-Token" , fake .TokenID )
401+ th .TestHeader (t , r , "Content-Type" , "application/json" )
402+ th .TestHeader (t , r , "Accept" , "application/json" )
403+ th .TestJSONRequest (t , r , `
404+ {
405+ "subnet": {
406+ "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a23",
407+ "ip_version": 4,
408+ "cidr": "192.168.1.0/24",
409+ "gateway_ip": "192.168.1.1",
410+ "allocation_pools": [
411+ {
412+ "start": "192.168.1.2",
413+ "end": "192.168.1.254"
414+ }
415+ ]
416+ }
417+ }
418+ ` )
419+
420+ w .Header ().Add ("Content-Type" , "application/json" )
421+ w .WriteHeader (http .StatusCreated )
422+ })
423+
424+ opts := CreateOpts {
425+ NetworkID : "d32019d3-bc6e-4319-9c1d-6722fc136a23" ,
426+ IPVersion : 4 ,
427+ CIDR : "192.168.1.0/24" ,
428+ NoGateway : true ,
429+ GatewayIP : "192.168.1.1" ,
430+ AllocationPools : []AllocationPool {
431+ AllocationPool {
432+ Start : "192.168.1.2" ,
433+ End : "192.168.1.254" ,
434+ },
435+ },
436+ DNSNameservers : []string {},
437+ }
438+ _ , err := Create (fake .ServiceClient (), opts ).Extract ()
439+ if err == nil {
440+ t .Fatalf ("Expected an error, got none" )
441+ }
442+
443+ if err != errInvalidGatewayConfig {
444+ t .Fatalf ("Exected errInvalidGateway but got: %s" , err )
445+ }
446+ }
447+
273448func TestRequiredCreateOpts (t * testing.T ) {
274449 res := Create (fake .ServiceClient (), CreateOpts {})
275450 if res .Err == nil {
0 commit comments