From 97b4110d9f8f154c1ec9dcc7fab345a16d247815 Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Mon, 19 Mar 2018 08:46:31 -0400 Subject: [PATCH] Stabilize openstack_test when running against real cloud in TestReadConfig, we are setting some env vars for testing if we read them back properly. However this interferes with running the unit test harness against a real openstack cloud where we source the OS_* environment variables. Adding code here to save and reset variables. --- .../providers/openstack/openstack_test.go | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/pkg/cloudprovider/providers/openstack/openstack_test.go b/pkg/cloudprovider/providers/openstack/openstack_test.go index f577a530540..40855e24a99 100644 --- a/pkg/cloudprovider/providers/openstack/openstack_test.go +++ b/pkg/cloudprovider/providers/openstack/openstack_test.go @@ -88,6 +88,11 @@ func TestReadConfig(t *testing.T) { t.Errorf("Should fail when no config is provided: %s", err) } + // Since we are setting env vars, we need to reset old + // values for other tests to succeed. + env := clearEnviron(t) + defer resetEnviron(t, env) + os.Setenv("OS_PASSWORD", "mypass") defer os.Unsetenv("OS_PASSWORD") @@ -681,3 +686,24 @@ func TestToAuth3Options(t *testing.T) { t.Errorf("DomainName %s != %s", ao.DomainName, cfg.Global.DomainName) } } + +func clearEnviron(t *testing.T) []string { + env := os.Environ() + for _, pair := range env { + if strings.HasPrefix(pair, "OS_") { + i := strings.Index(pair, "=") + 1 + os.Unsetenv(pair[:i-1]) + } + } + return env +} +func resetEnviron(t *testing.T, items []string) { + for _, pair := range items { + if strings.HasPrefix(pair, "OS_") { + i := strings.Index(pair, "=") + 1 + if err := os.Setenv(pair[:i-1], pair[i:]); err != nil { + t.Errorf("Setenv(%q, %q) failed during reset: %v", pair[:i-1], pair[i:], err) + } + } + } +}