blob: 8231bd6a72a06f335bda560b21845914b83d7242 [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
Jon Perritt27249f42016-02-18 10:35:59 -06009 "github.com/gophercloud/gophercloud"
10 "github.com/gophercloud/gophercloud/acceptance/tools"
11 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingip"
12 "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
13 th "github.com/gophercloud/gophercloud/testhelper"
Joe Topjiandee32222015-02-09 23:56:26 +000014)
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
Joe Topjiand97fe9b2015-09-17 02:08:38 +000052func associateFloatingIPDeprecated(t *testing.T, client *gophercloud.ServiceClient, serverId string, fip *floatingip.FloatingIP) {
53 // This form works, but is considered deprecated.
54 // See associateFloatingIP or associateFloatingIPFixed
Joe Topjiandee32222015-02-09 23:56:26 +000055 err := floatingip.Associate(client, serverId, fip.IP).ExtractErr()
56 th.AssertNoErr(t, err)
57 t.Logf("Associated floating IP %v from instance %v", fip.IP, serverId)
58 defer func() {
59 err = floatingip.Disassociate(client, serverId, fip.IP).ExtractErr()
60 th.AssertNoErr(t, err)
61 t.Logf("Disassociated floating IP %v from instance %v", fip.IP, serverId)
62 }()
63 floatingIp, err := floatingip.Get(client, fip.ID).Extract()
64 th.AssertNoErr(t, err)
65 t.Logf("Floating IP %v is associated with Fixed IP %v", fip.IP, floatingIp.FixedIP)
66}
67
Joe Topjiand97fe9b2015-09-17 02:08:38 +000068func associateFloatingIP(t *testing.T, client *gophercloud.ServiceClient, serverId string, fip *floatingip.FloatingIP) {
69 associateOpts := floatingip.AssociateOpts{
70 ServerID: serverId,
71 FloatingIP: fip.IP,
72 }
73
Joe Topjian94e4cc52016-01-05 17:01:18 +000074 err := floatingip.AssociateInstance(client, associateOpts).ExtractErr()
Joe Topjiand97fe9b2015-09-17 02:08:38 +000075 th.AssertNoErr(t, err)
76 t.Logf("Associated floating IP %v from instance %v", fip.IP, serverId)
77 defer func() {
Joe Topjian94e4cc52016-01-05 17:01:18 +000078 err = floatingip.DisassociateInstance(client, associateOpts).ExtractErr()
Joe Topjiand97fe9b2015-09-17 02:08:38 +000079 th.AssertNoErr(t, err)
80 t.Logf("Disassociated floating IP %v from instance %v", fip.IP, serverId)
81 }()
82 floatingIp, err := floatingip.Get(client, fip.ID).Extract()
83 th.AssertNoErr(t, err)
84 t.Logf("Floating IP %v is associated with Fixed IP %v", fip.IP, floatingIp.FixedIP)
85}
86
87func associateFloatingIPFixed(t *testing.T, client *gophercloud.ServiceClient, serverId string, fip *floatingip.FloatingIP) {
88
89 network := os.Getenv("OS_NETWORK_NAME")
90 server, err := servers.Get(client, serverId).Extract()
91 if err != nil {
92 t.Fatalf("%s", err)
93 }
94
95 var fixedIP string
96 for _, networkAddresses := range server.Addresses[network].([]interface{}) {
97 address := networkAddresses.(map[string]interface{})
98 if address["OS-EXT-IPS:type"] == "fixed" {
99 if address["version"].(float64) == 4 {
100 fixedIP = address["addr"].(string)
101 }
102 }
103 }
104
105 associateOpts := floatingip.AssociateOpts{
106 ServerID: serverId,
107 FloatingIP: fip.IP,
108 FixedIP: fixedIP,
109 }
110
Joe Topjian94e4cc52016-01-05 17:01:18 +0000111 err = floatingip.AssociateInstance(client, associateOpts).ExtractErr()
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000112 th.AssertNoErr(t, err)
113 t.Logf("Associated floating IP %v from instance %v with Fixed IP %v", fip.IP, serverId, fixedIP)
114 defer func() {
Joe Topjian94e4cc52016-01-05 17:01:18 +0000115 err = floatingip.DisassociateInstance(client, associateOpts).ExtractErr()
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000116 th.AssertNoErr(t, err)
117 t.Logf("Disassociated floating IP %v from instance %v with Fixed IP %v", fip.IP, serverId, fixedIP)
118 }()
119 floatingIp, err := floatingip.Get(client, fip.ID).Extract()
120 th.AssertNoErr(t, err)
121 th.AssertEquals(t, floatingIp.FixedIP, fixedIP)
122 t.Logf("Floating IP %v is associated with Fixed IP %v", fip.IP, floatingIp.FixedIP)
123}
124
Joe Topjiandee32222015-02-09 23:56:26 +0000125func TestFloatingIP(t *testing.T) {
126 pool := os.Getenv("OS_POOL_NAME")
127 if pool == "" {
128 t.Fatalf("OS_POOL_NAME must be set")
129 }
130
131 choices, err := ComputeChoicesFromEnv()
132 if err != nil {
133 t.Fatal(err)
134 }
135
136 client, err := newClient()
137 if err != nil {
138 t.Fatalf("Unable to create a compute client: %v", err)
139 }
140
141 server, err := createFIPServer(t, client, choices)
142 if err != nil {
143 t.Fatalf("Unable to create server: %v", err)
144 }
145 defer func() {
146 servers.Delete(client, server.ID)
147 t.Logf("Server deleted.")
148 }()
149
150 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
151 t.Fatalf("Unable to wait for server: %v", err)
152 }
153
154 fip, err := createFloatingIP(t, client)
155 if err != nil {
156 t.Fatalf("Unable to create floating IP: %v", err)
157 }
158 defer func() {
159 err = floatingip.Delete(client, fip.ID).ExtractErr()
160 th.AssertNoErr(t, err)
161 t.Logf("Floating IP deleted.")
162 }()
163
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000164 associateFloatingIPDeprecated(t, client, server.ID, fip)
Joe Topjiandee32222015-02-09 23:56:26 +0000165 associateFloatingIP(t, client, server.ID, fip)
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000166 associateFloatingIPFixed(t, client, server.ID, fip)
Joe Topjiandee32222015-02-09 23:56:26 +0000167
168}