github.com/anuaimi/terraform@v0.6.4-0.20150904235404-2bf9aec61da8/terraform/transform_destroy_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  )
     7  
     8  func TestDestroyTransformer(t *testing.T) {
     9  	mod := testModule(t, "transform-destroy-basic")
    10  
    11  	g := Graph{Path: RootModulePath}
    12  	{
    13  		tf := &ConfigTransformer{Module: mod}
    14  		if err := tf.Transform(&g); err != nil {
    15  			t.Fatalf("err: %s", err)
    16  		}
    17  	}
    18  
    19  	{
    20  		tf := &DestroyTransformer{}
    21  		if err := tf.Transform(&g); err != nil {
    22  			t.Fatalf("err: %s", err)
    23  		}
    24  	}
    25  
    26  	actual := strings.TrimSpace(g.String())
    27  	expected := strings.TrimSpace(testTransformDestroyBasicStr)
    28  	if actual != expected {
    29  		t.Fatalf("bad:\n\n%s", actual)
    30  	}
    31  }
    32  
    33  func TestCreateBeforeDestroyTransformer(t *testing.T) {
    34  	mod := testModule(t, "transform-create-before-destroy-basic")
    35  
    36  	g := Graph{Path: RootModulePath}
    37  	{
    38  		tf := &ConfigTransformer{Module: mod}
    39  		if err := tf.Transform(&g); err != nil {
    40  			t.Fatalf("err: %s", err)
    41  		}
    42  	}
    43  
    44  	{
    45  		tf := &DestroyTransformer{}
    46  		if err := tf.Transform(&g); err != nil {
    47  			t.Fatalf("err: %s", err)
    48  		}
    49  	}
    50  
    51  	{
    52  		tf := &CreateBeforeDestroyTransformer{}
    53  		if err := tf.Transform(&g); err != nil {
    54  			t.Fatalf("err: %s", err)
    55  		}
    56  	}
    57  
    58  	actual := strings.TrimSpace(g.String())
    59  	expected := strings.TrimSpace(testTransformCreateBeforeDestroyBasicStr)
    60  	if actual != expected {
    61  		t.Fatalf("bad:\n\n%s", actual)
    62  	}
    63  }
    64  
    65  func TestCreateBeforeDestroyTransformer_twice(t *testing.T) {
    66  	mod := testModule(t, "transform-create-before-destroy-twice")
    67  
    68  	g := Graph{Path: RootModulePath}
    69  	{
    70  		tf := &ConfigTransformer{Module: mod}
    71  		if err := tf.Transform(&g); err != nil {
    72  			t.Fatalf("err: %s", err)
    73  		}
    74  	}
    75  
    76  	{
    77  		tf := &DestroyTransformer{}
    78  		if err := tf.Transform(&g); err != nil {
    79  			t.Fatalf("err: %s", err)
    80  		}
    81  	}
    82  
    83  	{
    84  		tf := &CreateBeforeDestroyTransformer{}
    85  		if err := tf.Transform(&g); err != nil {
    86  			t.Fatalf("err: %s", err)
    87  		}
    88  	}
    89  
    90  	actual := strings.TrimSpace(g.String())
    91  	expected := strings.TrimSpace(testTransformCreateBeforeDestroyTwiceStr)
    92  	if actual != expected {
    93  		t.Fatalf("bad:\n\n%s", actual)
    94  	}
    95  }
    96  
    97  func TestPruneDestroyTransformer(t *testing.T) {
    98  	var diff *Diff
    99  	mod := testModule(t, "transform-destroy-basic")
   100  
   101  	g := Graph{Path: RootModulePath}
   102  	{
   103  		tf := &ConfigTransformer{Module: mod}
   104  		if err := tf.Transform(&g); err != nil {
   105  			t.Fatalf("err: %s", err)
   106  		}
   107  	}
   108  
   109  	{
   110  		tf := &DestroyTransformer{}
   111  		if err := tf.Transform(&g); err != nil {
   112  			t.Fatalf("err: %s", err)
   113  		}
   114  	}
   115  
   116  	{
   117  		tf := &PruneDestroyTransformer{Diff: diff}
   118  		if err := tf.Transform(&g); err != nil {
   119  			t.Fatalf("err: %s", err)
   120  		}
   121  	}
   122  
   123  	actual := strings.TrimSpace(g.String())
   124  	expected := strings.TrimSpace(testTransformPruneDestroyBasicStr)
   125  	if actual != expected {
   126  		t.Fatalf("bad:\n\n%s", actual)
   127  	}
   128  }
   129  
   130  func TestPruneDestroyTransformer_diff(t *testing.T) {
   131  	mod := testModule(t, "transform-destroy-basic")
   132  
   133  	diff := &Diff{
   134  		Modules: []*ModuleDiff{
   135  			&ModuleDiff{
   136  				Path: RootModulePath,
   137  				Resources: map[string]*InstanceDiff{
   138  					"aws_instance.bar": &InstanceDiff{},
   139  				},
   140  			},
   141  		},
   142  	}
   143  
   144  	g := Graph{Path: RootModulePath}
   145  	{
   146  		tf := &ConfigTransformer{Module: mod}
   147  		if err := tf.Transform(&g); err != nil {
   148  			t.Fatalf("err: %s", err)
   149  		}
   150  	}
   151  
   152  	{
   153  		tf := &DestroyTransformer{}
   154  		if err := tf.Transform(&g); err != nil {
   155  			t.Fatalf("err: %s", err)
   156  		}
   157  	}
   158  
   159  	{
   160  		tf := &PruneDestroyTransformer{Diff: diff}
   161  		if err := tf.Transform(&g); err != nil {
   162  			t.Fatalf("err: %s", err)
   163  		}
   164  	}
   165  
   166  	actual := strings.TrimSpace(g.String())
   167  	expected := strings.TrimSpace(testTransformPruneDestroyBasicDiffStr)
   168  	if actual != expected {
   169  		t.Fatalf("expected:\n\n%s\n\nbad:\n\n%s", expected, actual)
   170  	}
   171  }
   172  
   173  func TestPruneDestroyTransformer_count(t *testing.T) {
   174  	mod := testModule(t, "transform-destroy-prune-count")
   175  
   176  	diff := &Diff{}
   177  
   178  	g := Graph{Path: RootModulePath}
   179  	{
   180  		tf := &ConfigTransformer{Module: mod}
   181  		if err := tf.Transform(&g); err != nil {
   182  			t.Fatalf("err: %s", err)
   183  		}
   184  	}
   185  
   186  	{
   187  		tf := &DestroyTransformer{}
   188  		if err := tf.Transform(&g); err != nil {
   189  			t.Fatalf("err: %s", err)
   190  		}
   191  	}
   192  
   193  	{
   194  		tf := &PruneDestroyTransformer{Diff: diff}
   195  		if err := tf.Transform(&g); err != nil {
   196  			t.Fatalf("err: %s", err)
   197  		}
   198  	}
   199  
   200  	actual := strings.TrimSpace(g.String())
   201  	expected := strings.TrimSpace(testTransformPruneDestroyCountStr)
   202  	if actual != expected {
   203  		t.Fatalf("bad:\n\n%s", actual)
   204  	}
   205  }
   206  
   207  func TestPruneDestroyTransformer_countDec(t *testing.T) {
   208  	mod := testModule(t, "transform-destroy-basic")
   209  
   210  	diff := &Diff{}
   211  	state := &State{
   212  		Modules: []*ModuleState{
   213  			&ModuleState{
   214  				Path: RootModulePath,
   215  				Resources: map[string]*ResourceState{
   216  					"aws_instance.bar.1": &ResourceState{
   217  						Primary: &InstanceState{},
   218  					},
   219  					"aws_instance.bar.2": &ResourceState{
   220  						Primary: &InstanceState{},
   221  					},
   222  				},
   223  			},
   224  		},
   225  	}
   226  
   227  	g := Graph{Path: RootModulePath}
   228  	{
   229  		tf := &ConfigTransformer{Module: mod}
   230  		if err := tf.Transform(&g); err != nil {
   231  			t.Fatalf("err: %s", err)
   232  		}
   233  	}
   234  
   235  	{
   236  		tf := &DestroyTransformer{}
   237  		if err := tf.Transform(&g); err != nil {
   238  			t.Fatalf("err: %s", err)
   239  		}
   240  	}
   241  
   242  	{
   243  		tf := &PruneDestroyTransformer{Diff: diff, State: state}
   244  		if err := tf.Transform(&g); err != nil {
   245  			t.Fatalf("err: %s", err)
   246  		}
   247  	}
   248  
   249  	actual := strings.TrimSpace(g.String())
   250  	expected := strings.TrimSpace(testTransformPruneDestroyCountDecStr)
   251  	if actual != expected {
   252  		t.Fatalf("bad:\n\n%s", actual)
   253  	}
   254  }
   255  
   256  func TestPruneDestroyTransformer_countState(t *testing.T) {
   257  	mod := testModule(t, "transform-destroy-basic")
   258  
   259  	diff := &Diff{}
   260  	state := &State{
   261  		Modules: []*ModuleState{
   262  			&ModuleState{
   263  				Path: RootModulePath,
   264  				Resources: map[string]*ResourceState{
   265  					"aws_instance.bar": &ResourceState{
   266  						Primary: &InstanceState{},
   267  					},
   268  				},
   269  			},
   270  		},
   271  	}
   272  
   273  	g := Graph{Path: RootModulePath}
   274  	{
   275  		tf := &ConfigTransformer{Module: mod}
   276  		if err := tf.Transform(&g); err != nil {
   277  			t.Fatalf("err: %s", err)
   278  		}
   279  	}
   280  
   281  	{
   282  		tf := &DestroyTransformer{}
   283  		if err := tf.Transform(&g); err != nil {
   284  			t.Fatalf("err: %s", err)
   285  		}
   286  	}
   287  
   288  	{
   289  		tf := &PruneDestroyTransformer{Diff: diff, State: state}
   290  		if err := tf.Transform(&g); err != nil {
   291  			t.Fatalf("err: %s", err)
   292  		}
   293  	}
   294  
   295  	actual := strings.TrimSpace(g.String())
   296  	expected := strings.TrimSpace(testTransformPruneDestroyCountStateStr)
   297  	if actual != expected {
   298  		t.Fatalf("bad:\n\n%s", actual)
   299  	}
   300  }
   301  
   302  func TestPruneDestroyTransformer_prefixMatch(t *testing.T) {
   303  	mod := testModule(t, "transform-destroy-prefix")
   304  
   305  	diff := &Diff{}
   306  	state := &State{
   307  		Modules: []*ModuleState{
   308  			&ModuleState{
   309  				Path: RootModulePath,
   310  				Resources: map[string]*ResourceState{
   311  					"aws_instance.foo-bar.0": &ResourceState{
   312  						Primary: &InstanceState{ID: "foo"},
   313  					},
   314  
   315  					"aws_instance.foo-bar.1": &ResourceState{
   316  						Primary: &InstanceState{ID: "foo"},
   317  					},
   318  				},
   319  			},
   320  		},
   321  	}
   322  
   323  	g := Graph{Path: RootModulePath}
   324  	{
   325  		tf := &ConfigTransformer{Module: mod}
   326  		if err := tf.Transform(&g); err != nil {
   327  			t.Fatalf("err: %s", err)
   328  		}
   329  	}
   330  
   331  	{
   332  		tf := &DestroyTransformer{}
   333  		if err := tf.Transform(&g); err != nil {
   334  			t.Fatalf("err: %s", err)
   335  		}
   336  	}
   337  
   338  	{
   339  		tf := &PruneDestroyTransformer{Diff: diff, State: state}
   340  		if err := tf.Transform(&g); err != nil {
   341  			t.Fatalf("err: %s", err)
   342  		}
   343  	}
   344  
   345  	actual := strings.TrimSpace(g.String())
   346  	expected := strings.TrimSpace(testTransformPruneDestroyPrefixStr)
   347  	if actual != expected {
   348  		t.Fatalf("bad:\n\n%s", actual)
   349  	}
   350  }
   351  
   352  func TestPruneDestroyTransformer_tainted(t *testing.T) {
   353  	mod := testModule(t, "transform-destroy-basic")
   354  
   355  	diff := &Diff{}
   356  	state := &State{
   357  		Modules: []*ModuleState{
   358  			&ModuleState{
   359  				Path: RootModulePath,
   360  				Resources: map[string]*ResourceState{
   361  					"aws_instance.bar": &ResourceState{
   362  						Tainted: []*InstanceState{
   363  							&InstanceState{ID: "foo"},
   364  						},
   365  					},
   366  				},
   367  			},
   368  		},
   369  	}
   370  
   371  	g := Graph{Path: RootModulePath}
   372  	{
   373  		tf := &ConfigTransformer{Module: mod}
   374  		if err := tf.Transform(&g); err != nil {
   375  			t.Fatalf("err: %s", err)
   376  		}
   377  	}
   378  
   379  	{
   380  		tf := &DestroyTransformer{}
   381  		if err := tf.Transform(&g); err != nil {
   382  			t.Fatalf("err: %s", err)
   383  		}
   384  	}
   385  
   386  	{
   387  		tf := &PruneDestroyTransformer{Diff: diff, State: state}
   388  		if err := tf.Transform(&g); err != nil {
   389  			t.Fatalf("err: %s", err)
   390  		}
   391  	}
   392  
   393  	actual := strings.TrimSpace(g.String())
   394  	expected := strings.TrimSpace(testTransformPruneDestroyTaintedStr)
   395  	if actual != expected {
   396  		t.Fatalf("bad:\n\n%s", actual)
   397  	}
   398  }
   399  
   400  const testTransformDestroyBasicStr = `
   401  aws_instance.bar
   402    aws_instance.bar (destroy tainted)
   403    aws_instance.bar (destroy)
   404    aws_instance.foo
   405  aws_instance.bar (destroy tainted)
   406  aws_instance.bar (destroy)
   407  aws_instance.foo
   408    aws_instance.foo (destroy tainted)
   409    aws_instance.foo (destroy)
   410  aws_instance.foo (destroy tainted)
   411    aws_instance.bar (destroy tainted)
   412  aws_instance.foo (destroy)
   413    aws_instance.bar (destroy)
   414  `
   415  
   416  const testTransformPruneDestroyBasicStr = `
   417  aws_instance.bar
   418    aws_instance.foo
   419  aws_instance.foo
   420  `
   421  
   422  const testTransformPruneDestroyBasicDiffStr = `
   423  aws_instance.bar
   424    aws_instance.foo
   425  aws_instance.foo
   426  `
   427  
   428  const testTransformPruneDestroyCountStr = `
   429  aws_instance.bar
   430    aws_instance.bar (destroy)
   431    aws_instance.foo
   432  aws_instance.bar (destroy)
   433  aws_instance.foo
   434  `
   435  
   436  const testTransformPruneDestroyCountDecStr = `
   437  aws_instance.bar
   438    aws_instance.bar (destroy)
   439    aws_instance.foo
   440  aws_instance.bar (destroy)
   441  aws_instance.foo
   442  `
   443  
   444  const testTransformPruneDestroyCountStateStr = `
   445  aws_instance.bar
   446    aws_instance.foo
   447  aws_instance.foo
   448  `
   449  
   450  const testTransformPruneDestroyPrefixStr = `
   451  aws_instance.foo
   452  aws_instance.foo-bar
   453    aws_instance.foo-bar (destroy)
   454  aws_instance.foo-bar (destroy)
   455  `
   456  
   457  const testTransformPruneDestroyTaintedStr = `
   458  aws_instance.bar
   459    aws_instance.bar (destroy tainted)
   460    aws_instance.foo
   461  aws_instance.bar (destroy tainted)
   462  aws_instance.foo
   463  `
   464  
   465  const testTransformCreateBeforeDestroyBasicStr = `
   466  aws_instance.web
   467    aws_instance.web (destroy tainted)
   468  aws_instance.web (destroy tainted)
   469    aws_load_balancer.lb (destroy tainted)
   470  aws_instance.web (destroy)
   471    aws_instance.web
   472    aws_load_balancer.lb
   473    aws_load_balancer.lb (destroy)
   474  aws_load_balancer.lb
   475    aws_instance.web
   476    aws_load_balancer.lb (destroy tainted)
   477    aws_load_balancer.lb (destroy)
   478  aws_load_balancer.lb (destroy tainted)
   479  aws_load_balancer.lb (destroy)
   480  `
   481  
   482  const testTransformCreateBeforeDestroyTwiceStr = `
   483  aws_autoscale.bar
   484    aws_autoscale.bar (destroy tainted)
   485    aws_lc.foo
   486  aws_autoscale.bar (destroy tainted)
   487  aws_autoscale.bar (destroy)
   488    aws_autoscale.bar
   489  aws_lc.foo
   490    aws_lc.foo (destroy tainted)
   491  aws_lc.foo (destroy tainted)
   492    aws_autoscale.bar (destroy tainted)
   493  aws_lc.foo (destroy)
   494    aws_autoscale.bar
   495    aws_autoscale.bar (destroy)
   496    aws_lc.foo
   497  `