Skip to content
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

Fixed default path setting #90

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/gruntwork-io/pre-commit
rev: v0.1.23
rev: v0.1.25
hooks:
- id: terraform-fmt
- id: goimports
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ ENV ASDF_HASHICORP_TERRAFORM_VERSION_FILE=.terraform-version

COPY ["./src/main.sh", "/action/main.sh"]

WORKDIR /github/workspace

ENTRYPOINT ["/action/main.sh"]
2 changes: 1 addition & 1 deletion src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ function main {
local -r tg_command=${INPUT_TG_COMMAND}
local -r tg_comment=${INPUT_TG_COMMENT:-0}
local -r tg_add_approve=${INPUT_TG_ADD_APPROVE:-1}
local -r tg_dir=${INPUT_TG_DIR:-${pwd}} # an absolute path
local -r tg_dir=${INPUT_TG_DIR:-$(pwd)}

if [[ (-z "${tf_version}") && (-z "${tofu_version}")]]; then
log "One of tf_version or tofu_version must be set"
Expand Down
113 changes: 70 additions & 43 deletions test/action_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ type ActionConfig struct {
iacType string
iacVersion string
tgVersion string
tgDir string
tag string
}

func action(name, iacType, iacVersion, tgVersion, tag string) ActionConfig {

return ActionConfig{
iacName: name,
iacType: iacType,
iacVersion: iacVersion,
tgVersion: tgVersion,
tag: tag,
}
}

func TestTerragruntAction(t *testing.T) {
Expand All @@ -30,11 +43,11 @@ func TestTerragruntAction(t *testing.T) {
buildImage(t, "ssh-agent:local", "ssh-agent")

testCases := []ActionConfig{
{"Terraform1.5", "TF", "1.5.7", "0.55.18"},
{"Terraform1.7", "TF", "1.7.5", "0.55.18"},
{"Terraform1.8", "TF", "1.8.3", "0.55.18"},
{"OpenTofu1.6", "TOFU", "1.6.0", "0.55.18"},
{"OpenTofu1.7", "TOFU", "1.7.0", "0.55.18"},
action("Terraform1.5", "TF", "1.5.7", "0.55.18", tag),
action("Terraform1.7", "TF", "1.7.5", "0.55.18", tag),
action("Terraform1.8", "TF", "1.8.3", "0.55.18", tag),
action("OpenTofu1.6", "TOFU", "1.6.0", "0.55.18", tag),
action("OpenTofu1.7", "TOFU", "1.7.0", "0.55.18", tag),
}

for _, tc := range testCases {
Expand All @@ -44,120 +57,134 @@ func TestTerragruntAction(t *testing.T) {
t.Parallel()
t.Run("testActionIsExecuted", func(t *testing.T) {
t.Parallel()
testActionIsExecuted(t, tc, tag)
testActionIsExecuted(t, tc)
})
t.Run("testActionIsExecutedSSHProject", func(t *testing.T) {
t.Parallel()
testActionIsExecutedSSHProject(t, tc, tag)
testActionIsExecutedSSHProject(t, tc)
})
t.Run("testOutputPlanIsUsedInApply", func(t *testing.T) {
t.Parallel()
testOutputPlanIsUsedInApply(t, tc, tag)
testOutputPlanIsUsedInApply(t, tc)
})
t.Run("testGitWorkingAction", func(t *testing.T) {
t.Parallel()
testGitWorkingAction(t, tc, tag)
testGitWorkingAction(t, tc)
})
t.Run("testRunAllIsExecute", func(t *testing.T) {
t.Parallel()
testRunAllIsExecuted(t, tc, tag)
testRunAllIsExecuted(t, tc)
})
t.Run("testAutoApproveDelete", func(t *testing.T) {
t.Parallel()
testAutoApproveDelete(t, tc, tag)
testAutoApproveDelete(t, tc)
})

t.Run("testActionNotEmptyWorkingDir", func(t *testing.T) {
t.Parallel()
tc := tc
tc.tgDir = "/github/workspace"
testActionIsExecuted(t, tc)
})
})
}
}

func testActionIsExecuted(t *testing.T, actionConfig ActionConfig, tag string) {
func testActionIsExecuted(t *testing.T, actionConfig ActionConfig) {
fixturePath := prepareFixture(t, "fixture-action-execution")

outputTF := runAction(t, actionConfig, false, tag, fixturePath, "plan")
outputTF := runAction(t, actionConfig, false, fixturePath, "plan")
assert.Contains(t, outputTF, "You can apply this plan to save these new output values to the "+fetchIacType(actionConfig))
}

func testActionIsExecutedSSHProject(t *testing.T, actionConfig ActionConfig, tag string) {
func testActionIsExecutedSSHProject(t *testing.T, actionConfig ActionConfig) {
fixturePath := prepareFixture(t, "fixture-action-execution-ssh")

outputTF := runAction(t, actionConfig, true, tag, fixturePath, "plan")
outputTF := runAction(t, actionConfig, true, fixturePath, "plan")
assert.Contains(t, outputTF, "You can apply this plan to save these new output values to the "+fetchIacType(actionConfig))
}

func testOutputPlanIsUsedInApply(t *testing.T, actionConfig ActionConfig, tag string) {
func testOutputPlanIsUsedInApply(t *testing.T, actionConfig ActionConfig) {
fixturePath := prepareFixture(t, "fixture-dependencies-project")

output := runAction(t, actionConfig, false, tag, fixturePath, "run-all plan -out=plan.out --terragrunt-log-level debug")
output := runAction(t, actionConfig, false, fixturePath, "run-all plan -out=plan.out --terragrunt-log-level debug")
assert.Contains(t, output, "1 to add, 0 to change, 0 to destroy", actionConfig.iacName)

output = runAction(t, actionConfig, false, tag, fixturePath, "run-all apply plan.out --terragrunt-log-level debug")
output = runAction(t, actionConfig, false, fixturePath, "run-all apply plan.out --terragrunt-log-level debug")
assert.Contains(t, output, "1 added, 0 changed, 0 destroyed", actionConfig.iacName)
}

func testGitWorkingAction(t *testing.T, actionConfig ActionConfig, tag string) {
func testGitWorkingAction(t *testing.T, actionConfig ActionConfig) {
fixturePath := prepareFixture(t, "fixture-git-commands")
// init git repo in fixture path, run git init
_, err := exec.Command("git", "init", fixturePath).CombinedOutput()
if err != nil {
t.Fatalf("Error initializing git repo: %v", err)
}

output := runAction(t, actionConfig, true, tag, fixturePath, "run-all plan -out=plan.out --terragrunt-log-level debug")
output := runAction(t, actionConfig, true, fixturePath, "run-all plan -out=plan.out --terragrunt-log-level debug")
assert.Contains(t, output, fetchIacType(actionConfig)+" has been successfully initialized!", actionConfig.iacName)
assert.Contains(t, output, "execute_INPUT_POST_EXEC_1", actionConfig.iacName)
assert.Contains(t, output, "execute_INPUT_PRE_EXEC_1", actionConfig.iacName)
}

func testRunAllIsExecuted(t *testing.T, actionConfig ActionConfig, tag string) {
func testRunAllIsExecuted(t *testing.T, actionConfig ActionConfig) {
fixturePath := prepareFixture(t, "fixture-dependencies-project")

output := runAction(t, actionConfig, false, tag, fixturePath, "run-all plan")
output := runAction(t, actionConfig, false, fixturePath, "run-all plan")
assert.Contains(t, output, "1 to add, 0 to change, 0 to destroy", actionConfig.iacName)

output = runAction(t, actionConfig, false, tag, fixturePath, "run-all apply")
output = runAction(t, actionConfig, false, fixturePath, "run-all apply")
assert.Contains(t, output, "1 to add, 0 to change, 0 to destroy", actionConfig.iacName)

output = runAction(t, actionConfig, false, tag, fixturePath, "run-all destroy")
output = runAction(t, actionConfig, false, fixturePath, "run-all destroy")
assert.Contains(t, output, "0 to add, 0 to change, 1 to destroy", actionConfig.iacName)
assert.Contains(t, output, "Destroy complete! Resources: 1 destroyed", actionConfig.iacName)
}

func testAutoApproveDelete(t *testing.T, actionConfig ActionConfig, tag string) {
func testAutoApproveDelete(t *testing.T, actionConfig ActionConfig) {
fixturePath := prepareFixture(t, "fixture-dependencies-project")

output := runAction(t, actionConfig, false, tag, fixturePath, "run-all plan -out=plan.out")
output := runAction(t, actionConfig, false, fixturePath, "run-all plan -out=plan.out")
assert.Contains(t, output, "1 to add, 0 to change, 0 to destroy")

output = runAction(t, actionConfig, false, tag, fixturePath, "run-all apply plan.out")
output = runAction(t, actionConfig, false, fixturePath, "run-all apply plan.out")
assert.Contains(t, output, "1 added, 0 changed, 0 destroyed", actionConfig.iacName)

// run destroy with auto-approve
output = runAction(t, actionConfig, false, tag, fixturePath, "run-all plan -destroy -out=destroy.out")
output = runAction(t, actionConfig, false, fixturePath, "run-all plan -destroy -out=destroy.out")
assert.Contains(t, output, "0 to add, 0 to change, 1 to destroy", actionConfig.iacName)

output = runAction(t, actionConfig, false, tag, fixturePath, "run-all apply -destroy destroy.out")
output = runAction(t, actionConfig, false, fixturePath, "run-all apply -destroy destroy.out")
assert.Contains(t, output, "Resources: 0 added, 0 changed, 1 destroyed", actionConfig.iacName)

// check that fixturePath can removed recursively
// check that fixturePath can remove recursively
err := os.RemoveAll(fixturePath)
assert.NoError(t, err)
}

func runAction(t *testing.T, actionConfig ActionConfig, sshAgent bool, tag, fixturePath string, command string) string {

func runAction(t *testing.T, action ActionConfig, sshAgent bool, tgPath, command string) string {
logId := random.Random(1, 5000)
envVars := []string{
"INPUT_" + action.iacType + "_VERSION=" + action.iacVersion,
"INPUT_TG_VERSION=" + action.tgVersion,
"INPUT_TG_COMMAND=" + command,
"INPUT_PRE_EXEC_1=echo 'execute_INPUT_PRE_EXEC_1'",
"INPUT_POST_EXEC_1=echo 'execute_INPUT_POST_EXEC_1'",
fmt.Sprintf("GITHUB_OUTPUT=/tmp/github-action-logs.%d", logId),
}

if action.tgDir != "" {
envVars = append(envVars, "INPUT_TG_DIR="+action.tgDir)
} else {
envVars = append(envVars, "INPUT_TG_DIR=/github/workspace")

}

opts := &docker.RunOptions{
EnvironmentVariables: []string{
"INPUT_" + actionConfig.iacType + "_VERSION=" + actionConfig.iacVersion,
"INPUT_TG_VERSION=" + actionConfig.tgVersion,
"INPUT_TG_COMMAND=" + command,
"INPUT_TG_DIR=/github/workspace",
"INPUT_PRE_EXEC_1=echo 'execute_INPUT_PRE_EXEC_1'",
"INPUT_POST_EXEC_1=echo 'execute_INPUT_POST_EXEC_1'",
fmt.Sprintf("GITHUB_OUTPUT=/tmp/github-action-logs.%d", logId),
},
EnvironmentVariables: envVars,
Volumes: []string{
fixturePath + ":/github/workspace",
tgPath + ":/github/workspace",
},
}

Expand All @@ -184,7 +211,7 @@ func runAction(t *testing.T, actionConfig ActionConfig, sshAgent bool, tag, fixt
opts.Volumes = append(opts.Volumes, "/tmp:/tmp")
opts.EnvironmentVariables = append(opts.EnvironmentVariables, "SSH_AUTH_SOCK="+socketPath)
}
return docker.Run(t, tag, opts)
return docker.Run(t, action.tag, opts)
}

func prepareFixture(t *testing.T, fixtureDir string) string {
Expand Down
13 changes: 6 additions & 7 deletions test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ module github.com/gruntwork-io/terragrunt-action
go 1.21.1

require (
github.com/gruntwork-io/terratest v0.46.6
github.com/stretchr/testify v1.8.4
github.com/gruntwork-io/terratest v0.48.2
github.com/stretchr/testify v1.10.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/mattn/go-zglob v0.0.2-0.20190814121620-e3c945676326 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.0.3 // indirect
gotest.tools/v3 v3.5.2 // indirect
)
39 changes: 14 additions & 25 deletions test/go.sum
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gruntwork-io/terratest v0.46.6 h1:OO+BozS6nqeu0OhkLy7opkgDoYafkDr1TPlxprma26M=
github.com/gruntwork-io/terratest v0.46.6/go.mod h1:6gI5MlLeyF+SLwqocA5GBzcTix+XiuxCy1BPwKuT+WM=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gruntwork-io/terratest v0.48.2 h1:+VwfODchq8jxZZWD+s8gBlhD1z6/C4bFLNrhpm9ONrs=
github.com/gruntwork-io/terratest v0.48.2/go.mod h1:Y5ETyD4ZQ2MZhasPno272fWuCpKwvTPYDi8Y0tIMqTE=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.0 h1:B9UzwGQJehnUY1yNrnwREHc3fGbC2xefo8g4TbElacI=
github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/mattn/go-zglob v0.0.2-0.20190814121620-e3c945676326 h1:ofNAzWCcyTALn2Zv40+8XitdzCgXY6e9qvXwN9W0YXg=
github.com/mattn/go-zglob v0.0.2-0.20190814121620-e3c945676326/go.mod h1:9fxibJccNxU2cnpIKLRRFA7zX7qhkJIQWBb449FYHOo=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190624222133-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools/v3 v3.0.3 h1:4AuOwCGf4lLR9u3YOe2awrHygurzhO/HeQ6laiA6Sx0=
gotest.tools/v3 v3.0.3/go.mod h1:Z7Lb0S5l+klDB31fvDQX8ss/FlKDxtlFlw3Oa8Ymbl8=
gotest.tools/v3 v3.5.2 h1:7koQfIKdy+I8UTetycgUqXWSDwpgv193Ka+qRsmBY8Q=
gotest.tools/v3 v3.5.2/go.mod h1:LtdLGcnqToBH83WByAAi/wiwSFCArdFIUV/xxN4pcjA=