Skip to content
This repository was archived by the owner on Mar 22, 2018. It is now read-only.

Commit a15cff3

Browse files
author
Kubernetes Submit Queue
authored
Merge pull request #56437 from dims/fix-unit-tests-that-need-openstack
Automatic merge from submit-queue (batch tested with PRs 56446, 56437). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Fix unit tests that need openstack **What this PR does / why we need it**: Currently the unit tests that depend that they be on running inside an openstack vm fail as no one seem to have run them for a while. **Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*: ref #56437 **Special notes for your reviewer**: **Release note**: ```release-note NONE ```
2 parents 7f80ddd + 230bd2a commit a15cff3

3 files changed

Lines changed: 66 additions & 22 deletions

File tree

pkg/cloudprovider/providers/openstack/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ go_test(
7676
"//pkg/cloudprovider:go_default_library",
7777
"//vendor/github.com/gophercloud/gophercloud:go_default_library",
7878
"//vendor/github.com/gophercloud/gophercloud/openstack/compute/v2/servers:go_default_library",
79+
"//vendor/github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers:go_default_library",
7980
"//vendor/k8s.io/api/core/v1:go_default_library",
8081
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
8182
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",

pkg/cloudprovider/providers/openstack/openstack_routes_test.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"net"
2121
"testing"
2222

23+
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
24+
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers"
2325
"k8s.io/apimachinery/pkg/types"
2426
"k8s.io/kubernetes/pkg/cloudprovider"
2527
)
@@ -37,14 +39,18 @@ func TestRoutes(t *testing.T) {
3739
t.Fatalf("Failed to construct/authenticate OpenStack: %s", err)
3840
}
3941

42+
// Pick the first router and server to try a test with
43+
os.routeOpts.RouterId = getRouters(os)[0].ID
44+
servername := getServers(os)[0].Name
45+
4046
r, ok := os.Routes()
4147
if !ok {
42-
t.Fatalf("Routes() returned false - perhaps your stack doens't support Neutron?")
48+
t.Skip("Routes() returned false - perhaps your stack does not support Neutron extraroute extension?")
4349
}
4450

4551
newroute := cloudprovider.Route{
4652
DestinationCIDR: "10.164.2.0/24",
47-
TargetNode: types.NodeName("testinstance"),
53+
TargetNode: types.NodeName(servername),
4854
}
4955
err = r.CreateRoute(clusterName, "myhint", &newroute)
5056
if err != nil {
@@ -69,3 +75,39 @@ func TestRoutes(t *testing.T) {
6975
t.Fatalf("DeleteRoute error: %v", err)
7076
}
7177
}
78+
79+
func getServers(os *OpenStack) []servers.Server {
80+
c, err := os.NewComputeV2()
81+
allPages, err := servers.List(c, servers.ListOpts{}).AllPages()
82+
if err != nil {
83+
panic(err)
84+
}
85+
allServers, err := servers.ExtractServers(allPages)
86+
if err != nil {
87+
panic(err)
88+
}
89+
if len(allServers) == 0 {
90+
panic("No servers to test with")
91+
}
92+
return allServers
93+
}
94+
95+
func getRouters(os *OpenStack) []routers.Router {
96+
listOpts := routers.ListOpts{}
97+
n, err := os.NewNetworkV2()
98+
if err != nil {
99+
panic(err)
100+
}
101+
allPages, err := routers.List(n, listOpts).AllPages()
102+
if err != nil {
103+
panic(err)
104+
}
105+
allRouters, err := routers.ExtractRouters(allPages)
106+
if err != nil {
107+
panic(err)
108+
}
109+
if len(allRouters) == 0 {
110+
panic("No routers to test with")
111+
}
112+
return allRouters
113+
}

pkg/cloudprovider/providers/openstack/openstack_test.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ func configFromEnv() (cfg Config, ok bool) {
421421
cfg.Global.DomainId != "" || cfg.Global.DomainName != ""))
422422

423423
cfg.Metadata.SearchOrder = fmt.Sprintf("%s,%s", configDriveID, metadataID)
424+
cfg.BlockStorage.BSVersion = "auto"
424425

425426
return
426427
}
@@ -443,7 +444,7 @@ func TestLoadBalancer(t *testing.T) {
443444
t.Skipf("No config found in environment")
444445
}
445446

446-
versions := []string{"v1", "v2", ""}
447+
versions := []string{"v2", ""}
447448

448449
for _, v := range versions {
449450
t.Logf("Trying LBVersion = '%s'\n", v)
@@ -525,30 +526,30 @@ func TestVolumes(t *testing.T) {
525526

526527
id, err := os.InstanceID()
527528
if err != nil {
528-
t.Fatalf("Cannot find instance id: %v", err)
529-
}
529+
t.Logf("Cannot find instance id: %v - perhaps you are running this test outside a VM launched by OpenStack", err)
530+
} else {
531+
diskId, err := os.AttachDisk(id, vol)
532+
if err != nil {
533+
t.Fatalf("Cannot AttachDisk Cinder volume %s: %v", vol, err)
534+
}
535+
t.Logf("Volume (%s) attached, disk ID: %s\n", vol, diskId)
530536

531-
diskId, err := os.AttachDisk(id, vol)
532-
if err != nil {
533-
t.Fatalf("Cannot AttachDisk Cinder volume %s: %v", vol, err)
534-
}
535-
t.Logf("Volume (%s) attached, disk ID: %s\n", vol, diskId)
537+
WaitForVolumeStatus(t, os, vol, volumeInUseStatus)
536538

537-
WaitForVolumeStatus(t, os, vol, volumeInUseStatus)
539+
devicePath := os.GetDevicePath(diskId)
540+
if diskPathRegexp.FindString(devicePath) == "" {
541+
t.Fatalf("GetDevicePath returned and unexpected path for Cinder volume %s, returned %s", vol, devicePath)
542+
}
543+
t.Logf("Volume (%s) found at path: %s\n", vol, devicePath)
538544

539-
devicePath := os.GetDevicePath(diskId)
540-
if diskPathRegexp.FindString(devicePath) == "" {
541-
t.Fatalf("GetDevicePath returned and unexpected path for Cinder volume %s, returned %s", vol, devicePath)
542-
}
543-
t.Logf("Volume (%s) found at path: %s\n", vol, devicePath)
545+
err = os.DetachDisk(id, vol)
546+
if err != nil {
547+
t.Fatalf("Cannot DetachDisk Cinder volume %s: %v", vol, err)
548+
}
549+
t.Logf("Volume (%s) detached\n", vol)
544550

545-
err = os.DetachDisk(id, vol)
546-
if err != nil {
547-
t.Fatalf("Cannot DetachDisk Cinder volume %s: %v", vol, err)
551+
WaitForVolumeStatus(t, os, vol, volumeAvailableStatus)
548552
}
549-
t.Logf("Volume (%s) detached\n", vol)
550-
551-
WaitForVolumeStatus(t, os, vol, volumeAvailableStatus)
552553

553554
err = os.DeleteVolume(vol)
554555
if err != nil {

0 commit comments

Comments
 (0)