From a35930405fc65e1d3b0512eb54f40f504c2125a0 Mon Sep 17 00:00:00 2001 From: Devang Gaur Date: Fri, 15 Jan 2021 21:54:56 +0530 Subject: [PATCH] added unit tests for breaking changes case --- docs/getting-started/usage.md | 2 +- .../terraform/v12/load-dir_test.go | 6 +++ .../terraform/v12/load-file_test.go | 6 +++ .../v12/testdata/destroy-provisioners/main.tf | 44 +++++++++++++++++++ .../testdata/multiple-required-providers/a.tf | 7 +++ .../testdata/multiple-required-providers/b.tf | 7 +++ 6 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 pkg/iac-providers/terraform/v12/testdata/destroy-provisioners/main.tf create mode 100644 pkg/iac-providers/terraform/v12/testdata/multiple-required-providers/a.tf create mode 100644 pkg/iac-providers/terraform/v12/testdata/multiple-required-providers/b.tf diff --git a/docs/getting-started/usage.md b/docs/getting-started/usage.md index 7a5a0569d..032abde1e 100644 --- a/docs/getting-started/usage.md +++ b/docs/getting-started/usage.md @@ -118,7 +118,7 @@ Flags: -d, --iac-dir string path to a directory containing one or more IaC files (default ".") -f, --iac-file string path to a single IaC file -i, --iac-type string iac type (helm, k8s, kustomize, terraform) - --iac-version string iac version (helm: v3, k8s: v1, kustomize: v3, terraform: v14) + --iac-version string iac version (helm: v3, k8s: v1, kustomize: v3, terraform: v12, v14) -p, --policy-path stringArray policy path directory -t, --policy-type strings policy type (all, aws, azure, gcp, github, k8s) (default [all]) -r, --remote-type string type of remote backend (git, s3, gcs, http) diff --git a/pkg/iac-providers/terraform/v12/load-dir_test.go b/pkg/iac-providers/terraform/v12/load-dir_test.go index 8a847c2a0..a5220bcc7 100644 --- a/pkg/iac-providers/terraform/v12/load-dir_test.go +++ b/pkg/iac-providers/terraform/v12/load-dir_test.go @@ -60,6 +60,12 @@ func TestLoadIacDir(t *testing.T) { tfv12: TfV12{}, wantErr: commons.ErrLoadConfigDir, }, + { + name: "load invalid config dir", + dirPath: "./testdata/multiple-required-providers", + tfv12: TfV12{}, + wantErr: commons.ErrLoadConfigDir, + }, } for _, tt := range table { diff --git a/pkg/iac-providers/terraform/v12/load-file_test.go b/pkg/iac-providers/terraform/v12/load-file_test.go index 02b1ce3c9..18a8823f2 100644 --- a/pkg/iac-providers/terraform/v12/load-file_test.go +++ b/pkg/iac-providers/terraform/v12/load-file_test.go @@ -54,6 +54,12 @@ func TestLoadIacFile(t *testing.T) { tfv12: TfV12{}, wantErr: commons.ErrLoadConfigFile, }, + { + name: "destroy-provisioners", + filePath: "./testdata/destroy-provisioners/main.tf", + tfv12: TfV12{}, + wantErr: commons.ErrLoadConfigFile, + }, } for _, tt := range table { diff --git a/pkg/iac-providers/terraform/v12/testdata/destroy-provisioners/main.tf b/pkg/iac-providers/terraform/v12/testdata/destroy-provisioners/main.tf new file mode 100644 index 000000000..92fa066be --- /dev/null +++ b/pkg/iac-providers/terraform/v12/testdata/destroy-provisioners/main.tf @@ -0,0 +1,44 @@ +locals { + user = "name" +} + +resource "null_resource" "a" { + connection { + host = self.hostname + user = local.user # ERROR: Invalid reference from destroy provisioner + } + + provisioner "remote-exec" { + when = destroy + index = count.index + key = each.key + + // path and terraform values are static, and do not create any + // dependencies. + dir = path.module + workspace = terraform.workspace + } +} + +resource "null_resource" "b" { + connection { + host = self.hostname + # this is OK since there is no destroy provisioner + user = local.user + } + + provisioner "remote-exec" { + } +} + +resource "null_resource" "b" { + provisioner "remote-exec" { + when = destroy + connection { + host = self.hostname + user = local.user # ERROR: Invalid reference from destroy provisioner + } + + command = "echo ${local.name}" # ERROR: Invalid reference from destroy provisioner + } +} \ No newline at end of file diff --git a/pkg/iac-providers/terraform/v12/testdata/multiple-required-providers/a.tf b/pkg/iac-providers/terraform/v12/testdata/multiple-required-providers/a.tf new file mode 100644 index 000000000..30d836188 --- /dev/null +++ b/pkg/iac-providers/terraform/v12/testdata/multiple-required-providers/a.tf @@ -0,0 +1,7 @@ +terraform { + required_providers { + bar = { + version = "~>1.0.0" + } + } +} \ No newline at end of file diff --git a/pkg/iac-providers/terraform/v12/testdata/multiple-required-providers/b.tf b/pkg/iac-providers/terraform/v12/testdata/multiple-required-providers/b.tf new file mode 100644 index 000000000..e6aa01c14 --- /dev/null +++ b/pkg/iac-providers/terraform/v12/testdata/multiple-required-providers/b.tf @@ -0,0 +1,7 @@ +terraform { + required_providers { + foo = { + version = "~>2.0.0" + } + } +} \ No newline at end of file