From 4a21b763aad686b10aef2755e0eb2a56eac6878d Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Fri, 4 May 2018 19:24:06 -0700 Subject: [PATCH] core: Get tests compiling again After the refactoring to integrate HCL2 many of the tests were no longer using correct types, attribute names, etc. This is a bulk update of all of the tests to make them compile again, with minimal changes otherwise. Although the tests now compile, many of them do not yet pass. The tests will be gradually repaired in subsequent commits, as we continue to complete the refactoring and retrofit work. --- terraform/context_apply_test.go | 951 ++++++++------ terraform/context_import_test.go | 162 ++- terraform/context_input_test.go | 94 +- terraform/context_plan_test.go | 301 +++-- terraform/context_refresh_test.go | 78 +- terraform/context_test.go | 21 +- terraform/context_validate_test.go | 167 ++- terraform/debug_test.go | 8 +- terraform/diff_test.go | 6 +- terraform/eval_context_builtin_test.go | 34 +- terraform/eval_diff_test.go | 20 +- terraform/eval_local_test.go | 28 +- terraform/eval_output_test.go | 37 +- terraform/eval_provider_test.go | 108 +- terraform/eval_state_test.go | 8 +- terraform/eval_validate_selfref_test.go | 96 +- terraform/eval_validate_test.go | 277 ++-- terraform/eval_variable_test.go | 219 ---- terraform/evaluate_test.go | 5 +- terraform/graph_builder_apply_test.go | 194 ++- terraform/graph_builder_plan_test.go | 42 +- terraform/graph_builder_refresh_test.go | 21 +- terraform/graph_builder_test.go | 13 +- terraform/interpolate_test.go | 1133 ----------------- terraform/module_dependencies_test.go | 6 +- terraform/node_data_refresh_test.go | 40 +- terraform/node_module_variable_test.go | 88 +- terraform/node_resource_destroy_test.go | 22 +- terraform/node_resource_refresh_test.go | 52 +- terraform/plan_test.go | 57 +- terraform/state.go | 84 -- terraform/state_test.go | 199 +-- terraform/terraform_test.go | 36 +- terraform/transform_config_flat_test.go | 8 +- terraform/transform_config_test.go | 60 +- terraform/transform_destroy_cbd_test.go | 26 +- terraform/transform_destroy_edge_test.go | 26 +- terraform/transform_diff_test.go | 6 +- terraform/transform_module_variable_test.go | 14 +- terraform/transform_orphan_count_test.go | 92 +- terraform/transform_orphan_resource_test.go | 57 +- terraform/transform_provider_test.go | 132 +- terraform/transform_provisioner_test.go | 20 +- terraform/transform_reference_test.go | 17 +- terraform/transform_root_test.go | 6 +- terraform/transform_targets_test.go | 49 +- .../transform_transitive_reduction_test.go | 8 +- terraform/variables.go | 18 + terraform/variables_test.go | 166 +-- 49 files changed, 2142 insertions(+), 3170 deletions(-) delete mode 100644 terraform/eval_variable_test.go delete mode 100644 terraform/interpolate_test.go diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index 9bea9b1ab..99bd9dc66 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -14,7 +14,11 @@ import ( "time" "github.com/davecgh/go-spew/spew" - "github.com/hashicorp/terraform/config/module" + "github.com/zclconf/go-cty/cty" + + "github.com/hashicorp/terraform/addrs" + "github.com/hashicorp/terraform/tfdiags" + "github.com/hashicorp/terraform/configs" ) func TestContext2Apply_basic(t *testing.T) { @@ -23,7 +27,7 @@ func TestContext2Apply_basic(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -66,7 +70,7 @@ func TestContext2Apply_unstable(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "test": testProviderFuncFixed(p), @@ -110,7 +114,7 @@ func TestContext2Apply_escape(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -142,7 +146,7 @@ func TestContext2Apply_resourceCountOneList(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "null": testProviderFuncFixed(p), @@ -177,7 +181,7 @@ func TestContext2Apply_resourceCountZeroList(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "null": testProviderFuncFixed(p), @@ -235,7 +239,7 @@ func TestContext2Apply_resourceDependsOnModule(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -321,7 +325,7 @@ func TestContext2Apply_resourceDependsOnModuleStateOnly(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -356,7 +360,7 @@ func TestContext2Apply_resourceDependsOnModuleDestroy(t *testing.T) { { p.ApplyFn = testApplyFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -402,7 +406,7 @@ func TestContext2Apply_resourceDependsOnModuleDestroy(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -464,7 +468,7 @@ func TestContext2Apply_resourceDependsOnModuleGrandchild(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -520,7 +524,7 @@ func TestContext2Apply_resourceDependsOnModuleInModule(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -551,7 +555,7 @@ func TestContext2Apply_mapVarBetweenModules(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "null": testProviderFuncFixed(p), @@ -593,7 +597,7 @@ func TestContext2Apply_refCount(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -628,7 +632,7 @@ func TestContext2Apply_providerAlias(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -666,7 +670,7 @@ func TestContext2Apply_providerAliasConfigure(t *testing.T) { p2.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "another": testProviderFuncFixed(p2), @@ -724,7 +728,7 @@ func TestContext2Apply_providerWarning(t *testing.T) { return } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -808,7 +812,7 @@ func TestContext2Apply_computedAttrRefTypeMismatch(t *testing.T) { }, nil } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -820,14 +824,14 @@ func TestContext2Apply_computedAttrRefTypeMismatch(t *testing.T) { t.Fatalf("err: %s", err) } - _, err := ctx.Apply() - if err == nil { + _, diags := ctx.Apply() + if !diags.HasErrors() { t.Fatalf("Expected err, got none!") } expected := "Expected ami to be string" - if !strings.Contains(err.Error(), expected) { - t.Fatalf("expected:\n\n%s\n\nto contain:\n\n%s", err, expected) + if errStr := diags.Err().Error(); !strings.Contains(errStr, expected) { + t.Fatalf("expected:\n\n%s\n\nto contain:\n\n%s", errStr, expected) } } @@ -837,7 +841,7 @@ func TestContext2Apply_emptyModule(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -886,7 +890,7 @@ func TestContext2Apply_createBeforeDestroy(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -942,7 +946,7 @@ func TestContext2Apply_createBeforeDestroyUpdate(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1010,7 +1014,7 @@ func TestContext2Apply_createBeforeDestroy_dependsNonCBD(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1080,7 +1084,7 @@ func TestContext2Apply_createBeforeDestroy_hook(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, Hooks: []Hook{h}, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ @@ -1150,7 +1154,7 @@ func TestContext2Apply_createBeforeDestroy_deposedCount(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1215,7 +1219,7 @@ func TestContext2Apply_createBeforeDestroy_deposedOnly(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1266,7 +1270,7 @@ func TestContext2Apply_destroyComputed(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1338,7 +1342,7 @@ func testContext2Apply_destroyDependsOn(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1416,7 +1420,7 @@ func testContext2Apply_destroyDependsOnStateOnly(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1494,7 +1498,7 @@ func testContext2Apply_destroyDependsOnStateOnlyModule(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1527,7 +1531,7 @@ func TestContext2Apply_dataBasic(t *testing.T) { p.ReadDataApplyReturn = &InstanceState{ID: "yo"} ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "null": testProviderFuncFixed(p), @@ -1579,7 +1583,7 @@ func TestContext2Apply_destroyData(t *testing.T) { } hook := &testHook{} ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "null": testProviderFuncFixed(p), @@ -1650,7 +1654,7 @@ func TestContext2Apply_destroySkipsCBD(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1692,7 +1696,7 @@ func TestContext2Apply_destroyModuleVarProviderConfig(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1733,8 +1737,7 @@ func TestContext2Apply_destroyCrossProviders(t *testing.T) { // so we run this test multiple times // to check for the race-condition for i := 0; i <= 10; i++ { - ctx := getContextForApply_destroyCrossProviders( - t, m, providers) + ctx := getContextForApply_destroyCrossProviders(t, m, providers) if p, err := ctx.Plan(); err != nil { t.Fatalf("err: %s", err) @@ -1748,10 +1751,7 @@ func TestContext2Apply_destroyCrossProviders(t *testing.T) { } } -func getContextForApply_destroyCrossProviders( - t *testing.T, - m *module.Tree, - providers map[string]ResourceProviderFactory) *Context { +func getContextForApply_destroyCrossProviders(t *testing.T, m *configs.Config, providers map[string]ResourceProviderFactory) *Context { state := &State{ Modules: []*ModuleState{ &ModuleState{ @@ -1785,7 +1785,7 @@ func getContextForApply_destroyCrossProviders( }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed(providers), State: state, Destroy: true, @@ -1800,7 +1800,7 @@ func TestContext2Apply_minimal(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1830,7 +1830,7 @@ func TestContext2Apply_badDiff(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1861,7 +1861,7 @@ func TestContext2Apply_cancel(t *testing.T) { m := testModule(t, "apply-cancel") p := testProvider("aws") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1904,20 +1904,18 @@ func TestContext2Apply_cancel(t *testing.T) { } // Start the Apply in a goroutine - var applyErr error + var applyDiags tfdiags.Diagnostics stateCh := make(chan *State) go func() { - state, err := ctx.Apply() - if err != nil { - applyErr = err - } + state, diags := ctx.Apply() + applyDiags = diags stateCh <- state }() state := <-stateCh - if applyErr != nil { - t.Fatalf("err: %s", applyErr) + if applyDiags.HasErrors() { + t.Fatalf("unexpected errors: %s", applyDiags.Err()) } mod := state.RootModule() @@ -1940,7 +1938,7 @@ func TestContext2Apply_cancelBlock(t *testing.T) { m := testModule(t, "apply-cancel-block") p := testProvider("aws") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1972,13 +1970,11 @@ func TestContext2Apply_cancelBlock(t *testing.T) { } // Start the Apply in a goroutine - var applyErr error + var applyDiags tfdiags.Diagnostics stateCh := make(chan *State) go func() { - state, err := ctx.Apply() - if err != nil { - applyErr = err - } + state, diags := ctx.Apply() + applyDiags = diags stateCh <- state }() @@ -2006,8 +2002,8 @@ func TestContext2Apply_cancelBlock(t *testing.T) { // Wait for apply to complete state := <-stateCh - if applyErr != nil { - t.Fatalf("err: %s", applyErr) + if applyDiags.HasErrors() { + t.Fatalf("unexpected error: %s", applyDiags.Err()) } checkStateString(t, state, ` @@ -2024,7 +2020,7 @@ func TestContext2Apply_cancelProvisioner(t *testing.T) { p.DiffFn = testDiffFn pr := testProvisioner() ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2053,21 +2049,19 @@ func TestContext2Apply_cancelProvisioner(t *testing.T) { } // Start the Apply in a goroutine - var applyErr error + var applyDiags tfdiags.Diagnostics stateCh := make(chan *State) go func() { - state, err := ctx.Apply() - if err != nil { - applyErr = err - } + state, diags := ctx.Apply() + applyDiags = diags stateCh <- state }() // Wait for completion state := <-stateCh - if applyErr != nil { - t.Fatalf("err: %s", applyErr) + if applyDiags.HasErrors() { + t.Fatalf("unexpected errors: %s", applyDiags.Err()) } checkStateString(t, state, ` @@ -2089,7 +2083,7 @@ func TestContext2Apply_compute(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2101,11 +2095,16 @@ func TestContext2Apply_compute(t *testing.T) { t.Fatalf("err: %s", err) } - ctx.variables = map[string]interface{}{"value": "1"} + ctx.variables = InputValues{ + "value": &InputValue{ + Value: cty.NumberIntVal(1), + SourceType: ValueFromCaller, + }, + } - state, err := ctx.Apply() - if err != nil { - t.Fatalf("err: %s", err) + state, diags := ctx.Apply() + if diags.HasErrors() { + t.Fatalf("unexpected errors: %s", diags.Err()) } actual := strings.TrimSpace(state.String()) @@ -2159,7 +2158,7 @@ func TestContext2Apply_countDecrease(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2221,7 +2220,7 @@ func TestContext2Apply_countDecreaseToOneX(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2285,7 +2284,7 @@ func TestContext2Apply_countDecreaseToOneCorrupted(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2337,7 +2336,7 @@ func TestContext2Apply_countTainted(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2368,7 +2367,7 @@ func TestContext2Apply_countVariable(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2398,7 +2397,7 @@ func TestContext2Apply_countVariableRef(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2444,21 +2443,21 @@ func TestContext2Apply_provisionerInterpCount(t *testing.T) { "local-exec": testProvisionerFuncFixed(pr), } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: providerResolver, Provisioners: provisioners, }) - plan, err := ctx.Plan() - if err != nil { - t.Fatalf("plan failed: %s", err) + plan, diags := ctx.Plan() + if diags.HasErrors() { + t.Fatalf("plan failed unexpectedly: %s", diags.Err()) } // We'll marshal and unmarshal the plan here, to ensure that we have // a clean new context as would be created if we separately ran // terraform plan -out=tfplan && terraform apply tfplan var planBuf bytes.Buffer - err = WritePlan(plan, &planBuf) + err := WritePlan(plan, &planBuf) if err != nil { t.Fatalf("failed to write plan: %s", err) } @@ -2467,20 +2466,20 @@ func TestContext2Apply_provisionerInterpCount(t *testing.T) { t.Fatalf("failed to read plan: %s", err) } - ctx, err = plan.Context(&ContextOpts{ + ctx, diags = plan.Context(&ContextOpts{ // Most options are taken from the plan in this case, but we still // need to provide the plugins. ProviderResolver: providerResolver, Provisioners: provisioners, }) - if err != nil { - t.Fatalf("failed to create context for plan: %s", err) + if diags.HasErrors() { + t.Fatalf("failed to create context for plan: %s", diags.Err()) } // Applying the plan should now succeed - _, err = ctx.Apply() - if err != nil { - t.Fatalf("apply failed: %s", err) + _, diags = ctx.Apply() + if diags.HasErrors() { + t.Fatalf("apply failed unexpectedly: %s", diags.Err()) } // Verify apply was invoked @@ -2495,17 +2494,18 @@ func TestContext2Apply_mapVariableOverride(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "images": []map[string]interface{}{ - map[string]interface{}{ - "us-west-2": "overridden", - }, + Variables: InputValues{ + "images": &InputValue{ + Value: cty.MapVal(map[string]cty.Value{ + "us-west-2": cty.StringVal("overridden"), + }), + SourceType: ValueFromCaller, }, }, }) @@ -2543,7 +2543,7 @@ func TestContext2Apply_moduleBasic(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2612,7 +2612,7 @@ func TestContext2Apply_moduleDestroyOrder(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2664,7 +2664,7 @@ func TestContext2Apply_moduleInheritAlias(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2730,7 +2730,7 @@ func TestContext2Apply_moduleOrphanInheritAlias(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, State: state, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ @@ -2787,7 +2787,7 @@ func TestContext2Apply_moduleOrphanProvider(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, State: state, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ @@ -2837,7 +2837,7 @@ func TestContext2Apply_moduleOrphanGrandchildProvider(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, State: state, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ @@ -2875,7 +2875,7 @@ func TestContext2Apply_moduleGrandchildProvider(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2912,7 +2912,7 @@ func TestContext2Apply_moduleOnlyProvider(t *testing.T) { pTest.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2943,7 +2943,7 @@ func TestContext2Apply_moduleProviderAlias(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2973,13 +2973,22 @@ func TestContext2Apply_moduleProviderAliasTargets(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Targets: []string{"no.thing"}, + Targets: []addrs.Targetable{ + addrs.AbsResource{ + Module: addrs.RootModuleInstance, + Resource: addrs.Resource{ + Mode: addrs.ManagedResourceMode, + Type: "nonexistent", + Name: "thing", + }, + }, + }, }) if _, err := ctx.Plan(); err != nil { @@ -3006,7 +3015,7 @@ func TestContext2Apply_moduleProviderCloseNested(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -3070,7 +3079,7 @@ func TestContext2Apply_moduleVarRefExisting(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -3101,14 +3110,17 @@ func TestContext2Apply_moduleVarResourceCount(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "count": "2", + Variables: InputValues{ + "count": &InputValue{ + Value: cty.NumberIntVal(2), + SourceType: ValueFromCaller, + }, }, Destroy: true, }) @@ -3122,14 +3134,17 @@ func TestContext2Apply_moduleVarResourceCount(t *testing.T) { } ctx = testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "count": "5", + Variables: InputValues{ + "count": &InputValue{ + Value: cty.NumberIntVal(5), + SourceType: ValueFromCaller, + }, }, }) @@ -3149,7 +3164,7 @@ func TestContext2Apply_moduleBool(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -3181,13 +3196,15 @@ func TestContext2Apply_moduleTarget(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Targets: []string{"module.B"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Child("B", addrs.NoKey), + }, }) if _, err := ctx.Plan(); err != nil { @@ -3231,7 +3248,7 @@ func TestContext2Apply_multiProvider(t *testing.T) { pDO.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -3276,7 +3293,7 @@ func TestContext2Apply_multiProviderDestroy(t *testing.T) { // First, create the instances { ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -3333,7 +3350,7 @@ func TestContext2Apply_multiProviderDestroy(t *testing.T) { ctx := testContext2(t, &ContextOpts{ Destroy: true, State: state, - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -3379,7 +3396,7 @@ func TestContext2Apply_multiProviderDestroyChild(t *testing.T) { // First, create the instances { ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -3436,7 +3453,7 @@ func TestContext2Apply_multiProviderDestroyChild(t *testing.T) { ctx := testContext2(t, &ContextOpts{ Destroy: true, State: state, - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -3476,14 +3493,17 @@ func TestContext2Apply_multiVar(t *testing.T) { // First, apply with a count of 3 ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "count": "3", + Variables: InputValues{ + "count": &InputValue{ + Value: cty.NumberIntVal(3), + SourceType: ValueFromCaller, + }, }, }) @@ -3507,15 +3527,18 @@ func TestContext2Apply_multiVar(t *testing.T) { // Apply again, reduce the count to 1 { ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, State: state, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "count": "1", + Variables: InputValues{ + "count": &InputValue{ + Value: cty.NumberIntVal(1), + SourceType: ValueFromCaller, + }, }, }) @@ -3575,14 +3598,17 @@ func TestContext2Apply_multiVarComprehensive(t *testing.T) { // First, apply with a count of 3 ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "test": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "count": "3", + Variables: InputValues{ + "count": &InputValue{ + Value: cty.NumberIntVal(3), + SourceType: ValueFromCaller, + }, }, }) @@ -3702,7 +3728,7 @@ func TestContext2Apply_multiVarOrder(t *testing.T) { // First, apply with a count of 3 ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -3738,7 +3764,7 @@ func TestContext2Apply_multiVarOrderInterp(t *testing.T) { // First, apply with a count of 3 ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -3776,14 +3802,17 @@ func TestContext2Apply_multiVarCountDec(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "count": "2", + Variables: InputValues{ + "count": &InputValue{ + Value: cty.NumberIntVal(2), + SourceType: ValueFromCaller, + }, }, }) @@ -3839,14 +3868,17 @@ func TestContext2Apply_multiVarCountDec(t *testing.T) { ctx := testContext2(t, &ContextOpts{ State: s, - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "count": "1", + Variables: InputValues{ + "count": &InputValue{ + Value: cty.NumberIntVal(1), + SourceType: ValueFromCaller, + }, }, }) @@ -3881,7 +3913,7 @@ func TestContext2Apply_multiVarMissingState(t *testing.T) { // First, apply with a count of 3 ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "test": testProviderFuncFixed(p), @@ -3907,7 +3939,7 @@ func TestContext2Apply_nilDiff(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -3949,7 +3981,7 @@ func TestContext2Apply_outputDependsOn(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -3961,9 +3993,9 @@ func TestContext2Apply_outputDependsOn(t *testing.T) { t.Fatalf("err: %s", err) } - state, err := ctx.Apply() - if err == nil || !strings.Contains(err.Error(), "abcd") { - t.Fatalf("err: %s", err) + state, diags := ctx.Apply() + if !diags.HasErrors() || !strings.Contains(diags.Err().Error(), "abcd") { + t.Fatalf("err: %s", diags.Err()) } checkStateString(t, state, ``) @@ -3974,7 +4006,7 @@ func TestContext2Apply_outputDependsOn(t *testing.T) { p.ApplyFn = testApplyFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -4030,7 +4062,7 @@ func TestContext2Apply_outputOrphan(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -4080,7 +4112,7 @@ func TestContext2Apply_outputOrphanModule(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -4107,7 +4139,7 @@ func TestContext2Apply_outputOrphanModule(t *testing.T) { // now apply with no module in the config, which should remove the // remaining output ctx = testContext2(t, &ContextOpts{ - Module: module.NewEmptyTree(), + Config: configs.NewEmptyConfig(), ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -4142,7 +4174,7 @@ func TestContext2Apply_providerComputedVar(t *testing.T) { pTest.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -4194,7 +4226,7 @@ func TestContext2Apply_providerConfigureDisabled(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -4222,7 +4254,7 @@ func TestContext2Apply_provisionerModule(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -4269,7 +4301,7 @@ func TestContext2Apply_Provisioner_compute(t *testing.T) { return nil } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -4278,8 +4310,11 @@ func TestContext2Apply_Provisioner_compute(t *testing.T) { Provisioners: map[string]ResourceProvisionerFactory{ "shell": testProvisionerFuncFixed(pr), }, - Variables: map[string]interface{}{ - "value": "1", + Variables: InputValues{ + "value": &InputValue{ + Value: cty.NumberIntVal(1), + SourceType: ValueFromCaller, + }, }, }) @@ -4319,7 +4354,7 @@ func TestContext2Apply_provisionerCreateFail(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -4360,7 +4395,7 @@ func TestContext2Apply_provisionerCreateFailNoId(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -4399,7 +4434,7 @@ func TestContext2Apply_provisionerFail(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -4408,8 +4443,11 @@ func TestContext2Apply_provisionerFail(t *testing.T) { Provisioners: map[string]ResourceProvisionerFactory{ "shell": testProvisionerFuncFixed(pr), }, - Variables: map[string]interface{}{ - "value": "1", + Variables: InputValues{ + "value": &InputValue{ + Value: cty.NumberIntVal(1), + SourceType: ValueFromCaller, + }, }, }) @@ -4458,7 +4496,7 @@ func TestContext2Apply_provisionerFail_createBeforeDestroy(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -4508,7 +4546,7 @@ func TestContext2Apply_error_createBeforeDestroy(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -4559,7 +4597,7 @@ func TestContext2Apply_errorDestroy_createBeforeDestroy(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -4617,7 +4655,7 @@ func TestContext2Apply_multiDepose_createBeforeDestroy(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed(ps), State: state, }) @@ -4658,7 +4696,7 @@ aws_instance.web: (1 deposed) createdInstanceId = "baz" ctx = testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed(ps), State: state, }) @@ -4745,7 +4783,7 @@ func TestContext2Apply_provisionerFailContinue(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -4793,7 +4831,7 @@ func TestContext2Apply_provisionerFailContinueHook(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, Hooks: []Hook{h}, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ @@ -4853,7 +4891,7 @@ func TestContext2Apply_provisionerDestroy(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, State: state, Destroy: true, ProviderResolver: ResourceProviderResolverFixed( @@ -4911,7 +4949,7 @@ func TestContext2Apply_provisionerDestroyFail(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, State: state, Destroy: true, ProviderResolver: ResourceProviderResolverFixed( @@ -4984,7 +5022,7 @@ func TestContext2Apply_provisionerDestroyFailContinue(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, State: state, Destroy: true, ProviderResolver: ResourceProviderResolverFixed( @@ -5060,7 +5098,7 @@ func TestContext2Apply_provisionerDestroyFailContinueFail(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, State: state, Destroy: true, ProviderResolver: ResourceProviderResolverFixed( @@ -5140,7 +5178,7 @@ func TestContext2Apply_provisionerDestroyTainted(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, State: state, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ @@ -5211,7 +5249,7 @@ func TestContext2Apply_provisionerDestroyModule(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, State: state, Destroy: true, ProviderResolver: ResourceProviderResolverFixed( @@ -5285,7 +5323,7 @@ func TestContext2Apply_provisionerDestroyRef(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, State: state, Destroy: true, ProviderResolver: ResourceProviderResolverFixed( @@ -5350,7 +5388,7 @@ func TestContext2Apply_provisionerDestroyRefInvalid(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, State: state, Destroy: true, ProviderResolver: ResourceProviderResolverFixed( @@ -5388,7 +5426,7 @@ func TestContext2Apply_provisionerResourceRef(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -5436,7 +5474,7 @@ func TestContext2Apply_provisionerSelfRef(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -5491,7 +5529,7 @@ func TestContext2Apply_provisionerMultiSelfRef(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -5553,7 +5591,7 @@ func TestContext2Apply_provisionerMultiSelfRefSingle(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -5615,7 +5653,7 @@ func TestContext2Apply_provisionerMultiSelfRefCount(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -5665,7 +5703,7 @@ func TestContext2Apply_provisionerExplicitSelfRef(t *testing.T) { var state *State { ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -5694,7 +5732,7 @@ func TestContext2Apply_provisionerExplicitSelfRef(t *testing.T) { { ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, Destroy: true, State: state, ProviderResolver: ResourceProviderResolverFixed( @@ -5732,7 +5770,7 @@ func TestContext2Apply_Provisioner_Diff(t *testing.T) { return nil } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -5770,7 +5808,7 @@ func TestContext2Apply_Provisioner_Diff(t *testing.T) { // Re-create context with state ctx = testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -5821,7 +5859,7 @@ func TestContext2Apply_outputDiffVars(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -5905,7 +5943,7 @@ func TestContext2Apply_Provisioner_ConnInfo(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -5914,9 +5952,15 @@ func TestContext2Apply_Provisioner_ConnInfo(t *testing.T) { Provisioners: map[string]ResourceProvisionerFactory{ "shell": testProvisionerFuncFixed(pr), }, - Variables: map[string]interface{}{ - "value": "1", - "pass": "test", + Variables: InputValues{ + "value": &InputValue{ + Value: cty.StringVal("1"), + SourceType: ValueFromCaller, + }, + "pass": &InputValue{ + Value: cty.StringVal("test"), + SourceType: ValueFromCaller, + }, }, }) @@ -5948,7 +5992,7 @@ func TestContext2Apply_destroyX(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, Hooks: []Hook{h}, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ @@ -5972,7 +6016,7 @@ func TestContext2Apply_destroyX(t *testing.T) { ctx = testContext2(t, &ContextOpts{ Destroy: true, State: state, - Module: m, + Config: m, Hooks: []Hook{h}, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ @@ -6012,7 +6056,7 @@ func TestContext2Apply_destroyOrder(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, Hooks: []Hook{h}, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ @@ -6038,7 +6082,7 @@ func TestContext2Apply_destroyOrder(t *testing.T) { ctx = testContext2(t, &ContextOpts{ Destroy: true, State: state, - Module: m, + Config: m, Hooks: []Hook{h}, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ @@ -6079,7 +6123,7 @@ func TestContext2Apply_destroyModulePrefix(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, Hooks: []Hook{h}, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ @@ -6108,7 +6152,7 @@ func TestContext2Apply_destroyModulePrefix(t *testing.T) { ctx = testContext2(t, &ContextOpts{ Destroy: true, State: state, - Module: m, + Config: m, Hooks: []Hook{h}, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ @@ -6155,7 +6199,7 @@ func TestContext2Apply_destroyNestedModule(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -6204,7 +6248,7 @@ func TestContext2Apply_destroyDeeplyNestedModule(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -6238,10 +6282,9 @@ func TestContext2Apply_destroyModuleWithAttrsReferencingResource(t *testing.T) { p.DiffFn = testDiffFn var state *State - var err error { ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -6256,9 +6299,10 @@ func TestContext2Apply_destroyModuleWithAttrsReferencingResource(t *testing.T) { t.Logf("Step 1 plan: %s", p) } - state, err = ctx.Apply() - if err != nil { - t.Fatalf("apply err: %s", err) + var diags tfdiags.Diagnostics + state, diags = ctx.Apply() + if diags.HasErrors() { + t.Fatalf("apply errs: %s", diags.Err()) } t.Logf("Step 1 state: %s", state) @@ -6270,7 +6314,7 @@ func TestContext2Apply_destroyModuleWithAttrsReferencingResource(t *testing.T) { { ctx := testContext2(t, &ContextOpts{ Destroy: true, - Module: m, + Config: m, State: state, Hooks: []Hook{h}, ProviderResolver: ResourceProviderResolverFixed( @@ -6278,15 +6322,18 @@ func TestContext2Apply_destroyModuleWithAttrsReferencingResource(t *testing.T) { "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "key_name": "foobarkey", + Variables: InputValues{ + "key_name": &InputValue{ + Value: cty.StringVal("foobarkey"), + SourceType: ValueFromCaller, + }, }, }) // First plan and apply a create operation - plan, err := ctx.Plan() - if err != nil { - t.Fatalf("destroy plan err: %s", err) + plan, diags := ctx.Plan() + if diags.HasErrors() { + t.Fatalf("destroy plan err: %s", diags.Err()) } t.Logf("Step 2 plan: %s", plan) @@ -6301,20 +6348,20 @@ func TestContext2Apply_destroyModuleWithAttrsReferencingResource(t *testing.T) { t.Fatalf("plan read err: %s", err) } - ctx, err = planFromFile.Context(&ContextOpts{ + ctx, diags = planFromFile.Context(&ContextOpts{ ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), }) - if err != nil { - t.Fatalf("err: %s", err) + if diags.HasErrors() { + t.Fatalf("err: %s", diags.Err()) } - state, err = ctx.Apply() - if err != nil { - t.Fatalf("destroy apply err: %s", err) + state, diags = ctx.Apply() + if diags.HasErrors() { + t.Fatalf("destroy apply err: %s", diags.Err()) } t.Logf("Step 2 state: %s", state) @@ -6339,10 +6386,10 @@ func TestContext2Apply_destroyWithModuleVariableAndCount(t *testing.T) { p.DiffFn = testDiffFn var state *State - var err error + var diags tfdiags.Diagnostics { ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -6351,13 +6398,13 @@ func TestContext2Apply_destroyWithModuleVariableAndCount(t *testing.T) { }) // First plan and apply a create operation - if _, err := ctx.Plan(); err != nil { - t.Fatalf("plan err: %s", err) + if _, diags := ctx.Plan(); diags.HasErrors() { + t.Fatalf("plan err: %s", diags.Err()) } - state, err = ctx.Apply() - if err != nil { - t.Fatalf("apply err: %s", err) + state, diags = ctx.Apply() + if diags.HasErrors() { + t.Fatalf("apply err: %s", diags.Err()) } } @@ -6367,7 +6414,7 @@ func TestContext2Apply_destroyWithModuleVariableAndCount(t *testing.T) { { ctx := testContext2(t, &ContextOpts{ Destroy: true, - Module: m, + Config: m, State: state, Hooks: []Hook{h}, ProviderResolver: ResourceProviderResolverFixed( @@ -6378,9 +6425,9 @@ func TestContext2Apply_destroyWithModuleVariableAndCount(t *testing.T) { }) // First plan and apply a create operation - plan, err := ctx.Plan() - if err != nil { - t.Fatalf("destroy plan err: %s", err) + plan, diags := ctx.Plan() + if diags.HasErrors() { + t.Fatalf("destroy plan err: %s", diags.Err()) } var buf bytes.Buffer @@ -6393,20 +6440,20 @@ func TestContext2Apply_destroyWithModuleVariableAndCount(t *testing.T) { t.Fatalf("plan read err: %s", err) } - ctx, err = planFromFile.Context(&ContextOpts{ + ctx, diags = planFromFile.Context(&ContextOpts{ ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), }) - if err != nil { - t.Fatalf("err: %s", err) + if diags.HasErrors() { + t.Fatalf("err: %s", diags.Err()) } - state, err = ctx.Apply() - if err != nil { - t.Fatalf("destroy apply err: %s", err) + state, diags = ctx.Apply() + if diags.HasErrors() { + t.Fatalf("destroy apply err: %s", diags.Err()) } } @@ -6429,10 +6476,10 @@ func TestContext2Apply_destroyTargetWithModuleVariableAndCount(t *testing.T) { p.DiffFn = testDiffFn var state *State - var err error + var diags tfdiags.Diagnostics { ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -6441,27 +6488,29 @@ func TestContext2Apply_destroyTargetWithModuleVariableAndCount(t *testing.T) { }) // First plan and apply a create operation - if _, err := ctx.Plan(); err != nil { - t.Fatalf("plan err: %s", err) + if _, diags := ctx.Plan(); diags.HasErrors() { + t.Fatalf("plan err: %s", diags.Err()) } - state, err = ctx.Apply() - if err != nil { - t.Fatalf("apply err: %s", err) + state, diags = ctx.Apply() + if diags.HasErrors() { + t.Fatalf("apply err: %s", diags.Err()) } } { ctx := testContext2(t, &ContextOpts{ Destroy: true, - Module: m, + Config: m, State: state, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Targets: []string{"module.child"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Child("child", addrs.NoKey), + }, }) _, err := ctx.Plan() @@ -6495,10 +6544,10 @@ func TestContext2Apply_destroyWithModuleVariableAndCountNested(t *testing.T) { p.DiffFn = testDiffFn var state *State - var err error + var diags tfdiags.Diagnostics { ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -6507,13 +6556,13 @@ func TestContext2Apply_destroyWithModuleVariableAndCountNested(t *testing.T) { }) // First plan and apply a create operation - if _, err := ctx.Plan(); err != nil { - t.Fatalf("plan err: %s", err) + if _, diags := ctx.Plan(); diags.HasErrors() { + t.Fatalf("plan err: %s", diags.Err()) } - state, err = ctx.Apply() - if err != nil { - t.Fatalf("apply err: %s", err) + state, diags = ctx.Apply() + if diags.HasErrors() { + t.Fatalf("apply err: %s", diags.Err()) } } @@ -6523,7 +6572,7 @@ func TestContext2Apply_destroyWithModuleVariableAndCountNested(t *testing.T) { { ctx := testContext2(t, &ContextOpts{ Destroy: true, - Module: m, + Config: m, State: state, Hooks: []Hook{h}, ProviderResolver: ResourceProviderResolverFixed( @@ -6534,9 +6583,9 @@ func TestContext2Apply_destroyWithModuleVariableAndCountNested(t *testing.T) { }) // First plan and apply a create operation - plan, err := ctx.Plan() - if err != nil { - t.Fatalf("destroy plan err: %s", err) + plan, diags := ctx.Plan() + if diags.HasErrors() { + t.Fatalf("destroy plan err: %s", diags.Err()) } var buf bytes.Buffer @@ -6549,20 +6598,20 @@ func TestContext2Apply_destroyWithModuleVariableAndCountNested(t *testing.T) { t.Fatalf("plan read err: %s", err) } - ctx, err = planFromFile.Context(&ContextOpts{ + ctx, diags = planFromFile.Context(&ContextOpts{ ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), }) - if err != nil { - t.Fatalf("err: %s", err) + if diags.HasErrors() { + t.Fatalf("err: %s", diags.Err()) } - state, err = ctx.Apply() - if err != nil { - t.Fatalf("destroy apply err: %s", err) + state, diags = ctx.Apply() + if diags.HasErrors() { + t.Fatalf("destroy apply err: %s", diags.Err()) } } @@ -6586,7 +6635,7 @@ func TestContext2Apply_destroyOutputs(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -6609,7 +6658,7 @@ func TestContext2Apply_destroyOutputs(t *testing.T) { ctx = testContext2(t, &ContextOpts{ Destroy: true, State: state, - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -6670,7 +6719,7 @@ func TestContext2Apply_destroyOrphan(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -6747,7 +6796,7 @@ func TestContext2Apply_destroyTaintedProvisioner(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -6786,7 +6835,7 @@ func TestContext2Apply_error(t *testing.T) { m := testModule(t, "apply-error") p := testProvider("aws") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -6857,7 +6906,7 @@ func TestContext2Apply_errorPartial(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -6917,7 +6966,7 @@ func TestContext2Apply_hook(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, Hooks: []Hook{h}, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ @@ -6969,7 +7018,7 @@ func TestContext2Apply_hookOrphan(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, State: state, Hooks: []Hook{h}, ProviderResolver: ResourceProviderResolverFixed( @@ -7002,7 +7051,7 @@ func TestContext2Apply_idAttr(t *testing.T) { m := testModule(t, "apply-idattr") p := testProvider("aws") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -7057,7 +7106,7 @@ func TestContext2Apply_outputBasic(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -7087,7 +7136,7 @@ func TestContext2Apply_outputInvalid(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -7095,12 +7144,12 @@ func TestContext2Apply_outputInvalid(t *testing.T) { ), }) - _, err := ctx.Plan() - if err == nil { - t.Fatalf("err: %s", err) + _, diags := ctx.Plan() + if !diags.HasErrors() { + t.Fatal("succeeded; want error") } - if !strings.Contains(err.Error(), "is not a valid type") { - t.Fatalf("err: %s", err) + if errStr := diags.Err().Error(); !strings.Contains(errStr, "is not a valid type") { + t.Fatalf("wrong error: %s", errStr) } } @@ -7110,7 +7159,7 @@ func TestContext2Apply_outputAdd(t *testing.T) { p1.ApplyFn = testApplyFn p1.DiffFn = testDiffFn ctx1 := testContext2(t, &ContextOpts{ - Module: m1, + Config: m1, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p1), @@ -7132,7 +7181,7 @@ func TestContext2Apply_outputAdd(t *testing.T) { p2.ApplyFn = testApplyFn p2.DiffFn = testDiffFn ctx2 := testContext2(t, &ContextOpts{ - Module: m2, + Config: m2, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p2), @@ -7163,7 +7212,7 @@ func TestContext2Apply_outputList(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -7193,7 +7242,7 @@ func TestContext2Apply_outputMulti(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -7223,7 +7272,7 @@ func TestContext2Apply_outputMultiIndex(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -7288,7 +7337,7 @@ func TestContext2Apply_taintX(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -7356,7 +7405,7 @@ func TestContext2Apply_taintDep(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -7420,7 +7469,7 @@ func TestContext2Apply_taintDepRequiresNew(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -7453,13 +7502,17 @@ func TestContext2Apply_targeted(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Targets: []string{"aws_instance.foo"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "foo", + ), + }, }) if _, err := ctx.Plan(); err != nil { @@ -7512,13 +7565,17 @@ func TestContext2Apply_targetedCount(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Targets: []string{"aws_instance.foo"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "foo", + ), + }, }) if _, err := ctx.Plan(); err != nil { @@ -7549,13 +7606,17 @@ func TestContext2Apply_targetedCountIndex(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Targets: []string{"aws_instance.foo[1]"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.ResourceInstance( + addrs.ManagedResourceMode, "aws_instance", "foo", addrs.IntKey(1), + ), + }, }) if _, err := ctx.Plan(); err != nil { @@ -7580,7 +7641,7 @@ func TestContext2Apply_targetedDestroy(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -7597,7 +7658,11 @@ func TestContext2Apply_targetedDestroy(t *testing.T) { }, }, }, - Targets: []string{"aws_instance.foo"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "foo", + ), + }, Destroy: true, }) @@ -7638,7 +7703,7 @@ func TestContext2Apply_destroyProvisionerWithLocals(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -7661,7 +7726,11 @@ func TestContext2Apply_destroyProvisionerWithLocals(t *testing.T) { // the test works without targeting, but this also tests that the local // node isn't inadvertently pruned because of the wrong evaluation // order. - Targets: []string{"aws_instance.foo"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "foo", + ), + }, }) if _, err := ctx.Plan(); err != nil { @@ -7708,7 +7777,7 @@ func TestContext2Apply_destroyProvisionerWithMultipleLocals(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -7761,7 +7830,7 @@ func TestContext2Apply_destroyProvisionerWithOutput(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -7804,7 +7873,12 @@ func TestContext2Apply_destroyProvisionerWithOutput(t *testing.T) { // targeting the source of the value used by all resources should still // destroy them all. - Targets: []string{"module.mod.aws_instance.baz"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Child("mod", addrs.NoKey).Resource( + addrs.ManagedResourceMode, "aws_instance", "baz", + ), + }, + }) if _, err := ctx.Plan(); err != nil { @@ -7833,7 +7907,7 @@ func TestContext2Apply_targetedDestroyCountDeps(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -7850,7 +7924,11 @@ func TestContext2Apply_targetedDestroyCountDeps(t *testing.T) { }, }, }, - Targets: []string{"aws_instance.foo"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "foo", + ), + }, Destroy: true, }) @@ -7873,7 +7951,7 @@ func TestContext2Apply_targetedDestroyModule(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -7897,7 +7975,11 @@ func TestContext2Apply_targetedDestroyModule(t *testing.T) { }, }, }, - Targets: []string{"module.child.aws_instance.foo"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "foo", + ), + }, Destroy: true, }) @@ -7928,7 +8010,7 @@ func TestContext2Apply_targetedDestroyCountIndex(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -7949,9 +8031,13 @@ func TestContext2Apply_targetedDestroyCountIndex(t *testing.T) { }, }, }, - Targets: []string{ - "aws_instance.foo[2]", - "aws_instance.bar[1]", + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.ResourceInstance( + addrs.ManagedResourceMode, "aws_instance", "foo", addrs.IntKey(2), + ), + addrs.RootModuleInstance.ResourceInstance( + addrs.ManagedResourceMode, "aws_instance", "bar", addrs.IntKey(1), + ), }, Destroy: true, }) @@ -7983,13 +8069,15 @@ func TestContext2Apply_targetedModule(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Targets: []string{"module.child"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Child("child", addrs.NoKey), + }, }) if _, err := ctx.Plan(); err != nil { @@ -8001,7 +8089,7 @@ func TestContext2Apply_targetedModule(t *testing.T) { t.Fatalf("err: %s", err) } - mod := state.ModuleByPath([]string{"root", "child"}) + mod := state.ModuleByPath(addrs.RootModuleInstance.Child("child", addrs.NoKey)) if mod == nil { t.Fatalf("no child module found in the state!\n\n%#v", state) } @@ -8032,13 +8120,17 @@ func TestContext2Apply_targetedModuleDep(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Targets: []string{"aws_instance.foo"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "foo", + ), + }, }) if p, err := ctx.Plan(); err != nil { @@ -8081,13 +8173,15 @@ func TestContext2Apply_targetedModuleUnrelatedOutputs(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Targets: []string{"module.child2"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Child("child2", addrs.NoKey), + }, State: &State{ Modules: []*ModuleState{ { @@ -8154,13 +8248,17 @@ func TestContext2Apply_targetedModuleResource(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Targets: []string{"module.child.aws_instance.foo"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Child("child", addrs.NoKey).Resource( + addrs.ManagedResourceMode, "aws_instance", "foo", + ), + }, }) if _, err := ctx.Plan(); err != nil { @@ -8172,7 +8270,7 @@ func TestContext2Apply_targetedModuleResource(t *testing.T) { t.Fatalf("err: %s", err) } - mod := state.ModuleByPath([]string{"root", "child"}) + mod := state.ModuleByPath(addrs.RootModuleInstance.Child("child", addrs.NoKey)) if mod == nil || len(mod.Resources) != 1 { t.Fatalf("expected 1 resource, got: %#v", mod) } @@ -8194,7 +8292,7 @@ func TestContext2Apply_unknownAttribute(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -8224,7 +8322,7 @@ func TestContext2Apply_unknownAttributeInterpolate(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -8243,24 +8341,39 @@ func TestContext2Apply_vars(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "foo": "us-west-2", - "test_list": []interface{}{"Hello", "World"}, - "test_map": map[string]interface{}{ - "Hello": "World", - "Foo": "Bar", - "Baz": "Foo", + Variables: InputValues{ + "foo": &InputValue{ + Value: cty.StringVal("us-west-2"), + SourceType: ValueFromCaller, }, - "amis": []map[string]interface{}{ - map[string]interface{}{ - "us-east-1": "override", - }, + "test_list": &InputValue{ + Value: cty.ListVal([]cty.Value{ + cty.StringVal("Hello"), + cty.StringVal("World"), + }), + SourceType: ValueFromCaller, + }, + "test_map": &InputValue{ + Value: cty.MapVal(map[string]cty.Value{ + "Hello": cty.StringVal("World"), + "Foo": cty.StringVal("Bar"), + "Baz": cty.StringVal("Foo"), + }), + SourceType: ValueFromCaller, + }, + "amis": &InputValue{ + Value: cty.ListVal([]cty.Value{ + cty.MapVal(map[string]cty.Value{ + "us-west-1": cty.StringVal("override"), + }), + }), + SourceType: ValueFromCaller, }, }, }) @@ -8297,7 +8410,7 @@ func TestContext2Apply_varsEnv(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -8360,7 +8473,7 @@ func TestContext2Apply_createBefore_depends(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, Hooks: []Hook{h}, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ @@ -8473,7 +8586,7 @@ func TestContext2Apply_singleDestroy(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, Hooks: []Hook{h}, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ @@ -8510,7 +8623,7 @@ func TestContext2Apply_issue7824(t *testing.T) { // Apply cleanly step 0 ctx := testContext2(t, &ContextOpts{ - Module: testModule(t, "issue-7824"), + Config: testModule(t, "issue-7824"), ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "template": testProviderFuncFixed(p), @@ -8518,9 +8631,9 @@ func TestContext2Apply_issue7824(t *testing.T) { ), }) - plan, err := ctx.Plan() - if err != nil { - t.Fatalf("err: %s", err) + plan, diags := ctx.Plan() + if diags.HasErrors() { + t.Fatalf("err: %s", diags.Err()) } // Write / Read plan to simulate running it through a Plan file @@ -8534,20 +8647,20 @@ func TestContext2Apply_issue7824(t *testing.T) { t.Fatalf("err: %s", err) } - ctx, err = planFromFile.Context(&ContextOpts{ + ctx, diags = planFromFile.Context(&ContextOpts{ ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "template": testProviderFuncFixed(p), }, ), }) - if err != nil { - t.Fatalf("err: %s", err) + if diags.HasErrors() { + t.Fatalf("err: %s", diags.Err()) } - _, err = ctx.Apply() - if err != nil { - t.Fatalf("err: %s", err) + _, diags = ctx.Apply() + if diags.HasErrors() { + t.Fatalf("err: %s", diags.Err()) } } @@ -8565,7 +8678,7 @@ func TestContext2Apply_issue5254(t *testing.T) { // Apply cleanly step 0 ctx := testContext2(t, &ContextOpts{ - Module: testModule(t, "issue-5254/step-0"), + Config: testModule(t, "issue-5254/step-0"), ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "template": testProviderFuncFixed(p), @@ -8573,19 +8686,19 @@ func TestContext2Apply_issue5254(t *testing.T) { ), }) - plan, err := ctx.Plan() - if err != nil { - t.Fatalf("err: %s", err) + plan, diags := ctx.Plan() + if diags.HasErrors() { + t.Fatalf("err: %s", diags.Err()) } - state, err := ctx.Apply() - if err != nil { - t.Fatalf("err: %s", err) + state, diags := ctx.Apply() + if diags.HasErrors() { + t.Fatalf("err: %s", diags.Err()) } // Application success. Now make the modification and store a plan ctx = testContext2(t, &ContextOpts{ - Module: testModule(t, "issue-5254/step-1"), + Config: testModule(t, "issue-5254/step-1"), State: state, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ @@ -8594,9 +8707,9 @@ func TestContext2Apply_issue5254(t *testing.T) { ), }) - plan, err = ctx.Plan() - if err != nil { - t.Fatalf("err: %s", err) + plan, diags = ctx.Plan() + if diags.HasErrors() { + t.Fatalf("err: %s", diags.Err()) } // Write / Read plan to simulate running it through a Plan file @@ -8610,20 +8723,20 @@ func TestContext2Apply_issue5254(t *testing.T) { t.Fatalf("err: %s", err) } - ctx, err = planFromFile.Context(&ContextOpts{ + ctx, diags = planFromFile.Context(&ContextOpts{ ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "template": testProviderFuncFixed(p), }, ), }) - if err != nil { - t.Fatalf("err: %s", err) + if diags.HasErrors() { + t.Fatalf("err: %s", diags.Err()) } - state, err = ctx.Apply() - if err != nil { - t.Fatalf("err: %s", err) + state, diags = ctx.Apply() + if diags.HasErrors() { + t.Fatalf("err: %s", diags.Err()) } actual := strings.TrimSpace(state.String()) @@ -8652,13 +8765,17 @@ func TestContext2Apply_targetedWithTaintedInState(t *testing.T) { p.DiffFn = testDiffFn p.ApplyFn = testApplyFn ctx := testContext2(t, &ContextOpts{ - Module: testModule(t, "apply-tainted-targets"), + Config: testModule(t, "apply-tainted-targets"), ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Targets: []string{"aws_instance.iambeingadded"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "iambeingadded", + ), + }, State: &State{ Modules: []*ModuleState{ &ModuleState{ @@ -8677,9 +8794,9 @@ func TestContext2Apply_targetedWithTaintedInState(t *testing.T) { }, }) - plan, err := ctx.Plan() - if err != nil { - t.Fatalf("err: %s", err) + plan, diags := ctx.Plan() + if diags.HasErrors() { + t.Fatalf("err: %s", diags.Err()) } // Write / Read plan to simulate running it through a Plan file @@ -8693,21 +8810,21 @@ func TestContext2Apply_targetedWithTaintedInState(t *testing.T) { t.Fatalf("err: %s", err) } - ctx, err = planFromFile.Context(&ContextOpts{ - Module: testModule(t, "apply-tainted-targets"), + ctx, diags = planFromFile.Context(&ContextOpts{ + Config: testModule(t, "apply-tainted-targets"), ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), }) - if err != nil { - t.Fatalf("err: %s", err) + if diags.HasErrors() { + t.Fatalf("err: %s", diags.Err()) } - state, err := ctx.Apply() - if err != nil { - t.Fatalf("err: %s", err) + state, diags := ctx.Apply() + if diags.HasErrors() { + t.Fatalf("err: %s", diags.Err()) } actual := strings.TrimSpace(state.String()) @@ -8731,7 +8848,7 @@ func TestContext2Apply_ignoreChangesCreate(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -8843,7 +8960,7 @@ func TestContext2Apply_ignoreChangesWithDep(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -8876,7 +8993,7 @@ func TestContext2Apply_ignoreChangesWildcard(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -8922,10 +9039,10 @@ func TestContext2Apply_destroyNestedModuleWithAttrsReferencingResource(t *testin p.DiffFn = testDiffFn var state *State - var err error + var diags tfdiags.Diagnostics { ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "null": testProviderFuncFixed(p), @@ -8938,16 +9055,16 @@ func TestContext2Apply_destroyNestedModuleWithAttrsReferencingResource(t *testin t.Fatalf("plan err: %s", err) } - state, err = ctx.Apply() - if err != nil { - t.Fatalf("apply err: %s", err) + state, diags = ctx.Apply() + if diags.HasErrors() { + t.Fatalf("apply err: %s", diags.Err()) } } { ctx := testContext2(t, &ContextOpts{ Destroy: true, - Module: m, + Config: m, State: state, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ @@ -8956,9 +9073,9 @@ func TestContext2Apply_destroyNestedModuleWithAttrsReferencingResource(t *testin ), }) - plan, err := ctx.Plan() - if err != nil { - t.Fatalf("destroy plan err: %s", err) + plan, diags := ctx.Plan() + if diags.HasErrors() { + t.Fatalf("destroy plan err: %s", diags.Err()) } var buf bytes.Buffer @@ -8971,20 +9088,20 @@ func TestContext2Apply_destroyNestedModuleWithAttrsReferencingResource(t *testin t.Fatalf("plan read err: %s", err) } - ctx, err = planFromFile.Context(&ContextOpts{ + ctx, diags = planFromFile.Context(&ContextOpts{ ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "null": testProviderFuncFixed(p), }, ), }) - if err != nil { - t.Fatalf("err: %s", err) + if diags.HasErrors() { + t.Fatalf("err: %s", diags.Err()) } - - state, err = ctx.Apply() - if err != nil { - t.Fatalf("destroy apply err: %s", err) + + state, diags = ctx.Apply() + if diags.HasErrors() { + t.Fatalf("destroy apply err: %s", diags.Err()) } } @@ -9009,7 +9126,7 @@ func TestContext2Apply_dataDependsOn(t *testing.T) { m := testModule(t, "apply-data-depends-on") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "null": testProviderFuncFixed(p), @@ -9057,7 +9174,7 @@ func TestContext2Apply_dataDependsOn(t *testing.T) { t.Fatalf("err: %s", err) } - root := state.ModuleByPath(RootModulePath) + root := state.ModuleByPath(addrs.RootModuleInstance) actual := root.Resources["data.null_data_source.read"].Primary.Attributes["foo"] expected := "APPLIED" @@ -9074,7 +9191,7 @@ func TestContext2Apply_terraformEnv(t *testing.T) { ctx := testContext2(t, &ContextOpts{ Meta: &ContextMeta{Env: "foo"}, - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -9106,7 +9223,7 @@ func TestContext2Apply_multiRef(t *testing.T) { p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -9135,13 +9252,15 @@ func TestContext2Apply_targetedModuleRecursive(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Targets: []string{"module.child"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Child("child", addrs.NoKey), + }, }) if _, err := ctx.Plan(); err != nil { @@ -9153,7 +9272,9 @@ func TestContext2Apply_targetedModuleRecursive(t *testing.T) { t.Fatalf("err: %s", err) } - mod := state.ModuleByPath([]string{"root", "child", "subchild"}) + mod := state.ModuleByPath( + addrs.RootModuleInstance.Child("child", addrs.NoKey).Child("subchild", addrs.NoKey), + ) if mod == nil { t.Fatalf("no subchild module found in the state!\n\n%#v", state) } @@ -9175,7 +9296,7 @@ module.child.subchild: func TestContext2Apply_localVal(t *testing.T) { m := testModule(t, "apply-local-val") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{}, ), @@ -9245,7 +9366,7 @@ func TestContext2Apply_destroyWithLocals(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -9286,7 +9407,7 @@ func TestContext2Apply_providerWithLocals(t *testing.T) { p.DiffFn = testDiffFn p.ApplyFn = testApplyFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -9304,7 +9425,7 @@ func TestContext2Apply_providerWithLocals(t *testing.T) { } ctx = testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -9363,7 +9484,7 @@ func TestContext2Apply_destroyWithProviders(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -9398,7 +9519,7 @@ func TestContext2Apply_destroyWithProviders(t *testing.T) { } func TestContext2Apply_providersFromState(t *testing.T) { - m := module.NewEmptyTree() + m := configs.NewEmptyConfig() p := testProvider("aws") p.DiffFn = testDiffFn @@ -9478,7 +9599,7 @@ func TestContext2Apply_providersFromState(t *testing.T) { t.Run(tc.name, func(t *testing.T) { ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -9541,21 +9662,21 @@ func TestContext2Apply_plannedInterpolatedCount(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: providerResolver, State: s, }) - plan, err := ctx.Plan() - if err != nil { - t.Fatalf("plan failed: %s", err) + plan, diags := ctx.Plan() + if diags.HasErrors() { + t.Fatalf("plan failed: %s", diags.Err()) } // We'll marshal and unmarshal the plan here, to ensure that we have // a clean new context as would be created if we separately ran // terraform plan -out=tfplan && terraform apply tfplan var planBuf bytes.Buffer - err = WritePlan(plan, &planBuf) + err := WritePlan(plan, &planBuf) if err != nil { t.Fatalf("failed to write plan: %s", err) } @@ -9564,17 +9685,17 @@ func TestContext2Apply_plannedInterpolatedCount(t *testing.T) { t.Fatalf("failed to read plan: %s", err) } - ctx, err = plan.Context(&ContextOpts{ + ctx, diags = plan.Context(&ContextOpts{ ProviderResolver: providerResolver, }) - if err != nil { - t.Fatalf("failed to create context for plan: %s", err) + if diags.HasErrors() { + t.Fatalf("failed to create context for plan: %s", diags.Err()) } // Applying the plan should now succeed - _, err = ctx.Apply() - if err != nil { - t.Fatalf("apply failed: %s", err) + _, diags = ctx.Apply() + if diags.HasErrors() { + t.Fatalf("apply failed: %s", diags.Err()) } } diff --git a/terraform/context_import_test.go b/terraform/context_import_test.go index 2a258392b..fe430a00c 100644 --- a/terraform/context_import_test.go +++ b/terraform/context_import_test.go @@ -4,13 +4,16 @@ import ( "fmt" "strings" "testing" + + "github.com/hashicorp/terraform/addrs" + "github.com/zclconf/go-cty/cty" ) func TestContextImport_basic(t *testing.T) { p := testProvider("aws") m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -28,8 +31,10 @@ func TestContextImport_basic(t *testing.T) { state, err := ctx.Import(&ImportOpts{ Targets: []*ImportTarget{ &ImportTarget{ - Addr: "aws_instance.foo", - ID: "bar", + Addr: addrs.RootModuleInstance.ResourceInstance( + addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, + ), + ID: "bar", }, }, }) @@ -47,7 +52,7 @@ func TestContextImport_countIndex(t *testing.T) { p := testProvider("aws") m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -65,8 +70,10 @@ func TestContextImport_countIndex(t *testing.T) { state, err := ctx.Import(&ImportOpts{ Targets: []*ImportTarget{ &ImportTarget{ - Addr: "aws_instance.foo[0]", - ID: "bar", + Addr: addrs.RootModuleInstance.ResourceInstance( + addrs.ManagedResourceMode, "aws_instance", "foo", addrs.IntKey(0), + ), + ID: "bar", }, }, }) @@ -85,7 +92,7 @@ func TestContextImport_collision(t *testing.T) { p := testProvider("aws") m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -119,8 +126,10 @@ func TestContextImport_collision(t *testing.T) { state, err := ctx.Import(&ImportOpts{ Targets: []*ImportTarget{ &ImportTarget{ - Addr: "aws_instance.foo", - ID: "bar", + Addr: addrs.RootModuleInstance.ResourceInstance( + addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, + ), + ID: "bar", }, }, }) @@ -139,7 +148,7 @@ func TestContextImport_missingType(t *testing.T) { p := testProvider("aws") m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -156,8 +165,10 @@ func TestContextImport_missingType(t *testing.T) { state, err := ctx.Import(&ImportOpts{ Targets: []*ImportTarget{ &ImportTarget{ - Addr: "aws_instance.foo", - ID: "bar", + Addr: addrs.RootModuleInstance.ResourceInstance( + addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, + ), + ID: "bar", }, }, }) @@ -176,7 +187,7 @@ func TestContextImport_moduleProvider(t *testing.T) { p := testProvider("aws") m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -203,11 +214,13 @@ func TestContextImport_moduleProvider(t *testing.T) { } state, err := ctx.Import(&ImportOpts{ - Module: m, + Config: m, Targets: []*ImportTarget{ &ImportTarget{ - Addr: "aws_instance.foo", - ID: "bar", + Addr: addrs.RootModuleInstance.ResourceInstance( + addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, + ), + ID: "bar", }, }, }) @@ -231,7 +244,7 @@ func TestContextImport_providerModule(t *testing.T) { p := testProvider("aws") m := testModule(t, "import-provider-module") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -258,11 +271,13 @@ func TestContextImport_providerModule(t *testing.T) { } _, err := ctx.Import(&ImportOpts{ - Module: m, + Config: m, Targets: []*ImportTarget{ &ImportTarget{ - Addr: "module.child.aws_instance.foo", - ID: "bar", + Addr: addrs.RootModuleInstance.Child("child", addrs.NoKey).ResourceInstance( + addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, + ), + ID: "bar", }, }, }) @@ -281,14 +296,17 @@ func TestContextImport_providerVarConfig(t *testing.T) { p := testProvider("aws") m := testModule(t, "import-provider-vars") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "foo": "bar", + Variables: InputValues{ + "foo": &InputValue{ + Value: cty.StringVal("foo"), + SourceType: ValueFromCaller, + }, }, }) @@ -313,8 +331,10 @@ func TestContextImport_providerVarConfig(t *testing.T) { state, err := ctx.Import(&ImportOpts{ Targets: []*ImportTarget{ &ImportTarget{ - Addr: "aws_instance.foo", - ID: "bar", + Addr: addrs.RootModuleInstance.ResourceInstance( + addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, + ), + ID: "bar", }, }, }) @@ -338,7 +358,7 @@ func TestContextImport_providerNonVarConfig(t *testing.T) { p := testProvider("aws") m := testModule(t, "import-provider-non-vars") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -356,8 +376,10 @@ func TestContextImport_providerNonVarConfig(t *testing.T) { _, err := ctx.Import(&ImportOpts{ Targets: []*ImportTarget{ &ImportTarget{ - Addr: "aws_instance.foo", - ID: "bar", + Addr: addrs.RootModuleInstance.ResourceInstance( + addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, + ), + ID: "bar", }, }, }) @@ -370,7 +392,7 @@ func TestContextImport_refresh(t *testing.T) { p := testProvider("aws") m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -395,8 +417,10 @@ func TestContextImport_refresh(t *testing.T) { state, err := ctx.Import(&ImportOpts{ Targets: []*ImportTarget{ &ImportTarget{ - Addr: "aws_instance.foo", - ID: "bar", + Addr: addrs.RootModuleInstance.ResourceInstance( + addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, + ), + ID: "bar", }, }, }) @@ -415,7 +439,7 @@ func TestContextImport_refreshNil(t *testing.T) { p := testProvider("aws") m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -437,8 +461,10 @@ func TestContextImport_refreshNil(t *testing.T) { state, err := ctx.Import(&ImportOpts{ Targets: []*ImportTarget{ &ImportTarget{ - Addr: "aws_instance.foo", - ID: "bar", + Addr: addrs.RootModuleInstance.ResourceInstance( + addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, + ), + ID: "bar", }, }, }) @@ -457,7 +483,7 @@ func TestContextImport_module(t *testing.T) { p := testProvider("aws") m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -475,8 +501,10 @@ func TestContextImport_module(t *testing.T) { state, err := ctx.Import(&ImportOpts{ Targets: []*ImportTarget{ &ImportTarget{ - Addr: "module.foo.aws_instance.foo", - ID: "bar", + Addr: addrs.RootModuleInstance.Child("foo", addrs.NoKey).ResourceInstance( + addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, + ), + ID: "bar", }, }, }) @@ -495,7 +523,7 @@ func TestContextImport_moduleDepth2(t *testing.T) { p := testProvider("aws") m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -513,8 +541,10 @@ func TestContextImport_moduleDepth2(t *testing.T) { state, err := ctx.Import(&ImportOpts{ Targets: []*ImportTarget{ &ImportTarget{ - Addr: "module.a.module.b.aws_instance.foo", - ID: "bar", + Addr: addrs.RootModuleInstance.Child("a", addrs.NoKey).Child("b", addrs.NoKey).ResourceInstance( + addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, + ), + ID: "bar", }, }, }) @@ -533,7 +563,7 @@ func TestContextImport_moduleDiff(t *testing.T) { p := testProvider("aws") m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -567,8 +597,10 @@ func TestContextImport_moduleDiff(t *testing.T) { state, err := ctx.Import(&ImportOpts{ Targets: []*ImportTarget{ &ImportTarget{ - Addr: "module.foo.aws_instance.foo", - ID: "bar", + Addr: addrs.RootModuleInstance.Child("foo", addrs.NoKey).ResourceInstance( + addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, + ), + ID: "bar", }, }, }) @@ -587,7 +619,7 @@ func TestContextImport_moduleExisting(t *testing.T) { p := testProvider("aws") m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -621,8 +653,10 @@ func TestContextImport_moduleExisting(t *testing.T) { state, err := ctx.Import(&ImportOpts{ Targets: []*ImportTarget{ &ImportTarget{ - Addr: "module.foo.aws_instance.foo", - ID: "bar", + Addr: addrs.RootModuleInstance.Child("foo", addrs.NoKey).ResourceInstance( + addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, + ), + ID: "bar", }, }, }) @@ -641,7 +675,7 @@ func TestContextImport_multiState(t *testing.T) { p := testProvider("aws") m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -663,8 +697,10 @@ func TestContextImport_multiState(t *testing.T) { state, err := ctx.Import(&ImportOpts{ Targets: []*ImportTarget{ &ImportTarget{ - Addr: "aws_instance.foo", - ID: "bar", + Addr: addrs.RootModuleInstance.ResourceInstance( + addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, + ), + ID: "bar", }, }, }) @@ -683,7 +719,7 @@ func TestContextImport_multiStateSame(t *testing.T) { p := testProvider("aws") m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -709,8 +745,10 @@ func TestContextImport_multiStateSame(t *testing.T) { state, err := ctx.Import(&ImportOpts{ Targets: []*ImportTarget{ &ImportTarget{ - Addr: "aws_instance.foo", - ID: "bar", + Addr: addrs.RootModuleInstance.ResourceInstance( + addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, + ), + ID: "bar", }, }, }) @@ -730,7 +768,7 @@ func TestContextImport_customProviderMissing(t *testing.T) { p := testProvider("aws") m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -748,9 +786,11 @@ func TestContextImport_customProviderMissing(t *testing.T) { _, err := ctx.Import(&ImportOpts{ Targets: []*ImportTarget{ &ImportTarget{ - Addr: "aws_instance.foo", - ID: "bar", - Provider: "aws.alias", + Addr: addrs.RootModuleInstance.ResourceInstance( + addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, + ), + ID: "bar", + ProviderAddr: addrs.RootModuleInstance.ProviderConfigAliased("aws", "alias"), }, }, }) @@ -763,7 +803,7 @@ func TestContextImport_customProvider(t *testing.T) { p := testProvider("aws") m := testModule(t, "import-provider-alias") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -781,9 +821,11 @@ func TestContextImport_customProvider(t *testing.T) { state, err := ctx.Import(&ImportOpts{ Targets: []*ImportTarget{ &ImportTarget{ - Addr: "aws_instance.foo", - ID: "bar", - Provider: "aws.alias", + Addr: addrs.RootModuleInstance.ResourceInstance( + addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, + ), + ID: "bar", + ProviderAddr: addrs.RootModuleInstance.ProviderConfigAliased("aws", "alias"), }, }, }) diff --git a/terraform/context_input_test.go b/terraform/context_input_test.go index 0e94bf3c9..49a2ead7f 100644 --- a/terraform/context_input_test.go +++ b/terraform/context_input_test.go @@ -6,6 +6,8 @@ import ( "strings" "sync" "testing" + + "github.com/zclconf/go-cty/cty" ) func TestContext2Input(t *testing.T) { @@ -15,18 +17,23 @@ func TestContext2Input(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "foo": "us-west-2", - "amis": []map[string]interface{}{ - map[string]interface{}{ - "us-east-1": "override", - }, + Variables: InputValues{ + "foo": &InputValue{ + Value: cty.StringVal("us-west-2"), + SourceType: ValueFromCaller, + }, + "amis": &InputValue{ + Value: cty.ListVal([]cty.Value{ + cty.MapVal(map[string]cty.Value{ + "us-east-1": cty.StringVal("override"), + }), + }), }, }, UIInput: input, @@ -62,7 +69,7 @@ func TestContext2Input_moduleComputedOutputElement(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -85,7 +92,7 @@ func TestContext2Input_badVarDefault(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -109,7 +116,7 @@ func TestContext2Input_provider(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -153,7 +160,7 @@ func TestContext2Input_providerMulti(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -201,7 +208,7 @@ func TestContext2Input_providerOnce(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -240,7 +247,7 @@ func TestContext2Input_providerId(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -292,14 +299,17 @@ func TestContext2Input_providerOnly(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "foo": "us-west-2", + Variables: InputValues{ + "foo": &InputValue{ + Value: cty.StringVal("us-west-2"), + SourceType: ValueFromCaller, + }, }, UIInput: input, }) @@ -349,14 +359,17 @@ func TestContext2Input_providerVars(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "foo": "bar", + Variables: InputValues{ + "foo": &InputValue{ + Value: cty.StringVal("bar"), + SourceType: ValueFromCaller, + }, }, UIInput: input, }) @@ -399,7 +412,7 @@ func TestContext2Input_providerVarsModuleInherit(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -430,14 +443,17 @@ func TestContext2Input_varOnly(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "foo": "us-west-2", + Variables: InputValues{ + "foo": &InputValue{ + Value: cty.StringVal("us-west-2"), + SourceType: ValueFromCaller, + }, }, UIInput: input, }) @@ -487,14 +503,17 @@ func TestContext2Input_varOnlyUnset(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "foo": "foovalue", + Variables: InputValues{ + "foo": &InputValue{ + Value: cty.StringVal("foovalue"), + SourceType: ValueFromCaller, + }, }, UIInput: input, }) @@ -531,13 +550,13 @@ func TestContext2Input_varWithDefault(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{}, + Variables: InputValues{}, UIInput: input, }) @@ -580,14 +599,17 @@ func TestContext2Input_varPartiallyComputed(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "foo": "foovalue", + Variables: InputValues{ + "foo": &InputValue{ + Value: cty.StringVal("foovalue"), + SourceType: ValueFromCaller, + }, }, UIInput: input, State: &State{ @@ -645,7 +667,7 @@ func TestContext2Input_interpolateVar(t *testing.T) { p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "template": testProviderFuncFixed(p), @@ -666,13 +688,13 @@ func TestContext2Input_hcl(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "hcl": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{}, + Variables: InputValues{}, UIInput: input, }) @@ -710,7 +732,7 @@ func TestContext2Input_submoduleTriggersInvalidCount(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -762,7 +784,7 @@ func TestContext2Input_dataSourceRequiresRefresh(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "null": testProviderFuncFixed(p), diff --git a/terraform/context_plan_test.go b/terraform/context_plan_test.go index 205657461..b2641cc8b 100644 --- a/terraform/context_plan_test.go +++ b/terraform/context_plan_test.go @@ -9,6 +9,9 @@ import ( "strings" "sync" "testing" + + "github.com/hashicorp/terraform/addrs" + "github.com/zclconf/go-cty/cty" ) func TestContext2Plan_basic(t *testing.T) { @@ -16,7 +19,7 @@ func TestContext2Plan_basic(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -72,7 +75,7 @@ func TestContext2Plan_createBefore_deposed(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -108,14 +111,17 @@ func TestContext2Plan_createBefore_maintainRoot(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "in": "a,b,c", + Variables: InputValues{ + "in": &InputValue{ + Value: cty.StringVal("a,b,c"), + SourceType: ValueFromCaller, + }, }, }) @@ -153,7 +159,7 @@ func TestContext2Plan_emptyDiff(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -178,7 +184,7 @@ func TestContext2Plan_escapedVar(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -203,7 +209,7 @@ func TestContext2Plan_minimal(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -228,7 +234,7 @@ func TestContext2Plan_modules(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -254,7 +260,7 @@ func TestContext2Plan_moduleCycle(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -281,7 +287,7 @@ func TestContext2Plan_moduleDeadlock(t *testing.T) { p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -318,7 +324,7 @@ func TestContext2Plan_moduleInput(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -343,7 +349,7 @@ func TestContext2Plan_moduleInputComputed(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -368,14 +374,17 @@ func TestContext2Plan_moduleInputFromVar(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "foo": "52", + Variables: InputValues{ + "foo": &InputValue{ + Value: cty.StringVal("52"), + SourceType: ValueFromCaller, + }, }, }) @@ -396,7 +405,7 @@ func TestContext2Plan_moduleMultiVar(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -436,7 +445,7 @@ func TestContext2Plan_moduleOrphans(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -501,7 +510,7 @@ func TestContext2Plan_moduleOrphansWithProvisioner(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -550,7 +559,7 @@ func TestContext2Plan_moduleProviderInherit(t *testing.T) { m := testModule(t, "plan-module-provider-inherit") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": func() (ResourceProvider, error) { @@ -602,7 +611,7 @@ func TestContext2Plan_moduleProviderInheritDeep(t *testing.T) { m := testModule(t, "plan-module-provider-inherit-deep") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": func() (ResourceProvider, error) { @@ -649,7 +658,7 @@ func TestContext2Plan_moduleProviderDefaultsVar(t *testing.T) { m := testModule(t, "plan-module-provider-defaults-var") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": func() (ResourceProvider, error) { @@ -676,8 +685,11 @@ func TestContext2Plan_moduleProviderDefaultsVar(t *testing.T) { }, }, ), - Variables: map[string]interface{}{ - "foo": "root", + Variables: InputValues{ + "foo": &InputValue{ + Value: cty.StringVal("root"), + SourceType: ValueFromCaller, + }, }, }) @@ -701,7 +713,7 @@ func TestContext2Plan_moduleProviderVar(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -726,7 +738,7 @@ func TestContext2Plan_moduleVar(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -751,7 +763,7 @@ func TestContext2Plan_moduleVarWrongTypeBasic(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -770,7 +782,7 @@ func TestContext2Plan_moduleVarWrongTypeNested(t *testing.T) { p := testProvider("null") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "null": testProviderFuncFixed(p), @@ -789,7 +801,7 @@ func TestContext2Plan_moduleVarWithDefaultValue(t *testing.T) { p := testProvider("null") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "null": testProviderFuncFixed(p), @@ -808,7 +820,7 @@ func TestContext2Plan_moduleVarComputed(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -833,7 +845,7 @@ func TestContext2Plan_nil(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -870,7 +882,7 @@ func TestContext2Plan_preventDestroy_bad(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -907,7 +919,7 @@ func TestContext2Plan_preventDestroy_good(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -944,7 +956,7 @@ func TestContext2Plan_preventDestroy_countBad(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -987,7 +999,7 @@ func TestContext2Plan_preventDestroy_countGood(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1030,7 +1042,7 @@ func TestContext2Plan_preventDestroy_countGoodNoChange(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1071,7 +1083,7 @@ func TestContext2Plan_preventDestroy_destroyPlan(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1110,7 +1122,7 @@ func TestContext2Plan_provisionerCycle(t *testing.T) { p.DiffFn = testDiffFn pr := testProvisioner() ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1132,7 +1144,7 @@ func TestContext2Plan_computed(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1157,7 +1169,7 @@ func TestContext2Plan_computedDataResource(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1206,7 +1218,7 @@ func TestContext2Plan_computedDataCountResource(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1239,7 +1251,7 @@ func TestContext2Plan_localValueCount(t *testing.T) { p := testProvider("test") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "test": testProviderFuncFixed(p), @@ -1296,7 +1308,7 @@ func TestContext2Plan_dataSourceTypeMismatch(t *testing.T) { return nil, nil } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, // Pretend like we ran a Refresh and the AZs data source was populated. State: &State{ Modules: []*ModuleState{ @@ -1325,14 +1337,14 @@ func TestContext2Plan_dataSourceTypeMismatch(t *testing.T) { ), }) - _, err := ctx.Plan() + _, diags := ctx.Plan() - if err == nil { + if !diags.HasErrors() { t.Fatalf("Expected err, got none!") } expected := "Expected ami to be string" - if !strings.Contains(err.Error(), expected) { - t.Fatalf("expected:\n\n%s\n\nto contain:\n\n%s", err, expected) + if errStr := diags.Err().Error(); !strings.Contains(errStr, expected) { + t.Fatalf("expected:\n\n%s\n\nto contain:\n\n%s", errStr, expected) } } @@ -1367,7 +1379,7 @@ func TestContext2Plan_dataResourceBecomesComputed(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1434,7 +1446,7 @@ func TestContext2Plan_computedList(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1461,7 +1473,7 @@ func TestContext2Plan_computedMultiIndex(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1486,7 +1498,7 @@ func TestContext2Plan_count(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1515,7 +1527,7 @@ func TestContext2Plan_countComputed(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1534,7 +1546,7 @@ func TestContext2Plan_countComputedModule(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1556,7 +1568,7 @@ func TestContext2Plan_countModuleStatic(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1592,7 +1604,7 @@ func TestContext2Plan_countModuleStaticGrandchild(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1628,7 +1640,7 @@ func TestContext2Plan_countIndex(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1653,7 +1665,7 @@ func TestContext2Plan_countIndexZero(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1678,14 +1690,17 @@ func TestContext2Plan_countVar(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "count": "3", + Variables: InputValues{ + "count": &InputValue{ + Value: cty.StringVal("3"), + SourceType: ValueFromCaller, + }, }, }) @@ -1706,7 +1721,7 @@ func TestContext2Plan_countZero(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1732,7 +1747,7 @@ func TestContext2Plan_countOneIndex(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1788,7 +1803,7 @@ func TestContext2Plan_countDecreaseToOne(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1833,7 +1848,7 @@ func TestContext2Plan_countIncreaseFromNotSet(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1878,7 +1893,7 @@ func TestContext2Plan_countIncreaseFromOne(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1938,7 +1953,7 @@ func TestContext2Plan_countIncreaseFromOneCorrupted(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2017,7 +2032,7 @@ func TestContext2Plan_countIncreaseWithSplatReference(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2088,7 +2103,7 @@ func TestContext2Plan_destroy(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2145,7 +2160,7 @@ func TestContext2Plan_moduleDestroy(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2199,7 +2214,7 @@ func TestContext2Plan_moduleDestroyCycle(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2251,7 +2266,7 @@ func TestContext2Plan_moduleDestroyMultivar(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2283,7 +2298,7 @@ func TestContext2Plan_pathVar(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2291,9 +2306,9 @@ func TestContext2Plan_pathVar(t *testing.T) { ), }) - plan, err := ctx.Plan() - if err != nil { - t.Fatalf("err: %s", err) + plan, diags := ctx.Plan() + if diags.HasErrors() { + t.Fatalf("err: %s", diags.Err()) } actual := strings.TrimSpace(plan.String()) @@ -2304,8 +2319,9 @@ func TestContext2Plan_pathVar(t *testing.T) { expected = fmt.Sprintf( expected, cwd, - m.Config().Dir, - m.Config().Dir) + m.Module.SourceDir, + m.Module.SourceDir, + ) if actual != expected { t.Fatalf("bad:\n%s\n\nexpected:\n\n%s", actual, expected) @@ -2334,7 +2350,7 @@ func TestContext2Plan_diffVar(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2379,7 +2395,7 @@ func TestContext2Plan_hook(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, Hooks: []Hook{h}, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ @@ -2409,7 +2425,7 @@ func TestContext2Plan_closeProvider(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2447,7 +2463,7 @@ func TestContext2Plan_orphan(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2475,7 +2491,7 @@ func TestContext2Plan_shadowUuid(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2509,7 +2525,7 @@ func TestContext2Plan_state(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2562,7 +2578,7 @@ func TestContext2Plan_taint(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2609,7 +2625,7 @@ func TestContext2Apply_taintIgnoreChanges(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2660,7 +2676,7 @@ func TestContext2Plan_taintDestroyInterpolatedCountRace(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2702,13 +2718,17 @@ func TestContext2Plan_targeted(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Targets: []string{"aws_instance.foo"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "foo", + ), + }, }) plan, err := ctx.Plan() @@ -2760,13 +2780,15 @@ func TestContext2Plan_targetedCrossModule(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Targets: []string{"module.B"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Child("B", addrs.NoKey), + }, }) plan, err := ctx.Plan() @@ -2801,13 +2823,15 @@ func TestContext2Plan_targetedModuleWithProvider(t *testing.T) { p := testProvider("null") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "null": testProviderFuncFixed(p), }, ), - Targets: []string{"module.child2"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Child("child2", addrs.NoKey), + }, }) plan, err := ctx.Plan() @@ -2836,7 +2860,7 @@ func TestContext2Plan_targetedOrphan(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2864,7 +2888,11 @@ func TestContext2Plan_targetedOrphan(t *testing.T) { }, }, Destroy: true, - Targets: []string{"aws_instance.orphan"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "orphan", + ), + }, }) plan, err := ctx.Plan() @@ -2895,7 +2923,7 @@ func TestContext2Plan_targetedModuleOrphan(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2923,7 +2951,11 @@ func TestContext2Plan_targetedModuleOrphan(t *testing.T) { }, }, Destroy: true, - Targets: []string{"module.child.aws_instance.orphan"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Child("child", addrs.NoKey).Resource( + addrs.ManagedResourceMode, "aws_instance", "orphan", + ), + }, }) plan, err := ctx.Plan() @@ -2955,13 +2987,18 @@ func TestContext2Plan_targetedModuleUntargetedVariable(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Targets: []string{"aws_instance.blue", "module.blue_mod"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "blue", + ), + addrs.RootModuleInstance.Child("blue_mod", addrs.NoKey), + }, }) plan, err := ctx.Plan() @@ -2996,13 +3033,17 @@ func TestContext2Plan_outputContainsTargetedResource(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Targets: []string{"module.mod.aws_instance.a"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Child("mod", addrs.NoKey).Resource( + addrs.ManagedResourceMode, "aws_instance", "a", + ), + }, }) _, err := ctx.Plan() @@ -3030,7 +3071,7 @@ func TestContext2Plan_targetedOverTen(t *testing.T) { fmt.Sprintf("%s:\n ID = %s\n", key, id)) } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -3044,7 +3085,11 @@ func TestContext2Plan_targetedOverTen(t *testing.T) { }, }, }, - Targets: []string{"aws_instance.foo[1]"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.ResourceInstance( + addrs.ManagedResourceMode, "aws_instance", "foo", addrs.IntKey(1), + ), + }, }) plan, err := ctx.Plan() @@ -3105,14 +3150,17 @@ func TestContext2Plan_provider(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "foo": "bar", + Variables: InputValues{ + "foo": &InputValue{ + Value: cty.StringVal("bar"), + SourceType: ValueFromCaller, + }, }, }) @@ -3129,7 +3177,7 @@ func TestContext2Plan_varListErr(t *testing.T) { m := testModule(t, "plan-var-list-err") p := testProvider("aws") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -3165,14 +3213,17 @@ func TestContext2Plan_ignoreChanges(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "foo": "ami-1234abcd", + Variables: InputValues{ + "foo": &InputValue{ + Value: cty.StringVal("ami-1234abcd"), + SourceType: ValueFromCaller, + }, }, State: s, }) @@ -3217,15 +3268,21 @@ func TestContext2Plan_ignoreChangesWildcard(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "foo": "ami-1234abcd", - "bar": "t2.small", + Variables: InputValues{ + "foo": &InputValue{ + Value: cty.StringVal("ami-1234abcd"), + SourceType: ValueFromCaller, + }, + "bar": &InputValue{ + Value: cty.StringVal("t2.small"), + SourceType: ValueFromCaller, + }, }, State: s, }) @@ -3274,7 +3331,7 @@ func TestContext2Plan_moduleMapLiteral(t *testing.T) { return nil, nil } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -3305,7 +3362,7 @@ func TestContext2Plan_computedValueInMap(t *testing.T) { return testDiffFn(info, state, c) } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -3334,7 +3391,7 @@ func TestContext2Plan_moduleVariableFromSplat(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -3363,7 +3420,7 @@ func TestContext2Plan_createBeforeDestroy_depends_datasource(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -3403,7 +3460,7 @@ func TestContext2Plan_listOrder(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -3457,7 +3514,7 @@ func TestContext2Plan_ignoreChangesWithFlatmaps(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -3558,7 +3615,7 @@ func TestContext2Plan_resourceNestedCount(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -3638,7 +3695,7 @@ output "out" { p := testProvider("aws") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -3683,7 +3740,7 @@ resource "aws_instance" "foo" { p := testProvider("aws") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), diff --git a/terraform/context_refresh_test.go b/terraform/context_refresh_test.go index 9c4fad9a5..564e5a6ef 100644 --- a/terraform/context_refresh_test.go +++ b/terraform/context_refresh_test.go @@ -7,13 +7,15 @@ import ( "strings" "sync" "testing" + + "github.com/hashicorp/terraform/addrs" ) func TestContext2Refresh(t *testing.T) { p := testProvider("aws") m := testModule(t, "refresh-basic") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -67,7 +69,7 @@ func TestContext2Refresh_dataComputedModuleVar(t *testing.T) { p := testProvider("aws") m := testModule(t, "refresh-data-module-var") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -95,7 +97,7 @@ func TestContext2Refresh_targeted(t *testing.T) { p := testProvider("aws") m := testModule(t, "refresh-targeted") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -114,7 +116,11 @@ func TestContext2Refresh_targeted(t *testing.T) { }, }, }, - Targets: []string{"aws_instance.me"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "me", + ), + }, }) refreshedResources := make([]string, 0, 2) @@ -138,7 +144,7 @@ func TestContext2Refresh_targetedCount(t *testing.T) { p := testProvider("aws") m := testModule(t, "refresh-targeted-count") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -159,7 +165,11 @@ func TestContext2Refresh_targetedCount(t *testing.T) { }, }, }, - Targets: []string{"aws_instance.me"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "me", + ), + }, }) refreshedResources := make([]string, 0, 2) @@ -191,7 +201,7 @@ func TestContext2Refresh_targetedCountIndex(t *testing.T) { p := testProvider("aws") m := testModule(t, "refresh-targeted-count") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -212,7 +222,11 @@ func TestContext2Refresh_targetedCountIndex(t *testing.T) { }, }, }, - Targets: []string{"aws_instance.me[0]"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.ResourceInstance( + addrs.ManagedResourceMode, "aws_instance", "me", addrs.IntKey(0), + ), + }, }) refreshedResources := make([]string, 0, 2) @@ -236,7 +250,7 @@ func TestContext2Refresh_moduleComputedVar(t *testing.T) { p := testProvider("aws") m := testModule(t, "refresh-module-computed-var") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -255,7 +269,7 @@ func TestContext2Refresh_delete(t *testing.T) { p := testProvider("aws") m := testModule(t, "refresh-basic") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -296,7 +310,7 @@ func TestContext2Refresh_ignoreUncreated(t *testing.T) { p := testProvider("aws") m := testModule(t, "refresh-basic") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -324,7 +338,7 @@ func TestContext2Refresh_hook(t *testing.T) { p := testProvider("aws") m := testModule(t, "refresh-basic") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, Hooks: []Hook{h}, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ @@ -391,7 +405,7 @@ func TestContext2Refresh_modules(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -426,7 +440,7 @@ func TestContext2Refresh_moduleInputComputedOutput(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -444,7 +458,7 @@ func TestContext2Refresh_moduleVarModule(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -462,7 +476,7 @@ func TestContext2Refresh_noState(t *testing.T) { p := testProvider("aws") m := testModule(t, "refresh-no-state") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -484,7 +498,7 @@ func TestContext2Refresh_output(t *testing.T) { p := testProvider("aws") m := testModule(t, "refresh-output") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -538,7 +552,7 @@ func TestContext2Refresh_outputPartial(t *testing.T) { p := testProvider("aws") m := testModule(t, "refresh-output-partial") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -596,7 +610,7 @@ func TestContext2Refresh_stateBasic(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -679,7 +693,7 @@ func TestContext2Refresh_dataState(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "null": testProviderFuncFixed(p), @@ -751,7 +765,7 @@ func TestContext2Refresh_dataStateRefData(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "null": testProviderFuncFixed(p), @@ -795,7 +809,7 @@ func TestContext2Refresh_tainted(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -835,8 +849,8 @@ func TestContext2Refresh_unknownProvider(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn - _, err := NewContext(&ContextOpts{ - Module: m, + _, diags := NewContext(&ContextOpts{ + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{}, ), @@ -858,12 +872,12 @@ func TestContext2Refresh_unknownProvider(t *testing.T) { }, }) - if err == nil { + if !diags.HasErrors() { t.Fatal("successfully created context; want error") } - if !regexp.MustCompile(`provider ".+" is not available`).MatchString(err.Error()) { - t.Fatalf("wrong error: %s", err) + if !regexp.MustCompile(`provider ".+" is not available`).MatchString(diags.Err().Error()) { + t.Fatalf("wrong error: %s", diags.Err()) } } @@ -871,7 +885,7 @@ func TestContext2Refresh_vars(t *testing.T) { p := testProvider("aws") m := testModule(t, "refresh-vars") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1007,7 +1021,7 @@ func TestContext2Refresh_orphanModule(t *testing.T) { }, } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1034,7 +1048,7 @@ func TestContext2Validate(t *testing.T) { p := testProvider("aws") m := testModule(t, "validate-good") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1086,7 +1100,7 @@ func TestContext2Refresh_noDiffHookOnScaleOut(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, Hooks: []Hook{h}, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ @@ -1133,7 +1147,7 @@ func TestContext2Refresh_updateProviderInState(t *testing.T) { } ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), diff --git a/terraform/context_test.go b/terraform/context_test.go index 36ea61e89..e9e5b1fe8 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -6,6 +6,8 @@ import ( "testing" "time" + "github.com/hashicorp/terraform/configs" + "github.com/hashicorp/go-version" "github.com/hashicorp/terraform/flatmap" tfversion "github.com/hashicorp/terraform/version" @@ -73,16 +75,19 @@ func TestNewContextRequiredVersion(t *testing.T) { } mod := testModule(t, name) if tc.Value != "" { - mod.Config().Terraform.RequiredVersion = tc.Value + constraint, err := version.NewConstraint(tc.Value) + if err != nil { + t.Fatalf("can't parse %q as version constraint", tc.Value) + } + mod.Module.CoreVersionConstraints = append(mod.Module.CoreVersionConstraints, configs.VersionConstraint{ + Required: constraint, + }) } - _, err := NewContext(&ContextOpts{ - Module: mod, + _, diags := NewContext(&ContextOpts{ + Config: mod, }) - if (err != nil) != tc.Err { - t.Fatalf("err: %s", err) - } - if err != nil { - return + if diags.HasErrors() != tc.Err { + t.Fatalf("err: %s", diags.Err()) } }) } diff --git a/terraform/context_validate_test.go b/terraform/context_validate_test.go index 04dc7cb1a..40dfcdab4 100644 --- a/terraform/context_validate_test.go +++ b/terraform/context_validate_test.go @@ -4,13 +4,16 @@ import ( "fmt" "strings" "testing" + + "github.com/hashicorp/terraform/addrs" + "github.com/zclconf/go-cty/cty" ) func TestContext2Validate_badCount(t *testing.T) { p := testProvider("aws") m := testModule(t, "validate-bad-count") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -28,7 +31,7 @@ func TestContext2Validate_badVar(t *testing.T) { p := testProvider("aws") m := testModule(t, "validate-bad-var") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -46,14 +49,17 @@ func TestContext2Validate_varMapOverrideOld(t *testing.T) { m := testModule(t, "validate-module-pc-vars") p := testProvider("aws") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "foo.foo": "bar", + Variables: InputValues{ + "foo.foo": &InputValue{ + Value: cty.StringVal("bar"), + SourceType: ValueFromCaller, + }, }, }) @@ -66,7 +72,7 @@ func TestContext2Validate_varMapOverrideOld(t *testing.T) { func TestContext2Validate_varNoDefaultExplicitType(t *testing.T) { m := testModule(t, "validate-var-no-default-explicit-type") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, }) diags := c.Validate() @@ -79,7 +85,7 @@ func TestContext2Validate_computedVar(t *testing.T) { p := testProvider("aws") m := testModule(t, "validate-computed-var") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -113,7 +119,7 @@ func TestContext2Validate_countComputed(t *testing.T) { p := testProvider("aws") m := testModule(t, "validate-count-computed") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -131,7 +137,7 @@ func TestContext2Validate_countNegative(t *testing.T) { p := testProvider("aws") m := testModule(t, "validate-count-negative") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -149,7 +155,7 @@ func TestContext2Validate_countVariable(t *testing.T) { p := testProvider("aws") m := testModule(t, "apply-count-variable") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -167,7 +173,7 @@ func TestContext2Validate_countVariableNoDefault(t *testing.T) { p := testProvider("aws") m := testModule(t, "validate-count-variable") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -187,7 +193,7 @@ func TestContext2Validate_cycle(t *testing.T) { p := testProvider("aws") m := testModule(t, "validate-cycle") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -206,7 +212,7 @@ func TestContext2Validate_moduleBadOutput(t *testing.T) { p := testProvider("aws") m := testModule(t, "validate-bad-module-output") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -224,7 +230,7 @@ func TestContext2Validate_moduleGood(t *testing.T) { p := testProvider("aws") m := testModule(t, "validate-good-module") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -242,7 +248,7 @@ func TestContext2Validate_moduleBadResource(t *testing.T) { m := testModule(t, "validate-module-bad-rc") p := testProvider("aws") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -262,7 +268,7 @@ func TestContext2Validate_moduleDepsShouldNotCycle(t *testing.T) { m := testModule(t, "validate-module-deps-cycle") p := testProvider("aws") ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -280,7 +286,7 @@ func TestContext2Validate_moduleProviderInheritOrphan(t *testing.T) { m := testModule(t, "validate-module-pc-inherit-orphan") p := testProvider("aws") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -325,14 +331,17 @@ func TestContext2Validate_moduleProviderVar(t *testing.T) { m := testModule(t, "validate-module-pc-vars") p := testProvider("aws") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "provider_var": "bar", + Variables: InputValues{ + "provider_var": &InputValue{ + Value: cty.StringVal("bar"), + SourceType: ValueFromCaller, + }, }, }) @@ -350,7 +359,7 @@ func TestContext2Validate_moduleProviderInheritUnused(t *testing.T) { m := testModule(t, "validate-module-pc-inherit-unused") p := testProvider("aws") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -387,7 +396,7 @@ func TestContext2Validate_orphans(t *testing.T) { }, } c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -411,7 +420,7 @@ func TestContext2Validate_providerConfig_bad(t *testing.T) { m := testModule(t, "validate-bad-pc") p := testProvider("aws") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -434,7 +443,7 @@ func TestContext2Validate_providerConfig_badEmpty(t *testing.T) { m := testModule(t, "validate-bad-pc-empty") p := testProvider("aws") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -454,7 +463,7 @@ func TestContext2Validate_providerConfig_good(t *testing.T) { m := testModule(t, "validate-bad-pc") p := testProvider("aws") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -473,7 +482,7 @@ func TestContext2Validate_provisionerConfig_bad(t *testing.T) { p := testProvider("aws") pr := testProvisioner() c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -503,7 +512,7 @@ func TestContext2Validate_provisionerConfig_good(t *testing.T) { return nil, c.CheckSet([]string{"command"}) } c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -524,7 +533,7 @@ func TestContext2Validate_requiredVar(t *testing.T) { m := testModule(t, "validate-required-var") p := testProvider("aws") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -542,7 +551,7 @@ func TestContext2Validate_resourceConfig_bad(t *testing.T) { m := testModule(t, "validate-bad-rc") p := testProvider("aws") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -562,7 +571,7 @@ func TestContext2Validate_resourceConfig_good(t *testing.T) { m := testModule(t, "validate-bad-rc") p := testProvider("aws") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -580,7 +589,7 @@ func TestContext2Validate_resourceNameSymbol(t *testing.T) { p := testProvider("aws") m := testModule(t, "validate-resource-name-symbol") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -598,7 +607,7 @@ func TestContext2Validate_selfRef(t *testing.T) { p := testProvider("aws") m := testModule(t, "validate-self-ref") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -616,7 +625,7 @@ func TestContext2Validate_selfRefMulti(t *testing.T) { p := testProvider("aws") m := testModule(t, "validate-self-ref-multi") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -634,7 +643,7 @@ func TestContext2Validate_selfRefMultiAll(t *testing.T) { p := testProvider("aws") m := testModule(t, "validate-self-ref-multi-all") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -668,7 +677,7 @@ func TestContext2Validate_tainted(t *testing.T) { }, } c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -695,7 +704,7 @@ func TestContext2Validate_targetedDestroy(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -715,7 +724,11 @@ func TestContext2Validate_targetedDestroy(t *testing.T) { }, }, }, - Targets: []string{"aws_instance.foo"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "foo", + ), + }, Destroy: true, }) @@ -729,14 +742,17 @@ func TestContext2Validate_varRefFilled(t *testing.T) { m := testModule(t, "validate-variable-ref") p := testProvider("aws") c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "foo": "bar", + Variables: InputValues{ + "foo": &InputValue{ + Value: cty.StringVal("bar"), + SourceType: ValueFromCaller, + }, }, }) @@ -763,7 +779,7 @@ func TestContext2Validate_interpolateVar(t *testing.T) { p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "template": testProviderFuncFixed(p), @@ -789,7 +805,7 @@ func TestContext2Validate_interpolateComputedModuleVarDef(t *testing.T) { p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -814,7 +830,7 @@ func TestContext2Validate_interpolateMap(t *testing.T) { p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "template": testProviderFuncFixed(p), @@ -836,43 +852,58 @@ func TestContext2Validate_PlanGraphBuilder(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn c := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, ), - Variables: map[string]interface{}{ - "foo": "us-west-2", - "test_list": []interface{}{"Hello", "World"}, - "test_map": map[string]interface{}{ - "Hello": "World", - "Foo": "Bar", - "Baz": "Foo", + Variables: InputValues{ + "foo": &InputValue{ + Value: cty.StringVal("us-west-2"), + SourceType: ValueFromCaller, }, - "amis": []map[string]interface{}{ - map[string]interface{}{ - "us-east-1": "override", - }, + "test_list": &InputValue{ + Value: cty.ListVal([]cty.Value{ + cty.StringVal("Hello"), + cty.StringVal("World"), + }), + SourceType: ValueFromCaller, + }, + "test_map": &InputValue{ + Value: cty.MapVal(map[string]cty.Value{ + "Hello": cty.StringVal("World"), + "Foo": cty.StringVal("Bar"), + "Baz": cty.StringVal("Foo"), + }), + SourceType: ValueFromCaller, + }, + "amis": &InputValue{ + Value: cty.ListVal([]cty.Value{ + cty.MapVal(map[string]cty.Value{ + "us-east-1": cty.StringVal("override"), + }), + }), + SourceType: ValueFromCaller, }, }, }) - graph, err := (&PlanGraphBuilder{ - Module: c.module, - State: NewState(), - Providers: c.components.ResourceProviders(), - Targets: c.targets, - }).Build(RootModulePath) - if err != nil { - t.Fatalf("error attmepting to Build PlanGraphBuilder: %s", err) + graph, diags := (&PlanGraphBuilder{ + Config: c.config, + State: NewState(), + Components: c.components, + Targets: c.targets, + }).Build(addrs.RootModuleInstance) + if diags.HasErrors() { + t.Fatalf("errors from PlanGraphBuilder: %s", diags.Err()) } defer c.acquireRun("validate-test")() - walker, err := c.walk(graph, walkValidate) - if err != nil { - t.Fatal(err) + walker, diags := c.walk(graph, walkValidate) + if diags.HasErrors() { + t.Fatal(diags.Err()) } - if len(walker.ValidationErrors) > 0 { - t.Fatal(walker.ValidationErrors) + if len(walker.NonFatalDiagnostics) > 0 { + t.Fatal(walker.NonFatalDiagnostics.Err()) } } diff --git a/terraform/debug_test.go b/terraform/debug_test.go index 6e75116cd..b098f4260 100644 --- a/terraform/debug_test.go +++ b/terraform/debug_test.go @@ -97,7 +97,7 @@ func TestDebug_plan(t *testing.T) { p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext2(t, &ContextOpts{ - Module: m, + Config: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -105,9 +105,9 @@ func TestDebug_plan(t *testing.T) { ), }) - _, err = ctx.Plan() - if err != nil { - t.Fatalf("err: %s", err) + _, diags := ctx.Plan() + if diags.HasErrors() { + t.Fatalf("err: %s", diags.Err()) } err = CloseDebugInfo() diff --git a/terraform/diff_test.go b/terraform/diff_test.go index d88f383c5..b2ace77f4 100644 --- a/terraform/diff_test.go +++ b/terraform/diff_test.go @@ -5,6 +5,8 @@ import ( "reflect" "strings" "testing" + + "github.com/hashicorp/terraform/addrs" ) func TestDiffEmpty(t *testing.T) { @@ -18,7 +20,7 @@ func TestDiffEmpty(t *testing.T) { t.Fatal("should be empty") } - mod := diff.AddModule(rootModulePath) + mod := diff.AddModule(addrs.RootModuleInstance) mod.Resources["nodeA"] = &InstanceDiff{ Attributes: map[string]*ResourceAttrDiff{ "foo": &ResourceAttrDiff{ @@ -36,7 +38,7 @@ func TestDiffEmpty(t *testing.T) { func TestDiffEmpty_taintedIsNotEmpty(t *testing.T) { diff := new(Diff) - mod := diff.AddModule(rootModulePath) + mod := diff.AddModule(addrs.RootModuleInstance) mod.Resources["nodeA"] = &InstanceDiff{ DestroyTainted: true, } diff --git a/terraform/eval_context_builtin_test.go b/terraform/eval_context_builtin_test.go index e1ab95268..2cb130511 100644 --- a/terraform/eval_context_builtin_test.go +++ b/terraform/eval_context_builtin_test.go @@ -5,32 +5,37 @@ import ( "sync" "testing" + "github.com/hashicorp/terraform/addrs" + "github.com/zclconf/go-cty/cty" + "github.com/davecgh/go-spew/spew" "github.com/hashicorp/terraform/config/configschema" ) func TestBuiltinEvalContextProviderInput(t *testing.T) { var lock sync.Mutex - cache := make(map[string]map[string]interface{}) + cache := make(map[string]map[string]cty.Value) ctx1 := testBuiltinEvalContext(t) - ctx1.PathValue = []string{"root"} + ctx1.PathValue = addrs.RootModuleInstance ctx1.ProviderInputConfig = cache ctx1.ProviderLock = &lock ctx2 := testBuiltinEvalContext(t) - ctx2.PathValue = []string{"root", "child"} + ctx2.PathValue = addrs.RootModuleInstance.Child("child", addrs.NoKey) ctx2.ProviderInputConfig = cache ctx2.ProviderLock = &lock - expected1 := map[string]interface{}{"value": "foo"} - ctx1.SetProviderInput("foo", expected1) + providerAddr := addrs.ProviderConfig{Type: "foo"} - expected2 := map[string]interface{}{"value": "bar"} - ctx2.SetProviderInput("foo", expected2) + expected1 := map[string]cty.Value{"value": cty.StringVal("foo")} + ctx1.SetProviderInput(providerAddr, expected1) - actual1 := ctx1.ProviderInput("foo") - actual2 := ctx2.ProviderInput("foo") + expected2 := map[string]cty.Value{"value": cty.StringVal("bar")} + ctx2.SetProviderInput(providerAddr, expected2) + + actual1 := ctx1.ProviderInput(providerAddr) + actual2 := ctx2.ProviderInput(providerAddr) if !reflect.DeepEqual(actual1, expected1) { t.Fatalf("bad: %#v %#v", actual1, expected1) @@ -76,11 +81,14 @@ func TestBuildingEvalContextInitProvider(t *testing.T) { }, } - _, err := ctx.InitProvider("test", "test") + providerAddrDefault := addrs.ProviderConfig{Type: "test"} + providerAddrAlias := addrs.ProviderConfig{Type: "test", Alias: "foo"} + + _, err := ctx.InitProvider("test", providerAddrDefault) if err != nil { t.Fatalf("error initializing provider test: %s", err) } - _, err = ctx.InitProvider("test", "test.foo") + _, err = ctx.InitProvider("test", providerAddrAlias) if err != nil { t.Fatalf("error initializing provider test.foo: %s", err) } @@ -98,14 +106,14 @@ func TestBuildingEvalContextInitProvider(t *testing.T) { } { - schema := ctx.ProviderSchema("test") + schema := ctx.ProviderSchema(providerAddrDefault.Absolute(addrs.RootModuleInstance)) if got, want := schema, testP.GetSchemaReturn; !reflect.DeepEqual(got, want) { t.Errorf("wrong schema\ngot: %swant: %s", spew.Sdump(got), spew.Sdump(want)) } } { - schema := ctx.ProviderSchema("test.foo") + schema := ctx.ProviderSchema(providerAddrAlias.Absolute(addrs.RootModuleInstance)) if got, want := schema, testP.GetSchemaReturn; !reflect.DeepEqual(got, want) { t.Errorf("wrong schema\ngot: %swant: %s", spew.Sdump(got), spew.Sdump(want)) } diff --git a/terraform/eval_diff_test.go b/terraform/eval_diff_test.go index b1f76ea3d..ab71dc5c9 100644 --- a/terraform/eval_diff_test.go +++ b/terraform/eval_diff_test.go @@ -5,7 +5,10 @@ import ( "reflect" "testing" - "github.com/hashicorp/terraform/config" + "github.com/hashicorp/hcl2/hcl/hclsyntax" + + "github.com/hashicorp/hcl2/hcl" + "github.com/hashicorp/terraform/configs" ) func TestEvalFilterDiff(t *testing.T) { @@ -85,10 +88,19 @@ func TestProcessIgnoreChanges(t *testing.T) { var instanceDiff *InstanceDiff var testDiffs = func(ignoreChanges []string, newAttribute string) (*EvalDiff, *InstanceDiff) { + ignoreChangesTravs := make([]hcl.Traversal, len(ignoreChanges)) + for i, s := range ignoreChanges { + traversal, travDiags := hclsyntax.ParseTraversalAbs([]byte(s), "", hcl.Pos{Line: 1, Column: 1}) + if travDiags.HasErrors() { + t.Fatal(travDiags.Error()) + } + ignoreChangesTravs[i] = traversal + } + return &EvalDiff{ - Resource: &config.Resource{ - Lifecycle: config.ResourceLifecycle{ - IgnoreChanges: ignoreChanges, + Config: &configs.Resource{ + Managed: &configs.ManagedResource{ + IgnoreChanges: ignoreChangesTravs, }, }, }, diff --git a/terraform/eval_local_test.go b/terraform/eval_local_test.go index 47803be5d..4e644b917 100644 --- a/terraform/eval_local_test.go +++ b/terraform/eval_local_test.go @@ -5,8 +5,14 @@ import ( "sync" "testing" + "github.com/hashicorp/terraform/config/hcl2shim" + + "github.com/hashicorp/hcl2/hcl" + "github.com/hashicorp/hcl2/hcl/hclsyntax" + + "github.com/hashicorp/terraform/addrs" + "github.com/davecgh/go-spew/spew" - "github.com/hashicorp/terraform/config" ) func TestEvalLocal_impl(t *testing.T) { @@ -33,27 +39,23 @@ func TestEvalLocal(t *testing.T) { for _, test := range tests { t.Run(test.Value, func(t *testing.T) { - rawConfig, err := config.NewRawConfig(map[string]interface{}{ - "value": test.Value, - }) - if err != nil { - t.Fatal(err) + expr, diags := hclsyntax.ParseTemplate([]byte(test.Value), "", hcl.Pos{Line: 1, Column: 1}) + if diags.HasErrors() { + t.Fatal(diags.Error()) } n := &EvalLocal{ - Name: "foo", - Value: rawConfig, + Addr: addrs.LocalValue{Name: "foo"}, + Expr: expr, } ctx := &MockEvalContext{ StateState: &State{}, StateLock: &sync.RWMutex{}, - InterpolateConfigResult: testResourceConfig(t, map[string]interface{}{ - "value": test.Want, - }), + EvaluateExprResult: hcl2shim.HCL2ValueFromConfigValue(test.Want), } - _, err = n.Eval(ctx) + _, err := n.Eval(ctx) if (err != nil) != test.Err { if err != nil { t.Errorf("unexpected error: %s", err) @@ -62,7 +64,7 @@ func TestEvalLocal(t *testing.T) { } } - ms := ctx.StateState.ModuleByPath([]string{}) + ms := ctx.StateState.ModuleByPath(addrs.RootModuleInstance) gotLocals := ms.Locals wantLocals := map[string]interface{}{ "foo": test.Want, diff --git a/terraform/eval_output_test.go b/terraform/eval_output_test.go index f73b127de..87d3fe5a3 100644 --- a/terraform/eval_output_test.go +++ b/terraform/eval_output_test.go @@ -3,6 +3,9 @@ package terraform import ( "sync" "testing" + + "github.com/hashicorp/terraform/addrs" + "github.com/zclconf/go-cty/cty" ) func TestEvalWriteMapOutput(t *testing.T) { @@ -12,40 +15,38 @@ func TestEvalWriteMapOutput(t *testing.T) { cases := []struct { name string - cfg *ResourceConfig + val cty.Value err bool }{ { // Eval should recognize a single map in a slice, and collapse it // into the map value "single-map", - &ResourceConfig{ - Config: map[string]interface{}{ - "value": []map[string]interface{}{ - map[string]interface{}{"a": "b"}, - }, - }, - }, + cty.MapVal(map[string]cty.Value{ + "a": cty.StringVal("b"), + }), false, }, { // we can't apply a multi-valued map to a variable, so this should error "multi-map", - &ResourceConfig{ - Config: map[string]interface{}{ - "value": []map[string]interface{}{ - map[string]interface{}{"a": "b"}, - map[string]interface{}{"c": "d"}, - }, - }, - }, + cty.ListVal([]cty.Value{ + cty.MapVal(map[string]cty.Value{ + "a": cty.StringVal("b"), + }), + cty.MapVal(map[string]cty.Value{ + "c": cty.StringVal("d"), + }), + }), true, }, } for _, tc := range cases { - evalNode := &EvalWriteOutput{Name: tc.name} - ctx.InterpolateConfigResult = tc.cfg + evalNode := &EvalWriteOutput{ + Addr: addrs.OutputValue{Name: tc.name}, + } + ctx.EvaluateExprResult = tc.val t.Run(tc.name, func(t *testing.T) { _, err := evalNode.Eval(ctx) if err != nil && !tc.err { diff --git a/terraform/eval_provider_test.go b/terraform/eval_provider_test.go index 8722b3e51..720d65c4c 100644 --- a/terraform/eval_provider_test.go +++ b/terraform/eval_provider_test.go @@ -4,46 +4,41 @@ import ( "reflect" "testing" + "github.com/hashicorp/terraform/configs" + "github.com/zclconf/go-cty/cty" + + "github.com/hashicorp/terraform/addrs" + "github.com/hashicorp/terraform/config" ) -func TestEvalBuildProviderConfig_impl(t *testing.T) { - var _ EvalNode = new(EvalBuildProviderConfig) -} - -func TestEvalBuildProviderConfig(t *testing.T) { - config := testResourceConfig(t, map[string]interface{}{ - "set_in_config": "config", - "set_in_config_and_parent": "config", - "computed_in_config": "config", +func TestBuildProviderConfig(t *testing.T) { + configBody := configs.SynthBody("", map[string]cty.Value{ + "set_in_config": cty.StringVal("config"), + "set_in_config_and_parent": cty.StringVal("config"), + "computed_in_config": cty.StringVal("config"), }) - provider := "foo" - - n := &EvalBuildProviderConfig{ - Provider: provider, - Config: &config, - Output: &config, + providerAddr := addrs.ProviderConfig{ + Type: "foo", } ctx := &MockEvalContext{ - ProviderInputConfig: map[string]interface{}{ - "set_in_config": "input", - "set_by_input": "input", + ProviderInputValues: map[string]cty.Value{ + "set_in_config": cty.StringVal("input"), + "set_by_input": cty.StringVal("input"), }, } - if _, err := n.Eval(ctx); err != nil { - t.Fatalf("err: %s", err) - } + got := buildProviderConfig(ctx, providerAddr, configBody) // We expect the provider config with the added input value - expected := map[string]interface{}{ - "set_in_config": "config", - "set_in_config_and_parent": "config", - "computed_in_config": "config", - "set_by_input": "input", + want := map[string]cty.Value{ + "set_in_config": cty.StringVal("config"), + "set_in_config_and_parent": cty.StringVal("config"), + "computed_in_config": cty.StringVal("config"), + "set_by_input": cty.StringVal("input"), } - if !reflect.DeepEqual(config.Raw, expected) { - t.Fatalf("incorrect merged config:\n%#v\nwanted:\n%#v", config.Raw, expected) + if !reflect.DeepEqual(got, want) { + t.Fatalf("incorrect merged config\ngot: %#v\nwant: \n%#v", got, want) } } @@ -52,9 +47,11 @@ func TestEvalConfigProvider_impl(t *testing.T) { } func TestEvalConfigProvider(t *testing.T) { - config := testResourceConfig(t, map[string]interface{}{}) + config := &configs.Provider{ + Name: "foo", + } provider := &MockResourceProvider{} - n := &EvalConfigProvider{Config: &config} + n := &EvalConfigProvider{Config: config} ctx := &MockEvalContext{ProviderProvider: provider} if _, err := n.Eval(ctx); err != nil { @@ -74,7 +71,9 @@ func TestEvalInitProvider_impl(t *testing.T) { } func TestEvalInitProvider(t *testing.T) { - n := &EvalInitProvider{Name: "foo"} + n := &EvalInitProvider{ + Addr: addrs.ProviderConfig{Type: "foo"}, + } provider := &MockResourceProvider{} ctx := &MockEvalContext{InitProviderProvider: provider} if _, err := n.Eval(ctx); err != nil { @@ -84,14 +83,15 @@ func TestEvalInitProvider(t *testing.T) { if !ctx.InitProviderCalled { t.Fatal("should be called") } - if ctx.InitProviderName != "foo" { - t.Fatalf("bad: %#v", ctx.InitProviderName) + if ctx.InitProviderAddr.String() != "provider.foo" { + t.Fatalf("wrong provider address %s", ctx.InitProviderAddr) } } func TestEvalCloseProvider(t *testing.T) { - providerName := ResolveProviderName("foo", nil) - n := &EvalCloseProvider{Name: providerName} + n := &EvalCloseProvider{ + Addr: addrs.ProviderConfig{Type: "foo"}, + } provider := &MockResourceProvider{} ctx := &MockEvalContext{CloseProviderProvider: provider} if _, err := n.Eval(ctx); err != nil { @@ -101,8 +101,8 @@ func TestEvalCloseProvider(t *testing.T) { if !ctx.CloseProviderCalled { t.Fatal("should be called") } - if ctx.CloseProviderName != providerName { - t.Fatalf("bad: %#v", ctx.CloseProviderName) + if ctx.CloseProviderAddr.String() != "provider.foo" { + t.Fatalf("wrong provider address %s", ctx.CloseProviderAddr) } } @@ -112,7 +112,10 @@ func TestEvalGetProvider_impl(t *testing.T) { func TestEvalGetProvider(t *testing.T) { var actual ResourceProvider - n := &EvalGetProvider{Name: "foo", Output: &actual} + n := &EvalGetProvider{ + Addr: addrs.RootModuleInstance.ProviderConfigDefault("foo"), + Output: &actual, + } provider := &MockResourceProvider{} ctx := &MockEvalContext{ProviderProvider: provider} if _, err := n.Eval(ctx); err != nil { @@ -125,8 +128,8 @@ func TestEvalGetProvider(t *testing.T) { if !ctx.ProviderCalled { t.Fatal("should be called") } - if ctx.ProviderName != "foo" { - t.Fatalf("bad: %#v", ctx.ProviderName) + if ctx.ProviderAddr.String() != "provider.foo" { + t.Fatalf("wrong provider address %s", ctx.ProviderAddr) } } @@ -150,19 +153,18 @@ func TestEvalInputProvider(t *testing.T) { }, } ctx := &MockEvalContext{ProviderProvider: provider} - rawConfig, err := config.NewRawConfig(map[string]interface{}{ - "mock_config": "mock", - "set_in_config": "input", - }) - if err != nil { - t.Fatalf("NewRawConfig failed: %s", err) + config := &configs.Provider{ + Name: "foo", + Config: configs.SynthBody("synth", map[string]cty.Value{ + "mock_config": cty.StringVal("mock"), + "set_in_config": cty.StringVal("input"), + }), } - config := NewResourceConfig(rawConfig) n := &EvalInputProvider{ - Name: "mock", + Addr: addrs.ProviderConfig{Type: "foo"}, Provider: &provider, - Config: &config, + Config: config, } result, err := n.Eval(ctx) @@ -177,15 +179,15 @@ func TestEvalInputProvider(t *testing.T) { t.Fatalf("ctx.SetProviderInput wasn't called") } - if got, want := ctx.SetProviderInputName, "mock"; got != want { + if got, want := ctx.SetProviderInputAddr.String(), "provider.mock"; got != want { t.Errorf("wrong provider name %q; want %q", got, want) } - inputCfg := ctx.SetProviderInputConfig + inputCfg := ctx.SetProviderInputValues // we should only have the value that was set during Input - want := map[string]interface{}{ - "set_by_input": "input", + want := map[string]cty.Value{ + "set_by_input": cty.StringVal("input"), } if !reflect.DeepEqual(inputCfg, want) { t.Errorf("got incorrect input config:\n%#v\nwant:\n%#v", inputCfg, want) diff --git a/terraform/eval_state_test.go b/terraform/eval_state_test.go index e702634e0..4820ac2c0 100644 --- a/terraform/eval_state_test.go +++ b/terraform/eval_state_test.go @@ -3,6 +3,8 @@ package terraform import ( "sync" "testing" + + "github.com/hashicorp/terraform/addrs" ) func TestEvalRequireState(t *testing.T) { @@ -116,7 +118,7 @@ func TestEvalReadState(t *testing.T) { }, } ctx.StateLock = new(sync.RWMutex) - ctx.PathPath = rootModulePath + ctx.PathPath = addrs.RootModuleInstance result, err := c.Node.Eval(ctx) if err != nil { @@ -141,7 +143,7 @@ func TestEvalWriteState(t *testing.T) { ctx := new(MockEvalContext) ctx.StateState = state ctx.StateLock = new(sync.RWMutex) - ctx.PathPath = rootModulePath + ctx.PathPath = addrs.RootModuleInstance is := &InstanceState{ID: "i-abc123"} node := &EvalWriteState{ @@ -165,7 +167,7 @@ func TestEvalWriteStateDeposed(t *testing.T) { ctx := new(MockEvalContext) ctx.StateState = state ctx.StateLock = new(sync.RWMutex) - ctx.PathPath = rootModulePath + ctx.PathPath = addrs.RootModuleInstance is := &InstanceState{ID: "i-abc123"} node := &EvalWriteStateDeposed{ diff --git a/terraform/eval_validate_selfref_test.go b/terraform/eval_validate_selfref_test.go index f5e70cb66..c44cf9086 100644 --- a/terraform/eval_validate_selfref_test.go +++ b/terraform/eval_validate_selfref_test.go @@ -4,94 +4,88 @@ import ( "fmt" "testing" - "github.com/hashicorp/terraform/config" + "github.com/hashicorp/hcl2/hcl" + "github.com/hashicorp/hcl2/hcltest" + "github.com/hashicorp/terraform/addrs" + "github.com/zclconf/go-cty/cty" ) -func TestEvalValidateResourceSelfRef(t *testing.T) { - cases := []struct { - Name string - Addr string - Config map[string]interface{} - Err bool +func TestEvalValidateSelfRef(t *testing.T) { + rAddr := addrs.Resource{ + Mode: addrs.ManagedResourceMode, + Type: "aws_instance", + Name: "foo", + } + + tests := []struct { + Name string + Addr addrs.Referenceable + Expr hcl.Expression + Err bool }{ { - "no interpolations", - "aws_instance.foo", - map[string]interface{}{ - "foo": "bar", - }, + "no references at all", + rAddr, + hcltest.MockExprLiteral(cty.StringVal("bar")), false, }, { "non self reference", - "aws_instance.foo", - map[string]interface{}{ - "foo": "${aws_instance.bar.id}", - }, + rAddr, + hcltest.MockExprTraversalSrc("aws_instance.bar.id"), false, }, { "self reference", - "aws_instance.foo", - map[string]interface{}{ - "foo": "hello ${aws_instance.foo.id}", - }, + rAddr, + hcltest.MockExprTraversalSrc("aws_instance.foo.id"), true, }, { "self reference other index", - "aws_instance.foo", - map[string]interface{}{ - "foo": "hello ${aws_instance.foo.4.id}", - }, + rAddr, + hcltest.MockExprTraversalSrc("aws_instance.foo[4].id"), false, }, { "self reference same index", - "aws_instance.foo[4]", - map[string]interface{}{ - "foo": "hello ${aws_instance.foo.4.id}", - }, + rAddr.Instance(addrs.IntKey(4)), + hcltest.MockExprTraversalSrc("aws_instance.foo[4].id"), true, }, { - "self reference multi", - "aws_instance.foo[4]", - map[string]interface{}{ - "foo": "hello ${aws_instance.foo.*.id}", - }, - true, - }, - - { - "self reference multi single", - "aws_instance.foo", - map[string]interface{}{ - "foo": "hello ${aws_instance.foo.*.id}", - }, + "self reference whole", + rAddr.Instance(addrs.IntKey(4)), + hcltest.MockExprTraversalSrc("aws_instance.foo"), true, }, } - for i, tc := range cases { - t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) { - addr, err := ParseResourceAddress(tc.Addr) - if err != nil { - t.Fatalf("err: %s", err) - } - conf := config.TestRawConfig(t, tc.Config) + for i, test := range tests { + t.Run(fmt.Sprintf("%d-%s", i, test.Name), func(t *testing.T) { + body := hcltest.MockBody(&hcl.BodyContent{ + Attributes: hcl.Attributes{ + "foo": { + Name: "foo", + Expr: test.Expr, + }, + }, + }) - n := &EvalValidateResourceSelfRef{Addr: &addr, Config: &conf} + n := &EvalValidateSelfRef{ + Addr: test.Addr, + Config: body, + } result, err := n.Eval(nil) if result != nil { t.Fatal("result should always be nil") } - if (err != nil) != tc.Err { + if (err != nil) != test.Err { t.Fatalf("err: %s", err) } }) diff --git a/terraform/eval_validate_test.go b/terraform/eval_validate_test.go index 0bde2a870..6527a790b 100644 --- a/terraform/eval_validate_test.go +++ b/terraform/eval_validate_test.go @@ -5,7 +5,14 @@ import ( "strings" "testing" - "github.com/hashicorp/terraform/config" + "github.com/hashicorp/hcl2/hcl" + "github.com/hashicorp/terraform/config/configschema" + "github.com/hashicorp/terraform/tfdiags" + + "github.com/hashicorp/terraform/configs" + "github.com/zclconf/go-cty/cty" + + "github.com/hashicorp/terraform/addrs" ) func TestEvalValidateResource_managedResource(t *testing.T) { @@ -24,13 +31,24 @@ func TestEvalValidateResource_managedResource(t *testing.T) { } p := ResourceProvider(mp) - rc := testResourceConfig(t, map[string]interface{}{"foo": "bar"}) + rc := &configs.Resource{ + Mode: addrs.ManagedResourceMode, + Type: "aws_instance", + Name: "foo", + Config: configs.SynthBody("", map[string]cty.Value{ + "foo": cty.StringVal("bar"), + }), + } node := &EvalValidateResource{ - Provider: &p, - Config: &rc, - ResourceName: "foo", - ResourceType: "aws_instance", - ResourceMode: config.ManagedResourceMode, + Addr: addrs.ResourceInstance{ + Resource: addrs.Resource{ + Mode: addrs.ManagedResourceMode, + Type: "aws_instance", + Name: "foo", + }, + }, + Provider: &p, + Config: rc, } _, err := node.Eval(&MockEvalContext{}) @@ -59,13 +77,25 @@ func TestEvalValidateResource_dataSource(t *testing.T) { } p := ResourceProvider(mp) - rc := testResourceConfig(t, map[string]interface{}{"foo": "bar"}) + rc := &configs.Resource{ + Mode: addrs.DataResourceMode, + Type: "aws_ami", + Name: "foo", + Config: configs.SynthBody("", map[string]cty.Value{ + "foo": cty.StringVal("bar"), + }), + } + node := &EvalValidateResource{ - Provider: &p, - Config: &rc, - ResourceName: "foo", - ResourceType: "aws_ami", - ResourceMode: config.DataResourceMode, + Addr: addrs.ResourceInstance{ + Resource: addrs.Resource{ + Mode: addrs.DataResourceMode, + Type: "aws_ami", + Name: "foo", + }, + }, + Provider: &p, + Config: rc, } _, err := node.Eval(&MockEvalContext{}) @@ -85,13 +115,22 @@ func TestEvalValidateResource_validReturnsNilError(t *testing.T) { } p := ResourceProvider(mp) - rc := &ResourceConfig{} + rc := &configs.Resource{ + Mode: addrs.ManagedResourceMode, + Type: "aws_instance", + Name: "foo", + Config: configs.SynthBody("", map[string]cty.Value{}), + } node := &EvalValidateResource{ - Provider: &p, - Config: &rc, - ResourceName: "foo", - ResourceType: "aws_instance", - ResourceMode: config.ManagedResourceMode, + Addr: addrs.ResourceInstance{ + Resource: addrs.Resource{ + Mode: addrs.ManagedResourceMode, + Type: "aws_instance", + Name: "foo", + }, + }, + Provider: &p, + Config: rc, } _, err := node.Eval(&MockEvalContext{}) @@ -109,13 +148,22 @@ func TestEvalValidateResource_warningsAndErrorsPassedThrough(t *testing.T) { } p := ResourceProvider(mp) - rc := &ResourceConfig{} + rc := &configs.Resource{ + Mode: addrs.ManagedResourceMode, + Type: "aws_instance", + Name: "foo", + Config: configs.SynthBody("", map[string]cty.Value{}), + } node := &EvalValidateResource{ - Provider: &p, - Config: &rc, - ResourceName: "foo", - ResourceType: "aws_instance", - ResourceMode: config.ManagedResourceMode, + Addr: addrs.ResourceInstance{ + Resource: addrs.Resource{ + Mode: addrs.ManagedResourceMode, + Type: "aws_instance", + Name: "foo", + }, + }, + Provider: &p, + Config: rc, } _, err := node.Eval(&MockEvalContext{}) @@ -123,34 +171,17 @@ func TestEvalValidateResource_warningsAndErrorsPassedThrough(t *testing.T) { t.Fatal("Expected an error, got none!") } - verr := err.(*EvalValidateError) - if len(verr.Warnings) != 1 || verr.Warnings[0] != "warn" { - t.Fatalf("Expected 1 warning 'warn', got: %#v", verr.Warnings) + var diags tfdiags.Diagnostics + diags = diags.Append(err) + bySeverity := map[tfdiags.Severity]tfdiags.Diagnostics{} + for _, diag := range diags { + bySeverity[diag.Severity()] = append(bySeverity[diag.Severity()], diag) } - if len(verr.Errors) != 1 || verr.Errors[0].Error() != "err" { - t.Fatalf("Expected 1 error 'err', got: %#v", verr.Errors) + if len(bySeverity[tfdiags.Warning]) != 1 || bySeverity[tfdiags.Warning][0].Description().Summary != "warn" { + t.Fatalf("Expected 1 warning 'warn', got: %#v", bySeverity[tfdiags.Warning]) } -} - -func TestEvalValidateResource_checksResourceName(t *testing.T) { - mp := testProvider("aws") - p := ResourceProvider(mp) - rc := &ResourceConfig{} - node := &EvalValidateResource{ - Provider: &p, - Config: &rc, - ResourceName: "bad*name", - ResourceType: "aws_instance", - ResourceMode: config.ManagedResourceMode, - } - - _, err := node.Eval(&MockEvalContext{}) - if err == nil { - t.Fatal("Expected an error, got none!") - } - expectErr := "resource name can only contain" - if !strings.Contains(err.Error(), expectErr) { - t.Fatalf("Expected err: %s to contain %s", err, expectErr) + if len(bySeverity[tfdiags.Error]) != 1 || bySeverity[tfdiags.Error][0].Description().Summary != "err" { + t.Fatalf("Expected 1 error 'err', got: %#v", bySeverity[tfdiags.Error]) } } @@ -162,13 +193,22 @@ func TestEvalValidateResource_ignoreWarnings(t *testing.T) { } p := ResourceProvider(mp) - rc := &ResourceConfig{} + rc := &configs.Resource{ + Mode: addrs.ManagedResourceMode, + Type: "aws_instance", + Name: "foo", + Config: configs.SynthBody("", map[string]cty.Value{}), + } node := &EvalValidateResource{ - Provider: &p, - Config: &rc, - ResourceName: "foo", - ResourceType: "aws_instance", - ResourceMode: config.ManagedResourceMode, + Addr: addrs.ResourceInstance{ + Resource: addrs.Resource{ + Mode: addrs.ManagedResourceMode, + Type: "aws_instance", + Name: "foo", + }, + }, + Provider: &p, + Config: rc, IgnoreWarnings: true, } @@ -184,17 +224,26 @@ func TestEvalValidateProvisioner_valid(t *testing.T) { var p ResourceProvisioner = mp ctx := &MockEvalContext{} - cfg := &ResourceConfig{} - connInfo, err := config.NewRawConfig(map[string]interface{}{}) - if err != nil { - t.Fatalf("failed to make connInfo: %s", err) - } - connConfig := NewResourceConfig(connInfo) + schema := &configschema.Block{} node := &EvalValidateProvisioner{ + ResourceAddr: addrs.ResourceInstance{ + Resource: addrs.Resource{ + Mode: addrs.ManagedResourceMode, + Type: "foo", + Name: "bar", + }, + }, Provisioner: &p, - Config: &cfg, - ConnConfig: &connConfig, + Schema: &schema, + Config: &configs.Provisioner{ + Type: "baz", + Config: hcl.EmptyBody(), + }, + ConnConfig: &configs.Connection{ + Type: "ssh", + Config: hcl.EmptyBody(), + }, } result, err := node.Eval(ctx) @@ -206,10 +255,7 @@ func TestEvalValidateProvisioner_valid(t *testing.T) { } if !mp.ValidateCalled { - t.Fatalf("p.Config not called") - } - if mp.ValidateConfig != cfg { - t.Errorf("p.Config called with wrong config") + t.Fatalf("p.Validate not called") } } @@ -218,37 +264,43 @@ func TestEvalValidateProvisioner_warning(t *testing.T) { var p ResourceProvisioner = mp ctx := &MockEvalContext{} - cfg := &ResourceConfig{} - connInfo, err := config.NewRawConfig(map[string]interface{}{}) - if err != nil { - t.Fatalf("failed to make connInfo: %s", err) - } - connConfig := NewResourceConfig(connInfo) + schema := &configschema.Block{} node := &EvalValidateProvisioner{ + ResourceAddr: addrs.ResourceInstance{ + Resource: addrs.Resource{ + Mode: addrs.ManagedResourceMode, + Type: "foo", + Name: "bar", + }, + }, Provisioner: &p, - Config: &cfg, - ConnConfig: &connConfig, + Schema: &schema, + Config: &configs.Provisioner{ + Type: "baz", + Config: hcl.EmptyBody(), + }, + ConnConfig: &configs.Connection{ + Type: "ssh", + Config: hcl.EmptyBody(), + }, } mp.ValidateReturnWarns = []string{"foo is deprecated"} - _, err = node.Eval(ctx) + _, err := node.Eval(ctx) if err == nil { t.Fatalf("node.Eval succeeded; want error") } - valErr, ok := err.(*EvalValidateError) - if !ok { - t.Fatalf("node.Eval error is %#v; want *EvalValidateError", valErr) + var diags tfdiags.Diagnostics + diags = diags.Append(err) + if len(diags) != 1 { + t.Fatalf("wrong number of diagsnostics in %#v; want one warning", diags) } - warns := valErr.Warnings - if warns == nil || len(warns) != 1 { - t.Fatalf("wrong number of warnings in %#v; want one warning", warns) - } - if warns[0] != mp.ValidateReturnWarns[0] { - t.Fatalf("wrong warning %q; want %q", warns[0], mp.ValidateReturnWarns[0]) + if got, want := diags[0].Description().Summary, mp.ValidateReturnWarns[0]; got != want { + t.Fatalf("wrong warning %q; want %q", got, want) } } @@ -256,39 +308,44 @@ func TestEvalValidateProvisioner_connectionInvalid(t *testing.T) { var p ResourceProvisioner = &MockResourceProvisioner{} ctx := &MockEvalContext{} - cfg := &ResourceConfig{} - connInfo, err := config.NewRawConfig(map[string]interface{}{ - "bananananananana": "foo", - "bazaz": "bar", - }) - if err != nil { - t.Fatalf("failed to make connInfo: %s", err) - } - connConfig := NewResourceConfig(connInfo) + schema := &configschema.Block{} node := &EvalValidateProvisioner{ + ResourceAddr: addrs.ResourceInstance{ + Resource: addrs.Resource{ + Mode: addrs.ManagedResourceMode, + Type: "foo", + Name: "bar", + }, + }, Provisioner: &p, - Config: &cfg, - ConnConfig: &connConfig, + Schema: &schema, + Config: &configs.Provisioner{ + Type: "baz", + Config: hcl.EmptyBody(), + }, + ConnConfig: &configs.Connection{ + Type: "ssh", + Config: configs.SynthBody("", map[string]cty.Value{ + "bananananananana": cty.StringVal("foo"), + "bazaz": cty.StringVal("bar"), + }), + }, } - _, err = node.Eval(ctx) + _, err := node.Eval(ctx) if err == nil { t.Fatalf("node.Eval succeeded; want error") } - valErr, ok := err.(*EvalValidateError) - if !ok { - t.Fatalf("node.Eval error is %#v; want *EvalValidateError", valErr) + var diags tfdiags.Diagnostics + diags = diags.Append(err) + if len(diags) != 2 { + t.Fatalf("wrong number of diagsnostics in %#v; want two errors", diags) } - errs := valErr.Errors - if errs == nil || len(errs) != 2 { - t.Fatalf("wrong number of errors in %#v; want two errors", errs) - } - - errStr := errs[0].Error() - if !(strings.Contains(errStr, "bananananananana") || strings.Contains(errStr, "bazaz")) { - t.Fatalf("wrong first error %q; want something about our invalid connInfo keys", errStr) + errStr := diags.Err().Error() + if !(strings.Contains(errStr, "bananananananana") && strings.Contains(errStr, "bazaz")) { + t.Fatalf("wrong errors %q; want something about each of our invalid connInfo keys", errStr) } } diff --git a/terraform/eval_variable_test.go b/terraform/eval_variable_test.go deleted file mode 100644 index eb7ff92a3..000000000 --- a/terraform/eval_variable_test.go +++ /dev/null @@ -1,219 +0,0 @@ -package terraform - -import ( - "reflect" - "testing" - - "github.com/hashicorp/terraform/config" -) - -func TestCoerceMapVariable(t *testing.T) { - cases := map[string]struct { - Input *EvalCoerceMapVariable - ExpectVars map[string]interface{} - }{ - "a valid map is untouched": { - Input: &EvalCoerceMapVariable{ - Variables: map[string]interface{}{ - "amap": map[string]interface{}{"foo": "bar"}, - }, - ModulePath: []string{"root"}, - ModuleTree: testModuleInline(t, map[string]string{ - "main.tf": ` - variable "amap" { - type = "map" - } - `, - }), - }, - ExpectVars: map[string]interface{}{ - "amap": map[string]interface{}{"foo": "bar"}, - }, - }, - "a list w/ a single map element is coerced": { - Input: &EvalCoerceMapVariable{ - Variables: map[string]interface{}{ - "amap": []interface{}{ - map[string]interface{}{"foo": "bar"}, - }, - }, - ModulePath: []string{"root"}, - ModuleTree: testModuleInline(t, map[string]string{ - "main.tf": ` - variable "amap" { - type = "map" - } - `, - }), - }, - ExpectVars: map[string]interface{}{ - "amap": map[string]interface{}{"foo": "bar"}, - }, - }, - "a list w/ more than one map element is untouched": { - Input: &EvalCoerceMapVariable{ - Variables: map[string]interface{}{ - "amap": []interface{}{ - map[string]interface{}{"foo": "bar"}, - map[string]interface{}{"baz": "qux"}, - }, - }, - ModulePath: []string{"root"}, - ModuleTree: testModuleInline(t, map[string]string{ - "main.tf": ` - variable "amap" { - type = "map" - } - `, - }), - }, - ExpectVars: map[string]interface{}{ - "amap": []interface{}{ - map[string]interface{}{"foo": "bar"}, - map[string]interface{}{"baz": "qux"}, - }, - }, - }, - "list coercion also works in a module": { - Input: &EvalCoerceMapVariable{ - Variables: map[string]interface{}{ - "amap": []interface{}{ - map[string]interface{}{"foo": "bar"}, - }, - }, - ModulePath: []string{"root", "middle", "bottom"}, - ModuleTree: testModuleInline(t, map[string]string{ - "top.tf": ` - module "middle" { - source = "./middle" - } - `, - "middle/mid.tf": ` - module "bottom" { - source = "./bottom" - amap { - foo = "bar" - } - } - `, - "middle/bottom/bot.tf": ` - variable "amap" { - type = "map" - } - `, - }), - }, - ExpectVars: map[string]interface{}{ - "amap": map[string]interface{}{"foo": "bar"}, - }, - }, - "coercion only occurs when target var is a map": { - Input: &EvalCoerceMapVariable{ - Variables: map[string]interface{}{ - "alist": []interface{}{ - map[string]interface{}{"foo": "bar"}, - }, - }, - ModulePath: []string{"root"}, - ModuleTree: testModuleInline(t, map[string]string{ - "main.tf": ` - variable "alist" { - type = "list" - } - `, - }), - }, - ExpectVars: map[string]interface{}{ - "alist": []interface{}{ - map[string]interface{}{"foo": "bar"}, - }, - }, - }, - } - - for tn, tc := range cases { - _, err := tc.Input.Eval(&MockEvalContext{}) - if err != nil { - t.Fatalf("%s: Unexpected err: %s", tn, err) - } - if !reflect.DeepEqual(tc.Input.Variables, tc.ExpectVars) { - t.Fatalf("%s: Expected variables:\n\n%#v\n\nGot:\n\n%#v", - tn, tc.ExpectVars, tc.Input.Variables) - } - } -} - -func TestEvalVariableBlock(t *testing.T) { - rc, err := config.NewRawConfig(map[string]interface{}{ - "known": "foo", - "known_list": []interface{}{"foo"}, - "known_map": map[string]interface{}{ - "foo": "foo", - }, - "known_list_of_maps": []map[string]interface{}{ - map[string]interface{}{ - "foo": "foo", - }, - }, - "computed_map": map[string]interface{}{}, - "computed_list_of_maps": []map[string]interface{}{ - map[string]interface{}{}, - }, - // No computed_list right now, because that isn't currently supported: - // EvalVariableBlock assumes the final step of the path will always - // be a map. - }) - if err != nil { - t.Fatalf("config.NewRawConfig failed: %s", err) - } - - cfg := NewResourceConfig(rc) - cfg.ComputedKeys = []string{ - "computed", - "computed_map.foo", - "computed_list_of_maps.0.foo", - } - - n := &EvalVariableBlock{ - VariableValues: map[string]interface{}{ - // Should be cleared out on Eval - "should_be_deleted": true, - }, - Config: &cfg, - } - - ctx := &MockEvalContext{} - val, err := n.Eval(ctx) - if err != nil { - t.Fatalf("n.Eval failed: %s", err) - } - if val != nil { - t.Fatalf("n.Eval returned non-nil result: %#v", val) - } - - got := n.VariableValues - want := map[string]interface{}{ - "known": "foo", - "known_list": []interface{}{"foo"}, - "known_map": map[string]interface{}{ - "foo": "foo", - }, - "known_list_of_maps": []interface{}{ - map[string]interface{}{ - "foo": "foo", - }, - }, - "computed": config.UnknownVariableValue, - "computed_map": map[string]interface{}{ - "foo": config.UnknownVariableValue, - }, - "computed_list_of_maps": []interface{}{ - map[string]interface{}{ - "foo": config.UnknownVariableValue, - }, - }, - } - if !reflect.DeepEqual(got, want) { - t.Errorf("Incorrect variables\ngot: %#v\nwant: %#v", got, want) - } -} diff --git a/terraform/evaluate_test.go b/terraform/evaluate_test.go index 65675c3d5..d3f27ec4d 100644 --- a/terraform/evaluate_test.go +++ b/terraform/evaluate_test.go @@ -16,7 +16,10 @@ func TestEvaluatorGetTerraformAttr(t *testing.T) { Env: "foo", }, } - scope := evaluator.Scope(addrs.RootModuleInstance, nil) + data := &evaluationStateData{ + Evaluator: evaluator, + } + scope := evaluator.Scope(data, nil) t.Run("workspace", func(t *testing.T) { want := cty.StringVal("foo") diff --git a/terraform/graph_builder_apply_test.go b/terraform/graph_builder_apply_test.go index ccc14399a..e01b6c4d7 100644 --- a/terraform/graph_builder_apply_test.go +++ b/terraform/graph_builder_apply_test.go @@ -1,9 +1,11 @@ package terraform import ( - "reflect" + "fmt" "strings" "testing" + + "github.com/hashicorp/terraform/addrs" ) func TestApplyGraphBuilder_impl(t *testing.T) { @@ -65,20 +67,30 @@ func TestApplyGraphBuilder(t *testing.T) { } b := &ApplyGraphBuilder{ - Module: testModule(t, "graph-builder-apply-basic"), - Diff: diff, - Providers: []string{"aws"}, - Provisioners: []string{"exec"}, + Config: testModule(t, "graph-builder-apply-basic"), + Diff: diff, + Components: &basicComponentFactory{ + providers: map[string]ResourceProviderFactory{ + "aws": func() (ResourceProvider, error) { + return nil, fmt.Errorf("not implemented") + }, + }, + provisioners: map[string]ResourceProvisionerFactory{ + "exec": func() (ResourceProvisioner, error) { + return nil, fmt.Errorf("not implemented") + }, + }, + }, DisableReduce: true, } - g, err := b.Build(RootModulePath) + g, err := b.Build(addrs.RootModuleInstance) if err != nil { t.Fatalf("err: %s", err) } - if !reflect.DeepEqual(g.Path, RootModulePath) { - t.Fatalf("bad: %#v", g.Path) + if g.Path.String() != addrs.RootModuleInstance.String() { + t.Fatalf("wrong path %q", g.Path.String()) } actual := strings.TrimSpace(g.String()) @@ -119,21 +131,31 @@ func TestApplyGraphBuilder_depCbd(t *testing.T) { } b := &ApplyGraphBuilder{ - Module: testModule(t, "graph-builder-apply-dep-cbd"), - Diff: diff, - Providers: []string{"aws"}, - Provisioners: []string{"exec"}, + Config: testModule(t, "graph-builder-apply-dep-cbd"), + Diff: diff, + Components: &basicComponentFactory{ + providers: map[string]ResourceProviderFactory{ + "aws": func() (ResourceProvider, error) { + return nil, fmt.Errorf("not implemented") + }, + }, + provisioners: map[string]ResourceProvisionerFactory{ + "exec": func() (ResourceProvisioner, error) { + return nil, fmt.Errorf("not implemented") + }, + }, + }, DisableReduce: true, } - g, err := b.Build(RootModulePath) + g, err := b.Build(addrs.RootModuleInstance) if err != nil { t.Fatalf("err: %s", err) } t.Logf("Graph: %s", g.String()) - if !reflect.DeepEqual(g.Path, RootModulePath) { - t.Fatalf("bad: %#v", g.Path) + if g.Path.String() != addrs.RootModuleInstance.String() { + t.Fatalf("wrong path %q", g.Path.String()) } // Create A, Modify B, Destroy A @@ -185,20 +207,30 @@ func TestApplyGraphBuilder_doubleCBD(t *testing.T) { } b := &ApplyGraphBuilder{ - Module: testModule(t, "graph-builder-apply-double-cbd"), - Diff: diff, - Providers: []string{"aws"}, - Provisioners: []string{"exec"}, + Config: testModule(t, "graph-builder-apply-double-cbd"), + Diff: diff, + Components: &basicComponentFactory{ + providers: map[string]ResourceProviderFactory{ + "aws": func() (ResourceProvider, error) { + return nil, fmt.Errorf("not implemented") + }, + }, + provisioners: map[string]ResourceProvisionerFactory{ + "exec": func() (ResourceProvisioner, error) { + return nil, fmt.Errorf("not implemented") + }, + }, + }, DisableReduce: true, } - g, err := b.Build(RootModulePath) + g, err := b.Build(addrs.RootModuleInstance) if err != nil { t.Fatalf("err: %s", err) } - if !reflect.DeepEqual(g.Path, RootModulePath) { - t.Fatalf("bad: %#v", g.Path) + if g.Path.String() != addrs.RootModuleInstance.String() { + t.Fatalf("wrong path %q", g.Path.String()) } actual := strings.TrimSpace(g.String()) @@ -255,21 +287,27 @@ func TestApplyGraphBuilder_destroyStateOnly(t *testing.T) { } b := &ApplyGraphBuilder{ - Module: testModule(t, "empty"), - Diff: diff, - State: state, - Providers: []string{"aws"}, + Config: testModule(t, "empty"), + Diff: diff, + State: state, + Components: &basicComponentFactory{ + providers: map[string]ResourceProviderFactory{ + "aws": func() (ResourceProvider, error) { + return nil, fmt.Errorf("not implemented") + }, + }, + }, DisableReduce: true, } - g, err := b.Build(RootModulePath) + g, err := b.Build(addrs.RootModuleInstance) if err != nil { t.Fatalf("err: %s", err) } t.Logf("Graph: %s", g.String()) - if !reflect.DeepEqual(g.Path, RootModulePath) { - t.Fatalf("bad: %#v", g.Path) + if g.Path.String() != addrs.RootModuleInstance.String() { + t.Fatalf("wrong path %q", g.Path.String()) } testGraphHappensBefore( @@ -303,20 +341,30 @@ func TestApplyGraphBuilder_destroyCount(t *testing.T) { } b := &ApplyGraphBuilder{ - Module: testModule(t, "graph-builder-apply-count"), - Diff: diff, - Providers: []string{"aws"}, - Provisioners: []string{"exec"}, + Config: testModule(t, "graph-builder-apply-count"), + Diff: diff, + Components: &basicComponentFactory{ + providers: map[string]ResourceProviderFactory{ + "aws": func() (ResourceProvider, error) { + return nil, fmt.Errorf("not implemented") + }, + }, + provisioners: map[string]ResourceProvisionerFactory{ + "exec": func() (ResourceProvisioner, error) { + return nil, fmt.Errorf("not implemented") + }, + }, + }, DisableReduce: true, } - g, err := b.Build(RootModulePath) + g, err := b.Build(addrs.RootModuleInstance) if err != nil { t.Fatalf("err: %s", err) } - if !reflect.DeepEqual(g.Path, RootModulePath) { - t.Fatalf("bad: %#v", g.Path) + if g.Path.String() != addrs.RootModuleInstance.String() { + t.Fatalf("wrong module path %q", g.Path) } actual := strings.TrimSpace(g.String()) @@ -350,12 +398,18 @@ func TestApplyGraphBuilder_moduleDestroy(t *testing.T) { } b := &ApplyGraphBuilder{ - Module: testModule(t, "graph-builder-apply-module-destroy"), - Diff: diff, - Providers: []string{"null"}, + Config: testModule(t, "graph-builder-apply-module-destroy"), + Diff: diff, + Components: &basicComponentFactory{ + providers: map[string]ResourceProviderFactory{ + "null": func() (ResourceProvider, error) { + return nil, fmt.Errorf("not implemented") + }, + }, + }, } - g, err := b.Build(RootModulePath) + g, err := b.Build(addrs.RootModuleInstance) if err != nil { t.Fatalf("err: %s", err) } @@ -386,13 +440,23 @@ func TestApplyGraphBuilder_provisioner(t *testing.T) { } b := &ApplyGraphBuilder{ - Module: testModule(t, "graph-builder-apply-provisioner"), - Diff: diff, - Providers: []string{"null"}, - Provisioners: []string{"local"}, + Config: testModule(t, "graph-builder-apply-provisioner"), + Diff: diff, + Components: &basicComponentFactory{ + providers: map[string]ResourceProviderFactory{ + "null": func() (ResourceProvider, error) { + return nil, fmt.Errorf("not implemented") + }, + }, + provisioners: map[string]ResourceProvisionerFactory{ + "local": func() (ResourceProvisioner, error) { + return nil, fmt.Errorf("not implemented") + }, + }, + }, } - g, err := b.Build(RootModulePath) + g, err := b.Build(addrs.RootModuleInstance) if err != nil { t.Fatalf("err: %s", err) } @@ -419,14 +483,24 @@ func TestApplyGraphBuilder_provisionerDestroy(t *testing.T) { } b := &ApplyGraphBuilder{ - Destroy: true, - Module: testModule(t, "graph-builder-apply-provisioner"), - Diff: diff, - Providers: []string{"null"}, - Provisioners: []string{"local"}, + Destroy: true, + Config: testModule(t, "graph-builder-apply-provisioner"), + Diff: diff, + Components: &basicComponentFactory{ + providers: map[string]ResourceProviderFactory{ + "null": func() (ResourceProvider, error) { + return nil, fmt.Errorf("not implemented") + }, + }, + provisioners: map[string]ResourceProvisionerFactory{ + "local": func() (ResourceProvisioner, error) { + return nil, fmt.Errorf("not implemented") + }, + }, + }, } - g, err := b.Build(RootModulePath) + g, err := b.Build(addrs.RootModuleInstance) if err != nil { t.Fatalf("err: %s", err) } @@ -472,13 +546,21 @@ func TestApplyGraphBuilder_targetModule(t *testing.T) { } b := &ApplyGraphBuilder{ - Module: testModule(t, "graph-builder-apply-target-module"), - Diff: diff, - Providers: []string{"null"}, - Targets: []string{"module.child2"}, + Config: testModule(t, "graph-builder-apply-target-module"), + Diff: diff, + Components: &basicComponentFactory{ + providers: map[string]ResourceProviderFactory{ + "null": func() (ResourceProvider, error) { + return nil, fmt.Errorf("not implemented") + }, + }, + }, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Child("child2", addrs.NoKey), + }, } - g, err := b.Build(RootModulePath) + g, err := b.Build(addrs.RootModuleInstance) if err != nil { t.Fatalf("err: %s", err) } diff --git a/terraform/graph_builder_plan_test.go b/terraform/graph_builder_plan_test.go index 321ddbe3f..3da9287bb 100644 --- a/terraform/graph_builder_plan_test.go +++ b/terraform/graph_builder_plan_test.go @@ -1,9 +1,11 @@ package terraform import ( - "reflect" + "fmt" "strings" "testing" + + "github.com/hashicorp/terraform/addrs" ) func TestPlanGraphBuilder_impl(t *testing.T) { @@ -12,18 +14,27 @@ func TestPlanGraphBuilder_impl(t *testing.T) { func TestPlanGraphBuilder(t *testing.T) { b := &PlanGraphBuilder{ - Module: testModule(t, "graph-builder-plan-basic"), - Providers: []string{"aws", "openstack"}, + Config: testModule(t, "graph-builder-plan-basic"), + Components: &basicComponentFactory{ + providers: map[string]ResourceProviderFactory{ + "aws": func() (ResourceProvider, error) { + return nil, fmt.Errorf("not implemented") + }, + "openstack": func() (ResourceProvider, error) { + return nil, fmt.Errorf("not implemented") + }, + }, + }, DisableReduce: true, } - g, err := b.Build(RootModulePath) + g, err := b.Build(addrs.RootModuleInstance) if err != nil { t.Fatalf("err: %s", err) } - if !reflect.DeepEqual(g.Path, RootModulePath) { - t.Fatalf("bad: %#v", g.Path) + if g.Path.String() != addrs.RootModuleInstance.String() { + t.Fatalf("wrong module path %q", g.Path) } actual := strings.TrimSpace(g.String()) @@ -35,12 +46,23 @@ func TestPlanGraphBuilder(t *testing.T) { func TestPlanGraphBuilder_targetModule(t *testing.T) { b := &PlanGraphBuilder{ - Module: testModule(t, "graph-builder-plan-target-module-provider"), - Providers: []string{"null"}, - Targets: []string{"module.child2"}, + Config: testModule(t, "graph-builder-plan-target-module-provider"), + Components: &basicComponentFactory{ + providers: map[string]ResourceProviderFactory{ + "null": func() (ResourceProvider, error) { + return nil, fmt.Errorf("not implemented") + }, + "openstack": func() (ResourceProvider, error) { + return nil, fmt.Errorf("not implemented") + }, + }, + }, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Child("child2", addrs.NoKey), + }, } - g, err := b.Build(RootModulePath) + g, err := b.Build(addrs.RootModuleInstance) if err != nil { t.Fatalf("err: %s", err) } diff --git a/terraform/graph_builder_refresh_test.go b/terraform/graph_builder_refresh_test.go index e4383ab4e..59aaeb41c 100644 --- a/terraform/graph_builder_refresh_test.go +++ b/terraform/graph_builder_refresh_test.go @@ -1,6 +1,11 @@ package terraform -import "testing" +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/addrs" +) func TestRefreshGraphBuilder_configOrphans(t *testing.T) { @@ -65,11 +70,17 @@ func TestRefreshGraphBuilder_configOrphans(t *testing.T) { } b := &RefreshGraphBuilder{ - Module: m, - State: state, - Providers: []string{"aws"}, + Config: m, + State: state, + Components: &basicComponentFactory{ + providers: map[string]ResourceProviderFactory{ + "aws": func() (ResourceProvider, error) { + return nil, fmt.Errorf("not implemented") + }, + }, + }, } - g, err := b.Build(rootModulePath) + g, err := b.Build(addrs.RootModuleInstance) if err != nil { t.Fatalf("Error building graph: %s", err) } diff --git a/terraform/graph_builder_test.go b/terraform/graph_builder_test.go index 6d7f4b61b..21a3f1539 100644 --- a/terraform/graph_builder_test.go +++ b/terraform/graph_builder_test.go @@ -1,10 +1,11 @@ package terraform import ( - "reflect" "strings" "testing" + "github.com/hashicorp/terraform/addrs" + "github.com/hashicorp/terraform/dag" ) @@ -19,13 +20,13 @@ func TestBasicGraphBuilder(t *testing.T) { }, } - g, err := b.Build(RootModulePath) + g, err := b.Build(addrs.RootModuleInstance) if err != nil { t.Fatalf("err: %s", err) } - if !reflect.DeepEqual(g.Path, RootModulePath) { - t.Fatalf("bad: %#v", g.Path) + if g.Path.String() != addrs.RootModuleInstance.String() { + t.Fatalf("wrong module path %q", g.Path) } actual := strings.TrimSpace(g.String()) @@ -44,7 +45,7 @@ func TestBasicGraphBuilder_validate(t *testing.T) { Validate: true, } - _, err := b.Build(RootModulePath) + _, err := b.Build(addrs.RootModuleInstance) if err == nil { t.Fatal("should error") } @@ -59,7 +60,7 @@ func TestBasicGraphBuilder_validateOff(t *testing.T) { Validate: false, } - _, err := b.Build(RootModulePath) + _, err := b.Build(addrs.RootModuleInstance) if err != nil { t.Fatalf("expected no error, got: %s", err) } diff --git a/terraform/interpolate_test.go b/terraform/interpolate_test.go deleted file mode 100644 index 10f23a278..000000000 --- a/terraform/interpolate_test.go +++ /dev/null @@ -1,1133 +0,0 @@ -package terraform - -import ( - "fmt" - "os" - "reflect" - "sync" - "testing" - - "github.com/davecgh/go-spew/spew" - "github.com/hashicorp/hil" - "github.com/hashicorp/hil/ast" - "github.com/hashicorp/terraform/config" -) - -func TestInterpolater_simpleVar(t *testing.T) { - i := &Interpolater{} - scope := &InterpolationScope{} - testInterpolateErr(t, i, scope, "simple") -} - -func TestInterpolater_countIndex(t *testing.T) { - i := &Interpolater{} - - scope := &InterpolationScope{ - Path: rootModulePath, - Resource: &Resource{CountIndex: 42}, - } - - testInterpolate(t, i, scope, "count.index", ast.Variable{ - Value: 42, - Type: ast.TypeInt, - }) -} - -func TestInterpolater_countIndexInWrongContext(t *testing.T) { - i := &Interpolater{} - - scope := &InterpolationScope{ - Path: rootModulePath, - } - - n := "count.index" - - v, err := config.NewInterpolatedVariable(n) - if err != nil { - t.Fatalf("err: %s", err) - } - - expectedErr := fmt.Errorf("foo: count.index is only valid within resources") - - _, err = i.Values(scope, map[string]config.InterpolatedVariable{ - "foo": v, - }) - - if !reflect.DeepEqual(expectedErr, err) { - t.Fatalf("expected: %#v, got %#v", expectedErr, err) - } -} - -func TestInterpolater_moduleVariable(t *testing.T) { - lock := new(sync.RWMutex) - state := &State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: rootModulePath, - Resources: map[string]*ResourceState{ - "aws_instance.web": &ResourceState{ - Type: "aws_instance", - Primary: &InstanceState{ - ID: "bar", - }, - }, - }, - }, - &ModuleState{ - Path: []string{RootModuleName, "child"}, - Outputs: map[string]*OutputState{ - "foo": &OutputState{ - Type: "string", - Value: "bar", - }, - }, - }, - }, - } - - i := &Interpolater{ - State: state, - StateLock: lock, - } - - scope := &InterpolationScope{ - Path: rootModulePath, - } - - testInterpolate(t, i, scope, "module.child.foo", ast.Variable{ - Value: "bar", - Type: ast.TypeString, - }) -} - -func TestInterpolater_localVal(t *testing.T) { - lock := new(sync.RWMutex) - state := &State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: rootModulePath, - Locals: map[string]interface{}{ - "foo": "hello!", - }, - }, - }, - } - - i := &Interpolater{ - Module: testModule(t, "interpolate-local"), - State: state, - StateLock: lock, - } - - scope := &InterpolationScope{ - Path: rootModulePath, - } - - testInterpolate(t, i, scope, "local.foo", ast.Variable{ - Value: "hello!", - Type: ast.TypeString, - }) -} - -func TestInterpolater_missingID(t *testing.T) { - lock := new(sync.RWMutex) - state := &State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: rootModulePath, - Resources: map[string]*ResourceState{ - "aws_instance.web": &ResourceState{ - Type: "aws_instance", - Primary: &InstanceState{ - ID: "bar", - }, - }, - }, - }, - }, - } - - i := &Interpolater{ - Module: testModule(t, "interpolate-resource-variable"), - State: state, - StateLock: lock, - } - - scope := &InterpolationScope{ - Path: rootModulePath, - } - - testInterpolate(t, i, scope, "aws_instance.web.id", ast.Variable{ - Value: "bar", - Type: ast.TypeString, - }) -} - -func TestInterpolater_pathCwd(t *testing.T) { - i := &Interpolater{} - scope := &InterpolationScope{} - - expected, err := os.Getwd() - if err != nil { - t.Fatalf("err: %s", err) - } - - testInterpolate(t, i, scope, "path.cwd", ast.Variable{ - Value: expected, - Type: ast.TypeString, - }) -} - -func TestInterpolater_pathModule(t *testing.T) { - mod := testModule(t, "interpolate-path-module") - i := &Interpolater{ - Module: mod, - } - scope := &InterpolationScope{ - Path: []string{RootModuleName, "child"}, - } - - path := mod.Child([]string{"child"}).Config().Dir - testInterpolate(t, i, scope, "path.module", ast.Variable{ - Value: path, - Type: ast.TypeString, - }) -} - -func TestInterpolater_pathRoot(t *testing.T) { - mod := testModule(t, "interpolate-path-module") - i := &Interpolater{ - Module: mod, - } - scope := &InterpolationScope{ - Path: []string{RootModuleName, "child"}, - } - - path := mod.Config().Dir - testInterpolate(t, i, scope, "path.root", ast.Variable{ - Value: path, - Type: ast.TypeString, - }) -} - -func TestInterpolater_resourceVariableMap(t *testing.T) { - lock := new(sync.RWMutex) - state := &State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: rootModulePath, - Resources: map[string]*ResourceState{ - "aws_instance.web": &ResourceState{ - Type: "aws_instance", - Primary: &InstanceState{ - ID: "bar", - Attributes: map[string]string{ - "amap.%": "3", - "amap.key1": "value1", - "amap.key2": "value2", - "amap.key3": "value3", - }, - }, - }, - }, - }, - }, - } - - i := &Interpolater{ - Module: testModule(t, "interpolate-resource-variable"), - State: state, - StateLock: lock, - } - - scope := &InterpolationScope{ - Path: rootModulePath, - } - - expected := map[string]interface{}{ - "key1": "value1", - "key2": "value2", - "key3": "value3", - } - - testInterpolate(t, i, scope, "aws_instance.web.amap", - interfaceToVariableSwallowError(expected)) -} - -func TestInterpolater_resourceVariableComplexMap(t *testing.T) { - lock := new(sync.RWMutex) - state := &State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: rootModulePath, - Resources: map[string]*ResourceState{ - "aws_instance.web": &ResourceState{ - Type: "aws_instance", - Primary: &InstanceState{ - ID: "bar", - Attributes: map[string]string{ - "amap.%": "2", - "amap.key1.#": "2", - "amap.key1.0": "hello", - "amap.key1.1": "world", - "amap.key2.#": "1", - "amap.key2.0": "foo", - }, - }, - }, - }, - }, - }, - } - - i := &Interpolater{ - Module: testModule(t, "interpolate-resource-variable"), - State: state, - StateLock: lock, - } - - scope := &InterpolationScope{ - Path: rootModulePath, - } - - expected := map[string]interface{}{ - "key1": []interface{}{"hello", "world"}, - "key2": []interface{}{"foo"}, - } - - testInterpolate(t, i, scope, "aws_instance.web.amap", - interfaceToVariableSwallowError(expected)) -} - -func TestInterpolater_resourceVariable(t *testing.T) { - lock := new(sync.RWMutex) - state := &State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: rootModulePath, - Resources: map[string]*ResourceState{ - "aws_instance.web": &ResourceState{ - Type: "aws_instance", - Primary: &InstanceState{ - ID: "bar", - Attributes: map[string]string{ - "foo": "bar", - }, - }, - }, - }, - }, - }, - } - - i := &Interpolater{ - Module: testModule(t, "interpolate-resource-variable"), - State: state, - StateLock: lock, - } - - scope := &InterpolationScope{ - Path: rootModulePath, - } - - testInterpolate(t, i, scope, "aws_instance.web.foo", ast.Variable{ - Value: "bar", - Type: ast.TypeString, - }) -} - -func TestInterpolater_resourceVariableMissingDuringInput(t *testing.T) { - // During the input walk, computed resource attributes may be entirely - // absent since we've not yet produced diffs that tell us what computed - // attributes to expect. In that case, interpolator tolerates it and - // indicates the value is computed. - - lock := new(sync.RWMutex) - state := &State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: rootModulePath, - Resources: map[string]*ResourceState{ - // No resources at all yet, because we're still dealing - // with input and so the resources haven't been created. - }, - }, - }, - } - - { - i := &Interpolater{ - Operation: walkInput, - Module: testModule(t, "interpolate-resource-variable"), - State: state, - StateLock: lock, - } - - scope := &InterpolationScope{ - Path: rootModulePath, - } - - testInterpolate(t, i, scope, "aws_instance.web.foo", ast.Variable{ - Value: config.UnknownVariableValue, - Type: ast.TypeUnknown, - }) - } - - // This doesn't apply during other walks, like plan - { - i := &Interpolater{ - Operation: walkPlan, - Module: testModule(t, "interpolate-resource-variable"), - State: state, - StateLock: lock, - } - - scope := &InterpolationScope{ - Path: rootModulePath, - } - - testInterpolateErr(t, i, scope, "aws_instance.web.foo") - } -} - -func TestInterpolater_resourceVariableMulti(t *testing.T) { - lock := new(sync.RWMutex) - state := &State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: rootModulePath, - Resources: map[string]*ResourceState{ - "aws_instance.web": &ResourceState{ - Type: "aws_instance", - Primary: &InstanceState{ - ID: "bar", - Attributes: map[string]string{ - "foo": config.UnknownVariableValue, - }, - }, - }, - }, - }, - }, - } - - i := &Interpolater{ - Module: testModule(t, "interpolate-resource-variable"), - State: state, - StateLock: lock, - } - - scope := &InterpolationScope{ - Path: rootModulePath, - } - - testInterpolate(t, i, scope, "aws_instance.web.*.foo", ast.Variable{ - Type: ast.TypeList, - Value: []ast.Variable{ - { - Type: ast.TypeUnknown, - Value: config.UnknownVariableValue, - }, - }, - }) -} - -func TestInterpolater_resourceVariableMultiPartialUnknown(t *testing.T) { - lock := new(sync.RWMutex) - state := &State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: rootModulePath, - Resources: map[string]*ResourceState{ - "aws_instance.web.0": &ResourceState{ - Type: "aws_instance", - Primary: &InstanceState{ - ID: "bar", - Attributes: map[string]string{ - "foo": "1", - }, - }, - }, - "aws_instance.web.1": &ResourceState{ - Type: "aws_instance", - Primary: &InstanceState{ - ID: "bar", - Attributes: map[string]string{ - "foo": config.UnknownVariableValue, - }, - }, - }, - "aws_instance.web.2": &ResourceState{ - Type: "aws_instance", - Primary: &InstanceState{ - ID: "bar", - Attributes: map[string]string{ - "foo": "2", - }, - }, - }, - }, - }, - }, - } - - i := &Interpolater{ - Module: testModule(t, "interpolate-resource-variable-multi"), - State: state, - StateLock: lock, - } - - scope := &InterpolationScope{ - Path: rootModulePath, - } - - testInterpolate(t, i, scope, "aws_instance.web.*.foo", ast.Variable{ - Type: ast.TypeList, - Value: []ast.Variable{ - { - Type: ast.TypeString, - Value: "1", - }, - { - Type: ast.TypeUnknown, - Value: config.UnknownVariableValue, - }, - { - Type: ast.TypeString, - Value: "2", - }, - }, - }) -} - -func TestInterpolater_resourceVariableMultiNoState(t *testing.T) { - // When evaluating a "splat" variable in a module that doesn't have - // any state yet, we should still be able to resolve to an empty - // list. - // See https://github.com/hashicorp/terraform/issues/14438 for an - // example of what we're testing for here. - lock := new(sync.RWMutex) - state := &State{ - Modules: []*ModuleState{}, - } - - i := &Interpolater{ - Module: testModule(t, "interpolate-resource-variable-multi"), - State: state, - StateLock: lock, - Operation: walkApply, - } - - scope := &InterpolationScope{ - Path: rootModulePath, - } - - testInterpolate(t, i, scope, "aws_instance.web.*.foo", ast.Variable{ - Type: ast.TypeList, - Value: []ast.Variable{}, - }) -} - -// When a splat reference is made to an attribute that is a computed list, -// the result should be unknown. -func TestInterpolater_resourceVariableMultiList(t *testing.T) { - lock := new(sync.RWMutex) - state := &State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: rootModulePath, - Resources: map[string]*ResourceState{ - "aws_instance.web.0": &ResourceState{ - Type: "aws_instance", - Primary: &InstanceState{ - ID: "bar", - Attributes: map[string]string{ - "ip.#": config.UnknownVariableValue, - }, - }, - }, - - "aws_instance.web.1": &ResourceState{ - Type: "aws_instance", - Primary: &InstanceState{ - ID: "bar", - Attributes: map[string]string{ - "ip.#": "0", - }, - }, - }, - }, - }, - }, - } - - i := &Interpolater{ - Module: testModule(t, "interpolate-resource-variable"), - State: state, - StateLock: lock, - } - - scope := &InterpolationScope{ - Path: rootModulePath, - } - - testInterpolate(t, i, scope, "aws_instance.web.*.ip", ast.Variable{ - Type: ast.TypeList, - Value: []ast.Variable{ - { - Type: ast.TypeUnknown, - Value: config.UnknownVariableValue, - }, - }, - }) -} - -func TestInterpolater_resourceVariableMulti_interpolated(t *testing.T) { - lock := new(sync.RWMutex) - state := &State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: rootModulePath, - Resources: map[string]*ResourceState{ - "aws_instance.web.0": &ResourceState{ - Type: "aws_instance", - Primary: &InstanceState{ - ID: "a", - Attributes: map[string]string{"foo": "a"}, - }, - }, - - "aws_instance.web.1": &ResourceState{ - Type: "aws_instance", - Primary: &InstanceState{ - ID: "b", - Attributes: map[string]string{"foo": "b"}, - }, - }, - }, - }, - }, - } - - i := &Interpolater{ - Operation: walkApply, - Module: testModule(t, "interpolate-multi-interp"), - State: state, - StateLock: lock, - } - - scope := &InterpolationScope{ - Path: rootModulePath, - } - - expected := []interface{}{"a", "b"} - testInterpolate(t, i, scope, "aws_instance.web.*.foo", - interfaceToVariableSwallowError(expected)) -} - -func interfaceToVariableSwallowError(input interface{}) ast.Variable { - variable, _ := hil.InterfaceToVariable(input) - return variable -} - -func TestInterpolator_resourceMultiAttributes(t *testing.T) { - lock := new(sync.RWMutex) - state := &State{ - Modules: []*ModuleState{ - { - Path: rootModulePath, - Resources: map[string]*ResourceState{ - "aws_route53_zone.yada": { - Type: "aws_route53_zone", - Dependencies: []string{}, - Primary: &InstanceState{ - ID: "AAABBBCCCDDDEEE", - Attributes: map[string]string{ - "name_servers.#": "4", - "name_servers.0": "ns-1334.awsdns-38.org", - "name_servers.1": "ns-1680.awsdns-18.co.uk", - "name_servers.2": "ns-498.awsdns-62.com", - "name_servers.3": "ns-601.awsdns-11.net", - "listeners.#": "1", - "listeners.0": "red", - "tags.%": "1", - "tags.Name": "reindeer", - "nothing.#": "0", - }, - }, - }, - }, - }, - }, - } - - i := &Interpolater{ - Module: testModule(t, "interpolate-multi-vars"), - StateLock: lock, - State: state, - } - - scope := &InterpolationScope{ - Path: rootModulePath, - } - - name_servers := []interface{}{ - "ns-1334.awsdns-38.org", - "ns-1680.awsdns-18.co.uk", - "ns-498.awsdns-62.com", - "ns-601.awsdns-11.net", - } - - // More than 1 element - testInterpolate(t, i, scope, "aws_route53_zone.yada.name_servers", - interfaceToVariableSwallowError(name_servers)) - - // Exactly 1 element - testInterpolate(t, i, scope, "aws_route53_zone.yada.listeners", - interfaceToVariableSwallowError([]interface{}{"red"})) - - // Zero elements - testInterpolate(t, i, scope, "aws_route53_zone.yada.nothing", - interfaceToVariableSwallowError([]interface{}{})) - - // Maps still need to work - testInterpolate(t, i, scope, "aws_route53_zone.yada.tags.Name", ast.Variable{ - Value: "reindeer", - Type: ast.TypeString, - }) -} - -func TestInterpolator_resourceMultiAttributesWithResourceCount(t *testing.T) { - i := getInterpolaterFixture(t) - scope := &InterpolationScope{ - Path: rootModulePath, - } - - name_servers := []interface{}{ - "ns-1334.awsdns-38.org", - "ns-1680.awsdns-18.co.uk", - "ns-498.awsdns-62.com", - "ns-601.awsdns-11.net", - "ns-000.awsdns-38.org", - "ns-444.awsdns-18.co.uk", - "ns-999.awsdns-62.com", - "ns-666.awsdns-11.net", - } - - // More than 1 element - testInterpolate(t, i, scope, "aws_route53_zone.terra.0.name_servers", - interfaceToVariableSwallowError(name_servers[:4])) - - // More than 1 element in both - testInterpolate(t, i, scope, "aws_route53_zone.terra.*.name_servers", - interfaceToVariableSwallowError([]interface{}{name_servers[:4], name_servers[4:]})) - - // Exactly 1 element - testInterpolate(t, i, scope, "aws_route53_zone.terra.0.listeners", - interfaceToVariableSwallowError([]interface{}{"red"})) - - // Exactly 1 element in both - testInterpolate(t, i, scope, "aws_route53_zone.terra.*.listeners", - interfaceToVariableSwallowError([]interface{}{[]interface{}{"red"}, []interface{}{"blue"}})) - - // Zero elements - testInterpolate(t, i, scope, "aws_route53_zone.terra.0.nothing", - interfaceToVariableSwallowError([]interface{}{})) - - // Zero + 1 element - testInterpolate(t, i, scope, "aws_route53_zone.terra.*.special", - interfaceToVariableSwallowError([]interface{}{[]interface{}{"extra"}})) - - // Maps still need to work - testInterpolate(t, i, scope, "aws_route53_zone.terra.0.tags.Name", ast.Variable{ - Value: "reindeer", - Type: ast.TypeString, - }) - - // Maps still need to work in both - testInterpolate(t, i, scope, "aws_route53_zone.terra.*.tags.Name", - interfaceToVariableSwallowError([]interface{}{"reindeer", "white-hart"})) -} - -func TestInterpolator_resourceMultiAttributesComputed(t *testing.T) { - lock := new(sync.RWMutex) - // The state would never be written with an UnknownVariableValue in it, but - // it can/does exist that way in memory during the plan phase. - state := &State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: rootModulePath, - Resources: map[string]*ResourceState{ - "aws_route53_zone.yada": &ResourceState{ - Type: "aws_route53_zone", - Primary: &InstanceState{ - ID: "z-abc123", - Attributes: map[string]string{ - "name_servers.#": config.UnknownVariableValue, - }, - }, - }, - }, - }, - }, - } - i := &Interpolater{ - Module: testModule(t, "interpolate-multi-vars"), - StateLock: lock, - State: state, - } - - scope := &InterpolationScope{ - Path: rootModulePath, - } - - testInterpolate(t, i, scope, "aws_route53_zone.yada.name_servers", ast.Variable{ - Value: config.UnknownVariableValue, - Type: ast.TypeUnknown, - }) -} - -func TestInterpolator_resourceAttributeComputed(t *testing.T) { - lock := new(sync.RWMutex) - // The state would never be written with an UnknownVariableValue in it, but - // it can/does exist that way in memory during the plan phase. - state := &State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: rootModulePath, - Resources: map[string]*ResourceState{ - "aws_route53_zone.yada": &ResourceState{ - Type: "aws_route53_zone", - Primary: &InstanceState{ - ID: "z-abc123", - Attributes: map[string]string{ - "id": config.UnknownVariableValue, - }, - }, - }, - }, - }, - }, - } - i := &Interpolater{ - Module: testModule(t, "interpolate-multi-vars"), - StateLock: lock, - State: state, - } - - scope := &InterpolationScope{ - Path: rootModulePath, - } - - testInterpolate(t, i, scope, "aws_route53_zone.yada.id", ast.Variable{ - Value: config.UnknownVariableValue, - Type: ast.TypeUnknown, - }) -} - -func TestInterpolater_selfVarWithoutResource(t *testing.T) { - i := &Interpolater{} - - scope := &InterpolationScope{ - Path: rootModulePath, - } - - v, err := config.NewInterpolatedVariable("self.name") - if err != nil { - t.Fatalf("err: %s", err) - } - - _, err = i.Values(scope, map[string]config.InterpolatedVariable{"foo": v}) - if err == nil { - t.Fatalf("expected err, got none") - } -} - -func TestInterpolator_interpolatedListOrder(t *testing.T) { - state := &State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: rootModulePath, - Resources: map[string]*ResourceState{ - "aws_route53_zone.yada": &ResourceState{ - Type: "aws_route53_zone", - Dependencies: []string{}, - Primary: &InstanceState{ - ID: "null", - Attributes: map[string]string{ - "foo.#": "12", - "foo.0": "a", - "foo.1": "b", - "foo.2": "c", - "foo.3": "d", - "foo.4": "e", - "foo.5": "f", - "foo.6": "g", - "foo.7": "h", - "foo.8": "i", - "foo.9": "j", - "foo.10": "k", - "foo.11": "l", - }, - }, - }, - }, - }, - }, - } - - i := &Interpolater{ - Module: testModule(t, "interpolate-multi-vars"), - StateLock: new(sync.RWMutex), - State: state, - } - - scope := &InterpolationScope{ - Path: rootModulePath, - } - - list := []interface{}{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"} - - testInterpolate(t, i, scope, "aws_route53_zone.yada.foo", - interfaceToVariableSwallowError(list)) -} - -func getInterpolaterFixture(t *testing.T) *Interpolater { - lock := new(sync.RWMutex) - state := &State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: rootModulePath, - Resources: map[string]*ResourceState{ - "aws_route53_zone.terra.0": &ResourceState{ - Type: "aws_route53_zone", - Dependencies: []string{}, - Primary: &InstanceState{ - ID: "AAABBBCCCDDDEEE", - Attributes: map[string]string{ - "name_servers.#": "4", - "name_servers.0": "ns-1334.awsdns-38.org", - "name_servers.1": "ns-1680.awsdns-18.co.uk", - "name_servers.2": "ns-498.awsdns-62.com", - "name_servers.3": "ns-601.awsdns-11.net", - "listeners.#": "1", - "listeners.0": "red", - "tags.%": "1", - "tags.Name": "reindeer", - "nothing.#": "0", - }, - }, - }, - "aws_route53_zone.terra.1": &ResourceState{ - Type: "aws_route53_zone", - Dependencies: []string{}, - Primary: &InstanceState{ - ID: "EEEFFFGGGHHHIII", - Attributes: map[string]string{ - "name_servers.#": "4", - "name_servers.0": "ns-000.awsdns-38.org", - "name_servers.1": "ns-444.awsdns-18.co.uk", - "name_servers.2": "ns-999.awsdns-62.com", - "name_servers.3": "ns-666.awsdns-11.net", - "listeners.#": "1", - "listeners.0": "blue", - "special.#": "1", - "special.0": "extra", - "tags.%": "1", - "tags.Name": "white-hart", - "nothing.#": "0", - }, - }, - }, - }, - }, - }, - } - - return &Interpolater{ - Module: testModule(t, "interpolate-multi-vars"), - StateLock: lock, - State: state, - } -} - -func TestInterpolator_nestedMapsAndLists(t *testing.T) { - state := &State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: rootModulePath, - Resources: map[string]*ResourceState{ - "aws_route53_zone.yada": &ResourceState{ - Type: "aws_route53_zone", - Dependencies: []string{}, - Primary: &InstanceState{ - ID: "null", - Attributes: map[string]string{ - "list_of_map.#": "2", - "list_of_map.0.%": "1", - "list_of_map.0.a": "1", - "list_of_map.1.%": "1", - "list_of_map.1.b": "2", - "map_of_list.%": "2", - "map_of_list.list2.#": "1", - "map_of_list.list2.0": "b", - "map_of_list.list1.#": "1", - "map_of_list.list1.0": "a", - }, - }, - }, - }, - }, - }, - } - - i := &Interpolater{ - Module: testModule(t, "interpolate-multi-vars"), - StateLock: new(sync.RWMutex), - State: state, - } - - scope := &InterpolationScope{ - Path: rootModulePath, - } - - listOfMap := []interface{}{ - map[string]interface{}{"a": "1"}, - map[string]interface{}{"b": "2"}, - } - - mapOfList := map[string]interface{}{ - "list1": []interface{}{"a"}, - "list2": []interface{}{"b"}, - } - - testInterpolate(t, i, scope, "aws_route53_zone.yada.list_of_map", - interfaceToVariableSwallowError(listOfMap)) - testInterpolate(t, i, scope, `aws_route53_zone.yada.map_of_list`, - interfaceToVariableSwallowError(mapOfList)) -} - -func TestInterpolator_sets(t *testing.T) { - state := &State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: rootModulePath, - Resources: map[string]*ResourceState{ - "aws_route53_zone.yada": &ResourceState{ - Type: "aws_network_interface", - Dependencies: []string{}, - Primary: &InstanceState{ - ID: "null", - Attributes: map[string]string{ - "private_ips.#": "1", - "private_ips.3977356764": "10.42.16.179", - }, - }, - }, - }, - }, - }, - } - - i := &Interpolater{ - Module: testModule(t, "interpolate-multi-vars"), - StateLock: new(sync.RWMutex), - State: state, - } - - scope := &InterpolationScope{ - Path: rootModulePath, - } - - set := []interface{}{"10.42.16.179"} - - testInterpolate(t, i, scope, "aws_route53_zone.yada.private_ips", - interfaceToVariableSwallowError(set)) -} - -// When a splat reference is made to a resource that is unknown, we should -// return an empty list rather than panicking. -func TestInterpolater_resourceUnknownVariableList(t *testing.T) { - i := &Interpolater{ - Module: testModule(t, "plan-computed-data-resource"), - State: NewState(), // state, - StateLock: new(sync.RWMutex), - } - - scope := &InterpolationScope{ - Path: rootModulePath, - } - - testInterpolate(t, i, scope, "aws_vpc.bar.*.foo", - interfaceToVariableSwallowError([]interface{}{})) -} - -func TestInterpolater_terraformEnv(t *testing.T) { - i := &Interpolater{ - Meta: &ContextMeta{Env: "foo"}, - } - - scope := &InterpolationScope{ - Path: rootModulePath, - } - - testInterpolate(t, i, scope, "terraform.env", ast.Variable{ - Value: "foo", - Type: ast.TypeString, - }) -} - -func TestInterpolater_terraformInvalid(t *testing.T) { - i := &Interpolater{ - Meta: &ContextMeta{Env: "foo"}, - } - - scope := &InterpolationScope{ - Path: rootModulePath, - } - - testInterpolateErr(t, i, scope, "terraform.nope") -} - -func testInterpolate( - t *testing.T, i *Interpolater, - scope *InterpolationScope, - n string, expectedVar ast.Variable) { - v, err := config.NewInterpolatedVariable(n) - if err != nil { - t.Fatalf("err: %s", err) - } - - actual, err := i.Values(scope, map[string]config.InterpolatedVariable{ - "foo": v, - }) - if err != nil { - t.Fatalf("err: %s", err) - } - - expected := map[string]ast.Variable{ - "foo": expectedVar, - } - if !reflect.DeepEqual(actual, expected) { - spew.Config.DisableMethods = true - t.Fatalf("%q:\n\n actual: %#v\nexpected: %#v\n\n%s\n\n%s\n\n", n, actual, expected, - spew.Sdump(actual), spew.Sdump(expected)) - } -} - -func testInterpolateErr( - t *testing.T, i *Interpolater, - scope *InterpolationScope, - n string) { - v, err := config.NewInterpolatedVariable(n) - if err != nil { - t.Fatalf("err: %s", err) - } - - _, err = i.Values(scope, map[string]config.InterpolatedVariable{ - "foo": v, - }) - if err == nil { - t.Fatalf("%q: succeeded, but wanted error", n) - } -} diff --git a/terraform/module_dependencies_test.go b/terraform/module_dependencies_test.go index 8bcbef062..4c87a4c9a 100644 --- a/terraform/module_dependencies_test.go +++ b/terraform/module_dependencies_test.go @@ -4,7 +4,7 @@ import ( "testing" "github.com/davecgh/go-spew/spew" - "github.com/hashicorp/terraform/config/module" + "github.com/hashicorp/terraform/configs" "github.com/hashicorp/terraform/moduledeps" "github.com/hashicorp/terraform/plugin/discovery" ) @@ -243,12 +243,12 @@ func TestModuleTreeDependencies(t *testing.T) { for name, test := range tests { t.Run(name, func(t *testing.T) { - var root *module.Tree + var root *configs.Config if test.ConfigDir != "" { root = testModule(t, test.ConfigDir) } - got := ModuleTreeDependencies(root, test.State) + got := ConfigTreeDependencies(root, test.State) if !got.Equal(test.Want) { t.Errorf( "wrong dependency tree\ngot: %s\nwant: %s", diff --git a/terraform/node_data_refresh_test.go b/terraform/node_data_refresh_test.go index d58739f34..f856d561d 100644 --- a/terraform/node_data_refresh_test.go +++ b/terraform/node_data_refresh_test.go @@ -3,16 +3,13 @@ package terraform import ( "sync" "testing" + + "github.com/hashicorp/terraform/addrs" ) func TestNodeRefreshableDataResourceDynamicExpand_scaleOut(t *testing.T) { var stateLock sync.RWMutex - addr, err := ParseResourceAddress("data.aws_instance.foo") - if err != nil { - t.Fatalf("bad: %s", err) - } - m := testModule(t, "refresh-data-scale-inout") state := &State{ @@ -42,16 +39,18 @@ func TestNodeRefreshableDataResourceDynamicExpand_scaleOut(t *testing.T) { } n := &NodeRefreshableDataResource{ - NodeAbstractCountResource: &NodeAbstractCountResource{ - NodeAbstractResource: &NodeAbstractResource{ - Addr: addr, - Config: m.Config().Resources[0], - }, + NodeAbstractResource: &NodeAbstractResource{ + Addr: addrs.RootModuleInstance.Resource( + addrs.DataResourceMode, + "aws_instance", + "foo", + ), + Config: m.Module.DataResources["data.aws_instance.foo"], }, } g, err := n.DynamicExpand(&MockEvalContext{ - PathPath: []string{"root"}, + PathPath: addrs.RootModuleInstance, StateState: state, StateLock: &stateLock, }) @@ -76,11 +75,6 @@ root - terraform.graphNodeRoot func TestNodeRefreshableDataResourceDynamicExpand_scaleIn(t *testing.T) { var stateLock sync.RWMutex - addr, err := ParseResourceAddress("data.aws_instance.foo") - if err != nil { - t.Fatalf("bad: %s", err) - } - m := testModule(t, "refresh-data-scale-inout") state := &State{ @@ -126,16 +120,18 @@ func TestNodeRefreshableDataResourceDynamicExpand_scaleIn(t *testing.T) { } n := &NodeRefreshableDataResource{ - NodeAbstractCountResource: &NodeAbstractCountResource{ - NodeAbstractResource: &NodeAbstractResource{ - Addr: addr, - Config: m.Config().Resources[0], - }, + NodeAbstractResource: &NodeAbstractResource{ + Addr: addrs.RootModuleInstance.Resource( + addrs.DataResourceMode, + "aws_instance", + "foo", + ), + Config: m.Module.DataResources["data.aws_instance.foo"], }, } g, err := n.DynamicExpand(&MockEvalContext{ - PathPath: []string{"root"}, + PathPath: addrs.RootModuleInstance, StateState: state, StateLock: &stateLock, }) diff --git a/terraform/node_module_variable_test.go b/terraform/node_module_variable_test.go index 4fb6663b9..6e08a00c1 100644 --- a/terraform/node_module_variable_test.go +++ b/terraform/node_module_variable_test.go @@ -4,13 +4,19 @@ import ( "reflect" "testing" - "github.com/hashicorp/terraform/config" + "github.com/hashicorp/hcl2/hcl" + "github.com/hashicorp/hcl2/hcl/hclsyntax" + + "github.com/hashicorp/terraform/addrs" + "github.com/hashicorp/terraform/configs" ) func TestNodeApplyableModuleVariablePath(t *testing.T) { n := &NodeApplyableModuleVariable{ - PathValue: []string{"root", "child"}, - Config: &config.Variable{Name: "foo"}, + Addr: addrs.RootModuleInstance.Child("child", addrs.NoKey).InputVariable("foo"), + Config: &configs.Variable{ + Name: "foo", + }, } expected := []string{"root"} @@ -22,27 +28,55 @@ func TestNodeApplyableModuleVariablePath(t *testing.T) { func TestNodeApplyableModuleVariableReferenceableName(t *testing.T) { n := &NodeApplyableModuleVariable{ - PathValue: []string{"root", "child"}, - Config: &config.Variable{Name: "foo"}, + Addr: addrs.RootModuleInstance.Child("child", addrs.NoKey).InputVariable("foo"), + Config: &configs.Variable{ + Name: "foo", + }, } - expected := []string{"module.child.var.foo"} - actual := n.ReferenceableName() - if !reflect.DeepEqual(actual, expected) { - t.Fatalf("%#v != %#v", actual, expected) + { + expected := []addrs.Referenceable{ + addrs.InputVariable{Name: "foo"}, + } + actual := n.ReferenceableAddrs() + if !reflect.DeepEqual(actual, expected) { + t.Fatalf("%#v != %#v", actual, expected) + } } + + { + gotSelfPath, gotReferencePath := n.ReferenceOutside() + wantSelfPath := addrs.RootModuleInstance.Child("child", addrs.NoKey) + wantReferencePath := addrs.RootModuleInstance + if got, want := gotSelfPath.String(), wantSelfPath.String(); got != want { + t.Errorf("wrong self path\ngot: %s\nwant: %s", got, want) + } + if got, want := gotReferencePath.String(), wantReferencePath.String(); got != want { + t.Errorf("wrong reference path\ngot: %s\nwant: %s", got, want) + } + } + } func TestNodeApplyableModuleVariableReference(t *testing.T) { n := &NodeApplyableModuleVariable{ - PathValue: []string{"root", "child"}, - Config: &config.Variable{Name: "foo"}, - Value: config.TestRawConfig(t, map[string]interface{}{ - "foo": `${var.foo}`, - }), + Addr: addrs.RootModuleInstance.Child("child", addrs.NoKey).InputVariable("foo"), + Config: &configs.Variable{ + Name: "foo", + }, + Expr: &hclsyntax.ScopeTraversalExpr{ + Traversal: hcl.Traversal{ + hcl.TraverseRoot{Name: "var"}, + hcl.TraverseAttr{Name: "foo"}, + }, + }, } - expected := []string{"var.foo"} + expected := []*addrs.Reference{ + { + Subject: addrs.InputVariable{Name: "foo"}, + }, + } actual := n.References() if !reflect.DeepEqual(actual, expected) { t.Fatalf("%#v != %#v", actual, expected) @@ -51,14 +85,26 @@ func TestNodeApplyableModuleVariableReference(t *testing.T) { func TestNodeApplyableModuleVariableReference_grandchild(t *testing.T) { n := &NodeApplyableModuleVariable{ - PathValue: []string{"root", "child", "grandchild"}, - Config: &config.Variable{Name: "foo"}, - Value: config.TestRawConfig(t, map[string]interface{}{ - "foo": `${var.foo}`, - }), + Addr: addrs.RootModuleInstance. + Child("child", addrs.NoKey). + Child("grandchild", addrs.NoKey). + InputVariable("foo"), + Config: &configs.Variable{ + Name: "foo", + }, + Expr: &hclsyntax.ScopeTraversalExpr{ + Traversal: hcl.Traversal{ + hcl.TraverseRoot{Name: "var"}, + hcl.TraverseAttr{Name: "foo"}, + }, + }, } - expected := []string{"module.child.var.foo"} + expected := []*addrs.Reference{ + { + Subject: addrs.InputVariable{Name: "foo"}, + }, + } actual := n.References() if !reflect.DeepEqual(actual, expected) { t.Fatalf("%#v != %#v", actual, expected) diff --git a/terraform/node_resource_destroy_test.go b/terraform/node_resource_destroy_test.go index 869105fa2..59af9fb24 100644 --- a/terraform/node_resource_destroy_test.go +++ b/terraform/node_resource_destroy_test.go @@ -4,6 +4,8 @@ import ( "strings" "sync" "testing" + + "github.com/hashicorp/terraform/addrs" ) func TestNodeDestroyResourceDynamicExpand_deposedCount(t *testing.T) { @@ -34,22 +36,22 @@ func TestNodeDestroyResourceDynamicExpand_deposedCount(t *testing.T) { }, } - addr, err := parseResourceAddressInternal("aws_instance.bar.0") - if err != nil { - t.Fatalf("err: %s", err) - } - m := testModule(t, "apply-cbd-count") - n := &NodeDestroyResource{ - NodeAbstractResource: &NodeAbstractResource{ - Addr: addr, + n := &NodeDestroyResourceInstance{ + NodeAbstractResourceInstance: &NodeAbstractResourceInstance{ + NodeAbstractResource: NodeAbstractResource{ + Addr: addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "bar", + ), + Config: m.Module.ManagedResources["aws_instance.bar"], + }, + InstanceKey: addrs.IntKey(0), ResourceState: state.Modules[0].Resources["aws_instance.bar.0"], - Config: m.Config().Resources[0], }, } g, err := n.DynamicExpand(&MockEvalContext{ - PathPath: []string{"root"}, + PathPath: addrs.RootModuleInstance, StateState: state, StateLock: &stateLock, }) diff --git a/terraform/node_resource_refresh_test.go b/terraform/node_resource_refresh_test.go index b4f77ca6e..67033df57 100644 --- a/terraform/node_resource_refresh_test.go +++ b/terraform/node_resource_refresh_test.go @@ -5,17 +5,14 @@ import ( "sync" "testing" + "github.com/hashicorp/terraform/addrs" + "github.com/davecgh/go-spew/spew" ) func TestNodeRefreshableManagedResourceDynamicExpand_scaleOut(t *testing.T) { var stateLock sync.RWMutex - addr, err := ParseResourceAddress("aws_instance.foo") - if err != nil { - t.Fatalf("bad: %s", err) - } - m := testModule(t, "refresh-resource-scale-inout") state := &State{ @@ -45,16 +42,16 @@ func TestNodeRefreshableManagedResourceDynamicExpand_scaleOut(t *testing.T) { } n := &NodeRefreshableManagedResource{ - NodeAbstractCountResource: &NodeAbstractCountResource{ - NodeAbstractResource: &NodeAbstractResource{ - Addr: addr, - Config: m.Config().Resources[0], - }, + NodeAbstractResource: &NodeAbstractResource{ + Addr: addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "foo", + ), + Config: m.Module.ManagedResources["aws_instance.foo"], }, } g, err := n.DynamicExpand(&MockEvalContext{ - PathPath: []string{"root"}, + PathPath: addrs.RootModuleInstance, StateState: state, StateLock: &stateLock, }) @@ -79,11 +76,6 @@ root - terraform.graphNodeRoot func TestNodeRefreshableManagedResourceDynamicExpand_scaleIn(t *testing.T) { var stateLock sync.RWMutex - addr, err := ParseResourceAddress("aws_instance.foo") - if err != nil { - t.Fatalf("bad: %s", err) - } - m := testModule(t, "refresh-resource-scale-inout") state := &State{ @@ -129,16 +121,16 @@ func TestNodeRefreshableManagedResourceDynamicExpand_scaleIn(t *testing.T) { } n := &NodeRefreshableManagedResource{ - NodeAbstractCountResource: &NodeAbstractCountResource{ - NodeAbstractResource: &NodeAbstractResource{ - Addr: addr, - Config: m.Config().Resources[0], - }, + NodeAbstractResource: &NodeAbstractResource{ + Addr: addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "foo", + ), + Config: m.Module.ManagedResources["aws_instance.foo"], }, } g, err := n.DynamicExpand(&MockEvalContext{ - PathPath: []string{"root"}, + PathPath: addrs.RootModuleInstance, StateState: state, StateLock: &stateLock, }) @@ -162,17 +154,17 @@ root - terraform.graphNodeRoot } func TestNodeRefreshableManagedResourceEvalTree_scaleOut(t *testing.T) { - addr, err := ParseResourceAddress("aws_instance.foo[2]") - if err != nil { - t.Fatalf("bad: %s", err) - } - m := testModule(t, "refresh-resource-scale-inout") n := &NodeRefreshableManagedResourceInstance{ - NodeAbstractResource: &NodeAbstractResource{ - Addr: addr, - Config: m.Config().Resources[0], + NodeAbstractResourceInstance: &NodeAbstractResourceInstance{ + NodeAbstractResource: NodeAbstractResource{ + Addr: addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "foo", + ), + Config: m.Module.ManagedResources["aws_instance.foo"], + }, + InstanceKey: addrs.IntKey(2), }, } diff --git a/terraform/plan_test.go b/terraform/plan_test.go index 5de506165..d0c1c38a8 100644 --- a/terraform/plan_test.go +++ b/terraform/plan_test.go @@ -6,7 +6,10 @@ import ( "strings" "testing" - "github.com/hashicorp/terraform/config/module" + "github.com/hashicorp/terraform/addrs" + + "github.com/hashicorp/terraform/configs" + "github.com/zclconf/go-cty/cty" ) func TestPlanContextOpts(t *testing.T) { @@ -18,12 +21,12 @@ func TestPlanContextOpts(t *testing.T) { }, }, }, - Module: module.NewTree("test", nil), + Config: configs.NewEmptyConfig(), State: &State{ TFVersion: "sigil", }, - Vars: map[string]interface{}{"foo": "bar"}, - Targets: []string{"baz"}, + Vars: map[string]cty.Value{"foo": cty.StringVal("bar")}, + Targets: []string{"baz.bar"}, TerraformVersion: VersionString(), ProviderSHA256s: map[string][]byte{ @@ -37,11 +40,18 @@ func TestPlanContextOpts(t *testing.T) { } want := &ContextOpts{ - Diff: plan.Diff, - Module: plan.Module, - State: plan.State, - Variables: plan.Vars, - Targets: plan.Targets, + Diff: plan.Diff, + Config: plan.Config, + State: plan.State, + Variables: InputValues{ + "foo": &InputValue{ + Value: cty.StringVal("bar"), + SourceType: ValueFromPlan, + }, + }, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Resource(addrs.ManagedResourceMode, "baz", "bar"), + }, ProviderSHA256s: plan.ProviderSHA256s, } @@ -52,7 +62,7 @@ func TestPlanContextOpts(t *testing.T) { func TestReadWritePlan(t *testing.T) { plan := &Plan{ - Module: testModule(t, "new-good"), + Config: testModule(t, "new-good"), Diff: &Diff{ Modules: []*ModuleDiff{ &ModuleDiff{ @@ -97,8 +107,8 @@ func TestReadWritePlan(t *testing.T) { }, }, }, - Vars: map[string]interface{}{ - "foo": "bar", + Vars: map[string]cty.Value{ + "foo": cty.StringVal("bar"), }, } @@ -128,13 +138,13 @@ func TestPlanContextOptsOverrideStateGood(t *testing.T) { }, }, }, - Module: module.NewTree("test", nil), + Config: configs.NewEmptyConfig(), State: &State{ TFVersion: "sigil", Serial: 1, }, - Vars: map[string]interface{}{"foo": "bar"}, - Targets: []string{"baz"}, + Vars: map[string]cty.Value{"foo": cty.StringVal("bar")}, + Targets: []string{"baz.bar"}, TerraformVersion: VersionString(), ProviderSHA256s: map[string][]byte{ @@ -155,11 +165,18 @@ func TestPlanContextOptsOverrideStateGood(t *testing.T) { } want := &ContextOpts{ - Diff: plan.Diff, - Module: plan.Module, - State: base.State, - Variables: plan.Vars, - Targets: plan.Targets, + Diff: plan.Diff, + Config: plan.Config, + State: base.State, + Variables: InputValues{ + "foo": &InputValue{ + Value: cty.StringVal("bar"), + SourceType: ValueFromPlan, + }, + }, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Resource(addrs.ManagedResourceMode, "baz", "bar"), + }, ProviderSHA256s: plan.ProviderSHA256s, } diff --git a/terraform/state.go b/terraform/state.go index eddbcffac..da3f01e0e 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -229,90 +229,6 @@ func (s *State) moduleByPath(path addrs.ModuleInstance) *ModuleState { return nil } -// ModuleOrphans returns all the module orphans in this state by -// returning their full paths. These paths can be used with ModuleByPath -// to return the actual state. -func (s *State) ModuleOrphans(path []string, c *config.Config) [][]string { - s.Lock() - defer s.Unlock() - - return s.moduleOrphans(path, c) - -} - -func (s *State) moduleOrphans(path []string, c *config.Config) [][]string { - // direct keeps track of what direct children we have both in our config - // and in our state. childrenKeys keeps track of what isn't an orphan. - direct := make(map[string]struct{}) - childrenKeys := make(map[string]struct{}) - if c != nil { - for _, m := range c.Modules { - childrenKeys[m.Name] = struct{}{} - direct[m.Name] = struct{}{} - } - } - - // Go over the direct children and find any that aren't in our keys. - var orphans [][]string - for _, m := range s.children(path) { - key := m.Path[len(m.Path)-1] - - // Record that we found this key as a direct child. We use this - // later to find orphan nested modules. - direct[key] = struct{}{} - - // If we have a direct child still in our config, it is not an orphan - if _, ok := childrenKeys[key]; ok { - continue - } - - orphans = append(orphans, m.Path) - } - - // Find the orphans that are nested... - for _, m := range s.Modules { - if m == nil { - continue - } - - // We only want modules that are at least grandchildren - if len(m.Path) < len(path)+2 { - continue - } - - // If it isn't part of our tree, continue - if !reflect.DeepEqual(path, m.Path[:len(path)]) { - continue - } - - // If we have the direct child, then just skip it. - key := m.Path[len(path)] - if _, ok := direct[key]; ok { - continue - } - - orphanPath := m.Path[:len(path)+1] - - // Don't double-add if we've already added this orphan (which can happen if - // there are multiple nested sub-modules that get orphaned together). - alreadyAdded := false - for _, o := range orphans { - if reflect.DeepEqual(o, orphanPath) { - alreadyAdded = true - break - } - } - if alreadyAdded { - continue - } - - // Add this orphan - orphans = append(orphans, orphanPath) - } - - return orphans -} - // Empty returns true if the state is empty. func (s *State) Empty() bool { if s == nil { diff --git a/terraform/state_test.go b/terraform/state_test.go index 5dc5ee333..8ace3315c 100644 --- a/terraform/state_test.go +++ b/terraform/state_test.go @@ -10,6 +10,8 @@ import ( "strings" "testing" + "github.com/hashicorp/terraform/addrs" + "github.com/hashicorp/terraform/config" ) @@ -51,49 +53,49 @@ func TestStateValidate(t *testing.T) { func TestStateAddModule(t *testing.T) { cases := []struct { - In [][]string - Out [][]string + In []addrs.ModuleInstance + Out []addrs.ModuleInstance }{ { - [][]string{ - []string{"root"}, - []string{"root", "child"}, + []addrs.ModuleInstance{ + addrs.RootModuleInstance, + addrs.RootModuleInstance.Child("child", addrs.NoKey), }, - [][]string{ - []string{"root"}, - []string{"root", "child"}, + []addrs.ModuleInstance{ + addrs.RootModuleInstance, + addrs.RootModuleInstance.Child("child", addrs.NoKey), }, }, { - [][]string{ - []string{"root", "foo", "bar"}, - []string{"root", "foo"}, - []string{"root"}, - []string{"root", "bar"}, + []addrs.ModuleInstance{ + addrs.RootModuleInstance.Child("foo", addrs.NoKey).Child("bar", addrs.NoKey), + addrs.RootModuleInstance.Child("foo", addrs.NoKey), + addrs.RootModuleInstance, + addrs.RootModuleInstance.Child("bar", addrs.NoKey), }, - [][]string{ - []string{"root"}, - []string{"root", "bar"}, - []string{"root", "foo"}, - []string{"root", "foo", "bar"}, + []addrs.ModuleInstance{ + addrs.RootModuleInstance, + addrs.RootModuleInstance.Child("bar", addrs.NoKey), + addrs.RootModuleInstance.Child("foo", addrs.NoKey), + addrs.RootModuleInstance.Child("foo", addrs.NoKey).Child("bar", addrs.NoKey), }, }, // Same last element, different middle element { - [][]string{ - []string{"root", "foo", "bar"}, // This one should sort after... - []string{"root", "foo"}, - []string{"root"}, - []string{"root", "bar", "bar"}, // ...this one. - []string{"root", "bar"}, + []addrs.ModuleInstance{ + addrs.RootModuleInstance.Child("foo", addrs.NoKey).Child("bar", addrs.NoKey), // This one should sort after... + addrs.RootModuleInstance.Child("foo", addrs.NoKey), + addrs.RootModuleInstance, + addrs.RootModuleInstance.Child("bar", addrs.NoKey).Child("bar", addrs.NoKey), // ...this one. + addrs.RootModuleInstance.Child("bar", addrs.NoKey), }, - [][]string{ - []string{"root"}, - []string{"root", "bar"}, - []string{"root", "foo"}, - []string{"root", "bar", "bar"}, - []string{"root", "foo", "bar"}, + []addrs.ModuleInstance{ + addrs.RootModuleInstance, + addrs.RootModuleInstance.Child("bar", addrs.NoKey), + addrs.RootModuleInstance.Child("foo", addrs.NoKey), + addrs.RootModuleInstance.Child("bar", addrs.NoKey).Child("bar", addrs.NoKey), + addrs.RootModuleInstance.Child("foo", addrs.NoKey).Child("bar", addrs.NoKey), }, }, } @@ -119,7 +121,7 @@ func TestStateOutputTypeRoundTrip(t *testing.T) { state := &State{ Modules: []*ModuleState{ &ModuleState{ - Path: RootModulePath, + Path: []string{"root"}, Outputs: map[string]*OutputState{ "string_output": &OutputState{ Value: "String Value", @@ -147,113 +149,6 @@ func TestStateOutputTypeRoundTrip(t *testing.T) { } } -func TestStateModuleOrphans(t *testing.T) { - state := &State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: RootModulePath, - }, - &ModuleState{ - Path: []string{RootModuleName, "foo"}, - }, - &ModuleState{ - Path: []string{RootModuleName, "bar"}, - }, - }, - } - - state.init() - - config := testModule(t, "state-module-orphans").Config() - actual := state.ModuleOrphans(RootModulePath, config) - expected := [][]string{ - []string{RootModuleName, "foo"}, - } - - if !reflect.DeepEqual(actual, expected) { - t.Fatalf("bad: %#v", actual) - } -} - -func TestStateModuleOrphans_nested(t *testing.T) { - state := &State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: RootModulePath, - }, - &ModuleState{ - Path: []string{RootModuleName, "foo", "bar"}, - }, - }, - } - - state.init() - - actual := state.ModuleOrphans(RootModulePath, nil) - expected := [][]string{ - []string{RootModuleName, "foo"}, - } - - if !reflect.DeepEqual(actual, expected) { - t.Fatalf("bad: %#v", actual) - } -} - -func TestStateModuleOrphans_nilConfig(t *testing.T) { - state := &State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: RootModulePath, - }, - &ModuleState{ - Path: []string{RootModuleName, "foo"}, - }, - &ModuleState{ - Path: []string{RootModuleName, "bar"}, - }, - }, - } - - state.init() - - actual := state.ModuleOrphans(RootModulePath, nil) - expected := [][]string{ - []string{RootModuleName, "foo"}, - []string{RootModuleName, "bar"}, - } - - if !reflect.DeepEqual(actual, expected) { - t.Fatalf("bad: %#v", actual) - } -} - -func TestStateModuleOrphans_deepNestedNilConfig(t *testing.T) { - state := &State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: RootModulePath, - }, - &ModuleState{ - Path: []string{RootModuleName, "parent", "childfoo"}, - }, - &ModuleState{ - Path: []string{RootModuleName, "parent", "childbar"}, - }, - }, - } - - state.init() - - actual := state.ModuleOrphans(RootModulePath, nil) - expected := [][]string{ - []string{RootModuleName, "parent"}, - } - - if !reflect.DeepEqual(actual, expected) { - t.Fatalf("bad: %#v", actual) - } -} - func TestStateDeepCopy(t *testing.T) { cases := []struct { State *State @@ -360,7 +255,7 @@ func TestStateEqual(t *testing.T) { &State{ Modules: []*ModuleState{ &ModuleState{ - Path: RootModulePath, + Path: []string{"root"}, }, }, }, @@ -373,14 +268,14 @@ func TestStateEqual(t *testing.T) { &State{ Modules: []*ModuleState{ &ModuleState{ - Path: RootModulePath, + Path: []string{"root"}, }, }, }, &State{ Modules: []*ModuleState{ &ModuleState{ - Path: RootModulePath, + Path: []string{"root"}, }, }, }, @@ -1864,28 +1759,6 @@ func TestParseResourceStateKey(t *testing.T) { } } -func TestStateModuleOrphans_empty(t *testing.T) { - state := &State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: RootModulePath, - }, - &ModuleState{ - Path: []string{RootModuleName, "foo", "bar"}, - }, - &ModuleState{ - Path: []string{}, - }, - nil, - }, - } - - state.init() - - // just calling this to check for panic - state.ModuleOrphans(RootModulePath, nil) -} - func TestReadState_prune(t *testing.T) { state := &State{ Modules: []*ModuleState{ diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 1599e71ad..edf77292d 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -16,7 +16,6 @@ import ( "github.com/hashicorp/terraform/configs" - "github.com/hashicorp/terraform/config/module" "github.com/hashicorp/terraform/helper/experiment" "github.com/hashicorp/terraform/helper/logging" ) @@ -84,7 +83,6 @@ func testModule(t *testing.T, name string) *configs.Config { t.Helper() dir := filepath.Join(fixtureDir, name) - p := configs.NewParser(nil) // FIXME: We're not dealing with the cleanup function here because // this testModule function is used all over and so we don't want to @@ -107,7 +105,7 @@ func testModule(t *testing.T, name string) *configs.Config { // testModuleInline takes a map of path -> config strings and yields a config // structure with those files loaded from disk -func testModuleInline(t *testing.T, config map[string]string) *module.Tree { +func testModuleInline(t *testing.T, sources map[string]string) *configs.Config { t.Helper() cfgPath, err := ioutil.TempDir("", "tf-test") @@ -116,7 +114,7 @@ func testModuleInline(t *testing.T, config map[string]string) *module.Tree { } defer os.RemoveAll(cfgPath) - for path, configStr := range config { + for path, configStr := range sources { dir := filepath.Dir(path) if dir != "." { err := os.MkdirAll(filepath.Join(cfgPath, dir), os.FileMode(0777)) @@ -137,23 +135,23 @@ func testModuleInline(t *testing.T, config map[string]string) *module.Tree { } } - // Parse the configuration - mod, err := module.NewTreeModule("", cfgPath) - if err != nil { - t.Fatalf("Error loading configuration: %s", err) + // FIXME: We're not dealing with the cleanup function here because + // this testModule function is used all over and so we don't want to + // change its interface at this late stage. + loader, _ := configload.NewLoaderForTests(t) + + // Test modules usually do not refer to remote sources, and for local + // sources only this ultimately just records all of the module paths + // in a JSON file so that we can load them below. + diags := loader.InstallModules(cfgPath, true, configload.InstallHooksImpl{}) + t.Fatal(diags.Error()) + + config, diags := loader.LoadConfig(cfgPath) + if diags.HasErrors() { + t.Fatal(diags.Error()) } - // Load the modules - modStorage := &module.Storage{ - StorageDir: filepath.Join(cfgPath, ".tfmodules"), - Mode: module.GetModeGet, - } - err = mod.Load(modStorage) - if err != nil { - t.Errorf("Error downloading modules: %s", err) - } - - return mod + return config } func testStringMatch(t *testing.T, s fmt.Stringer, expected string) { diff --git a/terraform/transform_config_flat_test.go b/terraform/transform_config_flat_test.go index aa393d5a9..79fc09367 100644 --- a/terraform/transform_config_flat_test.go +++ b/terraform/transform_config_flat_test.go @@ -3,10 +3,12 @@ package terraform import ( "strings" "testing" + + "github.com/hashicorp/terraform/addrs" ) func TestFlatConfigTransformer_nilModule(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} tf := &FlatConfigTransformer{} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) @@ -18,9 +20,9 @@ func TestFlatConfigTransformer_nilModule(t *testing.T) { } func TestFlatConfigTransformer(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} tf := &FlatConfigTransformer{ - Module: testModule(t, "transform-flat-config-basic"), + Config: testModule(t, "transform-flat-config-basic"), } if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) diff --git a/terraform/transform_config_test.go b/terraform/transform_config_test.go index 1b2c27cbe..48157e038 100644 --- a/terraform/transform_config_test.go +++ b/terraform/transform_config_test.go @@ -1,16 +1,14 @@ package terraform import ( - "path/filepath" "strings" "testing" - "github.com/hashicorp/terraform/config" - "github.com/hashicorp/terraform/config/module" + "github.com/hashicorp/terraform/addrs" ) func TestConfigTransformer_nilModule(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} tf := &ConfigTransformer{} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) @@ -21,23 +19,9 @@ func TestConfigTransformer_nilModule(t *testing.T) { } } -func TestConfigTransformer_unloadedModule(t *testing.T) { - mod, err := module.NewTreeModule( - "", filepath.Join(fixtureDir, "graph-basic")) - if err != nil { - t.Fatalf("err: %s", err) - } - - g := Graph{Path: RootModulePath} - tf := &ConfigTransformer{Module: mod} - if err := tf.Transform(&g); err == nil { - t.Fatal("should error") - } -} - func TestConfigTransformer(t *testing.T) { - g := Graph{Path: RootModulePath} - tf := &ConfigTransformer{Module: testModule(t, "graph-basic")} + g := Graph{Path: addrs.RootModuleInstance} + tf := &ConfigTransformer{Config: testModule(t, "graph-basic")} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -50,11 +34,11 @@ func TestConfigTransformer(t *testing.T) { } func TestConfigTransformer_mode(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} tf := &ConfigTransformer{ - Module: testModule(t, "transform-config-mode-data"), + Config: testModule(t, "transform-config-mode-data"), ModeFilter: true, - Mode: config.DataResourceMode, + Mode: addrs.DataResourceMode, } if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) @@ -70,14 +54,13 @@ data.aws_ami.foo } func TestConfigTransformer_nonUnique(t *testing.T) { - addr, err := ParseResourceAddress("aws_instance.web") - if err != nil { - t.Fatalf("bad: %s", err) - } - - g := Graph{Path: RootModulePath} - g.Add(&NodeAbstractResource{Addr: addr}) - tf := &ConfigTransformer{Module: testModule(t, "graph-basic")} + g := Graph{Path: addrs.RootModuleInstance} + g.Add(NewNodeAbstractResource( + addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "web", + ), + )) + tf := &ConfigTransformer{Config: testModule(t, "graph-basic")} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -96,15 +79,14 @@ openstack_floating_ip.random } func TestConfigTransformer_unique(t *testing.T) { - addr, err := ParseResourceAddress("aws_instance.web") - if err != nil { - t.Fatalf("bad: %s", err) - } - - g := Graph{Path: RootModulePath} - g.Add(&NodeAbstractResource{Addr: addr}) + g := Graph{Path: addrs.RootModuleInstance} + g.Add(NewNodeAbstractResource( + addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "web", + ), + )) tf := &ConfigTransformer{ - Module: testModule(t, "graph-basic"), + Config: testModule(t, "graph-basic"), Unique: true, } if err := tf.Transform(&g); err != nil { diff --git a/terraform/transform_destroy_cbd_test.go b/terraform/transform_destroy_cbd_test.go index 0f79a1987..07d30810b 100644 --- a/terraform/transform_destroy_cbd_test.go +++ b/terraform/transform_destroy_cbd_test.go @@ -3,10 +3,12 @@ package terraform import ( "strings" "testing" + + "github.com/hashicorp/terraform/addrs" ) func TestCBDEdgeTransformer(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} g.Add(&graphNodeCreatorTest{AddrString: "test.A"}) g.Add(&graphNodeCreatorTest{AddrString: "test.B"}) g.Add(&graphNodeDestroyerTest{AddrString: "test.A", CBD: true}) @@ -15,7 +17,7 @@ func TestCBDEdgeTransformer(t *testing.T) { { tf := &DestroyEdgeTransformer{ - Module: module, + Config: module, } if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) @@ -23,7 +25,7 @@ func TestCBDEdgeTransformer(t *testing.T) { } { - tf := &CBDEdgeTransformer{Module: module} + tf := &CBDEdgeTransformer{Config: module} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -37,7 +39,7 @@ func TestCBDEdgeTransformer(t *testing.T) { } func TestCBDEdgeTransformer_depNonCBD(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} g.Add(&graphNodeCreatorTest{AddrString: "test.A"}) g.Add(&graphNodeCreatorTest{AddrString: "test.B"}) g.Add(&graphNodeDestroyerTest{AddrString: "test.A"}) @@ -47,7 +49,7 @@ func TestCBDEdgeTransformer_depNonCBD(t *testing.T) { { tf := &DestroyEdgeTransformer{ - Module: module, + Config: module, } if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) @@ -55,7 +57,7 @@ func TestCBDEdgeTransformer_depNonCBD(t *testing.T) { } { - tf := &CBDEdgeTransformer{Module: module} + tf := &CBDEdgeTransformer{Config: module} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -69,7 +71,7 @@ func TestCBDEdgeTransformer_depNonCBD(t *testing.T) { } func TestCBDEdgeTransformer_depNonCBDCount(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} g.Add(&graphNodeCreatorTest{AddrString: "test.A"}) g.Add(&graphNodeCreatorTest{AddrString: "test.B[0]"}) g.Add(&graphNodeCreatorTest{AddrString: "test.B[1]"}) @@ -79,7 +81,7 @@ func TestCBDEdgeTransformer_depNonCBDCount(t *testing.T) { { tf := &DestroyEdgeTransformer{ - Module: module, + Config: module, } if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) @@ -87,7 +89,7 @@ func TestCBDEdgeTransformer_depNonCBDCount(t *testing.T) { } { - tf := &CBDEdgeTransformer{Module: module} + tf := &CBDEdgeTransformer{Config: module} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -109,7 +111,7 @@ test.B[1] } func TestCBDEdgeTransformer_depNonCBDCountBoth(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} g.Add(&graphNodeCreatorTest{AddrString: "test.A[0]"}) g.Add(&graphNodeCreatorTest{AddrString: "test.A[1]"}) g.Add(&graphNodeCreatorTest{AddrString: "test.B[0]"}) @@ -121,7 +123,7 @@ func TestCBDEdgeTransformer_depNonCBDCountBoth(t *testing.T) { { tf := &DestroyEdgeTransformer{ - Module: module, + Config: module, } if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) @@ -129,7 +131,7 @@ func TestCBDEdgeTransformer_depNonCBDCountBoth(t *testing.T) { } { - tf := &CBDEdgeTransformer{Module: module} + tf := &CBDEdgeTransformer{Config: module} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } diff --git a/terraform/transform_destroy_edge_test.go b/terraform/transform_destroy_edge_test.go index a0792493c..73acc15db 100644 --- a/terraform/transform_destroy_edge_test.go +++ b/terraform/transform_destroy_edge_test.go @@ -3,14 +3,16 @@ package terraform import ( "strings" "testing" + + "github.com/hashicorp/terraform/addrs" ) func TestDestroyEdgeTransformer_basic(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} g.Add(&graphNodeDestroyerTest{AddrString: "test.A"}) g.Add(&graphNodeDestroyerTest{AddrString: "test.B"}) tf := &DestroyEdgeTransformer{ - Module: testModule(t, "transform-destroy-edge-basic"), + Config: testModule(t, "transform-destroy-edge-basic"), } if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) @@ -24,12 +26,12 @@ func TestDestroyEdgeTransformer_basic(t *testing.T) { } func TestDestroyEdgeTransformer_create(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} g.Add(&graphNodeDestroyerTest{AddrString: "test.A"}) g.Add(&graphNodeDestroyerTest{AddrString: "test.B"}) g.Add(&graphNodeCreatorTest{AddrString: "test.A"}) tf := &DestroyEdgeTransformer{ - Module: testModule(t, "transform-destroy-edge-basic"), + Config: testModule(t, "transform-destroy-edge-basic"), } if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) @@ -43,12 +45,12 @@ func TestDestroyEdgeTransformer_create(t *testing.T) { } func TestDestroyEdgeTransformer_multi(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} g.Add(&graphNodeDestroyerTest{AddrString: "test.A"}) g.Add(&graphNodeDestroyerTest{AddrString: "test.B"}) g.Add(&graphNodeDestroyerTest{AddrString: "test.C"}) tf := &DestroyEdgeTransformer{ - Module: testModule(t, "transform-destroy-edge-multi"), + Config: testModule(t, "transform-destroy-edge-multi"), } if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) @@ -62,10 +64,10 @@ func TestDestroyEdgeTransformer_multi(t *testing.T) { } func TestDestroyEdgeTransformer_selfRef(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} g.Add(&graphNodeDestroyerTest{AddrString: "test.A"}) tf := &DestroyEdgeTransformer{ - Module: testModule(t, "transform-destroy-edge-self-ref"), + Config: testModule(t, "transform-destroy-edge-self-ref"), } if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) @@ -79,11 +81,11 @@ func TestDestroyEdgeTransformer_selfRef(t *testing.T) { } func TestDestroyEdgeTransformer_module(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} g.Add(&graphNodeDestroyerTest{AddrString: "module.child.aws_instance.b"}) g.Add(&graphNodeDestroyerTest{AddrString: "aws_instance.a"}) tf := &DestroyEdgeTransformer{ - Module: testModule(t, "transform-destroy-edge-module"), + Config: testModule(t, "transform-destroy-edge-module"), } if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) @@ -97,12 +99,12 @@ func TestDestroyEdgeTransformer_module(t *testing.T) { } func TestDestroyEdgeTransformer_moduleOnly(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} g.Add(&graphNodeDestroyerTest{AddrString: "module.child.aws_instance.a"}) g.Add(&graphNodeDestroyerTest{AddrString: "module.child.aws_instance.b"}) g.Add(&graphNodeDestroyerTest{AddrString: "module.child.aws_instance.c"}) tf := &DestroyEdgeTransformer{ - Module: testModule(t, "transform-destroy-edge-module-only"), + Config: testModule(t, "transform-destroy-edge-module-only"), } if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) diff --git a/terraform/transform_diff_test.go b/terraform/transform_diff_test.go index c2505b087..b6a54c06f 100644 --- a/terraform/transform_diff_test.go +++ b/terraform/transform_diff_test.go @@ -3,10 +3,12 @@ package terraform import ( "strings" "testing" + + "github.com/hashicorp/terraform/addrs" ) func TestDiffTransformer_nilDiff(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} tf := &DiffTransformer{} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) @@ -18,7 +20,7 @@ func TestDiffTransformer_nilDiff(t *testing.T) { } func TestDiffTransformer(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} tf := &DiffTransformer{ Diff: &Diff{ Modules: []*ModuleDiff{ diff --git a/terraform/transform_module_variable_test.go b/terraform/transform_module_variable_test.go index bd1f9525b..619a20405 100644 --- a/terraform/transform_module_variable_test.go +++ b/terraform/transform_module_variable_test.go @@ -3,21 +3,23 @@ package terraform import ( "strings" "testing" + + "github.com/hashicorp/terraform/addrs" ) func TestModuleVariableTransformer(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} module := testModule(t, "transform-module-var-basic") { - tf := &RootVariableTransformer{Module: module} + tf := &RootVariableTransformer{Config: module} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } } { - tf := &ModuleVariableTransformer{Module: module, DisablePrune: true} + tf := &ModuleVariableTransformer{Config: module} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -31,18 +33,18 @@ func TestModuleVariableTransformer(t *testing.T) { } func TestModuleVariableTransformer_nested(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} module := testModule(t, "transform-module-var-nested") { - tf := &RootVariableTransformer{Module: module} + tf := &RootVariableTransformer{Config: module} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } } { - tf := &ModuleVariableTransformer{Module: module, DisablePrune: true} + tf := &ModuleVariableTransformer{Config: module} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } diff --git a/terraform/transform_orphan_count_test.go b/terraform/transform_orphan_count_test.go index d2da60e7d..210c61e99 100644 --- a/terraform/transform_orphan_count_test.go +++ b/terraform/transform_orphan_count_test.go @@ -3,18 +3,15 @@ package terraform import ( "strings" "testing" + + "github.com/hashicorp/terraform/addrs" ) func TestOrphanResourceCountTransformer(t *testing.T) { - addr, err := parseResourceAddressInternal("aws_instance.foo") - if err != nil { - t.Fatalf("err: %s", err) - } - state := &State{ Modules: []*ModuleState{ &ModuleState{ - Path: RootModulePath, + Path: []string{"root"}, Resources: map[string]*ResourceState{ "aws_instance.web": &ResourceState{ Type: "aws_instance", @@ -41,14 +38,16 @@ func TestOrphanResourceCountTransformer(t *testing.T) { }, } - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { tf := &OrphanResourceCountTransformer{ Concrete: testOrphanResourceConcreteFunc, Count: 1, - Addr: addr, - State: state, + Addr: addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "foo", + ), + State: state, } if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) @@ -63,15 +62,10 @@ func TestOrphanResourceCountTransformer(t *testing.T) { } func TestOrphanResourceCountTransformer_zero(t *testing.T) { - addr, err := parseResourceAddressInternal("aws_instance.foo") - if err != nil { - t.Fatalf("err: %s", err) - } - state := &State{ Modules: []*ModuleState{ &ModuleState{ - Path: RootModulePath, + Path: []string{"root"}, Resources: map[string]*ResourceState{ "aws_instance.web": &ResourceState{ Type: "aws_instance", @@ -98,14 +92,16 @@ func TestOrphanResourceCountTransformer_zero(t *testing.T) { }, } - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { tf := &OrphanResourceCountTransformer{ Concrete: testOrphanResourceConcreteFunc, Count: 0, - Addr: addr, - State: state, + Addr: addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "foo", + ), + State: state, } if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) @@ -120,15 +116,10 @@ func TestOrphanResourceCountTransformer_zero(t *testing.T) { } func TestOrphanResourceCountTransformer_oneNoIndex(t *testing.T) { - addr, err := parseResourceAddressInternal("aws_instance.foo") - if err != nil { - t.Fatalf("err: %s", err) - } - state := &State{ Modules: []*ModuleState{ &ModuleState{ - Path: RootModulePath, + Path: []string{"root"}, Resources: map[string]*ResourceState{ "aws_instance.web": &ResourceState{ Type: "aws_instance", @@ -155,14 +146,16 @@ func TestOrphanResourceCountTransformer_oneNoIndex(t *testing.T) { }, } - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { tf := &OrphanResourceCountTransformer{ Concrete: testOrphanResourceConcreteFunc, Count: 1, - Addr: addr, - State: state, + Addr: addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "foo", + ), + State: state, } if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) @@ -177,15 +170,10 @@ func TestOrphanResourceCountTransformer_oneNoIndex(t *testing.T) { } func TestOrphanResourceCountTransformer_oneIndex(t *testing.T) { - addr, err := parseResourceAddressInternal("aws_instance.foo") - if err != nil { - t.Fatalf("err: %s", err) - } - state := &State{ Modules: []*ModuleState{ &ModuleState{ - Path: RootModulePath, + Path: []string{"root"}, Resources: map[string]*ResourceState{ "aws_instance.web": &ResourceState{ Type: "aws_instance", @@ -212,14 +200,16 @@ func TestOrphanResourceCountTransformer_oneIndex(t *testing.T) { }, } - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { tf := &OrphanResourceCountTransformer{ Concrete: testOrphanResourceConcreteFunc, Count: 1, - Addr: addr, - State: state, + Addr: addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "foo", + ), + State: state, } if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) @@ -234,15 +224,10 @@ func TestOrphanResourceCountTransformer_oneIndex(t *testing.T) { } func TestOrphanResourceCountTransformer_zeroAndNone(t *testing.T) { - addr, err := parseResourceAddressInternal("aws_instance.foo") - if err != nil { - t.Fatalf("err: %s", err) - } - state := &State{ Modules: []*ModuleState{ &ModuleState{ - Path: RootModulePath, + Path: []string{"root"}, Resources: map[string]*ResourceState{ "aws_instance.web": &ResourceState{ Type: "aws_instance", @@ -269,14 +254,16 @@ func TestOrphanResourceCountTransformer_zeroAndNone(t *testing.T) { }, } - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { tf := &OrphanResourceCountTransformer{ Concrete: testOrphanResourceConcreteFunc, Count: 1, - Addr: addr, - State: state, + Addr: addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "foo", + ), + State: state, } if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) @@ -291,15 +278,10 @@ func TestOrphanResourceCountTransformer_zeroAndNone(t *testing.T) { } func TestOrphanResourceCountTransformer_zeroAndNoneCount(t *testing.T) { - addr, err := parseResourceAddressInternal("aws_instance.foo") - if err != nil { - t.Fatalf("err: %s", err) - } - state := &State{ Modules: []*ModuleState{ &ModuleState{ - Path: RootModulePath, + Path: []string{"root"}, Resources: map[string]*ResourceState{ "aws_instance.web": &ResourceState{ Type: "aws_instance", @@ -326,14 +308,16 @@ func TestOrphanResourceCountTransformer_zeroAndNoneCount(t *testing.T) { }, } - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { tf := &OrphanResourceCountTransformer{ Concrete: testOrphanResourceConcreteFunc, Count: 2, - Addr: addr, - State: state, + Addr: addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "foo", + ), + State: state, } if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) diff --git a/terraform/transform_orphan_resource_test.go b/terraform/transform_orphan_resource_test.go index c26f056a1..b3f8de1ad 100644 --- a/terraform/transform_orphan_resource_test.go +++ b/terraform/transform_orphan_resource_test.go @@ -5,6 +5,8 @@ import ( "strings" "testing" + "github.com/hashicorp/terraform/addrs" + "github.com/hashicorp/terraform/dag" ) @@ -13,7 +15,7 @@ func TestOrphanResourceTransformer(t *testing.T) { state := &State{ Modules: []*ModuleState{ &ModuleState{ - Path: RootModulePath, + Path: []string{"root"}, Resources: map[string]*ResourceState{ "aws_instance.web": &ResourceState{ Type: "aws_instance", @@ -34,9 +36,9 @@ func TestOrphanResourceTransformer(t *testing.T) { }, } - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { - tf := &ConfigTransformer{Module: mod} + tf := &ConfigTransformer{Config: mod} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -45,7 +47,8 @@ func TestOrphanResourceTransformer(t *testing.T) { { tf := &OrphanResourceTransformer{ Concrete: testOrphanResourceConcreteFunc, - State: state, Module: mod, + State: state, + Config: mod, } if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) @@ -65,9 +68,9 @@ func TestOrphanResourceTransformer_nilModule(t *testing.T) { Modules: []*ModuleState{nil}, } - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { - tf := &ConfigTransformer{Module: mod} + tf := &ConfigTransformer{Config: mod} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -76,7 +79,8 @@ func TestOrphanResourceTransformer_nilModule(t *testing.T) { { tf := &OrphanResourceTransformer{ Concrete: testOrphanResourceConcreteFunc, - State: state, Module: mod, + State: state, + Config: mod, } if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) @@ -89,7 +93,7 @@ func TestOrphanResourceTransformer_countGood(t *testing.T) { state := &State{ Modules: []*ModuleState{ &ModuleState{ - Path: RootModulePath, + Path: []string{"root"}, Resources: map[string]*ResourceState{ "aws_instance.foo.0": &ResourceState{ Type: "aws_instance", @@ -109,9 +113,9 @@ func TestOrphanResourceTransformer_countGood(t *testing.T) { }, } - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { - tf := &ConfigTransformer{Module: mod} + tf := &ConfigTransformer{Config: mod} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -120,7 +124,8 @@ func TestOrphanResourceTransformer_countGood(t *testing.T) { { tf := &OrphanResourceTransformer{ Concrete: testOrphanResourceConcreteFunc, - State: state, Module: mod, + State: state, + Config: mod, } if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) @@ -139,7 +144,7 @@ func TestOrphanResourceTransformer_countBad(t *testing.T) { state := &State{ Modules: []*ModuleState{ &ModuleState{ - Path: RootModulePath, + Path: []string{"root"}, Resources: map[string]*ResourceState{ "aws_instance.foo.0": &ResourceState{ Type: "aws_instance", @@ -159,9 +164,9 @@ func TestOrphanResourceTransformer_countBad(t *testing.T) { }, } - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { - tf := &ConfigTransformer{Module: mod} + tf := &ConfigTransformer{Config: mod} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -170,7 +175,8 @@ func TestOrphanResourceTransformer_countBad(t *testing.T) { { tf := &OrphanResourceTransformer{ Concrete: testOrphanResourceConcreteFunc, - State: state, Module: mod, + State: state, + Config: mod, } if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) @@ -189,7 +195,7 @@ func TestOrphanResourceTransformer_modules(t *testing.T) { state := &State{ Modules: []*ModuleState{ &ModuleState{ - Path: RootModulePath, + Path: []string{"root"}, Resources: map[string]*ResourceState{ "aws_instance.foo": &ResourceState{ Type: "aws_instance", @@ -214,9 +220,9 @@ func TestOrphanResourceTransformer_modules(t *testing.T) { }, } - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { - tf := &ConfigTransformer{Module: mod} + tf := &ConfigTransformer{Config: mod} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -225,7 +231,8 @@ func TestOrphanResourceTransformer_modules(t *testing.T) { { tf := &OrphanResourceTransformer{ Concrete: testOrphanResourceConcreteFunc, - State: state, Module: mod, + State: state, + Config: mod, } if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) @@ -258,14 +265,14 @@ aws_instance.foo module.child.aws_instance.web (orphan) ` -func testOrphanResourceConcreteFunc(a *NodeAbstractResource) dag.Vertex { - return &testOrphanResourceConcrete{a} +func testOrphanResourceConcreteFunc(a *NodeAbstractResourceInstance) dag.Vertex { + return &testOrphanResourceInstanceConcrete{a} } -type testOrphanResourceConcrete struct { - *NodeAbstractResource +type testOrphanResourceInstanceConcrete struct { + *NodeAbstractResourceInstance } -func (n *testOrphanResourceConcrete) Name() string { - return fmt.Sprintf("%s (orphan)", n.NodeAbstractResource.Name()) +func (n *testOrphanResourceInstanceConcrete) Name() string { + return fmt.Sprintf("%s (orphan)", n.NodeAbstractResourceInstance.Name()) } diff --git a/terraform/transform_provider_test.go b/terraform/transform_provider_test.go index a418b5b67..939d8de98 100644 --- a/terraform/transform_provider_test.go +++ b/terraform/transform_provider_test.go @@ -4,22 +4,23 @@ import ( "strings" "testing" + "github.com/hashicorp/terraform/addrs" "github.com/hashicorp/terraform/dag" ) func TestProviderTransformer(t *testing.T) { mod := testModule(t, "transform-provider-basic") - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { - tf := &ConfigTransformer{Module: mod} + tf := &ConfigTransformer{Config: mod} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } } { - transform := &AttachResourceConfigTransformer{Module: mod} + transform := &AttachResourceConfigTransformer{Config: mod} if err := transform.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -45,14 +46,21 @@ func TestProviderTransformer(t *testing.T) { } func TestProviderTransformer_moduleChild(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { tf := &ImportStateTransformer{ Targets: []*ImportTarget{ &ImportTarget{ - Addr: "module.moo.foo_instance.qux", - ID: "bar", + Addr: addrs.RootModuleInstance. + Child("moo", addrs.NoKey). + ResourceInstance( + addrs.ManagedResourceMode, + "foo_instance", + "qux", + addrs.NoKey, + ), + ID: "bar", }, }, } @@ -85,16 +93,16 @@ func TestProviderTransformer_moduleChild(t *testing.T) { func TestCloseProviderTransformer(t *testing.T) { mod := testModule(t, "transform-provider-basic") - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { - tf := &ConfigTransformer{Module: mod} + tf := &ConfigTransformer{Config: mod} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } } { - transform := &AttachResourceConfigTransformer{Module: mod} + transform := &AttachResourceConfigTransformer{Config: mod} if err := transform.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -131,14 +139,18 @@ func TestCloseProviderTransformer(t *testing.T) { func TestCloseProviderTransformer_withTargets(t *testing.T) { mod := testModule(t, "transform-provider-basic") - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} transforms := []GraphTransformer{ - &ConfigTransformer{Module: mod}, + &ConfigTransformer{Config: mod}, &MissingProviderTransformer{Providers: []string{"aws"}}, &ProviderTransformer{}, &CloseProviderTransformer{}, &TargetsTransformer{ - Targets: []string{"something.else"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "something", "else", + ), + }, }, } @@ -158,16 +170,16 @@ func TestCloseProviderTransformer_withTargets(t *testing.T) { func TestMissingProviderTransformer(t *testing.T) { mod := testModule(t, "transform-provider-missing") - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { - tf := &ConfigTransformer{Module: mod} + tf := &ConfigTransformer{Config: mod} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } } { - transform := &AttachResourceConfigTransformer{Module: mod} + transform := &AttachResourceConfigTransformer{Config: mod} if err := transform.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -206,16 +218,16 @@ func TestMissingProviderTransformer_grandchildMissing(t *testing.T) { concrete := func(a *NodeAbstractProvider) dag.Vertex { return a } - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { - tf := &ConfigTransformer{Module: mod} + tf := &ConfigTransformer{Config: mod} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } } { - transform := &AttachResourceConfigTransformer{Module: mod} + transform := &AttachResourceConfigTransformer{Config: mod} if err := transform.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -242,7 +254,7 @@ func TestMissingProviderTransformer_grandchildMissing(t *testing.T) { } func TestMissingProviderTransformer_moduleChild(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} // We use the import state transformer since at the time of writing // this test it is the first and only transformer that will introduce @@ -251,8 +263,15 @@ func TestMissingProviderTransformer_moduleChild(t *testing.T) { tf := &ImportStateTransformer{ Targets: []*ImportTarget{ &ImportTarget{ - Addr: "module.moo.foo_instance.qux", - ID: "bar", + Addr: addrs.RootModuleInstance. + Child("moo", addrs.NoKey). + ResourceInstance( + addrs.ManagedResourceMode, + "foo_instance", + "qux", + addrs.NoKey, + ), + ID: "bar", }, }, } @@ -276,7 +295,7 @@ func TestMissingProviderTransformer_moduleChild(t *testing.T) { } func TestMissingProviderTransformer_moduleGrandchild(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} // We use the import state transformer since at the time of writing // this test it is the first and only transformer that will introduce @@ -285,8 +304,16 @@ func TestMissingProviderTransformer_moduleGrandchild(t *testing.T) { tf := &ImportStateTransformer{ Targets: []*ImportTarget{ &ImportTarget{ - Addr: "module.a.module.b.foo_instance.qux", - ID: "bar", + Addr: addrs.RootModuleInstance. + Child("a", addrs.NoKey). + Child("b", addrs.NoKey). + ResourceInstance( + addrs.ManagedResourceMode, + "foo_instance", + "qux", + addrs.NoKey, + ), + ID: "bar", }, }, } @@ -310,15 +337,22 @@ func TestMissingProviderTransformer_moduleGrandchild(t *testing.T) { } func TestParentProviderTransformer(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} // Introduce a cihld module { tf := &ImportStateTransformer{ Targets: []*ImportTarget{ &ImportTarget{ - Addr: "module.moo.foo_instance.qux", - ID: "bar", + Addr: addrs.RootModuleInstance. + Child("moo", addrs.NoKey). + ResourceInstance( + addrs.ManagedResourceMode, + "foo_instance", + "qux", + addrs.NoKey, + ), + ID: "bar", }, }, } @@ -351,7 +385,7 @@ func TestParentProviderTransformer(t *testing.T) { } func TestParentProviderTransformer_moduleGrandchild(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} // We use the import state transformer since at the time of writing // this test it is the first and only transformer that will introduce @@ -360,8 +394,16 @@ func TestParentProviderTransformer_moduleGrandchild(t *testing.T) { tf := &ImportStateTransformer{ Targets: []*ImportTarget{ &ImportTarget{ - Addr: "module.a.module.b.foo_instance.qux", - ID: "bar", + Addr: addrs.RootModuleInstance. + Child("a", addrs.NoKey). + Child("b", addrs.NoKey). + ResourceInstance( + addrs.ManagedResourceMode, + "foo_instance", + "qux", + addrs.NoKey, + ), + ID: "bar", }, }, } @@ -395,16 +437,16 @@ func TestParentProviderTransformer_moduleGrandchild(t *testing.T) { func TestPruneProviderTransformer(t *testing.T) { mod := testModule(t, "transform-provider-prune") - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { - tf := &ConfigTransformer{Module: mod} + tf := &ConfigTransformer{Config: mod} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } } { - transform := &AttachResourceConfigTransformer{Module: mod} + transform := &AttachResourceConfigTransformer{Config: mod} if err := transform.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -450,15 +492,15 @@ func TestProviderConfigTransformer_parentProviders(t *testing.T) { mod := testModule(t, "transform-provider-inherit") concrete := func(a *NodeAbstractProvider) dag.Vertex { return a } - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { - tf := &ConfigTransformer{Module: mod} + tf := &ConfigTransformer{Config: mod} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } } { - tf := &AttachResourceConfigTransformer{Module: mod} + tf := &AttachResourceConfigTransformer{Config: mod} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -483,15 +525,15 @@ func TestProviderConfigTransformer_grandparentProviders(t *testing.T) { mod := testModule(t, "transform-provider-grandchild-inherit") concrete := func(a *NodeAbstractProvider) dag.Vertex { return a } - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { - tf := &ConfigTransformer{Module: mod} + tf := &ConfigTransformer{Config: mod} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } } { - tf := &AttachResourceConfigTransformer{Module: mod} + tf := &AttachResourceConfigTransformer{Config: mod} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -516,15 +558,15 @@ func TestProviderConfigTransformer_implicitModule(t *testing.T) { mod := testModule(t, "transform-provider-implicit-module") concrete := func(a *NodeAbstractProvider) dag.Vertex { return a } - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { - tf := &ConfigTransformer{Module: mod} + tf := &ConfigTransformer{Config: mod} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } } { - tf := &AttachResourceConfigTransformer{Module: mod} + tf := &AttachResourceConfigTransformer{Config: mod} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -550,15 +592,15 @@ func TestProviderConfigTransformer_invalidProvider(t *testing.T) { mod := testModule(t, "transform-provider-invalid") concrete := func(a *NodeAbstractProvider) dag.Vertex { return a } - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { - tf := &ConfigTransformer{Module: mod} + tf := &ConfigTransformer{Config: mod} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } } { - tf := &AttachResourceConfigTransformer{Module: mod} + tf := &AttachResourceConfigTransformer{Config: mod} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } diff --git a/terraform/transform_provisioner_test.go b/terraform/transform_provisioner_test.go index b3d7d4b66..b7002226f 100644 --- a/terraform/transform_provisioner_test.go +++ b/terraform/transform_provisioner_test.go @@ -4,22 +4,24 @@ import ( "strings" "testing" + "github.com/hashicorp/terraform/addrs" + "github.com/hashicorp/terraform/dag" ) func TestMissingProvisionerTransformer(t *testing.T) { mod := testModule(t, "transform-provisioner-basic") - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { - tf := &ConfigTransformer{Module: mod} + tf := &ConfigTransformer{Config: mod} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } } { - transform := &AttachResourceConfigTransformer{Module: mod} + transform := &AttachResourceConfigTransformer{Config: mod} if err := transform.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -49,9 +51,9 @@ func TestMissingProvisionerTransformer(t *testing.T) { func TestMissingProvisionerTransformer_module(t *testing.T) { mod := testModule(t, "transform-provisioner-module") - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { - concreteResource := func(a *NodeAbstractResource) dag.Vertex { + concreteResource := func(a *NodeAbstractResourceInstance) dag.Vertex { return a } @@ -87,7 +89,7 @@ func TestMissingProvisionerTransformer_module(t *testing.T) { } { - transform := &AttachResourceConfigTransformer{Module: mod} + transform := &AttachResourceConfigTransformer{Config: mod} if err := transform.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -117,16 +119,16 @@ func TestMissingProvisionerTransformer_module(t *testing.T) { func TestCloseProvisionerTransformer(t *testing.T) { mod := testModule(t, "transform-provisioner-basic") - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { - tf := &ConfigTransformer{Module: mod} + tf := &ConfigTransformer{Config: mod} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } } { - transform := &AttachResourceConfigTransformer{Module: mod} + transform := &AttachResourceConfigTransformer{Config: mod} if err := transform.Transform(&g); err != nil { t.Fatalf("err: %s", err) } diff --git a/terraform/transform_reference_test.go b/terraform/transform_reference_test.go index 6468bdf7e..dc03cdd1a 100644 --- a/terraform/transform_reference_test.go +++ b/terraform/transform_reference_test.go @@ -6,11 +6,12 @@ import ( "strings" "testing" + "github.com/hashicorp/terraform/addrs" "github.com/hashicorp/terraform/dag" ) func TestReferenceTransformer_simple(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} g.Add(&graphNodeRefParentTest{ NameValue: "A", Names: []string{"A"}, @@ -33,7 +34,7 @@ func TestReferenceTransformer_simple(t *testing.T) { } func TestReferenceTransformer_self(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} g.Add(&graphNodeRefParentTest{ NameValue: "A", Names: []string{"A"}, @@ -56,7 +57,7 @@ func TestReferenceTransformer_self(t *testing.T) { } func TestReferenceTransformer_path(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} g.Add(&graphNodeRefParentTest{ NameValue: "A", Names: []string{"A"}, @@ -89,7 +90,7 @@ func TestReferenceTransformer_path(t *testing.T) { } func TestReferenceTransformer_backup(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} g.Add(&graphNodeRefParentTest{ NameValue: "A", Names: []string{"A"}, @@ -112,7 +113,7 @@ func TestReferenceTransformer_backup(t *testing.T) { } func TestReferenceTransformer_backupPrimary(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} g.Add(&graphNodeRefParentTest{ NameValue: "A", Names: []string{"A"}, @@ -139,7 +140,7 @@ func TestReferenceTransformer_backupPrimary(t *testing.T) { } func TestReferenceTransformer_modulePath(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} g.Add(&graphNodeRefParentTest{ NameValue: "A", Names: []string{"A"}, @@ -163,7 +164,7 @@ func TestReferenceTransformer_modulePath(t *testing.T) { } func TestReferenceTransformer_modulePathNormalized(t *testing.T) { - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} g.Add(&graphNodeRefParentTest{ NameValue: "A", Names: []string{"A"}, @@ -258,7 +259,7 @@ func TestReferenceMapReferencedBy(t *testing.T) { for tn, tc := range cases { t.Run(tn, func(t *testing.T) { rm := NewReferenceMap(tc.Nodes) - result := rm.ReferencedBy(tc.Check) + result := rm.Referrers(tc.Check) var resultStr []string for _, v := range result { diff --git a/terraform/transform_root_test.go b/terraform/transform_root_test.go index 9f231906b..692fae90f 100644 --- a/terraform/transform_root_test.go +++ b/terraform/transform_root_test.go @@ -3,14 +3,16 @@ package terraform import ( "strings" "testing" + + "github.com/hashicorp/terraform/addrs" ) func TestRootTransformer(t *testing.T) { mod := testModule(t, "transform-root-basic") - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { - tf := &ConfigTransformer{Module: mod} + tf := &ConfigTransformer{Config: mod} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } diff --git a/terraform/transform_targets_test.go b/terraform/transform_targets_test.go index c5c97cd22..f29109af4 100644 --- a/terraform/transform_targets_test.go +++ b/terraform/transform_targets_test.go @@ -3,21 +3,23 @@ package terraform import ( "strings" "testing" + + "github.com/hashicorp/terraform/addrs" ) func TestTargetsTransformer(t *testing.T) { mod := testModule(t, "transform-targets-basic") - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { - tf := &ConfigTransformer{Module: mod} + tf := &ConfigTransformer{Config: mod} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } } { - transform := &AttachResourceConfigTransformer{Module: mod} + transform := &AttachResourceConfigTransformer{Config: mod} if err := transform.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -31,7 +33,13 @@ func TestTargetsTransformer(t *testing.T) { } { - transform := &TargetsTransformer{Targets: []string{"aws_instance.me"}} + transform := &TargetsTransformer{ + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "me", + ), + }, + } if err := transform.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -53,30 +61,30 @@ aws_vpc.me func TestTargetsTransformer_downstream(t *testing.T) { mod := testModule(t, "transform-targets-downstream") - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { - transform := &ConfigTransformer{Module: mod} + transform := &ConfigTransformer{Config: mod} if err := transform.Transform(&g); err != nil { t.Fatalf("%T failed: %s", transform, err) } } { - transform := &AttachResourceConfigTransformer{Module: mod} + transform := &AttachResourceConfigTransformer{Config: mod} if err := transform.Transform(&g); err != nil { t.Fatalf("%T failed: %s", transform, err) } } { - transform := &AttachResourceConfigTransformer{Module: mod} + transform := &AttachResourceConfigTransformer{Config: mod} if err := transform.Transform(&g); err != nil { t.Fatalf("%T failed: %s", transform, err) } } { - transform := &OutputTransformer{Module: mod} + transform := &OutputTransformer{Config: mod} if err := transform.Transform(&g); err != nil { t.Fatalf("%T failed: %s", transform, err) } @@ -90,7 +98,16 @@ func TestTargetsTransformer_downstream(t *testing.T) { } { - transform := &TargetsTransformer{Targets: []string{"module.child.module.grandchild.aws_instance.foo"}} + transform := &TargetsTransformer{ + Targets: []addrs.Targetable{ + addrs.RootModuleInstance. + Child("child", addrs.NoKey). + Child("grandchild", addrs.NoKey). + Resource( + addrs.ManagedResourceMode, "aws_instance", "foo", + ), + }, + } if err := transform.Transform(&g); err != nil { t.Fatalf("%T failed: %s", transform, err) } @@ -116,16 +133,16 @@ output.grandchild_id func TestTargetsTransformer_destroy(t *testing.T) { mod := testModule(t, "transform-targets-destroy") - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { - tf := &ConfigTransformer{Module: mod} + tf := &ConfigTransformer{Config: mod} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } } { - transform := &AttachResourceConfigTransformer{Module: mod} + transform := &AttachResourceConfigTransformer{Config: mod} if err := transform.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -140,7 +157,11 @@ func TestTargetsTransformer_destroy(t *testing.T) { { transform := &TargetsTransformer{ - Targets: []string{"aws_instance.me"}, + Targets: []addrs.Targetable{ + addrs.RootModuleInstance.Resource( + addrs.ManagedResourceMode, "aws_instance", "me", + ), + }, Destroy: true, } if err := transform.Transform(&g); err != nil { diff --git a/terraform/transform_transitive_reduction_test.go b/terraform/transform_transitive_reduction_test.go index 63a89b11a..0026400a6 100644 --- a/terraform/transform_transitive_reduction_test.go +++ b/terraform/transform_transitive_reduction_test.go @@ -3,21 +3,23 @@ package terraform import ( "strings" "testing" + + "github.com/hashicorp/terraform/addrs" ) func TestTransitiveReductionTransformer(t *testing.T) { mod := testModule(t, "transform-trans-reduce-basic") - g := Graph{Path: RootModulePath} + g := Graph{Path: addrs.RootModuleInstance} { - tf := &ConfigTransformer{Module: mod} + tf := &ConfigTransformer{Config: mod} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) } } { - transform := &AttachResourceConfigTransformer{Module: mod} + transform := &AttachResourceConfigTransformer{Config: mod} if err := transform.Transform(&g); err != nil { t.Fatalf("err: %s", err) } diff --git a/terraform/variables.go b/terraform/variables.go index 2acc503ef..49ed7851b 100644 --- a/terraform/variables.go +++ b/terraform/variables.go @@ -62,6 +62,24 @@ const ( // InputValues is a map of InputValue instances. type InputValues map[string]*InputValue +// InputValuesFromCaller turns the given map of naked values into an +// InputValues that attributes each value to "a caller", using the source +// type ValueFromCaller. This is primarily useful for testing purposes. +// +// This should not be used as a general way to convert map[string]cty.Value +// into InputValues, since in most real cases we want to set a suitable +// other SourceType and possibly SourceRange value. +func InputValuesFromCaller(vals map[string]cty.Value) InputValues { + ret := make(InputValues, len(vals)) + for k, v := range vals { + ret[k] = &InputValue{ + Value: v, + SourceType: ValueFromCaller, + } + } + return ret +} + // Override merges the given value maps with the receiver, overriding any // conflicting keys so that the latest definition wins. func (vv InputValues) Override(others ...InputValues) InputValues { diff --git a/terraform/variables_test.go b/terraform/variables_test.go index c29104e92..586416534 100644 --- a/terraform/variables_test.go +++ b/terraform/variables_test.go @@ -3,169 +3,91 @@ package terraform import ( "reflect" "testing" + + "github.com/zclconf/go-cty/cty" ) func TestVariables(t *testing.T) { cases := map[string]struct { Module string - Env map[string]string - Override map[string]interface{} - Error bool - Expected map[string]interface{} + Override map[string]cty.Value + Expected map[string]cty.Value }{ "config only": { "vars-basic", nil, - nil, - false, - map[string]interface{}{ - "a": "foo", - "b": []interface{}{}, - "c": map[string]interface{}{}, - }, - }, - - "env vars": { - "vars-basic", - map[string]string{ - "TF_VAR_a": "bar", - "TF_VAR_b": `["foo", "bar"]`, - "TF_VAR_c": `{"foo" = "bar"}`, - }, - nil, - false, - map[string]interface{}{ - "a": "bar", - "b": []interface{}{"foo", "bar"}, - "c": map[string]interface{}{ - "foo": "bar", - }, + map[string]cty.Value{ + "a": cty.StringVal("foo"), + "b": cty.ListValEmpty(cty.String), + "c": cty.MapValEmpty(cty.String), }, }, "override": { "vars-basic", - nil, - map[string]interface{}{ - "a": "bar", - "b": []interface{}{"foo", "bar"}, - "c": map[string]interface{}{ - "foo": "bar", - }, + map[string]cty.Value{ + "a": cty.StringVal("bar"), + "b": cty.ListVal([]cty.Value{ + cty.StringVal("foo"), + cty.StringVal("bar"), + }), + "c": cty.MapVal(map[string]cty.Value{ + "foo": cty.StringVal("bar"), + }), }, - false, - map[string]interface{}{ - "a": "bar", - "b": []interface{}{"foo", "bar"}, - "c": map[string]interface{}{ - "foo": "bar", - }, - }, - }, - - "override partial map": { - "vars-basic", - map[string]string{ - "TF_VAR_c": `{"foo" = "a", "bar" = "baz"}`, - }, - map[string]interface{}{ - "c": map[string]interface{}{ - "foo": "bar", - }, - }, - false, - map[string]interface{}{ - "a": "foo", - "b": []interface{}{}, - "c": map[string]interface{}{ - "foo": "bar", - "bar": "baz", - }, + map[string]cty.Value{ + "a": cty.StringVal("bar"), + "b": cty.ListVal([]cty.Value{ + cty.StringVal("foo"), + cty.StringVal("bar"), + }), + "c": cty.MapVal(map[string]cty.Value{ + "foo": cty.StringVal("bar"), + }), }, }, "bools: config only": { "vars-basic-bool", nil, - nil, - false, - map[string]interface{}{ - "a": "1", - "b": "0", + map[string]cty.Value{ + "a": cty.StringVal("1"), + "b": cty.StringVal("0"), }, }, "bools: override with string": { "vars-basic-bool", - nil, - map[string]interface{}{ - "a": "foo", - "b": "bar", + map[string]cty.Value{ + "a": cty.StringVal("foo"), + "b": cty.StringVal("bar"), }, - false, - map[string]interface{}{ - "a": "foo", - "b": "bar", - }, - }, - - "bools: override with env": { - "vars-basic-bool", - map[string]string{ - "TF_VAR_a": "false", - "TF_VAR_b": "true", - }, - nil, - false, - map[string]interface{}{ - "a": "false", - "b": "true", + map[string]cty.Value{ + "a": cty.StringVal("foo"), + "b": cty.StringVal("bar"), }, }, "bools: override with bool": { "vars-basic-bool", - nil, - map[string]interface{}{ - "a": false, - "b": true, + map[string]cty.Value{ + "a": cty.False, + "b": cty.True, }, - false, - map[string]interface{}{ - "a": "0", - "b": "1", + map[string]cty.Value{ + "a": cty.StringVal("0"), + "b": cty.StringVal("1"), }, }, - - "override map with string": { - "vars-basic", - map[string]string{ - "TF_VAR_c": `{"foo" = "a", "bar" = "baz"}`, - }, - map[string]interface{}{ - "c": "bar", - }, - true, - nil, - }, } for name, tc := range cases { // Wrapped in a func so we can get defers to work t.Run(name, func(t *testing.T) { - // Set the env vars - for k, v := range tc.Env { - defer tempEnv(t, k, v)() - } - m := testModule(t, tc.Module) - actual, err := Variables(m, tc.Override) - if (err != nil) != tc.Error { - t.Fatalf("%s: err: %s", name, err) - } - if err != nil { - return - } + fromConfig := DefaultVariableValues(m.Module.Variables) + overrides := InputValuesFromCaller(tc.Override) + actual := fromConfig.Override(overrides) if !reflect.DeepEqual(actual, tc.Expected) { t.Fatalf("%s\n\nexpected: %#v\n\ngot: %#v", name, tc.Expected, actual)