2015-03-24 17:18:15 +01:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2018-05-05 04:24:06 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2015-03-24 17:18:15 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTargetsTransformer(t *testing.T) {
|
|
|
|
mod := testModule(t, "transform-targets-basic")
|
|
|
|
|
2018-05-05 04:24:06 +02:00
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
2015-03-24 17:18:15 +01:00
|
|
|
{
|
2018-05-05 04:24:06 +02:00
|
|
|
tf := &ConfigTransformer{Config: mod}
|
2015-03-24 17:18:15 +01:00
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-27 04:56:16 +01:00
|
|
|
{
|
2018-05-05 04:24:06 +02:00
|
|
|
transform := &AttachResourceConfigTransformer{Config: mod}
|
2017-01-27 04:56:16 +01:00
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
transform := &ReferenceTransformer{}
|
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-24 17:18:15 +01:00
|
|
|
{
|
2018-05-05 04:24:06 +02:00
|
|
|
transform := &TargetsTransformer{
|
|
|
|
Targets: []addrs.Targetable{
|
|
|
|
addrs.RootModuleInstance.Resource(
|
|
|
|
addrs.ManagedResourceMode, "aws_instance", "me",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
}
|
2015-03-24 17:18:15 +01:00
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(`
|
|
|
|
aws_instance.me
|
|
|
|
aws_subnet.me
|
|
|
|
aws_subnet.me
|
|
|
|
aws_vpc.me
|
|
|
|
aws_vpc.me
|
|
|
|
`)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\nexpected:\n%s\n\ngot:\n%s\n", expected, actual)
|
2017-05-11 03:27:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTargetsTransformer_downstream(t *testing.T) {
|
|
|
|
mod := testModule(t, "transform-targets-downstream")
|
|
|
|
|
2018-05-05 04:24:06 +02:00
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
2017-05-11 03:27:49 +02:00
|
|
|
{
|
2018-05-05 04:24:06 +02:00
|
|
|
transform := &ConfigTransformer{Config: mod}
|
2017-05-11 03:27:49 +02:00
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("%T failed: %s", transform, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-05-05 04:24:06 +02:00
|
|
|
transform := &AttachResourceConfigTransformer{Config: mod}
|
2017-05-11 03:27:49 +02:00
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("%T failed: %s", transform, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-05-05 04:24:06 +02:00
|
|
|
transform := &AttachResourceConfigTransformer{Config: mod}
|
2017-05-11 03:27:49 +02:00
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("%T failed: %s", transform, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-05-05 04:24:06 +02:00
|
|
|
transform := &OutputTransformer{Config: mod}
|
2017-05-11 03:27:49 +02:00
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("%T failed: %s", transform, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
transform := &ReferenceTransformer{}
|
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-05-05 04:24:06 +02:00
|
|
|
transform := &TargetsTransformer{
|
|
|
|
Targets: []addrs.Targetable{
|
|
|
|
addrs.RootModuleInstance.
|
|
|
|
Child("child", addrs.NoKey).
|
|
|
|
Child("grandchild", addrs.NoKey).
|
|
|
|
Resource(
|
|
|
|
addrs.ManagedResourceMode, "aws_instance", "foo",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
}
|
2017-05-11 03:27:49 +02:00
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("%T failed: %s", transform, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
// Even though we only asked to target the grandchild resource, all of the
|
|
|
|
// outputs that descend from it are also targeted.
|
|
|
|
expected := strings.TrimSpace(`
|
|
|
|
module.child.module.grandchild.aws_instance.foo
|
|
|
|
module.child.module.grandchild.output.id
|
|
|
|
module.child.module.grandchild.aws_instance.foo
|
|
|
|
module.child.output.grandchild_id
|
|
|
|
module.child.module.grandchild.output.id
|
|
|
|
output.grandchild_id
|
|
|
|
module.child.output.grandchild_id
|
|
|
|
`)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\nexpected:\n%s\n\ngot:\n%s\n", expected, actual)
|
2015-03-24 17:18:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-06 22:07:57 +01:00
|
|
|
// This tests the TargetsTransformer targeting a whole module,
|
|
|
|
// rather than a resource within a module instance.
|
|
|
|
func TestTargetsTransformer_wholeModule(t *testing.T) {
|
|
|
|
mod := testModule(t, "transform-targets-downstream")
|
|
|
|
|
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
|
|
|
{
|
|
|
|
transform := &ConfigTransformer{Config: mod}
|
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("%T failed: %s", transform, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
transform := &AttachResourceConfigTransformer{Config: mod}
|
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("%T failed: %s", transform, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
transform := &AttachResourceConfigTransformer{Config: mod}
|
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("%T failed: %s", transform, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
transform := &OutputTransformer{Config: mod}
|
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("%T failed: %s", transform, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
transform := &ReferenceTransformer{}
|
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
transform := &TargetsTransformer{
|
|
|
|
Targets: []addrs.Targetable{
|
|
|
|
addrs.RootModule.
|
|
|
|
Child("child").
|
|
|
|
Child("grandchild"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("%T failed: %s", transform, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
// Even though we only asked to target the grandchild module, all of the
|
|
|
|
// outputs that descend from it are also targeted.
|
|
|
|
expected := strings.TrimSpace(`
|
|
|
|
module.child.module.grandchild.aws_instance.foo
|
|
|
|
module.child.module.grandchild.output.id
|
|
|
|
module.child.module.grandchild.aws_instance.foo
|
|
|
|
module.child.output.grandchild_id
|
|
|
|
module.child.module.grandchild.output.id
|
|
|
|
output.grandchild_id
|
|
|
|
module.child.output.grandchild_id
|
|
|
|
`)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\nexpected:\n%s\n\ngot:\n%s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-24 17:18:15 +01:00
|
|
|
func TestTargetsTransformer_destroy(t *testing.T) {
|
|
|
|
mod := testModule(t, "transform-targets-destroy")
|
|
|
|
|
2018-05-05 04:24:06 +02:00
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
2015-03-24 17:18:15 +01:00
|
|
|
{
|
2018-05-05 04:24:06 +02:00
|
|
|
tf := &ConfigTransformer{Config: mod}
|
2015-03-24 17:18:15 +01:00
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-27 04:56:16 +01:00
|
|
|
{
|
2018-05-05 04:24:06 +02:00
|
|
|
transform := &AttachResourceConfigTransformer{Config: mod}
|
2017-01-27 04:56:16 +01:00
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
transform := &ReferenceTransformer{}
|
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-24 17:18:15 +01:00
|
|
|
{
|
|
|
|
transform := &TargetsTransformer{
|
2018-05-05 04:24:06 +02:00
|
|
|
Targets: []addrs.Targetable{
|
|
|
|
addrs.RootModuleInstance.Resource(
|
|
|
|
addrs.ManagedResourceMode, "aws_instance", "me",
|
|
|
|
),
|
|
|
|
},
|
2015-03-24 17:18:15 +01:00
|
|
|
Destroy: true,
|
|
|
|
}
|
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(`
|
|
|
|
aws_elb.me
|
|
|
|
aws_instance.me
|
|
|
|
aws_instance.me
|
|
|
|
aws_instance.metoo
|
|
|
|
aws_instance.me
|
|
|
|
`)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\nexpected:\n%s\n\ngot:\n%s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|