1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
//go:build acceptance || networking
// +build acceptance networking
package v2
import (
"fmt"
"strings"
"testing"
"github.com/gophercloud/gophercloud/acceptance/clients"
subnetpools "github.com/gophercloud/gophercloud/acceptance/openstack/networking/v2/extensions/subnetpools"
"github.com/gophercloud/gophercloud/acceptance/tools"
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
th "github.com/gophercloud/gophercloud/testhelper"
)
func TestSubnetCRUD(t *testing.T) {
client, err := clients.NewNetworkV2Client()
th.AssertNoErr(t, err)
// Create Network
network, err := CreateNetwork(t, client)
th.AssertNoErr(t, err)
defer DeleteNetwork(t, client, network.ID)
// Create Subnet
subnet, err := CreateSubnet(t, client, network.ID)
th.AssertNoErr(t, err)
defer DeleteSubnet(t, client, subnet.ID)
tools.PrintResource(t, subnet)
// Update Subnet
newSubnetName := tools.RandomString("TESTACC-", 8)
newSubnetDescription := ""
updateOpts := subnets.UpdateOpts{
Name: &newSubnetName,
Description: &newSubnetDescription,
}
_, err = subnets.Update(client, subnet.ID, updateOpts).Extract()
th.AssertNoErr(t, err)
// Get subnet
newSubnet, err := subnets.Get(client, subnet.ID).Extract()
th.AssertNoErr(t, err)
tools.PrintResource(t, newSubnet)
th.AssertEquals(t, newSubnet.Name, newSubnetName)
th.AssertEquals(t, newSubnet.Description, newSubnetDescription)
allPages, err := subnets.List(client, nil).AllPages()
th.AssertNoErr(t, err)
allSubnets, err := subnets.ExtractSubnets(allPages)
th.AssertNoErr(t, err)
var found bool
for _, subnet := range allSubnets {
if subnet.ID == newSubnet.ID {
found = true
}
}
th.AssertEquals(t, found, true)
}
func TestSubnetsServiceType(t *testing.T) {
client, err := clients.NewNetworkV2Client()
th.AssertNoErr(t, err)
// Create Network
network, err := CreateNetwork(t, client)
th.AssertNoErr(t, err)
defer DeleteNetwork(t, client, network.ID)
// Create Subnet
subnet, err := CreateSubnetWithServiceTypes(t, client, network.ID)
th.AssertNoErr(t, err)
defer DeleteSubnet(t, client, subnet.ID)
tools.PrintResource(t, subnet)
serviceTypes := []string{"network:floatingip"}
updateOpts := subnets.UpdateOpts{
ServiceTypes: &serviceTypes,
}
newSubnet, err := subnets.Update(client, subnet.ID, updateOpts).Extract()
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, newSubnet.ServiceTypes, serviceTypes)
}
func TestSubnetsDefaultGateway(t *testing.T) {
client, err := clients.NewNetworkV2Client()
th.AssertNoErr(t, err)
// Create Network
network, err := CreateNetwork(t, client)
th.AssertNoErr(t, err)
defer DeleteNetwork(t, client, network.ID)
// Create Subnet
subnet, err := CreateSubnetWithDefaultGateway(t, client, network.ID)
th.AssertNoErr(t, err)
defer DeleteSubnet(t, client, subnet.ID)
tools.PrintResource(t, subnet)
if subnet.GatewayIP == "" {
t.Fatalf("A default gateway was not created.")
}
var noGateway = ""
updateOpts := subnets.UpdateOpts{
GatewayIP: &noGateway,
}
newSubnet, err := subnets.Update(client, subnet.ID, updateOpts).Extract()
th.AssertNoErr(t, err)
if newSubnet.GatewayIP != "" {
t.Fatalf("Gateway was not updated correctly")
}
}
func TestSubnetsNoGateway(t *testing.T) {
client, err := clients.NewNetworkV2Client()
th.AssertNoErr(t, err)
// Create Network
network, err := CreateNetwork(t, client)
th.AssertNoErr(t, err)
defer DeleteNetwork(t, client, network.ID)
// Create Subnet
subnet, err := CreateSubnetWithNoGateway(t, client, network.ID)
th.AssertNoErr(t, err)
defer DeleteSubnet(t, client, subnet.ID)
tools.PrintResource(t, subnet)
if subnet.GatewayIP != "" {
t.Fatalf("A gateway exists when it shouldn't.")
}
subnetParts := strings.Split(subnet.CIDR, ".")
newGateway := fmt.Sprintf("%s.%s.%s.1", subnetParts[0], subnetParts[1], subnetParts[2])
updateOpts := subnets.UpdateOpts{
GatewayIP: &newGateway,
}
newSubnet, err := subnets.Update(client, subnet.ID, updateOpts).Extract()
th.AssertNoErr(t, err)
if newSubnet.GatewayIP == "" {
t.Fatalf("Gateway was not updated correctly")
}
}
func TestSubnetsWithSubnetPool(t *testing.T) {
client, err := clients.NewNetworkV2Client()
th.AssertNoErr(t, err)
// Create Network
network, err := CreateNetwork(t, client)
th.AssertNoErr(t, err)
defer DeleteNetwork(t, client, network.ID)
// Create SubnetPool
subnetPool, err := subnetpools.CreateSubnetPool(t, client)
th.AssertNoErr(t, err)
defer subnetpools.DeleteSubnetPool(t, client, subnetPool.ID)
// Create Subnet
subnet, err := CreateSubnetWithSubnetPool(t, client, network.ID, subnetPool.ID)
th.AssertNoErr(t, err)
defer DeleteSubnet(t, client, subnet.ID)
tools.PrintResource(t, subnet)
if subnet.GatewayIP == "" {
t.Fatalf("A subnet pool was not associated.")
}
}
func TestSubnetsWithSubnetPoolNoCIDR(t *testing.T) {
client, err := clients.NewNetworkV2Client()
th.AssertNoErr(t, err)
// Create Network
network, err := CreateNetwork(t, client)
th.AssertNoErr(t, err)
defer DeleteNetwork(t, client, network.ID)
// Create SubnetPool
subnetPool, err := subnetpools.CreateSubnetPool(t, client)
th.AssertNoErr(t, err)
defer subnetpools.DeleteSubnetPool(t, client, subnetPool.ID)
// Create Subnet
subnet, err := CreateSubnetWithSubnetPoolNoCIDR(t, client, network.ID, subnetPool.ID)
th.AssertNoErr(t, err)
defer DeleteSubnet(t, client, subnet.ID)
tools.PrintResource(t, subnet)
if subnet.GatewayIP == "" {
t.Fatalf("A subnet pool was not associated.")
}
}
func TestSubnetsWithSubnetPoolPrefixlen(t *testing.T) {
client, err := clients.NewNetworkV2Client()
th.AssertNoErr(t, err)
// Create Network
network, err := CreateNetwork(t, client)
th.AssertNoErr(t, err)
defer DeleteNetwork(t, client, network.ID)
// Create SubnetPool
subnetPool, err := subnetpools.CreateSubnetPool(t, client)
th.AssertNoErr(t, err)
defer subnetpools.DeleteSubnetPool(t, client, subnetPool.ID)
// Create Subnet
subnet, err := CreateSubnetWithSubnetPoolPrefixlen(t, client, network.ID, subnetPool.ID)
th.AssertNoErr(t, err)
defer DeleteSubnet(t, client, subnet.ID)
tools.PrintResource(t, subnet)
if subnet.GatewayIP == "" {
t.Fatalf("A subnet pool was not associated.")
}
cidrParts := strings.Split(subnet.CIDR, "/")
if len(cidrParts) != 2 {
t.Fatalf("Got invalid CIDR for subnet '%s': %s", subnet.ID, subnet.CIDR)
}
if cidrParts[1] != "12" {
t.Fatalf("Got invalid prefix length for subnet '%s': wanted 12 but got '%s'", subnet.ID, cidrParts[1])
}
}
func TestSubnetDNSNameservers(t *testing.T) {
client, err := clients.NewNetworkV2Client()
th.AssertNoErr(t, err)
// Create Network
network, err := CreateNetwork(t, client)
th.AssertNoErr(t, err)
defer DeleteNetwork(t, client, network.ID)
// Create Subnet
subnet, err := CreateSubnet(t, client, network.ID)
th.AssertNoErr(t, err)
defer DeleteSubnet(t, client, subnet.ID)
tools.PrintResource(t, subnet)
// Update Subnet
dnsNameservers := []string{"1.1.1.1"}
updateOpts := subnets.UpdateOpts{
DNSNameservers: &dnsNameservers,
}
_, err = subnets.Update(client, subnet.ID, updateOpts).Extract()
th.AssertNoErr(t, err)
// Get subnet
newSubnet, err := subnets.Get(client, subnet.ID).Extract()
th.AssertNoErr(t, err)
tools.PrintResource(t, newSubnet)
th.AssertEquals(t, len(newSubnet.DNSNameservers), 1)
// Update Subnet again
dnsNameservers = []string{}
updateOpts = subnets.UpdateOpts{
DNSNameservers: &dnsNameservers,
}
_, err = subnets.Update(client, subnet.ID, updateOpts).Extract()
th.AssertNoErr(t, err)
// Get subnet
newSubnet, err = subnets.Get(client, subnet.ID).Extract()
th.AssertNoErr(t, err)
tools.PrintResource(t, newSubnet)
th.AssertEquals(t, len(newSubnet.DNSNameservers), 0)
}
func TestSubnetsRevision(t *testing.T) {
client, err := clients.NewNetworkV2Client()
th.AssertNoErr(t, err)
// Create Network
network, err := CreateNetwork(t, client)
th.AssertNoErr(t, err)
defer DeleteNetwork(t, client, network.ID)
// Create Subnet
subnet, err := CreateSubnet(t, client, network.ID)
th.AssertNoErr(t, err)
defer DeleteSubnet(t, client, subnet.ID)
tools.PrintResource(t, subnet)
// Store the current revision number.
oldRevisionNumber := subnet.RevisionNumber
// Update Subnet without revision number.
// This should work.
newSubnetName := tools.RandomString("TESTACC-", 8)
newSubnetDescription := ""
updateOpts := &subnets.UpdateOpts{
Name: &newSubnetName,
Description: &newSubnetDescription,
}
subnet, err = subnets.Update(client, subnet.ID, updateOpts).Extract()
th.AssertNoErr(t, err)
tools.PrintResource(t, subnet)
// This should fail due to an old revision number.
newSubnetDescription = "new description"
updateOpts = &subnets.UpdateOpts{
Name: &newSubnetName,
Description: &newSubnetDescription,
RevisionNumber: &oldRevisionNumber,
}
_, err = subnets.Update(client, subnet.ID, updateOpts).Extract()
th.AssertErr(t, err)
if !strings.Contains(err.Error(), "RevisionNumberConstraintFailed") {
t.Fatalf("expected to see an error of type RevisionNumberConstraintFailed, but got the following error instead: %v", err)
}
// Reread the subnet to show that it did not change.
subnet, err = subnets.Get(client, subnet.ID).Extract()
th.AssertNoErr(t, err)
tools.PrintResource(t, subnet)
// This should work because now we do provide a valid revision number.
newSubnetDescription = "new description"
updateOpts = &subnets.UpdateOpts{
Name: &newSubnetName,
Description: &newSubnetDescription,
RevisionNumber: &subnet.RevisionNumber,
}
subnet, err = subnets.Update(client, subnet.ID, updateOpts).Extract()
th.AssertNoErr(t, err)
tools.PrintResource(t, subnet)
th.AssertEquals(t, subnet.Name, newSubnetName)
th.AssertEquals(t, subnet.Description, newSubnetDescription)
}
|