Joe Topjian | 99a0613 | 2015-02-22 05:06:25 +0000 | [diff] [blame] | 1 | // +build acceptance compute servers |
| 2 | |
| 3 | package v2 |
| 4 | |
| 5 | import ( |
| 6 | "os" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/rackspace/gophercloud" |
| 10 | "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/networks" |
| 11 | "github.com/rackspace/gophercloud/openstack/compute/v2/servers" |
| 12 | th "github.com/rackspace/gophercloud/testhelper" |
| 13 | ) |
| 14 | |
| 15 | func getNetworkIDFromNetworkExtension(t *testing.T, client *gophercloud.ServiceClient, networkName string) (string, error) { |
| 16 | allPages, err := networks.List(client).AllPages() |
| 17 | if err != nil { |
| 18 | t.Fatalf("Unable to list networks: %v", err) |
| 19 | } |
| 20 | |
| 21 | networkList, err := networks.ExtractNetworks(allPages) |
| 22 | if err != nil { |
| 23 | t.Fatalf("Unable to list networks: %v", err) |
| 24 | } |
| 25 | |
| 26 | networkID := "" |
| 27 | for _, network := range networkList { |
| 28 | t.Logf("Network: %v", network) |
| 29 | if network.Label == networkName { |
| 30 | networkID = network.ID |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | t.Logf("Found network ID for %s: %s\n", networkName, networkID) |
| 35 | |
| 36 | return networkID, nil |
| 37 | } |
| 38 | |
| 39 | func TestNetworks(t *testing.T) { |
| 40 | networkName := os.Getenv("OS_NETWORK_NAME") |
| 41 | if networkName == "" { |
| 42 | t.Fatalf("OS_NETWORK_NAME must be set") |
| 43 | } |
| 44 | |
| 45 | choices, err := ComputeChoicesFromEnv() |
| 46 | if err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | |
| 50 | client, err := newClient() |
| 51 | if err != nil { |
| 52 | t.Fatalf("Unable to create a compute client: %v", err) |
| 53 | } |
| 54 | |
| 55 | networkID, err := getNetworkIDFromNetworkExtension(t, client, networkName) |
| 56 | if err != nil { |
| 57 | t.Fatalf("Unable to get network ID: %v", err) |
| 58 | } |
| 59 | |
| 60 | // createNetworkServer is defined in tenantnetworks_test.go |
| 61 | server, err := createNetworkServer(t, client, choices, networkID) |
| 62 | if err != nil { |
| 63 | t.Fatalf("Unable to create server: %v", err) |
| 64 | } |
| 65 | defer func() { |
| 66 | servers.Delete(client, server.ID) |
| 67 | t.Logf("Server deleted.") |
| 68 | }() |
| 69 | |
| 70 | if err = waitForStatus(client, server, "ACTIVE"); err != nil { |
| 71 | t.Fatalf("Unable to wait for server: %v", err) |
| 72 | } |
| 73 | |
| 74 | allPages, err := networks.List(client).AllPages() |
| 75 | allNetworks, err := networks.ExtractNetworks(allPages) |
| 76 | th.AssertNoErr(t, err) |
| 77 | t.Logf("Retrieved all %d networks: %+v", len(allNetworks), allNetworks) |
| 78 | } |