fixing fake_runner for interface

pull/5530/head
Medya Gh 2019-10-04 12:51:18 -07:00
parent 27321d5f33
commit 36de216aa6
2 changed files with 26 additions and 12 deletions

View File

@ -220,19 +220,22 @@ func (d *Driver) RunSSHCommandFromDriver() error {
} }
// stopKubelet idempotently stops the kubelet // stopKubelet idempotently stops the kubelet
func stopKubelet(exec command.Runner) error { func stopKubelet(cr command.Runner) error {
glog.Infof("stopping kubelet.service ...") glog.Infof("stopping kubelet.service ...")
stop := func() error { stop := func() error {
cmdStop := "sudo systemctl stop kubelet.service" cmdStop := command.ExecCmd("sudo systemctl stop kubelet.service")
cmdCheck := "sudo systemctl show -p SubState kubelet" rr, err := cr.RunCmd(cmdStop)
err := exec.Run(cmdStop)
if err != nil { if err != nil {
glog.Errorf("temporary error for %q : %v", cmdStop, err) glog.Errorf("temporary error for %q : %v", rr.Command(), err)
} }
var out bytes.Buffer var out bytes.Buffer
errStatus := exec.CombinedOutputTo(cmdCheck, &out) cmdCheck := command.ExecCmd("sudo systemctl show -p SubState kubelet")
if errStatus != nil { cmdCheck.Stdout = &out
glog.Errorf("temporary error: for %q : %v", cmdCheck, errStatus) cmdCheck.Stderr = &out
rr, err = cr.RunCmd(cmdCheck)
if err != nil {
glog.Errorf("temporary error: for %q : %v output: %q", cmdCheck, err, rr.Output())
} }
if !strings.Contains(out.String(), "dead") && !strings.Contains(out.String(), "failed") { if !strings.Contains(out.String(), "dead") && !strings.Contains(out.String(), "failed") {
return fmt.Errorf("unexpected kubelet state: %q", out) return fmt.Errorf("unexpected kubelet state: %q", out)

View File

@ -54,18 +54,29 @@ func (f *FakeCommandRunner) RunCmd(cmd *exec.Cmd) (*RunResult, error) {
start := time.Now() start := time.Now()
out, ok := f.cmdMap.Load(cmd.Args) out, ok := f.cmdMap.Load(strings.Join(cmd.Args, " "))
rr.Stderr = bytes.NewBuffer([]byte(out.(string))) // converting fake output string to a buffer buf := new(bytes.Buffer) // creating a buffer reader to convert out to rr.stdout
outStr := ""
if out != nil {
outStr = out.(string)
}
_, err := buf.WriteString(outStr)
if err != nil {
return rr, errors.Wrap(err, "Writing outStr to FakeCommandRunner's buffer")
}
rr.Stdout = buf
rr.Stderr = buf
elapsed := time.Since(start) elapsed := time.Since(start)
if ok { if ok {
// Reduce log spam // Reduce log spam
if elapsed > (1 * time.Second) { if elapsed > (1 * time.Second) {
glog.Infof("(FakeCommandRunner) Done: %v: (%s)", rr.Command(), elapsed) glog.Infof("(FakeCommandRunner) Done: %v: (%s)", rr.Command(), elapsed)
} }
} else { } else {
glog.Infof("(FakeCommandRunner) Non-zero exit: %v: (%s)\n%s", rr.Command(), elapsed, rr.Output()) glog.Infof("(FakeCommandRunner) Non-zero exit: %v: (%s)\n%s", rr.Command(), elapsed, out)
return rr, fmt.Errorf("unavailable command: %s", strings.Join(cmd.Args, " ")) return rr, fmt.Errorf("unavailable command: %s", rr.Command())
} }
return rr, nil return rr, nil
} }