未验证 提交 56d11a90 编写于 作者: M Medya Gh

refactor

上级 7ab632a0
......@@ -70,11 +70,7 @@ func validateMountCmd(ctx context.Context, t *testing.T, profile string) { //nol
args := []string{"mount", "-p", profile, fmt.Sprintf("%s:%s", tempDir, guestMount), "--alsologtostderr", "-v=1"}
var ss *StartSession
if runtime.GOOS == "windows" {
ss, err = Start(t, exec.CommandContext(mctx, Target(), args...), true)
} else {
ss, err = Start(t, exec.CommandContext(mctx, Target(), args...))
}
ss, err = Start(t, exec.CommandContext(mctx, Target(), args...))
if err != nil {
t.Fatalf("%v failed: %v", args, err)
}
......@@ -84,11 +80,7 @@ func validateMountCmd(ctx context.Context, t *testing.T, profile string) { //nol
var err error
if t.Failed() {
t.Logf("%q failed, getting debug info...", t.Name())
if runtime.GOOS == "windows" {
rr, err = Run(t, exec.Command(Target(), "-p", profile, "ssh", "mount | grep 9p; ls -la /mount-9p; cat /mount-9p/pod-dates"), true)
} else {
rr, err = Run(t, exec.Command(Target(), "-p", profile, "ssh", "mount | grep 9p; ls -la /mount-9p; cat /mount-9p/pod-dates"))
}
rr, err = Run(t, exec.Command(Target(), "-p", profile, "ssh", "mount | grep 9p; ls -la /mount-9p; cat /mount-9p/pod-dates"))
if err != nil {
t.Logf("debugging command %q failed : %v", rr.Command(), err)
} else {
......@@ -97,11 +89,7 @@ func validateMountCmd(ctx context.Context, t *testing.T, profile string) { //nol
}
// Cleanup in advance of future tests
if runtime.GOOS == "windows" {
rr, err = Run(t, exec.Command(Target(), "-p", profile, "ssh", "sudo umount -f /mount-9p"), true)
} else {
rr, err = Run(t, exec.Command(Target(), "-p", profile, "ssh", "sudo umount -f /mount-9p"))
}
rr, err = Run(t, exec.Command(Target(), "-p", profile, "ssh", "sudo umount -f /mount-9p"))
if err != nil {
t.Logf("%q: %v", rr.Command(), err)
}
......
......@@ -161,7 +161,7 @@ func validateDockerEnv(ctx context.Context, t *testing.T, profile string) {
var err error
if runtime.GOOS == "windows" { // golang exec powershell needs some tricks !
c := exec.CommandContext(mctx, Target(), "-p "+profile+" docker-env | Invoke-Expression ;"+Target()+" status -p "+profile)
rr, err = Run(t, c, true) // golang exec powershell needs some tricks !
rr, err = Run(t, c) // golang exec powershell needs some tricks !
} else {
c := exec.CommandContext(mctx, "/bin/bash", "-c", "eval $("+Target()+" -p "+profile+" docker-env) && "+Target()+" status -p "+profile)
// we should be able to get minikube status with a bash which evaled docker-env
......@@ -182,7 +182,7 @@ func validateDockerEnv(ctx context.Context, t *testing.T, profile string) {
// do a eval $(minikube -p profile docker-env) and check if we are point to docker inside minikube
if runtime.GOOS == "windows" { // testing docker-env eval in powershell
c := exec.CommandContext(mctx, Target(), "-p "+profile+" docker-env | Invoke-Expression ; docker images")
rr, err = Run(t, c, true) // golang exec powershell needs some tricks !
rr, err = Run(t, c) // golang exec powershell needs some tricks !
} else {
c := exec.CommandContext(mctx, "/bin/bash", "-c", "eval $("+Target()+" -p "+profile+" docker-env) && docker images")
rr, err = Run(t, c)
......@@ -520,12 +520,7 @@ func validateCacheCmd(ctx context.Context, t *testing.T, profile string) {
var rr *RunResult
var err error
if runtime.GOOS == "windows" {
rr, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "ssh", "sudo crictl images"), true)
} else {
rr, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "ssh", "sudo", "crictl", "images"))
}
rr, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "ssh", "sudo", "crictl", "images"))
if err != nil {
t.Errorf("failed to get images by %q ssh %v", rr.Command(), err)
}
......@@ -544,11 +539,7 @@ func validateCacheCmd(ctx context.Context, t *testing.T, profile string) {
// deleting image inside minikube node manually
var rr *RunResult
var err error
if runtime.GOOS == "windows" {
rr, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "ssh", "sudo docker rmi "+img), true) // for some reason crictl rmi doesn't work
} else {
rr, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "ssh", "sudo", "docker", "rmi", img))
}
rr, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "ssh", "sudo", "docker", "rmi", img))
if err != nil {
t.Errorf("failed to delete inside the node %q : %v", rr.Command(), err)
......@@ -864,7 +855,7 @@ func validateSSHCmd(ctx context.Context, t *testing.T, profile string) {
if runtime.GOOS == "windows" { // golang exec powershell needs some tricks !
cmd := exec.CommandContext(ctx, Target(), "-p", profile, "ssh", "\"cat /etc/hostname\"")
t.Logf("about to run %s: ", cmd.Args)
rr, err = Run(t, cmd, true)
rr, err = Run(t, cmd)
t.Logf("rr is %+v: \n", rr)
t.Logf("err is %v: \n", err)
} else {
......
......@@ -29,11 +29,13 @@ import (
"fmt"
"io/ioutil"
"os/exec"
"runtime"
"strings"
"testing"
"time"
"github.com/docker/machine/libmachine/state"
"github.com/pkg/errors"
"github.com/shirou/gopsutil/process"
core "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
......@@ -86,25 +88,22 @@ func (rr RunResult) Output() string {
}
// Run is a test helper to log a command being executed \_(ツ)_/¯
func Run(t *testing.T, cmd *exec.Cmd, powershell ...bool) (*RunResult, error) {
func Run(t *testing.T, cmd *exec.Cmd) (*RunResult, error) {
t.Helper()
isPowershell := false
if len(powershell) > 0 {
isPowershell = powershell[0]
}
newCmd := cmd
if isPowershell {
var newCmd *exec.Cmd
if runtime.GOOS == "windows" {
psBin, err := exec.LookPath("powershell.exe")
if err != nil {
return &RunResult{}, err
return &RunResult{}, errors.Wrapf(err, "lookup powershell")
}
args := append([]string{"-NoProfile", "-NonInteractive"}, cmd.Args...)
newCmd = exec.Command(psBin, args...)
} else {
newCmd = cmd
}
rr := &RunResult{Args: newCmd.Args}
rr := &RunResult{Args: cmd.Args}
t.Logf("(dbg) Run: %v", rr.Command())
......@@ -136,30 +135,27 @@ type StartSession struct {
}
// Start starts a process in the background, streaming output
func Start(t *testing.T, cmd *exec.Cmd, powershell ...bool) (*StartSession, error) {
func Start(t *testing.T, cmd *exec.Cmd) (*StartSession, error) {
t.Helper()
t.Logf("(dbg) daemon: %v", cmd.Args)
isPowershell := false
if len(powershell) > 0 {
isPowershell = powershell[0]
}
newCmd := cmd
if isPowershell {
var newCmd *exec.Cmd
if runtime.GOOS == "windows" {
psBin, err := exec.LookPath("powershell.exe")
if err != nil {
return nil, err
return &StartSession{}, errors.Wrapf(err, "lookup powershell")
}
args := append([]string{"-NoProfile", "-NonInteractive"}, cmd.Args...)
newCmd = exec.Command(psBin, args...)
} else {
newCmd = cmd
}
stdoutPipe, err := newCmd.StdoutPipe()
if err != nil {
t.Fatalf("stdout pipe failed: %v %v", newCmd.Args, err)
}
stderrPipe, err := cmd.StderrPipe()
stderrPipe, err := newCmd.StderrPipe()
if err != nil {
t.Fatalf("stderr pipe failed: %v %v", newCmd.Args, err)
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册