Compute Floating IP Acceptance Test Cleanup
The following changes were made to the floating IPs acceptance test:
* Renamed package floatingip to floatingips.
* Created List test.
* Broke out Create, Associate, and Associate Fixed IP tests.
* Removed deprecated associate tests.
* Printing all floating IP attributes.
diff --git a/acceptance/openstack/compute/v2/floatingip_test.go b/acceptance/openstack/compute/v2/floatingip_test.go
index 8231bd6..92249c7 100644
--- a/acceptance/openstack/compute/v2/floatingip_test.go
+++ b/acceptance/openstack/compute/v2/floatingip_test.go
@@ -3,97 +3,147 @@
package v2
import (
- "os"
"testing"
"github.com/gophercloud/gophercloud"
- "github.com/gophercloud/gophercloud/acceptance/tools"
- "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingip"
+ "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingips"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
- th "github.com/gophercloud/gophercloud/testhelper"
)
-func createFIPServer(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices) (*servers.Server, error) {
+func TestFloatingIPsList(t *testing.T) {
+ client, err := newClient()
+ if err != nil {
+ t.Fatalf("Unable to create a compute client: %v", err)
+ }
+
+ allPages, err := floatingips.List(client).AllPages()
+ if err != nil {
+ t.Fatalf("Unable to retrieve floating IPs: %v", err)
+ }
+
+ allFloatingIPs, err := floatingips.ExtractFloatingIPs(allPages)
+ if err != nil {
+ t.Fatalf("Unable to extract floating IPs: %v", err)
+ }
+
+ for _, floatingIP := range allFloatingIPs {
+ printFloatingIP(t, &floatingIP)
+ }
+}
+
+func TestFloatingIPsCreate(t *testing.T) {
+ client, err := newClient()
+ if err != nil {
+ t.Fatalf("Unable to create a compute client: %v", err)
+ }
+
+ choices, err := ComputeChoicesFromEnv()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ floatingIP, err := createFloatingIP(t, client, choices)
+ if err != nil {
+ t.Fatalf("Unable to create floating IP: %v", err)
+ }
+ defer deleteFloatingIP(t, client, floatingIP)
+
+ printFloatingIP(t, floatingIP)
+}
+
+func TestFloatingIPsAssociate(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test that requires server creation in short mode.")
}
- name := tools.RandomString("ACPTTEST", 16)
- t.Logf("Attempting to create server: %s\n", name)
+ client, err := newClient()
+ if err != nil {
+ t.Fatalf("Unable to create a compute client: %v", err)
+ }
- pwd := tools.MakeNewPassword("")
+ choices, err := ComputeChoicesFromEnv()
+ if err != nil {
+ t.Fatal(err)
+ }
- server, err := servers.Create(client, servers.CreateOpts{
- Name: name,
- FlavorRef: choices.FlavorID,
- ImageRef: choices.ImageID,
- AdminPass: pwd,
- }).Extract()
+ server, err := createServer(t, client, choices)
if err != nil {
t.Fatalf("Unable to create server: %v", err)
}
- th.AssertEquals(t, pwd, server.AdminPass)
-
- return server, err
-}
-
-func createFloatingIP(t *testing.T, client *gophercloud.ServiceClient) (*floatingip.FloatingIP, error) {
- pool := os.Getenv("OS_POOL_NAME")
- fip, err := floatingip.Create(client, &floatingip.CreateOpts{
- Pool: pool,
- }).Extract()
- th.AssertNoErr(t, err)
- t.Logf("Obtained Floating IP: %v", fip.IP)
-
- return fip, err
-}
-
-func associateFloatingIPDeprecated(t *testing.T, client *gophercloud.ServiceClient, serverId string, fip *floatingip.FloatingIP) {
- // This form works, but is considered deprecated.
- // See associateFloatingIP or associateFloatingIPFixed
- err := floatingip.Associate(client, serverId, fip.IP).ExtractErr()
- th.AssertNoErr(t, err)
- t.Logf("Associated floating IP %v from instance %v", fip.IP, serverId)
- defer func() {
- err = floatingip.Disassociate(client, serverId, fip.IP).ExtractErr()
- th.AssertNoErr(t, err)
- t.Logf("Disassociated floating IP %v from instance %v", fip.IP, serverId)
- }()
- floatingIp, err := floatingip.Get(client, fip.ID).Extract()
- th.AssertNoErr(t, err)
- t.Logf("Floating IP %v is associated with Fixed IP %v", fip.IP, floatingIp.FixedIP)
-}
-
-func associateFloatingIP(t *testing.T, client *gophercloud.ServiceClient, serverId string, fip *floatingip.FloatingIP) {
- associateOpts := floatingip.AssociateOpts{
- ServerID: serverId,
- FloatingIP: fip.IP,
+ if err = waitForStatus(client, server, "ACTIVE"); err != nil {
+ t.Fatalf("Unable to wait for server: %v", err)
}
+ defer deleteServer(t, client, server)
- err := floatingip.AssociateInstance(client, associateOpts).ExtractErr()
- th.AssertNoErr(t, err)
- t.Logf("Associated floating IP %v from instance %v", fip.IP, serverId)
- defer func() {
- err = floatingip.DisassociateInstance(client, associateOpts).ExtractErr()
- th.AssertNoErr(t, err)
- t.Logf("Disassociated floating IP %v from instance %v", fip.IP, serverId)
- }()
- floatingIp, err := floatingip.Get(client, fip.ID).Extract()
- th.AssertNoErr(t, err)
- t.Logf("Floating IP %v is associated with Fixed IP %v", fip.IP, floatingIp.FixedIP)
-}
-
-func associateFloatingIPFixed(t *testing.T, client *gophercloud.ServiceClient, serverId string, fip *floatingip.FloatingIP) {
-
- network := os.Getenv("OS_NETWORK_NAME")
- server, err := servers.Get(client, serverId).Extract()
+ floatingIP, err := createFloatingIP(t, client, choices)
if err != nil {
- t.Fatalf("%s", err)
+ t.Fatalf("Unable to create floating IP: %v", err)
}
+ defer deleteFloatingIP(t, client, floatingIP)
+
+ printFloatingIP(t, floatingIP)
+
+ associateOpts := floatingips.AssociateOpts{
+ FloatingIP: floatingIP.IP,
+ }
+
+ t.Logf("Attempting to associate floating IP %s to instance %s", floatingIP.IP, server.ID)
+ err = floatingips.AssociateInstance(client, server.ID, associateOpts).ExtractErr()
+ if err != nil {
+ t.Fatalf("Unable to associate floating IP %s with server %s: %v", floatingIP.IP, server.ID, err)
+ }
+ defer disassociateFloatingIP(t, client, floatingIP, server)
+ t.Logf("Floating IP %s is associated with Fixed IP %s", floatingIP.IP, floatingIP.FixedIP)
+
+ newFloatingIP, err := floatingips.Get(client, floatingIP.ID).Extract()
+ if err != nil {
+ t.Fatalf("Unable to get floating IP %s: %v", floatingIP.ID, err)
+ }
+
+ printFloatingIP(t, newFloatingIP)
+}
+
+func TestFloatingIPsFixedIPAssociate(t *testing.T) {
+ if testing.Short() {
+ t.Skip("Skipping test that requires server creation in short mode.")
+ }
+
+ client, err := newClient()
+ if err != nil {
+ t.Fatalf("Unable to create a compute client: %v", err)
+ }
+
+ choices, err := ComputeChoicesFromEnv()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ server, err := createServer(t, client, choices)
+ if err != nil {
+ t.Fatalf("Unable to create server: %v", err)
+ }
+
+ if err = waitForStatus(client, server, "ACTIVE"); err != nil {
+ t.Fatalf("Unable to wait for server: %v", err)
+ }
+ defer deleteServer(t, client, server)
+
+ newServer, err := servers.Get(client, server.ID).Extract()
+ if err != nil {
+ t.Fatalf("Unable to get server %s: %v", server.ID, err)
+ }
+
+ floatingIP, err := createFloatingIP(t, client, choices)
+ if err != nil {
+ t.Fatalf("Unable to create floating IP: %v", err)
+ }
+ defer deleteFloatingIP(t, client, floatingIP)
+
+ printFloatingIP(t, floatingIP)
var fixedIP string
- for _, networkAddresses := range server.Addresses[network].([]interface{}) {
+ for _, networkAddresses := range newServer.Addresses[choices.NetworkName].([]interface{}) {
address := networkAddresses.(map[string]interface{})
if address["OS-EXT-IPS:type"] == "fixed" {
if address["version"].(float64) == 4 {
@@ -102,67 +152,66 @@
}
}
- associateOpts := floatingip.AssociateOpts{
- ServerID: serverId,
- FloatingIP: fip.IP,
+ associateOpts := floatingips.AssociateOpts{
+ FloatingIP: floatingIP.IP,
FixedIP: fixedIP,
}
- err = floatingip.AssociateInstance(client, associateOpts).ExtractErr()
- th.AssertNoErr(t, err)
- t.Logf("Associated floating IP %v from instance %v with Fixed IP %v", fip.IP, serverId, fixedIP)
- defer func() {
- err = floatingip.DisassociateInstance(client, associateOpts).ExtractErr()
- th.AssertNoErr(t, err)
- t.Logf("Disassociated floating IP %v from instance %v with Fixed IP %v", fip.IP, serverId, fixedIP)
- }()
- floatingIp, err := floatingip.Get(client, fip.ID).Extract()
- th.AssertNoErr(t, err)
- th.AssertEquals(t, floatingIp.FixedIP, fixedIP)
- t.Logf("Floating IP %v is associated with Fixed IP %v", fip.IP, floatingIp.FixedIP)
+ t.Logf("Attempting to associate floating IP %s to instance %s", floatingIP.IP, newServer.ID)
+ err = floatingips.AssociateInstance(client, newServer.ID, associateOpts).ExtractErr()
+ if err != nil {
+ t.Fatalf("Unable to associate floating IP %s with server %s: %v", floatingIP.IP, newServer.ID, err)
+ }
+ defer disassociateFloatingIP(t, client, floatingIP, newServer)
+ t.Logf("Floating IP %s is associated with Fixed IP %s", floatingIP.IP, floatingIP.FixedIP)
+
+ newFloatingIP, err := floatingips.Get(client, floatingIP.ID).Extract()
+ if err != nil {
+ t.Fatalf("Unable to get floating IP %s: %v", floatingIP.ID, err)
+ }
+
+ printFloatingIP(t, newFloatingIP)
}
-func TestFloatingIP(t *testing.T) {
- pool := os.Getenv("OS_POOL_NAME")
- if pool == "" {
- t.Fatalf("OS_POOL_NAME must be set")
+func createFloatingIP(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices) (*floatingips.FloatingIP, error) {
+ createOpts := floatingips.CreateOpts{
+ Pool: choices.FloatingIPPoolName,
}
-
- choices, err := ComputeChoicesFromEnv()
+ floatingIP, err := floatingips.Create(client, createOpts).Extract()
if err != nil {
- t.Fatal(err)
+ return floatingIP, err
}
- client, err := newClient()
+ t.Logf("Created floating IP: %s", floatingIP.ID)
+ return floatingIP, nil
+}
+
+func deleteFloatingIP(t *testing.T, client *gophercloud.ServiceClient, floatingIP *floatingips.FloatingIP) {
+ err := floatingips.Delete(client, floatingIP.ID).ExtractErr()
if err != nil {
- t.Fatalf("Unable to create a compute client: %v", err)
+ t.Fatalf("Unable to delete floating IP %s: %v", floatingIP.ID, err)
}
- server, err := createFIPServer(t, client, choices)
+ t.Logf("Deleted floating IP: %s", floatingIP.ID)
+}
+
+func disassociateFloatingIP(t *testing.T, client *gophercloud.ServiceClient, floatingIP *floatingips.FloatingIP, server *servers.Server) {
+ disassociateOpts := floatingips.DisassociateOpts{
+ FloatingIP: floatingIP.IP,
+ }
+
+ err := floatingips.DisassociateInstance(client, server.ID, disassociateOpts).ExtractErr()
if err != nil {
- t.Fatalf("Unable to create server: %v", err)
- }
- defer func() {
- servers.Delete(client, server.ID)
- t.Logf("Server deleted.")
- }()
-
- if err = waitForStatus(client, server, "ACTIVE"); err != nil {
- t.Fatalf("Unable to wait for server: %v", err)
+ t.Fatalf("Unable to disassociate floating IP %s from server %s: %v", floatingIP.IP, server.ID, err)
}
- fip, err := createFloatingIP(t, client)
- if err != nil {
- t.Fatalf("Unable to create floating IP: %v", err)
- }
- defer func() {
- err = floatingip.Delete(client, fip.ID).ExtractErr()
- th.AssertNoErr(t, err)
- t.Logf("Floating IP deleted.")
- }()
+ t.Logf("Disassociated floating IP %s from server %s", floatingIP.IP, server.ID)
+}
- associateFloatingIPDeprecated(t, client, server.ID, fip)
- associateFloatingIP(t, client, server.ID, fip)
- associateFloatingIPFixed(t, client, server.ID, fip)
-
+func printFloatingIP(t *testing.T, floatingIP *floatingips.FloatingIP) {
+ t.Logf("ID: %s", floatingIP.ID)
+ t.Logf("Fixed IP: %s", floatingIP.FixedIP)
+ t.Logf("Instance ID: %s", floatingIP.InstanceID)
+ t.Logf("IP: %s", floatingIP.IP)
+ t.Logf("Pool: %s", floatingIP.Pool)
}