PageRenderTime 1006ms CodeModel.GetById 31ms RepoModel.GetById 8ms app.codeStats 0ms

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

https://gitlab.com/Red54/machine
Go | 107 lines | 86 code | 20 blank | 1 comment | 18 complexity | af180a1e7466600a6f4ff91260ed532c 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/floatingip"
  9. "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
  10. th "github.com/rackspace/gophercloud/testhelper"
  11. )
  12. func createFIPServer(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices) (*servers.Server, error) {
  13. if testing.Short() {
  14. t.Skip("Skipping test that requires server creation in short mode.")
  15. }
  16. name := tools.RandomString("ACPTTEST", 16)
  17. t.Logf("Attempting to create server: %s\n", name)
  18. pwd := tools.MakeNewPassword("")
  19. server, err := servers.Create(client, servers.CreateOpts{
  20. Name: name,
  21. FlavorRef: choices.FlavorID,
  22. ImageRef: choices.ImageID,
  23. AdminPass: pwd,
  24. }).Extract()
  25. if err != nil {
  26. t.Fatalf("Unable to create server: %v", err)
  27. }
  28. th.AssertEquals(t, pwd, server.AdminPass)
  29. return server, err
  30. }
  31. func createFloatingIP(t *testing.T, client *gophercloud.ServiceClient) (*floatingip.FloatingIP, error) {
  32. pool := os.Getenv("OS_POOL_NAME")
  33. fip, err := floatingip.Create(client, &floatingip.CreateOpts{
  34. Pool: pool,
  35. }).Extract()
  36. th.AssertNoErr(t, err)
  37. t.Logf("Obtained Floating IP: %v", fip.IP)
  38. return fip, err
  39. }
  40. func associateFloatingIP(t *testing.T, client *gophercloud.ServiceClient, serverId string, fip *floatingip.FloatingIP) {
  41. err := floatingip.Associate(client, serverId, fip.IP).ExtractErr()
  42. th.AssertNoErr(t, err)
  43. t.Logf("Associated floating IP %v from instance %v", fip.IP, serverId)
  44. defer func() {
  45. err = floatingip.Disassociate(client, serverId, fip.IP).ExtractErr()
  46. th.AssertNoErr(t, err)
  47. t.Logf("Disassociated floating IP %v from instance %v", fip.IP, serverId)
  48. }()
  49. floatingIp, err := floatingip.Get(client, fip.ID).Extract()
  50. th.AssertNoErr(t, err)
  51. t.Logf("Floating IP %v is associated with Fixed IP %v", fip.IP, floatingIp.FixedIP)
  52. }
  53. func TestFloatingIP(t *testing.T) {
  54. pool := os.Getenv("OS_POOL_NAME")
  55. if pool == "" {
  56. t.Fatalf("OS_POOL_NAME must be set")
  57. }
  58. choices, err := ComputeChoicesFromEnv()
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. client, err := newClient()
  63. if err != nil {
  64. t.Fatalf("Unable to create a compute client: %v", err)
  65. }
  66. server, err := createFIPServer(t, client, choices)
  67. if err != nil {
  68. t.Fatalf("Unable to create server: %v", err)
  69. }
  70. defer func() {
  71. servers.Delete(client, server.ID)
  72. t.Logf("Server deleted.")
  73. }()
  74. if err = waitForStatus(client, server, "ACTIVE"); err != nil {
  75. t.Fatalf("Unable to wait for server: %v", err)
  76. }
  77. fip, err := createFloatingIP(t, client)
  78. if err != nil {
  79. t.Fatalf("Unable to create floating IP: %v", err)
  80. }
  81. defer func() {
  82. err = floatingip.Delete(client, fip.ID).ExtractErr()
  83. th.AssertNoErr(t, err)
  84. t.Logf("Floating IP deleted.")
  85. }()
  86. associateFloatingIP(t, client, server.ID, fip)
  87. }