Skip to content

Commit

Permalink
fix: error_hook processing (#3608)
Browse files Browse the repository at this point in the history
* fix: error_hook output parsing

* chore: test improvements

* fis: error hook

* fix: test

* fix: test

* fix: test

* fix: test
  • Loading branch information
levkohimins authored Nov 28, 2024
1 parent 8e65d1e commit d8313f9
Show file tree
Hide file tree
Showing 27 changed files with 182 additions and 149 deletions.
6 changes: 3 additions & 3 deletions cli/commands/terraform/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ func processErrorHooks(ctx context.Context, hooks []config.ErrorHook, terragrunt
originalError := errors.Unwrap(e)

if originalError != nil {
var processExecutionError util.ProcessExecutionError
if ok := errors.As(originalError, &processExecutionError); ok {
errorMessage = processExecutionError.Error()
var processError util.ProcessExecutionError
if ok := errors.As(originalError, &processError); ok {
errorMessage = fmt.Sprintf("%s\n%s", processError.Error(), processError.Output.Stdout.String())
}
}
result = fmt.Sprintf("%s\n%s", result, errorMessage)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func checkForErrorsAndExit(logger log.Logger, exitCode int) func(error) {
os.Exit(exitCode)
} else {
logger.Error(err.Error())
logger.Debug(errors.ErrorStack(err))
logger.Trace(errors.ErrorStack(err))

// exit with the underlying error code
exitCoder, exitCodeErr := util.GetExitCode(err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// regardless of their position among the others registered commands and flags.
//
// For example, CLI command:
// `terragrunt run-all apply --terragrunt-log-level debug --auto-approve --terragrunt-non-interactive`
// `terragrunt run-all apply --terragrunt-log-level trace --auto-approve --terragrunt-non-interactive`
// The `App` will runs the registered command `run-all`, define the registered flags `--terragrunt-log-level`,
// `--terragrunt-non-interactive`, and define args `apply --auto-approve` which can be obtained from the App context,
// ctx.Args().Slice()
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/hooks/error-hooks/tf.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

(set -x && exec "${TERRAGRUNT_TFPATH:-terraform}" "$@" 2>&1)
4 changes: 2 additions & 2 deletions test/helpers/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func TestRunAllPlan(t *testing.T, args string) (string, string, string, error) {
testPath := util.JoinPath(tmpEnvPath, TestFixtureOutDir)

// run plan with output directory
stdout, stderr, err := RunTerragruntCommandWithOutput(t, fmt.Sprintf("terraform run-all plan --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-working-dir %s %s", testPath, args))
stdout, stderr, err := RunTerragruntCommandWithOutput(t, fmt.Sprintf("terraform run-all plan --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir %s %s", testPath, args))

return tmpEnvPath, stdout, stderr, err
}
Expand Down Expand Up @@ -821,7 +821,7 @@ func RunTerragruntValidateInputs(t *testing.T, moduleDir string, extraArgs []str
moduleDir = maybeNested
}

cmd := fmt.Sprintf("terragrunt validate-inputs %s --terragrunt-log-level debug --terragrunt-non-interactive --terragrunt-working-dir %s", strings.Join(extraArgs, " "), moduleDir)
cmd := fmt.Sprintf("terragrunt validate-inputs %s --terragrunt-log-level trace --terragrunt-non-interactive --terragrunt-working-dir %s", strings.Join(extraArgs, " "), moduleDir)
t.Logf("Command: %s", cmd)
_, _, err := RunTerragruntCommandWithOutput(t, cmd)

Expand Down
18 changes: 9 additions & 9 deletions test/integration_aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func TestAwsOutputFromDependency(t *testing.T) {

t.Setenv("AWS_CSM_ENABLED", "true")

err := helpers.RunTerragruntCommand(t, fmt.Sprintf("terragrunt run-all apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir %s --terragrunt-log-level debug", rootTerragruntPath), &stdout, &stderr)
err := helpers.RunTerragruntCommand(t, fmt.Sprintf("terragrunt run-all apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir %s --terragrunt-log-level trace", rootTerragruntPath), &stdout, &stderr)
require.NoError(t, err)

output := stderr.String()
Expand Down Expand Up @@ -596,7 +596,7 @@ func TestAwsProviderPatch(t *testing.T) {
mainContents = strings.ReplaceAll(mainContents, "__BRANCH_NAME__", branchName)
require.NoError(t, os.WriteFile(mainTFFile, []byte(mainContents), 0444))

_, stderr, err := helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt aws-provider-patch --terragrunt-override-attr region=\"eu-west-1\" --terragrunt-override-attr allowed_account_ids=[\"00000000000\"] --terragrunt-working-dir %s --terragrunt-log-level debug", modulePath))
_, stderr, err := helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt aws-provider-patch --terragrunt-override-attr region=\"eu-west-1\" --terragrunt-override-attr allowed_account_ids=[\"00000000000\"] --terragrunt-working-dir %s --terragrunt-log-level trace", modulePath))
require.NoError(t, err)

assert.Regexp(t, "Patching AWS provider in .+test/fixtures/aws-provider-patch/example-module/main.tf", stderr)
Expand Down Expand Up @@ -799,7 +799,7 @@ func TestAwsAssumeRoleWebIdentityFile(t *testing.T) {
stdout := bytes.Buffer{}
stderr := bytes.Buffer{}

err := helpers.RunTerragruntCommand(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-working-dir "+testPath, &stdout, &stderr)
err := helpers.RunTerragruntCommand(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir "+testPath, &stdout, &stderr)
require.NoError(t, err)

output := fmt.Sprintf("%s %s", stderr.String(), stdout.String())
Expand Down Expand Up @@ -831,7 +831,7 @@ func TestAwsAssumeRoleWebIdentityFlag(t *testing.T) {
token := os.Getenv("CIRCLE_OIDC_TOKEN_V2")
require.NotEmpty(t, token)

helpers.RunTerragrunt(t, "terragrunt apply --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-working-dir "+tmp+" --terragrunt-iam-role "+roleARN+" --terragrunt-iam-web-identity-token "+token)
helpers.RunTerragrunt(t, "terragrunt apply --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir "+tmp+" --terragrunt-iam-role "+roleARN+" --terragrunt-iam-web-identity-token "+token)
}

// Regression testing for https://github.com/gruntwork-io/terragrunt/issues/906
Expand Down Expand Up @@ -923,7 +923,7 @@ func TestAwsOutputFromRemoteState(t *testing.T) { //nolint: paralleltest
stderr bytes.Buffer
)

helpers.RunTerragruntRedirectOutput(t, "terragrunt run-all output --terragrunt-fetch-dependency-output-from-state --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-working-dir "+environmentPath, &stdout, &stderr)
helpers.RunTerragruntRedirectOutput(t, "terragrunt run-all output --terragrunt-fetch-dependency-output-from-state --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir "+environmentPath, &stdout, &stderr)
output := stdout.String()

assert.True(t, strings.Contains(output, "app1 output"))
Expand Down Expand Up @@ -1160,14 +1160,14 @@ func dependencyOutputOptimizationTest(t *testing.T, moduleName string, forceInit
defer cleanupTableForTest(t, lockTableName, helpers.TerraformRemoteStateS3Region)
helpers.CopyTerragruntConfigAndFillPlaceholders(t, rootTerragruntConfigPath, rootTerragruntConfigPath, s3BucketName, lockTableName, helpers.TerraformRemoteStateS3Region)

helpers.RunTerragrunt(t, "terragrunt apply-all --terragrunt-log-level debug --terragrunt-non-interactive --terragrunt-working-dir "+rootPath)
helpers.RunTerragrunt(t, "terragrunt apply-all --terragrunt-log-level trace --terragrunt-non-interactive --terragrunt-working-dir "+rootPath)

// We need to bust the output cache that stores the dependency outputs so that the second run pulls the outputs.
// This is only a problem during testing, where the process is shared across terragrunt runs.
config.ClearOutputCache()

// verify expected output
stdout, _, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt output -no-color -json --terragrunt-log-level debug --terragrunt-non-interactive --terragrunt-working-dir "+livePath)
stdout, _, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt output -no-color -json --terragrunt-log-level trace --terragrunt-non-interactive --terragrunt-working-dir "+livePath)
require.NoError(t, err)

outputs := map[string]helpers.TerraformOutput{}
Expand All @@ -1183,9 +1183,9 @@ func dependencyOutputOptimizationTest(t *testing.T, moduleName string, forceInit
config.ClearOutputCache()
require.NoError(t, os.Remove(filepath.Join(deepDepPath, "terraform.tfstate")))

fmt.Println("terragrunt output -no-color -json --terragrunt-log-level debug --terragrunt-non-interactive --terragrunt-working-dir " + livePath)
fmt.Println("terragrunt output -no-color -json --terragrunt-log-level trace --terragrunt-non-interactive --terragrunt-working-dir " + livePath)

reout, reerr, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt output -no-color -json --terragrunt-log-level debug --terragrunt-non-interactive --terragrunt-working-dir "+livePath)
reout, reerr, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt output -no-color -json --terragrunt-log-level trace --terragrunt-non-interactive --terragrunt-working-dir "+livePath)
require.NoError(t, err)

require.NoError(t, json.Unmarshal([]byte(reout), &outputs))
Expand Down
2 changes: 1 addition & 1 deletion test/integration_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func testRunAllPlan(t *testing.T, args string) (string, string, string, error) {
testPath := util.JoinPath(tmpEnvPath, testFixtureOutDir)

// run plan with output directory
stdout, stderr, err := helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terraform run-all plan --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-working-dir %s %s", testPath, args))
stdout, stderr, err := helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terraform run-all plan --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir %s %s", testPath, args))

return tmpEnvPath, stdout, stderr, err
}
Expand Down
14 changes: 7 additions & 7 deletions test/integration_debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestDebugGeneratedInputs(t *testing.T) {

require.NoError(
t,
helpers.RunTerragruntCommand(t, "terragrunt plan --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-debug --terragrunt-working-dir "+rootPath, &stdout, &stderr),
helpers.RunTerragruntCommand(t, "terragrunt plan --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-debug --terragrunt-working-dir "+rootPath, &stdout, &stderr),
)

debugFile := util.JoinPath(rootPath, helpers.TerragruntDebugFile)
Expand Down Expand Up @@ -188,8 +188,8 @@ func TestRenderJSONConfig(t *testing.T) {
helpers.CleanupTerraformFolder(t, fixtureRenderJSONMainModulePath)
helpers.CleanupTerraformFolder(t, fixtureRenderJSONDepModulePath)

helpers.RunTerragrunt(t, "terragrunt run-all apply -auto-approve --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-working-dir "+fixtureRenderJSON)
helpers.RunTerragrunt(t, fmt.Sprintf("terragrunt render-json --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-working-dir %s --terragrunt-json-out %s", fixtureRenderJSONMainModulePath, jsonOut))
helpers.RunTerragrunt(t, "terragrunt run-all apply -auto-approve --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir "+fixtureRenderJSON)
helpers.RunTerragrunt(t, fmt.Sprintf("terragrunt render-json --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir %s --terragrunt-json-out %s", fixtureRenderJSONMainModulePath, jsonOut))

jsonBytes, err := os.ReadFile(jsonOut)
require.NoError(t, err)
Expand Down Expand Up @@ -299,9 +299,9 @@ func TestRenderJSONConfigWithIncludesDependenciesAndLocals(t *testing.T) {
tmpEnvPath := helpers.CopyEnvironment(t, fixtureRenderJSONRegression)
workDir := filepath.Join(tmpEnvPath, fixtureRenderJSONRegression)

helpers.RunTerragrunt(t, "terragrunt run-all apply -auto-approve --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-working-dir "+workDir)
helpers.RunTerragrunt(t, "terragrunt run-all apply -auto-approve --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir "+workDir)

helpers.RunTerragrunt(t, fmt.Sprintf("terragrunt render-json --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-working-dir %s --terragrunt-json-out ", workDir)+jsonOut)
helpers.RunTerragrunt(t, fmt.Sprintf("terragrunt render-json --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir %s --terragrunt-json-out ", workDir)+jsonOut)

jsonBytes, err := os.ReadFile(jsonOut)
require.NoError(t, err)
Expand Down Expand Up @@ -402,9 +402,9 @@ func TestRenderJSONConfigRunAll(t *testing.T) {
defer os.Remove(bazJSONOut)
defer os.Remove(rootChildJSONOut)

helpers.RunTerragrunt(t, "terragrunt run-all apply -auto-approve --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-working-dir "+workDir)
helpers.RunTerragrunt(t, "terragrunt run-all apply -auto-approve --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir "+workDir)

helpers.RunTerragrunt(t, "terragrunt run-all render-json --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-working-dir "+workDir)
helpers.RunTerragrunt(t, "terragrunt run-all render-json --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir "+workDir)

bazJSONBytes, err := os.ReadFile(bazJSONOut)
require.NoError(t, err)
Expand Down
10 changes: 5 additions & 5 deletions test/integration_destroy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestDestroyDependentModule(t *testing.T) {
stderr := bytes.Buffer{}

workingDir := util.JoinPath(rootPath, "c")
err = helpers.RunTerragruntCommand(t, "terragrunt destroy -auto-approve --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-working-dir "+workingDir, &stdout, &stderr)
err = helpers.RunTerragruntCommand(t, "terragrunt destroy -auto-approve --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir "+workingDir, &stdout, &stderr)
require.NoError(t, err)

output := stderr.String()
Expand Down Expand Up @@ -328,10 +328,10 @@ func TestStorePlanFilesRunAllDestroy(t *testing.T) {
testPath := util.JoinPath(tmpEnvPath, testFixtureOutDir)

// plan and apply
_, _, err := helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt run-all plan --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-working-dir %s --terragrunt-out-dir %s", testPath, tmpDir))
_, _, err := helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt run-all plan --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir %s --terragrunt-out-dir %s", testPath, tmpDir))
require.NoError(t, err)

_, _, err = helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt run-all apply --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-working-dir %s --terragrunt-out-dir %s", testPath, tmpDir))
_, _, err = helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt run-all apply --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir %s --terragrunt-out-dir %s", testPath, tmpDir))
require.NoError(t, err)

// remove all tfstate files from temp directory to prepare destroy
Expand All @@ -343,7 +343,7 @@ func TestStorePlanFilesRunAllDestroy(t *testing.T) {
}

// prepare destroy plan
_, output, err := helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt run-all plan -destroy --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-working-dir %s --terragrunt-out-dir %s", testPath, tmpDir))
_, output, err := helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt run-all plan -destroy --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir %s --terragrunt-out-dir %s", testPath, tmpDir))
require.NoError(t, err)

assert.Contains(t, output, "Using output file "+getPathRelativeTo(t, tmpDir, testPath))
Expand All @@ -355,6 +355,6 @@ func TestStorePlanFilesRunAllDestroy(t *testing.T) {
assert.Equal(t, "tfplan.tfplan", filepath.Base(file))
}

_, _, err = helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt run-all apply --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-working-dir %s --terragrunt-out-dir %s", testPath, tmpDir))
_, _, err = helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt run-all apply --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir %s --terragrunt-out-dir %s", testPath, tmpDir))
require.NoError(t, err)
}
10 changes: 5 additions & 5 deletions test/integration_download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func TestCustomLockFile(t *testing.T) {
tmpEnvPath := helpers.CopyEnvironment(t, filepath.Dir(testFixtureCustomLockFile))
rootPath := util.JoinPath(tmpEnvPath, path)

helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-working-dir "+rootPath)
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir "+rootPath)

source := "../custom-lock-file-module"
downloadDir := util.JoinPath(rootPath, helpers.TerragruntCache)
Expand Down Expand Up @@ -289,7 +289,7 @@ func TestExcludeDirs(t *testing.T) {
}

// Apply modules according to test cases
err := helpers.RunTerragruntCommand(t, fmt.Sprintf("terragrunt apply-all --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-working-dir %s %s", testCase.workingDir, testCase.excludeArgs), &applyAllStdout, &applyAllStderr)
err := helpers.RunTerragruntCommand(t, fmt.Sprintf("terragrunt apply-all --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir %s %s", testCase.workingDir, testCase.excludeArgs), &applyAllStdout, &applyAllStderr)

helpers.LogBufferContentsLineByLine(t, applyAllStdout, "apply-all stdout")
helpers.LogBufferContentsLineByLine(t, applyAllStderr, "apply-all stderr")
Expand All @@ -303,7 +303,7 @@ func TestExcludeDirs(t *testing.T) {
showStdout := bytes.Buffer{}
showStderr := bytes.Buffer{}

err = helpers.RunTerragruntCommand(t, "terragrunt show --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-working-dir "+modulePath, &showStdout, &showStderr)
err = helpers.RunTerragruntCommand(t, "terragrunt show --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir "+modulePath, &showStdout, &showStderr)
helpers.LogBufferContentsLineByLine(t, showStdout, "show stdout for "+modulePath)
helpers.LogBufferContentsLineByLine(t, showStderr, "show stderr for "+modulePath)

Expand Down Expand Up @@ -356,7 +356,7 @@ func TestIncludeDirs(t *testing.T) {
}

// Apply modules according to test cases
err := helpers.RunTerragruntCommand(t, fmt.Sprintf("terragrunt apply-all --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-working-dir %s %s", testCase.workingDir, testCase.includeArgs), &applyAllStdout, &applyAllStderr)
err := helpers.RunTerragruntCommand(t, fmt.Sprintf("terragrunt apply-all --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir %s %s", testCase.workingDir, testCase.includeArgs), &applyAllStdout, &applyAllStderr)

helpers.LogBufferContentsLineByLine(t, applyAllStdout, "apply-all stdout")
helpers.LogBufferContentsLineByLine(t, applyAllStderr, "apply-all stderr")
Expand All @@ -370,7 +370,7 @@ func TestIncludeDirs(t *testing.T) {
showStdout := bytes.Buffer{}
showStderr := bytes.Buffer{}

err = helpers.RunTerragruntCommand(t, "terragrunt show --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-working-dir "+modulePath, &showStdout, &showStderr)
err = helpers.RunTerragruntCommand(t, "terragrunt show --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir "+modulePath, &showStdout, &showStderr)
helpers.LogBufferContentsLineByLine(t, showStdout, "show stdout for "+modulePath)
helpers.LogBufferContentsLineByLine(t, showStderr, "show stderr for "+modulePath)

Expand Down
Loading

0 comments on commit d8313f9

Please sign in to comment.