core: Get tests compiling again

After the refactoring to integrate HCL2 many of the tests were no longer
using correct types, attribute names, etc.

This is a bulk update of all of the tests to make them compile again, with
minimal changes otherwise. Although the tests now compile, many of them
do not yet pass. The tests will be gradually repaired in subsequent
commits, as we continue to complete the refactoring and retrofit work.
This commit is contained in:
Martin Atkins 2018-05-04 19:24:06 -07:00
parent 119c80d5e2
commit 4a21b763aa
49 changed files with 2142 additions and 3170 deletions

File diff suppressed because it is too large Load Diff

View File

@ -4,13 +4,16 @@ import (
"fmt"
"strings"
"testing"
"github.com/hashicorp/terraform/addrs"
"github.com/zclconf/go-cty/cty"
)
func TestContextImport_basic(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "import-provider")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -28,8 +31,10 @@ func TestContextImport_basic(t *testing.T) {
state, err := ctx.Import(&ImportOpts{
Targets: []*ImportTarget{
&ImportTarget{
Addr: "aws_instance.foo",
ID: "bar",
Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
),
ID: "bar",
},
},
})
@ -47,7 +52,7 @@ func TestContextImport_countIndex(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "import-provider")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -65,8 +70,10 @@ func TestContextImport_countIndex(t *testing.T) {
state, err := ctx.Import(&ImportOpts{
Targets: []*ImportTarget{
&ImportTarget{
Addr: "aws_instance.foo[0]",
ID: "bar",
Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.IntKey(0),
),
ID: "bar",
},
},
})
@ -85,7 +92,7 @@ func TestContextImport_collision(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "import-provider")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -119,8 +126,10 @@ func TestContextImport_collision(t *testing.T) {
state, err := ctx.Import(&ImportOpts{
Targets: []*ImportTarget{
&ImportTarget{
Addr: "aws_instance.foo",
ID: "bar",
Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
),
ID: "bar",
},
},
})
@ -139,7 +148,7 @@ func TestContextImport_missingType(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "import-provider")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -156,8 +165,10 @@ func TestContextImport_missingType(t *testing.T) {
state, err := ctx.Import(&ImportOpts{
Targets: []*ImportTarget{
&ImportTarget{
Addr: "aws_instance.foo",
ID: "bar",
Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
),
ID: "bar",
},
},
})
@ -176,7 +187,7 @@ func TestContextImport_moduleProvider(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "import-provider")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -203,11 +214,13 @@ func TestContextImport_moduleProvider(t *testing.T) {
}
state, err := ctx.Import(&ImportOpts{
Module: m,
Config: m,
Targets: []*ImportTarget{
&ImportTarget{
Addr: "aws_instance.foo",
ID: "bar",
Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
),
ID: "bar",
},
},
})
@ -231,7 +244,7 @@ func TestContextImport_providerModule(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "import-provider-module")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -258,11 +271,13 @@ func TestContextImport_providerModule(t *testing.T) {
}
_, err := ctx.Import(&ImportOpts{
Module: m,
Config: m,
Targets: []*ImportTarget{
&ImportTarget{
Addr: "module.child.aws_instance.foo",
ID: "bar",
Addr: addrs.RootModuleInstance.Child("child", addrs.NoKey).ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
),
ID: "bar",
},
},
})
@ -281,14 +296,17 @@ func TestContextImport_providerVarConfig(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "import-provider-vars")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
),
Variables: map[string]interface{}{
"foo": "bar",
Variables: InputValues{
"foo": &InputValue{
Value: cty.StringVal("foo"),
SourceType: ValueFromCaller,
},
},
})
@ -313,8 +331,10 @@ func TestContextImport_providerVarConfig(t *testing.T) {
state, err := ctx.Import(&ImportOpts{
Targets: []*ImportTarget{
&ImportTarget{
Addr: "aws_instance.foo",
ID: "bar",
Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
),
ID: "bar",
},
},
})
@ -338,7 +358,7 @@ func TestContextImport_providerNonVarConfig(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "import-provider-non-vars")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -356,8 +376,10 @@ func TestContextImport_providerNonVarConfig(t *testing.T) {
_, err := ctx.Import(&ImportOpts{
Targets: []*ImportTarget{
&ImportTarget{
Addr: "aws_instance.foo",
ID: "bar",
Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
),
ID: "bar",
},
},
})
@ -370,7 +392,7 @@ func TestContextImport_refresh(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "import-provider")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -395,8 +417,10 @@ func TestContextImport_refresh(t *testing.T) {
state, err := ctx.Import(&ImportOpts{
Targets: []*ImportTarget{
&ImportTarget{
Addr: "aws_instance.foo",
ID: "bar",
Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
),
ID: "bar",
},
},
})
@ -415,7 +439,7 @@ func TestContextImport_refreshNil(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "import-provider")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -437,8 +461,10 @@ func TestContextImport_refreshNil(t *testing.T) {
state, err := ctx.Import(&ImportOpts{
Targets: []*ImportTarget{
&ImportTarget{
Addr: "aws_instance.foo",
ID: "bar",
Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
),
ID: "bar",
},
},
})
@ -457,7 +483,7 @@ func TestContextImport_module(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "import-provider")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -475,8 +501,10 @@ func TestContextImport_module(t *testing.T) {
state, err := ctx.Import(&ImportOpts{
Targets: []*ImportTarget{
&ImportTarget{
Addr: "module.foo.aws_instance.foo",
ID: "bar",
Addr: addrs.RootModuleInstance.Child("foo", addrs.NoKey).ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
),
ID: "bar",
},
},
})
@ -495,7 +523,7 @@ func TestContextImport_moduleDepth2(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "import-provider")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -513,8 +541,10 @@ func TestContextImport_moduleDepth2(t *testing.T) {
state, err := ctx.Import(&ImportOpts{
Targets: []*ImportTarget{
&ImportTarget{
Addr: "module.a.module.b.aws_instance.foo",
ID: "bar",
Addr: addrs.RootModuleInstance.Child("a", addrs.NoKey).Child("b", addrs.NoKey).ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
),
ID: "bar",
},
},
})
@ -533,7 +563,7 @@ func TestContextImport_moduleDiff(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "import-provider")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -567,8 +597,10 @@ func TestContextImport_moduleDiff(t *testing.T) {
state, err := ctx.Import(&ImportOpts{
Targets: []*ImportTarget{
&ImportTarget{
Addr: "module.foo.aws_instance.foo",
ID: "bar",
Addr: addrs.RootModuleInstance.Child("foo", addrs.NoKey).ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
),
ID: "bar",
},
},
})
@ -587,7 +619,7 @@ func TestContextImport_moduleExisting(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "import-provider")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -621,8 +653,10 @@ func TestContextImport_moduleExisting(t *testing.T) {
state, err := ctx.Import(&ImportOpts{
Targets: []*ImportTarget{
&ImportTarget{
Addr: "module.foo.aws_instance.foo",
ID: "bar",
Addr: addrs.RootModuleInstance.Child("foo", addrs.NoKey).ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
),
ID: "bar",
},
},
})
@ -641,7 +675,7 @@ func TestContextImport_multiState(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "import-provider")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -663,8 +697,10 @@ func TestContextImport_multiState(t *testing.T) {
state, err := ctx.Import(&ImportOpts{
Targets: []*ImportTarget{
&ImportTarget{
Addr: "aws_instance.foo",
ID: "bar",
Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
),
ID: "bar",
},
},
})
@ -683,7 +719,7 @@ func TestContextImport_multiStateSame(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "import-provider")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -709,8 +745,10 @@ func TestContextImport_multiStateSame(t *testing.T) {
state, err := ctx.Import(&ImportOpts{
Targets: []*ImportTarget{
&ImportTarget{
Addr: "aws_instance.foo",
ID: "bar",
Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
),
ID: "bar",
},
},
})
@ -730,7 +768,7 @@ func TestContextImport_customProviderMissing(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "import-provider")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -748,9 +786,11 @@ func TestContextImport_customProviderMissing(t *testing.T) {
_, err := ctx.Import(&ImportOpts{
Targets: []*ImportTarget{
&ImportTarget{
Addr: "aws_instance.foo",
ID: "bar",
Provider: "aws.alias",
Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
),
ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigAliased("aws", "alias"),
},
},
})
@ -763,7 +803,7 @@ func TestContextImport_customProvider(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "import-provider-alias")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -781,9 +821,11 @@ func TestContextImport_customProvider(t *testing.T) {
state, err := ctx.Import(&ImportOpts{
Targets: []*ImportTarget{
&ImportTarget{
Addr: "aws_instance.foo",
ID: "bar",
Provider: "aws.alias",
Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
),
ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigAliased("aws", "alias"),
},
},
})

View File

@ -6,6 +6,8 @@ import (
"strings"
"sync"
"testing"
"github.com/zclconf/go-cty/cty"
)
func TestContext2Input(t *testing.T) {
@ -15,18 +17,23 @@ func TestContext2Input(t *testing.T) {
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
),
Variables: map[string]interface{}{
"foo": "us-west-2",
"amis": []map[string]interface{}{
map[string]interface{}{
"us-east-1": "override",
},
Variables: InputValues{
"foo": &InputValue{
Value: cty.StringVal("us-west-2"),
SourceType: ValueFromCaller,
},
"amis": &InputValue{
Value: cty.ListVal([]cty.Value{
cty.MapVal(map[string]cty.Value{
"us-east-1": cty.StringVal("override"),
}),
}),
},
},
UIInput: input,
@ -62,7 +69,7 @@ func TestContext2Input_moduleComputedOutputElement(t *testing.T) {
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -85,7 +92,7 @@ func TestContext2Input_badVarDefault(t *testing.T) {
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -109,7 +116,7 @@ func TestContext2Input_provider(t *testing.T) {
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -153,7 +160,7 @@ func TestContext2Input_providerMulti(t *testing.T) {
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -201,7 +208,7 @@ func TestContext2Input_providerOnce(t *testing.T) {
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -240,7 +247,7 @@ func TestContext2Input_providerId(t *testing.T) {
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -292,14 +299,17 @@ func TestContext2Input_providerOnly(t *testing.T) {
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
),
Variables: map[string]interface{}{
"foo": "us-west-2",
Variables: InputValues{
"foo": &InputValue{
Value: cty.StringVal("us-west-2"),
SourceType: ValueFromCaller,
},
},
UIInput: input,
})
@ -349,14 +359,17 @@ func TestContext2Input_providerVars(t *testing.T) {
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
),
Variables: map[string]interface{}{
"foo": "bar",
Variables: InputValues{
"foo": &InputValue{
Value: cty.StringVal("bar"),
SourceType: ValueFromCaller,
},
},
UIInput: input,
})
@ -399,7 +412,7 @@ func TestContext2Input_providerVarsModuleInherit(t *testing.T) {
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -430,14 +443,17 @@ func TestContext2Input_varOnly(t *testing.T) {
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
),
Variables: map[string]interface{}{
"foo": "us-west-2",
Variables: InputValues{
"foo": &InputValue{
Value: cty.StringVal("us-west-2"),
SourceType: ValueFromCaller,
},
},
UIInput: input,
})
@ -487,14 +503,17 @@ func TestContext2Input_varOnlyUnset(t *testing.T) {
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
),
Variables: map[string]interface{}{
"foo": "foovalue",
Variables: InputValues{
"foo": &InputValue{
Value: cty.StringVal("foovalue"),
SourceType: ValueFromCaller,
},
},
UIInput: input,
})
@ -531,13 +550,13 @@ func TestContext2Input_varWithDefault(t *testing.T) {
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
),
Variables: map[string]interface{}{},
Variables: InputValues{},
UIInput: input,
})
@ -580,14 +599,17 @@ func TestContext2Input_varPartiallyComputed(t *testing.T) {
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
),
Variables: map[string]interface{}{
"foo": "foovalue",
Variables: InputValues{
"foo": &InputValue{
Value: cty.StringVal("foovalue"),
SourceType: ValueFromCaller,
},
},
UIInput: input,
State: &State{
@ -645,7 +667,7 @@ func TestContext2Input_interpolateVar(t *testing.T) {
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"template": testProviderFuncFixed(p),
@ -666,13 +688,13 @@ func TestContext2Input_hcl(t *testing.T) {
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"hcl": testProviderFuncFixed(p),
},
),
Variables: map[string]interface{}{},
Variables: InputValues{},
UIInput: input,
})
@ -710,7 +732,7 @@ func TestContext2Input_submoduleTriggersInvalidCount(t *testing.T) {
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -762,7 +784,7 @@ func TestContext2Input_dataSourceRequiresRefresh(t *testing.T) {
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"null": testProviderFuncFixed(p),

View File

@ -9,6 +9,9 @@ import (
"strings"
"sync"
"testing"
"github.com/hashicorp/terraform/addrs"
"github.com/zclconf/go-cty/cty"
)
func TestContext2Plan_basic(t *testing.T) {
@ -16,7 +19,7 @@ func TestContext2Plan_basic(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -72,7 +75,7 @@ func TestContext2Plan_createBefore_deposed(t *testing.T) {
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -108,14 +111,17 @@ func TestContext2Plan_createBefore_maintainRoot(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
),
Variables: map[string]interface{}{
"in": "a,b,c",
Variables: InputValues{
"in": &InputValue{
Value: cty.StringVal("a,b,c"),
SourceType: ValueFromCaller,
},
},
})
@ -153,7 +159,7 @@ func TestContext2Plan_emptyDiff(t *testing.T) {
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -178,7 +184,7 @@ func TestContext2Plan_escapedVar(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -203,7 +209,7 @@ func TestContext2Plan_minimal(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -228,7 +234,7 @@ func TestContext2Plan_modules(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -254,7 +260,7 @@ func TestContext2Plan_moduleCycle(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -281,7 +287,7 @@ func TestContext2Plan_moduleDeadlock(t *testing.T) {
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -318,7 +324,7 @@ func TestContext2Plan_moduleInput(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -343,7 +349,7 @@ func TestContext2Plan_moduleInputComputed(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -368,14 +374,17 @@ func TestContext2Plan_moduleInputFromVar(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
),
Variables: map[string]interface{}{
"foo": "52",
Variables: InputValues{
"foo": &InputValue{
Value: cty.StringVal("52"),
SourceType: ValueFromCaller,
},
},
})
@ -396,7 +405,7 @@ func TestContext2Plan_moduleMultiVar(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -436,7 +445,7 @@ func TestContext2Plan_moduleOrphans(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -501,7 +510,7 @@ func TestContext2Plan_moduleOrphansWithProvisioner(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -550,7 +559,7 @@ func TestContext2Plan_moduleProviderInherit(t *testing.T) {
m := testModule(t, "plan-module-provider-inherit")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": func() (ResourceProvider, error) {
@ -602,7 +611,7 @@ func TestContext2Plan_moduleProviderInheritDeep(t *testing.T) {
m := testModule(t, "plan-module-provider-inherit-deep")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": func() (ResourceProvider, error) {
@ -649,7 +658,7 @@ func TestContext2Plan_moduleProviderDefaultsVar(t *testing.T) {
m := testModule(t, "plan-module-provider-defaults-var")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": func() (ResourceProvider, error) {
@ -676,8 +685,11 @@ func TestContext2Plan_moduleProviderDefaultsVar(t *testing.T) {
},
},
),
Variables: map[string]interface{}{
"foo": "root",
Variables: InputValues{
"foo": &InputValue{
Value: cty.StringVal("root"),
SourceType: ValueFromCaller,
},
},
})
@ -701,7 +713,7 @@ func TestContext2Plan_moduleProviderVar(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -726,7 +738,7 @@ func TestContext2Plan_moduleVar(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -751,7 +763,7 @@ func TestContext2Plan_moduleVarWrongTypeBasic(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -770,7 +782,7 @@ func TestContext2Plan_moduleVarWrongTypeNested(t *testing.T) {
p := testProvider("null")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"null": testProviderFuncFixed(p),
@ -789,7 +801,7 @@ func TestContext2Plan_moduleVarWithDefaultValue(t *testing.T) {
p := testProvider("null")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"null": testProviderFuncFixed(p),
@ -808,7 +820,7 @@ func TestContext2Plan_moduleVarComputed(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -833,7 +845,7 @@ func TestContext2Plan_nil(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -870,7 +882,7 @@ func TestContext2Plan_preventDestroy_bad(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -907,7 +919,7 @@ func TestContext2Plan_preventDestroy_good(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -944,7 +956,7 @@ func TestContext2Plan_preventDestroy_countBad(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -987,7 +999,7 @@ func TestContext2Plan_preventDestroy_countGood(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -1030,7 +1042,7 @@ func TestContext2Plan_preventDestroy_countGoodNoChange(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -1071,7 +1083,7 @@ func TestContext2Plan_preventDestroy_destroyPlan(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -1110,7 +1122,7 @@ func TestContext2Plan_provisionerCycle(t *testing.T) {
p.DiffFn = testDiffFn
pr := testProvisioner()
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -1132,7 +1144,7 @@ func TestContext2Plan_computed(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -1157,7 +1169,7 @@ func TestContext2Plan_computedDataResource(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -1206,7 +1218,7 @@ func TestContext2Plan_computedDataCountResource(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -1239,7 +1251,7 @@ func TestContext2Plan_localValueCount(t *testing.T) {
p := testProvider("test")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"test": testProviderFuncFixed(p),
@ -1296,7 +1308,7 @@ func TestContext2Plan_dataSourceTypeMismatch(t *testing.T) {
return nil, nil
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
// Pretend like we ran a Refresh and the AZs data source was populated.
State: &State{
Modules: []*ModuleState{
@ -1325,14 +1337,14 @@ func TestContext2Plan_dataSourceTypeMismatch(t *testing.T) {
),
})
_, err := ctx.Plan()
_, diags := ctx.Plan()
if err == nil {
if !diags.HasErrors() {
t.Fatalf("Expected err, got none!")
}
expected := "Expected ami to be string"
if !strings.Contains(err.Error(), expected) {
t.Fatalf("expected:\n\n%s\n\nto contain:\n\n%s", err, expected)
if errStr := diags.Err().Error(); !strings.Contains(errStr, expected) {
t.Fatalf("expected:\n\n%s\n\nto contain:\n\n%s", errStr, expected)
}
}
@ -1367,7 +1379,7 @@ func TestContext2Plan_dataResourceBecomesComputed(t *testing.T) {
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -1434,7 +1446,7 @@ func TestContext2Plan_computedList(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -1461,7 +1473,7 @@ func TestContext2Plan_computedMultiIndex(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -1486,7 +1498,7 @@ func TestContext2Plan_count(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -1515,7 +1527,7 @@ func TestContext2Plan_countComputed(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -1534,7 +1546,7 @@ func TestContext2Plan_countComputedModule(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -1556,7 +1568,7 @@ func TestContext2Plan_countModuleStatic(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -1592,7 +1604,7 @@ func TestContext2Plan_countModuleStaticGrandchild(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -1628,7 +1640,7 @@ func TestContext2Plan_countIndex(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -1653,7 +1665,7 @@ func TestContext2Plan_countIndexZero(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -1678,14 +1690,17 @@ func TestContext2Plan_countVar(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
),
Variables: map[string]interface{}{
"count": "3",
Variables: InputValues{
"count": &InputValue{
Value: cty.StringVal("3"),
SourceType: ValueFromCaller,
},
},
})
@ -1706,7 +1721,7 @@ func TestContext2Plan_countZero(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -1732,7 +1747,7 @@ func TestContext2Plan_countOneIndex(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -1788,7 +1803,7 @@ func TestContext2Plan_countDecreaseToOne(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -1833,7 +1848,7 @@ func TestContext2Plan_countIncreaseFromNotSet(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -1878,7 +1893,7 @@ func TestContext2Plan_countIncreaseFromOne(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -1938,7 +1953,7 @@ func TestContext2Plan_countIncreaseFromOneCorrupted(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -2017,7 +2032,7 @@ func TestContext2Plan_countIncreaseWithSplatReference(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -2088,7 +2103,7 @@ func TestContext2Plan_destroy(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -2145,7 +2160,7 @@ func TestContext2Plan_moduleDestroy(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -2199,7 +2214,7 @@ func TestContext2Plan_moduleDestroyCycle(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -2251,7 +2266,7 @@ func TestContext2Plan_moduleDestroyMultivar(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -2283,7 +2298,7 @@ func TestContext2Plan_pathVar(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -2291,9 +2306,9 @@ func TestContext2Plan_pathVar(t *testing.T) {
),
})
plan, err := ctx.Plan()
if err != nil {
t.Fatalf("err: %s", err)
plan, diags := ctx.Plan()
if diags.HasErrors() {
t.Fatalf("err: %s", diags.Err())
}
actual := strings.TrimSpace(plan.String())
@ -2304,8 +2319,9 @@ func TestContext2Plan_pathVar(t *testing.T) {
expected = fmt.Sprintf(
expected,
cwd,
m.Config().Dir,
m.Config().Dir)
m.Module.SourceDir,
m.Module.SourceDir,
)
if actual != expected {
t.Fatalf("bad:\n%s\n\nexpected:\n\n%s", actual, expected)
@ -2334,7 +2350,7 @@ func TestContext2Plan_diffVar(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -2379,7 +2395,7 @@ func TestContext2Plan_hook(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
Hooks: []Hook{h},
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
@ -2409,7 +2425,7 @@ func TestContext2Plan_closeProvider(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -2447,7 +2463,7 @@ func TestContext2Plan_orphan(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -2475,7 +2491,7 @@ func TestContext2Plan_shadowUuid(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -2509,7 +2525,7 @@ func TestContext2Plan_state(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -2562,7 +2578,7 @@ func TestContext2Plan_taint(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -2609,7 +2625,7 @@ func TestContext2Apply_taintIgnoreChanges(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -2660,7 +2676,7 @@ func TestContext2Plan_taintDestroyInterpolatedCountRace(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -2702,13 +2718,17 @@ func TestContext2Plan_targeted(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
),
Targets: []string{"aws_instance.foo"},
Targets: []addrs.Targetable{
addrs.RootModuleInstance.Resource(
addrs.ManagedResourceMode, "aws_instance", "foo",
),
},
})
plan, err := ctx.Plan()
@ -2760,13 +2780,15 @@ func TestContext2Plan_targetedCrossModule(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
),
Targets: []string{"module.B"},
Targets: []addrs.Targetable{
addrs.RootModuleInstance.Child("B", addrs.NoKey),
},
})
plan, err := ctx.Plan()
@ -2801,13 +2823,15 @@ func TestContext2Plan_targetedModuleWithProvider(t *testing.T) {
p := testProvider("null")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"null": testProviderFuncFixed(p),
},
),
Targets: []string{"module.child2"},
Targets: []addrs.Targetable{
addrs.RootModuleInstance.Child("child2", addrs.NoKey),
},
})
plan, err := ctx.Plan()
@ -2836,7 +2860,7 @@ func TestContext2Plan_targetedOrphan(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -2864,7 +2888,11 @@ func TestContext2Plan_targetedOrphan(t *testing.T) {
},
},
Destroy: true,
Targets: []string{"aws_instance.orphan"},
Targets: []addrs.Targetable{
addrs.RootModuleInstance.Resource(
addrs.ManagedResourceMode, "aws_instance", "orphan",
),
},
})
plan, err := ctx.Plan()
@ -2895,7 +2923,7 @@ func TestContext2Plan_targetedModuleOrphan(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -2923,7 +2951,11 @@ func TestContext2Plan_targetedModuleOrphan(t *testing.T) {
},
},
Destroy: true,
Targets: []string{"module.child.aws_instance.orphan"},
Targets: []addrs.Targetable{
addrs.RootModuleInstance.Child("child", addrs.NoKey).Resource(
addrs.ManagedResourceMode, "aws_instance", "orphan",
),
},
})
plan, err := ctx.Plan()
@ -2955,13 +2987,18 @@ func TestContext2Plan_targetedModuleUntargetedVariable(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
),
Targets: []string{"aws_instance.blue", "module.blue_mod"},
Targets: []addrs.Targetable{
addrs.RootModuleInstance.Resource(
addrs.ManagedResourceMode, "aws_instance", "blue",
),
addrs.RootModuleInstance.Child("blue_mod", addrs.NoKey),
},
})
plan, err := ctx.Plan()
@ -2996,13 +3033,17 @@ func TestContext2Plan_outputContainsTargetedResource(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
),
Targets: []string{"module.mod.aws_instance.a"},
Targets: []addrs.Targetable{
addrs.RootModuleInstance.Child("mod", addrs.NoKey).Resource(
addrs.ManagedResourceMode, "aws_instance", "a",
),
},
})
_, err := ctx.Plan()
@ -3030,7 +3071,7 @@ func TestContext2Plan_targetedOverTen(t *testing.T) {
fmt.Sprintf("%s:\n ID = %s\n", key, id))
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -3044,7 +3085,11 @@ func TestContext2Plan_targetedOverTen(t *testing.T) {
},
},
},
Targets: []string{"aws_instance.foo[1]"},
Targets: []addrs.Targetable{
addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.IntKey(1),
),
},
})
plan, err := ctx.Plan()
@ -3105,14 +3150,17 @@ func TestContext2Plan_provider(t *testing.T) {
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
),
Variables: map[string]interface{}{
"foo": "bar",
Variables: InputValues{
"foo": &InputValue{
Value: cty.StringVal("bar"),
SourceType: ValueFromCaller,
},
},
})
@ -3129,7 +3177,7 @@ func TestContext2Plan_varListErr(t *testing.T) {
m := testModule(t, "plan-var-list-err")
p := testProvider("aws")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -3165,14 +3213,17 @@ func TestContext2Plan_ignoreChanges(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
),
Variables: map[string]interface{}{
"foo": "ami-1234abcd",
Variables: InputValues{
"foo": &InputValue{
Value: cty.StringVal("ami-1234abcd"),
SourceType: ValueFromCaller,
},
},
State: s,
})
@ -3217,15 +3268,21 @@ func TestContext2Plan_ignoreChangesWildcard(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
),
Variables: map[string]interface{}{
"foo": "ami-1234abcd",
"bar": "t2.small",
Variables: InputValues{
"foo": &InputValue{
Value: cty.StringVal("ami-1234abcd"),
SourceType: ValueFromCaller,
},
"bar": &InputValue{
Value: cty.StringVal("t2.small"),
SourceType: ValueFromCaller,
},
},
State: s,
})
@ -3274,7 +3331,7 @@ func TestContext2Plan_moduleMapLiteral(t *testing.T) {
return nil, nil
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -3305,7 +3362,7 @@ func TestContext2Plan_computedValueInMap(t *testing.T) {
return testDiffFn(info, state, c)
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -3334,7 +3391,7 @@ func TestContext2Plan_moduleVariableFromSplat(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -3363,7 +3420,7 @@ func TestContext2Plan_createBeforeDestroy_depends_datasource(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -3403,7 +3460,7 @@ func TestContext2Plan_listOrder(t *testing.T) {
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -3457,7 +3514,7 @@ func TestContext2Plan_ignoreChangesWithFlatmaps(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -3558,7 +3615,7 @@ func TestContext2Plan_resourceNestedCount(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -3638,7 +3695,7 @@ output "out" {
p := testProvider("aws")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -3683,7 +3740,7 @@ resource "aws_instance" "foo" {
p := testProvider("aws")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),

View File

@ -7,13 +7,15 @@ import (
"strings"
"sync"
"testing"
"github.com/hashicorp/terraform/addrs"
)
func TestContext2Refresh(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "refresh-basic")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -67,7 +69,7 @@ func TestContext2Refresh_dataComputedModuleVar(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "refresh-data-module-var")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -95,7 +97,7 @@ func TestContext2Refresh_targeted(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "refresh-targeted")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -114,7 +116,11 @@ func TestContext2Refresh_targeted(t *testing.T) {
},
},
},
Targets: []string{"aws_instance.me"},
Targets: []addrs.Targetable{
addrs.RootModuleInstance.Resource(
addrs.ManagedResourceMode, "aws_instance", "me",
),
},
})
refreshedResources := make([]string, 0, 2)
@ -138,7 +144,7 @@ func TestContext2Refresh_targetedCount(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "refresh-targeted-count")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -159,7 +165,11 @@ func TestContext2Refresh_targetedCount(t *testing.T) {
},
},
},
Targets: []string{"aws_instance.me"},
Targets: []addrs.Targetable{
addrs.RootModuleInstance.Resource(
addrs.ManagedResourceMode, "aws_instance", "me",
),
},
})
refreshedResources := make([]string, 0, 2)
@ -191,7 +201,7 @@ func TestContext2Refresh_targetedCountIndex(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "refresh-targeted-count")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -212,7 +222,11 @@ func TestContext2Refresh_targetedCountIndex(t *testing.T) {
},
},
},
Targets: []string{"aws_instance.me[0]"},
Targets: []addrs.Targetable{
addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "me", addrs.IntKey(0),
),
},
})
refreshedResources := make([]string, 0, 2)
@ -236,7 +250,7 @@ func TestContext2Refresh_moduleComputedVar(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "refresh-module-computed-var")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -255,7 +269,7 @@ func TestContext2Refresh_delete(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "refresh-basic")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -296,7 +310,7 @@ func TestContext2Refresh_ignoreUncreated(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "refresh-basic")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -324,7 +338,7 @@ func TestContext2Refresh_hook(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "refresh-basic")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
Hooks: []Hook{h},
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
@ -391,7 +405,7 @@ func TestContext2Refresh_modules(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -426,7 +440,7 @@ func TestContext2Refresh_moduleInputComputedOutput(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -444,7 +458,7 @@ func TestContext2Refresh_moduleVarModule(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -462,7 +476,7 @@ func TestContext2Refresh_noState(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "refresh-no-state")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -484,7 +498,7 @@ func TestContext2Refresh_output(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "refresh-output")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -538,7 +552,7 @@ func TestContext2Refresh_outputPartial(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "refresh-output-partial")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -596,7 +610,7 @@ func TestContext2Refresh_stateBasic(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -679,7 +693,7 @@ func TestContext2Refresh_dataState(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"null": testProviderFuncFixed(p),
@ -751,7 +765,7 @@ func TestContext2Refresh_dataStateRefData(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"null": testProviderFuncFixed(p),
@ -795,7 +809,7 @@ func TestContext2Refresh_tainted(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -835,8 +849,8 @@ func TestContext2Refresh_unknownProvider(t *testing.T) {
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
_, err := NewContext(&ContextOpts{
Module: m,
_, diags := NewContext(&ContextOpts{
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{},
),
@ -858,12 +872,12 @@ func TestContext2Refresh_unknownProvider(t *testing.T) {
},
})
if err == nil {
if !diags.HasErrors() {
t.Fatal("successfully created context; want error")
}
if !regexp.MustCompile(`provider ".+" is not available`).MatchString(err.Error()) {
t.Fatalf("wrong error: %s", err)
if !regexp.MustCompile(`provider ".+" is not available`).MatchString(diags.Err().Error()) {
t.Fatalf("wrong error: %s", diags.Err())
}
}
@ -871,7 +885,7 @@ func TestContext2Refresh_vars(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "refresh-vars")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -1007,7 +1021,7 @@ func TestContext2Refresh_orphanModule(t *testing.T) {
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -1034,7 +1048,7 @@ func TestContext2Validate(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "validate-good")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -1086,7 +1100,7 @@ func TestContext2Refresh_noDiffHookOnScaleOut(t *testing.T) {
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
Hooks: []Hook{h},
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
@ -1133,7 +1147,7 @@ func TestContext2Refresh_updateProviderInState(t *testing.T) {
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),

View File

@ -6,6 +6,8 @@ import (
"testing"
"time"
"github.com/hashicorp/terraform/configs"
"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform/flatmap"
tfversion "github.com/hashicorp/terraform/version"
@ -73,16 +75,19 @@ func TestNewContextRequiredVersion(t *testing.T) {
}
mod := testModule(t, name)
if tc.Value != "" {
mod.Config().Terraform.RequiredVersion = tc.Value
constraint, err := version.NewConstraint(tc.Value)
if err != nil {
t.Fatalf("can't parse %q as version constraint", tc.Value)
}
mod.Module.CoreVersionConstraints = append(mod.Module.CoreVersionConstraints, configs.VersionConstraint{
Required: constraint,
})
}
_, err := NewContext(&ContextOpts{
Module: mod,
_, diags := NewContext(&ContextOpts{
Config: mod,
})
if (err != nil) != tc.Err {
t.Fatalf("err: %s", err)
}
if err != nil {
return
if diags.HasErrors() != tc.Err {
t.Fatalf("err: %s", diags.Err())
}
})
}

View File

@ -4,13 +4,16 @@ import (
"fmt"
"strings"
"testing"
"github.com/hashicorp/terraform/addrs"
"github.com/zclconf/go-cty/cty"
)
func TestContext2Validate_badCount(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "validate-bad-count")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -28,7 +31,7 @@ func TestContext2Validate_badVar(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "validate-bad-var")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -46,14 +49,17 @@ func TestContext2Validate_varMapOverrideOld(t *testing.T) {
m := testModule(t, "validate-module-pc-vars")
p := testProvider("aws")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
),
Variables: map[string]interface{}{
"foo.foo": "bar",
Variables: InputValues{
"foo.foo": &InputValue{
Value: cty.StringVal("bar"),
SourceType: ValueFromCaller,
},
},
})
@ -66,7 +72,7 @@ func TestContext2Validate_varMapOverrideOld(t *testing.T) {
func TestContext2Validate_varNoDefaultExplicitType(t *testing.T) {
m := testModule(t, "validate-var-no-default-explicit-type")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
})
diags := c.Validate()
@ -79,7 +85,7 @@ func TestContext2Validate_computedVar(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "validate-computed-var")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -113,7 +119,7 @@ func TestContext2Validate_countComputed(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "validate-count-computed")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -131,7 +137,7 @@ func TestContext2Validate_countNegative(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "validate-count-negative")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -149,7 +155,7 @@ func TestContext2Validate_countVariable(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "apply-count-variable")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -167,7 +173,7 @@ func TestContext2Validate_countVariableNoDefault(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "validate-count-variable")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -187,7 +193,7 @@ func TestContext2Validate_cycle(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "validate-cycle")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -206,7 +212,7 @@ func TestContext2Validate_moduleBadOutput(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "validate-bad-module-output")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -224,7 +230,7 @@ func TestContext2Validate_moduleGood(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "validate-good-module")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -242,7 +248,7 @@ func TestContext2Validate_moduleBadResource(t *testing.T) {
m := testModule(t, "validate-module-bad-rc")
p := testProvider("aws")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -262,7 +268,7 @@ func TestContext2Validate_moduleDepsShouldNotCycle(t *testing.T) {
m := testModule(t, "validate-module-deps-cycle")
p := testProvider("aws")
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -280,7 +286,7 @@ func TestContext2Validate_moduleProviderInheritOrphan(t *testing.T) {
m := testModule(t, "validate-module-pc-inherit-orphan")
p := testProvider("aws")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -325,14 +331,17 @@ func TestContext2Validate_moduleProviderVar(t *testing.T) {
m := testModule(t, "validate-module-pc-vars")
p := testProvider("aws")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
),
Variables: map[string]interface{}{
"provider_var": "bar",
Variables: InputValues{
"provider_var": &InputValue{
Value: cty.StringVal("bar"),
SourceType: ValueFromCaller,
},
},
})
@ -350,7 +359,7 @@ func TestContext2Validate_moduleProviderInheritUnused(t *testing.T) {
m := testModule(t, "validate-module-pc-inherit-unused")
p := testProvider("aws")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -387,7 +396,7 @@ func TestContext2Validate_orphans(t *testing.T) {
},
}
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -411,7 +420,7 @@ func TestContext2Validate_providerConfig_bad(t *testing.T) {
m := testModule(t, "validate-bad-pc")
p := testProvider("aws")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -434,7 +443,7 @@ func TestContext2Validate_providerConfig_badEmpty(t *testing.T) {
m := testModule(t, "validate-bad-pc-empty")
p := testProvider("aws")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -454,7 +463,7 @@ func TestContext2Validate_providerConfig_good(t *testing.T) {
m := testModule(t, "validate-bad-pc")
p := testProvider("aws")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -473,7 +482,7 @@ func TestContext2Validate_provisionerConfig_bad(t *testing.T) {
p := testProvider("aws")
pr := testProvisioner()
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -503,7 +512,7 @@ func TestContext2Validate_provisionerConfig_good(t *testing.T) {
return nil, c.CheckSet([]string{"command"})
}
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -524,7 +533,7 @@ func TestContext2Validate_requiredVar(t *testing.T) {
m := testModule(t, "validate-required-var")
p := testProvider("aws")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -542,7 +551,7 @@ func TestContext2Validate_resourceConfig_bad(t *testing.T) {
m := testModule(t, "validate-bad-rc")
p := testProvider("aws")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -562,7 +571,7 @@ func TestContext2Validate_resourceConfig_good(t *testing.T) {
m := testModule(t, "validate-bad-rc")
p := testProvider("aws")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -580,7 +589,7 @@ func TestContext2Validate_resourceNameSymbol(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "validate-resource-name-symbol")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -598,7 +607,7 @@ func TestContext2Validate_selfRef(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "validate-self-ref")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -616,7 +625,7 @@ func TestContext2Validate_selfRefMulti(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "validate-self-ref-multi")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -634,7 +643,7 @@ func TestContext2Validate_selfRefMultiAll(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "validate-self-ref-multi-all")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -668,7 +677,7 @@ func TestContext2Validate_tainted(t *testing.T) {
},
}
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -695,7 +704,7 @@ func TestContext2Validate_targetedDestroy(t *testing.T) {
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -715,7 +724,11 @@ func TestContext2Validate_targetedDestroy(t *testing.T) {
},
},
},
Targets: []string{"aws_instance.foo"},
Targets: []addrs.Targetable{
addrs.RootModuleInstance.Resource(
addrs.ManagedResourceMode, "aws_instance", "foo",
),
},
Destroy: true,
})
@ -729,14 +742,17 @@ func TestContext2Validate_varRefFilled(t *testing.T) {
m := testModule(t, "validate-variable-ref")
p := testProvider("aws")
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
),
Variables: map[string]interface{}{
"foo": "bar",
Variables: InputValues{
"foo": &InputValue{
Value: cty.StringVal("bar"),
SourceType: ValueFromCaller,
},
},
})
@ -763,7 +779,7 @@ func TestContext2Validate_interpolateVar(t *testing.T) {
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"template": testProviderFuncFixed(p),
@ -789,7 +805,7 @@ func TestContext2Validate_interpolateComputedModuleVarDef(t *testing.T) {
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -814,7 +830,7 @@ func TestContext2Validate_interpolateMap(t *testing.T) {
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"template": testProviderFuncFixed(p),
@ -836,43 +852,58 @@ func TestContext2Validate_PlanGraphBuilder(t *testing.T) {
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
c := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
),
Variables: map[string]interface{}{
"foo": "us-west-2",
"test_list": []interface{}{"Hello", "World"},
"test_map": map[string]interface{}{
"Hello": "World",
"Foo": "Bar",
"Baz": "Foo",
Variables: InputValues{
"foo": &InputValue{
Value: cty.StringVal("us-west-2"),
SourceType: ValueFromCaller,
},
"amis": []map[string]interface{}{
map[string]interface{}{
"us-east-1": "override",
},
"test_list": &InputValue{
Value: cty.ListVal([]cty.Value{
cty.StringVal("Hello"),
cty.StringVal("World"),
}),
SourceType: ValueFromCaller,
},
"test_map": &InputValue{
Value: cty.MapVal(map[string]cty.Value{
"Hello": cty.StringVal("World"),
"Foo": cty.StringVal("Bar"),
"Baz": cty.StringVal("Foo"),
}),
SourceType: ValueFromCaller,
},
"amis": &InputValue{
Value: cty.ListVal([]cty.Value{
cty.MapVal(map[string]cty.Value{
"us-east-1": cty.StringVal("override"),
}),
}),
SourceType: ValueFromCaller,
},
},
})
graph, err := (&PlanGraphBuilder{
Module: c.module,
State: NewState(),
Providers: c.components.ResourceProviders(),
Targets: c.targets,
}).Build(RootModulePath)
if err != nil {
t.Fatalf("error attmepting to Build PlanGraphBuilder: %s", err)
graph, diags := (&PlanGraphBuilder{
Config: c.config,
State: NewState(),
Components: c.components,
Targets: c.targets,
}).Build(addrs.RootModuleInstance)
if diags.HasErrors() {
t.Fatalf("errors from PlanGraphBuilder: %s", diags.Err())
}
defer c.acquireRun("validate-test")()
walker, err := c.walk(graph, walkValidate)
if err != nil {
t.Fatal(err)
walker, diags := c.walk(graph, walkValidate)
if diags.HasErrors() {
t.Fatal(diags.Err())
}
if len(walker.ValidationErrors) > 0 {
t.Fatal(walker.ValidationErrors)
if len(walker.NonFatalDiagnostics) > 0 {
t.Fatal(walker.NonFatalDiagnostics.Err())
}
}

View File

@ -97,7 +97,7 @@ func TestDebug_plan(t *testing.T) {
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Config: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -105,9 +105,9 @@ func TestDebug_plan(t *testing.T) {
),
})
_, err = ctx.Plan()
if err != nil {
t.Fatalf("err: %s", err)
_, diags := ctx.Plan()
if diags.HasErrors() {
t.Fatalf("err: %s", diags.Err())
}
err = CloseDebugInfo()

View File

@ -5,6 +5,8 @@ import (
"reflect"
"strings"
"testing"
"github.com/hashicorp/terraform/addrs"
)
func TestDiffEmpty(t *testing.T) {
@ -18,7 +20,7 @@ func TestDiffEmpty(t *testing.T) {
t.Fatal("should be empty")
}
mod := diff.AddModule(rootModulePath)
mod := diff.AddModule(addrs.RootModuleInstance)
mod.Resources["nodeA"] = &InstanceDiff{
Attributes: map[string]*ResourceAttrDiff{
"foo": &ResourceAttrDiff{
@ -36,7 +38,7 @@ func TestDiffEmpty(t *testing.T) {
func TestDiffEmpty_taintedIsNotEmpty(t *testing.T) {
diff := new(Diff)
mod := diff.AddModule(rootModulePath)
mod := diff.AddModule(addrs.RootModuleInstance)
mod.Resources["nodeA"] = &InstanceDiff{
DestroyTainted: true,
}

View File

@ -5,32 +5,37 @@ import (
"sync"
"testing"
"github.com/hashicorp/terraform/addrs"
"github.com/zclconf/go-cty/cty"
"github.com/davecgh/go-spew/spew"
"github.com/hashicorp/terraform/config/configschema"
)
func TestBuiltinEvalContextProviderInput(t *testing.T) {
var lock sync.Mutex
cache := make(map[string]map[string]interface{})
cache := make(map[string]map[string]cty.Value)
ctx1 := testBuiltinEvalContext(t)
ctx1.PathValue = []string{"root"}
ctx1.PathValue = addrs.RootModuleInstance
ctx1.ProviderInputConfig = cache
ctx1.ProviderLock = &lock
ctx2 := testBuiltinEvalContext(t)
ctx2.PathValue = []string{"root", "child"}
ctx2.PathValue = addrs.RootModuleInstance.Child("child", addrs.NoKey)
ctx2.ProviderInputConfig = cache
ctx2.ProviderLock = &lock
expected1 := map[string]interface{}{"value": "foo"}
ctx1.SetProviderInput("foo", expected1)
providerAddr := addrs.ProviderConfig{Type: "foo"}
expected2 := map[string]interface{}{"value": "bar"}
ctx2.SetProviderInput("foo", expected2)
expected1 := map[string]cty.Value{"value": cty.StringVal("foo")}
ctx1.SetProviderInput(providerAddr, expected1)
actual1 := ctx1.ProviderInput("foo")
actual2 := ctx2.ProviderInput("foo")
expected2 := map[string]cty.Value{"value": cty.StringVal("bar")}
ctx2.SetProviderInput(providerAddr, expected2)
actual1 := ctx1.ProviderInput(providerAddr)
actual2 := ctx2.ProviderInput(providerAddr)
if !reflect.DeepEqual(actual1, expected1) {
t.Fatalf("bad: %#v %#v", actual1, expected1)
@ -76,11 +81,14 @@ func TestBuildingEvalContextInitProvider(t *testing.T) {
},
}
_, err := ctx.InitProvider("test", "test")
providerAddrDefault := addrs.ProviderConfig{Type: "test"}
providerAddrAlias := addrs.ProviderConfig{Type: "test", Alias: "foo"}
_, err := ctx.InitProvider("test", providerAddrDefault)
if err != nil {
t.Fatalf("error initializing provider test: %s", err)
}
_, err = ctx.InitProvider("test", "test.foo")
_, err = ctx.InitProvider("test", providerAddrAlias)
if err != nil {
t.Fatalf("error initializing provider test.foo: %s", err)
}
@ -98,14 +106,14 @@ func TestBuildingEvalContextInitProvider(t *testing.T) {
}
{
schema := ctx.ProviderSchema("test")
schema := ctx.ProviderSchema(providerAddrDefault.Absolute(addrs.RootModuleInstance))
if got, want := schema, testP.GetSchemaReturn; !reflect.DeepEqual(got, want) {
t.Errorf("wrong schema\ngot: %swant: %s", spew.Sdump(got), spew.Sdump(want))
}
}
{
schema := ctx.ProviderSchema("test.foo")
schema := ctx.ProviderSchema(providerAddrAlias.Absolute(addrs.RootModuleInstance))
if got, want := schema, testP.GetSchemaReturn; !reflect.DeepEqual(got, want) {
t.Errorf("wrong schema\ngot: %swant: %s", spew.Sdump(got), spew.Sdump(want))
}

View File

@ -5,7 +5,10 @@ import (
"reflect"
"testing"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/hcl2/hcl/hclsyntax"
"github.com/hashicorp/hcl2/hcl"
"github.com/hashicorp/terraform/configs"
)
func TestEvalFilterDiff(t *testing.T) {
@ -85,10 +88,19 @@ func TestProcessIgnoreChanges(t *testing.T) {
var instanceDiff *InstanceDiff
var testDiffs = func(ignoreChanges []string, newAttribute string) (*EvalDiff, *InstanceDiff) {
ignoreChangesTravs := make([]hcl.Traversal, len(ignoreChanges))
for i, s := range ignoreChanges {
traversal, travDiags := hclsyntax.ParseTraversalAbs([]byte(s), "", hcl.Pos{Line: 1, Column: 1})
if travDiags.HasErrors() {
t.Fatal(travDiags.Error())
}
ignoreChangesTravs[i] = traversal
}
return &EvalDiff{
Resource: &config.Resource{
Lifecycle: config.ResourceLifecycle{
IgnoreChanges: ignoreChanges,
Config: &configs.Resource{
Managed: &configs.ManagedResource{
IgnoreChanges: ignoreChangesTravs,
},
},
},

View File

@ -5,8 +5,14 @@ import (
"sync"
"testing"
"github.com/hashicorp/terraform/config/hcl2shim"
"github.com/hashicorp/hcl2/hcl"
"github.com/hashicorp/hcl2/hcl/hclsyntax"
"github.com/hashicorp/terraform/addrs"
"github.com/davecgh/go-spew/spew"
"github.com/hashicorp/terraform/config"
)
func TestEvalLocal_impl(t *testing.T) {
@ -33,27 +39,23 @@ func TestEvalLocal(t *testing.T) {
for _, test := range tests {
t.Run(test.Value, func(t *testing.T) {
rawConfig, err := config.NewRawConfig(map[string]interface{}{
"value": test.Value,
})
if err != nil {
t.Fatal(err)
expr, diags := hclsyntax.ParseTemplate([]byte(test.Value), "", hcl.Pos{Line: 1, Column: 1})
if diags.HasErrors() {
t.Fatal(diags.Error())
}
n := &EvalLocal{
Name: "foo",
Value: rawConfig,
Addr: addrs.LocalValue{Name: "foo"},
Expr: expr,
}
ctx := &MockEvalContext{
StateState: &State{},
StateLock: &sync.RWMutex{},
InterpolateConfigResult: testResourceConfig(t, map[string]interface{}{
"value": test.Want,
}),
EvaluateExprResult: hcl2shim.HCL2ValueFromConfigValue(test.Want),
}
_, err = n.Eval(ctx)
_, err := n.Eval(ctx)
if (err != nil) != test.Err {
if err != nil {
t.Errorf("unexpected error: %s", err)
@ -62,7 +64,7 @@ func TestEvalLocal(t *testing.T) {
}
}
ms := ctx.StateState.ModuleByPath([]string{})
ms := ctx.StateState.ModuleByPath(addrs.RootModuleInstance)
gotLocals := ms.Locals
wantLocals := map[string]interface{}{
"foo": test.Want,

View File

@ -3,6 +3,9 @@ package terraform
import (
"sync"
"testing"
"github.com/hashicorp/terraform/addrs"
"github.com/zclconf/go-cty/cty"
)
func TestEvalWriteMapOutput(t *testing.T) {
@ -12,40 +15,38 @@ func TestEvalWriteMapOutput(t *testing.T) {
cases := []struct {
name string
cfg *ResourceConfig
val cty.Value
err bool
}{
{
// Eval should recognize a single map in a slice, and collapse it
// into the map value
"single-map",
&ResourceConfig{
Config: map[string]interface{}{
"value": []map[string]interface{}{
map[string]interface{}{"a": "b"},
},
},
},
cty.MapVal(map[string]cty.Value{
"a": cty.StringVal("b"),
}),
false,
},
{
// we can't apply a multi-valued map to a variable, so this should error
"multi-map",
&ResourceConfig{
Config: map[string]interface{}{
"value": []map[string]interface{}{
map[string]interface{}{"a": "b"},
map[string]interface{}{"c": "d"},
},
},
},
cty.ListVal([]cty.Value{
cty.MapVal(map[string]cty.Value{
"a": cty.StringVal("b"),
}),
cty.MapVal(map[string]cty.Value{
"c": cty.StringVal("d"),
}),
}),
true,
},
}
for _, tc := range cases {
evalNode := &EvalWriteOutput{Name: tc.name}
ctx.InterpolateConfigResult = tc.cfg
evalNode := &EvalWriteOutput{
Addr: addrs.OutputValue{Name: tc.name},
}
ctx.EvaluateExprResult = tc.val
t.Run(tc.name, func(t *testing.T) {
_, err := evalNode.Eval(ctx)
if err != nil && !tc.err {

View File

@ -4,46 +4,41 @@ import (
"reflect"
"testing"
"github.com/hashicorp/terraform/configs"
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/config"
)
func TestEvalBuildProviderConfig_impl(t *testing.T) {
var _ EvalNode = new(EvalBuildProviderConfig)
}
func TestEvalBuildProviderConfig(t *testing.T) {
config := testResourceConfig(t, map[string]interface{}{
"set_in_config": "config",
"set_in_config_and_parent": "config",
"computed_in_config": "config",
func TestBuildProviderConfig(t *testing.T) {
configBody := configs.SynthBody("", map[string]cty.Value{
"set_in_config": cty.StringVal("config"),
"set_in_config_and_parent": cty.StringVal("config"),
"computed_in_config": cty.StringVal("config"),
})
provider := "foo"
n := &EvalBuildProviderConfig{
Provider: provider,
Config: &config,
Output: &config,
providerAddr := addrs.ProviderConfig{
Type: "foo",
}
ctx := &MockEvalContext{
ProviderInputConfig: map[string]interface{}{
"set_in_config": "input",
"set_by_input": "input",
ProviderInputValues: map[string]cty.Value{
"set_in_config": cty.StringVal("input"),
"set_by_input": cty.StringVal("input"),
},
}
if _, err := n.Eval(ctx); err != nil {
t.Fatalf("err: %s", err)
}
got := buildProviderConfig(ctx, providerAddr, configBody)
// We expect the provider config with the added input value
expected := map[string]interface{}{
"set_in_config": "config",
"set_in_config_and_parent": "config",
"computed_in_config": "config",
"set_by_input": "input",
want := map[string]cty.Value{
"set_in_config": cty.StringVal("config"),
"set_in_config_and_parent": cty.StringVal("config"),
"computed_in_config": cty.StringVal("config"),
"set_by_input": cty.StringVal("input"),
}
if !reflect.DeepEqual(config.Raw, expected) {
t.Fatalf("incorrect merged config:\n%#v\nwanted:\n%#v", config.Raw, expected)
if !reflect.DeepEqual(got, want) {
t.Fatalf("incorrect merged config\ngot: %#v\nwant: \n%#v", got, want)
}
}
@ -52,9 +47,11 @@ func TestEvalConfigProvider_impl(t *testing.T) {
}
func TestEvalConfigProvider(t *testing.T) {
config := testResourceConfig(t, map[string]interface{}{})
config := &configs.Provider{
Name: "foo",
}
provider := &MockResourceProvider{}
n := &EvalConfigProvider{Config: &config}
n := &EvalConfigProvider{Config: config}
ctx := &MockEvalContext{ProviderProvider: provider}
if _, err := n.Eval(ctx); err != nil {
@ -74,7 +71,9 @@ func TestEvalInitProvider_impl(t *testing.T) {
}
func TestEvalInitProvider(t *testing.T) {
n := &EvalInitProvider{Name: "foo"}
n := &EvalInitProvider{
Addr: addrs.ProviderConfig{Type: "foo"},
}
provider := &MockResourceProvider{}
ctx := &MockEvalContext{InitProviderProvider: provider}
if _, err := n.Eval(ctx); err != nil {
@ -84,14 +83,15 @@ func TestEvalInitProvider(t *testing.T) {
if !ctx.InitProviderCalled {
t.Fatal("should be called")
}
if ctx.InitProviderName != "foo" {
t.Fatalf("bad: %#v", ctx.InitProviderName)
if ctx.InitProviderAddr.String() != "provider.foo" {
t.Fatalf("wrong provider address %s", ctx.InitProviderAddr)
}
}
func TestEvalCloseProvider(t *testing.T) {
providerName := ResolveProviderName("foo", nil)
n := &EvalCloseProvider{Name: providerName}
n := &EvalCloseProvider{
Addr: addrs.ProviderConfig{Type: "foo"},
}
provider := &MockResourceProvider{}
ctx := &MockEvalContext{CloseProviderProvider: provider}
if _, err := n.Eval(ctx); err != nil {
@ -101,8 +101,8 @@ func TestEvalCloseProvider(t *testing.T) {
if !ctx.CloseProviderCalled {
t.Fatal("should be called")
}
if ctx.CloseProviderName != providerName {
t.Fatalf("bad: %#v", ctx.CloseProviderName)
if ctx.CloseProviderAddr.String() != "provider.foo" {
t.Fatalf("wrong provider address %s", ctx.CloseProviderAddr)
}
}
@ -112,7 +112,10 @@ func TestEvalGetProvider_impl(t *testing.T) {
func TestEvalGetProvider(t *testing.T) {
var actual ResourceProvider
n := &EvalGetProvider{Name: "foo", Output: &actual}
n := &EvalGetProvider{
Addr: addrs.RootModuleInstance.ProviderConfigDefault("foo"),
Output: &actual,
}
provider := &MockResourceProvider{}
ctx := &MockEvalContext{ProviderProvider: provider}
if _, err := n.Eval(ctx); err != nil {
@ -125,8 +128,8 @@ func TestEvalGetProvider(t *testing.T) {
if !ctx.ProviderCalled {
t.Fatal("should be called")
}
if ctx.ProviderName != "foo" {
t.Fatalf("bad: %#v", ctx.ProviderName)
if ctx.ProviderAddr.String() != "provider.foo" {
t.Fatalf("wrong provider address %s", ctx.ProviderAddr)
}
}
@ -150,19 +153,18 @@ func TestEvalInputProvider(t *testing.T) {
},
}
ctx := &MockEvalContext{ProviderProvider: provider}
rawConfig, err := config.NewRawConfig(map[string]interface{}{
"mock_config": "mock",
"set_in_config": "input",
})
if err != nil {
t.Fatalf("NewRawConfig failed: %s", err)
config := &configs.Provider{
Name: "foo",
Config: configs.SynthBody("synth", map[string]cty.Value{
"mock_config": cty.StringVal("mock"),
"set_in_config": cty.StringVal("input"),
}),
}
config := NewResourceConfig(rawConfig)
n := &EvalInputProvider{
Name: "mock",
Addr: addrs.ProviderConfig{Type: "foo"},
Provider: &provider,
Config: &config,
Config: config,
}
result, err := n.Eval(ctx)
@ -177,15 +179,15 @@ func TestEvalInputProvider(t *testing.T) {
t.Fatalf("ctx.SetProviderInput wasn't called")
}
if got, want := ctx.SetProviderInputName, "mock"; got != want {
if got, want := ctx.SetProviderInputAddr.String(), "provider.mock"; got != want {
t.Errorf("wrong provider name %q; want %q", got, want)
}
inputCfg := ctx.SetProviderInputConfig
inputCfg := ctx.SetProviderInputValues
// we should only have the value that was set during Input
want := map[string]interface{}{
"set_by_input": "input",
want := map[string]cty.Value{
"set_by_input": cty.StringVal("input"),
}
if !reflect.DeepEqual(inputCfg, want) {
t.Errorf("got incorrect input config:\n%#v\nwant:\n%#v", inputCfg, want)

View File

@ -3,6 +3,8 @@ package terraform
import (
"sync"
"testing"
"github.com/hashicorp/terraform/addrs"
)
func TestEvalRequireState(t *testing.T) {
@ -116,7 +118,7 @@ func TestEvalReadState(t *testing.T) {
},
}
ctx.StateLock = new(sync.RWMutex)
ctx.PathPath = rootModulePath
ctx.PathPath = addrs.RootModuleInstance
result, err := c.Node.Eval(ctx)
if err != nil {
@ -141,7 +143,7 @@ func TestEvalWriteState(t *testing.T) {
ctx := new(MockEvalContext)
ctx.StateState = state
ctx.StateLock = new(sync.RWMutex)
ctx.PathPath = rootModulePath
ctx.PathPath = addrs.RootModuleInstance
is := &InstanceState{ID: "i-abc123"}
node := &EvalWriteState{
@ -165,7 +167,7 @@ func TestEvalWriteStateDeposed(t *testing.T) {
ctx := new(MockEvalContext)
ctx.StateState = state
ctx.StateLock = new(sync.RWMutex)
ctx.PathPath = rootModulePath
ctx.PathPath = addrs.RootModuleInstance
is := &InstanceState{ID: "i-abc123"}
node := &EvalWriteStateDeposed{

View File

@ -4,94 +4,88 @@ import (
"fmt"
"testing"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/hcl2/hcl"
"github.com/hashicorp/hcl2/hcltest"
"github.com/hashicorp/terraform/addrs"
"github.com/zclconf/go-cty/cty"
)
func TestEvalValidateResourceSelfRef(t *testing.T) {
cases := []struct {
Name string
Addr string
Config map[string]interface{}
Err bool
func TestEvalValidateSelfRef(t *testing.T) {
rAddr := addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance",
Name: "foo",
}
tests := []struct {
Name string
Addr addrs.Referenceable
Expr hcl.Expression
Err bool
}{
{
"no interpolations",
"aws_instance.foo",
map[string]interface{}{
"foo": "bar",
},
"no references at all",
rAddr,
hcltest.MockExprLiteral(cty.StringVal("bar")),
false,
},
{
"non self reference",
"aws_instance.foo",
map[string]interface{}{
"foo": "${aws_instance.bar.id}",
},
rAddr,
hcltest.MockExprTraversalSrc("aws_instance.bar.id"),
false,
},
{
"self reference",
"aws_instance.foo",
map[string]interface{}{
"foo": "hello ${aws_instance.foo.id}",
},
rAddr,
hcltest.MockExprTraversalSrc("aws_instance.foo.id"),
true,
},
{
"self reference other index",
"aws_instance.foo",
map[string]interface{}{
"foo": "hello ${aws_instance.foo.4.id}",
},
rAddr,
hcltest.MockExprTraversalSrc("aws_instance.foo[4].id"),
false,
},
{
"self reference same index",
"aws_instance.foo[4]",
map[string]interface{}{
"foo": "hello ${aws_instance.foo.4.id}",
},
rAddr.Instance(addrs.IntKey(4)),
hcltest.MockExprTraversalSrc("aws_instance.foo[4].id"),
true,
},
{
"self reference multi",
"aws_instance.foo[4]",
map[string]interface{}{
"foo": "hello ${aws_instance.foo.*.id}",
},
true,
},
{
"self reference multi single",
"aws_instance.foo",
map[string]interface{}{
"foo": "hello ${aws_instance.foo.*.id}",
},
"self reference whole",
rAddr.Instance(addrs.IntKey(4)),
hcltest.MockExprTraversalSrc("aws_instance.foo"),
true,
},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) {
addr, err := ParseResourceAddress(tc.Addr)
if err != nil {
t.Fatalf("err: %s", err)
}
conf := config.TestRawConfig(t, tc.Config)
for i, test := range tests {
t.Run(fmt.Sprintf("%d-%s", i, test.Name), func(t *testing.T) {
body := hcltest.MockBody(&hcl.BodyContent{
Attributes: hcl.Attributes{
"foo": {
Name: "foo",
Expr: test.Expr,
},
},
})
n := &EvalValidateResourceSelfRef{Addr: &addr, Config: &conf}
n := &EvalValidateSelfRef{
Addr: test.Addr,
Config: body,
}
result, err := n.Eval(nil)
if result != nil {
t.Fatal("result should always be nil")
}
if (err != nil) != tc.Err {
if (err != nil) != test.Err {
t.Fatalf("err: %s", err)
}
})

View File

@ -5,7 +5,14 @@ import (
"strings"
"testing"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/hcl2/hcl"
"github.com/hashicorp/terraform/config/configschema"
"github.com/hashicorp/terraform/tfdiags"
"github.com/hashicorp/terraform/configs"
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/terraform/addrs"
)
func TestEvalValidateResource_managedResource(t *testing.T) {
@ -24,13 +31,24 @@ func TestEvalValidateResource_managedResource(t *testing.T) {
}
p := ResourceProvider(mp)
rc := testResourceConfig(t, map[string]interface{}{"foo": "bar"})
rc := &configs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance",
Name: "foo",
Config: configs.SynthBody("", map[string]cty.Value{
"foo": cty.StringVal("bar"),
}),
}
node := &EvalValidateResource{
Provider: &p,
Config: &rc,
ResourceName: "foo",
ResourceType: "aws_instance",
ResourceMode: config.ManagedResourceMode,
Addr: addrs.ResourceInstance{
Resource: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance",
Name: "foo",
},
},
Provider: &p,
Config: rc,
}
_, err := node.Eval(&MockEvalContext{})
@ -59,13 +77,25 @@ func TestEvalValidateResource_dataSource(t *testing.T) {
}
p := ResourceProvider(mp)
rc := testResourceConfig(t, map[string]interface{}{"foo": "bar"})
rc := &configs.Resource{
Mode: addrs.DataResourceMode,
Type: "aws_ami",
Name: "foo",
Config: configs.SynthBody("", map[string]cty.Value{
"foo": cty.StringVal("bar"),
}),
}
node := &EvalValidateResource{
Provider: &p,
Config: &rc,
ResourceName: "foo",
ResourceType: "aws_ami",
ResourceMode: config.DataResourceMode,
Addr: addrs.ResourceInstance{
Resource: addrs.Resource{
Mode: addrs.DataResourceMode,
Type: "aws_ami",
Name: "foo",
},
},
Provider: &p,
Config: rc,
}
_, err := node.Eval(&MockEvalContext{})
@ -85,13 +115,22 @@ func TestEvalValidateResource_validReturnsNilError(t *testing.T) {
}
p := ResourceProvider(mp)
rc := &ResourceConfig{}
rc := &configs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance",
Name: "foo",
Config: configs.SynthBody("", map[string]cty.Value{}),
}
node := &EvalValidateResource{
Provider: &p,
Config: &rc,
ResourceName: "foo",
ResourceType: "aws_instance",
ResourceMode: config.ManagedResourceMode,
Addr: addrs.ResourceInstance{
Resource: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance",
Name: "foo",
},
},
Provider: &p,
Config: rc,
}
_, err := node.Eval(&MockEvalContext{})
@ -109,13 +148,22 @@ func TestEvalValidateResource_warningsAndErrorsPassedThrough(t *testing.T) {
}
p := ResourceProvider(mp)
rc := &ResourceConfig{}
rc := &configs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance",
Name: "foo",
Config: configs.SynthBody("", map[string]cty.Value{}),
}
node := &EvalValidateResource{
Provider: &p,
Config: &rc,
ResourceName: "foo",
ResourceType: "aws_instance",
ResourceMode: config.ManagedResourceMode,
Addr: addrs.ResourceInstance{
Resource: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance",
Name: "foo",
},
},
Provider: &p,
Config: rc,
}
_, err := node.Eval(&MockEvalContext{})
@ -123,34 +171,17 @@ func TestEvalValidateResource_warningsAndErrorsPassedThrough(t *testing.T) {
t.Fatal("Expected an error, got none!")
}
verr := err.(*EvalValidateError)
if len(verr.Warnings) != 1 || verr.Warnings[0] != "warn" {
t.Fatalf("Expected 1 warning 'warn', got: %#v", verr.Warnings)
var diags tfdiags.Diagnostics
diags = diags.Append(err)
bySeverity := map[tfdiags.Severity]tfdiags.Diagnostics{}
for _, diag := range diags {
bySeverity[diag.Severity()] = append(bySeverity[diag.Severity()], diag)
}
if len(verr.Errors) != 1 || verr.Errors[0].Error() != "err" {
t.Fatalf("Expected 1 error 'err', got: %#v", verr.Errors)
if len(bySeverity[tfdiags.Warning]) != 1 || bySeverity[tfdiags.Warning][0].Description().Summary != "warn" {
t.Fatalf("Expected 1 warning 'warn', got: %#v", bySeverity[tfdiags.Warning])
}
}
func TestEvalValidateResource_checksResourceName(t *testing.T) {
mp := testProvider("aws")
p := ResourceProvider(mp)
rc := &ResourceConfig{}
node := &EvalValidateResource{
Provider: &p,
Config: &rc,
ResourceName: "bad*name",
ResourceType: "aws_instance",
ResourceMode: config.ManagedResourceMode,
}
_, err := node.Eval(&MockEvalContext{})
if err == nil {
t.Fatal("Expected an error, got none!")
}
expectErr := "resource name can only contain"
if !strings.Contains(err.Error(), expectErr) {
t.Fatalf("Expected err: %s to contain %s", err, expectErr)
if len(bySeverity[tfdiags.Error]) != 1 || bySeverity[tfdiags.Error][0].Description().Summary != "err" {
t.Fatalf("Expected 1 error 'err', got: %#v", bySeverity[tfdiags.Error])
}
}
@ -162,13 +193,22 @@ func TestEvalValidateResource_ignoreWarnings(t *testing.T) {
}
p := ResourceProvider(mp)
rc := &ResourceConfig{}
rc := &configs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance",
Name: "foo",
Config: configs.SynthBody("", map[string]cty.Value{}),
}
node := &EvalValidateResource{
Provider: &p,
Config: &rc,
ResourceName: "foo",
ResourceType: "aws_instance",
ResourceMode: config.ManagedResourceMode,
Addr: addrs.ResourceInstance{
Resource: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance",
Name: "foo",
},
},
Provider: &p,
Config: rc,
IgnoreWarnings: true,
}
@ -184,17 +224,26 @@ func TestEvalValidateProvisioner_valid(t *testing.T) {
var p ResourceProvisioner = mp
ctx := &MockEvalContext{}
cfg := &ResourceConfig{}
connInfo, err := config.NewRawConfig(map[string]interface{}{})
if err != nil {
t.Fatalf("failed to make connInfo: %s", err)
}
connConfig := NewResourceConfig(connInfo)
schema := &configschema.Block{}
node := &EvalValidateProvisioner{
ResourceAddr: addrs.ResourceInstance{
Resource: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "foo",
Name: "bar",
},
},
Provisioner: &p,
Config: &cfg,
ConnConfig: &connConfig,
Schema: &schema,
Config: &configs.Provisioner{
Type: "baz",
Config: hcl.EmptyBody(),
},
ConnConfig: &configs.Connection{
Type: "ssh",
Config: hcl.EmptyBody(),
},
}
result, err := node.Eval(ctx)
@ -206,10 +255,7 @@ func TestEvalValidateProvisioner_valid(t *testing.T) {
}
if !mp.ValidateCalled {
t.Fatalf("p.Config not called")
}
if mp.ValidateConfig != cfg {
t.Errorf("p.Config called with wrong config")
t.Fatalf("p.Validate not called")
}
}
@ -218,37 +264,43 @@ func TestEvalValidateProvisioner_warning(t *testing.T) {
var p ResourceProvisioner = mp
ctx := &MockEvalContext{}
cfg := &ResourceConfig{}
connInfo, err := config.NewRawConfig(map[string]interface{}{})
if err != nil {
t.Fatalf("failed to make connInfo: %s", err)
}
connConfig := NewResourceConfig(connInfo)
schema := &configschema.Block{}
node := &EvalValidateProvisioner{
ResourceAddr: addrs.ResourceInstance{
Resource: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "foo",
Name: "bar",
},
},
Provisioner: &p,
Config: &cfg,
ConnConfig: &connConfig,
Schema: &schema,
Config: &configs.Provisioner{
Type: "baz",
Config: hcl.EmptyBody(),
},
ConnConfig: &configs.Connection{
Type: "ssh",
Config: hcl.EmptyBody(),
},
}
mp.ValidateReturnWarns = []string{"foo is deprecated"}
_, err = node.Eval(ctx)
_, err := node.Eval(ctx)
if err == nil {
t.Fatalf("node.Eval succeeded; want error")
}
valErr, ok := err.(*EvalValidateError)
if !ok {
t.Fatalf("node.Eval error is %#v; want *EvalValidateError", valErr)
var diags tfdiags.Diagnostics
diags = diags.Append(err)
if len(diags) != 1 {
t.Fatalf("wrong number of diagsnostics in %#v; want one warning", diags)
}
warns := valErr.Warnings
if warns == nil || len(warns) != 1 {
t.Fatalf("wrong number of warnings in %#v; want one warning", warns)
}
if warns[0] != mp.ValidateReturnWarns[0] {
t.Fatalf("wrong warning %q; want %q", warns[0], mp.ValidateReturnWarns[0])
if got, want := diags[0].Description().Summary, mp.ValidateReturnWarns[0]; got != want {
t.Fatalf("wrong warning %q; want %q", got, want)
}
}
@ -256,39 +308,44 @@ func TestEvalValidateProvisioner_connectionInvalid(t *testing.T) {
var p ResourceProvisioner = &MockResourceProvisioner{}
ctx := &MockEvalContext{}
cfg := &ResourceConfig{}
connInfo, err := config.NewRawConfig(map[string]interface{}{
"bananananananana": "foo",
"bazaz": "bar",
})
if err != nil {
t.Fatalf("failed to make connInfo: %s", err)
}
connConfig := NewResourceConfig(connInfo)
schema := &configschema.Block{}
node := &EvalValidateProvisioner{
ResourceAddr: addrs.ResourceInstance{
Resource: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "foo",
Name: "bar",
},
},
Provisioner: &p,
Config: &cfg,
ConnConfig: &connConfig,
Schema: &schema,
Config: &configs.Provisioner{
Type: "baz",
Config: hcl.EmptyBody(),
},
ConnConfig: &configs.Connection{
Type: "ssh",
Config: configs.SynthBody("", map[string]cty.Value{
"bananananananana": cty.StringVal("foo"),
"bazaz": cty.StringVal("bar"),
}),
},
}
_, err = node.Eval(ctx)
_, err := node.Eval(ctx)
if err == nil {
t.Fatalf("node.Eval succeeded; want error")
}
valErr, ok := err.(*EvalValidateError)
if !ok {
t.Fatalf("node.Eval error is %#v; want *EvalValidateError", valErr)
var diags tfdiags.Diagnostics
diags = diags.Append(err)
if len(diags) != 2 {
t.Fatalf("wrong number of diagsnostics in %#v; want two errors", diags)
}
errs := valErr.Errors
if errs == nil || len(errs) != 2 {
t.Fatalf("wrong number of errors in %#v; want two errors", errs)
}
errStr := errs[0].Error()
if !(strings.Contains(errStr, "bananananananana") || strings.Contains(errStr, "bazaz")) {
t.Fatalf("wrong first error %q; want something about our invalid connInfo keys", errStr)
errStr := diags.Err().Error()
if !(strings.Contains(errStr, "bananananananana") && strings.Contains(errStr, "bazaz")) {
t.Fatalf("wrong errors %q; want something about each of our invalid connInfo keys", errStr)
}
}

View File

@ -1,219 +0,0 @@
package terraform
import (
"reflect"
"testing"
"github.com/hashicorp/terraform/config"
)
func TestCoerceMapVariable(t *testing.T) {
cases := map[string]struct {
Input *EvalCoerceMapVariable
ExpectVars map[string]interface{}
}{
"a valid map is untouched": {
Input: &EvalCoerceMapVariable{
Variables: map[string]interface{}{
"amap": map[string]interface{}{"foo": "bar"},
},
ModulePath: []string{"root"},
ModuleTree: testModuleInline(t, map[string]string{
"main.tf": `
variable "amap" {
type = "map"
}
`,
}),
},
ExpectVars: map[string]interface{}{
"amap": map[string]interface{}{"foo": "bar"},
},
},
"a list w/ a single map element is coerced": {
Input: &EvalCoerceMapVariable{
Variables: map[string]interface{}{
"amap": []interface{}{
map[string]interface{}{"foo": "bar"},
},
},
ModulePath: []string{"root"},
ModuleTree: testModuleInline(t, map[string]string{
"main.tf": `
variable "amap" {
type = "map"
}
`,
}),
},
ExpectVars: map[string]interface{}{
"amap": map[string]interface{}{"foo": "bar"},
},
},
"a list w/ more than one map element is untouched": {
Input: &EvalCoerceMapVariable{
Variables: map[string]interface{}{
"amap": []interface{}{
map[string]interface{}{"foo": "bar"},
map[string]interface{}{"baz": "qux"},
},
},
ModulePath: []string{"root"},
ModuleTree: testModuleInline(t, map[string]string{
"main.tf": `
variable "amap" {
type = "map"
}
`,
}),
},
ExpectVars: map[string]interface{}{
"amap": []interface{}{
map[string]interface{}{"foo": "bar"},
map[string]interface{}{"baz": "qux"},
},
},
},
"list coercion also works in a module": {
Input: &EvalCoerceMapVariable{
Variables: map[string]interface{}{
"amap": []interface{}{
map[string]interface{}{"foo": "bar"},
},
},
ModulePath: []string{"root", "middle", "bottom"},
ModuleTree: testModuleInline(t, map[string]string{
"top.tf": `
module "middle" {
source = "./middle"
}
`,
"middle/mid.tf": `
module "bottom" {
source = "./bottom"
amap {
foo = "bar"
}
}
`,
"middle/bottom/bot.tf": `
variable "amap" {
type = "map"
}
`,
}),
},
ExpectVars: map[string]interface{}{
"amap": map[string]interface{}{"foo": "bar"},
},
},
"coercion only occurs when target var is a map": {
Input: &EvalCoerceMapVariable{
Variables: map[string]interface{}{
"alist": []interface{}{
map[string]interface{}{"foo": "bar"},
},
},
ModulePath: []string{"root"},
ModuleTree: testModuleInline(t, map[string]string{
"main.tf": `
variable "alist" {
type = "list"
}
`,
}),
},
ExpectVars: map[string]interface{}{
"alist": []interface{}{
map[string]interface{}{"foo": "bar"},
},
},
},
}
for tn, tc := range cases {
_, err := tc.Input.Eval(&MockEvalContext{})
if err != nil {
t.Fatalf("%s: Unexpected err: %s", tn, err)
}
if !reflect.DeepEqual(tc.Input.Variables, tc.ExpectVars) {
t.Fatalf("%s: Expected variables:\n\n%#v\n\nGot:\n\n%#v",
tn, tc.ExpectVars, tc.Input.Variables)
}
}
}
func TestEvalVariableBlock(t *testing.T) {
rc, err := config.NewRawConfig(map[string]interface{}{
"known": "foo",
"known_list": []interface{}{"foo"},
"known_map": map[string]interface{}{
"foo": "foo",
},
"known_list_of_maps": []map[string]interface{}{
map[string]interface{}{
"foo": "foo",
},
},
"computed_map": map[string]interface{}{},
"computed_list_of_maps": []map[string]interface{}{
map[string]interface{}{},
},
// No computed_list right now, because that isn't currently supported:
// EvalVariableBlock assumes the final step of the path will always
// be a map.
})
if err != nil {
t.Fatalf("config.NewRawConfig failed: %s", err)
}
cfg := NewResourceConfig(rc)
cfg.ComputedKeys = []string{
"computed",
"computed_map.foo",
"computed_list_of_maps.0.foo",
}
n := &EvalVariableBlock{
VariableValues: map[string]interface{}{
// Should be cleared out on Eval
"should_be_deleted": true,
},
Config: &cfg,
}
ctx := &MockEvalContext{}
val, err := n.Eval(ctx)
if err != nil {
t.Fatalf("n.Eval failed: %s", err)
}
if val != nil {
t.Fatalf("n.Eval returned non-nil result: %#v", val)
}
got := n.VariableValues
want := map[string]interface{}{
"known": "foo",
"known_list": []interface{}{"foo"},
"known_map": map[string]interface{}{
"foo": "foo",
},
"known_list_of_maps": []interface{}{
map[string]interface{}{
"foo": "foo",
},
},
"computed": config.UnknownVariableValue,
"computed_map": map[string]interface{}{
"foo": config.UnknownVariableValue,
},
"computed_list_of_maps": []interface{}{
map[string]interface{}{
"foo": config.UnknownVariableValue,
},
},
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Incorrect variables\ngot: %#v\nwant: %#v", got, want)
}
}

View File

@ -16,7 +16,10 @@ func TestEvaluatorGetTerraformAttr(t *testing.T) {
Env: "foo",
},
}
scope := evaluator.Scope(addrs.RootModuleInstance, nil)
data := &evaluationStateData{
Evaluator: evaluator,
}
scope := evaluator.Scope(data, nil)
t.Run("workspace", func(t *testing.T) {
want := cty.StringVal("foo")

View File

@ -1,9 +1,11 @@
package terraform
import (
"reflect"
"fmt"
"strings"
"testing"
"github.com/hashicorp/terraform/addrs"
)
func TestApplyGraphBuilder_impl(t *testing.T) {
@ -65,20 +67,30 @@ func TestApplyGraphBuilder(t *testing.T) {
}
b := &ApplyGraphBuilder{
Module: testModule(t, "graph-builder-apply-basic"),
Diff: diff,
Providers: []string{"aws"},
Provisioners: []string{"exec"},
Config: testModule(t, "graph-builder-apply-basic"),
Diff: diff,
Components: &basicComponentFactory{
providers: map[string]ResourceProviderFactory{
"aws": func() (ResourceProvider, error) {
return nil, fmt.Errorf("not implemented")
},
},
provisioners: map[string]ResourceProvisionerFactory{
"exec": func() (ResourceProvisioner, error) {
return nil, fmt.Errorf("not implemented")
},
},
},
DisableReduce: true,
}
g, err := b.Build(RootModulePath)
g, err := b.Build(addrs.RootModuleInstance)
if err != nil {
t.Fatalf("err: %s", err)
}
if !reflect.DeepEqual(g.Path, RootModulePath) {
t.Fatalf("bad: %#v", g.Path)
if g.Path.String() != addrs.RootModuleInstance.String() {
t.Fatalf("wrong path %q", g.Path.String())
}
actual := strings.TrimSpace(g.String())
@ -119,21 +131,31 @@ func TestApplyGraphBuilder_depCbd(t *testing.T) {
}
b := &ApplyGraphBuilder{
Module: testModule(t, "graph-builder-apply-dep-cbd"),
Diff: diff,
Providers: []string{"aws"},
Provisioners: []string{"exec"},
Config: testModule(t, "graph-builder-apply-dep-cbd"),
Diff: diff,
Components: &basicComponentFactory{
providers: map[string]ResourceProviderFactory{
"aws": func() (ResourceProvider, error) {
return nil, fmt.Errorf("not implemented")
},
},
provisioners: map[string]ResourceProvisionerFactory{
"exec": func() (ResourceProvisioner, error) {
return nil, fmt.Errorf("not implemented")
},
},
},
DisableReduce: true,
}
g, err := b.Build(RootModulePath)
g, err := b.Build(addrs.RootModuleInstance)
if err != nil {
t.Fatalf("err: %s", err)
}
t.Logf("Graph: %s", g.String())
if !reflect.DeepEqual(g.Path, RootModulePath) {
t.Fatalf("bad: %#v", g.Path)
if g.Path.String() != addrs.RootModuleInstance.String() {
t.Fatalf("wrong path %q", g.Path.String())
}
// Create A, Modify B, Destroy A
@ -185,20 +207,30 @@ func TestApplyGraphBuilder_doubleCBD(t *testing.T) {
}
b := &ApplyGraphBuilder{
Module: testModule(t, "graph-builder-apply-double-cbd"),
Diff: diff,
Providers: []string{"aws"},
Provisioners: []string{"exec"},
Config: testModule(t, "graph-builder-apply-double-cbd"),
Diff: diff,
Components: &basicComponentFactory{
providers: map[string]ResourceProviderFactory{
"aws": func() (ResourceProvider, error) {
return nil, fmt.Errorf("not implemented")
},
},
provisioners: map[string]ResourceProvisionerFactory{
"exec": func() (ResourceProvisioner, error) {
return nil, fmt.Errorf("not implemented")
},
},
},
DisableReduce: true,
}
g, err := b.Build(RootModulePath)
g, err := b.Build(addrs.RootModuleInstance)
if err != nil {
t.Fatalf("err: %s", err)
}
if !reflect.DeepEqual(g.Path, RootModulePath) {
t.Fatalf("bad: %#v", g.Path)
if g.Path.String() != addrs.RootModuleInstance.String() {
t.Fatalf("wrong path %q", g.Path.String())
}
actual := strings.TrimSpace(g.String())
@ -255,21 +287,27 @@ func TestApplyGraphBuilder_destroyStateOnly(t *testing.T) {
}
b := &ApplyGraphBuilder{
Module: testModule(t, "empty"),
Diff: diff,
State: state,
Providers: []string{"aws"},
Config: testModule(t, "empty"),
Diff: diff,
State: state,
Components: &basicComponentFactory{
providers: map[string]ResourceProviderFactory{
"aws": func() (ResourceProvider, error) {
return nil, fmt.Errorf("not implemented")
},
},
},
DisableReduce: true,
}
g, err := b.Build(RootModulePath)
g, err := b.Build(addrs.RootModuleInstance)
if err != nil {
t.Fatalf("err: %s", err)
}
t.Logf("Graph: %s", g.String())
if !reflect.DeepEqual(g.Path, RootModulePath) {
t.Fatalf("bad: %#v", g.Path)
if g.Path.String() != addrs.RootModuleInstance.String() {
t.Fatalf("wrong path %q", g.Path.String())
}
testGraphHappensBefore(
@ -303,20 +341,30 @@ func TestApplyGraphBuilder_destroyCount(t *testing.T) {
}
b := &ApplyGraphBuilder{
Module: testModule(t, "graph-builder-apply-count"),
Diff: diff,
Providers: []string{"aws"},
Provisioners: []string{"exec"},
Config: testModule(t, "graph-builder-apply-count"),
Diff: diff,
Components: &basicComponentFactory{
providers: map[string]ResourceProviderFactory{
"aws": func() (ResourceProvider, error) {
return nil, fmt.Errorf("not implemented")
},
},
provisioners: map[string]ResourceProvisionerFactory{
"exec": func() (ResourceProvisioner, error) {
return nil, fmt.Errorf("not implemented")
},
},
},
DisableReduce: true,
}
g, err := b.Build(RootModulePath)
g, err := b.Build(addrs.RootModuleInstance)
if err != nil {
t.Fatalf("err: %s", err)
}
if !reflect.DeepEqual(g.Path, RootModulePath) {
t.Fatalf("bad: %#v", g.Path)
if g.Path.String() != addrs.RootModuleInstance.String() {
t.Fatalf("wrong module path %q", g.Path)
}
actual := strings.TrimSpace(g.String())
@ -350,12 +398,18 @@ func TestApplyGraphBuilder_moduleDestroy(t *testing.T) {
}
b := &ApplyGraphBuilder{
Module: testModule(t, "graph-builder-apply-module-destroy"),
Diff: diff,
Providers: []string{"null"},
Config: testModule(t, "graph-builder-apply-module-destroy"),
Diff: diff,
Components: &basicComponentFactory{
providers: map[string]ResourceProviderFactory{
"null": func() (ResourceProvider, error) {
return nil, fmt.Errorf("not implemented")
},
},
},
}
g, err := b.Build(RootModulePath)
g, err := b.Build(addrs.RootModuleInstance)
if err != nil {
t.Fatalf("err: %s", err)
}
@ -386,13 +440,23 @@ func TestApplyGraphBuilder_provisioner(t *testing.T) {
}
b := &ApplyGraphBuilder{
Module: testModule(t, "graph-builder-apply-provisioner"),
Diff: diff,
Providers: []string{"null"},
Provisioners: []string{"local"},
Config: testModule(t, "graph-builder-apply-provisioner"),
Diff: diff,
Components: &basicComponentFactory{
providers: map[string]ResourceProviderFactory{
"null": func() (ResourceProvider, error) {
return nil, fmt.Errorf("not implemented")
},
},
provisioners: map[string]ResourceProvisionerFactory{
"local": func() (ResourceProvisioner, error) {
return nil, fmt.Errorf("not implemented")
},
},
},
}
g, err := b.Build(RootModulePath)
g, err := b.Build(addrs.RootModuleInstance)
if err != nil {
t.Fatalf("err: %s", err)
}
@ -419,14 +483,24 @@ func TestApplyGraphBuilder_provisionerDestroy(t *testing.T) {
}
b := &ApplyGraphBuilder{
Destroy: true,
Module: testModule(t, "graph-builder-apply-provisioner"),
Diff: diff,
Providers: []string{"null"},
Provisioners: []string{"local"},
Destroy: true,
Config: testModule(t, "graph-builder-apply-provisioner"),
Diff: diff,
Components: &basicComponentFactory{
providers: map[string]ResourceProviderFactory{
"null": func() (ResourceProvider, error) {
return nil, fmt.Errorf("not implemented")
},
},
provisioners: map[string]ResourceProvisionerFactory{
"local": func() (ResourceProvisioner, error) {
return nil, fmt.Errorf("not implemented")
},
},
},
}
g, err := b.Build(RootModulePath)
g, err := b.Build(addrs.RootModuleInstance)
if err != nil {
t.Fatalf("err: %s", err)
}
@ -472,13 +546,21 @@ func TestApplyGraphBuilder_targetModule(t *testing.T) {
}
b := &ApplyGraphBuilder{
Module: testModule(t, "graph-builder-apply-target-module"),
Diff: diff,
Providers: []string{"null"},
Targets: []string{"module.child2"},
Config: testModule(t, "graph-builder-apply-target-module"),
Diff: diff,
Components: &basicComponentFactory{
providers: map[string]ResourceProviderFactory{
"null": func() (ResourceProvider, error) {
return nil, fmt.Errorf("not implemented")
},
},
},
Targets: []addrs.Targetable{
addrs.RootModuleInstance.Child("child2", addrs.NoKey),
},
}
g, err := b.Build(RootModulePath)
g, err := b.Build(addrs.RootModuleInstance)
if err != nil {
t.Fatalf("err: %s", err)
}

View File

@ -1,9 +1,11 @@
package terraform
import (
"reflect"
"fmt"
"strings"
"testing"
"github.com/hashicorp/terraform/addrs"
)
func TestPlanGraphBuilder_impl(t *testing.T) {
@ -12,18 +14,27 @@ func TestPlanGraphBuilder_impl(t *testing.T) {
func TestPlanGraphBuilder(t *testing.T) {
b := &PlanGraphBuilder{
Module: testModule(t, "graph-builder-plan-basic"),
Providers: []string{"aws", "openstack"},
Config: testModule(t, "graph-builder-plan-basic"),
Components: &basicComponentFactory{
providers: map[string]ResourceProviderFactory{
"aws": func() (ResourceProvider, error) {
return nil, fmt.Errorf("not implemented")
},
"openstack": func() (ResourceProvider, error) {
return nil, fmt.Errorf("not implemented")
},
},
},
DisableReduce: true,
}
g, err := b.Build(RootModulePath)
g, err := b.Build(addrs.RootModuleInstance)
if err != nil {
t.Fatalf("err: %s", err)
}
if !reflect.DeepEqual(g.Path, RootModulePath) {
t.Fatalf("bad: %#v", g.Path)
if g.Path.String() != addrs.RootModuleInstance.String() {
t.Fatalf("wrong module path %q", g.Path)
}
actual := strings.TrimSpace(g.String())
@ -35,12 +46,23 @@ func TestPlanGraphBuilder(t *testing.T) {
func TestPlanGraphBuilder_targetModule(t *testing.T) {
b := &PlanGraphBuilder{
Module: testModule(t, "graph-builder-plan-target-module-provider"),
Providers: []string{"null"},
Targets: []string{"module.child2"},
Config: testModule(t, "graph-builder-plan-target-module-provider"),
Components: &basicComponentFactory{
providers: map[string]ResourceProviderFactory{
"null": func() (ResourceProvider, error) {
return nil, fmt.Errorf("not implemented")
},
"openstack": func() (ResourceProvider, error) {
return nil, fmt.Errorf("not implemented")
},
},
},
Targets: []addrs.Targetable{
addrs.RootModuleInstance.Child("child2", addrs.NoKey),
},
}
g, err := b.Build(RootModulePath)
g, err := b.Build(addrs.RootModuleInstance)
if err != nil {
t.Fatalf("err: %s", err)
}

View File

@ -1,6 +1,11 @@
package terraform
import "testing"
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/addrs"
)
func TestRefreshGraphBuilder_configOrphans(t *testing.T) {
@ -65,11 +70,17 @@ func TestRefreshGraphBuilder_configOrphans(t *testing.T) {
}
b := &RefreshGraphBuilder{
Module: m,
State: state,
Providers: []string{"aws"},
Config: m,
State: state,
Components: &basicComponentFactory{
providers: map[string]ResourceProviderFactory{
"aws": func() (ResourceProvider, error) {
return nil, fmt.Errorf("not implemented")
},
},
},
}
g, err := b.Build(rootModulePath)
g, err := b.Build(addrs.RootModuleInstance)
if err != nil {
t.Fatalf("Error building graph: %s", err)
}

View File

@ -1,10 +1,11 @@
package terraform
import (
"reflect"
"strings"
"testing"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/dag"
)
@ -19,13 +20,13 @@ func TestBasicGraphBuilder(t *testing.T) {
},
}
g, err := b.Build(RootModulePath)
g, err := b.Build(addrs.RootModuleInstance)
if err != nil {
t.Fatalf("err: %s", err)
}
if !reflect.DeepEqual(g.Path, RootModulePath) {
t.Fatalf("bad: %#v", g.Path)
if g.Path.String() != addrs.RootModuleInstance.String() {
t.Fatalf("wrong module path %q", g.Path)
}
actual := strings.TrimSpace(g.String())
@ -44,7 +45,7 @@ func TestBasicGraphBuilder_validate(t *testing.T) {
Validate: true,
}
_, err := b.Build(RootModulePath)
_, err := b.Build(addrs.RootModuleInstance)
if err == nil {
t.Fatal("should error")
}
@ -59,7 +60,7 @@ func TestBasicGraphBuilder_validateOff(t *testing.T) {
Validate: false,
}
_, err := b.Build(RootModulePath)
_, err := b.Build(addrs.RootModuleInstance)
if err != nil {
t.Fatalf("expected no error, got: %s", err)
}

File diff suppressed because it is too large Load Diff

View File

@ -4,7 +4,7 @@ import (
"testing"
"github.com/davecgh/go-spew/spew"
"github.com/hashicorp/terraform/config/module"
"github.com/hashicorp/terraform/configs"
"github.com/hashicorp/terraform/moduledeps"
"github.com/hashicorp/terraform/plugin/discovery"
)
@ -243,12 +243,12 @@ func TestModuleTreeDependencies(t *testing.T) {
for name, test := range tests {
t.Run(name, func(t *testing.T) {
var root *module.Tree
var root *configs.Config
if test.ConfigDir != "" {
root = testModule(t, test.ConfigDir)
}
got := ModuleTreeDependencies(root, test.State)
got := ConfigTreeDependencies(root, test.State)
if !got.Equal(test.Want) {
t.Errorf(
"wrong dependency tree\ngot: %s\nwant: %s",

View File

@ -3,16 +3,13 @@ package terraform
import (
"sync"
"testing"
"github.com/hashicorp/terraform/addrs"
)
func TestNodeRefreshableDataResourceDynamicExpand_scaleOut(t *testing.T) {
var stateLock sync.RWMutex
addr, err := ParseResourceAddress("data.aws_instance.foo")
if err != nil {
t.Fatalf("bad: %s", err)
}
m := testModule(t, "refresh-data-scale-inout")
state := &State{
@ -42,16 +39,18 @@ func TestNodeRefreshableDataResourceDynamicExpand_scaleOut(t *testing.T) {
}
n := &NodeRefreshableDataResource{
NodeAbstractCountResource: &NodeAbstractCountResource{
NodeAbstractResource: &NodeAbstractResource{
Addr: addr,
Config: m.Config().Resources[0],
},
NodeAbstractResource: &NodeAbstractResource{
Addr: addrs.RootModuleInstance.Resource(
addrs.DataResourceMode,
"aws_instance",
"foo",
),
Config: m.Module.DataResources["data.aws_instance.foo"],
},
}
g, err := n.DynamicExpand(&MockEvalContext{
PathPath: []string{"root"},
PathPath: addrs.RootModuleInstance,
StateState: state,
StateLock: &stateLock,
})
@ -76,11 +75,6 @@ root - terraform.graphNodeRoot
func TestNodeRefreshableDataResourceDynamicExpand_scaleIn(t *testing.T) {
var stateLock sync.RWMutex
addr, err := ParseResourceAddress("data.aws_instance.foo")
if err != nil {
t.Fatalf("bad: %s", err)
}
m := testModule(t, "refresh-data-scale-inout")
state := &State{
@ -126,16 +120,18 @@ func TestNodeRefreshableDataResourceDynamicExpand_scaleIn(t *testing.T) {
}
n := &NodeRefreshableDataResource{
NodeAbstractCountResource: &NodeAbstractCountResource{
NodeAbstractResource: &NodeAbstractResource{
Addr: addr,
Config: m.Config().Resources[0],
},
NodeAbstractResource: &NodeAbstractResource{
Addr: addrs.RootModuleInstance.Resource(
addrs.DataResourceMode,
"aws_instance",
"foo",
),
Config: m.Module.DataResources["data.aws_instance.foo"],
},
}
g, err := n.DynamicExpand(&MockEvalContext{
PathPath: []string{"root"},
PathPath: addrs.RootModuleInstance,
StateState: state,
StateLock: &stateLock,
})

View File

@ -4,13 +4,19 @@ import (
"reflect"
"testing"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/hcl2/hcl"
"github.com/hashicorp/hcl2/hcl/hclsyntax"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/configs"
)
func TestNodeApplyableModuleVariablePath(t *testing.T) {
n := &NodeApplyableModuleVariable{
PathValue: []string{"root", "child"},
Config: &config.Variable{Name: "foo"},
Addr: addrs.RootModuleInstance.Child("child", addrs.NoKey).InputVariable("foo"),
Config: &configs.Variable{
Name: "foo",
},
}
expected := []string{"root"}
@ -22,27 +28,55 @@ func TestNodeApplyableModuleVariablePath(t *testing.T) {
func TestNodeApplyableModuleVariableReferenceableName(t *testing.T) {
n := &NodeApplyableModuleVariable{
PathValue: []string{"root", "child"},
Config: &config.Variable{Name: "foo"},
Addr: addrs.RootModuleInstance.Child("child", addrs.NoKey).InputVariable("foo"),
Config: &configs.Variable{
Name: "foo",
},
}
expected := []string{"module.child.var.foo"}
actual := n.ReferenceableName()
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("%#v != %#v", actual, expected)
{
expected := []addrs.Referenceable{
addrs.InputVariable{Name: "foo"},
}
actual := n.ReferenceableAddrs()
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("%#v != %#v", actual, expected)
}
}
{
gotSelfPath, gotReferencePath := n.ReferenceOutside()
wantSelfPath := addrs.RootModuleInstance.Child("child", addrs.NoKey)
wantReferencePath := addrs.RootModuleInstance
if got, want := gotSelfPath.String(), wantSelfPath.String(); got != want {
t.Errorf("wrong self path\ngot: %s\nwant: %s", got, want)
}
if got, want := gotReferencePath.String(), wantReferencePath.String(); got != want {
t.Errorf("wrong reference path\ngot: %s\nwant: %s", got, want)
}
}
}
func TestNodeApplyableModuleVariableReference(t *testing.T) {
n := &NodeApplyableModuleVariable{
PathValue: []string{"root", "child"},
Config: &config.Variable{Name: "foo"},
Value: config.TestRawConfig(t, map[string]interface{}{
"foo": `${var.foo}`,
}),
Addr: addrs.RootModuleInstance.Child("child", addrs.NoKey).InputVariable("foo"),
Config: &configs.Variable{
Name: "foo",
},
Expr: &hclsyntax.ScopeTraversalExpr{
Traversal: hcl.Traversal{
hcl.TraverseRoot{Name: "var"},
hcl.TraverseAttr{Name: "foo"},
},
},
}
expected := []string{"var.foo"}
expected := []*addrs.Reference{
{
Subject: addrs.InputVariable{Name: "foo"},
},
}
actual := n.References()
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("%#v != %#v", actual, expected)
@ -51,14 +85,26 @@ func TestNodeApplyableModuleVariableReference(t *testing.T) {
func TestNodeApplyableModuleVariableReference_grandchild(t *testing.T) {
n := &NodeApplyableModuleVariable{
PathValue: []string{"root", "child", "grandchild"},
Config: &config.Variable{Name: "foo"},
Value: config.TestRawConfig(t, map[string]interface{}{
"foo": `${var.foo}`,
}),
Addr: addrs.RootModuleInstance.
Child("child", addrs.NoKey).
Child("grandchild", addrs.NoKey).
InputVariable("foo"),
Config: &configs.Variable{
Name: "foo",
},
Expr: &hclsyntax.ScopeTraversalExpr{
Traversal: hcl.Traversal{
hcl.TraverseRoot{Name: "var"},
hcl.TraverseAttr{Name: "foo"},
},
},
}
expected := []string{"module.child.var.foo"}
expected := []*addrs.Reference{
{
Subject: addrs.InputVariable{Name: "foo"},
},
}
actual := n.References()
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("%#v != %#v", actual, expected)

View File

@ -4,6 +4,8 @@ import (
"strings"
"sync"
"testing"
"github.com/hashicorp/terraform/addrs"
)
func TestNodeDestroyResourceDynamicExpand_deposedCount(t *testing.T) {
@ -34,22 +36,22 @@ func TestNodeDestroyResourceDynamicExpand_deposedCount(t *testing.T) {
},
}
addr, err := parseResourceAddressInternal("aws_instance.bar.0")
if err != nil {
t.Fatalf("err: %s", err)
}
m := testModule(t, "apply-cbd-count")
n := &NodeDestroyResource{
NodeAbstractResource: &NodeAbstractResource{
Addr: addr,
n := &NodeDestroyResourceInstance{
NodeAbstractResourceInstance: &NodeAbstractResourceInstance{
NodeAbstractResource: NodeAbstractResource{
Addr: addrs.RootModuleInstance.Resource(
addrs.ManagedResourceMode, "aws_instance", "bar",
),
Config: m.Module.ManagedResources["aws_instance.bar"],
},
InstanceKey: addrs.IntKey(0),
ResourceState: state.Modules[0].Resources["aws_instance.bar.0"],
Config: m.Config().Resources[0],
},
}
g, err := n.DynamicExpand(&MockEvalContext{
PathPath: []string{"root"},
PathPath: addrs.RootModuleInstance,
StateState: state,
StateLock: &stateLock,
})

View File

@ -5,17 +5,14 @@ import (
"sync"
"testing"
"github.com/hashicorp/terraform/addrs"
"github.com/davecgh/go-spew/spew"
)
func TestNodeRefreshableManagedResourceDynamicExpand_scaleOut(t *testing.T) {
var stateLock sync.RWMutex
addr, err := ParseResourceAddress("aws_instance.foo")
if err != nil {
t.Fatalf("bad: %s", err)
}
m := testModule(t, "refresh-resource-scale-inout")
state := &State{
@ -45,16 +42,16 @@ func TestNodeRefreshableManagedResourceDynamicExpand_scaleOut(t *testing.T) {
}
n := &NodeRefreshableManagedResource{
NodeAbstractCountResource: &NodeAbstractCountResource{
NodeAbstractResource: &NodeAbstractResource{
Addr: addr,
Config: m.Config().Resources[0],
},
NodeAbstractResource: &NodeAbstractResource{
Addr: addrs.RootModuleInstance.Resource(
addrs.ManagedResourceMode, "aws_instance", "foo",
),
Config: m.Module.ManagedResources["aws_instance.foo"],
},
}
g, err := n.DynamicExpand(&MockEvalContext{
PathPath: []string{"root"},
PathPath: addrs.RootModuleInstance,
StateState: state,
StateLock: &stateLock,
})
@ -79,11 +76,6 @@ root - terraform.graphNodeRoot
func TestNodeRefreshableManagedResourceDynamicExpand_scaleIn(t *testing.T) {
var stateLock sync.RWMutex
addr, err := ParseResourceAddress("aws_instance.foo")
if err != nil {
t.Fatalf("bad: %s", err)
}
m := testModule(t, "refresh-resource-scale-inout")
state := &State{
@ -129,16 +121,16 @@ func TestNodeRefreshableManagedResourceDynamicExpand_scaleIn(t *testing.T) {
}
n := &NodeRefreshableManagedResource{
NodeAbstractCountResource: &NodeAbstractCountResource{
NodeAbstractResource: &NodeAbstractResource{
Addr: addr,
Config: m.Config().Resources[0],
},
NodeAbstractResource: &NodeAbstractResource{
Addr: addrs.RootModuleInstance.Resource(
addrs.ManagedResourceMode, "aws_instance", "foo",
),
Config: m.Module.ManagedResources["aws_instance.foo"],
},
}
g, err := n.DynamicExpand(&MockEvalContext{
PathPath: []string{"root"},
PathPath: addrs.RootModuleInstance,
StateState: state,
StateLock: &stateLock,
})
@ -162,17 +154,17 @@ root - terraform.graphNodeRoot
}
func TestNodeRefreshableManagedResourceEvalTree_scaleOut(t *testing.T) {
addr, err := ParseResourceAddress("aws_instance.foo[2]")
if err != nil {
t.Fatalf("bad: %s", err)
}
m := testModule(t, "refresh-resource-scale-inout")
n := &NodeRefreshableManagedResourceInstance{
NodeAbstractResource: &NodeAbstractResource{
Addr: addr,
Config: m.Config().Resources[0],
NodeAbstractResourceInstance: &NodeAbstractResourceInstance{
NodeAbstractResource: NodeAbstractResource{
Addr: addrs.RootModuleInstance.Resource(
addrs.ManagedResourceMode, "aws_instance", "foo",
),
Config: m.Module.ManagedResources["aws_instance.foo"],
},
InstanceKey: addrs.IntKey(2),
},
}

View File

@ -6,7 +6,10 @@ import (
"strings"
"testing"
"github.com/hashicorp/terraform/config/module"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/configs"
"github.com/zclconf/go-cty/cty"
)
func TestPlanContextOpts(t *testing.T) {
@ -18,12 +21,12 @@ func TestPlanContextOpts(t *testing.T) {
},
},
},
Module: module.NewTree("test", nil),
Config: configs.NewEmptyConfig(),
State: &State{
TFVersion: "sigil",
},
Vars: map[string]interface{}{"foo": "bar"},
Targets: []string{"baz"},
Vars: map[string]cty.Value{"foo": cty.StringVal("bar")},
Targets: []string{"baz.bar"},
TerraformVersion: VersionString(),
ProviderSHA256s: map[string][]byte{
@ -37,11 +40,18 @@ func TestPlanContextOpts(t *testing.T) {
}
want := &ContextOpts{
Diff: plan.Diff,
Module: plan.Module,
State: plan.State,
Variables: plan.Vars,
Targets: plan.Targets,
Diff: plan.Diff,
Config: plan.Config,
State: plan.State,
Variables: InputValues{
"foo": &InputValue{
Value: cty.StringVal("bar"),
SourceType: ValueFromPlan,
},
},
Targets: []addrs.Targetable{
addrs.RootModuleInstance.Resource(addrs.ManagedResourceMode, "baz", "bar"),
},
ProviderSHA256s: plan.ProviderSHA256s,
}
@ -52,7 +62,7 @@ func TestPlanContextOpts(t *testing.T) {
func TestReadWritePlan(t *testing.T) {
plan := &Plan{
Module: testModule(t, "new-good"),
Config: testModule(t, "new-good"),
Diff: &Diff{
Modules: []*ModuleDiff{
&ModuleDiff{
@ -97,8 +107,8 @@ func TestReadWritePlan(t *testing.T) {
},
},
},
Vars: map[string]interface{}{
"foo": "bar",
Vars: map[string]cty.Value{
"foo": cty.StringVal("bar"),
},
}
@ -128,13 +138,13 @@ func TestPlanContextOptsOverrideStateGood(t *testing.T) {
},
},
},
Module: module.NewTree("test", nil),
Config: configs.NewEmptyConfig(),
State: &State{
TFVersion: "sigil",
Serial: 1,
},
Vars: map[string]interface{}{"foo": "bar"},
Targets: []string{"baz"},
Vars: map[string]cty.Value{"foo": cty.StringVal("bar")},
Targets: []string{"baz.bar"},
TerraformVersion: VersionString(),
ProviderSHA256s: map[string][]byte{
@ -155,11 +165,18 @@ func TestPlanContextOptsOverrideStateGood(t *testing.T) {
}
want := &ContextOpts{
Diff: plan.Diff,
Module: plan.Module,
State: base.State,
Variables: plan.Vars,
Targets: plan.Targets,
Diff: plan.Diff,
Config: plan.Config,
State: base.State,
Variables: InputValues{
"foo": &InputValue{
Value: cty.StringVal("bar"),
SourceType: ValueFromPlan,
},
},
Targets: []addrs.Targetable{
addrs.RootModuleInstance.Resource(addrs.ManagedResourceMode, "baz", "bar"),
},
ProviderSHA256s: plan.ProviderSHA256s,
}

View File

@ -229,90 +229,6 @@ func (s *State) moduleByPath(path addrs.ModuleInstance) *ModuleState {
return nil
}
// ModuleOrphans returns all the module orphans in this state by
// returning their full paths. These paths can be used with ModuleByPath
// to return the actual state.
func (s *State) ModuleOrphans(path []string, c *config.Config) [][]string {
s.Lock()
defer s.Unlock()
return s.moduleOrphans(path, c)
}
func (s *State) moduleOrphans(path []string, c *config.Config) [][]string {
// direct keeps track of what direct children we have both in our config
// and in our state. childrenKeys keeps track of what isn't an orphan.
direct := make(map[string]struct{})
childrenKeys := make(map[string]struct{})
if c != nil {
for _, m := range c.Modules {
childrenKeys[m.Name] = struct{}{}
direct[m.Name] = struct{}{}
}
}
// Go over the direct children and find any that aren't in our keys.
var orphans [][]string
for _, m := range s.children(path) {
key := m.Path[len(m.Path)-1]
// Record that we found this key as a direct child. We use this
// later to find orphan nested modules.
direct[key] = struct{}{}
// If we have a direct child still in our config, it is not an orphan
if _, ok := childrenKeys[key]; ok {
continue
}
orphans = append(orphans, m.Path)
}
// Find the orphans that are nested...
for _, m := range s.Modules {
if m == nil {
continue
}
// We only want modules that are at least grandchildren
if len(m.Path) < len(path)+2 {
continue
}
// If it isn't part of our tree, continue
if !reflect.DeepEqual(path, m.Path[:len(path)]) {
continue
}
// If we have the direct child, then just skip it.
key := m.Path[len(path)]
if _, ok := direct[key]; ok {
continue
}
orphanPath := m.Path[:len(path)+1]
// Don't double-add if we've already added this orphan (which can happen if
// there are multiple nested sub-modules that get orphaned together).
alreadyAdded := false
for _, o := range orphans {
if reflect.DeepEqual(o, orphanPath) {
alreadyAdded = true
break
}
}
if alreadyAdded {
continue
}
// Add this orphan
orphans = append(orphans, orphanPath)
}
return orphans
}
// Empty returns true if the state is empty.
func (s *State) Empty() bool {
if s == nil {

View File

@ -10,6 +10,8 @@ import (
"strings"
"testing"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/config"
)
@ -51,49 +53,49 @@ func TestStateValidate(t *testing.T) {
func TestStateAddModule(t *testing.T) {
cases := []struct {
In [][]string
Out [][]string
In []addrs.ModuleInstance
Out []addrs.ModuleInstance
}{
{
[][]string{
[]string{"root"},
[]string{"root", "child"},
[]addrs.ModuleInstance{
addrs.RootModuleInstance,
addrs.RootModuleInstance.Child("child", addrs.NoKey),
},
[][]string{
[]string{"root"},
[]string{"root", "child"},
[]addrs.ModuleInstance{
addrs.RootModuleInstance,
addrs.RootModuleInstance.Child("child", addrs.NoKey),
},
},
{
[][]string{
[]string{"root", "foo", "bar"},
[]string{"root", "foo"},
[]string{"root"},
[]string{"root", "bar"},
[]addrs.ModuleInstance{
addrs.RootModuleInstance.Child("foo", addrs.NoKey).Child("bar", addrs.NoKey),
addrs.RootModuleInstance.Child("foo", addrs.NoKey),
addrs.RootModuleInstance,
addrs.RootModuleInstance.Child("bar", addrs.NoKey),
},
[][]string{
[]string{"root"},
[]string{"root", "bar"},
[]string{"root", "foo"},
[]string{"root", "foo", "bar"},
[]addrs.ModuleInstance{
addrs.RootModuleInstance,
addrs.RootModuleInstance.Child("bar", addrs.NoKey),
addrs.RootModuleInstance.Child("foo", addrs.NoKey),
addrs.RootModuleInstance.Child("foo", addrs.NoKey).Child("bar", addrs.NoKey),
},
},
// Same last element, different middle element
{
[][]string{
[]string{"root", "foo", "bar"}, // This one should sort after...
[]string{"root", "foo"},
[]string{"root"},
[]string{"root", "bar", "bar"}, // ...this one.
[]string{"root", "bar"},
[]addrs.ModuleInstance{
addrs.RootModuleInstance.Child("foo", addrs.NoKey).Child("bar", addrs.NoKey), // This one should sort after...
addrs.RootModuleInstance.Child("foo", addrs.NoKey),
addrs.RootModuleInstance,
addrs.RootModuleInstance.Child("bar", addrs.NoKey).Child("bar", addrs.NoKey), // ...this one.
addrs.RootModuleInstance.Child("bar", addrs.NoKey),
},
[][]string{
[]string{"root"},
[]string{"root", "bar"},
[]string{"root", "foo"},
[]string{"root", "bar", "bar"},
[]string{"root", "foo", "bar"},
[]addrs.ModuleInstance{
addrs.RootModuleInstance,
addrs.RootModuleInstance.Child("bar", addrs.NoKey),
addrs.RootModuleInstance.Child("foo", addrs.NoKey),
addrs.RootModuleInstance.Child("bar", addrs.NoKey).Child("bar", addrs.NoKey),
addrs.RootModuleInstance.Child("foo", addrs.NoKey).Child("bar", addrs.NoKey),
},
},
}
@ -119,7 +121,7 @@ func TestStateOutputTypeRoundTrip(t *testing.T) {
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: RootModulePath,
Path: []string{"root"},
Outputs: map[string]*OutputState{
"string_output": &OutputState{
Value: "String Value",
@ -147,113 +149,6 @@ func TestStateOutputTypeRoundTrip(t *testing.T) {
}
}
func TestStateModuleOrphans(t *testing.T) {
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: RootModulePath,
},
&ModuleState{
Path: []string{RootModuleName, "foo"},
},
&ModuleState{
Path: []string{RootModuleName, "bar"},
},
},
}
state.init()
config := testModule(t, "state-module-orphans").Config()
actual := state.ModuleOrphans(RootModulePath, config)
expected := [][]string{
[]string{RootModuleName, "foo"},
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: %#v", actual)
}
}
func TestStateModuleOrphans_nested(t *testing.T) {
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: RootModulePath,
},
&ModuleState{
Path: []string{RootModuleName, "foo", "bar"},
},
},
}
state.init()
actual := state.ModuleOrphans(RootModulePath, nil)
expected := [][]string{
[]string{RootModuleName, "foo"},
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: %#v", actual)
}
}
func TestStateModuleOrphans_nilConfig(t *testing.T) {
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: RootModulePath,
},
&ModuleState{
Path: []string{RootModuleName, "foo"},
},
&ModuleState{
Path: []string{RootModuleName, "bar"},
},
},
}
state.init()
actual := state.ModuleOrphans(RootModulePath, nil)
expected := [][]string{
[]string{RootModuleName, "foo"},
[]string{RootModuleName, "bar"},
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: %#v", actual)
}
}
func TestStateModuleOrphans_deepNestedNilConfig(t *testing.T) {
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: RootModulePath,
},
&ModuleState{
Path: []string{RootModuleName, "parent", "childfoo"},
},
&ModuleState{
Path: []string{RootModuleName, "parent", "childbar"},
},
},
}
state.init()
actual := state.ModuleOrphans(RootModulePath, nil)
expected := [][]string{
[]string{RootModuleName, "parent"},
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: %#v", actual)
}
}
func TestStateDeepCopy(t *testing.T) {
cases := []struct {
State *State
@ -360,7 +255,7 @@ func TestStateEqual(t *testing.T) {
&State{
Modules: []*ModuleState{
&ModuleState{
Path: RootModulePath,
Path: []string{"root"},
},
},
},
@ -373,14 +268,14 @@ func TestStateEqual(t *testing.T) {
&State{
Modules: []*ModuleState{
&ModuleState{
Path: RootModulePath,
Path: []string{"root"},
},
},
},
&State{
Modules: []*ModuleState{
&ModuleState{
Path: RootModulePath,
Path: []string{"root"},
},
},
},
@ -1864,28 +1759,6 @@ func TestParseResourceStateKey(t *testing.T) {
}
}
func TestStateModuleOrphans_empty(t *testing.T) {
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: RootModulePath,
},
&ModuleState{
Path: []string{RootModuleName, "foo", "bar"},
},
&ModuleState{
Path: []string{},
},
nil,
},
}
state.init()
// just calling this to check for panic
state.ModuleOrphans(RootModulePath, nil)
}
func TestReadState_prune(t *testing.T) {
state := &State{
Modules: []*ModuleState{

View File

@ -16,7 +16,6 @@ import (
"github.com/hashicorp/terraform/configs"
"github.com/hashicorp/terraform/config/module"
"github.com/hashicorp/terraform/helper/experiment"
"github.com/hashicorp/terraform/helper/logging"
)
@ -84,7 +83,6 @@ func testModule(t *testing.T, name string) *configs.Config {
t.Helper()
dir := filepath.Join(fixtureDir, name)
p := configs.NewParser(nil)
// FIXME: We're not dealing with the cleanup function here because
// this testModule function is used all over and so we don't want to
@ -107,7 +105,7 @@ func testModule(t *testing.T, name string) *configs.Config {
// testModuleInline takes a map of path -> config strings and yields a config
// structure with those files loaded from disk
func testModuleInline(t *testing.T, config map[string]string) *module.Tree {
func testModuleInline(t *testing.T, sources map[string]string) *configs.Config {
t.Helper()
cfgPath, err := ioutil.TempDir("", "tf-test")
@ -116,7 +114,7 @@ func testModuleInline(t *testing.T, config map[string]string) *module.Tree {
}
defer os.RemoveAll(cfgPath)
for path, configStr := range config {
for path, configStr := range sources {
dir := filepath.Dir(path)
if dir != "." {
err := os.MkdirAll(filepath.Join(cfgPath, dir), os.FileMode(0777))
@ -137,23 +135,23 @@ func testModuleInline(t *testing.T, config map[string]string) *module.Tree {
}
}
// Parse the configuration
mod, err := module.NewTreeModule("", cfgPath)
if err != nil {
t.Fatalf("Error loading configuration: %s", err)
// FIXME: We're not dealing with the cleanup function here because
// this testModule function is used all over and so we don't want to
// change its interface at this late stage.
loader, _ := configload.NewLoaderForTests(t)
// Test modules usually do not refer to remote sources, and for local
// sources only this ultimately just records all of the module paths
// in a JSON file so that we can load them below.
diags := loader.InstallModules(cfgPath, true, configload.InstallHooksImpl{})
t.Fatal(diags.Error())
config, diags := loader.LoadConfig(cfgPath)
if diags.HasErrors() {
t.Fatal(diags.Error())
}
// Load the modules
modStorage := &module.Storage{
StorageDir: filepath.Join(cfgPath, ".tfmodules"),
Mode: module.GetModeGet,
}
err = mod.Load(modStorage)
if err != nil {
t.Errorf("Error downloading modules: %s", err)
}
return mod
return config
}
func testStringMatch(t *testing.T, s fmt.Stringer, expected string) {

View File

@ -3,10 +3,12 @@ package terraform
import (
"strings"
"testing"
"github.com/hashicorp/terraform/addrs"
)
func TestFlatConfigTransformer_nilModule(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
tf := &FlatConfigTransformer{}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
@ -18,9 +20,9 @@ func TestFlatConfigTransformer_nilModule(t *testing.T) {
}
func TestFlatConfigTransformer(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
tf := &FlatConfigTransformer{
Module: testModule(t, "transform-flat-config-basic"),
Config: testModule(t, "transform-flat-config-basic"),
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)

View File

@ -1,16 +1,14 @@
package terraform
import (
"path/filepath"
"strings"
"testing"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/config/module"
"github.com/hashicorp/terraform/addrs"
)
func TestConfigTransformer_nilModule(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
tf := &ConfigTransformer{}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
@ -21,23 +19,9 @@ func TestConfigTransformer_nilModule(t *testing.T) {
}
}
func TestConfigTransformer_unloadedModule(t *testing.T) {
mod, err := module.NewTreeModule(
"", filepath.Join(fixtureDir, "graph-basic"))
if err != nil {
t.Fatalf("err: %s", err)
}
g := Graph{Path: RootModulePath}
tf := &ConfigTransformer{Module: mod}
if err := tf.Transform(&g); err == nil {
t.Fatal("should error")
}
}
func TestConfigTransformer(t *testing.T) {
g := Graph{Path: RootModulePath}
tf := &ConfigTransformer{Module: testModule(t, "graph-basic")}
g := Graph{Path: addrs.RootModuleInstance}
tf := &ConfigTransformer{Config: testModule(t, "graph-basic")}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
@ -50,11 +34,11 @@ func TestConfigTransformer(t *testing.T) {
}
func TestConfigTransformer_mode(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
tf := &ConfigTransformer{
Module: testModule(t, "transform-config-mode-data"),
Config: testModule(t, "transform-config-mode-data"),
ModeFilter: true,
Mode: config.DataResourceMode,
Mode: addrs.DataResourceMode,
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
@ -70,14 +54,13 @@ data.aws_ami.foo
}
func TestConfigTransformer_nonUnique(t *testing.T) {
addr, err := ParseResourceAddress("aws_instance.web")
if err != nil {
t.Fatalf("bad: %s", err)
}
g := Graph{Path: RootModulePath}
g.Add(&NodeAbstractResource{Addr: addr})
tf := &ConfigTransformer{Module: testModule(t, "graph-basic")}
g := Graph{Path: addrs.RootModuleInstance}
g.Add(NewNodeAbstractResource(
addrs.RootModuleInstance.Resource(
addrs.ManagedResourceMode, "aws_instance", "web",
),
))
tf := &ConfigTransformer{Config: testModule(t, "graph-basic")}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
@ -96,15 +79,14 @@ openstack_floating_ip.random
}
func TestConfigTransformer_unique(t *testing.T) {
addr, err := ParseResourceAddress("aws_instance.web")
if err != nil {
t.Fatalf("bad: %s", err)
}
g := Graph{Path: RootModulePath}
g.Add(&NodeAbstractResource{Addr: addr})
g := Graph{Path: addrs.RootModuleInstance}
g.Add(NewNodeAbstractResource(
addrs.RootModuleInstance.Resource(
addrs.ManagedResourceMode, "aws_instance", "web",
),
))
tf := &ConfigTransformer{
Module: testModule(t, "graph-basic"),
Config: testModule(t, "graph-basic"),
Unique: true,
}
if err := tf.Transform(&g); err != nil {

View File

@ -3,10 +3,12 @@ package terraform
import (
"strings"
"testing"
"github.com/hashicorp/terraform/addrs"
)
func TestCBDEdgeTransformer(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
g.Add(&graphNodeCreatorTest{AddrString: "test.A"})
g.Add(&graphNodeCreatorTest{AddrString: "test.B"})
g.Add(&graphNodeDestroyerTest{AddrString: "test.A", CBD: true})
@ -15,7 +17,7 @@ func TestCBDEdgeTransformer(t *testing.T) {
{
tf := &DestroyEdgeTransformer{
Module: module,
Config: module,
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
@ -23,7 +25,7 @@ func TestCBDEdgeTransformer(t *testing.T) {
}
{
tf := &CBDEdgeTransformer{Module: module}
tf := &CBDEdgeTransformer{Config: module}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
@ -37,7 +39,7 @@ func TestCBDEdgeTransformer(t *testing.T) {
}
func TestCBDEdgeTransformer_depNonCBD(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
g.Add(&graphNodeCreatorTest{AddrString: "test.A"})
g.Add(&graphNodeCreatorTest{AddrString: "test.B"})
g.Add(&graphNodeDestroyerTest{AddrString: "test.A"})
@ -47,7 +49,7 @@ func TestCBDEdgeTransformer_depNonCBD(t *testing.T) {
{
tf := &DestroyEdgeTransformer{
Module: module,
Config: module,
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
@ -55,7 +57,7 @@ func TestCBDEdgeTransformer_depNonCBD(t *testing.T) {
}
{
tf := &CBDEdgeTransformer{Module: module}
tf := &CBDEdgeTransformer{Config: module}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
@ -69,7 +71,7 @@ func TestCBDEdgeTransformer_depNonCBD(t *testing.T) {
}
func TestCBDEdgeTransformer_depNonCBDCount(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
g.Add(&graphNodeCreatorTest{AddrString: "test.A"})
g.Add(&graphNodeCreatorTest{AddrString: "test.B[0]"})
g.Add(&graphNodeCreatorTest{AddrString: "test.B[1]"})
@ -79,7 +81,7 @@ func TestCBDEdgeTransformer_depNonCBDCount(t *testing.T) {
{
tf := &DestroyEdgeTransformer{
Module: module,
Config: module,
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
@ -87,7 +89,7 @@ func TestCBDEdgeTransformer_depNonCBDCount(t *testing.T) {
}
{
tf := &CBDEdgeTransformer{Module: module}
tf := &CBDEdgeTransformer{Config: module}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
@ -109,7 +111,7 @@ test.B[1]
}
func TestCBDEdgeTransformer_depNonCBDCountBoth(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
g.Add(&graphNodeCreatorTest{AddrString: "test.A[0]"})
g.Add(&graphNodeCreatorTest{AddrString: "test.A[1]"})
g.Add(&graphNodeCreatorTest{AddrString: "test.B[0]"})
@ -121,7 +123,7 @@ func TestCBDEdgeTransformer_depNonCBDCountBoth(t *testing.T) {
{
tf := &DestroyEdgeTransformer{
Module: module,
Config: module,
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
@ -129,7 +131,7 @@ func TestCBDEdgeTransformer_depNonCBDCountBoth(t *testing.T) {
}
{
tf := &CBDEdgeTransformer{Module: module}
tf := &CBDEdgeTransformer{Config: module}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}

View File

@ -3,14 +3,16 @@ package terraform
import (
"strings"
"testing"
"github.com/hashicorp/terraform/addrs"
)
func TestDestroyEdgeTransformer_basic(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
g.Add(&graphNodeDestroyerTest{AddrString: "test.A"})
g.Add(&graphNodeDestroyerTest{AddrString: "test.B"})
tf := &DestroyEdgeTransformer{
Module: testModule(t, "transform-destroy-edge-basic"),
Config: testModule(t, "transform-destroy-edge-basic"),
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
@ -24,12 +26,12 @@ func TestDestroyEdgeTransformer_basic(t *testing.T) {
}
func TestDestroyEdgeTransformer_create(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
g.Add(&graphNodeDestroyerTest{AddrString: "test.A"})
g.Add(&graphNodeDestroyerTest{AddrString: "test.B"})
g.Add(&graphNodeCreatorTest{AddrString: "test.A"})
tf := &DestroyEdgeTransformer{
Module: testModule(t, "transform-destroy-edge-basic"),
Config: testModule(t, "transform-destroy-edge-basic"),
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
@ -43,12 +45,12 @@ func TestDestroyEdgeTransformer_create(t *testing.T) {
}
func TestDestroyEdgeTransformer_multi(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
g.Add(&graphNodeDestroyerTest{AddrString: "test.A"})
g.Add(&graphNodeDestroyerTest{AddrString: "test.B"})
g.Add(&graphNodeDestroyerTest{AddrString: "test.C"})
tf := &DestroyEdgeTransformer{
Module: testModule(t, "transform-destroy-edge-multi"),
Config: testModule(t, "transform-destroy-edge-multi"),
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
@ -62,10 +64,10 @@ func TestDestroyEdgeTransformer_multi(t *testing.T) {
}
func TestDestroyEdgeTransformer_selfRef(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
g.Add(&graphNodeDestroyerTest{AddrString: "test.A"})
tf := &DestroyEdgeTransformer{
Module: testModule(t, "transform-destroy-edge-self-ref"),
Config: testModule(t, "transform-destroy-edge-self-ref"),
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
@ -79,11 +81,11 @@ func TestDestroyEdgeTransformer_selfRef(t *testing.T) {
}
func TestDestroyEdgeTransformer_module(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
g.Add(&graphNodeDestroyerTest{AddrString: "module.child.aws_instance.b"})
g.Add(&graphNodeDestroyerTest{AddrString: "aws_instance.a"})
tf := &DestroyEdgeTransformer{
Module: testModule(t, "transform-destroy-edge-module"),
Config: testModule(t, "transform-destroy-edge-module"),
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
@ -97,12 +99,12 @@ func TestDestroyEdgeTransformer_module(t *testing.T) {
}
func TestDestroyEdgeTransformer_moduleOnly(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
g.Add(&graphNodeDestroyerTest{AddrString: "module.child.aws_instance.a"})
g.Add(&graphNodeDestroyerTest{AddrString: "module.child.aws_instance.b"})
g.Add(&graphNodeDestroyerTest{AddrString: "module.child.aws_instance.c"})
tf := &DestroyEdgeTransformer{
Module: testModule(t, "transform-destroy-edge-module-only"),
Config: testModule(t, "transform-destroy-edge-module-only"),
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)

View File

@ -3,10 +3,12 @@ package terraform
import (
"strings"
"testing"
"github.com/hashicorp/terraform/addrs"
)
func TestDiffTransformer_nilDiff(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
tf := &DiffTransformer{}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
@ -18,7 +20,7 @@ func TestDiffTransformer_nilDiff(t *testing.T) {
}
func TestDiffTransformer(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
tf := &DiffTransformer{
Diff: &Diff{
Modules: []*ModuleDiff{

View File

@ -3,21 +3,23 @@ package terraform
import (
"strings"
"testing"
"github.com/hashicorp/terraform/addrs"
)
func TestModuleVariableTransformer(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
module := testModule(t, "transform-module-var-basic")
{
tf := &RootVariableTransformer{Module: module}
tf := &RootVariableTransformer{Config: module}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
tf := &ModuleVariableTransformer{Module: module, DisablePrune: true}
tf := &ModuleVariableTransformer{Config: module}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
@ -31,18 +33,18 @@ func TestModuleVariableTransformer(t *testing.T) {
}
func TestModuleVariableTransformer_nested(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
module := testModule(t, "transform-module-var-nested")
{
tf := &RootVariableTransformer{Module: module}
tf := &RootVariableTransformer{Config: module}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
tf := &ModuleVariableTransformer{Module: module, DisablePrune: true}
tf := &ModuleVariableTransformer{Config: module}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}

View File

@ -3,18 +3,15 @@ package terraform
import (
"strings"
"testing"
"github.com/hashicorp/terraform/addrs"
)
func TestOrphanResourceCountTransformer(t *testing.T) {
addr, err := parseResourceAddressInternal("aws_instance.foo")
if err != nil {
t.Fatalf("err: %s", err)
}
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: RootModulePath,
Path: []string{"root"},
Resources: map[string]*ResourceState{
"aws_instance.web": &ResourceState{
Type: "aws_instance",
@ -41,14 +38,16 @@ func TestOrphanResourceCountTransformer(t *testing.T) {
},
}
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &OrphanResourceCountTransformer{
Concrete: testOrphanResourceConcreteFunc,
Count: 1,
Addr: addr,
State: state,
Addr: addrs.RootModuleInstance.Resource(
addrs.ManagedResourceMode, "aws_instance", "foo",
),
State: state,
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
@ -63,15 +62,10 @@ func TestOrphanResourceCountTransformer(t *testing.T) {
}
func TestOrphanResourceCountTransformer_zero(t *testing.T) {
addr, err := parseResourceAddressInternal("aws_instance.foo")
if err != nil {
t.Fatalf("err: %s", err)
}
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: RootModulePath,
Path: []string{"root"},
Resources: map[string]*ResourceState{
"aws_instance.web": &ResourceState{
Type: "aws_instance",
@ -98,14 +92,16 @@ func TestOrphanResourceCountTransformer_zero(t *testing.T) {
},
}
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &OrphanResourceCountTransformer{
Concrete: testOrphanResourceConcreteFunc,
Count: 0,
Addr: addr,
State: state,
Addr: addrs.RootModuleInstance.Resource(
addrs.ManagedResourceMode, "aws_instance", "foo",
),
State: state,
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
@ -120,15 +116,10 @@ func TestOrphanResourceCountTransformer_zero(t *testing.T) {
}
func TestOrphanResourceCountTransformer_oneNoIndex(t *testing.T) {
addr, err := parseResourceAddressInternal("aws_instance.foo")
if err != nil {
t.Fatalf("err: %s", err)
}
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: RootModulePath,
Path: []string{"root"},
Resources: map[string]*ResourceState{
"aws_instance.web": &ResourceState{
Type: "aws_instance",
@ -155,14 +146,16 @@ func TestOrphanResourceCountTransformer_oneNoIndex(t *testing.T) {
},
}
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &OrphanResourceCountTransformer{
Concrete: testOrphanResourceConcreteFunc,
Count: 1,
Addr: addr,
State: state,
Addr: addrs.RootModuleInstance.Resource(
addrs.ManagedResourceMode, "aws_instance", "foo",
),
State: state,
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
@ -177,15 +170,10 @@ func TestOrphanResourceCountTransformer_oneNoIndex(t *testing.T) {
}
func TestOrphanResourceCountTransformer_oneIndex(t *testing.T) {
addr, err := parseResourceAddressInternal("aws_instance.foo")
if err != nil {
t.Fatalf("err: %s", err)
}
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: RootModulePath,
Path: []string{"root"},
Resources: map[string]*ResourceState{
"aws_instance.web": &ResourceState{
Type: "aws_instance",
@ -212,14 +200,16 @@ func TestOrphanResourceCountTransformer_oneIndex(t *testing.T) {
},
}
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &OrphanResourceCountTransformer{
Concrete: testOrphanResourceConcreteFunc,
Count: 1,
Addr: addr,
State: state,
Addr: addrs.RootModuleInstance.Resource(
addrs.ManagedResourceMode, "aws_instance", "foo",
),
State: state,
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
@ -234,15 +224,10 @@ func TestOrphanResourceCountTransformer_oneIndex(t *testing.T) {
}
func TestOrphanResourceCountTransformer_zeroAndNone(t *testing.T) {
addr, err := parseResourceAddressInternal("aws_instance.foo")
if err != nil {
t.Fatalf("err: %s", err)
}
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: RootModulePath,
Path: []string{"root"},
Resources: map[string]*ResourceState{
"aws_instance.web": &ResourceState{
Type: "aws_instance",
@ -269,14 +254,16 @@ func TestOrphanResourceCountTransformer_zeroAndNone(t *testing.T) {
},
}
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &OrphanResourceCountTransformer{
Concrete: testOrphanResourceConcreteFunc,
Count: 1,
Addr: addr,
State: state,
Addr: addrs.RootModuleInstance.Resource(
addrs.ManagedResourceMode, "aws_instance", "foo",
),
State: state,
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
@ -291,15 +278,10 @@ func TestOrphanResourceCountTransformer_zeroAndNone(t *testing.T) {
}
func TestOrphanResourceCountTransformer_zeroAndNoneCount(t *testing.T) {
addr, err := parseResourceAddressInternal("aws_instance.foo")
if err != nil {
t.Fatalf("err: %s", err)
}
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: RootModulePath,
Path: []string{"root"},
Resources: map[string]*ResourceState{
"aws_instance.web": &ResourceState{
Type: "aws_instance",
@ -326,14 +308,16 @@ func TestOrphanResourceCountTransformer_zeroAndNoneCount(t *testing.T) {
},
}
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &OrphanResourceCountTransformer{
Concrete: testOrphanResourceConcreteFunc,
Count: 2,
Addr: addr,
State: state,
Addr: addrs.RootModuleInstance.Resource(
addrs.ManagedResourceMode, "aws_instance", "foo",
),
State: state,
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)

View File

@ -5,6 +5,8 @@ import (
"strings"
"testing"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/dag"
)
@ -13,7 +15,7 @@ func TestOrphanResourceTransformer(t *testing.T) {
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: RootModulePath,
Path: []string{"root"},
Resources: map[string]*ResourceState{
"aws_instance.web": &ResourceState{
Type: "aws_instance",
@ -34,9 +36,9 @@ func TestOrphanResourceTransformer(t *testing.T) {
},
}
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &ConfigTransformer{Module: mod}
tf := &ConfigTransformer{Config: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
@ -45,7 +47,8 @@ func TestOrphanResourceTransformer(t *testing.T) {
{
tf := &OrphanResourceTransformer{
Concrete: testOrphanResourceConcreteFunc,
State: state, Module: mod,
State: state,
Config: mod,
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
@ -65,9 +68,9 @@ func TestOrphanResourceTransformer_nilModule(t *testing.T) {
Modules: []*ModuleState{nil},
}
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &ConfigTransformer{Module: mod}
tf := &ConfigTransformer{Config: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
@ -76,7 +79,8 @@ func TestOrphanResourceTransformer_nilModule(t *testing.T) {
{
tf := &OrphanResourceTransformer{
Concrete: testOrphanResourceConcreteFunc,
State: state, Module: mod,
State: state,
Config: mod,
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
@ -89,7 +93,7 @@ func TestOrphanResourceTransformer_countGood(t *testing.T) {
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: RootModulePath,
Path: []string{"root"},
Resources: map[string]*ResourceState{
"aws_instance.foo.0": &ResourceState{
Type: "aws_instance",
@ -109,9 +113,9 @@ func TestOrphanResourceTransformer_countGood(t *testing.T) {
},
}
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &ConfigTransformer{Module: mod}
tf := &ConfigTransformer{Config: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
@ -120,7 +124,8 @@ func TestOrphanResourceTransformer_countGood(t *testing.T) {
{
tf := &OrphanResourceTransformer{
Concrete: testOrphanResourceConcreteFunc,
State: state, Module: mod,
State: state,
Config: mod,
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
@ -139,7 +144,7 @@ func TestOrphanResourceTransformer_countBad(t *testing.T) {
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: RootModulePath,
Path: []string{"root"},
Resources: map[string]*ResourceState{
"aws_instance.foo.0": &ResourceState{
Type: "aws_instance",
@ -159,9 +164,9 @@ func TestOrphanResourceTransformer_countBad(t *testing.T) {
},
}
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &ConfigTransformer{Module: mod}
tf := &ConfigTransformer{Config: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
@ -170,7 +175,8 @@ func TestOrphanResourceTransformer_countBad(t *testing.T) {
{
tf := &OrphanResourceTransformer{
Concrete: testOrphanResourceConcreteFunc,
State: state, Module: mod,
State: state,
Config: mod,
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
@ -189,7 +195,7 @@ func TestOrphanResourceTransformer_modules(t *testing.T) {
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: RootModulePath,
Path: []string{"root"},
Resources: map[string]*ResourceState{
"aws_instance.foo": &ResourceState{
Type: "aws_instance",
@ -214,9 +220,9 @@ func TestOrphanResourceTransformer_modules(t *testing.T) {
},
}
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &ConfigTransformer{Module: mod}
tf := &ConfigTransformer{Config: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
@ -225,7 +231,8 @@ func TestOrphanResourceTransformer_modules(t *testing.T) {
{
tf := &OrphanResourceTransformer{
Concrete: testOrphanResourceConcreteFunc,
State: state, Module: mod,
State: state,
Config: mod,
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
@ -258,14 +265,14 @@ aws_instance.foo
module.child.aws_instance.web (orphan)
`
func testOrphanResourceConcreteFunc(a *NodeAbstractResource) dag.Vertex {
return &testOrphanResourceConcrete{a}
func testOrphanResourceConcreteFunc(a *NodeAbstractResourceInstance) dag.Vertex {
return &testOrphanResourceInstanceConcrete{a}
}
type testOrphanResourceConcrete struct {
*NodeAbstractResource
type testOrphanResourceInstanceConcrete struct {
*NodeAbstractResourceInstance
}
func (n *testOrphanResourceConcrete) Name() string {
return fmt.Sprintf("%s (orphan)", n.NodeAbstractResource.Name())
func (n *testOrphanResourceInstanceConcrete) Name() string {
return fmt.Sprintf("%s (orphan)", n.NodeAbstractResourceInstance.Name())
}

View File

@ -4,22 +4,23 @@ import (
"strings"
"testing"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/dag"
)
func TestProviderTransformer(t *testing.T) {
mod := testModule(t, "transform-provider-basic")
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &ConfigTransformer{Module: mod}
tf := &ConfigTransformer{Config: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
transform := &AttachResourceConfigTransformer{Module: mod}
transform := &AttachResourceConfigTransformer{Config: mod}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
@ -45,14 +46,21 @@ func TestProviderTransformer(t *testing.T) {
}
func TestProviderTransformer_moduleChild(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &ImportStateTransformer{
Targets: []*ImportTarget{
&ImportTarget{
Addr: "module.moo.foo_instance.qux",
ID: "bar",
Addr: addrs.RootModuleInstance.
Child("moo", addrs.NoKey).
ResourceInstance(
addrs.ManagedResourceMode,
"foo_instance",
"qux",
addrs.NoKey,
),
ID: "bar",
},
},
}
@ -85,16 +93,16 @@ func TestProviderTransformer_moduleChild(t *testing.T) {
func TestCloseProviderTransformer(t *testing.T) {
mod := testModule(t, "transform-provider-basic")
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &ConfigTransformer{Module: mod}
tf := &ConfigTransformer{Config: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
transform := &AttachResourceConfigTransformer{Module: mod}
transform := &AttachResourceConfigTransformer{Config: mod}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
@ -131,14 +139,18 @@ func TestCloseProviderTransformer(t *testing.T) {
func TestCloseProviderTransformer_withTargets(t *testing.T) {
mod := testModule(t, "transform-provider-basic")
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
transforms := []GraphTransformer{
&ConfigTransformer{Module: mod},
&ConfigTransformer{Config: mod},
&MissingProviderTransformer{Providers: []string{"aws"}},
&ProviderTransformer{},
&CloseProviderTransformer{},
&TargetsTransformer{
Targets: []string{"something.else"},
Targets: []addrs.Targetable{
addrs.RootModuleInstance.Resource(
addrs.ManagedResourceMode, "something", "else",
),
},
},
}
@ -158,16 +170,16 @@ func TestCloseProviderTransformer_withTargets(t *testing.T) {
func TestMissingProviderTransformer(t *testing.T) {
mod := testModule(t, "transform-provider-missing")
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &ConfigTransformer{Module: mod}
tf := &ConfigTransformer{Config: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
transform := &AttachResourceConfigTransformer{Module: mod}
transform := &AttachResourceConfigTransformer{Config: mod}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
@ -206,16 +218,16 @@ func TestMissingProviderTransformer_grandchildMissing(t *testing.T) {
concrete := func(a *NodeAbstractProvider) dag.Vertex { return a }
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &ConfigTransformer{Module: mod}
tf := &ConfigTransformer{Config: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
transform := &AttachResourceConfigTransformer{Module: mod}
transform := &AttachResourceConfigTransformer{Config: mod}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
@ -242,7 +254,7 @@ func TestMissingProviderTransformer_grandchildMissing(t *testing.T) {
}
func TestMissingProviderTransformer_moduleChild(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
// We use the import state transformer since at the time of writing
// this test it is the first and only transformer that will introduce
@ -251,8 +263,15 @@ func TestMissingProviderTransformer_moduleChild(t *testing.T) {
tf := &ImportStateTransformer{
Targets: []*ImportTarget{
&ImportTarget{
Addr: "module.moo.foo_instance.qux",
ID: "bar",
Addr: addrs.RootModuleInstance.
Child("moo", addrs.NoKey).
ResourceInstance(
addrs.ManagedResourceMode,
"foo_instance",
"qux",
addrs.NoKey,
),
ID: "bar",
},
},
}
@ -276,7 +295,7 @@ func TestMissingProviderTransformer_moduleChild(t *testing.T) {
}
func TestMissingProviderTransformer_moduleGrandchild(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
// We use the import state transformer since at the time of writing
// this test it is the first and only transformer that will introduce
@ -285,8 +304,16 @@ func TestMissingProviderTransformer_moduleGrandchild(t *testing.T) {
tf := &ImportStateTransformer{
Targets: []*ImportTarget{
&ImportTarget{
Addr: "module.a.module.b.foo_instance.qux",
ID: "bar",
Addr: addrs.RootModuleInstance.
Child("a", addrs.NoKey).
Child("b", addrs.NoKey).
ResourceInstance(
addrs.ManagedResourceMode,
"foo_instance",
"qux",
addrs.NoKey,
),
ID: "bar",
},
},
}
@ -310,15 +337,22 @@ func TestMissingProviderTransformer_moduleGrandchild(t *testing.T) {
}
func TestParentProviderTransformer(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
// Introduce a cihld module
{
tf := &ImportStateTransformer{
Targets: []*ImportTarget{
&ImportTarget{
Addr: "module.moo.foo_instance.qux",
ID: "bar",
Addr: addrs.RootModuleInstance.
Child("moo", addrs.NoKey).
ResourceInstance(
addrs.ManagedResourceMode,
"foo_instance",
"qux",
addrs.NoKey,
),
ID: "bar",
},
},
}
@ -351,7 +385,7 @@ func TestParentProviderTransformer(t *testing.T) {
}
func TestParentProviderTransformer_moduleGrandchild(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
// We use the import state transformer since at the time of writing
// this test it is the first and only transformer that will introduce
@ -360,8 +394,16 @@ func TestParentProviderTransformer_moduleGrandchild(t *testing.T) {
tf := &ImportStateTransformer{
Targets: []*ImportTarget{
&ImportTarget{
Addr: "module.a.module.b.foo_instance.qux",
ID: "bar",
Addr: addrs.RootModuleInstance.
Child("a", addrs.NoKey).
Child("b", addrs.NoKey).
ResourceInstance(
addrs.ManagedResourceMode,
"foo_instance",
"qux",
addrs.NoKey,
),
ID: "bar",
},
},
}
@ -395,16 +437,16 @@ func TestParentProviderTransformer_moduleGrandchild(t *testing.T) {
func TestPruneProviderTransformer(t *testing.T) {
mod := testModule(t, "transform-provider-prune")
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &ConfigTransformer{Module: mod}
tf := &ConfigTransformer{Config: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
transform := &AttachResourceConfigTransformer{Module: mod}
transform := &AttachResourceConfigTransformer{Config: mod}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
@ -450,15 +492,15 @@ func TestProviderConfigTransformer_parentProviders(t *testing.T) {
mod := testModule(t, "transform-provider-inherit")
concrete := func(a *NodeAbstractProvider) dag.Vertex { return a }
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &ConfigTransformer{Module: mod}
tf := &ConfigTransformer{Config: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
tf := &AttachResourceConfigTransformer{Module: mod}
tf := &AttachResourceConfigTransformer{Config: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
@ -483,15 +525,15 @@ func TestProviderConfigTransformer_grandparentProviders(t *testing.T) {
mod := testModule(t, "transform-provider-grandchild-inherit")
concrete := func(a *NodeAbstractProvider) dag.Vertex { return a }
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &ConfigTransformer{Module: mod}
tf := &ConfigTransformer{Config: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
tf := &AttachResourceConfigTransformer{Module: mod}
tf := &AttachResourceConfigTransformer{Config: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
@ -516,15 +558,15 @@ func TestProviderConfigTransformer_implicitModule(t *testing.T) {
mod := testModule(t, "transform-provider-implicit-module")
concrete := func(a *NodeAbstractProvider) dag.Vertex { return a }
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &ConfigTransformer{Module: mod}
tf := &ConfigTransformer{Config: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
tf := &AttachResourceConfigTransformer{Module: mod}
tf := &AttachResourceConfigTransformer{Config: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
@ -550,15 +592,15 @@ func TestProviderConfigTransformer_invalidProvider(t *testing.T) {
mod := testModule(t, "transform-provider-invalid")
concrete := func(a *NodeAbstractProvider) dag.Vertex { return a }
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &ConfigTransformer{Module: mod}
tf := &ConfigTransformer{Config: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
tf := &AttachResourceConfigTransformer{Module: mod}
tf := &AttachResourceConfigTransformer{Config: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}

View File

@ -4,22 +4,24 @@ import (
"strings"
"testing"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/dag"
)
func TestMissingProvisionerTransformer(t *testing.T) {
mod := testModule(t, "transform-provisioner-basic")
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &ConfigTransformer{Module: mod}
tf := &ConfigTransformer{Config: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
transform := &AttachResourceConfigTransformer{Module: mod}
transform := &AttachResourceConfigTransformer{Config: mod}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
@ -49,9 +51,9 @@ func TestMissingProvisionerTransformer(t *testing.T) {
func TestMissingProvisionerTransformer_module(t *testing.T) {
mod := testModule(t, "transform-provisioner-module")
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
concreteResource := func(a *NodeAbstractResource) dag.Vertex {
concreteResource := func(a *NodeAbstractResourceInstance) dag.Vertex {
return a
}
@ -87,7 +89,7 @@ func TestMissingProvisionerTransformer_module(t *testing.T) {
}
{
transform := &AttachResourceConfigTransformer{Module: mod}
transform := &AttachResourceConfigTransformer{Config: mod}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
@ -117,16 +119,16 @@ func TestMissingProvisionerTransformer_module(t *testing.T) {
func TestCloseProvisionerTransformer(t *testing.T) {
mod := testModule(t, "transform-provisioner-basic")
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &ConfigTransformer{Module: mod}
tf := &ConfigTransformer{Config: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
transform := &AttachResourceConfigTransformer{Module: mod}
transform := &AttachResourceConfigTransformer{Config: mod}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}

View File

@ -6,11 +6,12 @@ import (
"strings"
"testing"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/dag"
)
func TestReferenceTransformer_simple(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
g.Add(&graphNodeRefParentTest{
NameValue: "A",
Names: []string{"A"},
@ -33,7 +34,7 @@ func TestReferenceTransformer_simple(t *testing.T) {
}
func TestReferenceTransformer_self(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
g.Add(&graphNodeRefParentTest{
NameValue: "A",
Names: []string{"A"},
@ -56,7 +57,7 @@ func TestReferenceTransformer_self(t *testing.T) {
}
func TestReferenceTransformer_path(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
g.Add(&graphNodeRefParentTest{
NameValue: "A",
Names: []string{"A"},
@ -89,7 +90,7 @@ func TestReferenceTransformer_path(t *testing.T) {
}
func TestReferenceTransformer_backup(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
g.Add(&graphNodeRefParentTest{
NameValue: "A",
Names: []string{"A"},
@ -112,7 +113,7 @@ func TestReferenceTransformer_backup(t *testing.T) {
}
func TestReferenceTransformer_backupPrimary(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
g.Add(&graphNodeRefParentTest{
NameValue: "A",
Names: []string{"A"},
@ -139,7 +140,7 @@ func TestReferenceTransformer_backupPrimary(t *testing.T) {
}
func TestReferenceTransformer_modulePath(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
g.Add(&graphNodeRefParentTest{
NameValue: "A",
Names: []string{"A"},
@ -163,7 +164,7 @@ func TestReferenceTransformer_modulePath(t *testing.T) {
}
func TestReferenceTransformer_modulePathNormalized(t *testing.T) {
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
g.Add(&graphNodeRefParentTest{
NameValue: "A",
Names: []string{"A"},
@ -258,7 +259,7 @@ func TestReferenceMapReferencedBy(t *testing.T) {
for tn, tc := range cases {
t.Run(tn, func(t *testing.T) {
rm := NewReferenceMap(tc.Nodes)
result := rm.ReferencedBy(tc.Check)
result := rm.Referrers(tc.Check)
var resultStr []string
for _, v := range result {

View File

@ -3,14 +3,16 @@ package terraform
import (
"strings"
"testing"
"github.com/hashicorp/terraform/addrs"
)
func TestRootTransformer(t *testing.T) {
mod := testModule(t, "transform-root-basic")
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &ConfigTransformer{Module: mod}
tf := &ConfigTransformer{Config: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}

View File

@ -3,21 +3,23 @@ package terraform
import (
"strings"
"testing"
"github.com/hashicorp/terraform/addrs"
)
func TestTargetsTransformer(t *testing.T) {
mod := testModule(t, "transform-targets-basic")
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &ConfigTransformer{Module: mod}
tf := &ConfigTransformer{Config: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
transform := &AttachResourceConfigTransformer{Module: mod}
transform := &AttachResourceConfigTransformer{Config: mod}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
@ -31,7 +33,13 @@ func TestTargetsTransformer(t *testing.T) {
}
{
transform := &TargetsTransformer{Targets: []string{"aws_instance.me"}}
transform := &TargetsTransformer{
Targets: []addrs.Targetable{
addrs.RootModuleInstance.Resource(
addrs.ManagedResourceMode, "aws_instance", "me",
),
},
}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
@ -53,30 +61,30 @@ aws_vpc.me
func TestTargetsTransformer_downstream(t *testing.T) {
mod := testModule(t, "transform-targets-downstream")
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
transform := &ConfigTransformer{Module: mod}
transform := &ConfigTransformer{Config: mod}
if err := transform.Transform(&g); err != nil {
t.Fatalf("%T failed: %s", transform, err)
}
}
{
transform := &AttachResourceConfigTransformer{Module: mod}
transform := &AttachResourceConfigTransformer{Config: mod}
if err := transform.Transform(&g); err != nil {
t.Fatalf("%T failed: %s", transform, err)
}
}
{
transform := &AttachResourceConfigTransformer{Module: mod}
transform := &AttachResourceConfigTransformer{Config: mod}
if err := transform.Transform(&g); err != nil {
t.Fatalf("%T failed: %s", transform, err)
}
}
{
transform := &OutputTransformer{Module: mod}
transform := &OutputTransformer{Config: mod}
if err := transform.Transform(&g); err != nil {
t.Fatalf("%T failed: %s", transform, err)
}
@ -90,7 +98,16 @@ func TestTargetsTransformer_downstream(t *testing.T) {
}
{
transform := &TargetsTransformer{Targets: []string{"module.child.module.grandchild.aws_instance.foo"}}
transform := &TargetsTransformer{
Targets: []addrs.Targetable{
addrs.RootModuleInstance.
Child("child", addrs.NoKey).
Child("grandchild", addrs.NoKey).
Resource(
addrs.ManagedResourceMode, "aws_instance", "foo",
),
},
}
if err := transform.Transform(&g); err != nil {
t.Fatalf("%T failed: %s", transform, err)
}
@ -116,16 +133,16 @@ output.grandchild_id
func TestTargetsTransformer_destroy(t *testing.T) {
mod := testModule(t, "transform-targets-destroy")
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &ConfigTransformer{Module: mod}
tf := &ConfigTransformer{Config: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
transform := &AttachResourceConfigTransformer{Module: mod}
transform := &AttachResourceConfigTransformer{Config: mod}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
@ -140,7 +157,11 @@ func TestTargetsTransformer_destroy(t *testing.T) {
{
transform := &TargetsTransformer{
Targets: []string{"aws_instance.me"},
Targets: []addrs.Targetable{
addrs.RootModuleInstance.Resource(
addrs.ManagedResourceMode, "aws_instance", "me",
),
},
Destroy: true,
}
if err := transform.Transform(&g); err != nil {

View File

@ -3,21 +3,23 @@ package terraform
import (
"strings"
"testing"
"github.com/hashicorp/terraform/addrs"
)
func TestTransitiveReductionTransformer(t *testing.T) {
mod := testModule(t, "transform-trans-reduce-basic")
g := Graph{Path: RootModulePath}
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &ConfigTransformer{Module: mod}
tf := &ConfigTransformer{Config: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
transform := &AttachResourceConfigTransformer{Module: mod}
transform := &AttachResourceConfigTransformer{Config: mod}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}

View File

@ -62,6 +62,24 @@ const (
// InputValues is a map of InputValue instances.
type InputValues map[string]*InputValue
// InputValuesFromCaller turns the given map of naked values into an
// InputValues that attributes each value to "a caller", using the source
// type ValueFromCaller. This is primarily useful for testing purposes.
//
// This should not be used as a general way to convert map[string]cty.Value
// into InputValues, since in most real cases we want to set a suitable
// other SourceType and possibly SourceRange value.
func InputValuesFromCaller(vals map[string]cty.Value) InputValues {
ret := make(InputValues, len(vals))
for k, v := range vals {
ret[k] = &InputValue{
Value: v,
SourceType: ValueFromCaller,
}
}
return ret
}
// Override merges the given value maps with the receiver, overriding any
// conflicting keys so that the latest definition wins.
func (vv InputValues) Override(others ...InputValues) InputValues {

View File

@ -3,169 +3,91 @@ package terraform
import (
"reflect"
"testing"
"github.com/zclconf/go-cty/cty"
)
func TestVariables(t *testing.T) {
cases := map[string]struct {
Module string
Env map[string]string
Override map[string]interface{}
Error bool
Expected map[string]interface{}
Override map[string]cty.Value
Expected map[string]cty.Value
}{
"config only": {
"vars-basic",
nil,
nil,
false,
map[string]interface{}{
"a": "foo",
"b": []interface{}{},
"c": map[string]interface{}{},
},
},
"env vars": {
"vars-basic",
map[string]string{
"TF_VAR_a": "bar",
"TF_VAR_b": `["foo", "bar"]`,
"TF_VAR_c": `{"foo" = "bar"}`,
},
nil,
false,
map[string]interface{}{
"a": "bar",
"b": []interface{}{"foo", "bar"},
"c": map[string]interface{}{
"foo": "bar",
},
map[string]cty.Value{
"a": cty.StringVal("foo"),
"b": cty.ListValEmpty(cty.String),
"c": cty.MapValEmpty(cty.String),
},
},
"override": {
"vars-basic",
nil,
map[string]interface{}{
"a": "bar",
"b": []interface{}{"foo", "bar"},
"c": map[string]interface{}{
"foo": "bar",
},
map[string]cty.Value{
"a": cty.StringVal("bar"),
"b": cty.ListVal([]cty.Value{
cty.StringVal("foo"),
cty.StringVal("bar"),
}),
"c": cty.MapVal(map[string]cty.Value{
"foo": cty.StringVal("bar"),
}),
},
false,
map[string]interface{}{
"a": "bar",
"b": []interface{}{"foo", "bar"},
"c": map[string]interface{}{
"foo": "bar",
},
},
},
"override partial map": {
"vars-basic",
map[string]string{
"TF_VAR_c": `{"foo" = "a", "bar" = "baz"}`,
},
map[string]interface{}{
"c": map[string]interface{}{
"foo": "bar",
},
},
false,
map[string]interface{}{
"a": "foo",
"b": []interface{}{},
"c": map[string]interface{}{
"foo": "bar",
"bar": "baz",
},
map[string]cty.Value{
"a": cty.StringVal("bar"),
"b": cty.ListVal([]cty.Value{
cty.StringVal("foo"),
cty.StringVal("bar"),
}),
"c": cty.MapVal(map[string]cty.Value{
"foo": cty.StringVal("bar"),
}),
},
},
"bools: config only": {
"vars-basic-bool",
nil,
nil,
false,
map[string]interface{}{
"a": "1",
"b": "0",
map[string]cty.Value{
"a": cty.StringVal("1"),
"b": cty.StringVal("0"),
},
},
"bools: override with string": {
"vars-basic-bool",
nil,
map[string]interface{}{
"a": "foo",
"b": "bar",
map[string]cty.Value{
"a": cty.StringVal("foo"),
"b": cty.StringVal("bar"),
},
false,
map[string]interface{}{
"a": "foo",
"b": "bar",
},
},
"bools: override with env": {
"vars-basic-bool",
map[string]string{
"TF_VAR_a": "false",
"TF_VAR_b": "true",
},
nil,
false,
map[string]interface{}{
"a": "false",
"b": "true",
map[string]cty.Value{
"a": cty.StringVal("foo"),
"b": cty.StringVal("bar"),
},
},
"bools: override with bool": {
"vars-basic-bool",
nil,
map[string]interface{}{
"a": false,
"b": true,
map[string]cty.Value{
"a": cty.False,
"b": cty.True,
},
false,
map[string]interface{}{
"a": "0",
"b": "1",
map[string]cty.Value{
"a": cty.StringVal("0"),
"b": cty.StringVal("1"),
},
},
"override map with string": {
"vars-basic",
map[string]string{
"TF_VAR_c": `{"foo" = "a", "bar" = "baz"}`,
},
map[string]interface{}{
"c": "bar",
},
true,
nil,
},
}
for name, tc := range cases {
// Wrapped in a func so we can get defers to work
t.Run(name, func(t *testing.T) {
// Set the env vars
for k, v := range tc.Env {
defer tempEnv(t, k, v)()
}
m := testModule(t, tc.Module)
actual, err := Variables(m, tc.Override)
if (err != nil) != tc.Error {
t.Fatalf("%s: err: %s", name, err)
}
if err != nil {
return
}
fromConfig := DefaultVariableValues(m.Module.Variables)
overrides := InputValuesFromCaller(tc.Override)
actual := fromConfig.Override(overrides)
if !reflect.DeepEqual(actual, tc.Expected) {
t.Fatalf("%s\n\nexpected: %#v\n\ngot: %#v", name, tc.Expected, actual)