Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 1 | // +build acceptance networking |
| 2 | |
| 3 | package v2 |
| 4 | |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 5 | import ( |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/rackspace/gophercloud/openstack/networking/v2/networks" |
| 9 | "github.com/rackspace/gophercloud/openstack/networking/v2/subnets" |
| 10 | "github.com/rackspace/gophercloud/pagination" |
| 11 | th "github.com/rackspace/gophercloud/testhelper" |
| 12 | ) |
Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 13 | |
| 14 | func TestList(t *testing.T) { |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 15 | Setup(t) |
| 16 | defer Teardown() |
Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 17 | |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 18 | pager := subnets.List(Client, subnets.ListOpts{Limit: 2}) |
| 19 | err := pager.EachPage(func(page pagination.Page) (bool, error) { |
| 20 | t.Logf("--- Page ---") |
| 21 | |
| 22 | subnets, err := subnets.ExtractSubnets(page) |
| 23 | th.AssertNoErr(t, err) |
| 24 | |
| 25 | for _, s := range subnets { |
| 26 | t.Logf("Subnet: ID [%s] Name [%s] IP Version [%d] CIDR [%s] GatewayIP [%s]", |
| 27 | s.ID, s.Name, s.IPVersion, s.CIDR, s.GatewayIP) |
| 28 | } |
| 29 | |
| 30 | return true, nil |
| 31 | }) |
| 32 | th.CheckNoErr(t, err) |
Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | func TestCRUD(t *testing.T) { |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 36 | Setup(t) |
| 37 | defer Teardown() |
| 38 | |
| 39 | // Setup network |
| 40 | t.Log("Setting up network") |
| 41 | res, err := networks.Create(Client, networks.NetworkOpts{Name: "tmp_network", AdminStateUp: true}) |
| 42 | th.AssertNoErr(t, err) |
| 43 | networkID := res.ID |
| 44 | defer networks.Delete(Client, networkID) |
Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 45 | |
| 46 | // Create subnet |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 47 | t.Log("Create subnet") |
| 48 | enable := false |
| 49 | opts := subnets.SubnetOpts{ |
| 50 | NetworkID: networkID, |
| 51 | CIDR: "192.168.199.0/24", |
| 52 | IPVersion: subnets.IPv4, |
| 53 | Name: "my_subnet", |
| 54 | EnableDHCP: &enable, |
| 55 | } |
| 56 | s, err := subnets.Create(Client, opts) |
| 57 | th.AssertNoErr(t, err) |
| 58 | |
| 59 | th.AssertEquals(t, s.NetworkID, networkID) |
| 60 | th.AssertEquals(t, s.CIDR, "192.168.199.0/24") |
| 61 | th.AssertEquals(t, s.IPVersion, 4) |
| 62 | th.AssertEquals(t, s.Name, "my_subnet") |
| 63 | th.AssertEquals(t, s.EnableDHCP, false) |
| 64 | subnetID := s.ID |
Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 65 | |
| 66 | // Get subnet |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 67 | t.Log("Getting subnet") |
| 68 | s, err = subnets.Get(Client, subnetID) |
| 69 | th.AssertNoErr(t, err) |
| 70 | th.AssertEquals(t, s.ID, subnetID) |
Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 71 | |
| 72 | // Update subnet |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 73 | t.Log("Update subnet") |
| 74 | s, err = subnets.Update(Client, subnetID, subnets.SubnetOpts{Name: "new_subnet_name"}) |
| 75 | th.AssertNoErr(t, err) |
| 76 | th.AssertEquals(t, s.Name, "new_subnet_name") |
Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 77 | |
| 78 | // Delete subnet |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 79 | t.Log("Delete subnet") |
| 80 | err = subnets.Delete(Client, subnetID) |
| 81 | th.AssertNoErr(t, err) |
Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | func TestBatchCreate(t *testing.T) { |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 85 | // todo |
Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 86 | } |