From 0da0b24527129c4fa89f82615b3c679968627ca3 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Thu, 18 Feb 2021 11:45:35 -0500 Subject: [PATCH] provider transformer test mixing legacy stuff Test a combination of new required_providers modules and legacy implied providers along with providers within modules and inheritance. --- terraform/transform_provider_test.go | 97 ++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/terraform/transform_provider_test.go b/terraform/transform_provider_test.go index fbc9a4187..cfe5e98f1 100644 --- a/terraform/transform_provider_test.go +++ b/terraform/transform_provider_test.go @@ -434,6 +434,103 @@ func TestProviderConfigTransformer_grandparentProviders(t *testing.T) { } } +// Verify that configurations which are not recommended yet supported still work +func TestProviderConfigTransformer_nestedModuleProviders(t *testing.T) { + mod := testModuleInline(t, map[string]string{ + "main.tf": ` +terraform { + required_providers { + test = { + source = "registry.terraform.io/hashicorp/test" + } + } +} + +provider "test" { + alias = "z" + test_string = "config" +} + +module "moda" { + source = "./moda" + providers = { + test.x = test.z + } +} +`, + + "moda/main.tf": ` +terraform { + required_providers { + test = { + source = "registry.terraform.io/hashicorp/test" + configuration_aliases = [ test.x ] + } + } +} + +provider "test" { + test_string = "config" +} + +// this should connect to this module's provider +resource "test_object" "a" { +} + +resource "test_object" "x" { + provider = test.x +} + +module "modb" { + source = "./modb" +} +`, + + "moda/modb/main.tf": ` +# this should end up with the provider from the parent module +resource "test_object" "a" { +} +`, + }) + concrete := func(a *NodeAbstractProvider) dag.Vertex { return a } + + g := Graph{Path: addrs.RootModuleInstance} + + { + tf := &ConfigTransformer{Config: mod} + if err := tf.Transform(&g); err != nil { + t.Fatalf("err: %s", err) + } + } + { + tf := &AttachResourceConfigTransformer{Config: mod} + if err := tf.Transform(&g); err != nil { + t.Fatalf("err: %s", err) + } + } + + { + tf := TransformProviders([]string{"registry.terraform.io/hashicorp/test"}, concrete, mod) + if err := tf.Transform(&g); err != nil { + t.Fatalf("err: %s", err) + } + } + + expected := `module.moda.module.modb.test_object.a + module.moda.provider["registry.terraform.io/hashicorp/test"] +module.moda.provider["registry.terraform.io/hashicorp/test"] +module.moda.test_object.a + module.moda.provider["registry.terraform.io/hashicorp/test"] +module.moda.test_object.x + provider["registry.terraform.io/hashicorp/test"].z +provider["registry.terraform.io/hashicorp/test"].z` + + actual := strings.TrimSpace(g.String()) + if actual != expected { + t.Fatalf("expected:\n%s\n\ngot:\n%s", expected, actual) + } +} + const testTransformProviderBasicStr = ` aws_instance.web provider["registry.terraform.io/hashicorp/aws"]