Compute Acceptance Test Changes (#33)

This commit makes more changes to the compute acceptance tests:

* Makes all reusable functions exportable so other APIs can use them.
* Centralizes client initialization and environment variable checks.
diff --git a/acceptance/openstack/compute/v2/servergroup_test.go b/acceptance/openstack/compute/v2/servergroup_test.go
index 0ee47ea..5921014 100644
--- a/acceptance/openstack/compute/v2/servergroup_test.go
+++ b/acceptance/openstack/compute/v2/servergroup_test.go
@@ -5,15 +5,13 @@
 import (
 	"testing"
 
-	"github.com/gophercloud/gophercloud"
-	"github.com/gophercloud/gophercloud/acceptance/tools"
-	"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/schedulerhints"
+	"github.com/gophercloud/gophercloud/acceptance/clients"
 	"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/servergroups"
 	"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
 )
 
 func TestServergroupsList(t *testing.T) {
-	client, err := newClient()
+	client, err := clients.NewComputeV2Client()
 	if err != nil {
 		t.Fatalf("Unable to create a compute client: %v", err)
 	}
@@ -29,68 +27,64 @@
 	}
 
 	for _, serverGroup := range allServerGroups {
-		printServerGroup(t, &serverGroup)
+		PrintServerGroup(t, &serverGroup)
 	}
 }
 
 func TestServergroupsCreate(t *testing.T) {
-	client, err := newClient()
+	client, err := clients.NewComputeV2Client()
 	if err != nil {
 		t.Fatalf("Unable to create a compute client: %v", err)
 	}
 
-	serverGroup, err := createServerGroup(t, client, "anti-affinity")
+	serverGroup, err := CreateServerGroup(t, client, "anti-affinity")
 	if err != nil {
 		t.Fatalf("Unable to create server group: %v", err)
 	}
-	defer deleteServerGroup(t, client, serverGroup)
+	defer DeleteServerGroup(t, client, serverGroup)
 
 	serverGroup, err = servergroups.Get(client, serverGroup.ID).Extract()
 	if err != nil {
 		t.Fatalf("Unable to get server group: %v", err)
 	}
 
-	printServerGroup(t, serverGroup)
+	PrintServerGroup(t, serverGroup)
 }
 
 func TestServergroupsAffinityPolicy(t *testing.T) {
-	client, err := newClient()
+	client, err := clients.NewComputeV2Client()
 	if err != nil {
 		t.Fatalf("Unable to create a compute client: %v", err)
 	}
 
-	choices, err := ComputeChoicesFromEnv()
+	choices, err := clients.AcceptanceTestChoicesFromEnv()
 	if err != nil {
 		t.Fatal(err)
 	}
 
-	serverGroup, err := createServerGroup(t, client, "affinity")
+	serverGroup, err := CreateServerGroup(t, client, "affinity")
 	if err != nil {
 		t.Fatalf("Unable to create server group: %v", err)
 	}
-	defer deleteServerGroup(t, client, serverGroup)
+	defer DeleteServerGroup(t, client, serverGroup)
 
-	firstServer, err := createServerInServerGroup(t, client, choices, serverGroup)
+	firstServer, err := CreateServerInServerGroup(t, client, choices, serverGroup)
 	if err != nil {
 		t.Fatalf("Unable to create server: %v", err)
 	}
-
-	if err = waitForStatus(client, firstServer, "ACTIVE"); err != nil {
-		t.Fatalf("Unable to wait for server: %v", err)
-	}
-	defer deleteServer(t, client, firstServer)
+	defer DeleteServer(t, client, firstServer)
 
 	firstServer, err = servers.Get(client, firstServer.ID).Extract()
 
-	secondServer, err := createServerInServerGroup(t, client, choices, serverGroup)
+	secondServer, err := CreateServerInServerGroup(t, client, choices, serverGroup)
 	if err != nil {
 		t.Fatalf("Unable to create server: %v", err)
 	}
 
-	if err = waitForStatus(client, secondServer, "ACTIVE"); err != nil {
+	if err = WaitForComputeStatus(client, secondServer, "ACTIVE"); err != nil {
 		t.Fatalf("Unable to wait for server: %v", err)
 	}
-	defer deleteServer(t, client, secondServer)
+	defer DeleteServer(t, client, secondServer)
 
 	secondServer, err = servers.Get(client, secondServer.ID).Extract()
 
@@ -98,72 +92,3 @@
 		t.Fatalf("%s and %s were not scheduled on the same host.", firstServer.ID, secondServer.ID)
 	}
 }
-
-func createServerGroup(t *testing.T, client *gophercloud.ServiceClient, policy string) (*servergroups.ServerGroup, error) {
-	sg, err := servergroups.Create(client, &servergroups.CreateOpts{
-		Name:     "test",
-		Policies: []string{policy},
-	}).Extract()
-
-	if err != nil {
-		t.Fatalf("Unable to create server group: %v", err)
-	}
-
-	return sg, nil
-}
-
-func createServerInServerGroup(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices, serverGroup *servergroups.ServerGroup) (*servers.Server, error) {
-	if testing.Short() {
-		t.Skip("Skipping test that requires server creation in short mode.")
-	}
-
-	networkID, err := getNetworkIDFromTenantNetworks(t, client, choices.NetworkName)
-	if err != nil {
-		t.Fatalf("Failed to obtain network ID: %v", err)
-	}
-
-	name := tools.RandomString("ACPTTEST", 16)
-	t.Logf("Attempting to create server: %s", name)
-
-	pwd := tools.MakeNewPassword("")
-
-	serverCreateOpts := servers.CreateOpts{
-		Name:      name,
-		FlavorRef: choices.FlavorID,
-		ImageRef:  choices.ImageID,
-		AdminPass: pwd,
-		Networks: []servers.Network{
-			servers.Network{UUID: networkID},
-		},
-	}
-
-	schedulerHintsOpts := schedulerhints.CreateOptsExt{
-		serverCreateOpts,
-		schedulerhints.SchedulerHints{
-			Group: serverGroup.ID,
-		},
-	}
-	server, err := servers.Create(client, schedulerHintsOpts).Extract()
-	if err != nil {
-		t.Fatalf("Unable to create server: %v", err)
-	}
-
-	return server, err
-}
-
-func deleteServerGroup(t *testing.T, client *gophercloud.ServiceClient, serverGroup *servergroups.ServerGroup) {
-	err := servergroups.Delete(client, serverGroup.ID).ExtractErr()
-	if err != nil {
-		t.Fatalf("Unable to delete server group %s: %v", serverGroup.ID, err)
-	}
-
-	t.Logf("Deleted server group %s", serverGroup.ID)
-}
-
-func printServerGroup(t *testing.T, serverGroup *servergroups.ServerGroup) {
-	t.Logf("ID: %s", serverGroup.ID)
-	t.Logf("Name: %s", serverGroup.Name)
-	t.Logf("Policies: %#v", serverGroup.Policies)
-	t.Logf("Members: %#v", serverGroup.Members)
-	t.Logf("Metadata: %#v", serverGroup.Metadata)
-}