Implement Instances interface for OpenStack cloud provider

This commit is contained in:
Angus Lees
2014-10-10 02:21:06 +11:00
parent a90d503fce
commit fffa0527d4
2 changed files with 195 additions and 3 deletions

View File

@@ -17,11 +17,12 @@ limitations under the License.
package openstack
import (
"os"
"strings"
"testing"
)
func TestNewOpenStack(t *testing.T) {
func TestReadConfig(t *testing.T) {
_, err := readConfig(nil)
if err == nil {
t.Errorf("Should fail when no config is provided: %s", err)
@@ -54,3 +55,81 @@ func TestToAuthOptions(t *testing.T) {
t.Errorf("Username %s != %s", ao.Username, cfg.Global.Username)
}
}
// This allows testing against an existing OpenStack install, using the
// standard OS_* OpenStack client environment variables.
func configFromEnv() (cfg Config, ok bool) {
cfg.Global.AuthUrl = os.Getenv("OS_AUTH_URL")
// gophercloud wants "provider" to point specifically at tokens URL
if !strings.HasSuffix(cfg.Global.AuthUrl, "/tokens") {
cfg.Global.AuthUrl += "/tokens"
}
cfg.Global.TenantId = os.Getenv("OS_TENANT_ID")
// Rax/nova _insists_ that we don't specify both tenant ID and name
if cfg.Global.TenantId == "" {
cfg.Global.TenantName = os.Getenv("OS_TENANT_NAME")
}
cfg.Global.Username = os.Getenv("OS_USERNAME")
cfg.Global.Password = os.Getenv("OS_PASSWORD")
cfg.Global.ApiKey = os.Getenv("OS_API_KEY")
cfg.Global.Region = os.Getenv("OS_REGION_NAME")
ok = (cfg.Global.AuthUrl != "" &&
cfg.Global.Username != "" &&
(cfg.Global.Password != "" || cfg.Global.ApiKey != "") &&
(cfg.Global.TenantId != "" || cfg.Global.TenantName != ""))
return
}
func TestNewOpenStack(t *testing.T) {
cfg, ok := configFromEnv()
if !ok {
t.Skipf("No config found in environment")
}
_, err := newOpenStack(cfg)
if err != nil {
t.Fatalf("Failed to construct/authenticate OpenStack: %s", err)
}
}
func TestInstances(t *testing.T) {
cfg, ok := configFromEnv()
if !ok {
t.Skipf("No config found in environment")
}
os, err := newOpenStack(cfg)
if err != nil {
t.Fatalf("Failed to construct/authenticate OpenStack: %s", err)
}
i, ok := os.Instances()
if !ok {
t.Fatalf("Instances() returned false")
}
srvs, err := i.List(".")
if err != nil {
t.Fatalf("Instances.List() failed: %s", err)
}
if len(srvs) == 0 {
t.Fatalf("Instances.List() returned zero servers")
}
t.Logf("Found servers (%d): %s\n", len(srvs), srvs)
ip, err := i.IPAddress(srvs[0])
if err != nil {
t.Fatalf("Instances.IPAddress(%s) failed: %s", srvs[0], err)
}
t.Logf("Found IPAddress(%s) = %s\n", srvs[0], ip)
rsrcs, err := i.GetNodeResources(srvs[0])
if err != nil {
t.Fatalf("Instances.GetNodeResources(%s) failed: %s", srvs[0], err)
}
t.Logf("Found GetNodeResources(%s) = %s\n", srvs[0], rsrcs)
}