2014-10-08 01:29:28 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
2014-10-09 15:21:06 +00:00
|
|
|
"os"
|
2014-10-08 01:29:28 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-10-09 15:21:06 +00:00
|
|
|
func TestReadConfig(t *testing.T) {
|
2014-10-09 13:22:01 +00:00
|
|
|
_, err := readConfig(nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Should fail when no config is provided: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg, err := readConfig(strings.NewReader(`
|
|
|
|
[Global]
|
|
|
|
authurl = http://auth.url
|
|
|
|
username = user
|
|
|
|
`))
|
2014-10-08 01:29:28 +00:00
|
|
|
if err != nil {
|
2014-10-09 13:22:01 +00:00
|
|
|
t.Fatalf("Should succeed when a valid config is provided: %s", err)
|
|
|
|
}
|
|
|
|
if cfg.Global.AuthUrl != "http://auth.url" {
|
|
|
|
t.Errorf("incorrect authurl: %s", cfg.Global.AuthUrl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestToAuthOptions(t *testing.T) {
|
|
|
|
cfg := Config{}
|
|
|
|
cfg.Global.Username = "user"
|
|
|
|
// etc.
|
|
|
|
|
|
|
|
ao := cfg.toAuthOptions()
|
|
|
|
|
|
|
|
if !ao.AllowReauth {
|
|
|
|
t.Errorf("Will need to be able to reauthenticate")
|
|
|
|
}
|
|
|
|
if ao.Username != cfg.Global.Username {
|
|
|
|
t.Errorf("Username %s != %s", ao.Username, cfg.Global.Username)
|
2014-10-08 01:29:28 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-09 15:21:06 +00:00
|
|
|
|
|
|
|
// 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")
|
|
|
|
|
|
|
|
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")
|
2014-10-29 08:30:55 +00:00
|
|
|
cfg.Global.DomainId = os.Getenv("OS_DOMAIN_ID")
|
|
|
|
cfg.Global.DomainName = os.Getenv("OS_DOMAIN_NAME")
|
2014-10-09 15:21:06 +00:00
|
|
|
|
|
|
|
ok = (cfg.Global.AuthUrl != "" &&
|
|
|
|
cfg.Global.Username != "" &&
|
|
|
|
(cfg.Global.Password != "" || cfg.Global.ApiKey != "") &&
|
2014-10-29 08:30:55 +00:00
|
|
|
(cfg.Global.TenantId != "" || cfg.Global.TenantName != "" ||
|
|
|
|
cfg.Global.DomainId != "" || cfg.Global.DomainName != ""))
|
2014-10-09 15:21:06 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|