2014-10-08 01:29:28 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2014-10-08 01:29:28 +00:00
|
|
|
|
|
|
|
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-30 03:16:50 +00:00
|
|
|
"time"
|
|
|
|
|
2016-02-12 08:46:59 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/rand"
|
|
|
|
|
2014-10-30 03:16:50 +00:00
|
|
|
"github.com/rackspace/gophercloud"
|
Change LoadBalancer methods to take api.Service
This is a better abstraction than passing in specific pieces of the
Service that each of the cloudproviders may or may not need. For
instance, many of the providers don't need a region, yet this is passed
in. Similarly many of the providers want a string IP for the load
balancer, but it passes in a converted net ip. Affinity is unused by
AWS. A provider change may also require adding a new parameter which has
an effect on all other cloud provider implementations.
Further, this will simplify adding provider specific load balancer
options, such as with labels or some other metadata. For example, we
could add labels for configuring the details of an AWS elastic load
balancer, such as idle timeout on connections, whether it is
internal or external, cross-zone load balancing, and so on.
Authors: @chbatey, @jsravn
2016-02-17 11:36:50 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2014-10-08 01:29:28 +00:00
|
|
|
)
|
|
|
|
|
2016-04-04 22:01:44 +00:00
|
|
|
const volumeAvailableStatus = "available"
|
|
|
|
const volumeInUseStatus = "in-use"
|
|
|
|
const volumeCreateTimeoutSeconds = 30
|
2016-05-29 03:54:26 +00:00
|
|
|
const testClusterName = "testCluster"
|
2016-04-04 22:01:44 +00:00
|
|
|
|
|
|
|
func WaitForVolumeStatus(t *testing.T, os *OpenStack, volumeName string, status string, timeoutSeconds int) {
|
|
|
|
timeout := timeoutSeconds
|
|
|
|
start := time.Now().Second()
|
|
|
|
for {
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
if timeout >= 0 && time.Now().Second()-start >= timeout {
|
2016-04-15 19:18:27 +00:00
|
|
|
t.Logf("Volume (%s) status did not change to %s after %v seconds\n",
|
2016-04-04 22:01:44 +00:00
|
|
|
volumeName,
|
|
|
|
status,
|
|
|
|
timeout)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
getVol, err := os.getVolume(volumeName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot get existing Cinder volume (%s): %v", volumeName, err)
|
|
|
|
}
|
|
|
|
if getVol.Status == status {
|
2016-04-15 19:18:27 +00:00
|
|
|
t.Logf("Volume (%s) status changed to %s after %v seconds\n",
|
2016-04-04 22:01:44 +00:00
|
|
|
volumeName,
|
|
|
|
status,
|
|
|
|
timeout)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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(`
|
2016-05-17 22:05:03 +00:00
|
|
|
[Global]
|
|
|
|
auth-url = http://auth.url
|
|
|
|
username = user
|
|
|
|
[LoadBalancer]
|
|
|
|
create-monitor = yes
|
|
|
|
monitor-delay = 1m
|
|
|
|
monitor-timeout = 30s
|
|
|
|
monitor-max-retries = 3
|
|
|
|
`))
|
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)
|
|
|
|
}
|
2014-10-30 03:16:50 +00:00
|
|
|
|
|
|
|
if !cfg.LoadBalancer.CreateMonitor {
|
2015-01-07 08:40:16 +00:00
|
|
|
t.Errorf("incorrect lb.createmonitor: %t", cfg.LoadBalancer.CreateMonitor)
|
2014-10-30 03:16:50 +00:00
|
|
|
}
|
|
|
|
if cfg.LoadBalancer.MonitorDelay.Duration != 1*time.Minute {
|
|
|
|
t.Errorf("incorrect lb.monitordelay: %s", cfg.LoadBalancer.MonitorDelay)
|
|
|
|
}
|
|
|
|
if cfg.LoadBalancer.MonitorTimeout.Duration != 30*time.Second {
|
|
|
|
t.Errorf("incorrect lb.monitortimeout: %s", cfg.LoadBalancer.MonitorTimeout)
|
|
|
|
}
|
|
|
|
if cfg.LoadBalancer.MonitorMaxRetries != 3 {
|
2015-01-07 08:40:16 +00:00
|
|
|
t.Errorf("incorrect lb.monitormaxretries: %d", cfg.LoadBalancer.MonitorMaxRetries)
|
2014-10-30 03:16:50 +00:00
|
|
|
}
|
2014-10-09 13:22:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2014-10-30 03:16:50 +00:00
|
|
|
// This allows acceptance testing against an existing OpenStack
|
|
|
|
// install, using the standard OS_* OpenStack client environment
|
|
|
|
// variables.
|
|
|
|
// FIXME: it would be better to hermetically test against canned JSON
|
|
|
|
// requests/responses.
|
2014-10-09 15:21:06 +00:00
|
|
|
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)
|
|
|
|
|
2016-04-01 23:48:46 +00:00
|
|
|
srvExternalId, err := i.ExternalID(srvs[0])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Instances.ExternalId(%s) failed: %s", srvs[0], err)
|
|
|
|
}
|
|
|
|
t.Logf("Found server (%s), with external id: %s\n", srvs[0], srvExternalId)
|
|
|
|
|
|
|
|
srvInstanceId, err := i.InstanceID(srvs[0])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Instance.InstanceId(%s) failed: %s", srvs[0], err)
|
|
|
|
}
|
|
|
|
t.Logf("Found server (%s), with instance id: %s\n", srvs[0], srvInstanceId)
|
|
|
|
|
2015-03-11 23:37:11 +00:00
|
|
|
addrs, err := i.NodeAddresses(srvs[0])
|
2014-10-09 15:21:06 +00:00
|
|
|
if err != nil {
|
2015-03-11 23:37:11 +00:00
|
|
|
t.Fatalf("Instances.NodeAddresses(%s) failed: %s", srvs[0], err)
|
2014-10-09 15:21:06 +00:00
|
|
|
}
|
2015-03-11 23:37:11 +00:00
|
|
|
t.Logf("Found NodeAddresses(%s) = %s\n", srvs[0], addrs)
|
2014-10-09 15:21:06 +00:00
|
|
|
}
|
2014-10-30 03:16:50 +00:00
|
|
|
|
2015-09-28 20:57:58 +00:00
|
|
|
func TestLoadBalancer(t *testing.T) {
|
2014-10-30 03:16:50 +00:00
|
|
|
cfg, ok := configFromEnv()
|
|
|
|
if !ok {
|
|
|
|
t.Skipf("No config found in environment")
|
|
|
|
}
|
|
|
|
|
2016-07-28 07:07:10 +00:00
|
|
|
versions := []string{"v1", "v2", ""}
|
2016-05-17 22:05:03 +00:00
|
|
|
|
2016-07-28 07:07:10 +00:00
|
|
|
for _, v := range versions {
|
|
|
|
t.Logf("Trying LBVersion = '%s'\n", v)
|
|
|
|
cfg.LoadBalancer.LBVersion = v
|
2014-10-30 03:16:50 +00:00
|
|
|
|
2016-07-28 07:07:10 +00:00
|
|
|
os, err := newOpenStack(cfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to construct/authenticate OpenStack: %s", err)
|
|
|
|
}
|
2016-05-17 22:05:03 +00:00
|
|
|
|
2016-07-28 07:07:10 +00:00
|
|
|
lb, ok := os.LoadBalancer()
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("LoadBalancer() returned false - perhaps your stack doesn't support Neutron?")
|
|
|
|
}
|
2016-05-17 22:05:03 +00:00
|
|
|
|
2016-07-28 07:07:10 +00:00
|
|
|
_, exists, err := lb.GetLoadBalancer(testClusterName, &api.Service{ObjectMeta: api.ObjectMeta{Name: "noexist"}})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("GetLoadBalancer(\"noexist\") returned error: %s", err)
|
|
|
|
}
|
|
|
|
if exists {
|
|
|
|
t.Fatalf("GetLoadBalancer(\"noexist\") returned exists")
|
|
|
|
}
|
2016-05-17 22:05:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-30 03:16:50 +00:00
|
|
|
func TestZones(t *testing.T) {
|
2016-08-30 02:41:43 +00:00
|
|
|
SetMetadataFixture(&FakeMetadata)
|
|
|
|
defer ClearMetadata()
|
|
|
|
|
2014-10-30 03:16:50 +00:00
|
|
|
os := OpenStack{
|
|
|
|
provider: &gophercloud.ProviderClient{
|
|
|
|
IdentityBase: "http://auth.url/",
|
|
|
|
},
|
|
|
|
region: "myRegion",
|
|
|
|
}
|
|
|
|
|
|
|
|
z, ok := os.Zones()
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Zones() returned false")
|
|
|
|
}
|
|
|
|
|
|
|
|
zone, err := z.GetZone()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("GetZone() returned error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if zone.Region != "myRegion" {
|
|
|
|
t.Fatalf("GetZone() returned wrong region (%s)", zone.Region)
|
|
|
|
}
|
2016-08-30 02:41:43 +00:00
|
|
|
|
|
|
|
if zone.FailureDomain != "nova" {
|
|
|
|
t.Fatalf("GetZone() returned wrong failure domain (%s)", zone.FailureDomain)
|
|
|
|
}
|
2014-10-30 03:16:50 +00:00
|
|
|
}
|
2015-12-15 11:38:59 +00:00
|
|
|
|
|
|
|
func TestVolumes(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)
|
|
|
|
}
|
|
|
|
|
2015-12-21 10:36:42 +00:00
|
|
|
tags := map[string]string{
|
|
|
|
"test": "value",
|
|
|
|
}
|
2016-08-17 19:24:53 +00:00
|
|
|
vol, err := os.CreateVolume("kubernetes-test-volume-"+rand.String(10), 1, "", "", &tags)
|
2015-12-15 11:38:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot create a new Cinder volume: %v", err)
|
|
|
|
}
|
2016-04-04 22:01:44 +00:00
|
|
|
t.Logf("Volume (%s) created\n", vol)
|
|
|
|
|
|
|
|
WaitForVolumeStatus(t, os, vol, volumeAvailableStatus, volumeCreateTimeoutSeconds)
|
|
|
|
|
2016-02-24 17:07:04 +00:00
|
|
|
diskId, err := os.AttachDisk(os.localInstanceID, vol)
|
2016-04-04 22:01:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot AttachDisk Cinder volume %s: %v", vol, err)
|
|
|
|
}
|
|
|
|
t.Logf("Volume (%s) attached, disk ID: %s\n", vol, diskId)
|
|
|
|
|
|
|
|
WaitForVolumeStatus(t, os, vol, volumeInUseStatus, volumeCreateTimeoutSeconds)
|
|
|
|
|
2016-02-24 17:07:04 +00:00
|
|
|
err = os.DetachDisk(os.localInstanceID, vol)
|
2016-04-04 22:01:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot DetachDisk Cinder volume %s: %v", vol, err)
|
|
|
|
}
|
|
|
|
t.Logf("Volume (%s) detached\n", vol)
|
|
|
|
|
|
|
|
WaitForVolumeStatus(t, os, vol, volumeAvailableStatus, volumeCreateTimeoutSeconds)
|
2015-12-15 11:38:59 +00:00
|
|
|
|
|
|
|
err = os.DeleteVolume(vol)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot delete Cinder volume %s: %v", vol, err)
|
|
|
|
}
|
2016-04-04 22:01:44 +00:00
|
|
|
t.Logf("Volume (%s) deleted\n", vol)
|
2015-12-15 11:38:59 +00:00
|
|
|
|
|
|
|
}
|