Joe Topjian | c21202d | 2015-02-27 21:32:58 +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/acceptance/tools" |
| 11 | "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/tenantnetworks" |
| 12 | "github.com/rackspace/gophercloud/openstack/compute/v2/servers" |
| 13 | th "github.com/rackspace/gophercloud/testhelper" |
| 14 | ) |
| 15 | |
| 16 | func getNetworkID(t *testing.T, client *gophercloud.ServiceClient, networkName string) (string, error) { |
| 17 | allPages, err := tenantnetworks.List(client).AllPages() |
| 18 | if err != nil { |
| 19 | t.Fatalf("Unable to list networks: %v", err) |
| 20 | } |
| 21 | |
| 22 | networkList, err := tenantnetworks.ExtractNetworks(allPages) |
| 23 | if err != nil { |
| 24 | t.Fatalf("Unable to list networks: %v", err) |
| 25 | } |
| 26 | |
| 27 | networkID := "" |
| 28 | for _, network := range networkList { |
| 29 | t.Logf("Network: %v", network) |
| 30 | if network.Name == networkName { |
| 31 | networkID = network.ID |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | t.Logf("Found network ID for %s: %s\n", networkName, networkID) |
| 36 | |
| 37 | return networkID, nil |
| 38 | } |
| 39 | |
| 40 | func createNetworkServer(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices, networkID string) (*servers.Server, error) { |
| 41 | if testing.Short() { |
| 42 | t.Skip("Skipping test that requires server creation in short mode.") |
| 43 | } |
| 44 | |
| 45 | name := tools.RandomString("ACPTTEST", 16) |
| 46 | t.Logf("Attempting to create server: %s\n", name) |
| 47 | |
| 48 | pwd := tools.MakeNewPassword("") |
| 49 | |
| 50 | networks := make([]servers.Network, 1) |
| 51 | networks[0] = servers.Network{ |
| 52 | UUID: networkID, |
| 53 | } |
| 54 | |
| 55 | server, err := servers.Create(client, servers.CreateOpts{ |
| 56 | Name: name, |
| 57 | FlavorRef: choices.FlavorID, |
| 58 | ImageRef: choices.ImageID, |
| 59 | AdminPass: pwd, |
| 60 | Networks: networks, |
| 61 | }).Extract() |
| 62 | if err != nil { |
| 63 | t.Fatalf("Unable to create server: %v", err) |
| 64 | } |
| 65 | |
| 66 | th.AssertEquals(t, pwd, server.AdminPass) |
| 67 | |
| 68 | return server, err |
| 69 | } |
| 70 | |
| 71 | func TestTenantNetworks(t *testing.T) { |
| 72 | networkName := os.Getenv("OS_NETWORK_NAME") |
| 73 | if networkName == "" { |
| 74 | t.Fatalf("OS_NETWORK_NAME must be set") |
| 75 | } |
| 76 | |
| 77 | choices, err := ComputeChoicesFromEnv() |
| 78 | if err != nil { |
| 79 | t.Fatal(err) |
| 80 | } |
| 81 | |
| 82 | client, err := newClient() |
| 83 | if err != nil { |
| 84 | t.Fatalf("Unable to create a compute client: %v", err) |
| 85 | } |
| 86 | |
| 87 | networkID, err := getNetworkID(t, client, networkName) |
| 88 | if err != nil { |
| 89 | t.Fatalf("Unable to get network ID: %v", err) |
| 90 | } |
| 91 | |
| 92 | server, err := createNetworkServer(t, client, choices, networkID) |
| 93 | if err != nil { |
| 94 | t.Fatalf("Unable to create server: %v", err) |
| 95 | } |
| 96 | defer func() { |
| 97 | servers.Delete(client, server.ID) |
| 98 | t.Logf("Server deleted.") |
| 99 | }() |
| 100 | |
| 101 | if err = waitForStatus(client, server, "ACTIVE"); err != nil { |
| 102 | t.Fatalf("Unable to wait for server: %v", err) |
| 103 | } |
| 104 | |
| 105 | allPages, err := tenantnetworks.List(client).AllPages() |
| 106 | allNetworks, err := tenantnetworks.ExtractNetworks(allPages) |
| 107 | th.AssertNoErr(t, err) |
| 108 | t.Logf("Retrieved all %d networks: %+v", len(allNetworks), allNetworks) |
| 109 | } |