2016-05-04 19:32:08 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2016-11-02 18:30:28 +01:00
|
|
|
"fmt"
|
2017-06-27 17:07:45 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2017-05-17 03:20:08 +02:00
|
|
|
"strings"
|
2016-05-04 19:32:08 +02:00
|
|
|
"testing"
|
|
|
|
|
2017-06-27 17:07:45 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/copy"
|
|
|
|
"github.com/hashicorp/terraform/plugin"
|
|
|
|
"github.com/hashicorp/terraform/plugin/discovery"
|
2016-05-04 19:32:08 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2016-05-04 19:32:08 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p.ImportStateFn = nil
|
|
|
|
p.ImportStateReturn = []*terraform.InstanceState{
|
|
|
|
&terraform.InstanceState{
|
|
|
|
ID: "yay",
|
|
|
|
Ephemeral: terraform.EphemeralState{
|
2016-05-04 19:48:16 +02:00
|
|
|
Type: "test_instance",
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.ImportStateCalled {
|
|
|
|
t.Fatal("ImportState should be called")
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2016-11-02 18:30:28 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p.ImportStateFn = nil
|
|
|
|
p.ImportStateReturn = []*terraform.InstanceState{
|
|
|
|
&terraform.InstanceState{
|
|
|
|
ID: "yay",
|
|
|
|
Ephemeral: terraform.EphemeralState{
|
|
|
|
Type: "test_instance",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
configured := false
|
|
|
|
p.ConfigureFn = func(c *terraform.ResourceConfig) error {
|
|
|
|
configured = true
|
|
|
|
|
|
|
|
if v, ok := c.Get("foo"); !ok || v.(string) != "bar" {
|
|
|
|
return fmt.Errorf("bad value: %#v", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.ImportStateCalled {
|
|
|
|
t.Fatal("ImportState should be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
copy.CopyDir(testFixturePath("import-provider-remote-state"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
statePath := "imported.tfstate"
|
|
|
|
|
|
|
|
// init our backend
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
m := Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
}
|
|
|
|
|
|
|
|
ic := &InitCommand{
|
|
|
|
Meta: m,
|
|
|
|
providerInstaller: &mockProviderInstaller{
|
|
|
|
Providers: map[string][]string{
|
|
|
|
"test": []string{"1.2.3"},
|
|
|
|
},
|
|
|
|
|
|
|
|
Dir: m.pluginDir(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := ic.Run([]string{}); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui = new(cli.MockUi)
|
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p.ImportStateFn = nil
|
|
|
|
p.ImportStateReturn = []*terraform.InstanceState{
|
|
|
|
&terraform.InstanceState{
|
|
|
|
ID: "yay",
|
|
|
|
Ephemeral: terraform.EphemeralState{
|
|
|
|
Type: "test_instance",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
configured := false
|
|
|
|
p.ConfigureFn = func(c *terraform.ResourceConfig) error {
|
|
|
|
configured = true
|
|
|
|
|
|
|
|
if v, ok := c.Get("foo"); !ok || v.(string) != "bar" {
|
|
|
|
return fmt.Errorf("bad value: %#v", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"test_instance.foo",
|
|
|
|
"bar",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
fmt.Println(ui.OutputWriter)
|
|
|
|
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.ImportStateCalled {
|
|
|
|
t.Fatal("ImportState should be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
testStateOutput(t, statePath, testImportStr)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2017-01-24 22:01:23 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p.ImportStateFn = nil
|
|
|
|
p.ImportStateReturn = []*terraform.InstanceState{
|
|
|
|
&terraform.InstanceState{
|
|
|
|
ID: "yay",
|
|
|
|
Ephemeral: terraform.EphemeralState{
|
|
|
|
Type: "test_instance",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
configured := false
|
|
|
|
p.ConfigureFn = func(c *terraform.ResourceConfig) error {
|
|
|
|
configured = true
|
|
|
|
|
|
|
|
if v, ok := c.Get("foo"); !ok || v.(string) != "bar" {
|
|
|
|
return fmt.Errorf("bad value: %#v", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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.ImportStateCalled {
|
|
|
|
t.Fatal("ImportState should be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
testStateOutput(t, statePath, testImportStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImport_providerConfigWithVarDefault(t *testing.T) {
|
|
|
|
defer testChdir(t, testFixturePath("import-provider-var-default"))()
|
|
|
|
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2017-01-24 22:01:23 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p.ImportStateFn = nil
|
|
|
|
p.ImportStateReturn = []*terraform.InstanceState{
|
|
|
|
&terraform.InstanceState{
|
|
|
|
ID: "yay",
|
|
|
|
Ephemeral: terraform.EphemeralState{
|
|
|
|
Type: "test_instance",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
configured := false
|
|
|
|
p.ConfigureFn = func(c *terraform.ResourceConfig) error {
|
|
|
|
configured = true
|
|
|
|
|
|
|
|
if v, ok := c.Get("foo"); !ok || v.(string) != "bar" {
|
|
|
|
return fmt.Errorf("bad value: %#v", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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.ImportStateCalled {
|
|
|
|
t.Fatal("ImportState 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)
|
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2017-01-24 22:01:23 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p.ImportStateFn = nil
|
|
|
|
p.ImportStateReturn = []*terraform.InstanceState{
|
|
|
|
&terraform.InstanceState{
|
|
|
|
ID: "yay",
|
|
|
|
Ephemeral: terraform.EphemeralState{
|
|
|
|
Type: "test_instance",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
configured := false
|
|
|
|
p.ConfigureFn = func(c *terraform.ResourceConfig) error {
|
|
|
|
configured = true
|
|
|
|
|
|
|
|
if v, ok := c.Get("foo"); !ok || v.(string) != "bar" {
|
|
|
|
return fmt.Errorf("bad value: %#v", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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.ImportStateCalled {
|
|
|
|
t.Fatal("ImportState should be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
testStateOutput(t, statePath, testImportStr)
|
|
|
|
}
|
|
|
|
|
2016-11-23 10:44:52 +01:00
|
|
|
func TestImport_customProvider(t *testing.T) {
|
2017-04-22 02:19:37 +02:00
|
|
|
defer testChdir(t, testFixturePath("import-provider-aliased"))()
|
|
|
|
|
2016-11-23 10:44:52 +01:00
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2016-11-23 10:44:52 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p.ImportStateFn = nil
|
|
|
|
p.ImportStateReturn = []*terraform.InstanceState{
|
|
|
|
&terraform.InstanceState{
|
|
|
|
ID: "yay",
|
|
|
|
Ephemeral: terraform.EphemeralState{
|
|
|
|
Type: "test_instance",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-provider", "test.alias",
|
|
|
|
"-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.ImportStateCalled {
|
|
|
|
t.Fatal("ImportState should be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
testStateOutput(t, statePath, testImportCustomProviderStr)
|
|
|
|
}
|
|
|
|
|
2017-09-18 20:41:30 +02:00
|
|
|
func TestImport_allowMissingResourceConfig(t *testing.T) {
|
|
|
|
defer testChdir(t, testFixturePath("import-missing-resource-config"))()
|
|
|
|
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p.ImportStateFn = nil
|
|
|
|
p.ImportStateReturn = []*terraform.InstanceState{
|
|
|
|
{
|
|
|
|
ID: "yay",
|
|
|
|
Ephemeral: terraform.EphemeralState{
|
|
|
|
Type: "test_instance",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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.ImportStateCalled {
|
|
|
|
t.Fatal("ImportState should be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
testStateOutput(t, statePath, testImportStr)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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 := `resource address must refer to a managed resource`; !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)
|
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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 := `invalid resource address "bananas"`; !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)
|
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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 := `resource address must include a full resource spec`; !strings.Contains(msg, want) {
|
|
|
|
t.Errorf("incorrect message\nwant substring: %s\ngot:\n%s", want, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-27 17:07:45 +02:00
|
|
|
// make sure we search the full plugin path during import
|
|
|
|
func TestImport_pluginDir(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("import-provider"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
// make a fake provider in a custom plugin directory
|
|
|
|
if err := os.Mkdir("plugins", 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile("plugins/terraform-provider-test_v1.1.1_x4", []byte("invalid binary"), 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ImportCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// store our custom plugin path, which would normally happen during init
|
|
|
|
if err := c.storePluginPath([]string{"./plugins"}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we need to go through some plugin init.
|
|
|
|
// This discovers our fake plugin and writes the lock file.
|
|
|
|
initCmd := &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
pluginPath: []string{"./plugins"},
|
|
|
|
Ui: new(cli.MockUi),
|
|
|
|
},
|
|
|
|
providerInstaller: &discovery.ProviderInstaller{
|
|
|
|
PluginProtocolVersion: plugin.Handshake.ProtocolVersion,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := initCmd.getProviders(".", nil, false); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"test_instance.foo",
|
|
|
|
"bar",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code == 0 {
|
|
|
|
t.Fatalf("expected error, got: %s", ui.OutputWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
outMsg := ui.OutputWriter.String()
|
|
|
|
// if we were missing a plugin, the output will have some explanation
|
|
|
|
// about requirements. If discovery starts verifying binary compatibility,
|
|
|
|
// we will need to write a dummy provider above.
|
|
|
|
if strings.Contains(outMsg, "requirements") {
|
|
|
|
t.Fatal("unexpected output:", outMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We wanted a plugin execution error, rather than a requirement error.
|
|
|
|
// Looking for "exec" in the error should suffice for now.
|
|
|
|
errMsg := ui.ErrorWriter.String()
|
|
|
|
if !strings.Contains(errMsg, "exec") {
|
|
|
|
t.Fatal("unexpected error:", errMsg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2017-11-08 03:18:01 +01:00
|
|
|
provider = provider.test
|
2016-05-04 19:32:08 +02:00
|
|
|
`
|
2016-11-23 10:44:52 +01:00
|
|
|
|
|
|
|
const testImportCustomProviderStr = `
|
|
|
|
test_instance.foo:
|
|
|
|
ID = yay
|
2017-11-08 03:18:01 +01:00
|
|
|
provider = provider.test.alias
|
2016-11-23 10:44:52 +01:00
|
|
|
`
|