2016-04-26 19:47:56 +02:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2016-05-10 18:15:19 +02:00
|
|
|
"fmt"
|
2016-04-26 19:47:56 +02:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-04-28 19:25:58 +02:00
|
|
|
func TestContextImport_basic(t *testing.T) {
|
2016-04-26 19:47:56 +02:00
|
|
|
p := testProvider("aws")
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
|
|
|
Providers: map[string]ResourceProviderFactory{
|
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2016-04-28 19:25:58 +02:00
|
|
|
p.ImportStateReturn = []*InstanceState{
|
|
|
|
&InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
Ephemeral: EphemeralState{Type: "aws_instance"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-04-28 11:46:46 +02:00
|
|
|
state, err := ctx.Import(&ImportOpts{
|
|
|
|
Targets: []*ImportTarget{
|
|
|
|
&ImportTarget{
|
|
|
|
Addr: "aws_instance.foo",
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2016-04-26 19:47:56 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
actual := strings.TrimSpace(state.String())
|
2016-04-28 11:46:46 +02:00
|
|
|
expected := strings.TrimSpace(testImportStr)
|
2016-04-26 19:47:56 +02:00
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: \n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
2016-04-28 11:46:46 +02:00
|
|
|
|
2016-08-20 01:14:33 +02:00
|
|
|
func TestContextImport_countIndex(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
|
|
|
Providers: map[string]ResourceProviderFactory{
|
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
p.ImportStateReturn = []*InstanceState{
|
|
|
|
&InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
Ephemeral: EphemeralState{Type: "aws_instance"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err := ctx.Import(&ImportOpts{
|
|
|
|
Targets: []*ImportTarget{
|
|
|
|
&ImportTarget{
|
|
|
|
Addr: "aws_instance.foo[0]",
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(state.String())
|
|
|
|
expected := strings.TrimSpace(testImportCountIndexStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: \n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-01 00:53:48 +02:00
|
|
|
func TestContextImport_collision(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
|
|
|
Providers: map[string]ResourceProviderFactory{
|
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
|
|
|
|
State: &State{
|
|
|
|
Modules: []*ModuleState{
|
|
|
|
&ModuleState{
|
|
|
|
Path: []string{"root"},
|
|
|
|
Resources: map[string]*ResourceState{
|
|
|
|
"aws_instance.foo": &ResourceState{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Primary: &InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
p.ImportStateReturn = []*InstanceState{
|
|
|
|
&InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
Ephemeral: EphemeralState{Type: "aws_instance"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err := ctx.Import(&ImportOpts{
|
|
|
|
Targets: []*ImportTarget{
|
|
|
|
&ImportTarget{
|
|
|
|
Addr: "aws_instance.foo",
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(state.String())
|
|
|
|
expected := strings.TrimSpace(testImportCollisionStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: \n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-30 23:55:26 +02:00
|
|
|
func TestContextImport_missingType(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
|
|
|
Providers: map[string]ResourceProviderFactory{
|
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
p.ImportStateReturn = []*InstanceState{
|
|
|
|
&InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-04-30 23:56:22 +02:00
|
|
|
state, err := ctx.Import(&ImportOpts{
|
2016-04-30 23:55:26 +02:00
|
|
|
Targets: []*ImportTarget{
|
|
|
|
&ImportTarget{
|
|
|
|
Addr: "aws_instance.foo",
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
2016-04-30 23:56:22 +02:00
|
|
|
|
|
|
|
actual := strings.TrimSpace(state.String())
|
2016-05-01 00:53:48 +02:00
|
|
|
expected := "<no state>"
|
2016-04-30 23:56:22 +02:00
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: \n%s", actual)
|
|
|
|
}
|
2016-04-30 23:55:26 +02:00
|
|
|
}
|
|
|
|
|
2016-05-10 18:15:19 +02:00
|
|
|
func TestContextImport_moduleProvider(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
|
|
|
Providers: map[string]ResourceProviderFactory{
|
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
p.ImportStateReturn = []*InstanceState{
|
|
|
|
&InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
Ephemeral: EphemeralState{Type: "aws_instance"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
configured := false
|
|
|
|
p.ConfigureFn = func(c *ResourceConfig) error {
|
|
|
|
configured = true
|
|
|
|
|
|
|
|
if v, ok := c.Get("foo"); !ok || v.(string) != "bar" {
|
|
|
|
return fmt.Errorf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
m := testModule(t, "import-provider")
|
|
|
|
|
|
|
|
state, err := ctx.Import(&ImportOpts{
|
|
|
|
Module: m,
|
|
|
|
Targets: []*ImportTarget{
|
|
|
|
&ImportTarget{
|
|
|
|
Addr: "aws_instance.foo",
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !configured {
|
|
|
|
t.Fatal("didn't configure provider")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(state.String())
|
2016-11-02 18:29:58 +01:00
|
|
|
expected := strings.TrimSpace(testImportStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: \n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-25 00:36:45 +01:00
|
|
|
// Test that import sets up the graph properly for provider inheritance
|
|
|
|
func TestContextImport_providerInherit(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
|
|
|
Providers: map[string]ResourceProviderFactory{
|
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
p.ImportStateReturn = []*InstanceState{
|
|
|
|
&InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
Ephemeral: EphemeralState{Type: "aws_instance"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
configured := false
|
|
|
|
p.ConfigureFn = func(c *ResourceConfig) error {
|
|
|
|
configured = true
|
|
|
|
|
|
|
|
if v, ok := c.Get("foo"); !ok || v.(string) != "bar" {
|
|
|
|
return fmt.Errorf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
m := testModule(t, "import-provider-inherit")
|
|
|
|
|
|
|
|
_, err := ctx.Import(&ImportOpts{
|
|
|
|
Module: m,
|
|
|
|
Targets: []*ImportTarget{
|
|
|
|
&ImportTarget{
|
|
|
|
Addr: "module.child.aws_instance.foo",
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !configured {
|
|
|
|
t.Fatal("didn't configure provider")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 18:29:58 +01:00
|
|
|
// Test that import will interpolate provider configuration and use
|
|
|
|
// that configuration for import.
|
|
|
|
func TestContextImport_providerVarConfig(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
|
|
|
Module: testModule(t, "import-provider-vars"),
|
|
|
|
Providers: map[string]ResourceProviderFactory{
|
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
Variables: map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
configured := false
|
|
|
|
p.ConfigureFn = func(c *ResourceConfig) error {
|
|
|
|
configured = true
|
|
|
|
|
|
|
|
if v, ok := c.Get("foo"); !ok || v.(string) != "bar" {
|
|
|
|
return fmt.Errorf("bad value: %#v", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p.ImportStateReturn = []*InstanceState{
|
|
|
|
&InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
Ephemeral: EphemeralState{Type: "aws_instance"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err := ctx.Import(&ImportOpts{
|
|
|
|
Targets: []*ImportTarget{
|
|
|
|
&ImportTarget{
|
|
|
|
Addr: "aws_instance.foo",
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !configured {
|
|
|
|
t.Fatal("didn't configure provider")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(state.String())
|
2016-05-10 18:15:19 +02:00
|
|
|
expected := strings.TrimSpace(testImportStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: \n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 19:08:16 +01:00
|
|
|
// Test that provider configs can't reference resources.
|
|
|
|
func TestContextImport_providerNonVarConfig(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
|
|
|
Module: testModule(t, "import-provider-non-vars"),
|
|
|
|
Providers: map[string]ResourceProviderFactory{
|
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
p.ImportStateReturn = []*InstanceState{
|
|
|
|
&InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
Ephemeral: EphemeralState{Type: "aws_instance"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := ctx.Import(&ImportOpts{
|
|
|
|
Targets: []*ImportTarget{
|
|
|
|
&ImportTarget{
|
|
|
|
Addr: "aws_instance.foo",
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 19:28:23 +02:00
|
|
|
func TestContextImport_refresh(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
|
|
|
Providers: map[string]ResourceProviderFactory{
|
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
p.ImportStateReturn = []*InstanceState{
|
|
|
|
&InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
Ephemeral: EphemeralState{Type: "aws_instance"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p.RefreshFn = func(info *InstanceInfo, s *InstanceState) (*InstanceState, error) {
|
|
|
|
return &InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
Attributes: map[string]string{"foo": "bar"},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err := ctx.Import(&ImportOpts{
|
|
|
|
Targets: []*ImportTarget{
|
|
|
|
&ImportTarget{
|
|
|
|
Addr: "aws_instance.foo",
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(state.String())
|
|
|
|
expected := strings.TrimSpace(testImportRefreshStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: \n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-09 22:39:34 +02:00
|
|
|
func TestContextImport_refreshNil(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
|
|
|
Providers: map[string]ResourceProviderFactory{
|
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
p.ImportStateReturn = []*InstanceState{
|
|
|
|
&InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
Ephemeral: EphemeralState{Type: "aws_instance"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p.RefreshFn = func(info *InstanceInfo, s *InstanceState) (*InstanceState, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err := ctx.Import(&ImportOpts{
|
|
|
|
Targets: []*ImportTarget{
|
|
|
|
&ImportTarget{
|
|
|
|
Addr: "aws_instance.foo",
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(state.String())
|
|
|
|
expected := "<no state>"
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: \n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-30 07:09:34 +02:00
|
|
|
func TestContextImport_module(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
|
|
|
Providers: map[string]ResourceProviderFactory{
|
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
p.ImportStateReturn = []*InstanceState{
|
|
|
|
&InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
Ephemeral: EphemeralState{Type: "aws_instance"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err := ctx.Import(&ImportOpts{
|
|
|
|
Targets: []*ImportTarget{
|
|
|
|
&ImportTarget{
|
|
|
|
Addr: "module.foo.aws_instance.foo",
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(state.String())
|
2016-04-30 08:28:58 +02:00
|
|
|
expected := strings.TrimSpace(testImportModuleStr)
|
2016-04-30 07:09:34 +02:00
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: \n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-30 08:59:11 +02:00
|
|
|
func TestContextImport_moduleDepth2(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
|
|
|
Providers: map[string]ResourceProviderFactory{
|
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
p.ImportStateReturn = []*InstanceState{
|
|
|
|
&InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
Ephemeral: EphemeralState{Type: "aws_instance"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err := ctx.Import(&ImportOpts{
|
|
|
|
Targets: []*ImportTarget{
|
|
|
|
&ImportTarget{
|
|
|
|
Addr: "module.a.module.b.aws_instance.foo",
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(state.String())
|
|
|
|
expected := strings.TrimSpace(testImportModuleDepth2Str)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: \n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-01 00:01:31 +02:00
|
|
|
func TestContextImport_moduleDiff(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
|
|
|
Providers: map[string]ResourceProviderFactory{
|
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
|
|
|
|
State: &State{
|
|
|
|
Modules: []*ModuleState{
|
|
|
|
&ModuleState{
|
|
|
|
Path: []string{"root", "bar"},
|
|
|
|
Resources: map[string]*ResourceState{
|
|
|
|
"aws_instance.bar": &ResourceState{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Primary: &InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
p.ImportStateReturn = []*InstanceState{
|
|
|
|
&InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
Ephemeral: EphemeralState{Type: "aws_instance"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err := ctx.Import(&ImportOpts{
|
|
|
|
Targets: []*ImportTarget{
|
|
|
|
&ImportTarget{
|
|
|
|
Addr: "module.foo.aws_instance.foo",
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(state.String())
|
|
|
|
expected := strings.TrimSpace(testImportModuleDiffStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: \n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-01 00:00:18 +02:00
|
|
|
func TestContextImport_moduleExisting(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
|
|
|
Providers: map[string]ResourceProviderFactory{
|
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
|
|
|
|
State: &State{
|
|
|
|
Modules: []*ModuleState{
|
|
|
|
&ModuleState{
|
|
|
|
Path: []string{"root", "foo"},
|
|
|
|
Resources: map[string]*ResourceState{
|
|
|
|
"aws_instance.bar": &ResourceState{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Primary: &InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
p.ImportStateReturn = []*InstanceState{
|
|
|
|
&InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
Ephemeral: EphemeralState{Type: "aws_instance"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err := ctx.Import(&ImportOpts{
|
|
|
|
Targets: []*ImportTarget{
|
|
|
|
&ImportTarget{
|
|
|
|
Addr: "module.foo.aws_instance.foo",
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(state.String())
|
|
|
|
expected := strings.TrimSpace(testImportModuleExistingStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: \n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-01 00:04:40 +02:00
|
|
|
func TestContextImport_multiState(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
|
|
|
Providers: map[string]ResourceProviderFactory{
|
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
p.ImportStateReturn = []*InstanceState{
|
|
|
|
&InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
Ephemeral: EphemeralState{Type: "aws_instance"},
|
|
|
|
},
|
|
|
|
&InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
Ephemeral: EphemeralState{Type: "aws_instance_thing"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err := ctx.Import(&ImportOpts{
|
|
|
|
Targets: []*ImportTarget{
|
|
|
|
&ImportTarget{
|
|
|
|
Addr: "aws_instance.foo",
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(state.String())
|
|
|
|
expected := strings.TrimSpace(testImportMultiStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: \n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-01 00:29:03 +02:00
|
|
|
func TestContextImport_multiStateSame(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
|
|
|
Providers: map[string]ResourceProviderFactory{
|
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
p.ImportStateReturn = []*InstanceState{
|
|
|
|
&InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
Ephemeral: EphemeralState{Type: "aws_instance"},
|
|
|
|
},
|
|
|
|
&InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
Ephemeral: EphemeralState{Type: "aws_instance_thing"},
|
|
|
|
},
|
|
|
|
&InstanceState{
|
|
|
|
ID: "qux",
|
|
|
|
Ephemeral: EphemeralState{Type: "aws_instance_thing"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err := ctx.Import(&ImportOpts{
|
|
|
|
Targets: []*ImportTarget{
|
|
|
|
&ImportTarget{
|
|
|
|
Addr: "aws_instance.foo",
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(state.String())
|
|
|
|
expected := strings.TrimSpace(testImportMultiSameStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: \n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-23 10:44:52 +01:00
|
|
|
func TestContextImport_customProvider(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
|
|
|
Providers: map[string]ResourceProviderFactory{
|
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
p.ImportStateReturn = []*InstanceState{
|
|
|
|
&InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
Ephemeral: EphemeralState{Type: "aws_instance"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err := ctx.Import(&ImportOpts{
|
|
|
|
Targets: []*ImportTarget{
|
|
|
|
&ImportTarget{
|
|
|
|
Addr: "aws_instance.foo",
|
|
|
|
ID: "bar",
|
|
|
|
Provider: "aws.alias",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(state.String())
|
|
|
|
expected := strings.TrimSpace(testImportCustomProviderStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: \n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 19:25:58 +02:00
|
|
|
const testImportStr = `
|
|
|
|
aws_instance.foo:
|
|
|
|
ID = foo
|
|
|
|
provider = aws
|
|
|
|
`
|
2016-04-28 19:28:23 +02:00
|
|
|
|
2016-08-20 01:14:33 +02:00
|
|
|
const testImportCountIndexStr = `
|
|
|
|
aws_instance.foo.0:
|
|
|
|
ID = foo
|
|
|
|
provider = aws
|
|
|
|
`
|
|
|
|
|
2016-05-01 00:53:48 +02:00
|
|
|
const testImportCollisionStr = `
|
|
|
|
aws_instance.foo:
|
|
|
|
ID = bar
|
|
|
|
`
|
|
|
|
|
2016-04-30 08:28:58 +02:00
|
|
|
const testImportModuleStr = `
|
|
|
|
<no state>
|
|
|
|
module.foo:
|
|
|
|
aws_instance.foo:
|
|
|
|
ID = foo
|
|
|
|
provider = aws
|
|
|
|
`
|
|
|
|
|
2016-04-30 08:59:11 +02:00
|
|
|
const testImportModuleDepth2Str = `
|
|
|
|
<no state>
|
|
|
|
module.a.b:
|
|
|
|
aws_instance.foo:
|
|
|
|
ID = foo
|
|
|
|
provider = aws
|
|
|
|
`
|
|
|
|
|
2016-05-01 00:01:31 +02:00
|
|
|
const testImportModuleDiffStr = `
|
|
|
|
module.bar:
|
|
|
|
aws_instance.bar:
|
|
|
|
ID = bar
|
|
|
|
module.foo:
|
|
|
|
aws_instance.foo:
|
|
|
|
ID = foo
|
|
|
|
provider = aws
|
|
|
|
`
|
|
|
|
|
2016-05-01 00:00:18 +02:00
|
|
|
const testImportModuleExistingStr = `
|
|
|
|
module.foo:
|
|
|
|
aws_instance.bar:
|
|
|
|
ID = bar
|
|
|
|
aws_instance.foo:
|
|
|
|
ID = foo
|
|
|
|
provider = aws
|
|
|
|
`
|
|
|
|
|
2016-05-01 00:04:40 +02:00
|
|
|
const testImportMultiStr = `
|
|
|
|
aws_instance.foo:
|
|
|
|
ID = foo
|
|
|
|
provider = aws
|
|
|
|
aws_instance_thing.foo:
|
|
|
|
ID = bar
|
|
|
|
provider = aws
|
|
|
|
`
|
|
|
|
|
2016-05-01 00:29:03 +02:00
|
|
|
const testImportMultiSameStr = `
|
|
|
|
aws_instance.foo:
|
|
|
|
ID = foo
|
|
|
|
provider = aws
|
|
|
|
aws_instance_thing.foo:
|
|
|
|
ID = bar
|
|
|
|
provider = aws
|
|
|
|
aws_instance_thing.foo-1:
|
|
|
|
ID = qux
|
|
|
|
provider = aws
|
|
|
|
`
|
|
|
|
|
2016-04-28 19:28:23 +02:00
|
|
|
const testImportRefreshStr = `
|
|
|
|
aws_instance.foo:
|
|
|
|
ID = foo
|
|
|
|
provider = aws
|
|
|
|
foo = bar
|
|
|
|
`
|
2016-11-23 10:44:52 +01:00
|
|
|
|
|
|
|
const testImportCustomProviderStr = `
|
|
|
|
aws_instance.foo:
|
|
|
|
ID = foo
|
|
|
|
provider = aws.alias
|
|
|
|
`
|