-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
feat: -detailed-exitcode
with run-all
commands
#3585
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package shell | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
const ( | ||
DetailedExitCodeNoChanges int = iota | ||
DetailedExitCodeError | ||
DetailedExitCodeChangesPresent | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused constant 'DetailedExitCodeChangesPresent'
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed unused. |
||
) | ||
|
||
// DetailedExitCode is the TF detailed exit code. https://opentofu.org/docs/cli/commands/plan/ | ||
type DetailedExitCode struct { | ||
Code int | ||
mu sync.RWMutex | ||
} | ||
|
||
// Get returns exit code. | ||
func (coder *DetailedExitCode) Get() int { | ||
coder.mu.RLock() | ||
defer coder.mu.RUnlock() | ||
|
||
return coder.Code | ||
} | ||
|
||
// Set sets the newCode if the previous value is not 1 and the new value is greater than the previous one. | ||
func (coder *DetailedExitCode) Set(newCode int) { | ||
coder.mu.Lock() | ||
defer coder.mu.Unlock() | ||
|
||
if coder.Code == DetailedExitCodeError { | ||
return | ||
} | ||
|
||
if coder.Code < newCode || newCode == DetailedExitCodeError { | ||
coder.Code = newCode | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
resource "local_file" "example" { | ||
content = "Test" | ||
filename = "${path.module}/example.txt" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Intentionally empty |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
resource "local_file" "example" { | ||
content = "Test" | ||
filename = "${path.module}/example.txt" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Intentionally empty |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
data "local_file" "read_not_existing_file" { | ||
filename = "${path.module}/not-existing-file.txt" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Intentionally empty |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
resource "local_file" "example" { | ||
content = "Test" | ||
filename = "${path.module}/example.txt" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Intentionally empty |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package test_test | |
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
@@ -49,7 +50,6 @@ const ( | |
testFixtureEmptyState = "fixtures/empty-state/" | ||
testFixtureEnvVarsBlockPath = "fixtures/env-vars-block/" | ||
testFixtureExcludesFile = "fixtures/excludes-file" | ||
testFixtureExitCode = "fixtures/exit-code" | ||
testFixtureExternalDependence = "fixtures/external-dependencies" | ||
testFixtureExternalDependency = "fixtures/external-dependency/" | ||
testFixtureExtraArgsPath = "fixtures/extra-args/" | ||
|
@@ -98,6 +98,7 @@ const ( | |
testFixtureErrorPrint = "fixtures/error-print" | ||
testFixtureBufferModuleOutput = "fixtures/buffer-module-output" | ||
testFixtureDependenciesOptimisation = "fixtures/dependency-optimisation" | ||
testFixtureDetailedExitCode = "fixtures/detailed-exitcode" | ||
|
||
terraformFolder = ".terraform" | ||
|
||
|
@@ -107,6 +108,85 @@ const ( | |
terragruntCache = ".terragrunt-cache" | ||
) | ||
|
||
func TestDetailedExitCodeError(t *testing.T) { | ||
t.Parallel() | ||
|
||
testFixturePath := filepath.Join(testFixtureDetailedExitCode, "error") | ||
|
||
helpers.CleanupTerraformFolder(t, testFixturePath) | ||
tmpEnvPath := helpers.CopyEnvironment(t, testFixturePath) | ||
rootPath := util.JoinPath(tmpEnvPath, testFixturePath) | ||
|
||
var exitCode shell.DetailedExitCode | ||
ctx := context.Background() | ||
ctx = shell.ContextWithDetailedExitCode(ctx, &exitCode) | ||
|
||
_, stderr, err := helpers.RunTerragruntCommandWithOutputWithContext(t, ctx, "terragrunt run-all plan --terragrunt-log-level debug --terragrunt-non-interactive -detailed-exitcode --terragrunt-working-dir "+rootPath) | ||
require.Error(t, err) | ||
require.Contains(t, stderr, "not-existing-file.txt: no such file or directory") | ||
require.Equal(t, 1, exitCode.Get()) | ||
} | ||
|
||
func TestDetailedExitCodeChangesPresentAll(t *testing.T) { | ||
t.Parallel() | ||
|
||
testFixturePath := filepath.Join(testFixtureDetailedExitCode, "changes") | ||
|
||
helpers.CleanupTerraformFolder(t, testFixturePath) | ||
tmpEnvPath := helpers.CopyEnvironment(t, testFixturePath) | ||
rootPath := util.JoinPath(tmpEnvPath, testFixturePath) | ||
|
||
var exitCode shell.DetailedExitCode | ||
ctx := context.Background() | ||
ctx = shell.ContextWithDetailedExitCode(ctx, &exitCode) | ||
|
||
_, _, err := helpers.RunTerragruntCommandWithOutputWithContext(t, ctx, "terragrunt run-all plan --terragrunt-log-level debug --terragrunt-non-interactive -detailed-exitcode --terragrunt-working-dir "+rootPath) | ||
require.NoError(t, err) | ||
require.Equal(t, 2, exitCode.Get()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hm, afaik, erorrs are checked with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
} | ||
|
||
func TestDetailedExitCodeChangesPresentOne(t *testing.T) { | ||
t.Parallel() | ||
|
||
testFixturePath := filepath.Join(testFixtureDetailedExitCode, "changes") | ||
|
||
helpers.CleanupTerraformFolder(t, testFixturePath) | ||
tmpEnvPath := helpers.CopyEnvironment(t, testFixturePath) | ||
rootPath := util.JoinPath(tmpEnvPath, testFixturePath) | ||
|
||
var exitCode shell.DetailedExitCode | ||
ctx := context.Background() | ||
ctx = shell.ContextWithDetailedExitCode(ctx, &exitCode) | ||
|
||
_, _, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt run-all apply --terragrunt-log-level debug --terragrunt-non-interactive --terragrunt-working-dir "+filepath.Join(rootPath, "app1")) | ||
require.NoError(t, err) | ||
|
||
_, _, err = helpers.RunTerragruntCommandWithOutputWithContext(t, ctx, "terragrunt run-all plan --terragrunt-log-level debug --terragrunt-non-interactive -detailed-exitcode --terragrunt-working-dir "+rootPath) | ||
require.NoError(t, err) | ||
require.Equal(t, 2, exitCode.Get()) | ||
} | ||
|
||
func TestDetailedExitCodeNoChanges(t *testing.T) { | ||
t.Parallel() | ||
|
||
testFixturePath := filepath.Join(testFixtureDetailedExitCode, "changes") | ||
|
||
helpers.CleanupTerraformFolder(t, testFixturePath) | ||
tmpEnvPath := helpers.CopyEnvironment(t, testFixturePath) | ||
rootPath := util.JoinPath(tmpEnvPath, testFixturePath) | ||
|
||
var exitCode shell.DetailedExitCode | ||
ctx := context.Background() | ||
ctx = shell.ContextWithDetailedExitCode(ctx, &exitCode) | ||
|
||
_, _, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt run-all apply --terragrunt-log-level debug --terragrunt-non-interactive --terragrunt-working-dir "+rootPath) | ||
require.NoError(t, err) | ||
|
||
_, _, err = helpers.RunTerragruntCommandWithOutputWithContext(t, ctx, "terragrunt run-all plan --terragrunt-log-level debug --terragrunt-non-interactive -detailed-exitcode --terragrunt-working-dir "+rootPath) | ||
require.NoError(t, err) | ||
require.Equal(t, 0, exitCode.Get()) | ||
} | ||
|
||
func TestLogCustomFormatOutput(t *testing.T) { | ||
t.Parallel() | ||
|
||
|
@@ -795,20 +875,6 @@ func TestInvalidSource(t *testing.T) { | |
assert.True(t, ok) | ||
} | ||
|
||
// Run terragrunt plan -detailed-exitcode on a folder with some uncreated resources and make sure that you get an exit | ||
// code of "2", which means there are changes to apply. | ||
func TestExitCode(t *testing.T) { | ||
t.Parallel() | ||
|
||
rootPath := helpers.CopyEnvironment(t, testFixtureExitCode) | ||
modulePath := util.JoinPath(rootPath, testFixtureExitCode) | ||
err := helpers.RunTerragruntCommand(t, "terragrunt plan -detailed-exitcode --terragrunt-non-interactive --terragrunt-working-dir "+modulePath, os.Stdout, os.Stderr) | ||
|
||
exitCode, exitCodeErr := util.GetExitCode(err) | ||
require.NoError(t, exitCodeErr) | ||
assert.Equal(t, 2, exitCode) | ||
} | ||
|
||
func TestPlanfileOrder(t *testing.T) { | ||
t.Parallel() | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused constant 'DetailedExitCodeNoChanges'