blob: fd50a1f84bd1323e9a5d74d41c4a0ed32168b408 [file] [log] [blame]
Joe Topjian7c8dd022016-09-01 12:02:04 -06001// +build acceptance networking
2
3package v2
4
5import (
Joe Topjian19e713b2016-10-06 10:10:24 -06006 "fmt"
7 "strings"
Joe Topjian7c8dd022016-09-01 12:02:04 -06008 "testing"
9
10 "github.com/gophercloud/gophercloud/acceptance/clients"
11 "github.com/gophercloud/gophercloud/acceptance/tools"
12 "github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
13)
14
15func TestSubnetsList(t *testing.T) {
16 client, err := clients.NewNetworkV2Client()
17 if err != nil {
18 t.Fatalf("Unable to create a network client: %v", err)
19 }
20
21 allPages, err := subnets.List(client, nil).AllPages()
22 if err != nil {
23 t.Fatalf("Unable to list subnets: %v", err)
24 }
25
26 allSubnets, err := subnets.ExtractSubnets(allPages)
27 if err != nil {
28 t.Fatalf("Unable to extract subnets: %v", err)
29 }
30
31 for _, subnet := range allSubnets {
Joe Topjian66a046c2017-01-19 22:07:26 -070032 tools.PrintResource(t, subnet)
Joe Topjian7c8dd022016-09-01 12:02:04 -060033 }
34}
35
36func TestSubnetCRUD(t *testing.T) {
37 client, err := clients.NewNetworkV2Client()
38 if err != nil {
39 t.Fatalf("Unable to create a network client: %v", err)
40 }
41
42 // Create Network
43 network, err := CreateNetwork(t, client)
44 if err != nil {
45 t.Fatalf("Unable to create network: %v", err)
46 }
47 defer DeleteNetwork(t, client, network.ID)
48
49 // Create Subnet
50 subnet, err := CreateSubnet(t, client, network.ID)
51 if err != nil {
52 t.Fatalf("Unable to create subnet: %v", err)
53 }
54 defer DeleteSubnet(t, client, subnet.ID)
55
Joe Topjian66a046c2017-01-19 22:07:26 -070056 tools.PrintResource(t, subnet)
Joe Topjian7c8dd022016-09-01 12:02:04 -060057
58 // Update Subnet
59 newSubnetName := tools.RandomString("TESTACC-", 8)
60 updateOpts := subnets.UpdateOpts{
61 Name: newSubnetName,
62 }
63 _, err = subnets.Update(client, subnet.ID, updateOpts).Extract()
64 if err != nil {
65 t.Fatalf("Unable to update subnet: %v", err)
66 }
67
68 // Get subnet
69 newSubnet, err := subnets.Get(client, subnet.ID).Extract()
70 if err != nil {
71 t.Fatalf("Unable to get subnet: %v", err)
72 }
73
Joe Topjian66a046c2017-01-19 22:07:26 -070074 tools.PrintResource(t, newSubnet)
Joe Topjian7c8dd022016-09-01 12:02:04 -060075}
Joe Topjian19e713b2016-10-06 10:10:24 -060076
77func TestSubnetsDefaultGateway(t *testing.T) {
78 client, err := clients.NewNetworkV2Client()
79 if err != nil {
80 t.Fatalf("Unable to create a network client: %v", err)
81 }
82
83 // Create Network
84 network, err := CreateNetwork(t, client)
85 if err != nil {
86 t.Fatalf("Unable to create network: %v", err)
87 }
88 defer DeleteNetwork(t, client, network.ID)
89
90 // Create Subnet
91 subnet, err := CreateSubnetWithDefaultGateway(t, client, network.ID)
92 if err != nil {
93 t.Fatalf("Unable to create subnet: %v", err)
94 }
95 defer DeleteSubnet(t, client, subnet.ID)
96
Joe Topjian66a046c2017-01-19 22:07:26 -070097 tools.PrintResource(t, subnet)
Joe Topjian19e713b2016-10-06 10:10:24 -060098
99 if subnet.GatewayIP == "" {
100 t.Fatalf("A default gateway was not created.")
101 }
102
103 var noGateway = ""
104 updateOpts := subnets.UpdateOpts{
105 GatewayIP: &noGateway,
106 }
107
108 newSubnet, err := subnets.Update(client, subnet.ID, updateOpts).Extract()
109 if err != nil {
110 t.Fatalf("Unable to update subnet")
111 }
112
113 if newSubnet.GatewayIP != "" {
114 t.Fatalf("Gateway was not updated correctly")
115 }
116}
117
118func TestSubnetsNoGateway(t *testing.T) {
119 client, err := clients.NewNetworkV2Client()
120 if err != nil {
121 t.Fatalf("Unable to create a network client: %v", err)
122 }
123
124 // Create Network
125 network, err := CreateNetwork(t, client)
126 if err != nil {
127 t.Fatalf("Unable to create network: %v", err)
128 }
129 defer DeleteNetwork(t, client, network.ID)
130
131 // Create Subnet
132 subnet, err := CreateSubnetWithNoGateway(t, client, network.ID)
133 if err != nil {
134 t.Fatalf("Unable to create subnet: %v", err)
135 }
136 defer DeleteSubnet(t, client, subnet.ID)
137
Joe Topjian66a046c2017-01-19 22:07:26 -0700138 tools.PrintResource(t, subnet)
Joe Topjian19e713b2016-10-06 10:10:24 -0600139
140 if subnet.GatewayIP != "" {
141 t.Fatalf("A gateway exists when it shouldn't.")
142 }
143
144 subnetParts := strings.Split(subnet.CIDR, ".")
145 newGateway := fmt.Sprintf("%s.%s.%s.1", subnetParts[0], subnetParts[1], subnetParts[2])
146 updateOpts := subnets.UpdateOpts{
147 GatewayIP: &newGateway,
148 }
149
150 newSubnet, err := subnets.Update(client, subnet.ID, updateOpts).Extract()
151 if err != nil {
152 t.Fatalf("Unable to update subnet")
153 }
154
155 if newSubnet.GatewayIP == "" {
156 t.Fatalf("Gateway was not updated correctly")
157 }
158}