mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-28 01:41:48 -06:00
df578afd7e
In historical versions of Terraform the responsibility to check this was inside the terraform.NewContext function, along with various other assorted concerns that made that function particularly complicated. More recently, we reduced the responsibility of the "terraform" package only to instantiating particular named plugins, assuming that its caller is responsible for selecting appropriate versions of any providers that _are_ external. However, until this commit we were just assuming that "terraform init" had correctly selected appropriate plugins and recorded them in the lock file, and so nothing was dealing with the problem of ensuring that there haven't been any changes to the lock file or config since the most recent "terraform init" which would cause us to need to re-evaluate those decisions. Part of the game here is to slightly extend the role of the dependency locks object to also carry information about a subset of provider addresses whose lock entries we're intentionally disregarding as part of the various little edge-case features we have for overridding providers: dev_overrides, "unmanaged providers", and the testing overrides in our own unit tests. This is an in-memory-only annotation, never included in the serialized plan files on disk. I had originally intended to create a new package to encapsulate all of this plugin-selection logic, including both the version constraint checking here and also the handling of the provider factory functions, but as an interim step I've just made version constraint consistency checks the responsibility of the backend/local package, which means that we'll always catch problems as part of preparing for local operations, while not imposing these additional checks on commands that _don't_ run local operations, such as "terraform apply" when in remote operations mode.
1041 lines
25 KiB
Go
1041 lines
25 KiB
Go
package command
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/mitchellh/cli"
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
"github.com/hashicorp/terraform/internal/configs/configschema"
|
|
"github.com/hashicorp/terraform/internal/copy"
|
|
"github.com/hashicorp/terraform/internal/providers"
|
|
"github.com/hashicorp/terraform/internal/tfdiags"
|
|
)
|
|
|
|
func TestImport(t *testing.T) {
|
|
defer testChdir(t, testFixturePath("import-provider-implicit"))()
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
view, _ := testView(t)
|
|
c := &ImportCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
View: view,
|
|
},
|
|
}
|
|
|
|
p.ImportResourceStateFn = nil
|
|
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
|
|
ImportedResources: []providers.ImportedResource{
|
|
{
|
|
TypeName: "test_instance",
|
|
State: cty.ObjectVal(map[string]cty.Value{
|
|
"id": cty.StringVal("yay"),
|
|
}),
|
|
},
|
|
},
|
|
}
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
|
ResourceTypes: map[string]providers.Schema{
|
|
"test_instance": {
|
|
Block: &configschema.Block{
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
"-state", statePath,
|
|
"test_instance.foo",
|
|
"bar",
|
|
}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
if !p.ImportResourceStateCalled {
|
|
t.Fatal("ImportResourceState should be called")
|
|
}
|
|
|
|
testStateOutput(t, statePath, testImportStr)
|
|
}
|
|
|
|
func TestImport_providerConfig(t *testing.T) {
|
|
defer testChdir(t, testFixturePath("import-provider"))()
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
view, _ := testView(t)
|
|
c := &ImportCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
View: view,
|
|
},
|
|
}
|
|
|
|
p.ImportResourceStateFn = nil
|
|
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
|
|
ImportedResources: []providers.ImportedResource{
|
|
{
|
|
TypeName: "test_instance",
|
|
State: cty.ObjectVal(map[string]cty.Value{
|
|
"id": cty.StringVal("yay"),
|
|
}),
|
|
},
|
|
},
|
|
}
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
|
Provider: providers.Schema{
|
|
Block: &configschema.Block{
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"foo": {Type: cty.String, Optional: true},
|
|
},
|
|
},
|
|
},
|
|
ResourceTypes: map[string]providers.Schema{
|
|
"test_instance": {
|
|
Block: &configschema.Block{
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
configured := false
|
|
p.ConfigureProviderFn = func(req providers.ConfigureProviderRequest) providers.ConfigureProviderResponse {
|
|
configured = true
|
|
|
|
cfg := req.Config
|
|
if !cfg.Type().HasAttribute("foo") {
|
|
return providers.ConfigureProviderResponse{
|
|
Diagnostics: tfdiags.Diagnostics{}.Append(fmt.Errorf("configuration has no foo argument")),
|
|
}
|
|
}
|
|
if got, want := cfg.GetAttr("foo"), cty.StringVal("bar"); !want.RawEquals(got) {
|
|
return providers.ConfigureProviderResponse{
|
|
Diagnostics: tfdiags.Diagnostics{}.Append(fmt.Errorf("foo argument is %#v, but want %#v", got, want)),
|
|
}
|
|
}
|
|
|
|
return providers.ConfigureProviderResponse{}
|
|
}
|
|
|
|
args := []string{
|
|
"-state", statePath,
|
|
"test_instance.foo",
|
|
"bar",
|
|
}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
// Verify that we were called
|
|
if !configured {
|
|
t.Fatal("Configure should be called")
|
|
}
|
|
|
|
if !p.ImportResourceStateCalled {
|
|
t.Fatal("ImportResourceState should be called")
|
|
}
|
|
|
|
testStateOutput(t, statePath, testImportStr)
|
|
}
|
|
|
|
// "remote" state provided by the "local" backend
|
|
func TestImport_remoteState(t *testing.T) {
|
|
td := tempDir(t)
|
|
testCopyDir(t, testFixturePath("import-provider-remote-state"), td)
|
|
defer os.RemoveAll(td)
|
|
defer testChdir(t, td)()
|
|
|
|
statePath := "imported.tfstate"
|
|
|
|
providerSource, close := newMockProviderSource(t, map[string][]string{
|
|
"test": []string{"1.2.3"},
|
|
})
|
|
defer close()
|
|
|
|
// init our backend
|
|
ui := cli.NewMockUi()
|
|
view, _ := testView(t)
|
|
m := Meta{
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
Ui: ui,
|
|
View: view,
|
|
ProviderSource: providerSource,
|
|
}
|
|
|
|
ic := &InitCommand{
|
|
Meta: m,
|
|
}
|
|
|
|
// (Using log here rather than t.Log so that these messages interleave with other trace logs)
|
|
log.Print("[TRACE] TestImport_remoteState running: terraform init")
|
|
if code := ic.Run([]string{}); code != 0 {
|
|
t.Fatalf("init failed\n%s", ui.ErrorWriter)
|
|
}
|
|
|
|
p := testProvider()
|
|
ui = new(cli.MockUi)
|
|
c := &ImportCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
View: view,
|
|
},
|
|
}
|
|
|
|
p.ImportResourceStateFn = nil
|
|
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
|
|
ImportedResources: []providers.ImportedResource{
|
|
{
|
|
TypeName: "test_instance",
|
|
State: cty.ObjectVal(map[string]cty.Value{
|
|
"id": cty.StringVal("yay"),
|
|
}),
|
|
},
|
|
},
|
|
}
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
|
Provider: providers.Schema{
|
|
Block: &configschema.Block{
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"foo": {Type: cty.String, Optional: true},
|
|
},
|
|
},
|
|
},
|
|
ResourceTypes: map[string]providers.Schema{
|
|
"test_instance": {
|
|
Block: &configschema.Block{
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
configured := false
|
|
p.ConfigureProviderFn = func(req providers.ConfigureProviderRequest) providers.ConfigureProviderResponse {
|
|
var diags tfdiags.Diagnostics
|
|
configured = true
|
|
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.ConfigureProviderResponse{
|
|
Diagnostics: diags,
|
|
}
|
|
}
|
|
|
|
args := []string{
|
|
"test_instance.foo",
|
|
"bar",
|
|
}
|
|
log.Printf("[TRACE] TestImport_remoteState running: terraform import %s %s", args[0], args[1])
|
|
if code := c.Run(args); code != 0 {
|
|
fmt.Println(ui.OutputWriter)
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
// verify that the local state was unlocked after import
|
|
if _, err := os.Stat(filepath.Join(td, fmt.Sprintf(".%s.lock.info", statePath))); !os.IsNotExist(err) {
|
|
t.Fatal("state left locked after import")
|
|
}
|
|
|
|
// Verify that we were called
|
|
if !configured {
|
|
t.Fatal("Configure should be called")
|
|
}
|
|
|
|
if !p.ImportResourceStateCalled {
|
|
t.Fatal("ImportResourceState should be called")
|
|
}
|
|
|
|
testStateOutput(t, statePath, testImportStr)
|
|
}
|
|
|
|
// early failure on import should not leave stale lock
|
|
func TestImport_initializationErrorShouldUnlock(t *testing.T) {
|
|
td := tempDir(t)
|
|
testCopyDir(t, testFixturePath("import-provider-remote-state"), td)
|
|
defer os.RemoveAll(td)
|
|
defer testChdir(t, td)()
|
|
|
|
statePath := "imported.tfstate"
|
|
|
|
providerSource, close := newMockProviderSource(t, map[string][]string{
|
|
"test": []string{"1.2.3"},
|
|
})
|
|
defer close()
|
|
|
|
// init our backend
|
|
ui := cli.NewMockUi()
|
|
view, _ := testView(t)
|
|
m := Meta{
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
Ui: ui,
|
|
View: view,
|
|
ProviderSource: providerSource,
|
|
}
|
|
|
|
ic := &InitCommand{
|
|
Meta: m,
|
|
}
|
|
|
|
// (Using log here rather than t.Log so that these messages interleave with other trace logs)
|
|
log.Print("[TRACE] TestImport_initializationErrorShouldUnlock running: terraform init")
|
|
if code := ic.Run([]string{}); code != 0 {
|
|
t.Fatalf("init failed\n%s", ui.ErrorWriter)
|
|
}
|
|
|
|
// overwrite the config with one including a resource from an invalid provider
|
|
copy.CopyFile(filepath.Join(testFixturePath("import-provider-invalid"), "main.tf"), filepath.Join(td, "main.tf"))
|
|
|
|
p := testProvider()
|
|
ui = new(cli.MockUi)
|
|
c := &ImportCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
View: view,
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
"unknown_instance.baz",
|
|
"bar",
|
|
}
|
|
log.Printf("[TRACE] TestImport_initializationErrorShouldUnlock running: terraform import %s %s", args[0], args[1])
|
|
|
|
// this should fail
|
|
if code := c.Run(args); code != 1 {
|
|
fmt.Println(ui.OutputWriter)
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
// specifically, it should fail due to a missing provider
|
|
msg := strings.ReplaceAll(ui.ErrorWriter.String(), "\n", " ")
|
|
if want := `provider registry.terraform.io/hashicorp/unknown: required by this configuration but no version is selected`; !strings.Contains(msg, want) {
|
|
t.Errorf("incorrect message\nwant substring: %s\ngot:\n%s", want, msg)
|
|
}
|
|
|
|
// verify that the local state was unlocked after initialization error
|
|
if _, err := os.Stat(filepath.Join(td, fmt.Sprintf(".%s.lock.info", statePath))); !os.IsNotExist(err) {
|
|
t.Fatal("state left locked after import")
|
|
}
|
|
}
|
|
|
|
func TestImport_providerConfigWithVar(t *testing.T) {
|
|
defer testChdir(t, testFixturePath("import-provider-var"))()
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
view, _ := testView(t)
|
|
c := &ImportCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
View: view,
|
|
},
|
|
}
|
|
|
|
p.ImportResourceStateFn = nil
|
|
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
|
|
ImportedResources: []providers.ImportedResource{
|
|
{
|
|
TypeName: "test_instance",
|
|
State: cty.ObjectVal(map[string]cty.Value{
|
|
"id": cty.StringVal("yay"),
|
|
}),
|
|
},
|
|
},
|
|
}
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
|
Provider: providers.Schema{
|
|
Block: &configschema.Block{
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"foo": {Type: cty.String, Optional: true},
|
|
},
|
|
},
|
|
},
|
|
ResourceTypes: map[string]providers.Schema{
|
|
"test_instance": {
|
|
Block: &configschema.Block{
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
configured := false
|
|
p.ConfigureProviderFn = func(req providers.ConfigureProviderRequest) providers.ConfigureProviderResponse {
|
|
var diags tfdiags.Diagnostics
|
|
configured = true
|
|
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.ConfigureProviderResponse{
|
|
Diagnostics: diags,
|
|
}
|
|
}
|
|
|
|
args := []string{
|
|
"-state", statePath,
|
|
"-var", "foo=bar",
|
|
"test_instance.foo",
|
|
"bar",
|
|
}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
// Verify that we were called
|
|
if !configured {
|
|
t.Fatal("Configure should be called")
|
|
}
|
|
|
|
if !p.ImportResourceStateCalled {
|
|
t.Fatal("ImportResourceState should be called")
|
|
}
|
|
|
|
testStateOutput(t, statePath, testImportStr)
|
|
}
|
|
|
|
func TestImport_providerConfigWithDataSource(t *testing.T) {
|
|
defer testChdir(t, testFixturePath("import-provider-datasource"))()
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
view, _ := testView(t)
|
|
c := &ImportCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
View: view,
|
|
},
|
|
}
|
|
|
|
p.ImportResourceStateFn = nil
|
|
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
|
|
ImportedResources: []providers.ImportedResource{
|
|
{
|
|
TypeName: "test_instance",
|
|
State: cty.ObjectVal(map[string]cty.Value{
|
|
"id": cty.StringVal("yay"),
|
|
}),
|
|
},
|
|
},
|
|
}
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
|
Provider: providers.Schema{
|
|
Block: &configschema.Block{
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"foo": {Type: cty.String, Optional: true},
|
|
},
|
|
},
|
|
},
|
|
ResourceTypes: map[string]providers.Schema{
|
|
"test_instance": {
|
|
Block: &configschema.Block{
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
DataSources: map[string]providers.Schema{
|
|
"test_data": {
|
|
Block: &configschema.Block{
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"foo": {Type: cty.String, Optional: true},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
"-state", statePath,
|
|
"test_instance.foo",
|
|
"bar",
|
|
}
|
|
if code := c.Run(args); code != 1 {
|
|
t.Fatalf("bad, wanted error: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
}
|
|
|
|
func TestImport_providerConfigWithVarDefault(t *testing.T) {
|
|
defer testChdir(t, testFixturePath("import-provider-var-default"))()
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
view, _ := testView(t)
|
|
c := &ImportCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
View: view,
|
|
},
|
|
}
|
|
|
|
p.ImportResourceStateFn = nil
|
|
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
|
|
ImportedResources: []providers.ImportedResource{
|
|
{
|
|
TypeName: "test_instance",
|
|
State: cty.ObjectVal(map[string]cty.Value{
|
|
"id": cty.StringVal("yay"),
|
|
}),
|
|
},
|
|
},
|
|
}
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
|
Provider: providers.Schema{
|
|
Block: &configschema.Block{
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"foo": {Type: cty.String, Optional: true},
|
|
},
|
|
},
|
|
},
|
|
ResourceTypes: map[string]providers.Schema{
|
|
"test_instance": {
|
|
Block: &configschema.Block{
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
configured := false
|
|
p.ConfigureProviderFn = func(req providers.ConfigureProviderRequest) providers.ConfigureProviderResponse {
|
|
var diags tfdiags.Diagnostics
|
|
configured = true
|
|
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.ConfigureProviderResponse{
|
|
Diagnostics: diags,
|
|
}
|
|
}
|
|
|
|
args := []string{
|
|
"-state", statePath,
|
|
"test_instance.foo",
|
|
"bar",
|
|
}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
// Verify that we were called
|
|
if !configured {
|
|
t.Fatal("Configure should be called")
|
|
}
|
|
|
|
if !p.ImportResourceStateCalled {
|
|
t.Fatal("ImportResourceState should be called")
|
|
}
|
|
|
|
testStateOutput(t, statePath, testImportStr)
|
|
}
|
|
|
|
func TestImport_providerConfigWithVarFile(t *testing.T) {
|
|
defer testChdir(t, testFixturePath("import-provider-var-file"))()
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
view, _ := testView(t)
|
|
c := &ImportCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
View: view,
|
|
},
|
|
}
|
|
|
|
p.ImportResourceStateFn = nil
|
|
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
|
|
ImportedResources: []providers.ImportedResource{
|
|
{
|
|
TypeName: "test_instance",
|
|
State: cty.ObjectVal(map[string]cty.Value{
|
|
"id": cty.StringVal("yay"),
|
|
}),
|
|
},
|
|
},
|
|
}
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
|
Provider: providers.Schema{
|
|
Block: &configschema.Block{
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"foo": {Type: cty.String, Optional: true},
|
|
},
|
|
},
|
|
},
|
|
ResourceTypes: map[string]providers.Schema{
|
|
"test_instance": {
|
|
Block: &configschema.Block{
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
configured := false
|
|
p.ConfigureProviderFn = func(req providers.ConfigureProviderRequest) providers.ConfigureProviderResponse {
|
|
var diags tfdiags.Diagnostics
|
|
configured = true
|
|
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.ConfigureProviderResponse{
|
|
Diagnostics: diags,
|
|
}
|
|
}
|
|
|
|
args := []string{
|
|
"-state", statePath,
|
|
"-var-file", "blah.tfvars",
|
|
"test_instance.foo",
|
|
"bar",
|
|
}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
// Verify that we were called
|
|
if !configured {
|
|
t.Fatal("Configure should be called")
|
|
}
|
|
|
|
if !p.ImportResourceStateCalled {
|
|
t.Fatal("ImportResourceState should be called")
|
|
}
|
|
|
|
testStateOutput(t, statePath, testImportStr)
|
|
}
|
|
|
|
func TestImport_allowMissingResourceConfig(t *testing.T) {
|
|
defer testChdir(t, testFixturePath("import-missing-resource-config"))()
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
view, _ := testView(t)
|
|
c := &ImportCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
View: view,
|
|
},
|
|
}
|
|
|
|
p.ImportResourceStateFn = nil
|
|
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
|
|
ImportedResources: []providers.ImportedResource{
|
|
{
|
|
TypeName: "test_instance",
|
|
State: cty.ObjectVal(map[string]cty.Value{
|
|
"id": cty.StringVal("yay"),
|
|
}),
|
|
},
|
|
},
|
|
}
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
|
ResourceTypes: map[string]providers.Schema{
|
|
"test_instance": {
|
|
Block: &configschema.Block{
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
"-state", statePath,
|
|
"-allow-missing-config",
|
|
"test_instance.foo",
|
|
"bar",
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
if !p.ImportResourceStateCalled {
|
|
t.Fatal("ImportResourceState should be called")
|
|
}
|
|
|
|
testStateOutput(t, statePath, testImportStr)
|
|
}
|
|
|
|
func TestImport_emptyConfig(t *testing.T) {
|
|
defer testChdir(t, testFixturePath("empty"))()
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
view, _ := testView(t)
|
|
c := &ImportCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
View: view,
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
"-state", statePath,
|
|
"test_instance.foo",
|
|
"bar",
|
|
}
|
|
code := c.Run(args)
|
|
if code != 1 {
|
|
t.Fatalf("import succeeded; expected failure")
|
|
}
|
|
|
|
msg := ui.ErrorWriter.String()
|
|
if want := `No Terraform configuration files`; !strings.Contains(msg, want) {
|
|
t.Errorf("incorrect message\nwant substring: %s\ngot:\n%s", want, msg)
|
|
}
|
|
}
|
|
|
|
func TestImport_missingResourceConfig(t *testing.T) {
|
|
defer testChdir(t, testFixturePath("import-missing-resource-config"))()
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
view, _ := testView(t)
|
|
c := &ImportCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
View: view,
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
"-state", statePath,
|
|
"test_instance.foo",
|
|
"bar",
|
|
}
|
|
code := c.Run(args)
|
|
if code != 1 {
|
|
t.Fatalf("import succeeded; expected failure")
|
|
}
|
|
|
|
msg := ui.ErrorWriter.String()
|
|
if want := `resource address "test_instance.foo" does not exist`; !strings.Contains(msg, want) {
|
|
t.Errorf("incorrect message\nwant substring: %s\ngot:\n%s", want, msg)
|
|
}
|
|
}
|
|
|
|
func TestImport_missingModuleConfig(t *testing.T) {
|
|
defer testChdir(t, testFixturePath("import-missing-resource-config"))()
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
view, _ := testView(t)
|
|
c := &ImportCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
View: view,
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
"-state", statePath,
|
|
"module.baz.test_instance.foo",
|
|
"bar",
|
|
}
|
|
code := c.Run(args)
|
|
if code != 1 {
|
|
t.Fatalf("import succeeded; expected failure")
|
|
}
|
|
|
|
msg := ui.ErrorWriter.String()
|
|
if want := `module.baz is not defined in the configuration`; !strings.Contains(msg, want) {
|
|
t.Errorf("incorrect message\nwant substring: %s\ngot:\n%s", want, msg)
|
|
}
|
|
}
|
|
|
|
func TestImportModuleVarFile(t *testing.T) {
|
|
td := tempDir(t)
|
|
testCopyDir(t, testFixturePath("import-module-var-file"), td)
|
|
defer os.RemoveAll(td)
|
|
defer testChdir(t, td)()
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
p := testProvider()
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
|
ResourceTypes: map[string]providers.Schema{
|
|
"test_instance": {
|
|
Block: &configschema.Block{
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"foo": {Type: cty.String, Optional: true},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
providerSource, close := newMockProviderSource(t, map[string][]string{
|
|
"test": []string{"1.2.3"},
|
|
})
|
|
defer close()
|
|
|
|
// init to install the module
|
|
ui := new(cli.MockUi)
|
|
view, _ := testView(t)
|
|
m := Meta{
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
Ui: ui,
|
|
View: view,
|
|
ProviderSource: providerSource,
|
|
}
|
|
|
|
ic := &InitCommand{
|
|
Meta: m,
|
|
}
|
|
if code := ic.Run([]string{}); code != 0 {
|
|
t.Fatalf("init failed\n%s", ui.ErrorWriter)
|
|
}
|
|
|
|
// import
|
|
ui = new(cli.MockUi)
|
|
c := &ImportCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
View: view,
|
|
},
|
|
}
|
|
args := []string{
|
|
"-state", statePath,
|
|
"module.child.test_instance.foo",
|
|
"bar",
|
|
}
|
|
code := c.Run(args)
|
|
if code != 0 {
|
|
t.Fatalf("import failed; expected success")
|
|
}
|
|
}
|
|
|
|
// This test covers an edge case where a module with a complex input variable
|
|
// of nested objects has an invalid default which is overridden by the calling
|
|
// context, and is used in locals. If we don't evaluate module call variables
|
|
// for the import walk, this results in an error.
|
|
//
|
|
// The specific example has a variable "foo" which is a nested object:
|
|
//
|
|
// foo = { bar = { baz = true } }
|
|
//
|
|
// This is used as foo = var.foo in the call to the child module, which then
|
|
// uses the traversal foo.bar.baz in a local. A default value in the child
|
|
// module of {} causes this local evaluation to error, breaking import.
|
|
func TestImportModuleInputVariableEvaluation(t *testing.T) {
|
|
td := tempDir(t)
|
|
testCopyDir(t, testFixturePath("import-module-input-variable"), td)
|
|
defer os.RemoveAll(td)
|
|
defer testChdir(t, td)()
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
p := testProvider()
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
|
ResourceTypes: map[string]providers.Schema{
|
|
"test_instance": {
|
|
Block: &configschema.Block{
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"foo": {Type: cty.String, Optional: true},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
providerSource, close := newMockProviderSource(t, map[string][]string{
|
|
"test": {"1.2.3"},
|
|
})
|
|
defer close()
|
|
|
|
// init to install the module
|
|
ui := new(cli.MockUi)
|
|
view, _ := testView(t)
|
|
m := Meta{
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
Ui: ui,
|
|
View: view,
|
|
ProviderSource: providerSource,
|
|
}
|
|
|
|
ic := &InitCommand{
|
|
Meta: m,
|
|
}
|
|
if code := ic.Run([]string{}); code != 0 {
|
|
t.Fatalf("init failed\n%s", ui.ErrorWriter)
|
|
}
|
|
|
|
// import
|
|
ui = new(cli.MockUi)
|
|
c := &ImportCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
View: view,
|
|
},
|
|
}
|
|
args := []string{
|
|
"-state", statePath,
|
|
"module.child.test_instance.foo",
|
|
"bar",
|
|
}
|
|
code := c.Run(args)
|
|
if code != 0 {
|
|
t.Fatalf("import failed; expected success")
|
|
}
|
|
}
|
|
|
|
func TestImport_dataResource(t *testing.T) {
|
|
defer testChdir(t, testFixturePath("import-missing-resource-config"))()
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
view, _ := testView(t)
|
|
c := &ImportCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
View: view,
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
"-state", statePath,
|
|
"data.test_data_source.foo",
|
|
"bar",
|
|
}
|
|
code := c.Run(args)
|
|
if code != 1 {
|
|
t.Fatalf("import succeeded; expected failure")
|
|
}
|
|
|
|
msg := ui.ErrorWriter.String()
|
|
if want := `A managed resource address is required`; !strings.Contains(msg, want) {
|
|
t.Errorf("incorrect message\nwant substring: %s\ngot:\n%s", want, msg)
|
|
}
|
|
}
|
|
|
|
func TestImport_invalidResourceAddr(t *testing.T) {
|
|
defer testChdir(t, testFixturePath("import-missing-resource-config"))()
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
view, _ := testView(t)
|
|
c := &ImportCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
View: view,
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
"-state", statePath,
|
|
"bananas",
|
|
"bar",
|
|
}
|
|
code := c.Run(args)
|
|
if code != 1 {
|
|
t.Fatalf("import succeeded; expected failure")
|
|
}
|
|
|
|
msg := ui.ErrorWriter.String()
|
|
if want := `Error: Invalid address`; !strings.Contains(msg, want) {
|
|
t.Errorf("incorrect message\nwant substring: %s\ngot:\n%s", want, msg)
|
|
}
|
|
}
|
|
|
|
func TestImport_targetIsModule(t *testing.T) {
|
|
defer testChdir(t, testFixturePath("import-missing-resource-config"))()
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
view, _ := testView(t)
|
|
c := &ImportCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
View: view,
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
"-state", statePath,
|
|
"module.foo",
|
|
"bar",
|
|
}
|
|
code := c.Run(args)
|
|
if code != 1 {
|
|
t.Fatalf("import succeeded; expected failure")
|
|
}
|
|
|
|
msg := ui.ErrorWriter.String()
|
|
if want := `Error: Invalid address`; !strings.Contains(msg, want) {
|
|
t.Errorf("incorrect message\nwant substring: %s\ngot:\n%s", want, msg)
|
|
}
|
|
}
|
|
|
|
const testImportStr = `
|
|
test_instance.foo:
|
|
ID = yay
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
|
`
|