github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/terraform/transform_targets_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  )
     7  
     8  func TestTargetsTransformer(t *testing.T) {
     9  	mod := testModule(t, "transform-targets-basic")
    10  
    11  	g := Graph{Path: RootModulePath}
    12  	{
    13  		tf := &ConfigTransformer{Module: mod}
    14  		if err := tf.Transform(&g); err != nil {
    15  			t.Fatalf("err: %s", err)
    16  		}
    17  	}
    18  
    19  	{
    20  		transform := &AttachResourceConfigTransformer{Module: mod}
    21  		if err := transform.Transform(&g); err != nil {
    22  			t.Fatalf("err: %s", err)
    23  		}
    24  	}
    25  
    26  	{
    27  		transform := &ReferenceTransformer{}
    28  		if err := transform.Transform(&g); err != nil {
    29  			t.Fatalf("err: %s", err)
    30  		}
    31  	}
    32  
    33  	{
    34  		transform := &TargetsTransformer{Targets: []string{"aws_instance.me"}}
    35  		if err := transform.Transform(&g); err != nil {
    36  			t.Fatalf("err: %s", err)
    37  		}
    38  	}
    39  
    40  	actual := strings.TrimSpace(g.String())
    41  	expected := strings.TrimSpace(`
    42  aws_instance.me
    43    aws_subnet.me
    44  aws_subnet.me
    45    aws_vpc.me
    46  aws_vpc.me
    47  	`)
    48  	if actual != expected {
    49  		t.Fatalf("bad:\n\nexpected:\n%s\n\ngot:\n%s\n", expected, actual)
    50  	}
    51  }
    52  
    53  func TestTargetsTransformer_destroy(t *testing.T) {
    54  	mod := testModule(t, "transform-targets-destroy")
    55  
    56  	g := Graph{Path: RootModulePath}
    57  	{
    58  		tf := &ConfigTransformer{Module: mod}
    59  		if err := tf.Transform(&g); err != nil {
    60  			t.Fatalf("err: %s", err)
    61  		}
    62  	}
    63  
    64  	{
    65  		transform := &AttachResourceConfigTransformer{Module: mod}
    66  		if err := transform.Transform(&g); err != nil {
    67  			t.Fatalf("err: %s", err)
    68  		}
    69  	}
    70  
    71  	{
    72  		transform := &ReferenceTransformer{}
    73  		if err := transform.Transform(&g); err != nil {
    74  			t.Fatalf("err: %s", err)
    75  		}
    76  	}
    77  
    78  	{
    79  		transform := &TargetsTransformer{
    80  			Targets: []string{"aws_instance.me"},
    81  			Destroy: true,
    82  		}
    83  		if err := transform.Transform(&g); err != nil {
    84  			t.Fatalf("err: %s", err)
    85  		}
    86  	}
    87  
    88  	actual := strings.TrimSpace(g.String())
    89  	expected := strings.TrimSpace(`
    90  aws_elb.me
    91    aws_instance.me
    92  aws_instance.me
    93  aws_instance.metoo
    94    aws_instance.me
    95  	`)
    96  	if actual != expected {
    97  		t.Fatalf("bad:\n\nexpected:\n%s\n\ngot:\n%s\n", expected, actual)
    98  	}
    99  }