From 5a77045a6170ee23523836019f25e73c71df5694 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Tue, 22 May 2018 11:16:08 -0700 Subject: [PATCH] core: Move invalid output context tests to "validate" This problem should now be caught at validate time rather than plan time, because we can use the schema to detect the problem before the resource has been resolved. --- terraform/context_plan_test.go | 82 ------------------------------ terraform/context_validate_test.go | 64 +++++++++++++++++++++++ 2 files changed, 64 insertions(+), 82 deletions(-) diff --git a/terraform/context_plan_test.go b/terraform/context_plan_test.go index 55fc0313ae..eac78e26a5 100644 --- a/terraform/context_plan_test.go +++ b/terraform/context_plan_test.go @@ -3701,85 +3701,3 @@ aws_instance.foo.1: t.Fatalf("bad:\n%s\n\nexpected\n\n%s", actual, expected) } } - -func TestContext2Plan_invalidOutput(t *testing.T) { - m := testModuleInline(t, map[string]string{ - "main.tf": ` -data "aws_instance" "name" {} - -output "out" { - value = "${data.aws_instance.name.missing}" -}`, - }) - - p := testProvider("aws") - ctx := testContext2(t, &ContextOpts{ - Config: m, - ProviderResolver: ResourceProviderResolverFixed( - map[string]ResourceProviderFactory{ - "aws": testProviderFuncFixed(p), - }, - ), - }) - - // if this ever fails to pass validate, add a resource to reference in the config - diags := ctx.Validate() - if diags.HasErrors() { - t.Fatalf("validate errors: %s", diags.Err()) - } - - _, diags = ctx.Refresh() - if diags.HasErrors() { - t.Fatalf("refresh errors: %s", diags.Err()) - } - - _, diags = ctx.Plan() - if !diags.HasErrors() { - t.Fatal("succeeded; want errors") - } -} - -func TestContext2Plan_invalidModuleOutput(t *testing.T) { - m := testModuleInline(t, map[string]string{ - "child/main.tf": ` -data "aws_instance" "name" {} - -output "out" { - value = "${data.aws_instance.name.missing}" -}`, - "main.tf": ` -module "child" { - source = "./child" -} - -resource "aws_instance" "foo" { - foo = "${module.child.out}" -}`, - }) - - p := testProvider("aws") - ctx := testContext2(t, &ContextOpts{ - Config: m, - ProviderResolver: ResourceProviderResolverFixed( - map[string]ResourceProviderFactory{ - "aws": testProviderFuncFixed(p), - }, - ), - }) - - // if this ever fails to pass validate, add a resource to reference in the config - diags := ctx.Validate() - if diags.HasErrors() { - t.Fatalf("validate errors: %s", diags.Err()) - } - - _, diags = ctx.Refresh() - if diags.HasErrors() { - t.Fatalf("refresh errors: %s", diags.Err()) - } - - _, diags = ctx.Plan() - if !diags.HasErrors() { - t.Fatal("succeeded; want errors") - } -} diff --git a/terraform/context_validate_test.go b/terraform/context_validate_test.go index af512c59e0..ba10766443 100644 --- a/terraform/context_validate_test.go +++ b/terraform/context_validate_test.go @@ -1234,3 +1234,67 @@ func TestContext2Validate_PlanGraphBuilder(t *testing.T) { t.Fatal(walker.NonFatalDiagnostics.Err()) } } + +func TestContext2Validate_invalidOutput(t *testing.T) { + m := testModuleInline(t, map[string]string{ + "main.tf": ` +data "aws_data_source" "name" {} + +output "out" { + value = "${data.aws_data_source.name.missing}" +}`, + }) + + p := testProvider("aws") + ctx := testContext2(t, &ContextOpts{ + Config: m, + ProviderResolver: ResourceProviderResolverFixed( + map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + ), + }) + + diags := ctx.Validate() + if !diags.HasErrors() { + // Should get this error: + // Unsupported attribute: This object does not have an attribute named "missing" + t.Fatal("succeeded; want errors") + } +} + +func TestContext2Validate_invalidModuleOutput(t *testing.T) { + m := testModuleInline(t, map[string]string{ + "child/main.tf": ` +data "aws_data_source" "name" {} + +output "out" { + value = "${data.aws_data_source.name.missing}" +}`, + "main.tf": ` +module "child" { + source = "./child" +} + +resource "aws_instance" "foo" { + foo = "${module.child.out}" +}`, + }) + + p := testProvider("aws") + ctx := testContext2(t, &ContextOpts{ + Config: m, + ProviderResolver: ResourceProviderResolverFixed( + map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + ), + }) + + diags := ctx.Validate() + if !diags.HasErrors() { + // Should get this error: + // Unsupported attribute: This object does not have an attribute named "missing" + t.Fatal("succeeded; want errors") + } +}