github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/terraform/transform_config_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"path/filepath"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/config/module"
     9  )
    10  
    11  func TestConfigTransformer_nilModule(t *testing.T) {
    12  	g := Graph{Path: RootModulePath}
    13  	tf := &ConfigTransformer{}
    14  	if err := tf.Transform(&g); err == nil {
    15  		t.Fatal("should error")
    16  	}
    17  }
    18  
    19  func TestConfigTransformer_unloadedModule(t *testing.T) {
    20  	mod, err := module.NewTreeModule(
    21  		"", filepath.Join(fixtureDir, "graph-basic"))
    22  	if err != nil {
    23  		t.Fatalf("err: %s", err)
    24  	}
    25  
    26  	g := Graph{Path: RootModulePath}
    27  	tf := &ConfigTransformer{Module: mod}
    28  	if err := tf.Transform(&g); err == nil {
    29  		t.Fatal("should error")
    30  	}
    31  }
    32  
    33  func TestConfigTransformer(t *testing.T) {
    34  	g := Graph{Path: RootModulePath}
    35  	tf := &ConfigTransformer{Module: testModule(t, "graph-basic")}
    36  	if err := tf.Transform(&g); err != nil {
    37  		t.Fatalf("err: %s", err)
    38  	}
    39  
    40  	actual := strings.TrimSpace(g.String())
    41  	expected := strings.TrimSpace(testGraphBasicStr)
    42  	if actual != expected {
    43  		t.Fatalf("bad:\n\n%s", actual)
    44  	}
    45  }
    46  
    47  func TestConfigTransformer_dependsOn(t *testing.T) {
    48  	g := Graph{Path: RootModulePath}
    49  	tf := &ConfigTransformer{Module: testModule(t, "graph-depends-on")}
    50  	if err := tf.Transform(&g); err != nil {
    51  		t.Fatalf("err: %s", err)
    52  	}
    53  
    54  	actual := strings.TrimSpace(g.String())
    55  	expected := strings.TrimSpace(testGraphDependsOnStr)
    56  	if actual != expected {
    57  		t.Fatalf("bad:\n\n%s", actual)
    58  	}
    59  }
    60  
    61  func TestConfigTransformer_modules(t *testing.T) {
    62  	g := Graph{Path: RootModulePath}
    63  	tf := &ConfigTransformer{Module: testModule(t, "graph-modules")}
    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(testGraphModulesStr)
    70  	if actual != expected {
    71  		t.Fatalf("bad:\n\n%s", actual)
    72  	}
    73  }
    74  
    75  func TestConfigTransformer_outputs(t *testing.T) {
    76  	g := Graph{Path: RootModulePath}
    77  	tf := &ConfigTransformer{Module: testModule(t, "graph-outputs")}
    78  	if err := tf.Transform(&g); err != nil {
    79  		t.Fatalf("err: %s", err)
    80  	}
    81  
    82  	actual := strings.TrimSpace(g.String())
    83  	expected := strings.TrimSpace(testGraphOutputsStr)
    84  	if actual != expected {
    85  		t.Fatalf("bad:\n\n%s", actual)
    86  	}
    87  }
    88  
    89  func TestConfigTransformer_providerAlias(t *testing.T) {
    90  	g := Graph{Path: RootModulePath}
    91  	tf := &ConfigTransformer{Module: testModule(t, "graph-provider-alias")}
    92  	if err := tf.Transform(&g); err != nil {
    93  		t.Fatalf("err: %s", err)
    94  	}
    95  
    96  	actual := strings.TrimSpace(g.String())
    97  	expected := strings.TrimSpace(testGraphProviderAliasStr)
    98  	if actual != expected {
    99  		t.Fatalf("bad:\n\n%s", actual)
   100  	}
   101  }
   102  
   103  func TestConfigTransformer_errMissingDeps(t *testing.T) {
   104  	g := Graph{Path: RootModulePath}
   105  	tf := &ConfigTransformer{Module: testModule(t, "graph-missing-deps")}
   106  	if err := tf.Transform(&g); err == nil {
   107  		t.Fatalf("err: %s", err)
   108  	}
   109  }
   110  
   111  const testGraphBasicStr = `
   112  aws_instance.web
   113    aws_security_group.firewall
   114    var.foo
   115  aws_load_balancer.weblb
   116    aws_instance.web
   117  aws_security_group.firewall
   118  openstack_floating_ip.random
   119  provider.aws
   120    openstack_floating_ip.random
   121  var.foo
   122  `
   123  
   124  const testGraphDependsOnStr = `
   125  aws_instance.db
   126    aws_instance.web
   127  aws_instance.web
   128  `
   129  
   130  const testGraphModulesStr = `
   131  aws_instance.web
   132    aws_security_group.firewall
   133    module.consul
   134  aws_security_group.firewall
   135  module.consul
   136    aws_security_group.firewall
   137  provider.aws
   138  `
   139  
   140  const testGraphOutputsStr = `
   141  aws_instance.foo
   142  output.foo
   143    aws_instance.foo
   144  `
   145  
   146  const testGraphProviderAliasStr = `
   147  provider.aws
   148  provider.aws.bar
   149  provider.aws.foo
   150  `