Skip to content

Commit

Permalink
Log errors of runner commands only in debug mode
Browse files Browse the repository at this point in the history
Signed-off-by: David Cassany <dcassany@suse.com>
(cherry picked from commit f98349a)
  • Loading branch information
davidcassany committed Dec 1, 2023
1 parent 9c55b9d commit cb8347b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
3 changes: 2 additions & 1 deletion 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 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

0 comments on commit cb8347b

Please sign in to comment.