Use global flag and only print one warning for the same cmd per binaray

run
pull/10346/head
Yanshu Zhao 2021-02-02 16:28:04 +00:00
parent 14c499b00f
commit 397b7b2286
2 changed files with 57 additions and 4 deletions

View File

@ -24,6 +24,7 @@ import (
"os/exec"
"runtime"
"strings"
"sync"
"time"
"k8s.io/klog/v2"
@ -32,6 +33,9 @@ import (
"k8s.io/minikube/pkg/minikube/style"
)
var Lock sync.Mutex
var WarningPrintedSet = make(map[string]bool)
// RunResult holds the results of a Runner
type RunResult struct {
Stdout bytes.Buffer
@ -133,10 +137,15 @@ func runCmd(cmd *exec.Cmd, warnSlow ...bool) (*RunResult, error) {
elapsed := time.Since(start)
if warn {
if elapsed > warnTime {
out.WarningT(`Executing "{{.command}}" took an unusually long time: {{.duration}}`, out.V{"command": rr.Command(), "duration": elapsed})
// Don't show any restarting hint, when running podman locally (on linux, with sudo). Only when having a service.
if cmd.Args[0] != "sudo" {
out.ErrT(style.Tip, `Restarting the {{.name}} service may improve performance.`, out.V{"name": cmd.Args[0]})
Lock.Lock()
defer Lock.Unlock()
if _, ok := WarningPrintedSet[rr.Command()]; !ok {
out.WarningT(`Executing "{{.command}}" took an unusually long time: {{.duration}}`, out.V{"command": rr.Command(), "duration": elapsed})
// Don't show any restarting hint, when running podman locally (on linux, with sudo). Only when having a service.
if cmd.Args[0] != "sudo" {
out.ErrT(style.Tip, `Restarting the {{.name}} service may improve performance.`, out.V{"name": cmd.Args[0]})
}
WarningPrintedSet[rr.Command()] = true
}
}

View File

@ -0,0 +1,44 @@
package oci
import (
"os/exec"
"runtime"
"strings"
"testing"
"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/minikube/tests"
)
func TestCliRunnerOnlyPrintOnce(t *testing.T) {
if runtime.GOOS != "linux" {
return
}
f1 := tests.NewFakeFile()
out.SetErrFile(f1)
cmd := exec.Command("sleep", "3")
_, err := runCmd(cmd, true)
if err != nil {
t.Errorf("runCmd has error: %v", err)
}
if !strings.Contains(f1.String(), "Executing \"sleep 3\" took an unusually long time") {
t.Errorf("runCmd does not print the correct log, instead print :%v", f1.String())
}
f2 := tests.NewFakeFile()
out.SetErrFile(f2)
cmd = exec.Command("sleep", "3")
_, err = runCmd(cmd, true)
if err != nil {
t.Errorf("runCmd has error: %v", err)
}
if strings.Contains(f2.String(), "Executing \"sleep 3\" took an unusually long time") {
t.Errorf("runCmd does not print the correct log, instead print :%v", f2.String())
}
}