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/keypairs_test.go b/acceptance/openstack/compute/v2/keypairs_test.go
index b9fb57a..3c6abb4 100644
--- a/acceptance/openstack/compute/v2/keypairs_test.go
+++ b/acceptance/openstack/compute/v2/keypairs_test.go
@@ -3,22 +3,17 @@
package v2
import (
- "crypto/rand"
- "crypto/rsa"
"testing"
- "github.com/gophercloud/gophercloud"
- "github.com/gophercloud/gophercloud/acceptance/tools"
+ "github.com/gophercloud/gophercloud/acceptance/clients"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
-
- "golang.org/x/crypto/ssh"
)
const keyName = "gophercloud_test_key_pair"
func TestKeypairsList(t *testing.T) {
- client, err := newClient()
+ client, err := clients.NewComputeV2Client()
if err != nil {
t.Fatalf("Unable to create a compute client: %v", err)
}
@@ -34,30 +29,27 @@
}
for _, keypair := range allKeys {
- printKeyPair(t, &keypair)
+ PrintKeyPair(t, &keypair)
}
}
func TestKeypairsCreate(t *testing.T) {
- client, err := newClient()
+ client, err := clients.NewComputeV2Client()
if err != nil {
t.Fatalf("Unable to create a compute client: %v", err)
}
- createOpts := keypairs.CreateOpts{
- Name: keyName,
- }
- keyPair, err := keypairs.Create(client, createOpts).Extract()
+ keyPair, err := CreateKeyPair(t, client)
if err != nil {
- t.Fatalf("Unable to create keypair: %s", err)
+ t.Fatalf("Unable to create key pair: %v", err)
}
- defer deleteKeyPair(t, client, keyPair)
+ defer DeleteKeyPair(t, client, keyPair)
- printKeyPair(t, keyPair)
+ PrintKeyPair(t, keyPair)
}
func TestKeypairsImportPublicKey(t *testing.T) {
- client, err := newClient()
+ client, err := clients.NewComputeV2Client()
if err != nil {
t.Fatalf("Unable to create a compute client: %v", err)
}
@@ -67,17 +59,13 @@
t.Fatalf("Unable to create public key: %s", err)
}
- createOpts := keypairs.CreateOpts{
- Name: keyName,
- PublicKey: publicKey,
- }
- keyPair, err := keypairs.Create(client, createOpts).Extract()
+ keyPair, err := ImportPublicKey(t, client, publicKey)
if err != nil {
t.Fatalf("Unable to create keypair: %s", err)
}
- defer deleteKeyPair(t, client, keyPair)
+ defer DeleteKeyPair(t, client, keyPair)
- printKeyPair(t, keyPair)
+ PrintKeyPair(t, keyPair)
}
func TestKeypairsServerCreateWithKey(t *testing.T) {
@@ -85,12 +73,12 @@
t.Skip("Skipping test that requires server creation in short mode.")
}
- 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)
}
@@ -100,87 +88,24 @@
t.Fatalf("Unable to create public key: %s", err)
}
- createOpts := keypairs.CreateOpts{
- Name: keyName,
- PublicKey: publicKey,
- }
-
- keyPair, err := keypairs.Create(client, createOpts).Extract()
+ keyPair, err := ImportPublicKey(t, client, publicKey)
if err != nil {
t.Fatalf("Unable to create keypair: %s", err)
}
- defer deleteKeyPair(t, client, keyPair)
+ defer DeleteKeyPair(t, client, keyPair)
- 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)
-
- serverCreateOpts := servers.CreateOpts{
- Name: name,
- FlavorRef: choices.FlavorID,
- ImageRef: choices.ImageID,
- Networks: []servers.Network{
- servers.Network{UUID: networkID},
- },
- }
-
- server, err := servers.Create(client, keypairs.CreateOptsExt{
- serverCreateOpts,
- keyName,
- }).Extract()
+ server, err := CreateServerWithPublicKey(t, client, choices, keyPair.Name)
if err != nil {
t.Fatalf("Unable to create server: %s", err)
}
-
- if err = waitForStatus(client, server, "ACTIVE"); err != nil {
- t.Fatalf("Unable to wait for server: %v", err)
- }
- defer deleteServer(t, client, server)
+ defer DeleteServer(t, client, server)
server, err = servers.Get(client, server.ID).Extract()
if err != nil {
t.Fatalf("Unable to retrieve server: %s", err)
}
- if server.KeyName != keyName {
- t.Fatalf("key name of server %s is %s, not %s", server.ID, server.KeyName, keyName)
+ if server.KeyName != keyPair.Name {
+ t.Fatalf("key name of server %s is %s, not %s", server.ID, server.KeyName, keyPair.Name)
}
}
-
-func createKey() (string, error) {
- privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
- if err != nil {
- return "", err
- }
-
- publicKey := privateKey.PublicKey
- pub, err := ssh.NewPublicKey(&publicKey)
- if err != nil {
- return "", err
- }
-
- pubBytes := ssh.MarshalAuthorizedKey(pub)
- pk := string(pubBytes)
- return pk, nil
-}
-
-func deleteKeyPair(t *testing.T, client *gophercloud.ServiceClient, keyPair *keypairs.KeyPair) {
- err := keypairs.Delete(client, keyPair.Name).ExtractErr()
- if err != nil {
- t.Fatalf("Unable to delete keypair %s: %v", keyPair.Name, err)
- }
-
- t.Logf("Deleted keypair: %s", keyPair.Name)
-}
-
-func printKeyPair(t *testing.T, keypair *keypairs.KeyPair) {
- t.Logf("Name: %s", keypair.Name)
- t.Logf("Fingerprint: %s", keypair.Fingerprint)
- t.Logf("Public Key: %s", keypair.PublicKey)
- t.Logf("Private Key: %s", keypair.PrivateKey)
- t.Logf("UserID: %s", keypair.UserID)
-}