blob: e8fc4d37dfc0cdfeb8babeb2b0abe1f1c8905fce [file] [log] [blame]
Jon Perritt26ff5b72014-10-23 11:54:39 -05001// +build acceptance rackspace
Jon Perritt857b3122014-10-22 18:17:44 -05002
3package v2
4
5import (
6 "testing"
7
8 "github.com/rackspace/gophercloud/pagination"
Jon Perritt26ff5b72014-10-23 11:54:39 -05009 "github.com/rackspace/gophercloud/rackspace/compute/v2/networks"
Jon Perritt857b3122014-10-22 18:17:44 -050010 th "github.com/rackspace/gophercloud/testhelper"
11)
12
13func TestNetworks(t *testing.T) {
Jon Perritt26ff5b72014-10-23 11:54:39 -050014 client, err := newClient()
15 th.AssertNoErr(t, err)
Jon Perritt857b3122014-10-22 18:17:44 -050016
17 // Create a network
Jon Perritt26ff5b72014-10-23 11:54:39 -050018 n, err := networks.Create(client, networks.CreateOpts{Label: "sample_network", CIDR: "172.20.0.0/24"}).Extract()
Jon Perritt857b3122014-10-22 18:17:44 -050019 th.AssertNoErr(t, err)
Jon Perritt26ff5b72014-10-23 11:54:39 -050020 t.Logf("Created network: %+v\n", n)
21 defer networks.Delete(client, n.ID)
Jon Perritt857b3122014-10-22 18:17:44 -050022 th.AssertEquals(t, n.Label, "sample_network")
23 th.AssertEquals(t, n.CIDR, "172.20.0.0/24")
24 networkID := n.ID
25
26 // List networks
Jon Perritt26ff5b72014-10-23 11:54:39 -050027 pager := networks.List(client)
Jon Perritt857b3122014-10-22 18:17:44 -050028 err = pager.EachPage(func(page pagination.Page) (bool, error) {
29 t.Logf("--- Page ---")
30
31 networkList, err := networks.ExtractNetworks(page)
32 th.AssertNoErr(t, err)
33
34 for _, n := range networkList {
35 t.Logf("Network: ID [%s] Label [%s] CIDR [%s]",
36 n.ID, n.Label, n.CIDR)
37 }
38
39 return true, nil
40 })
41 th.CheckNoErr(t, err)
42
43 // Get a network
44 if networkID == "" {
45 t.Fatalf("In order to retrieve a network, the NetworkID must be set")
46 }
Jon Perritt26ff5b72014-10-23 11:54:39 -050047 n, err = networks.Get(client, networkID).Extract()
48 t.Logf("Retrieved Network: %+v\n", n)
Jon Perritt857b3122014-10-22 18:17:44 -050049 th.AssertNoErr(t, err)
Jon Perritt26ff5b72014-10-23 11:54:39 -050050 th.AssertEquals(t, n.CIDR, "172.20.0.0/24")
Jon Perritt857b3122014-10-22 18:17:44 -050051 th.AssertEquals(t, n.Label, "sample_network")
52 th.AssertEquals(t, n.ID, networkID)
53}