2017-05-02 22:36:38 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"testing"
|
2018-05-04 21:24:06 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2018-05-10 11:34:14 -05:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2017-05-02 22:36:38 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNodeRefreshableDataResourceDynamicExpand_scaleOut(t *testing.T) {
|
|
|
|
var stateLock sync.RWMutex
|
|
|
|
|
|
|
|
m := testModule(t, "refresh-data-scale-inout")
|
|
|
|
|
|
|
|
state := &State{
|
|
|
|
Modules: []*ModuleState{
|
|
|
|
&ModuleState{
|
|
|
|
Path: rootModulePath,
|
|
|
|
Resources: map[string]*ResourceState{
|
|
|
|
"data.aws_instance.foo.0": &ResourceState{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Deposed: []*InstanceState{
|
|
|
|
&InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"data.aws_instance.foo.1": &ResourceState{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Deposed: []*InstanceState{
|
|
|
|
&InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
n := &NodeRefreshableDataResource{
|
2018-05-04 21:24:06 -05:00
|
|
|
NodeAbstractResource: &NodeAbstractResource{
|
|
|
|
Addr: addrs.RootModuleInstance.Resource(
|
|
|
|
addrs.DataResourceMode,
|
|
|
|
"aws_instance",
|
|
|
|
"foo",
|
|
|
|
),
|
|
|
|
Config: m.Module.DataResources["data.aws_instance.foo"],
|
2017-05-02 22:36:38 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
g, err := n.DynamicExpand(&MockEvalContext{
|
2018-05-04 21:24:06 -05:00
|
|
|
PathPath: addrs.RootModuleInstance,
|
2017-05-02 22:36:38 -05:00
|
|
|
StateState: state,
|
|
|
|
StateLock: &stateLock,
|
2018-05-10 11:34:14 -05:00
|
|
|
|
|
|
|
// DynamicExpand will call EvaluateExpr to evaluate the "count"
|
|
|
|
// expression, which is just a literal number 3 in the fixture config
|
|
|
|
// and so we'll just hard-code this here too.
|
|
|
|
EvaluateExprResult: cty.NumberIntVal(3),
|
2017-05-02 22:36:38 -05:00
|
|
|
})
|
2017-07-20 04:23:43 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error on DynamicExpand: %s", err)
|
|
|
|
}
|
2017-05-02 22:36:38 -05:00
|
|
|
|
|
|
|
actual := g.StringWithNodeTypes()
|
|
|
|
expected := `data.aws_instance.foo[0] - *terraform.NodeRefreshableDataResourceInstance
|
|
|
|
data.aws_instance.foo[1] - *terraform.NodeRefreshableDataResourceInstance
|
|
|
|
data.aws_instance.foo[2] - *terraform.NodeRefreshableDataResourceInstance
|
|
|
|
root - terraform.graphNodeRoot
|
|
|
|
data.aws_instance.foo[0] - *terraform.NodeRefreshableDataResourceInstance
|
|
|
|
data.aws_instance.foo[1] - *terraform.NodeRefreshableDataResourceInstance
|
|
|
|
data.aws_instance.foo[2] - *terraform.NodeRefreshableDataResourceInstance
|
|
|
|
`
|
|
|
|
if expected != actual {
|
|
|
|
t.Fatalf("Expected:\n%s\nGot:\n%s", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeRefreshableDataResourceDynamicExpand_scaleIn(t *testing.T) {
|
|
|
|
var stateLock sync.RWMutex
|
|
|
|
|
|
|
|
m := testModule(t, "refresh-data-scale-inout")
|
|
|
|
|
|
|
|
state := &State{
|
|
|
|
Modules: []*ModuleState{
|
|
|
|
&ModuleState{
|
|
|
|
Path: rootModulePath,
|
|
|
|
Resources: map[string]*ResourceState{
|
|
|
|
"data.aws_instance.foo.0": &ResourceState{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Deposed: []*InstanceState{
|
|
|
|
&InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"data.aws_instance.foo.1": &ResourceState{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Deposed: []*InstanceState{
|
|
|
|
&InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"data.aws_instance.foo.2": &ResourceState{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Deposed: []*InstanceState{
|
|
|
|
&InstanceState{
|
|
|
|
ID: "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"data.aws_instance.foo.3": &ResourceState{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Deposed: []*InstanceState{
|
|
|
|
&InstanceState{
|
|
|
|
ID: "qux",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
n := &NodeRefreshableDataResource{
|
2018-05-04 21:24:06 -05:00
|
|
|
NodeAbstractResource: &NodeAbstractResource{
|
|
|
|
Addr: addrs.RootModuleInstance.Resource(
|
|
|
|
addrs.DataResourceMode,
|
|
|
|
"aws_instance",
|
|
|
|
"foo",
|
|
|
|
),
|
|
|
|
Config: m.Module.DataResources["data.aws_instance.foo"],
|
2017-05-02 22:36:38 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
g, err := n.DynamicExpand(&MockEvalContext{
|
2018-05-04 21:24:06 -05:00
|
|
|
PathPath: addrs.RootModuleInstance,
|
2017-05-02 22:36:38 -05:00
|
|
|
StateState: state,
|
|
|
|
StateLock: &stateLock,
|
2018-05-10 11:34:14 -05:00
|
|
|
|
|
|
|
// DynamicExpand will call EvaluateExpr to evaluate the "count"
|
|
|
|
// expression, which is just a literal number 3 in the fixture config
|
|
|
|
// and so we'll just hard-code this here too.
|
|
|
|
EvaluateExprResult: cty.NumberIntVal(3),
|
2017-05-02 22:36:38 -05:00
|
|
|
})
|
2017-07-20 04:23:43 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error on DynamicExpand: %s", err)
|
|
|
|
}
|
2017-05-02 22:36:38 -05:00
|
|
|
actual := g.StringWithNodeTypes()
|
|
|
|
expected := `data.aws_instance.foo[0] - *terraform.NodeRefreshableDataResourceInstance
|
|
|
|
data.aws_instance.foo[1] - *terraform.NodeRefreshableDataResourceInstance
|
|
|
|
data.aws_instance.foo[2] - *terraform.NodeRefreshableDataResourceInstance
|
|
|
|
data.aws_instance.foo[3] - *terraform.NodeDestroyableDataResource
|
|
|
|
root - terraform.graphNodeRoot
|
|
|
|
data.aws_instance.foo[0] - *terraform.NodeRefreshableDataResourceInstance
|
|
|
|
data.aws_instance.foo[1] - *terraform.NodeRefreshableDataResourceInstance
|
|
|
|
data.aws_instance.foo[2] - *terraform.NodeRefreshableDataResourceInstance
|
|
|
|
data.aws_instance.foo[3] - *terraform.NodeDestroyableDataResource
|
|
|
|
`
|
|
|
|
if expected != actual {
|
|
|
|
t.Fatalf("Expected:\n%s\nGot:\n%s", expected, actual)
|
|
|
|
}
|
|
|
|
}
|