Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 1 | package subnets |
| 2 | |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 8 | fake "github.com/gophercloud/gophercloud/openstack/networking/v2/common" |
| 9 | "github.com/gophercloud/gophercloud/pagination" |
| 10 | th "github.com/gophercloud/gophercloud/testhelper" |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 11 | ) |
| 12 | |
Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 13 | func TestList(t *testing.T) { |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 14 | th.SetupHTTP() |
| 15 | defer th.TeardownHTTP() |
Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 16 | |
Jamie Hannaford | a2976ab | 2014-10-09 10:32:58 +0200 | [diff] [blame] | 17 | th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 18 | th.TestMethod(t, r, "GET") |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 19 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 20 | |
| 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 Topjian | f92ae6c | 2016-04-06 21:24:43 -0600 | [diff] [blame] | 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" |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 80 | } |
| 81 | ] |
| 82 | } |
| 83 | `) |
| 84 | }) |
| 85 | |
| 86 | count := 0 |
| 87 | |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 88 | List(fake.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 89 | 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 Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 102 | DNSNameservers: []string{}, |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 103 | AllocationPools: []AllocationPool{ |
| 104 | AllocationPool{ |
| 105 | Start: "10.0.0.2", |
| 106 | End: "10.0.0.254", |
| 107 | }, |
| 108 | }, |
Jamie Hannaford | f283540 | 2014-09-23 11:01:21 +0200 | [diff] [blame] | 109 | HostRoutes: []HostRoute{}, |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 110 | 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 Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 120 | DNSNameservers: []string{}, |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 121 | AllocationPools: []AllocationPool{ |
| 122 | AllocationPool{ |
| 123 | Start: "192.0.0.2", |
| 124 | End: "192.255.255.254", |
| 125 | }, |
| 126 | }, |
Jamie Hannaford | f283540 | 2014-09-23 11:01:21 +0200 | [diff] [blame] | 127 | HostRoutes: []HostRoute{}, |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 128 | IPVersion: 4, |
| 129 | GatewayIP: "192.0.0.1", |
| 130 | CIDR: "192.0.0.0/8", |
| 131 | ID: "54d6f61d-db07-451c-9ab3-b9609b6b6f0b", |
| 132 | }, |
Joe Topjian | f92ae6c | 2016-04-06 21:24:43 -0600 | [diff] [blame] | 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 | }, |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 151 | } |
| 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 | |
| 163 | func TestGet(t *testing.T) { |
| 164 | th.SetupHTTP() |
| 165 | defer th.TeardownHTTP() |
| 166 | |
Jamie Hannaford | a2976ab | 2014-10-09 10:32:58 +0200 | [diff] [blame] | 167 | th.Mux.HandleFunc("/v2.0/subnets/54d6f61d-db07-451c-9ab3-b9609b6b6f0b", func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 168 | th.TestMethod(t, r, "GET") |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 169 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 170 | |
| 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 Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 198 | s, err := Get(fake.ServiceClient(), "54d6f61d-db07-451c-9ab3-b9609b6b6f0b").Extract() |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 199 | 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 Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 205 | th.AssertDeepEquals(t, s.DNSNameservers, []string{}) |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 206 | th.AssertDeepEquals(t, s.AllocationPools, []AllocationPool{ |
| 207 | AllocationPool{ |
| 208 | Start: "192.0.0.2", |
| 209 | End: "192.255.255.254", |
| 210 | }, |
| 211 | }) |
Jamie Hannaford | f283540 | 2014-09-23 11:01:21 +0200 | [diff] [blame] | 212 | th.AssertDeepEquals(t, s.HostRoutes, []HostRoute{}) |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 213 | 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 Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 217 | } |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 218 | |
| 219 | func TestCreate(t *testing.T) { |
| 220 | th.SetupHTTP() |
| 221 | defer th.TeardownHTTP() |
| 222 | |
Jamie Hannaford | a2976ab | 2014-10-09 10:32:58 +0200 | [diff] [blame] | 223 | th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 224 | th.TestMethod(t, r, "POST") |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 225 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 226 | 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, |
jrperritt | bc54861 | 2016-04-13 17:03:59 -0500 | [diff] [blame] | 233 | "gateway_ip": null, |
Jamie Hannaford | 5d93f56 | 2014-10-09 10:53:32 +0200 | [diff] [blame] | 234 | "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 Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 243 | } |
| 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 Hannaford | 5d93f56 | 2014-10-09 10:53:32 +0200 | [diff] [blame] | 274 | 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 Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 289 | s, err := Create(fake.ServiceClient(), opts).Extract() |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 290 | 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 Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 296 | th.AssertDeepEquals(t, s.DNSNameservers, []string{}) |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 297 | th.AssertDeepEquals(t, s.AllocationPools, []AllocationPool{ |
| 298 | AllocationPool{ |
| 299 | Start: "192.168.199.2", |
| 300 | End: "192.168.199.254", |
| 301 | }, |
| 302 | }) |
Jamie Hannaford | f283540 | 2014-09-23 11:01:21 +0200 | [diff] [blame] | 303 | th.AssertDeepEquals(t, s.HostRoutes, []HostRoute{}) |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 304 | 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 Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 309 | |
Joe Topjian | f92ae6c | 2016-04-06 21:24:43 -0600 | [diff] [blame] | 310 | func 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 Topjian | f92ae6c | 2016-04-06 21:24:43 -0600 | [diff] [blame] | 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 | |
Jamie Hannaford | 5d93f56 | 2014-10-09 10:53:32 +0200 | [diff] [blame] | 394 | func 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 Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 411 | func TestUpdate(t *testing.T) { |
| 412 | th.SetupHTTP() |
| 413 | defer th.TeardownHTTP() |
| 414 | |
Jamie Hannaford | a2976ab | 2014-10-09 10:32:58 +0200 | [diff] [blame] | 415 | th.Mux.HandleFunc("/v2.0/subnets/08eae331-0402-425a-923c-34f7cfe39c1b", func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 416 | th.TestMethod(t, r, "PUT") |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 417 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 418 | 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 Hannaford | 5d93f56 | 2014-10-09 10:53:32 +0200 | [diff] [blame] | 423 | "name": "my_new_subnet", |
| 424 | "dns_nameservers": ["foo"], |
| 425 | "host_routes": [{"destination":"","nexthop": "bar"}] |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 426 | } |
| 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 Hannaford | 5d93f56 | 2014-10-09 10:53:32 +0200 | [diff] [blame] | 457 | opts := UpdateOpts{ |
| 458 | Name: "my_new_subnet", |
| 459 | DNSNameservers: []string{"foo"}, |
| 460 | HostRoutes: []HostRoute{ |
| 461 | HostRoute{NextHop: "bar"}, |
| 462 | }, |
| 463 | } |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 464 | s, err := Update(fake.ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b", opts).Extract() |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 465 | 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 Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 471 | func TestDelete(t *testing.T) { |
| 472 | th.SetupHTTP() |
| 473 | defer th.TeardownHTTP() |
| 474 | |
Jamie Hannaford | a2976ab | 2014-10-09 10:32:58 +0200 | [diff] [blame] | 475 | th.Mux.HandleFunc("/v2.0/subnets/08eae331-0402-425a-923c-34f7cfe39c1b", func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 476 | th.TestMethod(t, r, "DELETE") |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 477 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 478 | w.WriteHeader(http.StatusNoContent) |
| 479 | }) |
| 480 | |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 481 | res := Delete(fake.ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b") |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 482 | th.AssertNoErr(t, res.Err) |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 483 | } |