From ea727d99187dbe0f50136b1718f44a633b763df6 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Wed, 16 May 2018 08:47:22 -0700 Subject: [PATCH] core: Add mock schemas to the refresh context tests There are still some other issues with some of these tests right now, but all the ones that need to have schema should now have it. It seems that there is a bug with the evaluation of child module input variables where they can't find their schema even when a mock is provided. Will attack this in a subsequent commit. --- terraform/context_refresh_test.go | 290 +++++++++++++++++- .../refresh-data-module-var/main.tf | 6 +- .../child/main.tf | 4 +- 3 files changed, 292 insertions(+), 8 deletions(-) diff --git a/terraform/context_refresh_test.go b/terraform/context_refresh_test.go index 24eb7730d..5bbe6fc67 100644 --- a/terraform/context_refresh_test.go +++ b/terraform/context_refresh_test.go @@ -8,7 +8,10 @@ import ( "sync" "testing" + "github.com/zclconf/go-cty/cty" + "github.com/hashicorp/terraform/addrs" + "github.com/hashicorp/terraform/config/configschema" ) func TestContext2Refresh(t *testing.T) { @@ -82,6 +85,34 @@ func TestContext2Refresh_dataComputedModuleVar(t *testing.T) { ID: "foo", } + p.GetSchemaReturn = &ProviderSchema{ + Provider: &configschema.Block{}, + ResourceTypes: map[string]*configschema.Block{ + "aws_instance": { + Attributes: map[string]*configschema.Attribute{ + "foo": { + Type: cty.String, + Optional: true, + }, + "id": { + Type: cty.String, + Computed: true, + }, + }, + }, + }, + DataSources: map[string]*configschema.Block{ + "aws_data_source": { + Attributes: map[string]*configschema.Attribute{ + "id": { + Type: cty.String, + Optional: true, + }, + }, + }, + }, + } + s, err := ctx.Refresh() if err != nil { t.Fatalf("err: %s", err) @@ -95,6 +126,40 @@ module.child: func TestContext2Refresh_targeted(t *testing.T) { p := testProvider("aws") + p.GetSchemaReturn = &ProviderSchema{ + Provider: &configschema.Block{}, + ResourceTypes: map[string]*configschema.Block{ + "aws_elb": { + Attributes: map[string]*configschema.Attribute{ + "instances": { + Type: cty.Set(cty.String), + Optional: true, + }, + }, + }, + "aws_instance": { + Attributes: map[string]*configschema.Attribute{ + "id": { + Type: cty.String, + Computed: true, + }, + "vpc_id": { + Type: cty.String, + Optional: true, + }, + }, + }, + "aws_vpc": { + Attributes: map[string]*configschema.Attribute{ + "id": { + Type: cty.String, + Computed: true, + }, + }, + }, + }, + } + m := testModule(t, "refresh-targeted") ctx := testContext2(t, &ContextOpts{ Config: m, @@ -142,6 +207,40 @@ func TestContext2Refresh_targeted(t *testing.T) { func TestContext2Refresh_targetedCount(t *testing.T) { p := testProvider("aws") + p.GetSchemaReturn = &ProviderSchema{ + Provider: &configschema.Block{}, + ResourceTypes: map[string]*configschema.Block{ + "aws_elb": { + Attributes: map[string]*configschema.Attribute{ + "instances": { + Type: cty.Set(cty.String), + Optional: true, + }, + }, + }, + "aws_instance": { + Attributes: map[string]*configschema.Attribute{ + "id": { + Type: cty.String, + Computed: true, + }, + "vpc_id": { + Type: cty.String, + Optional: true, + }, + }, + }, + "aws_vpc": { + Attributes: map[string]*configschema.Attribute{ + "id": { + Type: cty.String, + Computed: true, + }, + }, + }, + }, + } + m := testModule(t, "refresh-targeted-count") ctx := testContext2(t, &ContextOpts{ Config: m, @@ -199,6 +298,40 @@ func TestContext2Refresh_targetedCount(t *testing.T) { func TestContext2Refresh_targetedCountIndex(t *testing.T) { p := testProvider("aws") + p.GetSchemaReturn = &ProviderSchema{ + Provider: &configschema.Block{}, + ResourceTypes: map[string]*configschema.Block{ + "aws_elb": { + Attributes: map[string]*configschema.Attribute{ + "instances": { + Type: cty.Set(cty.String), + Optional: true, + }, + }, + }, + "aws_instance": { + Attributes: map[string]*configschema.Attribute{ + "id": { + Type: cty.String, + Computed: true, + }, + "vpc_id": { + Type: cty.String, + Optional: true, + }, + }, + }, + "aws_vpc": { + Attributes: map[string]*configschema.Attribute{ + "id": { + Type: cty.String, + Computed: true, + }, + }, + }, + }, + } + m := testModule(t, "refresh-targeted-count") ctx := testContext2(t, &ContextOpts{ Config: m, @@ -248,6 +381,24 @@ func TestContext2Refresh_targetedCountIndex(t *testing.T) { func TestContext2Refresh_moduleComputedVar(t *testing.T) { p := testProvider("aws") + p.GetSchemaReturn = &ProviderSchema{ + Provider: &configschema.Block{}, + ResourceTypes: map[string]*configschema.Block{ + "aws_instance": { + Attributes: map[string]*configschema.Attribute{ + "id": { + Type: cty.String, + Computed: true, + }, + "value": { + Type: cty.String, + Optional: true, + }, + }, + }, + }, + } + m := testModule(t, "refresh-module-computed-var") ctx := testContext2(t, &ContextOpts{ Config: m, @@ -439,6 +590,24 @@ func TestContext2Refresh_moduleInputComputedOutput(t *testing.T) { m := testModule(t, "refresh-module-input-computed-output") p := testProvider("aws") p.DiffFn = testDiffFn + p.GetSchemaReturn = &ProviderSchema{ + Provider: &configschema.Block{}, + ResourceTypes: map[string]*configschema.Block{ + "aws_instance": { + Attributes: map[string]*configschema.Attribute{ + "foo": { + Type: cty.String, + Optional: true, + }, + "compute": { + Type: cty.String, + Optional: true, + }, + }, + }, + }, + } + ctx := testContext2(t, &ContextOpts{ Config: m, ProviderResolver: ResourceProviderResolverFixed( @@ -496,6 +665,20 @@ func TestContext2Refresh_noState(t *testing.T) { func TestContext2Refresh_output(t *testing.T) { p := testProvider("aws") + p.GetSchemaReturn = &ProviderSchema{ + Provider: &configschema.Block{}, + ResourceTypes: map[string]*configschema.Block{ + "aws_instance": { + Attributes: map[string]*configschema.Attribute{ + "foo": { + Type: cty.String, + Computed: true, + }, + }, + }, + }, + } + m := testModule(t, "refresh-output") ctx := testContext2(t, &ContextOpts{ Config: m, @@ -579,6 +762,20 @@ func TestContext2Refresh_outputPartial(t *testing.T) { p.RefreshFn = nil p.RefreshReturn = nil + p.GetSchemaReturn = &ProviderSchema{ + Provider: &configschema.Block{}, + ResourceTypes: map[string]*configschema.Block{ + "aws_instance": { + Attributes: map[string]*configschema.Attribute{ + "foo": { + Type: cty.String, + Computed: true, + }, + }, + }, + }, + } + s, err := ctx.Refresh() if err != nil { t.Fatalf("err: %s", err) @@ -702,6 +899,20 @@ func TestContext2Refresh_dataState(t *testing.T) { State: state, }) + p.GetSchemaReturn = &ProviderSchema{ + Provider: &configschema.Block{}, + DataSources: map[string]*configschema.Block{ + "null_data_source": { + Attributes: map[string]*configschema.Attribute{ + "inputs": { + Type: cty.Map(cty.String), + Optional: true, + }, + }, + }, + }, + } + p.ReadDataDiffFn = nil p.ReadDataDiffReturn = &InstanceDiff{ Attributes: map[string]*ResourceAttrDiff{ @@ -752,6 +963,24 @@ func TestContext2Refresh_dataState(t *testing.T) { func TestContext2Refresh_dataStateRefData(t *testing.T) { p := testProvider("null") + p.GetSchemaReturn = &ProviderSchema{ + Provider: &configschema.Block{}, + DataSources: map[string]*configschema.Block{ + "null_data_source": { + Attributes: map[string]*configschema.Attribute{ + "foo": { + Type: cty.String, + Optional: true, + }, + "bar": { + Type: cty.String, + Optional: true, + }, + }, + }, + }, + } + m := testModule(t, "refresh-data-ref-data") state := &State{ Modules: []*ModuleState{ @@ -883,6 +1112,24 @@ func TestContext2Refresh_unknownProvider(t *testing.T) { func TestContext2Refresh_vars(t *testing.T) { p := testProvider("aws") + p.GetSchemaReturn = &ProviderSchema{ + Provider: &configschema.Block{}, + ResourceTypes: map[string]*configschema.Block{ + "aws_instance": { + Attributes: map[string]*configschema.Attribute{ + "ami": { + Type: cty.String, + Optional: true, + }, + "id": { + Type: cty.String, + Computed: true, + }, + }, + }, + }, + } + m := testModule(t, "refresh-vars") ctx := testContext2(t, &ContextOpts{ Config: m, @@ -952,6 +1199,12 @@ func TestContext2Refresh_orphanModule(t *testing.T) { order = append(order, is.ID) return is, nil } + p.GetSchemaReturn = &ProviderSchema{ + Provider: &configschema.Block{}, + ResourceTypes: map[string]*configschema.Block{ + "aws_instance": {}, + }, + } state := &State{ Modules: []*ModuleState{ @@ -1046,6 +1299,24 @@ func TestContext2Refresh_orphanModule(t *testing.T) { func TestContext2Validate(t *testing.T) { p := testProvider("aws") + p.GetSchemaReturn = &ProviderSchema{ + Provider: &configschema.Block{}, + ResourceTypes: map[string]*configschema.Block{ + "aws_instance": { + Attributes: map[string]*configschema.Attribute{ + "foo": { + Type: cty.String, + Optional: true, + }, + "num": { + Type: cty.String, + Optional: true, + }, + }, + }, + }, + } + m := testModule(t, "validate-good") c := testContext2(t, &ContextOpts{ Config: m, @@ -1072,6 +1343,12 @@ func TestContext2Refresh_noDiffHookOnScaleOut(t *testing.T) { p := testProvider("aws") m := testModule(t, "refresh-resource-scale-inout") p.RefreshFn = nil + p.GetSchemaReturn = &ProviderSchema{ + Provider: &configschema.Block{}, + ResourceTypes: map[string]*configschema.Block{ + "aws_instance": {}, + }, + } state := &State{ Modules: []*ModuleState{ @@ -1125,9 +1402,14 @@ func TestContext2Refresh_noDiffHookOnScaleOut(t *testing.T) { func TestContext2Refresh_updateProviderInState(t *testing.T) { m := testModule(t, "update-resource-provider") p := testProvider("aws") - p.DiffFn = testDiffFn p.ApplyFn = testApplyFn + p.GetSchemaReturn = &ProviderSchema{ + Provider: &configschema.Block{}, + ResourceTypes: map[string]*configschema.Block{ + "aws_instance": {}, + }, + } s := &State{ Modules: []*ModuleState{ @@ -1161,9 +1443,9 @@ aws_instance.bar: ID = foo provider = provider.aws.foo`) - state, err := ctx.Refresh() - if err != nil { - t.Fatal(err) + state, diags := ctx.Refresh() + if diags.HasErrors() { + t.Fatal(diags.Err()) } actual := state.String() diff --git a/terraform/test-fixtures/refresh-data-module-var/main.tf b/terraform/test-fixtures/refresh-data-module-var/main.tf index 06f18b1b5..a371831bd 100644 --- a/terraform/test-fixtures/refresh-data-module-var/main.tf +++ b/terraform/test-fixtures/refresh-data-module-var/main.tf @@ -1,8 +1,8 @@ resource "aws_instance" "A" { - foo = "bar" + foo = "bar" } module "child" { - source = "child" - key = "${aws_instance.A.id}" + source = "./child" + key = "${aws_instance.A.id}" } diff --git a/terraform/test-fixtures/refresh-module-input-computed-output/child/main.tf b/terraform/test-fixtures/refresh-module-input-computed-output/child/main.tf index fff03a60f..1e3a16519 100644 --- a/terraform/test-fixtures/refresh-module-input-computed-output/child/main.tf +++ b/terraform/test-fixtures/refresh-module-input-computed-output/child/main.tf @@ -1,4 +1,6 @@ -variable "input" {} +variable "input" { + type = list(string) +} resource "aws_instance" "foo" { foo = "${var.input}"