blob: 987064ada642c1b1f7fc80b2ecaedddd4b95faff [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
Jamie Hannaforda2976ab2014-10-09 10:32:58 +02008 fake "github.com/rackspace/gophercloud/openstack/networking/v2/common"
Jamie Hannaford0708c002014-09-17 16:08:49 +02009 "github.com/rackspace/gophercloud/pagination"
10 th "github.com/rackspace/gophercloud/testhelper"
11)
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"
62 }
63 ]
64}
65 `)
66 })
67
68 count := 0
69
Jamie Hannaford58b008f2014-10-06 10:07:47 +020070 List(fake.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Jamie Hannaford0708c002014-09-17 16:08:49 +020071 count++
72 actual, err := ExtractSubnets(page)
73 if err != nil {
74 t.Errorf("Failed to extract subnets: %v", err)
75 return false, nil
76 }
77
78 expected := []Subnet{
79 Subnet{
80 Name: "private-subnet",
81 EnableDHCP: true,
82 NetworkID: "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
83 TenantID: "26a7980765d0414dbc1fc1f88cdb7e6e",
Jamie Hannaford965ae702014-09-22 14:58:19 +020084 DNSNameservers: []string{},
Jamie Hannaford0708c002014-09-17 16:08:49 +020085 AllocationPools: []AllocationPool{
86 AllocationPool{
87 Start: "10.0.0.2",
88 End: "10.0.0.254",
89 },
90 },
Jamie Hannafordf2835402014-09-23 11:01:21 +020091 HostRoutes: []HostRoute{},
Jamie Hannaford0708c002014-09-17 16:08:49 +020092 IPVersion: 4,
93 GatewayIP: "10.0.0.1",
94 CIDR: "10.0.0.0/24",
95 ID: "08eae331-0402-425a-923c-34f7cfe39c1b",
96 },
97 Subnet{
98 Name: "my_subnet",
99 EnableDHCP: true,
100 NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22",
101 TenantID: "4fd44f30292945e481c7b8a0c8908869",
Jamie Hannaford965ae702014-09-22 14:58:19 +0200102 DNSNameservers: []string{},
Jamie Hannaford0708c002014-09-17 16:08:49 +0200103 AllocationPools: []AllocationPool{
104 AllocationPool{
105 Start: "192.0.0.2",
106 End: "192.255.255.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: "192.0.0.1",
112 CIDR: "192.0.0.0/8",
113 ID: "54d6f61d-db07-451c-9ab3-b9609b6b6f0b",
114 },
115 }
116
117 th.CheckDeepEquals(t, expected, actual)
118
119 return true, nil
120 })
121
122 if count != 1 {
123 t.Errorf("Expected 1 page, got %d", count)
124 }
125}
126
127func TestGet(t *testing.T) {
128 th.SetupHTTP()
129 defer th.TeardownHTTP()
130
Jamie Hannaforda2976ab2014-10-09 10:32:58 +0200131 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 +0200132 th.TestMethod(t, r, "GET")
Jamie Hannaford58b008f2014-10-06 10:07:47 +0200133 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jamie Hannaford0708c002014-09-17 16:08:49 +0200134
135 w.Header().Add("Content-Type", "application/json")
136 w.WriteHeader(http.StatusOK)
137
138 fmt.Fprintf(w, `
139{
140 "subnet": {
141 "name": "my_subnet",
142 "enable_dhcp": true,
143 "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
144 "tenant_id": "4fd44f30292945e481c7b8a0c8908869",
145 "dns_nameservers": [],
146 "allocation_pools": [
147 {
148 "start": "192.0.0.2",
149 "end": "192.255.255.254"
150 }
151 ],
152 "host_routes": [],
153 "ip_version": 4,
154 "gateway_ip": "192.0.0.1",
155 "cidr": "192.0.0.0/8",
156 "id": "54d6f61d-db07-451c-9ab3-b9609b6b6f0b"
157 }
158}
159 `)
160 })
161
Jamie Hannaford58b008f2014-10-06 10:07:47 +0200162 s, err := Get(fake.ServiceClient(), "54d6f61d-db07-451c-9ab3-b9609b6b6f0b").Extract()
Jamie Hannaford0708c002014-09-17 16:08:49 +0200163 th.AssertNoErr(t, err)
164
165 th.AssertEquals(t, s.Name, "my_subnet")
166 th.AssertEquals(t, s.EnableDHCP, true)
167 th.AssertEquals(t, s.NetworkID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
168 th.AssertEquals(t, s.TenantID, "4fd44f30292945e481c7b8a0c8908869")
Jamie Hannaford965ae702014-09-22 14:58:19 +0200169 th.AssertDeepEquals(t, s.DNSNameservers, []string{})
Jamie Hannaford0708c002014-09-17 16:08:49 +0200170 th.AssertDeepEquals(t, s.AllocationPools, []AllocationPool{
171 AllocationPool{
172 Start: "192.0.0.2",
173 End: "192.255.255.254",
174 },
175 })
Jamie Hannafordf2835402014-09-23 11:01:21 +0200176 th.AssertDeepEquals(t, s.HostRoutes, []HostRoute{})
Jamie Hannaford0708c002014-09-17 16:08:49 +0200177 th.AssertEquals(t, s.IPVersion, 4)
178 th.AssertEquals(t, s.GatewayIP, "192.0.0.1")
179 th.AssertEquals(t, s.CIDR, "192.0.0.0/8")
180 th.AssertEquals(t, s.ID, "54d6f61d-db07-451c-9ab3-b9609b6b6f0b")
Jamie Hannaford89f9af22014-09-17 12:21:48 +0200181}
Jamie Hannaford63631432014-09-18 11:40:09 +0200182
183func TestCreate(t *testing.T) {
184 th.SetupHTTP()
185 defer th.TeardownHTTP()
186
Jamie Hannaforda2976ab2014-10-09 10:32:58 +0200187 th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford63631432014-09-18 11:40:09 +0200188 th.TestMethod(t, r, "POST")
Jamie Hannaford58b008f2014-10-06 10:07:47 +0200189 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jamie Hannaford63631432014-09-18 11:40:09 +0200190 th.TestHeader(t, r, "Content-Type", "application/json")
191 th.TestHeader(t, r, "Accept", "application/json")
192 th.TestJSONRequest(t, r, `
193{
194 "subnet": {
195 "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
196 "ip_version": 4,
Jamie Hannaford5d93f562014-10-09 10:53:32 +0200197 "cidr": "192.168.199.0/24",
198 "dns_nameservers": ["foo"],
199 "allocation_pools": [
200 {
201 "start": "192.168.199.2",
202 "end": "192.168.199.254"
203 }
204 ],
205 "host_routes": [{"destination":"","nexthop": "bar"}]
Jamie Hannaford63631432014-09-18 11:40:09 +0200206 }
207}
208 `)
209
210 w.Header().Add("Content-Type", "application/json")
211 w.WriteHeader(http.StatusCreated)
212
213 fmt.Fprintf(w, `
214{
215 "subnet": {
216 "name": "",
217 "enable_dhcp": true,
218 "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
219 "tenant_id": "4fd44f30292945e481c7b8a0c8908869",
220 "dns_nameservers": [],
221 "allocation_pools": [
222 {
223 "start": "192.168.199.2",
224 "end": "192.168.199.254"
225 }
226 ],
227 "host_routes": [],
228 "ip_version": 4,
229 "gateway_ip": "192.168.199.1",
230 "cidr": "192.168.199.0/24",
231 "id": "3b80198d-4f7b-4f77-9ef5-774d54e17126"
232 }
233}
234 `)
235 })
236
Jamie Hannaford5d93f562014-10-09 10:53:32 +0200237 opts := CreateOpts{
238 NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22",
239 IPVersion: 4,
240 CIDR: "192.168.199.0/24",
241 AllocationPools: []AllocationPool{
242 AllocationPool{
243 Start: "192.168.199.2",
244 End: "192.168.199.254",
245 },
246 },
247 DNSNameservers: []string{"foo"},
248 HostRoutes: []HostRoute{
249 HostRoute{NextHop: "bar"},
250 },
251 }
Jamie Hannaford58b008f2014-10-06 10:07:47 +0200252 s, err := Create(fake.ServiceClient(), opts).Extract()
Jamie Hannaford63631432014-09-18 11:40:09 +0200253 th.AssertNoErr(t, err)
254
255 th.AssertEquals(t, s.Name, "")
256 th.AssertEquals(t, s.EnableDHCP, true)
257 th.AssertEquals(t, s.NetworkID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
258 th.AssertEquals(t, s.TenantID, "4fd44f30292945e481c7b8a0c8908869")
Jamie Hannaford965ae702014-09-22 14:58:19 +0200259 th.AssertDeepEquals(t, s.DNSNameservers, []string{})
Jamie Hannaford63631432014-09-18 11:40:09 +0200260 th.AssertDeepEquals(t, s.AllocationPools, []AllocationPool{
261 AllocationPool{
262 Start: "192.168.199.2",
263 End: "192.168.199.254",
264 },
265 })
Jamie Hannafordf2835402014-09-23 11:01:21 +0200266 th.AssertDeepEquals(t, s.HostRoutes, []HostRoute{})
Jamie Hannaford63631432014-09-18 11:40:09 +0200267 th.AssertEquals(t, s.IPVersion, 4)
268 th.AssertEquals(t, s.GatewayIP, "192.168.199.1")
269 th.AssertEquals(t, s.CIDR, "192.168.199.0/24")
270 th.AssertEquals(t, s.ID, "3b80198d-4f7b-4f77-9ef5-774d54e17126")
271}
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200272
Jamie Hannaford5d93f562014-10-09 10:53:32 +0200273func TestRequiredCreateOpts(t *testing.T) {
274 res := Create(fake.ServiceClient(), CreateOpts{})
275 if res.Err == nil {
276 t.Fatalf("Expected error, got none")
277 }
278
279 res = Create(fake.ServiceClient(), CreateOpts{NetworkID: "foo"})
280 if res.Err == nil {
281 t.Fatalf("Expected error, got none")
282 }
283
284 res = Create(fake.ServiceClient(), CreateOpts{NetworkID: "foo", CIDR: "bar", IPVersion: 40})
285 if res.Err == nil {
286 t.Fatalf("Expected error, got none")
287 }
288}
289
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200290func TestUpdate(t *testing.T) {
291 th.SetupHTTP()
292 defer th.TeardownHTTP()
293
Jamie Hannaforda2976ab2014-10-09 10:32:58 +0200294 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 +0200295 th.TestMethod(t, r, "PUT")
Jamie Hannaford58b008f2014-10-06 10:07:47 +0200296 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200297 th.TestHeader(t, r, "Content-Type", "application/json")
298 th.TestHeader(t, r, "Accept", "application/json")
299 th.TestJSONRequest(t, r, `
300{
301 "subnet": {
Jamie Hannaford5d93f562014-10-09 10:53:32 +0200302 "name": "my_new_subnet",
303 "dns_nameservers": ["foo"],
304 "host_routes": [{"destination":"","nexthop": "bar"}]
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200305 }
306}
307 `)
308
309 w.Header().Add("Content-Type", "application/json")
310 w.WriteHeader(http.StatusCreated)
311
312 fmt.Fprintf(w, `
313{
314 "subnet": {
315 "name": "my_new_subnet",
316 "enable_dhcp": true,
317 "network_id": "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
318 "tenant_id": "26a7980765d0414dbc1fc1f88cdb7e6e",
319 "dns_nameservers": [],
320 "allocation_pools": [
321 {
322 "start": "10.0.0.2",
323 "end": "10.0.0.254"
324 }
325 ],
326 "host_routes": [],
327 "ip_version": 4,
328 "gateway_ip": "10.0.0.1",
329 "cidr": "10.0.0.0/24",
330 "id": "08eae331-0402-425a-923c-34f7cfe39c1b"
331 }
332}
333 `)
334 })
335
Jamie Hannaford5d93f562014-10-09 10:53:32 +0200336 opts := UpdateOpts{
337 Name: "my_new_subnet",
338 DNSNameservers: []string{"foo"},
339 HostRoutes: []HostRoute{
340 HostRoute{NextHop: "bar"},
341 },
342 }
Jamie Hannaford58b008f2014-10-06 10:07:47 +0200343 s, err := Update(fake.ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b", opts).Extract()
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200344 th.AssertNoErr(t, err)
345
346 th.AssertEquals(t, s.Name, "my_new_subnet")
347 th.AssertEquals(t, s.ID, "08eae331-0402-425a-923c-34f7cfe39c1b")
348}
349
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200350func TestDelete(t *testing.T) {
351 th.SetupHTTP()
352 defer th.TeardownHTTP()
353
Jamie Hannaforda2976ab2014-10-09 10:32:58 +0200354 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 +0200355 th.TestMethod(t, r, "DELETE")
Jamie Hannaford58b008f2014-10-06 10:07:47 +0200356 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200357 w.WriteHeader(http.StatusNoContent)
358 })
359
Jamie Hannaford58b008f2014-10-06 10:07:47 +0200360 res := Delete(fake.ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b")
Jamie Hannafordd9036422014-09-23 17:50:24 +0200361 th.AssertNoErr(t, res.Err)
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200362}