Skip to content

Commit

Permalink
Log the processed command. (#105)
Browse files Browse the repository at this point in the history
Co-authored-by: Andreas Neumann <[email protected]>
Co-authored-by: Ken Sipe <[email protected]>
Signed-off-by: Marcin Owsiany <[email protected]>
  • Loading branch information
3 people authored May 20, 2020
1 parent ea5f31d commit f85e19e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ linters:
- scopelint
- unconvert
- unparam
- interfacer
- nakedret
- gocyclo
- dupl
Expand Down
6 changes: 4 additions & 2 deletions pkg/test/utils/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ func GetArgs(ctx context.Context, cmd harness.Command, namespace string, env map
// RunCommand runs a command with args.
// args gets split on spaces (respecting quoted strings).
// if the command is run in the background a reference to the process is returned for later cleanup
func RunCommand(ctx context.Context, namespace string, cmd harness.Command, cwd string, stdout io.Writer, stderr io.Writer, timeout int) (*exec.Cmd, error) {
func RunCommand(ctx context.Context, namespace string, cmd harness.Command, cwd string, stdout io.Writer, stderr io.Writer, logger Logger, timeout int) (*exec.Cmd, error) {
actualDir, err := os.Getwd()
if err != nil {
return nil, err
Expand Down Expand Up @@ -1008,6 +1008,8 @@ func RunCommand(ctx context.Context, namespace string, cmd harness.Command, cwd
return nil, err
}

logger.Logf("running command: %v", builtCmd.Args)

builtCmd.Dir = cwd
builtCmd.Stdout = stdout
builtCmd.Stderr = stderr
Expand Down Expand Up @@ -1054,7 +1056,7 @@ func RunCommands(logger Logger, namespace string, commands []harness.Command, wo
for _, cmd := range commands {
logger.Logf("running command: %q", cmd.Command)

bg, err := RunCommand(context.Background(), namespace, cmd, workdir, logger, logger, timeout)
bg, err := RunCommand(context.Background(), namespace, cmd, workdir, logger, logger, logger, timeout)
if err != nil {
errs = append(errs, err)
}
Expand Down
16 changes: 9 additions & 7 deletions pkg/test/utils/kubernetes_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ func TestRunCommand(t *testing.T) {
Command: "echo 'hello'",
}

logger := NewTestLogger(t, "")
// assert foreground cmd returns nil
cmd, err := RunCommand(context.TODO(), "", hcmd, "", stdout, stderr, 0)
cmd, err := RunCommand(context.TODO(), "", hcmd, "", stdout, stderr, logger, 0)
assert.NoError(t, err)
assert.Nil(t, cmd)
// foreground processes should have stdout
Expand All @@ -130,7 +131,7 @@ func TestRunCommand(t *testing.T) {
stdout = &bytes.Buffer{}

// assert background cmd returns process
cmd, err = RunCommand(context.TODO(), "", hcmd, "", stdout, stderr, 0)
cmd, err = RunCommand(context.TODO(), "", hcmd, "", stdout, stderr, logger, 0)
assert.NoError(t, err)
assert.NotNil(t, cmd)
// no stdout for background processes
Expand All @@ -141,7 +142,7 @@ func TestRunCommand(t *testing.T) {
hcmd.Command = "sleep 42"

// assert foreground cmd times out
cmd, err = RunCommand(context.TODO(), "", hcmd, "", stdout, stderr, 2)
cmd, err = RunCommand(context.TODO(), "", hcmd, "", stdout, stderr, logger, 2)
assert.Error(t, err)
assert.True(t, strings.Contains(err.Error(), "timeout"))
assert.Nil(t, cmd)
Expand All @@ -152,7 +153,7 @@ func TestRunCommand(t *testing.T) {
hcmd.Timeout = 2

// assert foreground cmd times out with command timeout
cmd, err = RunCommand(context.TODO(), "", hcmd, "", stdout, stderr, 0)
cmd, err = RunCommand(context.TODO(), "", hcmd, "", stdout, stderr, logger, 0)
assert.Error(t, err)
assert.True(t, strings.Contains(err.Error(), "timeout"))
assert.Nil(t, cmd)
Expand All @@ -167,13 +168,14 @@ func TestRunCommandIgnoreErrors(t *testing.T) {
IgnoreFailure: true,
}

logger := NewTestLogger(t, "")
// assert foreground cmd returns nil
cmd, err := RunCommand(context.TODO(), "", hcmd, "", stdout, stderr, 0)
cmd, err := RunCommand(context.TODO(), "", hcmd, "", stdout, stderr, logger, 0)
assert.NoError(t, err)
assert.Nil(t, cmd)

hcmd.IgnoreFailure = false
cmd, err = RunCommand(context.TODO(), "", hcmd, "", stdout, stderr, 0)
cmd, err = RunCommand(context.TODO(), "", hcmd, "", stdout, stderr, logger, 0)
assert.Error(t, err)
assert.Nil(t, cmd)

Expand All @@ -182,7 +184,7 @@ func TestRunCommandIgnoreErrors(t *testing.T) {
Command: "bad-command",
IgnoreFailure: true,
}
cmd, err = RunCommand(context.TODO(), "", hcmd, "", stdout, stderr, 0)
cmd, err = RunCommand(context.TODO(), "", hcmd, "", stdout, stderr, logger, 0)
assert.Error(t, err)
assert.Nil(t, cmd)
}

0 comments on commit f85e19e

Please sign in to comment.