blob: 92249c70edf2b1ecf5c77996965c88a5354364b0 [file] [log] [blame]
Joe Topjiandee32222015-02-09 23:56:26 +00001// +build acceptance compute servers
2
3package v2
4
5import (
Joe Topjiandee32222015-02-09 23:56:26 +00006 "testing"
7
Jon Perritt27249f42016-02-18 10:35:59 -06008 "github.com/gophercloud/gophercloud"
Joe Topjian48209e32016-07-25 16:31:06 +00009 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingips"
Jon Perritt27249f42016-02-18 10:35:59 -060010 "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
Joe Topjiandee32222015-02-09 23:56:26 +000011)
12
Joe Topjian48209e32016-07-25 16:31:06 +000013func TestFloatingIPsList(t *testing.T) {
14 client, err := newClient()
15 if err != nil {
16 t.Fatalf("Unable to create a compute client: %v", err)
17 }
18
19 allPages, err := floatingips.List(client).AllPages()
20 if err != nil {
21 t.Fatalf("Unable to retrieve floating IPs: %v", err)
22 }
23
24 allFloatingIPs, err := floatingips.ExtractFloatingIPs(allPages)
25 if err != nil {
26 t.Fatalf("Unable to extract floating IPs: %v", err)
27 }
28
29 for _, floatingIP := range allFloatingIPs {
30 printFloatingIP(t, &floatingIP)
31 }
32}
33
34func TestFloatingIPsCreate(t *testing.T) {
35 client, err := newClient()
36 if err != nil {
37 t.Fatalf("Unable to create a compute client: %v", err)
38 }
39
40 choices, err := ComputeChoicesFromEnv()
41 if err != nil {
42 t.Fatal(err)
43 }
44
45 floatingIP, err := createFloatingIP(t, client, choices)
46 if err != nil {
47 t.Fatalf("Unable to create floating IP: %v", err)
48 }
49 defer deleteFloatingIP(t, client, floatingIP)
50
51 printFloatingIP(t, floatingIP)
52}
53
54func TestFloatingIPsAssociate(t *testing.T) {
Joe Topjiandee32222015-02-09 23:56:26 +000055 if testing.Short() {
56 t.Skip("Skipping test that requires server creation in short mode.")
57 }
58
Joe Topjian48209e32016-07-25 16:31:06 +000059 client, err := newClient()
60 if err != nil {
61 t.Fatalf("Unable to create a compute client: %v", err)
62 }
Joe Topjiandee32222015-02-09 23:56:26 +000063
Joe Topjian48209e32016-07-25 16:31:06 +000064 choices, err := ComputeChoicesFromEnv()
65 if err != nil {
66 t.Fatal(err)
67 }
Joe Topjiandee32222015-02-09 23:56:26 +000068
Joe Topjian48209e32016-07-25 16:31:06 +000069 server, err := createServer(t, client, choices)
Joe Topjiandee32222015-02-09 23:56:26 +000070 if err != nil {
71 t.Fatalf("Unable to create server: %v", err)
72 }
73
Joe Topjian48209e32016-07-25 16:31:06 +000074 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
75 t.Fatalf("Unable to wait for server: %v", err)
Joe Topjiand97fe9b2015-09-17 02:08:38 +000076 }
Joe Topjian48209e32016-07-25 16:31:06 +000077 defer deleteServer(t, client, server)
Joe Topjiand97fe9b2015-09-17 02:08:38 +000078
Joe Topjian48209e32016-07-25 16:31:06 +000079 floatingIP, err := createFloatingIP(t, client, choices)
Joe Topjiand97fe9b2015-09-17 02:08:38 +000080 if err != nil {
Joe Topjian48209e32016-07-25 16:31:06 +000081 t.Fatalf("Unable to create floating IP: %v", err)
Joe Topjiand97fe9b2015-09-17 02:08:38 +000082 }
Joe Topjian48209e32016-07-25 16:31:06 +000083 defer deleteFloatingIP(t, client, floatingIP)
84
85 printFloatingIP(t, floatingIP)
86
87 associateOpts := floatingips.AssociateOpts{
88 FloatingIP: floatingIP.IP,
89 }
90
91 t.Logf("Attempting to associate floating IP %s to instance %s", floatingIP.IP, server.ID)
92 err = floatingips.AssociateInstance(client, server.ID, associateOpts).ExtractErr()
93 if err != nil {
94 t.Fatalf("Unable to associate floating IP %s with server %s: %v", floatingIP.IP, server.ID, err)
95 }
96 defer disassociateFloatingIP(t, client, floatingIP, server)
97 t.Logf("Floating IP %s is associated with Fixed IP %s", floatingIP.IP, floatingIP.FixedIP)
98
99 newFloatingIP, err := floatingips.Get(client, floatingIP.ID).Extract()
100 if err != nil {
101 t.Fatalf("Unable to get floating IP %s: %v", floatingIP.ID, err)
102 }
103
104 printFloatingIP(t, newFloatingIP)
105}
106
107func TestFloatingIPsFixedIPAssociate(t *testing.T) {
108 if testing.Short() {
109 t.Skip("Skipping test that requires server creation in short mode.")
110 }
111
112 client, err := newClient()
113 if err != nil {
114 t.Fatalf("Unable to create a compute client: %v", err)
115 }
116
117 choices, err := ComputeChoicesFromEnv()
118 if err != nil {
119 t.Fatal(err)
120 }
121
122 server, err := createServer(t, client, choices)
123 if err != nil {
124 t.Fatalf("Unable to create server: %v", err)
125 }
126
127 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
128 t.Fatalf("Unable to wait for server: %v", err)
129 }
130 defer deleteServer(t, client, server)
131
132 newServer, err := servers.Get(client, server.ID).Extract()
133 if err != nil {
134 t.Fatalf("Unable to get server %s: %v", server.ID, err)
135 }
136
137 floatingIP, err := createFloatingIP(t, client, choices)
138 if err != nil {
139 t.Fatalf("Unable to create floating IP: %v", err)
140 }
141 defer deleteFloatingIP(t, client, floatingIP)
142
143 printFloatingIP(t, floatingIP)
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000144
145 var fixedIP string
Joe Topjian48209e32016-07-25 16:31:06 +0000146 for _, networkAddresses := range newServer.Addresses[choices.NetworkName].([]interface{}) {
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000147 address := networkAddresses.(map[string]interface{})
148 if address["OS-EXT-IPS:type"] == "fixed" {
149 if address["version"].(float64) == 4 {
150 fixedIP = address["addr"].(string)
151 }
152 }
153 }
154
Joe Topjian48209e32016-07-25 16:31:06 +0000155 associateOpts := floatingips.AssociateOpts{
156 FloatingIP: floatingIP.IP,
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000157 FixedIP: fixedIP,
158 }
159
Joe Topjian48209e32016-07-25 16:31:06 +0000160 t.Logf("Attempting to associate floating IP %s to instance %s", floatingIP.IP, newServer.ID)
161 err = floatingips.AssociateInstance(client, newServer.ID, associateOpts).ExtractErr()
162 if err != nil {
163 t.Fatalf("Unable to associate floating IP %s with server %s: %v", floatingIP.IP, newServer.ID, err)
164 }
165 defer disassociateFloatingIP(t, client, floatingIP, newServer)
166 t.Logf("Floating IP %s is associated with Fixed IP %s", floatingIP.IP, floatingIP.FixedIP)
167
168 newFloatingIP, err := floatingips.Get(client, floatingIP.ID).Extract()
169 if err != nil {
170 t.Fatalf("Unable to get floating IP %s: %v", floatingIP.ID, err)
171 }
172
173 printFloatingIP(t, newFloatingIP)
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000174}
175
Joe Topjian48209e32016-07-25 16:31:06 +0000176func createFloatingIP(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices) (*floatingips.FloatingIP, error) {
177 createOpts := floatingips.CreateOpts{
178 Pool: choices.FloatingIPPoolName,
Joe Topjiandee32222015-02-09 23:56:26 +0000179 }
Joe Topjian48209e32016-07-25 16:31:06 +0000180 floatingIP, err := floatingips.Create(client, createOpts).Extract()
Joe Topjiandee32222015-02-09 23:56:26 +0000181 if err != nil {
Joe Topjian48209e32016-07-25 16:31:06 +0000182 return floatingIP, err
Joe Topjiandee32222015-02-09 23:56:26 +0000183 }
184
Joe Topjian48209e32016-07-25 16:31:06 +0000185 t.Logf("Created floating IP: %s", floatingIP.ID)
186 return floatingIP, nil
187}
188
189func deleteFloatingIP(t *testing.T, client *gophercloud.ServiceClient, floatingIP *floatingips.FloatingIP) {
190 err := floatingips.Delete(client, floatingIP.ID).ExtractErr()
Joe Topjiandee32222015-02-09 23:56:26 +0000191 if err != nil {
Joe Topjian48209e32016-07-25 16:31:06 +0000192 t.Fatalf("Unable to delete floating IP %s: %v", floatingIP.ID, err)
Joe Topjiandee32222015-02-09 23:56:26 +0000193 }
194
Joe Topjian48209e32016-07-25 16:31:06 +0000195 t.Logf("Deleted floating IP: %s", floatingIP.ID)
196}
197
198func disassociateFloatingIP(t *testing.T, client *gophercloud.ServiceClient, floatingIP *floatingips.FloatingIP, server *servers.Server) {
199 disassociateOpts := floatingips.DisassociateOpts{
200 FloatingIP: floatingIP.IP,
201 }
202
203 err := floatingips.DisassociateInstance(client, server.ID, disassociateOpts).ExtractErr()
Joe Topjiandee32222015-02-09 23:56:26 +0000204 if err != nil {
Joe Topjian48209e32016-07-25 16:31:06 +0000205 t.Fatalf("Unable to disassociate floating IP %s from server %s: %v", floatingIP.IP, server.ID, err)
Joe Topjiandee32222015-02-09 23:56:26 +0000206 }
207
Joe Topjian48209e32016-07-25 16:31:06 +0000208 t.Logf("Disassociated floating IP %s from server %s", floatingIP.IP, server.ID)
209}
Joe Topjiandee32222015-02-09 23:56:26 +0000210
Joe Topjian48209e32016-07-25 16:31:06 +0000211func printFloatingIP(t *testing.T, floatingIP *floatingips.FloatingIP) {
212 t.Logf("ID: %s", floatingIP.ID)
213 t.Logf("Fixed IP: %s", floatingIP.FixedIP)
214 t.Logf("Instance ID: %s", floatingIP.InstanceID)
215 t.Logf("IP: %s", floatingIP.IP)
216 t.Logf("Pool: %s", floatingIP.Pool)
Joe Topjiandee32222015-02-09 23:56:26 +0000217}