2016-11-05 19:58:38 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2018-05-04 21:24:06 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2018-09-05 16:35:30 -05:00
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
2018-08-17 14:32:35 -05:00
|
|
|
"github.com/hashicorp/terraform/providers"
|
2016-11-05 19:58:38 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPlanGraphBuilder_impl(t *testing.T) {
|
|
|
|
var _ GraphBuilder = new(PlanGraphBuilder)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPlanGraphBuilder(t *testing.T) {
|
2018-08-17 14:32:35 -05:00
|
|
|
awsProvider := &MockProvider{
|
2018-09-05 16:35:30 -05:00
|
|
|
GetSchemaReturn: &ProviderSchema{
|
|
|
|
Provider: simpleTestSchema(),
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_security_group": simpleTestSchema(),
|
|
|
|
"aws_instance": simpleTestSchema(),
|
|
|
|
"aws_load_balancer": simpleTestSchema(),
|
2018-05-31 14:39:45 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-08-17 14:32:35 -05:00
|
|
|
openstackProvider := &MockProvider{
|
2018-09-05 16:35:30 -05:00
|
|
|
GetSchemaReturn: &ProviderSchema{
|
|
|
|
Provider: simpleTestSchema(),
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"openstack_floating_ip": simpleTestSchema(),
|
2018-05-31 14:39:45 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
components := &basicComponentFactory{
|
2018-08-17 14:32:35 -05:00
|
|
|
providers: map[string]providers.Factory{
|
|
|
|
"aws": providers.FactoryFixed(awsProvider),
|
|
|
|
"openstack": providers.FactoryFixed(openstackProvider),
|
2018-05-31 14:39:45 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-11-05 19:58:38 -05:00
|
|
|
b := &PlanGraphBuilder{
|
2018-05-31 14:39:45 -05:00
|
|
|
Config: testModule(t, "graph-builder-plan-basic"),
|
|
|
|
Components: components,
|
|
|
|
Schemas: &Schemas{
|
2018-09-25 12:12:56 -05:00
|
|
|
Providers: map[string]*ProviderSchema{
|
2018-05-31 14:39:45 -05:00
|
|
|
"aws": awsProvider.GetSchemaReturn,
|
|
|
|
"openstack": openstackProvider.GetSchemaReturn,
|
2018-05-04 21:24:06 -05:00
|
|
|
},
|
|
|
|
},
|
2016-11-05 19:58:38 -05:00
|
|
|
DisableReduce: true,
|
|
|
|
}
|
|
|
|
|
2018-05-04 21:24:06 -05:00
|
|
|
g, err := b.Build(addrs.RootModuleInstance)
|
2016-11-05 19:58:38 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-05-04 21:24:06 -05:00
|
|
|
if g.Path.String() != addrs.RootModuleInstance.String() {
|
|
|
|
t.Fatalf("wrong module path %q", g.Path)
|
2016-11-05 19:58:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testPlanGraphBuilderStr)
|
|
|
|
if actual != expected {
|
2017-04-20 00:23:52 -05:00
|
|
|
t.Fatalf("expected:\n%s\n\ngot:\n%s", expected, actual)
|
2016-11-05 19:58:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-17 11:21:29 -06:00
|
|
|
func TestPlanGraphBuilder_targetModule(t *testing.T) {
|
|
|
|
b := &PlanGraphBuilder{
|
2018-05-10 11:56:32 -05:00
|
|
|
Config: testModule(t, "graph-builder-plan-target-module-provider"),
|
|
|
|
Components: simpleMockComponentFactory(),
|
2018-05-31 14:39:45 -05:00
|
|
|
Schemas: simpleTestSchemas(),
|
2018-05-04 21:24:06 -05:00
|
|
|
Targets: []addrs.Targetable{
|
|
|
|
addrs.RootModuleInstance.Child("child2", addrs.NoKey),
|
|
|
|
},
|
2017-02-17 11:21:29 -06:00
|
|
|
}
|
|
|
|
|
2018-05-04 21:24:06 -05:00
|
|
|
g, err := b.Build(addrs.RootModuleInstance)
|
2017-02-17 11:21:29 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Graph: %s", g.String())
|
|
|
|
|
2018-05-10 11:56:32 -05:00
|
|
|
testGraphNotContains(t, g, "module.child1.provider.test")
|
|
|
|
testGraphNotContains(t, g, "module.child1.test_object.foo")
|
2017-02-17 11:21:29 -06:00
|
|
|
}
|
|
|
|
|
2016-11-05 19:58:38 -05:00
|
|
|
const testPlanGraphBuilderStr = `
|
|
|
|
aws_instance.web
|
|
|
|
aws_security_group.firewall
|
|
|
|
provider.aws
|
|
|
|
var.foo
|
|
|
|
aws_load_balancer.weblb
|
|
|
|
aws_instance.web
|
|
|
|
provider.aws
|
|
|
|
aws_security_group.firewall
|
|
|
|
provider.aws
|
2017-07-01 11:48:37 -05:00
|
|
|
local.instance_id
|
|
|
|
aws_instance.web
|
2018-09-07 18:58:32 -05:00
|
|
|
meta.count-boundary (EachMode fixup)
|
2017-04-20 00:23:52 -05:00
|
|
|
aws_instance.web
|
|
|
|
aws_load_balancer.weblb
|
|
|
|
aws_security_group.firewall
|
2017-07-01 11:48:37 -05:00
|
|
|
local.instance_id
|
2017-04-20 00:23:52 -05:00
|
|
|
openstack_floating_ip.random
|
2017-07-01 11:48:37 -05:00
|
|
|
output.instance_id
|
2017-04-20 00:23:52 -05:00
|
|
|
provider.aws
|
|
|
|
provider.openstack
|
|
|
|
var.foo
|
2016-11-05 19:58:38 -05:00
|
|
|
openstack_floating_ip.random
|
|
|
|
provider.openstack
|
2017-07-01 11:48:37 -05:00
|
|
|
output.instance_id
|
|
|
|
local.instance_id
|
2016-11-05 19:58:38 -05:00
|
|
|
provider.aws
|
|
|
|
openstack_floating_ip.random
|
2017-04-12 16:25:15 -05:00
|
|
|
provider.aws (close)
|
|
|
|
aws_instance.web
|
|
|
|
aws_load_balancer.weblb
|
|
|
|
aws_security_group.firewall
|
|
|
|
provider.aws
|
2016-11-05 19:58:38 -05:00
|
|
|
provider.openstack
|
2017-04-12 16:25:15 -05:00
|
|
|
provider.openstack (close)
|
|
|
|
openstack_floating_ip.random
|
|
|
|
provider.openstack
|
|
|
|
root
|
2018-09-07 18:58:32 -05:00
|
|
|
meta.count-boundary (EachMode fixup)
|
2017-04-12 16:25:15 -05:00
|
|
|
provider.aws (close)
|
|
|
|
provider.openstack (close)
|
2016-11-05 19:58:38 -05:00
|
|
|
var.foo
|
|
|
|
`
|