github.com/nicgrayson/terraform@v0.4.3-0.20150415203910-c4de50829380/terraform/graph_builder_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"reflect"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/dag"
     9  )
    10  
    11  func TestBasicGraphBuilder_impl(t *testing.T) {
    12  	var _ GraphBuilder = new(BasicGraphBuilder)
    13  }
    14  
    15  func TestBasicGraphBuilder(t *testing.T) {
    16  	b := &BasicGraphBuilder{
    17  		Steps: []GraphTransformer{
    18  			&testBasicGraphBuilderTransform{1},
    19  		},
    20  	}
    21  
    22  	g, err := b.Build(RootModulePath)
    23  	if err != nil {
    24  		t.Fatalf("err: %s", err)
    25  	}
    26  
    27  	if !reflect.DeepEqual(g.Path, RootModulePath) {
    28  		t.Fatalf("bad: %#v", g.Path)
    29  	}
    30  
    31  	actual := strings.TrimSpace(g.String())
    32  	expected := strings.TrimSpace(testBasicGraphBuilderStr)
    33  	if actual != expected {
    34  		t.Fatalf("bad: %s", actual)
    35  	}
    36  }
    37  
    38  func TestBasicGraphBuilder_validate(t *testing.T) {
    39  	b := &BasicGraphBuilder{
    40  		Steps: []GraphTransformer{
    41  			&testBasicGraphBuilderTransform{1},
    42  			&testBasicGraphBuilderTransform{2},
    43  		},
    44  	}
    45  
    46  	_, err := b.Build(RootModulePath)
    47  	if err == nil {
    48  		t.Fatal("should error")
    49  	}
    50  }
    51  
    52  func TestBuiltinGraphBuilder_impl(t *testing.T) {
    53  	var _ GraphBuilder = new(BuiltinGraphBuilder)
    54  }
    55  
    56  // This test is not meant to test all the transforms but rather just
    57  // to verify we get some basic sane graph out. Special tests to ensure
    58  // specific ordering of steps should be added in other tests.
    59  func TestBuiltinGraphBuilder(t *testing.T) {
    60  	b := &BuiltinGraphBuilder{
    61  		Root: testModule(t, "graph-builder-basic"),
    62  	}
    63  
    64  	g, err := b.Build(RootModulePath)
    65  	if err != nil {
    66  		t.Fatalf("err: %s", err)
    67  	}
    68  
    69  	actual := strings.TrimSpace(g.String())
    70  	expected := strings.TrimSpace(testBuiltinGraphBuilderBasicStr)
    71  	if actual != expected {
    72  		t.Fatalf("bad: %s", actual)
    73  	}
    74  }
    75  
    76  // This tests a cycle we got when a CBD resource depends on a non-CBD
    77  // resource. This cycle shouldn't happen in the general case anymore.
    78  func TestBuiltinGraphBuilder_cbdDepNonCbd(t *testing.T) {
    79  	b := &BuiltinGraphBuilder{
    80  		Root: testModule(t, "graph-builder-cbd-non-cbd"),
    81  	}
    82  
    83  	_, err := b.Build(RootModulePath)
    84  	if err != nil {
    85  		t.Fatalf("err: %s", err)
    86  	}
    87  }
    88  
    89  /*
    90  TODO: This exposes a really bad bug we need to fix after we merge
    91  the f-ast-branch. This bug still exists in master.
    92  
    93  // This test tests that the graph builder properly expands modules.
    94  func TestBuiltinGraphBuilder_modules(t *testing.T) {
    95  	b := &BuiltinGraphBuilder{
    96  		Root: testModule(t, "graph-builder-modules"),
    97  	}
    98  
    99  	g, err := b.Build(RootModulePath)
   100  	if err != nil {
   101  		t.Fatalf("err: %s", err)
   102  	}
   103  
   104  	actual := strings.TrimSpace(g.String())
   105  	expected := strings.TrimSpace(testBuiltinGraphBuilderModuleStr)
   106  	if actual != expected {
   107  		t.Fatalf("bad: %s", actual)
   108  	}
   109  }
   110  */
   111  
   112  type testBasicGraphBuilderTransform struct {
   113  	V dag.Vertex
   114  }
   115  
   116  func (t *testBasicGraphBuilderTransform) Transform(g *Graph) error {
   117  	g.Add(t.V)
   118  	return nil
   119  }
   120  
   121  const testBasicGraphBuilderStr = `
   122  1
   123  `
   124  
   125  const testBuiltinGraphBuilderBasicStr = `
   126  aws_instance.db
   127    provider.aws
   128  aws_instance.web
   129    aws_instance.db
   130  provider.aws
   131  `
   132  
   133  const testBuiltinGraphBuilderModuleStr = `
   134  aws_instance.web
   135    aws_instance.web (destroy)
   136  aws_instance.web (destroy)
   137    aws_security_group.firewall
   138    module.consul (expanded)
   139    provider.aws
   140  aws_security_group.firewall
   141    aws_security_group.firewall (destroy)
   142  aws_security_group.firewall (destroy)
   143    provider.aws
   144  module.consul (expanded)
   145    aws_security_group.firewall
   146    provider.aws
   147  provider.aws
   148  `