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 | |
Joe Topjian | f92ae6c | 2016-04-06 21:24:43 -0600 | [diff] [blame] | 14 | func TestSubnetList(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 | |
Joe Topjian | f92ae6c | 2016-04-06 21:24:43 -0600 | [diff] [blame] | 35 | func TestSubnetCRUD(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) |
Joe Topjian | f92ae6c | 2016-04-06 21:24:43 -0600 | [diff] [blame] | 64 | th.AssertEquals(t, s.GatewayIP, "192.168.199.1") |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 65 | subnetID := s.ID |
Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 66 | |
| 67 | // Get subnet |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 68 | t.Log("Getting subnet") |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 69 | s, err = subnets.Get(Client, subnetID).Extract() |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 70 | th.AssertNoErr(t, err) |
| 71 | th.AssertEquals(t, s.ID, subnetID) |
Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 72 | |
| 73 | // Update subnet |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 74 | t.Log("Update subnet") |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 75 | 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] | 76 | th.AssertNoErr(t, err) |
| 77 | th.AssertEquals(t, s.Name, "new_subnet_name") |
Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 78 | |
| 79 | // Delete subnet |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 80 | t.Log("Delete subnet") |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 81 | res := subnets.Delete(Client, subnetID) |
| 82 | th.AssertNoErr(t, res.Err) |
Joe Topjian | f92ae6c | 2016-04-06 21:24:43 -0600 | [diff] [blame] | 83 | |
| 84 | // Create subnet with no gateway |
| 85 | t.Log("Create subnet with no gateway") |
| 86 | opts = subnets.CreateOpts{ |
| 87 | NetworkID: networkID, |
| 88 | CIDR: "192.168.199.0/24", |
| 89 | IPVersion: subnets.IPv4, |
| 90 | Name: "my_subnet", |
| 91 | EnableDHCP: &enable, |
| 92 | NoGateway: true, |
| 93 | } |
| 94 | s, err = subnets.Create(Client, opts).Extract() |
| 95 | th.AssertNoErr(t, err) |
| 96 | |
| 97 | th.AssertEquals(t, s.NetworkID, networkID) |
| 98 | th.AssertEquals(t, s.CIDR, "192.168.199.0/24") |
| 99 | th.AssertEquals(t, s.IPVersion, 4) |
| 100 | th.AssertEquals(t, s.Name, "my_subnet") |
| 101 | th.AssertEquals(t, s.EnableDHCP, false) |
| 102 | th.AssertEquals(t, s.GatewayIP, "") |
| 103 | subnetID = s.ID |
| 104 | |
| 105 | // Get subnet |
| 106 | t.Log("Getting subnet with no gateway") |
| 107 | s, err = subnets.Get(Client, subnetID).Extract() |
| 108 | th.AssertNoErr(t, err) |
| 109 | th.AssertEquals(t, s.ID, subnetID) |
| 110 | |
| 111 | // Update subnet |
| 112 | t.Log("Update subnet with no gateway") |
| 113 | s, err = subnets.Update(Client, subnetID, subnets.UpdateOpts{Name: "new_subnet_name"}).Extract() |
| 114 | th.AssertNoErr(t, err) |
| 115 | th.AssertEquals(t, s.Name, "new_subnet_name") |
| 116 | |
| 117 | // Delete subnet |
| 118 | t.Log("Delete subnet with no gateway") |
| 119 | res = subnets.Delete(Client, subnetID) |
| 120 | th.AssertNoErr(t, res.Err) |
Joe Topjian | 2524d11 | 2016-04-07 15:41:39 +0000 | [diff] [blame] | 121 | |
| 122 | // Create subnet with invalid gateway configuration |
| 123 | t.Log("Create subnet with invalid gateway configuration") |
| 124 | opts = subnets.CreateOpts{ |
| 125 | NetworkID: networkID, |
| 126 | CIDR: "192.168.199.0/24", |
| 127 | IPVersion: subnets.IPv4, |
| 128 | Name: "my_subnet", |
| 129 | EnableDHCP: &enable, |
| 130 | NoGateway: true, |
| 131 | GatewayIP: "192.168.199.1", |
| 132 | } |
| 133 | _, err = subnets.Create(Client, opts).Extract() |
| 134 | if err == nil { |
| 135 | t.Fatalf("Expected an error, got none") |
| 136 | } |
Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | func TestBatchCreate(t *testing.T) { |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 140 | // todo |
Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 141 | } |