From 441f7f08495e0054d0e9ba7d1582c2e31c938a8b Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Tue, 11 Sep 2018 17:34:17 -0700 Subject: [PATCH] core: Fix TestContext2Apply_multiVarComprehensive This test was relying on the fact that we used to expose the full resource instance address to providers via the InstanceInfo value, but we no longer do that (since in practice no "real" providers depended on it, nor should depend on it) so we need to instead include in the config itself a key to use for tracking each resource instance for later test assertions. --- terraform/context_apply_test.go | 91 +++++++++---------- .../child/child.tf | 4 + .../apply-multi-var-comprehensive/root.tf | 8 ++ 3 files changed, 54 insertions(+), 49 deletions(-) diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index 87dd424f3..2e46caa5e 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -3538,7 +3538,8 @@ func TestContext2Apply_multiVarComprehensive(t *testing.T) { p.DiffFn = func(info *InstanceInfo, s *InstanceState, c *ResourceConfig) (*InstanceDiff, error) { configsLock.Lock() defer configsLock.Unlock() - configs[info.HumanId()] = c + key := c.Config["key"].(string) + configs[key] = c // Return a minimal diff to make sure this resource gets included in // the apply graph and thus the final state, but otherwise we're just @@ -3549,7 +3550,7 @@ func TestContext2Apply_multiVarComprehensive(t *testing.T) { NewComputed: true, }, "name": &ResourceAttrDiff{ - New: info.HumanId(), + New: key, }, }, }, nil @@ -3559,6 +3560,8 @@ func TestContext2Apply_multiVarComprehensive(t *testing.T) { ResourceTypes: map[string]*configschema.Block{ "test_thing": { Attributes: map[string]*configschema.Attribute{ + "key": {Type: cty.String, Required: true}, + "source_id": {Type: cty.String, Optional: true}, "source_name": {Type: cty.String, Optional: true}, "first_source_id": {Type: cty.String, Optional: true}, @@ -3594,67 +3597,59 @@ func TestContext2Apply_multiVarComprehensive(t *testing.T) { }) if _, diags := ctx.Plan(); diags.HasErrors() { - t.Fatalf("error during plan: %s", diags.Err()) + logDiagnostics(t, diags) + t.Fatalf("errors during plan") } - checkConfig := func(name string, want map[string]interface{}) { + checkConfig := func(key string, want map[string]interface{}) { configsLock.Lock() defer configsLock.Unlock() - if _, ok := configs[name]; !ok { - t.Errorf("no config recorded for %s; expected a configuration", name) + if _, ok := configs[key]; !ok { + t.Errorf("no config recorded for %s; expected a configuration", key) return } - got := configs[name].Config - t.Run("config for "+name, func(t *testing.T) { + got := configs[key].Config + t.Run("config for "+key, func(t *testing.T) { + want["key"] = key // to avoid doing this for every example for _, problem := range deep.Equal(got, want) { t.Errorf(problem) } }) } - checkConfig("test_thing.multi_count_var.0", map[string]interface{}{ - "id": unknownValue(), - "name": unknownValue(), + checkConfig("multi_count_var.0", map[string]interface{}{ "source_id": unknownValue(), - "source_name": "test_thing.source.0", + "source_name": "source.0", }) - checkConfig("test_thing.multi_count_var.2", map[string]interface{}{ - "id": unknownValue(), - "name": unknownValue(), + checkConfig("multi_count_var.2", map[string]interface{}{ "source_id": unknownValue(), - "source_name": "test_thing.source.2", + "source_name": "source.2", }) - checkConfig("test_thing.multi_count_derived.0", map[string]interface{}{ - "id": unknownValue(), - "name": unknownValue(), + checkConfig("multi_count_derived.0", map[string]interface{}{ "source_id": unknownValue(), - "source_name": "test_thing.source.0", + "source_name": "source.0", }) - checkConfig("test_thing.multi_count_derived.2", map[string]interface{}{ - "id": unknownValue(), - "name": unknownValue(), + checkConfig("multi_count_derived.2", map[string]interface{}{ "source_id": unknownValue(), - "source_name": "test_thing.source.2", + "source_name": "source.2", }) - checkConfig("test_thing.whole_splat", map[string]interface{}{ - "id": unknownValue(), - "name": unknownValue(), + checkConfig("whole_splat", map[string]interface{}{ "source_ids": []interface{}{ unknownValue(), unknownValue(), unknownValue(), }, "source_names": []interface{}{ - "test_thing.source.0", - "test_thing.source.1", - "test_thing.source.2", + "source.0", + "source.1", + "source.2", }, "source_ids_from_func": unknownValue(), "source_names_from_func": []interface{}{ - "test_thing.source.0", - "test_thing.source.1", - "test_thing.source.2", + "source.0", + "source.1", + "source.2", }, "source_ids_wrapped": []interface{}{ @@ -3666,27 +3661,25 @@ func TestContext2Apply_multiVarComprehensive(t *testing.T) { }, "source_names_wrapped": []interface{}{ []interface{}{ - "test_thing.source.0", - "test_thing.source.1", - "test_thing.source.2", + "source.0", + "source.1", + "source.2", }, }, "first_source_id": unknownValue(), - "first_source_name": "test_thing.source.0", + "first_source_name": "source.0", }) - checkConfig("module.child.test_thing.whole_splat", map[string]interface{}{ - "id": unknownValue(), - "name": unknownValue(), + checkConfig("child.whole_splat", map[string]interface{}{ "source_ids": []interface{}{ unknownValue(), unknownValue(), unknownValue(), }, "source_names": []interface{}{ - "test_thing.source.0", - "test_thing.source.1", - "test_thing.source.2", + "source.0", + "source.1", + "source.2", }, "source_ids_wrapped": []interface{}{ @@ -3698,9 +3691,9 @@ func TestContext2Apply_multiVarComprehensive(t *testing.T) { }, "source_names_wrapped": []interface{}{ []interface{}{ - "test_thing.source.0", - "test_thing.source.1", - "test_thing.source.2", + "source.0", + "source.1", + "source.2", }, }, }) @@ -3714,9 +3707,9 @@ func TestContext2Apply_multiVarComprehensive(t *testing.T) { want := map[string]interface{}{ "source_ids": []interface{}{"foo", "foo", "foo"}, "source_names": []interface{}{ - "test_thing.source.0", - "test_thing.source.1", - "test_thing.source.2", + "source.0", + "source.1", + "source.2", }, } got := map[string]interface{}{} diff --git a/terraform/test-fixtures/apply-multi-var-comprehensive/child/child.tf b/terraform/test-fixtures/apply-multi-var-comprehensive/child/child.tf index 5ab54019d..5d925d93b 100644 --- a/terraform/test-fixtures/apply-multi-var-comprehensive/child/child.tf +++ b/terraform/test-fixtures/apply-multi-var-comprehensive/child/child.tf @@ -12,11 +12,15 @@ variable "source_names" { resource "test_thing" "multi_count_var" { count = var.num + key = "child.multi_count_var.${count.index}" + # Can pluck a single item out of a multi-var source_id = var.source_ids[count.index] } resource "test_thing" "whole_splat" { + key = "child.whole_splat" + # Can "splat" the ids directly into an attribute of type list. source_ids = var.source_ids source_names = var.source_names diff --git a/terraform/test-fixtures/apply-multi-var-comprehensive/root.tf b/terraform/test-fixtures/apply-multi-var-comprehensive/root.tf index 3358fc63b..64ada6be6 100644 --- a/terraform/test-fixtures/apply-multi-var-comprehensive/root.tf +++ b/terraform/test-fixtures/apply-multi-var-comprehensive/root.tf @@ -4,6 +4,8 @@ variable "num" { resource "test_thing" "source" { count = var.num + key = "source.${count.index}" + # The diffFunc in the test exports "name" here too, which we can use # to test values that are known during plan. } @@ -11,6 +13,8 @@ resource "test_thing" "source" { resource "test_thing" "multi_count_var" { count = var.num + key = "multi_count_var.${count.index}" + # Can pluck a single item out of a multi-var source_id = test_thing.source.*.id[count.index] source_name = test_thing.source.*.name[count.index] @@ -20,11 +24,15 @@ resource "test_thing" "multi_count_derived" { # Can use the source to get the count count = length(test_thing.source) + key = "multi_count_derived.${count.index}" + source_id = test_thing.source.*.id[count.index] source_name = test_thing.source.*.name[count.index] } resource "test_thing" "whole_splat" { + key = "whole_splat" + # Can "splat" the ids directly into an attribute of type list. source_ids = test_thing.source.*.id source_names = test_thing.source.*.name