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