github.com/hugorut/terraform@v1.1.3/src/terraform/transform_config_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/hugorut/terraform/src/addrs"
     8  )
     9  
    10  func TestConfigTransformer_nilModule(t *testing.T) {
    11  	g := Graph{Path: addrs.RootModuleInstance}
    12  	tf := &ConfigTransformer{}
    13  	if err := tf.Transform(&g); err != nil {
    14  		t.Fatalf("err: %s", err)
    15  	}
    16  
    17  	if len(g.Vertices()) > 0 {
    18  		t.Fatalf("graph is not empty: %s", g.String())
    19  	}
    20  }
    21  
    22  func TestConfigTransformer(t *testing.T) {
    23  	g := Graph{Path: addrs.RootModuleInstance}
    24  	tf := &ConfigTransformer{Config: testModule(t, "graph-basic")}
    25  	if err := tf.Transform(&g); err != nil {
    26  		t.Fatalf("err: %s", err)
    27  	}
    28  
    29  	actual := strings.TrimSpace(g.String())
    30  	expected := strings.TrimSpace(testConfigTransformerGraphBasicStr)
    31  	if actual != expected {
    32  		t.Fatalf("bad:\n\n%s", actual)
    33  	}
    34  }
    35  
    36  func TestConfigTransformer_mode(t *testing.T) {
    37  	g := Graph{Path: addrs.RootModuleInstance}
    38  	tf := &ConfigTransformer{
    39  		Config:     testModule(t, "transform-config-mode-data"),
    40  		ModeFilter: true,
    41  		Mode:       addrs.DataResourceMode,
    42  	}
    43  	if err := tf.Transform(&g); err != nil {
    44  		t.Fatalf("err: %s", err)
    45  	}
    46  
    47  	actual := strings.TrimSpace(g.String())
    48  	expected := strings.TrimSpace(`
    49  data.aws_ami.foo
    50  `)
    51  	if actual != expected {
    52  		t.Fatalf("bad:\n\n%s", actual)
    53  	}
    54  }
    55  
    56  func TestConfigTransformer_nonUnique(t *testing.T) {
    57  	g := Graph{Path: addrs.RootModuleInstance}
    58  	g.Add(NewNodeAbstractResource(
    59  		addrs.RootModule.Resource(
    60  			addrs.ManagedResourceMode, "aws_instance", "web",
    61  		),
    62  	))
    63  	tf := &ConfigTransformer{Config: testModule(t, "graph-basic")}
    64  	if err := tf.Transform(&g); err != nil {
    65  		t.Fatalf("err: %s", err)
    66  	}
    67  
    68  	actual := strings.TrimSpace(g.String())
    69  	expected := strings.TrimSpace(`
    70  aws_instance.web
    71  aws_instance.web
    72  aws_load_balancer.weblb
    73  aws_security_group.firewall
    74  openstack_floating_ip.random
    75  `)
    76  	if actual != expected {
    77  		t.Fatalf("bad:\n\n%s", actual)
    78  	}
    79  }
    80  
    81  const testConfigTransformerGraphBasicStr = `
    82  aws_instance.web
    83  aws_load_balancer.weblb
    84  aws_security_group.firewall
    85  openstack_floating_ip.random
    86  `