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

feat: readd templatefile support #3041

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions cmd/infracost/breakdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1322,3 +1322,43 @@ func TestBreakdownSkipAutodetectionIfTerraformVarFilePassed(t *testing.T) {
nil,
)
}

func TestBreakdownTerragruntFileFuncs(t *testing.T) {
if os.Getenv("GITHUB_ACTIONS") == "" {
t.Skip("skipping as this test is only designed for GitHub Actions")
}

t.Setenv("INFRACOST_CI_PLATFORM", "github_app")

testName := testutil.CalcGoldenFileTestdataDirName()
dir := path.Join("./testdata", testName)
GoldenFileCommandTest(
t,
testutil.CalcGoldenFileTestdataDirName(),
[]string{
"breakdown",
"--path", dir,
},
&GoldenFileOptions{IgnoreLogs: true, IgnoreNonGraph: true},
)
}

func TestBreakdownTerraformFileFuncs(t *testing.T) {
if os.Getenv("GITHUB_ACTIONS") == "" {
t.Skip("skipping as this test is only designed for GitHub Actions")
}

t.Setenv("INFRACOST_CI_PLATFORM", "github_app")

testName := testutil.CalcGoldenFileTestdataDirName()
dir := path.Join("./testdata", testName)
GoldenFileCommandTest(
t,
testutil.CalcGoldenFileTestdataDirName(),
[]string{
"breakdown",
"--path", dir,
},
&GoldenFileOptions{IgnoreLogs: true, IgnoreNonGraph: true},
)
}
3 changes: 2 additions & 1 deletion cmd/infracost/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type GoldenFileOptions = struct {
// RunTerraformCLI sets the cmd test to also run the cmd with --terraform-force-cli set
RunTerraformCLI bool
IgnoreNonGraph bool
IgnoreLogs bool
}

func DefaultOptions() *GoldenFileOptions {
Expand Down Expand Up @@ -130,7 +131,7 @@ func GetCommandOutput(t *testing.T, args []string, testOptions *GoldenFileOption

if testOptions.CaptureLogs {
logBuf = testutil.ConfigureTestToCaptureLogs(t, c)
} else {
} else if !testOptions.IgnoreLogs {
testutil.ConfigureTestToFailOnLogs(t, c)
}

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"instance_type": "m5.large"
}
70 changes: 70 additions & 0 deletions cmd/infracost/testdata/breakdown_terraform_file_funcs/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
provider "aws" {
region = "us-east-1"
skip_credentials_validation = true
skip_requesting_account_id = true
access_key = "mock_access_key"
secret_key = "mock_secret_key"
}

locals {
files = [
{ name = "cd", file = "instance.json", },
{ name = "sym", file = "sym-instance.json", },
{ name = "pd", file = "../../../testdata/instance.json", },
{
name = "abs",
file = "/home/runner/work/infracost/infracost/cmd/infracost/testdata/breakdown_terraform_file_funcs/instance.json",
},
{ name = "pdabs", file = "/home/runner/work/infracost/infracost/cmd/testdata/instance.json", },
]
dirs = [
{ name = "cd", dir = "." },
{ name = "pd", dir = "../../../testdata" },
{ name = "abs", dir = "/home/runner/work/infracost/infracost/cmd/infracost/testdata/breakdown_terraform_file_funcs/" },
{ name = "pdabs", dir = "/home/runner/work/infracost/infracost/cmd/testdata/" },

]
template_files = [
{ name = "cd", file = "templ.tftpl", },
{ name = "pd", file = "../../../testdata/templ.tftpl", },
{
name = "abs", file = "/home/runner/work/infracost/infracost/cmd/infracost/testdata/breakdown_terraform_file_funcs/templ.tftpl",
},
{ name = "pdabs", file = "/home/runner/work/infracost/infracost/cmd/testdata/templ.tftpl", }
]
}

resource "aws_instance" "file" {
for_each = { for f in local.files : f.name => f.file }

ami = "ami-674cbc1e"
instance_type = jsondecode(file(each.value)).instance_type
}

module "mod_files" {
source = "./modules/test"
}

resource "aws_instance" "fileexists" {
for_each = { for f in local.files : f.name => f.file }
ami = "ami-674cbc1e"
instance_type = fileexists(each.value) ? "e" : "ne"
}

resource "aws_instance" "fileset" {
for_each = { for f in local.dirs : f.name => f.dir }
ami = "ami-674cbc1e"
instance_type = length(fileset(each.value, "*.json"))
}

resource "aws_instance" "filemd5" {
for_each = { for f in local.files : f.name => f.file }
ami = "ami-674cbc1e"
instance_type = filemd5(each.value)
}

resource "aws_instance" "template_file" {
for_each = { for f in local.template_files : f.name => f.file }
ami = "ami-674cbc1e"
instance_type = jsondecode(templatefile(each.value, {})).instance_type
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"instance_type": "m5.2xlarge"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
locals {
files = [
{ name = "cd", file = "${path.module}/instance.json", },
{ name = "rootcd", file = "${path.module}/../../instance.json", },
{ name = "pd", file = "${path.module}/../../../../testdata/instance.json", },
]
}

resource "aws_instance" "file" {
for_each = { for f in local.files : f.name => f.file }

ami = "ami-674cbc1e"
instance_type = jsondecode(file(each.value)).instance_type
}

resource "aws_instance" "fileexists" {
for_each = { for f in local.files : f.name => f.file }

ami = "ami-674cbc1e"
instance_type = fileexists(each.value) ? "e" : "ne"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
${jsonencode({
"instance_type": "t2.micro",
})}