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 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 8 | "github.com/gophercloud/gophercloud/openstack/networking/v2/networks" |
| 9 | "github.com/gophercloud/gophercloud/openstack/networking/v2/subnets" |
| 10 | "github.com/gophercloud/gophercloud/pagination" |
| 11 | th "github.com/gophercloud/gophercloud/testhelper" |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 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 | |
Jamie Hannaford | 2a0492a | 2014-09-22 12:02:11 +0200 | [diff] [blame] | 22 | subnetList, err := subnets.ExtractSubnets(page) |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 23 | th.AssertNoErr(t, err) |
| 24 | |
Jamie Hannaford | 2a0492a | 2014-09-22 12:02:11 +0200 | [diff] [blame] | 25 | for _, s := range subnetList { |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 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") |
Jamie Hannaford | 9b18ae2 | 2014-10-09 10:42:21 +0200 | [diff] [blame] | 41 | n, err := networks.Create(Client, networks.CreateOpts{Name: "tmp_network", AdminStateUp: networks.Up}).Extract() |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 42 | th.AssertNoErr(t, err) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 43 | networkID := n.ID |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 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 |
Jamie Hannaford | 5e45ccb | 2014-09-22 15:41:10 +0200 | [diff] [blame] | 49 | opts := subnets.CreateOpts{ |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 50 | NetworkID: networkID, |
| 51 | CIDR: "192.168.199.0/24", |
| 52 | IPVersion: subnets.IPv4, |
| 53 | Name: "my_subnet", |
| 54 | EnableDHCP: &enable, |
| 55 | } |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 56 | s, err := subnets.Create(Client, opts).Extract() |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 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") |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 68 | s, err = subnets.Get(Client, subnetID).Extract() |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 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") |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 74 | s, err = subnets.Update(Client, subnetID, subnets.UpdateOpts{Name: "new_subnet_name"}).Extract() |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 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") |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 80 | res := subnets.Delete(Client, subnetID) |
| 81 | th.AssertNoErr(t, res.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 | } |