github.com/pbthorste/terraform@v0.8.6-0.20170127005045-deb56bd93da2/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  func TestApplyGraphBuilder_provisioner(t *testing.T) {
   236  	diff := &Diff{
   237  		Modules: []*ModuleDiff{
   238  			&ModuleDiff{
   239  				Path: []string{"root"},
   240  				Resources: map[string]*InstanceDiff{
   241  					"null_resource.foo": &InstanceDiff{
   242  						Attributes: map[string]*ResourceAttrDiff{
   243  							"name": &ResourceAttrDiff{
   244  								Old: "",
   245  								New: "foo",
   246  							},
   247  						},
   248  					},
   249  				},
   250  			},
   251  		},
   252  	}
   253  
   254  	b := &ApplyGraphBuilder{
   255  		Module:       testModule(t, "graph-builder-apply-provisioner"),
   256  		Diff:         diff,
   257  		Providers:    []string{"null"},
   258  		Provisioners: []string{"local"},
   259  	}
   260  
   261  	g, err := b.Build(RootModulePath)
   262  	if err != nil {
   263  		t.Fatalf("err: %s", err)
   264  	}
   265  
   266  	testGraphContains(t, g, "provisioner.local")
   267  	testGraphHappensBefore(
   268  		t, g,
   269  		"provisioner.local",
   270  		"null_resource.foo")
   271  }
   272  
   273  func TestApplyGraphBuilder_provisionerDestroy(t *testing.T) {
   274  	diff := &Diff{
   275  		Modules: []*ModuleDiff{
   276  			&ModuleDiff{
   277  				Path: []string{"root"},
   278  				Resources: map[string]*InstanceDiff{
   279  					"null_resource.foo": &InstanceDiff{
   280  						Destroy: true,
   281  					},
   282  				},
   283  			},
   284  		},
   285  	}
   286  
   287  	b := &ApplyGraphBuilder{
   288  		Destroy:      true,
   289  		Module:       testModule(t, "graph-builder-apply-provisioner"),
   290  		Diff:         diff,
   291  		Providers:    []string{"null"},
   292  		Provisioners: []string{"local"},
   293  	}
   294  
   295  	g, err := b.Build(RootModulePath)
   296  	if err != nil {
   297  		t.Fatalf("err: %s", err)
   298  	}
   299  
   300  	testGraphContains(t, g, "provisioner.local")
   301  	testGraphHappensBefore(
   302  		t, g,
   303  		"provisioner.local",
   304  		"null_resource.foo (destroy)")
   305  }
   306  
   307  const testApplyGraphBuilderStr = `
   308  aws_instance.create
   309    provider.aws
   310  aws_instance.other
   311    aws_instance.create
   312    provider.aws
   313  meta.count-boundary (count boundary fixup)
   314    aws_instance.create
   315    aws_instance.other
   316    module.child.aws_instance.create
   317    module.child.aws_instance.other
   318    module.child.provider.aws
   319    module.child.provisioner.exec
   320    provider.aws
   321  module.child.aws_instance.create
   322    module.child.provider.aws
   323    module.child.provisioner.exec
   324  module.child.aws_instance.other
   325    module.child.aws_instance.create
   326    module.child.provider.aws
   327  module.child.provider.aws
   328    provider.aws
   329  module.child.provisioner.exec
   330  provider.aws
   331  `
   332  
   333  const testApplyGraphBuilderDoubleCBDStr = `
   334  aws_instance.A
   335    provider.aws
   336  aws_instance.A (destroy)
   337    aws_instance.A
   338    aws_instance.B
   339    aws_instance.B (destroy)
   340    provider.aws
   341  aws_instance.B
   342    aws_instance.A
   343    provider.aws
   344  aws_instance.B (destroy)
   345    aws_instance.B
   346    provider.aws
   347  meta.count-boundary (count boundary fixup)
   348    aws_instance.A
   349    aws_instance.A (destroy)
   350    aws_instance.B
   351    aws_instance.B (destroy)
   352    provider.aws
   353  provider.aws
   354  `
   355  
   356  const testApplyGraphBuilderDestroyCountStr = `
   357  aws_instance.A[1] (destroy)
   358    provider.aws
   359  aws_instance.B
   360    aws_instance.A[1] (destroy)
   361    provider.aws
   362  meta.count-boundary (count boundary fixup)
   363    aws_instance.A[1] (destroy)
   364    aws_instance.B
   365    provider.aws
   366  provider.aws
   367  `