github.com/hobbeswalsh/terraform@v0.3.7-0.20150619183303-ad17cf55a0fa/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  		Validate: true,
    45  	}
    46  
    47  	_, err := b.Build(RootModulePath)
    48  	if err == nil {
    49  		t.Fatal("should error")
    50  	}
    51  }
    52  
    53  func TestBasicGraphBuilder_validateOff(t *testing.T) {
    54  	b := &BasicGraphBuilder{
    55  		Steps: []GraphTransformer{
    56  			&testBasicGraphBuilderTransform{1},
    57  			&testBasicGraphBuilderTransform{2},
    58  		},
    59  		Validate: false,
    60  	}
    61  
    62  	_, err := b.Build(RootModulePath)
    63  	if err != nil {
    64  		t.Fatalf("expected no error, got: %s", err)
    65  	}
    66  }
    67  
    68  func TestBuiltinGraphBuilder_impl(t *testing.T) {
    69  	var _ GraphBuilder = new(BuiltinGraphBuilder)
    70  }
    71  
    72  // This test is not meant to test all the transforms but rather just
    73  // to verify we get some basic sane graph out. Special tests to ensure
    74  // specific ordering of steps should be added in other tests.
    75  func TestBuiltinGraphBuilder(t *testing.T) {
    76  	b := &BuiltinGraphBuilder{
    77  		Root:     testModule(t, "graph-builder-basic"),
    78  		Validate: true,
    79  	}
    80  
    81  	g, err := b.Build(RootModulePath)
    82  	if err != nil {
    83  		t.Fatalf("err: %s", err)
    84  	}
    85  
    86  	actual := strings.TrimSpace(g.String())
    87  	expected := strings.TrimSpace(testBuiltinGraphBuilderBasicStr)
    88  	if actual != expected {
    89  		t.Fatalf("bad: %s", actual)
    90  	}
    91  }
    92  
    93  func TestBuiltinGraphBuilder_Verbose(t *testing.T) {
    94  	b := &BuiltinGraphBuilder{
    95  		Root:     testModule(t, "graph-builder-basic"),
    96  		Validate: true,
    97  		Verbose:  true,
    98  	}
    99  
   100  	g, err := b.Build(RootModulePath)
   101  	if err != nil {
   102  		t.Fatalf("err: %s", err)
   103  	}
   104  
   105  	actual := strings.TrimSpace(g.String())
   106  	expected := strings.TrimSpace(testBuiltinGraphBuilderVerboseStr)
   107  	if actual != expected {
   108  		t.Fatalf("bad: %s", actual)
   109  	}
   110  }
   111  
   112  // This tests a cycle we got when a CBD resource depends on a non-CBD
   113  // resource. This cycle shouldn't happen in the general case anymore.
   114  func TestBuiltinGraphBuilder_cbdDepNonCbd(t *testing.T) {
   115  	b := &BuiltinGraphBuilder{
   116  		Root:     testModule(t, "graph-builder-cbd-non-cbd"),
   117  		Validate: true,
   118  	}
   119  
   120  	_, err := b.Build(RootModulePath)
   121  	if err != nil {
   122  		t.Fatalf("err: %s", err)
   123  	}
   124  }
   125  
   126  func TestBuiltinGraphBuilder_cbdDepNonCbd_errorsWhenVerbose(t *testing.T) {
   127  	b := &BuiltinGraphBuilder{
   128  		Root:     testModule(t, "graph-builder-cbd-non-cbd"),
   129  		Validate: true,
   130  		Verbose:  true,
   131  	}
   132  
   133  	_, err := b.Build(RootModulePath)
   134  	if err == nil {
   135  		t.Fatalf("expected err, got none")
   136  	}
   137  }
   138  
   139  func TestBuiltinGraphBuilder_multiLevelModule(t *testing.T) {
   140  	b := &BuiltinGraphBuilder{
   141  		Root:     testModule(t, "graph-builder-multi-level-module"),
   142  		Validate: true,
   143  	}
   144  
   145  	g, err := b.Build(RootModulePath)
   146  	if err != nil {
   147  		t.Fatalf("err: %s", err)
   148  	}
   149  
   150  	actual := strings.TrimSpace(g.String())
   151  	expected := strings.TrimSpace(testBuiltinGraphBuilderMultiLevelStr)
   152  	if actual != expected {
   153  		t.Fatalf("bad: %s", actual)
   154  	}
   155  }
   156  
   157  /*
   158  TODO: This exposes a really bad bug we need to fix after we merge
   159  the f-ast-branch. This bug still exists in master.
   160  
   161  // This test tests that the graph builder properly expands modules.
   162  func TestBuiltinGraphBuilder_modules(t *testing.T) {
   163  	b := &BuiltinGraphBuilder{
   164  		Root: testModule(t, "graph-builder-modules"),
   165  	}
   166  
   167  	g, err := b.Build(RootModulePath)
   168  	if err != nil {
   169  		t.Fatalf("err: %s", err)
   170  	}
   171  
   172  	actual := strings.TrimSpace(g.String())
   173  	expected := strings.TrimSpace(testBuiltinGraphBuilderModuleStr)
   174  	if actual != expected {
   175  		t.Fatalf("bad: %s", actual)
   176  	}
   177  }
   178  */
   179  
   180  type testBasicGraphBuilderTransform struct {
   181  	V dag.Vertex
   182  }
   183  
   184  func (t *testBasicGraphBuilderTransform) Transform(g *Graph) error {
   185  	g.Add(t.V)
   186  	return nil
   187  }
   188  
   189  const testBasicGraphBuilderStr = `
   190  1
   191  `
   192  
   193  const testBuiltinGraphBuilderBasicStr = `
   194  aws_instance.db
   195    provider.aws
   196  aws_instance.web
   197    aws_instance.db
   198  provider.aws
   199  `
   200  
   201  const testBuiltinGraphBuilderVerboseStr = `
   202  aws_instance.db
   203    aws_instance.db (destroy tainted)
   204    aws_instance.db (destroy)
   205  aws_instance.db (destroy tainted)
   206    aws_instance.web (destroy tainted)
   207  aws_instance.db (destroy)
   208    aws_instance.web (destroy)
   209  aws_instance.web
   210    aws_instance.db
   211  aws_instance.web (destroy tainted)
   212    provider.aws
   213  aws_instance.web (destroy)
   214    provider.aws
   215  provider.aws
   216  `
   217  
   218  const testBuiltinGraphBuilderModuleStr = `
   219  aws_instance.web
   220    aws_instance.web (destroy)
   221  aws_instance.web (destroy)
   222    aws_security_group.firewall
   223    module.consul (expanded)
   224    provider.aws
   225  aws_security_group.firewall
   226    aws_security_group.firewall (destroy)
   227  aws_security_group.firewall (destroy)
   228    provider.aws
   229  module.consul (expanded)
   230    aws_security_group.firewall
   231    provider.aws
   232  provider.aws
   233  `
   234  
   235  const testBuiltinGraphBuilderMultiLevelStr = `
   236  module.foo.module.bar.output.value
   237    module.foo.module.bar.var.bar
   238  module.foo.module.bar.plan-destroy
   239  module.foo.module.bar.var.bar
   240    module.foo.var.foo
   241  module.foo.plan-destroy
   242  module.foo.var.foo
   243  root
   244    module.foo.module.bar.output.value
   245    module.foo.module.bar.plan-destroy
   246    module.foo.plan-destroy
   247  `