Skip to content

Commit

Permalink
Redirect engine messages to stderr (#3468)
Browse files Browse the repository at this point in the history
* Add option to redirect stdout to stderr

* Engine tests update

* Tests update

* Lint issues update
  • Loading branch information
denis256 authored Oct 11, 2024
1 parent 7108133 commit af89a98
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
16 changes: 11 additions & 5 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ func initialize(ctx context.Context, runOptions *ExecutionOptions, client *proto

terragruntOptions.Logger.Debugf("Reading init output for engine in %s", runOptions.WorkingDir)

return ReadEngineOutput(runOptions, func() (*OutputLine, error) {
return ReadEngineOutput(runOptions, true, func() (*OutputLine, error) {
output, err := request.Recv()
if err != nil {
return nil, err
Expand Down Expand Up @@ -718,7 +718,7 @@ func shutdown(ctx context.Context, runOptions *ExecutionOptions, terragruntEngin

terragruntOptions.Logger.Debugf("Reading shutdown output for engine in %s", runOptions.WorkingDir)

return ReadEngineOutput(runOptions, func() (*OutputLine, error) {
return ReadEngineOutput(runOptions, true, func() (*OutputLine, error) {
output, err := request.Recv()
if err != nil {
return nil, err
Expand All @@ -745,7 +745,7 @@ type outputFn func() (*OutputLine, error)

// ReadEngineOutput reads the output from the engine, since grpc plugins don't have common type,
// use lambda function to read bytes from the stream
func ReadEngineOutput(runOptions *ExecutionOptions, output outputFn) error {
func ReadEngineOutput(runOptions *ExecutionOptions, forceStdErr bool, output outputFn) error {
cmdStdout := runOptions.CmdStdout
cmdStderr := runOptions.CmdStderr

Expand All @@ -756,8 +756,14 @@ func ReadEngineOutput(runOptions *ExecutionOptions, output outputFn) error {
}

if response.Stdout != "" {
if _, err := cmdStdout.Write([]byte(response.Stdout)); err != nil {
return errors.New(err)
if forceStdErr { // redirect stdout to stderr
if _, err := cmdStderr.Write([]byte(response.Stdout)); err != nil {
return errors.New(err)
}
} else {
if _, err := cmdStdout.Write([]byte(response.Stdout)); err != nil {
return errors.New(err)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ func TestReadEngineOutput(t *testing.T) {
}, nil
}

err := engine.ReadEngineOutput(runOptions, outputFn)
err := engine.ReadEngineOutput(runOptions, false, outputFn)
assert.NoError(t, err)
}
8 changes: 4 additions & 4 deletions test/integration_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestEngineRunAllOpentofu(t *testing.T) {
assert.Contains(t, stdout, "resource \"local_file\" \"test\"")
assert.Contains(t, stdout, "filename = \"./test.txt\"\n")
assert.Contains(t, stdout, "OpenTofu has been successful")
assert.Contains(t, stdout, "Tofu Shutdown completed")
assert.Contains(t, stderr, "Tofu Shutdown completed")
assert.Contains(t, stdout, "Apply complete!")
}

Expand All @@ -101,7 +101,7 @@ func TestEngineRunAllOpentofuCustomPath(t *testing.T) {
assert.Contains(t, stderr, "[INFO] plugin process exited:")
assert.Contains(t, stderr, "plugin process exited:")
assert.Contains(t, stdout, "OpenTofu has been successful")
assert.Contains(t, stdout, "Tofu Shutdown completed")
assert.Contains(t, stderr, "Tofu Shutdown completed")
assert.Contains(t, stdout, "Apply complete!")

// check if cache folder is not empty
Expand Down Expand Up @@ -208,12 +208,12 @@ func TestEngineOpentofuLatestRunAll(t *testing.T) {
tmpEnvPath := copyEnvironment(t, testFixtureOpenTofuLatestRunAll)
rootPath := util.JoinPath(tmpEnvPath, testFixtureOpenTofuLatestRunAll)

stdout, _, err := runTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt run-all apply -no-color -auto-approve --terragrunt-non-interactive --terragrunt-forward-tf-stdout --terragrunt-working-dir %s", rootPath))
stdout, stderr, err := runTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt run-all apply -no-color -auto-approve --terragrunt-non-interactive --terragrunt-forward-tf-stdout --terragrunt-working-dir %s", rootPath))
require.NoError(t, err)

assert.Contains(t, stdout, "resource \"local_file\" \"test\"")
assert.Contains(t, stdout, "filename = \"./test.txt\"\n")
assert.Contains(t, stdout, "Tofu Shutdown completed")
assert.Contains(t, stderr, "Tofu Shutdown completed")
assert.Contains(t, stdout, "Apply complete!")
}

Expand Down

0 comments on commit af89a98

Please sign in to comment.