PageRenderTime 294ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/Godeps/_workspace/src/github.com/rackspace/gophercloud/acceptance/openstack/compute/v2/tenantnetworks_test.go

https://gitlab.com/Red54/machine
Go | 109 lines | 86 code | 22 blank | 1 comment | 23 complexity | b884f23fa9fcada1a5cce2a62bfa7fb7 MD5 | raw file
  1. // +build acceptance compute servers
  2. package v2
  3. import (
  4. "os"
  5. "testing"
  6. "github.com/rackspace/gophercloud"
  7. "github.com/rackspace/gophercloud/acceptance/tools"
  8. "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/tenantnetworks"
  9. "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
  10. th "github.com/rackspace/gophercloud/testhelper"
  11. )
  12. func getNetworkID(t *testing.T, client *gophercloud.ServiceClient, networkName string) (string, error) {
  13. allPages, err := tenantnetworks.List(client).AllPages()
  14. if err != nil {
  15. t.Fatalf("Unable to list networks: %v", err)
  16. }
  17. networkList, err := tenantnetworks.ExtractNetworks(allPages)
  18. if err != nil {
  19. t.Fatalf("Unable to list networks: %v", err)
  20. }
  21. networkID := ""
  22. for _, network := range networkList {
  23. t.Logf("Network: %v", network)
  24. if network.Name == networkName {
  25. networkID = network.ID
  26. }
  27. }
  28. t.Logf("Found network ID for %s: %s\n", networkName, networkID)
  29. return networkID, nil
  30. }
  31. func createNetworkServer(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices, networkID string) (*servers.Server, error) {
  32. if testing.Short() {
  33. t.Skip("Skipping test that requires server creation in short mode.")
  34. }
  35. name := tools.RandomString("ACPTTEST", 16)
  36. t.Logf("Attempting to create server: %s\n", name)
  37. pwd := tools.MakeNewPassword("")
  38. networks := make([]servers.Network, 1)
  39. networks[0] = servers.Network{
  40. UUID: networkID,
  41. }
  42. server, err := servers.Create(client, servers.CreateOpts{
  43. Name: name,
  44. FlavorRef: choices.FlavorID,
  45. ImageRef: choices.ImageID,
  46. AdminPass: pwd,
  47. Networks: networks,
  48. }).Extract()
  49. if err != nil {
  50. t.Fatalf("Unable to create server: %v", err)
  51. }
  52. th.AssertEquals(t, pwd, server.AdminPass)
  53. return server, err
  54. }
  55. func TestTenantNetworks(t *testing.T) {
  56. networkName := os.Getenv("OS_NETWORK_NAME")
  57. if networkName == "" {
  58. t.Fatalf("OS_NETWORK_NAME must be set")
  59. }
  60. choices, err := ComputeChoicesFromEnv()
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. client, err := newClient()
  65. if err != nil {
  66. t.Fatalf("Unable to create a compute client: %v", err)
  67. }
  68. networkID, err := getNetworkID(t, client, networkName)
  69. if err != nil {
  70. t.Fatalf("Unable to get network ID: %v", err)
  71. }
  72. server, err := createNetworkServer(t, client, choices, networkID)
  73. if err != nil {
  74. t.Fatalf("Unable to create server: %v", err)
  75. }
  76. defer func() {
  77. servers.Delete(client, server.ID)
  78. t.Logf("Server deleted.")
  79. }()
  80. if err = waitForStatus(client, server, "ACTIVE"); err != nil {
  81. t.Fatalf("Unable to wait for server: %v", err)
  82. }
  83. allPages, err := tenantnetworks.List(client).AllPages()
  84. allNetworks, err := tenantnetworks.ExtractNetworks(allPages)
  85. th.AssertNoErr(t, err)
  86. t.Logf("Retrieved all %d networks: %+v", len(allNetworks), allNetworks)
  87. }