Fix broken TestFunctional/EnvVars test when run under non-bash shell.

Make future failures to this test easier to debug. Here's an example of
the test failure I ran into:

    --- FAIL: TestFunctional/EnvVars (0.58s)
        cluster_env_test.go:36: SetEnvFromEnvCmdOutput: Error: No variables were parsed from docker-env output:
            set -gx DOCKER_TLS_VERIFY "1";
            set -gx DOCKER_HOST "tcp://192.168.39.199:2376";
            set -gx DOCKER_CERT_PATH "/usr/local/google/home/tstromberg/.minikube/certs";
            set -gx DOCKER_API_VERSION "1.35";
            # Run this command to configure your shell:
            # eval (minikube docker-env)
pull/3218/head
Thomas Stromberg 2018-09-26 17:53:42 -07:00
parent c7ac28356e
commit 349ea5a06a
2 changed files with 25 additions and 16 deletions

View File

@ -19,6 +19,7 @@ limitations under the License.
package integration package integration
import ( import (
"os"
"os/exec" "os/exec"
"testing" "testing"
"time" "time"
@ -26,16 +27,30 @@ import (
"k8s.io/minikube/test/integration/util" "k8s.io/minikube/test/integration/util"
) )
// Assert that docker-env subcommand outputs usable information for "docker ps"
func testClusterEnv(t *testing.T) { func testClusterEnv(t *testing.T) {
t.Parallel() t.Parallel()
minikubeRunner := NewMinikubeRunner(t) r := NewMinikubeRunner(t)
dockerEnvVars := minikubeRunner.RunCommand("docker-env", true) // Set a specific shell syntax so that we don't have to handle every possible user shell
if err := minikubeRunner.SetEnvFromEnvCmdOutput(dockerEnvVars); err != nil { envOut := r.RunCommand("docker-env --shell=bash", true)
t.Fatalf("Error parsing output: %s", err) 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") 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 var output []byte
dockerPs := func() error { dockerPs := func() error {

View File

@ -21,7 +21,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"math/rand" "math/rand"
"os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -114,19 +113,14 @@ func (m *MinikubeRunner) EnsureRunning() {
m.CheckStatus("Running") 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`) re := regexp.MustCompile(`(\w+?) ?= ?"?(.+?)"?\n`)
matches := re.FindAllStringSubmatch(dockerEnvVars, -1) for _, m := range re.FindAllStringSubmatch(out, -1) {
seenEnvVar := false env[m[1]] = m[2]
for _, m := range matches {
seenEnvVar = true
key, val := m[1], m[2]
os.Setenv(key, val)
} }
if !seenEnvVar { return env
return fmt.Errorf("Error: No environment variables were found in docker-env command output: %s", dockerEnvVars)
}
return nil
} }
func (m *MinikubeRunner) GetStatus() string { func (m *MinikubeRunner) GetStatus() string {