From 5b61cc919bb788022a402c3f2a5b632e06a7c4a4 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Sun, 14 Oct 2018 16:54:23 -0700 Subject: [PATCH] command: Fix "terraform import" tests --- command/import.go | 9 +++ command/import_test.go | 155 ++++++++++++++++++++++++++++++++++------- 2 files changed, 140 insertions(+), 24 deletions(-) diff --git a/command/import.go b/command/import.go index 6d72adae42..50b260b2d1 100644 --- a/command/import.go +++ b/command/import.go @@ -217,6 +217,15 @@ func (c *ImportCommand) Run(args []string) int { c.showDiagnostics(diags) return 1 } + { + var moreDiags tfdiags.Diagnostics + opReq.Variables, moreDiags = c.collectVariableValues() + diags = diags.Append(moreDiags) + if moreDiags.HasErrors() { + c.showDiagnostics(diags) + return 1 + } + } // Get the context ctx, state, ctxDiags := local.Context(opReq) diff --git a/command/import_test.go b/command/import_test.go index dfef0bc002..4695548849 100644 --- a/command/import_test.go +++ b/command/import_test.go @@ -11,6 +11,7 @@ import ( "github.com/mitchellh/cli" "github.com/zclconf/go-cty/cty" + "github.com/hashicorp/terraform/configs/configschema" "github.com/hashicorp/terraform/helper/copy" "github.com/hashicorp/terraform/plugin" "github.com/hashicorp/terraform/plugin/discovery" @@ -44,6 +45,15 @@ func TestImport(t *testing.T) { }, }, } + p.GetSchemaReturn = &terraform.ProviderSchema{ + ResourceTypes: map[string]*configschema.Block{ + "test_instance": { + Attributes: map[string]*configschema.Attribute{ + "id": {Type: cty.String, Optional: true, Computed: true}, + }, + }, + }, + } args := []string{ "-state", statePath, @@ -86,6 +96,20 @@ func TestImport_providerConfig(t *testing.T) { }, }, } + p.GetSchemaReturn = &terraform.ProviderSchema{ + Provider: &configschema.Block{ + Attributes: map[string]*configschema.Attribute{ + "foo": {Type: cty.String, Optional: true}, + }, + }, + ResourceTypes: map[string]*configschema.Block{ + "test_instance": { + Attributes: map[string]*configschema.Attribute{ + "id": {Type: cty.String, Optional: true, Computed: true}, + }, + }, + }, + } configured := false p.ConfigureNewFn = func(req providers.ConfigureRequest) providers.ConfigureResponse { @@ -178,16 +202,31 @@ func TestImport_remoteState(t *testing.T) { }, }, } + p.GetSchemaReturn = &terraform.ProviderSchema{ + Provider: &configschema.Block{ + Attributes: map[string]*configschema.Attribute{ + "foo": {Type: cty.String, Optional: true}, + }, + }, + ResourceTypes: map[string]*configschema.Block{ + "test_instance": { + Attributes: map[string]*configschema.Attribute{ + "id": {Type: cty.String, Optional: true, Computed: true}, + }, + }, + }, + } configured := false - p.ConfigureFn = func(c *terraform.ResourceConfig) error { + p.ConfigureNewFn = func(req providers.ConfigureRequest) providers.ConfigureResponse { + var diags tfdiags.Diagnostics configured = true - - if v, ok := c.Get("foo"); !ok || v.(string) != "bar" { - return fmt.Errorf("bad value: %#v", v) + if got, want := req.Config.GetAttr("foo"), cty.StringVal("bar"); !want.RawEquals(got) { + diags = diags.Append(fmt.Errorf("wrong \"foo\" value %#v; want %#v", got, want)) + } + return providers.ConfigureResponse{ + Diagnostics: diags, } - - return nil } args := []string{ @@ -242,16 +281,31 @@ func TestImport_providerConfigWithVar(t *testing.T) { }, }, } + p.GetSchemaReturn = &terraform.ProviderSchema{ + Provider: &configschema.Block{ + Attributes: map[string]*configschema.Attribute{ + "foo": {Type: cty.String, Optional: true}, + }, + }, + ResourceTypes: map[string]*configschema.Block{ + "test_instance": { + Attributes: map[string]*configschema.Attribute{ + "id": {Type: cty.String, Optional: true, Computed: true}, + }, + }, + }, + } configured := false - p.ConfigureFn = func(c *terraform.ResourceConfig) error { + p.ConfigureNewFn = func(req providers.ConfigureRequest) providers.ConfigureResponse { + var diags tfdiags.Diagnostics configured = true - - if v, ok := c.Get("foo"); !ok || v.(string) != "bar" { - return fmt.Errorf("bad value: %#v", v) + if got, want := req.Config.GetAttr("foo"), cty.StringVal("bar"); !want.RawEquals(got) { + diags = diags.Append(fmt.Errorf("wrong \"foo\" value %#v; want %#v", got, want)) + } + return providers.ConfigureResponse{ + Diagnostics: diags, } - - return nil } args := []string{ @@ -301,16 +355,31 @@ func TestImport_providerConfigWithVarDefault(t *testing.T) { }, }, } + p.GetSchemaReturn = &terraform.ProviderSchema{ + Provider: &configschema.Block{ + Attributes: map[string]*configschema.Attribute{ + "foo": {Type: cty.String, Optional: true}, + }, + }, + ResourceTypes: map[string]*configschema.Block{ + "test_instance": { + Attributes: map[string]*configschema.Attribute{ + "id": {Type: cty.String, Optional: true, Computed: true}, + }, + }, + }, + } configured := false - p.ConfigureFn = func(c *terraform.ResourceConfig) error { + p.ConfigureNewFn = func(req providers.ConfigureRequest) providers.ConfigureResponse { + var diags tfdiags.Diagnostics configured = true - - if v, ok := c.Get("foo"); !ok || v.(string) != "bar" { - return fmt.Errorf("bad value: %#v", v) + if got, want := req.Config.GetAttr("foo"), cty.StringVal("bar"); !want.RawEquals(got) { + diags = diags.Append(fmt.Errorf("wrong \"foo\" value %#v; want %#v", got, want)) + } + return providers.ConfigureResponse{ + Diagnostics: diags, } - - return nil } args := []string{ @@ -359,16 +428,31 @@ func TestImport_providerConfigWithVarFile(t *testing.T) { }, }, } + p.GetSchemaReturn = &terraform.ProviderSchema{ + Provider: &configschema.Block{ + Attributes: map[string]*configschema.Attribute{ + "foo": {Type: cty.String, Optional: true}, + }, + }, + ResourceTypes: map[string]*configschema.Block{ + "test_instance": { + Attributes: map[string]*configschema.Attribute{ + "id": {Type: cty.String, Optional: true, Computed: true}, + }, + }, + }, + } configured := false - p.ConfigureFn = func(c *terraform.ResourceConfig) error { + p.ConfigureNewFn = func(req providers.ConfigureRequest) providers.ConfigureResponse { + var diags tfdiags.Diagnostics configured = true - - if v, ok := c.Get("foo"); !ok || v.(string) != "bar" { - return fmt.Errorf("bad value: %#v", v) + if got, want := req.Config.GetAttr("foo"), cty.StringVal("bar"); !want.RawEquals(got) { + diags = diags.Append(fmt.Errorf("wrong \"foo\" value %#v; want %#v", got, want)) + } + return providers.ConfigureResponse{ + Diagnostics: diags, } - - return nil } args := []string{ @@ -418,6 +502,20 @@ func TestImport_customProvider(t *testing.T) { }, }, } + p.GetSchemaReturn = &terraform.ProviderSchema{ + Provider: &configschema.Block{ + Attributes: map[string]*configschema.Attribute{ + "foo": {Type: cty.String, Optional: true}, + }, + }, + ResourceTypes: map[string]*configschema.Block{ + "test_instance": { + Attributes: map[string]*configschema.Attribute{ + "id": {Type: cty.String, Optional: true, Computed: true}, + }, + }, + }, + } args := []string{ "-provider", "test.alias", @@ -461,6 +559,15 @@ func TestImport_allowMissingResourceConfig(t *testing.T) { }, }, } + p.GetSchemaReturn = &terraform.ProviderSchema{ + ResourceTypes: map[string]*configschema.Block{ + "test_instance": { + Attributes: map[string]*configschema.Attribute{ + "id": {Type: cty.String, Optional: true, Computed: true}, + }, + }, + }, + } args := []string{ "-state", statePath,