Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log errors of runner commands only in debug mode #1870

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions pkg/types/v1/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func (r RealRunner) Run(command string, args ...string) ([]byte, error) {
cmd := r.InitCmd(command, args...)
out, err := r.RunCmd(cmd)
if err != nil {
r.error(fmt.Sprintf("Error running command: %s", err.Error()))
r.debug(fmt.Sprintf("'%s' command reported an error: %s", command, err.Error()))
r.debug(fmt.Sprintf("'%s' command output: %s", command, out))
}
return out, err
}
Expand All @@ -66,12 +67,6 @@ func (r *RealRunner) SetLogger(logger Logger) {
r.Logger = logger
}

func (r RealRunner) error(msg string) {
if r.Logger != nil {
r.Logger.Error(msg)
}
}

func (r RealRunner) debug(msg string) {
if r.Logger != nil {
r.Logger.Debug(msg)
Expand Down
6 changes: 3 additions & 3 deletions pkg/types/v1/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/sirupsen/logrus"

v1mock "github.com/rancher/elemental-toolkit/pkg/mocks"
v1 "github.com/rancher/elemental-toolkit/pkg/types/v1"
Expand Down Expand Up @@ -56,15 +55,16 @@ var _ = Describe("Runner", Label("types", "runner"), func() {
It("logs the command when on debug", func() {
memLog := &bytes.Buffer{}
logger := v1.NewBufferLogger(memLog)
logger.SetLevel(logrus.DebugLevel)
logger.SetLevel(v1.DebugLevel())
r := v1.RealRunner{Logger: logger}
_, err := r.Run("echo", "-n", "Some message")
Expect(err).To(BeNil())
Expect(memLog.String()).To(ContainSubstring("echo -n Some message"))
})
It("logs when command is not found", func() {
It("logs when command is not found in debug mode", func() {
memLog := &bytes.Buffer{}
logger := v1.NewBufferLogger(memLog)
logger.SetLevel(v1.DebugLevel())
r := v1.RealRunner{Logger: logger}
_, err := r.Run("IAmMissing")
Expect(err).NotTo(BeNil())
Expand Down