diff --git a/test/integration/cluster_env_test.go b/test/integration/cluster_env_test.go index 04ba05ce09..7da0b6968e 100644 --- a/test/integration/cluster_env_test.go +++ b/test/integration/cluster_env_test.go @@ -19,6 +19,7 @@ limitations under the License. package integration import ( + "os" "os/exec" "testing" "time" @@ -26,16 +27,30 @@ import ( "k8s.io/minikube/test/integration/util" ) +// Assert that docker-env subcommand outputs usable information for "docker ps" func testClusterEnv(t *testing.T) { t.Parallel() - minikubeRunner := NewMinikubeRunner(t) + r := NewMinikubeRunner(t) - dockerEnvVars := minikubeRunner.RunCommand("docker-env", true) - if err := minikubeRunner.SetEnvFromEnvCmdOutput(dockerEnvVars); err != nil { - t.Fatalf("Error parsing output: %s", err) + // Set a specific shell syntax so that we don't have to handle every possible user shell + envOut := r.RunCommand("docker-env --shell=bash", true) + vars := r.ParseEnvCmdOutput(envOut) + if len(vars) == 0 { + t.Fatalf("Failed to parse env vars:\n%s", envOut) } + for k, v := range vars { + t.Logf("Found: %s=%s", k, v) + if err := os.Setenv(k, v); err != nil { + t.Errorf("failed to set %s=%s: %v", k, v, err) + } + } + path, err := exec.LookPath("docker") + if err != nil { + t.Fatalf("Unable to complete test: docker is not installed in PATH") + } + t.Logf("Using docker installed at %s", path) var output []byte dockerPs := func() error { diff --git a/test/integration/util/util.go b/test/integration/util/util.go index 96bdad2928..80b5849030 100644 --- a/test/integration/util/util.go +++ b/test/integration/util/util.go @@ -21,7 +21,6 @@ import ( "encoding/json" "fmt" "math/rand" - "os" "os/exec" "path/filepath" "regexp" @@ -114,19 +113,14 @@ func (m *MinikubeRunner) EnsureRunning() { m.CheckStatus("Running") } -func (m *MinikubeRunner) SetEnvFromEnvCmdOutput(dockerEnvVars string) error { +// ParseEnvCmdOutput parses the output of `env` (assumes bash) +func (m *MinikubeRunner) ParseEnvCmdOutput(out string) map[string]string { + env := map[string]string{} re := regexp.MustCompile(`(\w+?) ?= ?"?(.+?)"?\n`) - matches := re.FindAllStringSubmatch(dockerEnvVars, -1) - seenEnvVar := false - for _, m := range matches { - seenEnvVar = true - key, val := m[1], m[2] - os.Setenv(key, val) + for _, m := range re.FindAllStringSubmatch(out, -1) { + env[m[1]] = m[2] } - if !seenEnvVar { - return fmt.Errorf("Error: No environment variables were found in docker-env command output: %s", dockerEnvVars) - } - return nil + return env } func (m *MinikubeRunner) GetStatus() string {