blob: 49c970ae67988c5fc3e592b37c95e4869fe27c2b [file] [log] [blame]
Joe Topjian7c8dd022016-09-01 12:02:04 -06001// +build acceptance networking
2
3package v2
4
5import (
6 "testing"
7
8 "github.com/gophercloud/gophercloud/acceptance/clients"
9 "github.com/gophercloud/gophercloud/acceptance/tools"
10 "github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
11)
12
13func TestSubnetsList(t *testing.T) {
14 client, err := clients.NewNetworkV2Client()
15 if err != nil {
16 t.Fatalf("Unable to create a network client: %v", err)
17 }
18
19 allPages, err := subnets.List(client, nil).AllPages()
20 if err != nil {
21 t.Fatalf("Unable to list subnets: %v", err)
22 }
23
24 allSubnets, err := subnets.ExtractSubnets(allPages)
25 if err != nil {
26 t.Fatalf("Unable to extract subnets: %v", err)
27 }
28
29 for _, subnet := range allSubnets {
30 PrintSubnet(t, &subnet)
31 }
32}
33
34func TestSubnetCRUD(t *testing.T) {
35 client, err := clients.NewNetworkV2Client()
36 if err != nil {
37 t.Fatalf("Unable to create a network client: %v", err)
38 }
39
40 // Create Network
41 network, err := CreateNetwork(t, client)
42 if err != nil {
43 t.Fatalf("Unable to create network: %v", err)
44 }
45 defer DeleteNetwork(t, client, network.ID)
46
47 // Create Subnet
48 subnet, err := CreateSubnet(t, client, network.ID)
49 if err != nil {
50 t.Fatalf("Unable to create subnet: %v", err)
51 }
52 defer DeleteSubnet(t, client, subnet.ID)
53
54 PrintSubnet(t, subnet)
55
56 // Update Subnet
57 newSubnetName := tools.RandomString("TESTACC-", 8)
58 updateOpts := subnets.UpdateOpts{
59 Name: newSubnetName,
60 }
61 _, err = subnets.Update(client, subnet.ID, updateOpts).Extract()
62 if err != nil {
63 t.Fatalf("Unable to update subnet: %v", err)
64 }
65
66 // Get subnet
67 newSubnet, err := subnets.Get(client, subnet.ID).Extract()
68 if err != nil {
69 t.Fatalf("Unable to get subnet: %v", err)
70 }
71
72 PrintSubnet(t, newSubnet)
73}