Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
The provider template is deprecated. This provider has been used extensively in tests (both in the upstream Terragrunt and within our fork) because `data.template_file` was seen as a simple Terraform resource that didn’t produce any real artifacts. However, while the Linux versions are still available for download, the Windows and OSX/Darwin versions are no longer offered, causing test failures in local setups on Mac or Windows. This makes it challenging to add or modify tests locally.
  • Loading branch information
jocgir committed Oct 3, 2024
1 parent 497fd67 commit 1f1f5f4
Show file tree
Hide file tree
Showing 61 changed files with 88 additions and 302 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ jobs:
strategy:
matrix:
terraform:
- '1.0.2'
- '1.3.8'
- '1.4.0'
- '1.7.5'
- '1.8.5'
- '1.9.7'
steps:
- uses: actions/setup-go@v5
with:
Expand Down
8 changes: 4 additions & 4 deletions configstack/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestResolveTerraformModulesOneModuleWithIncludesNoDependencies(t *testing.T
Path: canonical(t, "../test/fixture-modules/module-b/module-b-child"),
Dependencies: []*TerraformModule{},
Config: config.TerragruntConfig{
RemoteState: state(t, "bucket", "module-b-child/terraform.tfstate"),
RemoteState: state("bucket", "module-b-child/terraform.tfstate"),
Terraform: &config.TerraformConfig{Source: "..."},
},
TerragruntOptions: mockOptions.Clone(canonical(t, "../test/fixture-modules/module-b/module-b-child/"+config.DefaultConfigName)),
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestResolveTerraformModulesMultipleModulesWithDependencies(t *testing.T) {
Path: canonical(t, "../test/fixture-modules/module-b/module-b-child"),
Dependencies: []*TerraformModule{},
Config: config.TerragruntConfig{
RemoteState: state(t, "bucket", "module-b-child/terraform.tfstate"),
RemoteState: state("bucket", "module-b-child/terraform.tfstate"),
Terraform: &config.TerraformConfig{Source: "..."},
},
TerragruntOptions: mockOptions.Clone(canonical(t, "../test/fixture-modules/module-b/module-b-child/"+config.DefaultConfigName)),
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestResolveTerraformModulesMultipleModulesWithDependenciesWithIncludes(t *t
Path: canonical(t, "../test/fixture-modules/module-b/module-b-child"),
Dependencies: []*TerraformModule{},
Config: config.TerragruntConfig{
RemoteState: state(t, "bucket", "module-b-child/terraform.tfstate"),
RemoteState: state("bucket", "module-b-child/terraform.tfstate"),
Terraform: &config.TerraformConfig{Source: "..."},
},
TerragruntOptions: mockOptions.Clone(canonical(t, "../test/fixture-modules/module-b/module-b-child/"+config.DefaultConfigName)),
Expand All @@ -162,7 +162,7 @@ func TestResolveTerraformModulesMultipleModulesWithDependenciesWithIncludes(t *t
Path: canonical(t, "../test/fixture-modules/module-e/module-e-child"),
Dependencies: []*TerraformModule{moduleA, moduleB},
Config: config.TerragruntConfig{
RemoteState: state(t, "bucket", "module-e-child/terraform.tfstate"),
RemoteState: state("bucket", "module-e-child/terraform.tfstate"),
Dependencies: &config.ModuleDependencies{Paths: []string{Abs("../test/fixture-modules/module-a"), Abs("../test/fixture-modules/module-b/module-b-child")}},
Terraform: &config.TerraformConfig{Source: "test"},
},
Expand Down
21 changes: 8 additions & 13 deletions configstack/stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ func TestFindStackInSubfolders(t *testing.T) {
t.Parallel()

filePaths := []string{
"/stage/data-stores/redis/" + config.DefaultConfigName,
"/stage/data-stores/postgres/" + config.DefaultConfigName,
"/stage/ecs-cluster/" + config.DefaultConfigName,
"/stage/kms-master-key/" + config.DefaultConfigName,
"/stage/vpc/" + config.DefaultConfigName,
"stage/data-stores/redis/" + config.DefaultConfigName,
"stage/data-stores/postgres/" + config.DefaultConfigName,
"stage/ecs-cluster/" + config.DefaultConfigName,
"stage/kms-master-key/" + config.DefaultConfigName,
"stage/vpc/" + config.DefaultConfigName,
}

tempFolder := createTempFolder(t)
tempFolder := createTempFolder()
writeDummyTerragruntConfigs(t, tempFolder, filePaths)

envFolder := filepath.ToSlash(util.JoinPath(tempFolder + "/stage"))
Expand All @@ -51,13 +51,8 @@ func TestFindStackInSubfolders(t *testing.T) {
}
}

func createTempFolder(t *testing.T) string {
tmpFolder, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("Failed to create temp directory: %s\n", err.Error())
}

return filepath.ToSlash(tmpFolder)
func createTempFolder() string {
return filepath.ToSlash(os.TempDir())
}

// Create a dummy Terragrunt config file at each of the given paths
Expand Down
4 changes: 2 additions & 2 deletions configstack/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func assertRunningModulesEqual(t *testing.T, expected *runningModule, actual *ru
}

// We can't do a simple IsError comparison for UnrecognizedDependency because that error is a struct that
// contains an array, and in Go, trying to compare arrays gives a "comparing noncomparable type
// contains an array, and in Go, trying to compare arrays gives a "comparing non comparable type
// configstack.UnrecognizedDependency" panic. Therefore, we have to compare that error more manually.
func assertErrorsEqual(t *testing.T, expected error, actual error, messageAndArgs ...interface{}) {
actual = tgerrors.Unwrap(actual)
Expand Down Expand Up @@ -165,7 +165,7 @@ func canonical(t *testing.T, path string) string {
}

// Create a State struct
func state(t *testing.T, bucket string, key string) *remote.State {
func state(bucket string, key string) *remote.State {
return &remote.State{
Backend: "s3",
Config: map[string]interface{}{
Expand Down
8 changes: 2 additions & 6 deletions test/fixture-download/hello-world-with-backend/main.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
data "template_file" "test" {
template = "hello, ${var.name}"
}

variable "name" {
description = "Specify a name"
}

output "test" {
value = data.template_file.test.rendered
output "example" {
value = "hello, ${var.name}"
}

terraform {
Expand Down
6 changes: 1 addition & 5 deletions test/fixture-download/hello-world/main.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
data "template_file" "test" {
template = "${module.hello.hello}, ${var.name}"
}

variable "name" {
description = "Specify a name"
}

output "test" {
value = data.template_file.test.rendered
value = "${module.hello.hello}, ${var.name}"
}

module "hello" {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
data "template_file" "test" {
template = "${module.hello.hello}, ${var.name}"
}

variable "name" {
description = "Specify a name"
}

output "test" {
value = data.template_file.test.rendered
value = "${module.hello.hello}, ${var.name}"
}

module "hello" {
Expand Down
8 changes: 2 additions & 6 deletions test/fixture-gotemplate/main.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
data "template_file" "@String(`This is an example`).Fields().Join(`_`)" {
template = "ok"
}

output "result" {
value = data.template_file.This_is_an_example.rendered
output "@String(`This is an output`).Fields().Join(`_`)" {
value = "ok"
}

variable "test1" {
Expand Down
6 changes: 1 addition & 5 deletions test/fixture-hooks/after-only/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
data "template_file" "example" {
template = "hello, world"
}

output "example" {
value = data.template_file.example.rendered
value = "hello, world"
}
6 changes: 1 addition & 5 deletions test/fixture-hooks/before-and-after-merge/qa/my-app/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ terraform {
backend "s3" {}
}

data "template_file" "example" {
template = "hello, world"
}

output "example" {
value = data.template_file.example.rendered
value = "hello, world"
}
6 changes: 1 addition & 5 deletions test/fixture-hooks/before-and-after/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
data "template_file" "example" {
template = "hello, world"
}

output "example" {
value = data.template_file.example.rendered
value = "hello, world"
}
6 changes: 1 addition & 5 deletions test/fixture-hooks/before-only/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
data "template_file" "example" {
template = "hello, world"
}

output "example" {
value = data.template_file.example.rendered
value = "hello, world"
}
2 changes: 1 addition & 1 deletion test/fixture-hooks/before-only/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pre_hook "pre_hook_1" {
on_commands = ["apply", "plan"]
command = "touch file.out"
}
}
6 changes: 1 addition & 5 deletions test/fixture-hooks/exitcode-1-run-on-errors/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
data "template_file" "example" {
template = "hello, world"
}

output "example" {
value = data.template_file.example.rendered
value = "hello, world"
}
6 changes: 1 addition & 5 deletions test/fixture-hooks/exitcode-1/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
data "template_file" "example" {
template = "hello, world"
}

output "example" {
value = data.template_file.example.rendered
value = "hello, world"
}
2 changes: 1 addition & 1 deletion test/fixture-hooks/exitcode-1/terragrunt.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ post_hook "post_hook_1" {
post_hook "post_hook_2" {
on_commands = ["apply", "plan"]
command = "touch test.out"
}
}
6 changes: 1 addition & 5 deletions test/fixture-hooks/exitcode-2-pre/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
data "template_file" "example" {
template = "hello, world"
}

output "example" {
value = data.template_file.example.rendered
value = "hello, world"
}
6 changes: 1 addition & 5 deletions test/fixture-hooks/exitcode-2/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
data "template_file" "example" {
template = "hello, world"
}

output "example" {
value = data.template_file.example.rendered
value = "hello, world"
}
6 changes: 1 addition & 5 deletions test/fixture-hooks/ignore-error/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
data "template_file" "example" {
template = "hello, world"
}

output "example" {
value = data.template_file.example.rendered
value = "hello, world"
}
6 changes: 1 addition & 5 deletions test/fixture-hooks/interpolations/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
data "template_file" "example" {
template = "hello, world"
}

output "example" {
value = data.template_file.example.rendered
value = "hello, world"
}
8 changes: 4 additions & 4 deletions test/fixture-import-files/bad-source/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
data "template_file" "example" {
template = var.var
output "example" {
value = var.var
}

output "example" {
value = data.template_file.example.rendered
variable "var" {
default = "123"
}
3 changes: 0 additions & 3 deletions test/fixture-import-files/bad-source/vars.tf

This file was deleted.

6 changes: 1 addition & 5 deletions test/fixture-import-files/basic/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
data "template_file" "example" {
template = var.var
}

output "example" {
value = data.template_file.example.rendered
value = var.var
}
6 changes: 1 addition & 5 deletions test/fixture-import-files/overwrite/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
data "template_file" "example" {
template = var.var
}

output "example" {
value = data.template_file.example.rendered
value = var.var
}
7 changes: 3 additions & 4 deletions test/fixture-include/qa/my-app/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ terraform {
backend "s3" {}
}

# Create an arbitrary local resource
data "template_file" "test" {
template = "Hello, I am a template."
}
output "text" {
value = "Hello, I am an output"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ terraform {
backend "s3" {}
}

# Create an arbitrary local resource
data "template_file" "test" {
template = "Hello, I am a template."
}
output "test" {
value = "Hello, I am a output"
}
13 changes: 0 additions & 13 deletions test/fixture-provider/provider.tf

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixture-provider/terragrunt.hcl

This file was deleted.

7 changes: 1 addition & 6 deletions test/fixture-stack/mgmt/bastion-host/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ terraform {
backend "s3" {}
}

# Create an arbitrary local resource
data "template_file" "text" {
template = "[I am a bastion-host template. Data from my dependencies: vpc = ${data.terraform_remote_state.vpc.outputs.text}]"
}

output "text" {
value = data.template_file.text.rendered
value = "[I am a bastion-host template. Data from my dependencies: vpc = ${data.terraform_remote_state.vpc.outputs.text}]"
}

variable "terraform_remote_state_s3_bucket" {
Expand Down
7 changes: 1 addition & 6 deletions test/fixture-stack/mgmt/vpc/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ terraform {
backend "s3" {}
}

# Create an arbitrary local resource
data "template_file" "text" {
template = "[I am a mgmt vpc template. I have no dependencies.]"
}

output "text" {
value = data.template_file.text.rendered
value = "[I am a mgmt vpc template. I have no dependencies.]"
}
7 changes: 1 addition & 6 deletions test/fixture-stack/stage/backend-app/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ terraform {
backend "s3" {}
}

# Create an arbitrary local resource
data "template_file" "text" {
template = "[I am a backend-app template. Data from my dependencies: vpc = ${data.terraform_remote_state.vpc.outputs.text}, bastion-host = ${data.terraform_remote_state.bastion_host.outputs.text}, mysql = ${data.terraform_remote_state.mysql.outputs.text}, search-app = ${data.terraform_remote_state.search_app.outputs.text}]"
}

output "text" {
value = data.template_file.text.rendered
value = "[I am a backend-app template. Data from my dependencies: vpc = ${data.terraform_remote_state.vpc.outputs.text}, bastion-host = ${data.terraform_remote_state.bastion_host.outputs.text}, mysql = ${data.terraform_remote_state.mysql.outputs.text}, search-app = ${data.terraform_remote_state.search_app.outputs.text}]"
}

variable "terraform_remote_state_s3_bucket" {
Expand Down
Loading

0 comments on commit 1f1f5f4

Please sign in to comment.