blob: ab7554b5a880a3c38fec3f90b5d688080c77523e [file] [log] [blame]
Joe Topjiandee32222015-02-09 23:56:26 +00001// +build acceptance compute servers
2
3package v2
4
5import (
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/floatingip"
12 "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
13 th "github.com/rackspace/gophercloud/testhelper"
14)
15
16func createFIPServer(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices) (*servers.Server, error) {
17 if testing.Short() {
18 t.Skip("Skipping test that requires server creation in short mode.")
19 }
20
21 name := tools.RandomString("ACPTTEST", 16)
22 t.Logf("Attempting to create server: %s\n", name)
23
24 pwd := tools.MakeNewPassword("")
25
26 server, err := servers.Create(client, servers.CreateOpts{
27 Name: name,
28 FlavorRef: choices.FlavorID,
29 ImageRef: choices.ImageID,
30 AdminPass: pwd,
31 }).Extract()
32 if err != nil {
33 t.Fatalf("Unable to create server: %v", err)
34 }
35
36 th.AssertEquals(t, pwd, server.AdminPass)
37
38 return server, err
39}
40
41func createFloatingIP(t *testing.T, client *gophercloud.ServiceClient) (*floatingip.FloatingIP, error) {
42 pool := os.Getenv("OS_POOL_NAME")
43 fip, err := floatingip.Create(client, &floatingip.CreateOpts{
44 Pool: pool,
45 }).Extract()
46 th.AssertNoErr(t, err)
47 t.Logf("Obtained Floating IP: %v", fip.IP)
48
49 return fip, err
50}
51
52func associateFloatingIP(t *testing.T, client *gophercloud.ServiceClient, serverId string, fip *floatingip.FloatingIP) {
53 err := floatingip.Associate(client, serverId, fip.IP).ExtractErr()
54 th.AssertNoErr(t, err)
55 t.Logf("Associated floating IP %v from instance %v", fip.IP, serverId)
56 defer func() {
57 err = floatingip.Disassociate(client, serverId, fip.IP).ExtractErr()
58 th.AssertNoErr(t, err)
59 t.Logf("Disassociated floating IP %v from instance %v", fip.IP, serverId)
60 }()
61 floatingIp, err := floatingip.Get(client, fip.ID).Extract()
62 th.AssertNoErr(t, err)
63 t.Logf("Floating IP %v is associated with Fixed IP %v", fip.IP, floatingIp.FixedIP)
64}
65
66func TestFloatingIP(t *testing.T) {
67 pool := os.Getenv("OS_POOL_NAME")
68 if pool == "" {
69 t.Fatalf("OS_POOL_NAME must be set")
70 }
71
72 choices, err := ComputeChoicesFromEnv()
73 if err != nil {
74 t.Fatal(err)
75 }
76
77 client, err := newClient()
78 if err != nil {
79 t.Fatalf("Unable to create a compute client: %v", err)
80 }
81
82 server, err := createFIPServer(t, client, choices)
83 if err != nil {
84 t.Fatalf("Unable to create server: %v", err)
85 }
86 defer func() {
87 servers.Delete(client, server.ID)
88 t.Logf("Server deleted.")
89 }()
90
91 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
92 t.Fatalf("Unable to wait for server: %v", err)
93 }
94
95 fip, err := createFloatingIP(t, client)
96 if err != nil {
97 t.Fatalf("Unable to create floating IP: %v", err)
98 }
99 defer func() {
100 err = floatingip.Delete(client, fip.ID).ExtractErr()
101 th.AssertNoErr(t, err)
102 t.Logf("Floating IP deleted.")
103 }()
104
105 associateFloatingIP(t, client, server.ID, fip)
106
107}