github.com/aspring/terraform@v0.8.2-0.20161216122603-6a8619a5db2e/terraform/graph_builder_apply_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"reflect"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestApplyGraphBuilder_impl(t *testing.T) {
    10  	var _ GraphBuilder = new(ApplyGraphBuilder)
    11  }
    12  
    13  func TestApplyGraphBuilder(t *testing.T) {
    14  	diff := &Diff{
    15  		Modules: []*ModuleDiff{
    16  			&ModuleDiff{
    17  				Path: []string{"root"},
    18  				Resources: map[string]*InstanceDiff{
    19  					// Verify noop doesn't show up in graph
    20  					"aws_instance.noop": &InstanceDiff{},
    21  
    22  					"aws_instance.create": &InstanceDiff{
    23  						Attributes: map[string]*ResourceAttrDiff{
    24  							"name": &ResourceAttrDiff{
    25  								Old: "",
    26  								New: "foo",
    27  							},
    28  						},
    29  					},
    30  
    31  					"aws_instance.other": &InstanceDiff{
    32  						Attributes: map[string]*ResourceAttrDiff{
    33  							"name": &ResourceAttrDiff{
    34  								Old: "",
    35  								New: "foo",
    36  							},
    37  						},
    38  					},
    39  				},
    40  			},
    41  
    42  			&ModuleDiff{
    43  				Path: []string{"root", "child"},
    44  				Resources: map[string]*InstanceDiff{
    45  					"aws_instance.create": &InstanceDiff{
    46  						Attributes: map[string]*ResourceAttrDiff{
    47  							"name": &ResourceAttrDiff{
    48  								Old: "",
    49  								New: "foo",
    50  							},
    51  						},
    52  					},
    53  
    54  					"aws_instance.other": &InstanceDiff{
    55  						Attributes: map[string]*ResourceAttrDiff{
    56  							"name": &ResourceAttrDiff{
    57  								Old: "",
    58  								New: "foo",
    59  							},
    60  						},
    61  					},
    62  				},
    63  			},
    64  		},
    65  	}
    66  
    67  	b := &ApplyGraphBuilder{
    68  		Module:        testModule(t, "graph-builder-apply-basic"),
    69  		Diff:          diff,
    70  		Providers:     []string{"aws"},
    71  		Provisioners:  []string{"exec"},
    72  		DisableReduce: true,
    73  	}
    74  
    75  	g, err := b.Build(RootModulePath)
    76  	if err != nil {
    77  		t.Fatalf("err: %s", err)
    78  	}
    79  
    80  	if !reflect.DeepEqual(g.Path, RootModulePath) {
    81  		t.Fatalf("bad: %#v", g.Path)
    82  	}
    83  
    84  	actual := strings.TrimSpace(g.String())
    85  	expected := strings.TrimSpace(testApplyGraphBuilderStr)
    86  	if actual != expected {
    87  		t.Fatalf("bad: %s", actual)
    88  	}
    89  }
    90  
    91  // This tests the ordering of two resources that are both CBD that
    92  // require destroy/create.
    93  func TestApplyGraphBuilder_doubleCBD(t *testing.T) {
    94  	diff := &Diff{
    95  		Modules: []*ModuleDiff{
    96  			&ModuleDiff{
    97  				Path: []string{"root"},
    98  				Resources: map[string]*InstanceDiff{
    99  					"aws_instance.A": &InstanceDiff{
   100  						Destroy: true,
   101  						Attributes: map[string]*ResourceAttrDiff{
   102  							"name": &ResourceAttrDiff{
   103  								Old: "",
   104  								New: "foo",
   105  							},
   106  						},
   107  					},
   108  
   109  					"aws_instance.B": &InstanceDiff{
   110  						Destroy: true,
   111  						Attributes: map[string]*ResourceAttrDiff{
   112  							"name": &ResourceAttrDiff{
   113  								Old: "",
   114  								New: "foo",
   115  							},
   116  						},
   117  					},
   118  				},
   119  			},
   120  		},
   121  	}
   122  
   123  	b := &ApplyGraphBuilder{
   124  		Module:        testModule(t, "graph-builder-apply-double-cbd"),
   125  		Diff:          diff,
   126  		Providers:     []string{"aws"},
   127  		Provisioners:  []string{"exec"},
   128  		DisableReduce: true,
   129  	}
   130  
   131  	g, err := b.Build(RootModulePath)
   132  	if err != nil {
   133  		t.Fatalf("err: %s", err)
   134  	}
   135  
   136  	if !reflect.DeepEqual(g.Path, RootModulePath) {
   137  		t.Fatalf("bad: %#v", g.Path)
   138  	}
   139  
   140  	actual := strings.TrimSpace(g.String())
   141  	expected := strings.TrimSpace(testApplyGraphBuilderDoubleCBDStr)
   142  	if actual != expected {
   143  		t.Fatalf("bad: %s", actual)
   144  	}
   145  }
   146  
   147  // This tests the ordering of destroying a single count of a resource.
   148  func TestApplyGraphBuilder_destroyCount(t *testing.T) {
   149  	diff := &Diff{
   150  		Modules: []*ModuleDiff{
   151  			&ModuleDiff{
   152  				Path: []string{"root"},
   153  				Resources: map[string]*InstanceDiff{
   154  					"aws_instance.A.1": &InstanceDiff{
   155  						Destroy: true,
   156  					},
   157  
   158  					"aws_instance.B": &InstanceDiff{
   159  						Attributes: map[string]*ResourceAttrDiff{
   160  							"name": &ResourceAttrDiff{
   161  								Old: "",
   162  								New: "foo",
   163  							},
   164  						},
   165  					},
   166  				},
   167  			},
   168  		},
   169  	}
   170  
   171  	b := &ApplyGraphBuilder{
   172  		Module:        testModule(t, "graph-builder-apply-count"),
   173  		Diff:          diff,
   174  		Providers:     []string{"aws"},
   175  		Provisioners:  []string{"exec"},
   176  		DisableReduce: true,
   177  	}
   178  
   179  	g, err := b.Build(RootModulePath)
   180  	if err != nil {
   181  		t.Fatalf("err: %s", err)
   182  	}
   183  
   184  	if !reflect.DeepEqual(g.Path, RootModulePath) {
   185  		t.Fatalf("bad: %#v", g.Path)
   186  	}
   187  
   188  	actual := strings.TrimSpace(g.String())
   189  	expected := strings.TrimSpace(testApplyGraphBuilderDestroyCountStr)
   190  	if actual != expected {
   191  		t.Fatalf("bad: %s", actual)
   192  	}
   193  }
   194  
   195  func TestApplyGraphBuilder_moduleDestroy(t *testing.T) {
   196  	diff := &Diff{
   197  		Modules: []*ModuleDiff{
   198  			&ModuleDiff{
   199  				Path: []string{"root", "A"},
   200  				Resources: map[string]*InstanceDiff{
   201  					"null_resource.foo": &InstanceDiff{
   202  						Destroy: true,
   203  					},
   204  				},
   205  			},
   206  
   207  			&ModuleDiff{
   208  				Path: []string{"root", "B"},
   209  				Resources: map[string]*InstanceDiff{
   210  					"null_resource.foo": &InstanceDiff{
   211  						Destroy: true,
   212  					},
   213  				},
   214  			},
   215  		},
   216  	}
   217  
   218  	b := &ApplyGraphBuilder{
   219  		Module:    testModule(t, "graph-builder-apply-module-destroy"),
   220  		Diff:      diff,
   221  		Providers: []string{"null"},
   222  	}
   223  
   224  	g, err := b.Build(RootModulePath)
   225  	if err != nil {
   226  		t.Fatalf("err: %s", err)
   227  	}
   228  
   229  	testGraphHappensBefore(
   230  		t, g,
   231  		"module.B.null_resource.foo (destroy)",
   232  		"module.A.null_resource.foo (destroy)")
   233  }
   234  
   235  const testApplyGraphBuilderStr = `
   236  aws_instance.create
   237    provider.aws
   238  aws_instance.other
   239    aws_instance.create
   240    provider.aws
   241  meta.count-boundary (count boundary fixup)
   242    aws_instance.create
   243    aws_instance.other
   244    module.child.aws_instance.create
   245    module.child.aws_instance.other
   246    module.child.provider.aws
   247    module.child.provisioner.exec
   248    provider.aws
   249  module.child.aws_instance.create
   250    module.child.provider.aws
   251    module.child.provisioner.exec
   252  module.child.aws_instance.other
   253    module.child.aws_instance.create
   254    module.child.provider.aws
   255  module.child.provider.aws
   256    provider.aws
   257  module.child.provisioner.exec
   258  provider.aws
   259  `
   260  
   261  const testApplyGraphBuilderDoubleCBDStr = `
   262  aws_instance.A
   263    provider.aws
   264  aws_instance.A (destroy)
   265    aws_instance.A
   266    aws_instance.B
   267    aws_instance.B (destroy)
   268    provider.aws
   269  aws_instance.B
   270    aws_instance.A
   271    provider.aws
   272  aws_instance.B (destroy)
   273    aws_instance.B
   274    provider.aws
   275  meta.count-boundary (count boundary fixup)
   276    aws_instance.A
   277    aws_instance.A (destroy)
   278    aws_instance.B
   279    aws_instance.B (destroy)
   280    provider.aws
   281  provider.aws
   282  `
   283  
   284  const testApplyGraphBuilderDestroyCountStr = `
   285  aws_instance.A[1] (destroy)
   286    provider.aws
   287  aws_instance.B
   288    aws_instance.A[1] (destroy)
   289    provider.aws
   290  meta.count-boundary (count boundary fixup)
   291    aws_instance.A[1] (destroy)
   292    aws_instance.B
   293    provider.aws
   294  provider.aws
   295  `