2016-05-04 19:32:08 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2016-11-02 18:30:28 +01:00
|
|
|
"fmt"
|
2018-11-17 03:37:26 +01:00
|
|
|
"log"
|
2017-06-27 17:07:45 +02:00
|
|
|
"os"
|
2018-03-20 17:44:12 +01:00
|
|
|
"path/filepath"
|
2017-05-17 03:20:08 +02:00
|
|
|
"strings"
|
2016-05-04 19:32:08 +02:00
|
|
|
"testing"
|
|
|
|
|
2018-09-29 18:45:09 +02:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
2021-05-17 21:17:09 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/configs/configschema"
|
2020-10-07 18:48:25 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/copy"
|
2021-05-17 19:40:40 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/providers"
|
2021-05-17 19:11:06 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/tfdiags"
|
2016-05-04 19:32:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestImport(t *testing.T) {
|
2017-04-22 02:19:37 +02:00
|
|
|
defer testChdir(t, testFixturePath("import-provider-implicit"))()
|
|
|
|
|
2016-05-04 19:32:08 +02:00
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-12 02:52:10 +01:00
|
|
|
view, _ := testView(t)
|
2016-05-04 19:32:08 +02:00
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2016-05-04 19:32:08 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-29 18:45:09 +02:00
|
|
|
p.ImportResourceStateFn = nil
|
2021-01-12 22:13:10 +01:00
|
|
|
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
|
2018-09-29 18:45:09 +02:00
|
|
|
ImportedResources: []providers.ImportedResource{
|
|
|
|
{
|
|
|
|
TypeName: "test_instance",
|
2018-10-14 16:59:15 +02:00
|
|
|
State: cty.ObjectVal(map[string]cty.Value{
|
2018-09-29 18:45:09 +02:00
|
|
|
"id": cty.StringVal("yay"),
|
|
|
|
}),
|
2016-05-04 19:32:08 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-02-18 16:13:43 +01:00
|
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
2021-01-12 22:13:10 +01:00
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2018-10-15 01:54:23 +02:00
|
|
|
"test_instance": {
|
2021-01-12 22:13:10 +01:00
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
|
|
},
|
2018-10-15 01:54:23 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-05-04 19:32:08 +02:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
2016-05-04 19:48:16 +02:00
|
|
|
"test_instance.foo",
|
2016-05-04 19:32:08 +02:00
|
|
|
"bar",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2018-09-29 18:45:09 +02:00
|
|
|
if !p.ImportResourceStateCalled {
|
|
|
|
t.Fatal("ImportResourceState should be called")
|
2016-05-04 19:32:08 +02:00
|
|
|
}
|
|
|
|
|
2016-05-04 19:48:16 +02:00
|
|
|
testStateOutput(t, statePath, testImportStr)
|
2016-05-04 19:32:08 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 18:30:28 +01:00
|
|
|
func TestImport_providerConfig(t *testing.T) {
|
|
|
|
defer testChdir(t, testFixturePath("import-provider"))()
|
|
|
|
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-12 02:52:10 +01:00
|
|
|
view, _ := testView(t)
|
2016-11-02 18:30:28 +01:00
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2016-11-02 18:30:28 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-29 18:45:09 +02:00
|
|
|
p.ImportResourceStateFn = nil
|
2021-01-12 22:13:10 +01:00
|
|
|
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
|
2018-09-29 18:45:09 +02:00
|
|
|
ImportedResources: []providers.ImportedResource{
|
|
|
|
{
|
|
|
|
TypeName: "test_instance",
|
2018-10-14 16:59:15 +02:00
|
|
|
State: cty.ObjectVal(map[string]cty.Value{
|
2018-09-29 18:45:09 +02:00
|
|
|
"id": cty.StringVal("yay"),
|
|
|
|
}),
|
2016-11-02 18:30:28 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-02-18 16:13:43 +01:00
|
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
2021-01-12 22:13:10 +01:00
|
|
|
Provider: providers.Schema{
|
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
2018-10-15 01:54:23 +02:00
|
|
|
},
|
|
|
|
},
|
2021-01-12 22:13:10 +01:00
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2018-10-15 01:54:23 +02:00
|
|
|
"test_instance": {
|
2021-01-12 22:13:10 +01:00
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
|
|
},
|
2018-10-15 01:54:23 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-11-02 18:30:28 +01:00
|
|
|
|
|
|
|
configured := false
|
2021-02-18 16:13:43 +01:00
|
|
|
p.ConfigureProviderFn = func(req providers.ConfigureProviderRequest) providers.ConfigureProviderResponse {
|
2016-11-02 18:30:28 +01:00
|
|
|
configured = true
|
|
|
|
|
2018-09-29 18:45:09 +02:00
|
|
|
cfg := req.Config
|
|
|
|
if !cfg.Type().HasAttribute("foo") {
|
2021-02-18 16:13:43 +01:00
|
|
|
return providers.ConfigureProviderResponse{
|
2018-09-29 18:45:09 +02:00
|
|
|
Diagnostics: tfdiags.Diagnostics{}.Append(fmt.Errorf("configuration has no foo argument")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if got, want := cfg.GetAttr("foo"), cty.StringVal("bar"); !want.RawEquals(got) {
|
2021-02-18 16:13:43 +01:00
|
|
|
return providers.ConfigureProviderResponse{
|
2018-09-29 18:45:09 +02:00
|
|
|
Diagnostics: tfdiags.Diagnostics{}.Append(fmt.Errorf("foo argument is %#v, but want %#v", got, want)),
|
|
|
|
}
|
2016-11-02 18:30:28 +01:00
|
|
|
}
|
|
|
|
|
2021-02-18 16:13:43 +01:00
|
|
|
return providers.ConfigureProviderResponse{}
|
2016-11-02 18:30:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
2016-11-02 19:11:42 +01:00
|
|
|
|
|
|
|
// Verify that we were called
|
|
|
|
if !configured {
|
|
|
|
t.Fatal("Configure should be called")
|
|
|
|
}
|
|
|
|
|
2018-09-29 18:45:09 +02:00
|
|
|
if !p.ImportResourceStateCalled {
|
|
|
|
t.Fatal("ImportResourceState should be called")
|
2016-11-02 19:11:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
testStateOutput(t, statePath, testImportStr)
|
|
|
|
}
|
|
|
|
|
2017-08-09 00:41:00 +02:00
|
|
|
// "remote" state provided by the "local" backend
|
|
|
|
func TestImport_remoteState(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("import-provider-remote-state"), td)
|
2017-08-09 00:41:00 +02:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
statePath := "imported.tfstate"
|
|
|
|
|
2020-03-31 23:02:40 +02:00
|
|
|
providerSource, close := newMockProviderSource(t, map[string][]string{
|
|
|
|
"test": []string{"1.2.3"},
|
|
|
|
})
|
|
|
|
defer close()
|
|
|
|
|
2017-08-09 00:41:00 +02:00
|
|
|
// init our backend
|
2018-11-15 22:31:30 +01:00
|
|
|
ui := cli.NewMockUi()
|
2021-02-12 02:52:10 +01:00
|
|
|
view, _ := testView(t)
|
2017-08-09 00:41:00 +02:00
|
|
|
m := Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2020-03-31 23:02:40 +02:00
|
|
|
ProviderSource: providerSource,
|
2017-08-09 00:41:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ic := &InitCommand{
|
|
|
|
Meta: m,
|
|
|
|
}
|
|
|
|
|
2018-11-15 22:31:30 +01:00
|
|
|
// (Using log here rather than t.Log so that these messages interleave with other trace logs)
|
|
|
|
log.Print("[TRACE] TestImport_remoteState running: terraform init")
|
2017-08-09 00:41:00 +02:00
|
|
|
if code := ic.Run([]string{}); code != 0 {
|
2018-11-15 22:31:30 +01:00
|
|
|
t.Fatalf("init failed\n%s", ui.ErrorWriter)
|
2017-08-09 00:41:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui = new(cli.MockUi)
|
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2017-08-09 00:41:00 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-29 18:45:09 +02:00
|
|
|
p.ImportResourceStateFn = nil
|
2021-01-12 22:13:10 +01:00
|
|
|
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
|
2018-09-29 18:45:09 +02:00
|
|
|
ImportedResources: []providers.ImportedResource{
|
|
|
|
{
|
|
|
|
TypeName: "test_instance",
|
2018-10-14 16:59:15 +02:00
|
|
|
State: cty.ObjectVal(map[string]cty.Value{
|
2018-09-29 18:45:09 +02:00
|
|
|
"id": cty.StringVal("yay"),
|
|
|
|
}),
|
2017-08-09 00:41:00 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-02-18 16:13:43 +01:00
|
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
2021-01-12 22:13:10 +01:00
|
|
|
Provider: providers.Schema{
|
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
2018-10-15 01:54:23 +02:00
|
|
|
},
|
|
|
|
},
|
2021-01-12 22:13:10 +01:00
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2018-10-15 01:54:23 +02:00
|
|
|
"test_instance": {
|
2021-01-12 22:13:10 +01:00
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
|
|
},
|
2018-10-15 01:54:23 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-08-09 00:41:00 +02:00
|
|
|
|
|
|
|
configured := false
|
2021-02-18 16:13:43 +01:00
|
|
|
p.ConfigureProviderFn = func(req providers.ConfigureProviderRequest) providers.ConfigureProviderResponse {
|
2018-10-15 01:54:23 +02:00
|
|
|
var diags tfdiags.Diagnostics
|
2017-08-09 00:41:00 +02:00
|
|
|
configured = true
|
2018-10-15 01:54:23 +02:00
|
|
|
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))
|
|
|
|
}
|
2021-02-18 16:13:43 +01:00
|
|
|
return providers.ConfigureProviderResponse{
|
2018-10-15 01:54:23 +02:00
|
|
|
Diagnostics: diags,
|
2017-08-09 00:41:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"test_instance.foo",
|
|
|
|
"bar",
|
|
|
|
}
|
2018-11-15 22:31:30 +01:00
|
|
|
log.Printf("[TRACE] TestImport_remoteState running: terraform import %s %s", args[0], args[1])
|
2017-08-09 00:41:00 +02:00
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
fmt.Println(ui.OutputWriter)
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2018-03-20 17:44:12 +01:00
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
|
2017-08-09 00:41:00 +02:00
|
|
|
// Verify that we were called
|
|
|
|
if !configured {
|
|
|
|
t.Fatal("Configure should be called")
|
|
|
|
}
|
|
|
|
|
2018-09-29 18:45:09 +02:00
|
|
|
if !p.ImportResourceStateCalled {
|
|
|
|
t.Fatal("ImportResourceState should be called")
|
2017-08-09 00:41:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
testStateOutput(t, statePath, testImportStr)
|
|
|
|
}
|
|
|
|
|
2020-02-14 19:47:29 +01:00
|
|
|
// early failure on import should not leave stale lock
|
|
|
|
func TestImport_initializationErrorShouldUnlock(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("import-provider-remote-state"), td)
|
2020-02-14 19:47:29 +01:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
statePath := "imported.tfstate"
|
|
|
|
|
2020-03-31 23:02:40 +02:00
|
|
|
providerSource, close := newMockProviderSource(t, map[string][]string{
|
|
|
|
"test": []string{"1.2.3"},
|
|
|
|
})
|
|
|
|
defer close()
|
|
|
|
|
2020-02-14 19:47:29 +01:00
|
|
|
// init our backend
|
|
|
|
ui := cli.NewMockUi()
|
2021-02-12 02:52:10 +01:00
|
|
|
view, _ := testView(t)
|
2020-02-14 19:47:29 +01:00
|
|
|
m := Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2020-03-31 23:02:40 +02:00
|
|
|
ProviderSource: providerSource,
|
2020-02-14 19:47:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2020-02-14 19:47:29 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
core: Functional-style API for terraform.Context
Previously terraform.Context was built in an unfortunate way where all of
the data was provided up front in terraform.NewContext and then mutated
directly by subsequent operations. That made the data flow hard to follow,
commonly leading to bugs, and also meant that we were forced to take
various actions too early in terraform.NewContext, rather than waiting
until a more appropriate time during an operation.
This (enormous) commit changes terraform.Context so that its fields are
broadly just unchanging data about the execution context (current
workspace name, available plugins, etc) whereas the main data Terraform
works with arrives via individual method arguments and is returned in
return values.
Specifically, this means that terraform.Context no longer "has-a" config,
state, and "planned changes", instead holding on to those only temporarily
during an operation. The caller is responsible for propagating the outcome
of one step into the next step so that the data flow between operations is
actually visible.
However, since that's a change to the main entry points in the "terraform"
package, this commit also touches every file in the codebase which
interacted with those APIs. Most of the noise here is in updating tests
to take the same actions using the new API style, but this also affects
the main-code callers in the backends and in the command package.
My goal here was to refactor without changing observable behavior, but in
practice there are a couple externally-visible behavior variations here
that seemed okay in service of the broader goal:
- The "terraform graph" command is no longer hooked directly into the
core graph builders, because that's no longer part of the public API.
However, I did include a couple new Context functions whose contract
is to produce a UI-oriented graph, and _for now_ those continue to
return the physical graph we use for those operations. There's no
exported API for generating the "validate" and "eval" graphs, because
neither is particularly interesting in its own right, and so
"terraform graph" no longer supports those graph types.
- terraform.NewContext no longer has the responsibility for collecting
all of the provider schemas up front. Instead, we wait until we need
them. However, that means that some of our error messages now have a
slightly different shape due to unwinding through a differently-shaped
call stack. As of this commit we also end up reloading the schemas
multiple times in some cases, which is functionally acceptable but
likely represents a performance regression. I intend to rework this to
use caching, but I'm saving that for a later commit because this one is
big enough already.
The proximal reason for this change is to resolve the chicken/egg problem
whereby there was previously no single point where we could apply "moved"
statements to the previous run state before creating a plan. With this
change in place, we can now do that as part of Context.Plan, prior to
forking the input state into the three separate state artifacts we use
during planning.
However, this is at least the third project in a row where the previous
API design led to piling more functionality into terraform.NewContext and
then working around the incorrect order of operations that produces, so
I intend that by paying the cost/risk of this large diff now we can in
turn reduce the cost/risk of future projects that relate to our main
workflow actions.
2021-08-24 21:06:38 +02:00
|
|
|
msg := strings.ReplaceAll(ui.ErrorWriter.String(), "\n", " ")
|
backend/local: Check dependency lock consistency before any operations
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.
2021-09-30 02:31:43 +02:00
|
|
|
if want := `provider registry.terraform.io/hashicorp/unknown: required by this configuration but no version is selected`; !strings.Contains(msg, want) {
|
2020-02-14 19:47:29 +01:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-24 22:01:23 +01:00
|
|
|
func TestImport_providerConfigWithVar(t *testing.T) {
|
|
|
|
defer testChdir(t, testFixturePath("import-provider-var"))()
|
|
|
|
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-12 02:52:10 +01:00
|
|
|
view, _ := testView(t)
|
2017-01-24 22:01:23 +01:00
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2017-01-24 22:01:23 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-29 18:45:09 +02:00
|
|
|
p.ImportResourceStateFn = nil
|
2021-01-12 22:13:10 +01:00
|
|
|
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
|
2018-09-29 18:45:09 +02:00
|
|
|
ImportedResources: []providers.ImportedResource{
|
|
|
|
{
|
|
|
|
TypeName: "test_instance",
|
2018-10-14 16:59:15 +02:00
|
|
|
State: cty.ObjectVal(map[string]cty.Value{
|
2018-09-29 18:45:09 +02:00
|
|
|
"id": cty.StringVal("yay"),
|
|
|
|
}),
|
2017-01-24 22:01:23 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-02-18 16:13:43 +01:00
|
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
2021-01-12 22:13:10 +01:00
|
|
|
Provider: providers.Schema{
|
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
2018-10-15 01:54:23 +02:00
|
|
|
},
|
|
|
|
},
|
2021-01-12 22:13:10 +01:00
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2018-10-15 01:54:23 +02:00
|
|
|
"test_instance": {
|
2021-01-12 22:13:10 +01:00
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
|
|
},
|
2018-10-15 01:54:23 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-01-24 22:01:23 +01:00
|
|
|
|
|
|
|
configured := false
|
2021-02-18 16:13:43 +01:00
|
|
|
p.ConfigureProviderFn = func(req providers.ConfigureProviderRequest) providers.ConfigureProviderResponse {
|
2018-10-15 01:54:23 +02:00
|
|
|
var diags tfdiags.Diagnostics
|
2017-01-24 22:01:23 +01:00
|
|
|
configured = true
|
2018-10-15 01:54:23 +02:00
|
|
|
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))
|
|
|
|
}
|
2021-02-18 16:13:43 +01:00
|
|
|
return providers.ConfigureProviderResponse{
|
2018-10-15 01:54:23 +02:00
|
|
|
Diagnostics: diags,
|
2017-01-24 22:01:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2018-09-29 18:45:09 +02:00
|
|
|
if !p.ImportResourceStateCalled {
|
|
|
|
t.Fatal("ImportResourceState should be called")
|
2017-01-24 22:01:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
testStateOutput(t, statePath, testImportStr)
|
|
|
|
}
|
|
|
|
|
2020-02-12 20:00:08 +01:00
|
|
|
func TestImport_providerConfigWithDataSource(t *testing.T) {
|
|
|
|
defer testChdir(t, testFixturePath("import-provider-datasource"))()
|
|
|
|
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-12 02:52:10 +01:00
|
|
|
view, _ := testView(t)
|
2020-02-12 20:00:08 +01:00
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2020-02-12 20:00:08 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p.ImportResourceStateFn = nil
|
2021-01-12 22:13:10 +01:00
|
|
|
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
|
2020-02-12 20:00:08 +01:00
|
|
|
ImportedResources: []providers.ImportedResource{
|
|
|
|
{
|
|
|
|
TypeName: "test_instance",
|
|
|
|
State: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.StringVal("yay"),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-02-18 16:13:43 +01:00
|
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
2021-01-12 22:13:10 +01:00
|
|
|
Provider: providers.Schema{
|
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
2020-02-12 20:00:08 +01:00
|
|
|
},
|
|
|
|
},
|
2021-01-12 22:13:10 +01:00
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2020-02-12 20:00:08 +01:00
|
|
|
"test_instance": {
|
2021-01-12 22:13:10 +01:00
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
|
|
},
|
2020-02-12 20:00:08 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-01-12 22:13:10 +01:00
|
|
|
DataSources: map[string]providers.Schema{
|
2020-02-12 20:00:08 +01:00
|
|
|
"test_data": {
|
2021-01-12 22:13:10 +01:00
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
2020-02-12 20:00:08 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-24 22:01:23 +01:00
|
|
|
func TestImport_providerConfigWithVarDefault(t *testing.T) {
|
|
|
|
defer testChdir(t, testFixturePath("import-provider-var-default"))()
|
|
|
|
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-12 02:52:10 +01:00
|
|
|
view, _ := testView(t)
|
2017-01-24 22:01:23 +01:00
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2017-01-24 22:01:23 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-29 18:45:09 +02:00
|
|
|
p.ImportResourceStateFn = nil
|
2021-01-12 22:13:10 +01:00
|
|
|
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
|
2018-09-29 18:45:09 +02:00
|
|
|
ImportedResources: []providers.ImportedResource{
|
|
|
|
{
|
|
|
|
TypeName: "test_instance",
|
2018-10-14 16:59:15 +02:00
|
|
|
State: cty.ObjectVal(map[string]cty.Value{
|
2018-09-29 18:45:09 +02:00
|
|
|
"id": cty.StringVal("yay"),
|
|
|
|
}),
|
2017-01-24 22:01:23 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-02-18 16:13:43 +01:00
|
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
2021-01-12 22:13:10 +01:00
|
|
|
Provider: providers.Schema{
|
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
2018-10-15 01:54:23 +02:00
|
|
|
},
|
|
|
|
},
|
2021-01-12 22:13:10 +01:00
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2018-10-15 01:54:23 +02:00
|
|
|
"test_instance": {
|
2021-01-12 22:13:10 +01:00
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
|
|
},
|
2018-10-15 01:54:23 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-01-24 22:01:23 +01:00
|
|
|
|
|
|
|
configured := false
|
2021-02-18 16:13:43 +01:00
|
|
|
p.ConfigureProviderFn = func(req providers.ConfigureProviderRequest) providers.ConfigureProviderResponse {
|
2018-10-15 01:54:23 +02:00
|
|
|
var diags tfdiags.Diagnostics
|
2017-01-24 22:01:23 +01:00
|
|
|
configured = true
|
2018-10-15 01:54:23 +02:00
|
|
|
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))
|
|
|
|
}
|
2021-02-18 16:13:43 +01:00
|
|
|
return providers.ConfigureProviderResponse{
|
2018-10-15 01:54:23 +02:00
|
|
|
Diagnostics: diags,
|
2017-01-24 22:01:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2018-09-29 18:45:09 +02:00
|
|
|
if !p.ImportResourceStateCalled {
|
|
|
|
t.Fatal("ImportResourceState should be called")
|
2017-01-24 22:01:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2021-02-12 02:52:10 +01:00
|
|
|
view, _ := testView(t)
|
2017-01-24 22:01:23 +01:00
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2017-01-24 22:01:23 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-29 18:45:09 +02:00
|
|
|
p.ImportResourceStateFn = nil
|
2021-01-12 22:13:10 +01:00
|
|
|
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
|
2018-09-29 18:45:09 +02:00
|
|
|
ImportedResources: []providers.ImportedResource{
|
|
|
|
{
|
|
|
|
TypeName: "test_instance",
|
2018-10-14 16:59:15 +02:00
|
|
|
State: cty.ObjectVal(map[string]cty.Value{
|
2018-09-29 18:45:09 +02:00
|
|
|
"id": cty.StringVal("yay"),
|
|
|
|
}),
|
2017-01-24 22:01:23 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-02-18 16:13:43 +01:00
|
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
2021-01-12 22:13:10 +01:00
|
|
|
Provider: providers.Schema{
|
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
2018-10-15 01:54:23 +02:00
|
|
|
},
|
|
|
|
},
|
2021-01-12 22:13:10 +01:00
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2018-10-15 01:54:23 +02:00
|
|
|
"test_instance": {
|
2021-01-12 22:13:10 +01:00
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
|
|
},
|
2018-10-15 01:54:23 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-01-24 22:01:23 +01:00
|
|
|
|
|
|
|
configured := false
|
2021-02-18 16:13:43 +01:00
|
|
|
p.ConfigureProviderFn = func(req providers.ConfigureProviderRequest) providers.ConfigureProviderResponse {
|
2018-10-15 01:54:23 +02:00
|
|
|
var diags tfdiags.Diagnostics
|
2017-01-24 22:01:23 +01:00
|
|
|
configured = true
|
2018-10-15 01:54:23 +02:00
|
|
|
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))
|
|
|
|
}
|
2021-02-18 16:13:43 +01:00
|
|
|
return providers.ConfigureProviderResponse{
|
2018-10-15 01:54:23 +02:00
|
|
|
Diagnostics: diags,
|
2017-01-24 22:01:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2018-09-29 18:45:09 +02:00
|
|
|
if !p.ImportResourceStateCalled {
|
|
|
|
t.Fatal("ImportResourceState should be called")
|
2017-01-24 22:01:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
testStateOutput(t, statePath, testImportStr)
|
|
|
|
}
|
|
|
|
|
2020-06-23 20:08:27 +02:00
|
|
|
func TestImport_allowMissingResourceConfig(t *testing.T) {
|
2017-09-18 20:41:30 +02:00
|
|
|
defer testChdir(t, testFixturePath("import-missing-resource-config"))()
|
|
|
|
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-12 02:52:10 +01:00
|
|
|
view, _ := testView(t)
|
2017-09-18 20:41:30 +02:00
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2017-09-18 20:41:30 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-29 18:45:09 +02:00
|
|
|
p.ImportResourceStateFn = nil
|
2021-01-12 22:13:10 +01:00
|
|
|
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
|
2018-09-29 18:45:09 +02:00
|
|
|
ImportedResources: []providers.ImportedResource{
|
|
|
|
{
|
|
|
|
TypeName: "test_instance",
|
2018-10-14 16:59:15 +02:00
|
|
|
State: cty.ObjectVal(map[string]cty.Value{
|
2018-09-29 18:45:09 +02:00
|
|
|
"id": cty.StringVal("yay"),
|
|
|
|
}),
|
2017-09-18 20:41:30 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-02-18 16:13:43 +01:00
|
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
2021-01-12 22:13:10 +01:00
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2018-10-15 01:54:23 +02:00
|
|
|
"test_instance": {
|
2021-01-12 22:13:10 +01:00
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
|
|
},
|
2018-10-15 01:54:23 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-09-18 20:41:30 +02:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-allow-missing-config",
|
|
|
|
"test_instance.foo",
|
|
|
|
"bar",
|
|
|
|
}
|
|
|
|
|
2020-06-23 20:08:27 +02:00
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
2017-09-18 20:41:30 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 20:08:27 +02:00
|
|
|
if !p.ImportResourceStateCalled {
|
|
|
|
t.Fatal("ImportResourceState should be called")
|
2020-03-20 13:15:29 +01:00
|
|
|
}
|
2020-06-23 20:08:27 +02:00
|
|
|
|
|
|
|
testStateOutput(t, statePath, testImportStr)
|
2017-09-18 20:41:30 +02:00
|
|
|
}
|
|
|
|
|
2017-12-08 19:22:07 +01:00
|
|
|
func TestImport_emptyConfig(t *testing.T) {
|
|
|
|
defer testChdir(t, testFixturePath("empty"))()
|
|
|
|
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-12 02:52:10 +01:00
|
|
|
view, _ := testView(t)
|
2017-12-08 19:22:07 +01:00
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2017-12-08 19:22:07 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 03:26:20 +02:00
|
|
|
func TestImport_missingResourceConfig(t *testing.T) {
|
|
|
|
defer testChdir(t, testFixturePath("import-missing-resource-config"))()
|
|
|
|
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-12 02:52:10 +01:00
|
|
|
view, _ := testView(t)
|
2017-05-17 03:26:20 +02:00
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2017-05-17 03:26:20 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2021-02-12 02:52:10 +01:00
|
|
|
view, _ := testView(t)
|
2017-05-17 03:26:20 +02:00
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2017-05-17 03:26:20 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2017-12-08 19:22:07 +01:00
|
|
|
if want := `module.baz is not defined in the configuration`; !strings.Contains(msg, want) {
|
2017-05-17 03:26:20 +02:00
|
|
|
t.Errorf("incorrect message\nwant substring: %s\ngot:\n%s", want, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 18:35:30 +02:00
|
|
|
func TestImportModuleVarFile(t *testing.T) {
|
2020-06-16 20:11:08 +02:00
|
|
|
td := tempDir(t)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("import-module-var-file"), td)
|
2020-06-16 20:11:08 +02:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
2020-06-12 18:35:30 +02:00
|
|
|
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
|
|
|
p := testProvider()
|
2021-02-18 16:13:43 +01:00
|
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
2021-01-12 22:13:10 +01:00
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2020-06-12 18:35:30 +02:00
|
|
|
"test_instance": {
|
2021-01-12 22:13:10 +01:00
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
2020-06-12 18:35:30 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
providerSource, close := newMockProviderSource(t, map[string][]string{
|
|
|
|
"test": []string{"1.2.3"},
|
2020-08-17 23:03:25 +02:00
|
|
|
})
|
|
|
|
defer close()
|
|
|
|
|
|
|
|
// init to install the module
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-12 02:52:10 +01:00
|
|
|
view, _ := testView(t)
|
2020-08-17 23:03:25 +02:00
|
|
|
m := Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2020-08-17 23:03:25 +02:00
|
|
|
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,
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2020-08-17 23:03:25 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
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)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("import-module-input-variable"), td)
|
2020-08-17 23:03:25 +02:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
|
|
|
p := testProvider()
|
2021-02-18 16:13:43 +01:00
|
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
2021-01-12 22:13:10 +01:00
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2020-08-17 23:03:25 +02:00
|
|
|
"test_instance": {
|
2021-01-12 22:13:10 +01:00
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
2020-08-17 23:03:25 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
providerSource, close := newMockProviderSource(t, map[string][]string{
|
|
|
|
"test": {"1.2.3"},
|
2020-06-12 18:35:30 +02:00
|
|
|
})
|
|
|
|
defer close()
|
|
|
|
|
|
|
|
// init to install the module
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-12 02:52:10 +01:00
|
|
|
view, _ := testView(t)
|
2020-06-12 18:35:30 +02:00
|
|
|
m := Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2020-06-12 18:35:30 +02:00
|
|
|
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,
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2020-06-12 18:35:30 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"module.child.test_instance.foo",
|
|
|
|
"bar",
|
|
|
|
}
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("import failed; expected success")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 03:20:08 +02:00
|
|
|
func TestImport_dataResource(t *testing.T) {
|
|
|
|
defer testChdir(t, testFixturePath("import-missing-resource-config"))()
|
|
|
|
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-12 02:52:10 +01:00
|
|
|
view, _ := testView(t)
|
2017-05-17 03:20:08 +02:00
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2017-05-17 03:20:08 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2018-10-10 02:47:53 +02:00
|
|
|
if want := `A managed resource address is required`; !strings.Contains(msg, want) {
|
2017-05-17 03:20:08 +02:00
|
|
|
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)
|
2021-02-12 02:52:10 +01:00
|
|
|
view, _ := testView(t)
|
2017-05-17 03:20:08 +02:00
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2017-05-17 03:20:08 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"bananas",
|
|
|
|
"bar",
|
|
|
|
}
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 1 {
|
|
|
|
t.Fatalf("import succeeded; expected failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := ui.ErrorWriter.String()
|
2018-10-10 02:47:53 +02:00
|
|
|
if want := `Error: Invalid address`; !strings.Contains(msg, want) {
|
2017-05-17 03:20:08 +02:00
|
|
|
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)
|
2021-02-12 02:52:10 +01:00
|
|
|
view, _ := testView(t)
|
2017-05-17 03:20:08 +02:00
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2017-05-17 03:20:08 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"module.foo",
|
|
|
|
"bar",
|
|
|
|
}
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 1 {
|
|
|
|
t.Fatalf("import succeeded; expected failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := ui.ErrorWriter.String()
|
2018-10-10 02:47:53 +02:00
|
|
|
if want := `Error: Invalid address`; !strings.Contains(msg, want) {
|
2017-05-17 03:20:08 +02:00
|
|
|
t.Errorf("incorrect message\nwant substring: %s\ngot:\n%s", want, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-04 19:48:16 +02:00
|
|
|
const testImportStr = `
|
2016-05-04 19:32:08 +02:00
|
|
|
test_instance.foo:
|
2016-05-04 19:48:16 +02:00
|
|
|
ID = yay
|
2020-04-01 21:55:25 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-05-04 19:32:08 +02:00
|
|
|
`
|