mirror of https://github.com/k3s-io/k3s
Merge pull request #61352 from dims/stabilize-openstack-test-when-running-against-real-openstack-cloud
Automatic merge from submit-queue (batch tested with PRs 60373, 61098, 61352, 61359, 61362). 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>. Stabilize openstack_test when running against real cloud **What this PR does / why we need it**: 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. **Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*: Fixes # **Special notes for your reviewer**: **Release note**: ```release-note NONE ```pull/8/head
commit
a29b047aa7
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue