Skip to content

Commit

Permalink
Add test to make sure all parts of config can be read in
Browse files Browse the repository at this point in the history
  • Loading branch information
yorinasub17 committed Feb 18, 2020
1 parent eb815fb commit d9e2543
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 0 deletions.
32 changes: 32 additions & 0 deletions test/fixture-read-config/full/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
variable "localstg" {}
output "localstg" { value = var.localstg }

variable "remote_state" {}
output "remote_state" { value = var.remote_state }

variable "terraformtg" {}
output "terraformtg" { value = var.terraformtg }

variable "dependencies" {}
output "dependencies" { value = var.dependencies }

variable "terraform_binary" {}
output "terraform_binary" { value = var.terraform_binary }

variable "terraform_version_constraint" {}
output "terraform_version_constraint" { value = var.terraform_version_constraint }

variable "download_dir" {}
output "download_dir" { value = var.download_dir }

variable "prevent_destroy" {}
output "prevent_destroy" { value = var.prevent_destroy }

variable "skip" {}
output "skip" { value = var.skip }

variable "iam_role" {}
output "iam_role" { value = var.iam_role }

variable "inputs" {}
output "inputs" { value = var.inputs }
50 changes: 50 additions & 0 deletions test/fixture-read-config/full/source.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
locals {
the_answer = 42
}

remote_state {
backend = "local"
config = {
path = "foo.tfstate"
}
}

terraform {
source = "./delorean"

extra_arguments "var-files" {
commands = ["apply", "plan"]
required_var_files = ["extra.tfvars"]
optional_var_files = ["optional.tfvars"]
env_vars = {
TF_VAR_custom_var = "I'm set in extra_arguments env_vars"
}
}

before_hook "before_hook_1" {
commands = ["apply", "plan"]
execute = ["touch", "before.out"]
run_on_error = true
}

after_hook "after_hook_1" {
commands = ["apply", "plan"]
execute = ["touch", "after.out"]
run_on_error = true
}
}

dependencies {
paths = ["../module-a"]
}

terraform_binary = "terragrunt"
terraform_version_constraint = "= 0.12.20"
download_dir = ".terragrunt-cache"
prevent_destroy = true
skip = true
iam_role = "TerragruntIAMRole"

inputs = {
doc = "Emmett Brown"
}
17 changes: 17 additions & 0 deletions test/fixture-read-config/full/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
locals {
config = read_terragrunt_config("${get_terragrunt_dir()}/source.hcl")
}

inputs = {
localstg = local.config.locals
remote_state = local.config.remote_state
terraformtg = local.config.terraform
dependencies = local.config.dependencies
terraform_binary = local.config.terraform_binary
terraform_version_constraint = local.config.terraform_version_constraint
download_dir = local.config.download_dir
prevent_destroy = local.config.prevent_destroy
skip = local.config.skip
iam_role = local.config.iam_role
inputs = local.config.inputs
}
96 changes: 96 additions & 0 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2278,6 +2278,102 @@ func TestReadTerragruntConfigWithDefault(t *testing.T) {
assert.Equal(t, outputs["data"].Value, "default value")
}

func TestReadTerragruntConfigFull(t *testing.T) {
t.Parallel()

cleanupTerraformFolder(t, TEST_FIXTURE_READ_CONFIG)
rootPath := util.JoinPath(TEST_FIXTURE_READ_CONFIG, "full")

runTerragrunt(t, fmt.Sprintf("terragrunt apply --terragrunt-non-interactive --terragrunt-working-dir %s", rootPath))

// check the outputs to make sure they are as expected
stdout := bytes.Buffer{}
stderr := bytes.Buffer{}

require.NoError(
t,
runTerragruntCommand(t, fmt.Sprintf("terragrunt output -no-color -json --terragrunt-non-interactive --terragrunt-working-dir %s", rootPath), &stdout, &stderr),
)

outputs := map[string]TerraformOutput{}
require.NoError(t, json.Unmarshal([]byte(stdout.String()), &outputs))

// Primitive config attributes
assert.Equal(t, outputs["terraform_binary"].Value, "terragrunt")
assert.Equal(t, outputs["terraform_version_constraint"].Value, "= 0.12.20")
assert.Equal(t, outputs["download_dir"].Value, ".terragrunt-cache")
assert.Equal(t, outputs["iam_role"].Value, "TerragruntIAMRole")
assert.Equal(t, outputs["skip"].Value, "true")
assert.Equal(t, outputs["prevent_destroy"].Value, "true")

// Simple maps
localstgOut := map[string]interface{}{}
require.NoError(t, json.Unmarshal([]byte(outputs["localstg"].Value.(string)), &localstgOut))
assert.Equal(t, localstgOut, map[string]interface{}{"the_answer": float64(42)})
inputsOut := map[string]interface{}{}
require.NoError(t, json.Unmarshal([]byte(outputs["inputs"].Value.(string)), &inputsOut))
assert.Equal(t, inputsOut, map[string]interface{}{"doc": "Emmett Brown"})

// Complex blocks
depsOut := map[string]interface{}{}
require.NoError(t, json.Unmarshal([]byte(outputs["dependencies"].Value.(string)), &depsOut))
assert.Equal(
t,
depsOut,
map[string]interface{}{
"paths": []interface{}{"../module-a"},
},
)
remoteStateOut := map[string]interface{}{}
require.NoError(t, json.Unmarshal([]byte(outputs["remote_state"].Value.(string)), &remoteStateOut))
assert.Equal(
t,
remoteStateOut,
map[string]interface{}{
"backend": "local",
"disable_init": false,
"config": map[string]interface{}{"path": "foo.tfstate"},
},
)
terraformOut := map[string]interface{}{}
require.NoError(t, json.Unmarshal([]byte(outputs["terraformtg"].Value.(string)), &terraformOut))
assert.Equal(
t,
terraformOut,
map[string]interface{}{
"source": "./delorean",
"extra_arguments": map[string]interface{}{
"var-files": map[string]interface{}{
"name": "var-files",
"commands": []interface{}{"apply", "plan"},
"arguments": nil,
"required_var_files": []interface{}{"extra.tfvars"},
"optional_var_files": []interface{}{"optional.tfvars"},
"env_vars": map[string]interface{}{
"TF_VAR_custom_var": "I'm set in extra_arguments env_vars",
},
},
},
"before_hook": map[string]interface{}{
"before_hook_1": map[string]interface{}{
"name": "before_hook_1",
"commands": []interface{}{"apply", "plan"},
"execute": []interface{}{"touch", "before.out"},
"run_on_error": true,
},
},
"after_hook": map[string]interface{}{
"after_hook_1": map[string]interface{}{
"name": "after_hook_1",
"commands": []interface{}{"apply", "plan"},
"execute": []interface{}{"touch", "after.out"},
"run_on_error": true,
},
},
},
)
}

func logBufferContentsLineByLine(t *testing.T, out bytes.Buffer, label string) {
t.Logf("[%s] Full contents of %s:", t.Name(), label)
lines := strings.Split(out.String(), "\n")
Expand Down

0 comments on commit d9e2543

Please sign in to comment.