From ea868026b85ddeae36b4254c2d1cbcba42fe77a0 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Thu, 15 Feb 2018 09:23:07 -0800 Subject: [PATCH] configs/configload: installer tests inspect their result Previously we were just loading the module and asserting no diagnostics, but that is not really good enough since if we install modules incorrectly it's possible that we are still able to load an empty configuration successfully. Now we'll do some basic inspecetion of the module tree that results from loading what we installed, to ensure that all of the expected modules are present at the right locations in the tree. --- configs/configload/loader_install_test.go | 69 ++++++++++++++++++- .../test-fixtures/go-getter-modules/root.tf | 5 ++ .../local-modules/child_a/child_a.tf | 5 ++ .../local-modules/child_a/child_b/child_b.tf | 5 ++ .../test-fixtures/local-modules/root.tf | 5 ++ .../test-fixtures/registry-modules/root.tf | 6 ++ 6 files changed, 92 insertions(+), 3 deletions(-) diff --git a/configs/configload/loader_install_test.go b/configs/configload/loader_install_test.go index 2be85a6baa..e0e6bdf98d 100644 --- a/configs/configload/loader_install_test.go +++ b/configs/configload/loader_install_test.go @@ -3,9 +3,11 @@ package configload import ( "os" "path/filepath" + "strings" "testing" version "github.com/hashicorp/go-version" + "github.com/hashicorp/terraform/configs" ) func TestLoaderInstallModules_local(t *testing.T) { @@ -39,8 +41,25 @@ func TestLoaderInstallModules_local(t *testing.T) { // Make sure the configuration is loadable now. // (This ensures that correct information is recorded in the manifest.) - _, loadDiags := loader.LoadConfig(".") + config, loadDiags := loader.LoadConfig(".") assertNoDiagnostics(t, loadDiags) + + wantTraces := map[string]string{ + "": "in root module", + "child_a": "in child_a module", + "child_a.child_b": "in child_b module", + } + gotTraces := map[string]string{} + config.DeepEach(func(c *configs.Config) { + path := strings.Join(c.Path, ".") + if c.Module.Variables["v"] == nil { + gotTraces[path] = "" + return + } + varDesc := c.Module.Variables["v"].Description + gotTraces[path] = varDesc + }) + assertResultDeepEqual(t, gotTraces, wantTraces) } func TestLoaderInstallModules_registry(t *testing.T) { @@ -136,8 +155,30 @@ func TestLoaderInstallModules_registry(t *testing.T) { // Make sure the configuration is loadable now. // (This ensures that correct information is recorded in the manifest.) - _, loadDiags := loader.LoadConfig(".") + config, loadDiags := loader.LoadConfig(".") assertNoDiagnostics(t, loadDiags) + + wantTraces := map[string]string{ + "": "in local caller for registry-modules", + "acctest_root": "in root module", + "acctest_root.child_a": "in child_a module", + "acctest_root.child_a.child_b": "in child_b module", + "acctest_child_a": "in child_a module", + "acctest_child_a.child_b": "in child_b module", + "acctest_child_b": "in child_b module", + } + gotTraces := map[string]string{} + config.DeepEach(func(c *configs.Config) { + path := strings.Join(c.Path, ".") + if c.Module.Variables["v"] == nil { + gotTraces[path] = "" + return + } + varDesc := c.Module.Variables["v"].Description + gotTraces[path] = varDesc + }) + assertResultDeepEqual(t, gotTraces, wantTraces) + } func TestLoaderInstallModules_goGetter(t *testing.T) { @@ -225,8 +266,30 @@ func TestLoaderInstallModules_goGetter(t *testing.T) { // Make sure the configuration is loadable now. // (This ensures that correct information is recorded in the manifest.) - _, loadDiags := loader.LoadConfig(".") + config, loadDiags := loader.LoadConfig(".") assertNoDiagnostics(t, loadDiags) + + wantTraces := map[string]string{ + "": "in local caller for go-getter-modules", + "acctest_root": "in root module", + "acctest_root.child_a": "in child_a module", + "acctest_root.child_a.child_b": "in child_b module", + "acctest_child_a": "in child_a module", + "acctest_child_a.child_b": "in child_b module", + "acctest_child_b": "in child_b module", + } + gotTraces := map[string]string{} + config.DeepEach(func(c *configs.Config) { + path := strings.Join(c.Path, ".") + if c.Module.Variables["v"] == nil { + gotTraces[path] = "" + return + } + varDesc := c.Module.Variables["v"].Description + gotTraces[path] = varDesc + }) + assertResultDeepEqual(t, gotTraces, wantTraces) + } type testInstallHooks struct { diff --git a/configs/configload/test-fixtures/go-getter-modules/root.tf b/configs/configload/test-fixtures/go-getter-modules/root.tf index 3c92ef1db9..9b174a7a50 100644 --- a/configs/configload/test-fixtures/go-getter-modules/root.tf +++ b/configs/configload/test-fixtures/go-getter-modules/root.tf @@ -3,6 +3,11 @@ # ...and expects its v0.0.1 tag to be pointing at the following commit: # d676ab2559d4e0621d59e3c3c4cbb33958ac4608 +variable "v" { + description = "in local caller for go-getter-modules" + default = "" +} + module "acctest_root" { source = "github.com/hashicorp/terraform-aws-module-installer-acctest?ref=v0.0.1" } diff --git a/configs/configload/test-fixtures/local-modules/child_a/child_a.tf b/configs/configload/test-fixtures/local-modules/child_a/child_a.tf index eb2c0044f2..68ebb8e404 100644 --- a/configs/configload/test-fixtures/local-modules/child_a/child_a.tf +++ b/configs/configload/test-fixtures/local-modules/child_a/child_a.tf @@ -1,4 +1,9 @@ +variable "v" { + description = "in child_a module" + default = "" +} + module "child_b" { source = "./child_b" } diff --git a/configs/configload/test-fixtures/local-modules/child_a/child_b/child_b.tf b/configs/configload/test-fixtures/local-modules/child_a/child_b/child_b.tf index 4900e9796c..e2e2209164 100644 --- a/configs/configload/test-fixtures/local-modules/child_a/child_b/child_b.tf +++ b/configs/configload/test-fixtures/local-modules/child_a/child_b/child_b.tf @@ -1,4 +1,9 @@ +variable "v" { + description = "in child_b module" + default = "" +} + output "hello" { value = "Hello from child_b!" } diff --git a/configs/configload/test-fixtures/local-modules/root.tf b/configs/configload/test-fixtures/local-modules/root.tf index 1a6721b5df..3b4c6416d7 100644 --- a/configs/configload/test-fixtures/local-modules/root.tf +++ b/configs/configload/test-fixtures/local-modules/root.tf @@ -1,4 +1,9 @@ +variable "v" { + description = "in root module" + default = "" +} + module "child_a" { source = "./child_a" } diff --git a/configs/configload/test-fixtures/registry-modules/root.tf b/configs/configload/test-fixtures/registry-modules/root.tf index 786966494c..4b5ad1f1ed 100644 --- a/configs/configload/test-fixtures/registry-modules/root.tf +++ b/configs/configload/test-fixtures/registry-modules/root.tf @@ -11,6 +11,12 @@ # 853d03855b3290a3ca491d4c3a7684572dd42237 # (this particular assumption is encoded in the tests that use this fixture) + +variable "v" { + description = "in local caller for registry-modules" + default = "" +} + module "acctest_root" { source = "hashicorp/module-installer-acctest/aws" version = "0.0.1"