|
| 1 | +package test_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/gruntwork-io/terragrunt/test/helpers" |
| 12 | + "github.com/gruntwork-io/terragrunt/util" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +func TestExecCommand(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + |
| 20 | + testCases := []struct { |
| 21 | + scriptPath string |
| 22 | + runInDir string |
| 23 | + args []string |
| 24 | + }{ |
| 25 | + { |
| 26 | + scriptPath: "./script.sh arg1 arg2", |
| 27 | + runInDir: "", |
| 28 | + }, |
| 29 | + { |
| 30 | + args: []string{"--in-download-dir"}, |
| 31 | + scriptPath: "./script.sh arg1 arg2", |
| 32 | + runInDir: ".terragrunt-cache", |
| 33 | + }, |
| 34 | + } |
| 35 | + |
| 36 | + for i, tc := range testCases { |
| 37 | + t.Run(fmt.Sprintf("testCase-%d", i), func(t *testing.T) { |
| 38 | + t.Parallel() |
| 39 | + |
| 40 | + helpers.CleanupTerraformFolder(t, testFixtureExecCmd) |
| 41 | + tmpEnvPath := helpers.CopyEnvironment(t, testFixtureExecCmd) |
| 42 | + |
| 43 | + rootPath := util.JoinPath(tmpEnvPath, testFixtureExecCmd, "app") |
| 44 | + rootPath, err := filepath.EvalSymlinks(rootPath) |
| 45 | + require.NoError(t, err) |
| 46 | + |
| 47 | + downloadDirPath := util.JoinPath(rootPath, ".terragrunt-cache") |
| 48 | + scriptPath := util.JoinPath(tmpEnvPath, testFixtureExecCmd, tc.scriptPath) |
| 49 | + |
| 50 | + err = os.Mkdir(downloadDirPath, os.ModePerm) |
| 51 | + require.NoError(t, err) |
| 52 | + |
| 53 | + stdout, _, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt exec --working-dir "+rootPath+" "+strings.Join(tc.args, " ")+" -- "+scriptPath) |
| 54 | + require.NoError(t, err) |
| 55 | + assert.Contains(t, stdout, "The first arg is arg1. The second arg is arg2. The script is running in the directory "+util.JoinPath(rootPath, tc.runInDir)) |
| 56 | + }) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestExecCommandTfPath(t *testing.T) { |
| 61 | + t.Parallel() |
| 62 | + |
| 63 | + testCases := []struct { |
| 64 | + expected string |
| 65 | + tfPath string |
| 66 | + }{ |
| 67 | + { |
| 68 | + expected: "baz is baz", |
| 69 | + }, |
| 70 | + { |
| 71 | + expected: "baz is terraform", |
| 72 | + tfPath: "terraform-output-json.sh", |
| 73 | + }, |
| 74 | + { |
| 75 | + expected: "baz is tofu", |
| 76 | + tfPath: "tofu-output-json.sh", |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + for i, tc := range testCases { |
| 81 | + t.Run(fmt.Sprintf("testCase-%d", i), func(t *testing.T) { |
| 82 | + t.Parallel() |
| 83 | + |
| 84 | + helpers.CleanupTerraformFolder(t, testFixtureExecCmdTfPath) |
| 85 | + tmpEnvPath := helpers.CopyEnvironment(t, testFixtureExecCmdTfPath) |
| 86 | + |
| 87 | + rootPath := util.JoinPath(tmpEnvPath, testFixtureExecCmdTfPath, "app") |
| 88 | + rootPath, err := filepath.EvalSymlinks(rootPath) |
| 89 | + require.NoError(t, err) |
| 90 | + |
| 91 | + downloadDirPath := util.JoinPath(rootPath, ".terragrunt-cache") |
| 92 | + scriptPath := util.JoinPath(tmpEnvPath, testFixtureExecCmdTfPath, "./script.sh") |
| 93 | + tfPath := "" |
| 94 | + if tc.tfPath != "" { |
| 95 | + tfPath = "--tf-path " + util.JoinPath(tmpEnvPath, testFixtureExecCmdTfPath, tc.tfPath) |
| 96 | + } |
| 97 | + |
| 98 | + err = os.Mkdir(downloadDirPath, os.ModePerm) |
| 99 | + require.NoError(t, err) |
| 100 | + |
| 101 | + depPath := util.JoinPath(tmpEnvPath, testFixtureExecCmdTfPath, "dep") |
| 102 | + depStdout := bytes.Buffer{} |
| 103 | + depStderr := bytes.Buffer{} |
| 104 | + require.NoError(t, helpers.RunTerragruntCommand(t, "terragrunt apply -auto-approve --non-interactive -no-color --no-color --log-format=pretty --working-dir "+depPath, &depStdout, &depStderr)) |
| 105 | + |
| 106 | + stdout, _, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt --log-level debug exec "+tfPath+" --working-dir "+rootPath+" -- "+scriptPath) |
| 107 | + require.NoError(t, err) |
| 108 | + assert.Contains(t, stdout, tc.expected) |
| 109 | + }) |
| 110 | + } |
| 111 | +} |
0 commit comments