github.com/tomaszheflik/terraform@v0.7.3-0.20160827060421-32f990b41594/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  						Primary: &InstanceState{
   363  							ID:      "foo",
   364  							Tainted: true,
   365  						},
   366  					},
   367  				},
   368  			},
   369  		},
   370  	}
   371  
   372  	g := Graph{Path: RootModulePath}
   373  	{
   374  		tf := &ConfigTransformer{Module: mod}
   375  		if err := tf.Transform(&g); err != nil {
   376  			t.Fatalf("err: %s", err)
   377  		}
   378  	}
   379  
   380  	{
   381  		tf := &DestroyTransformer{}
   382  		if err := tf.Transform(&g); err != nil {
   383  			t.Fatalf("err: %s", err)
   384  		}
   385  	}
   386  
   387  	{
   388  		tf := &PruneDestroyTransformer{Diff: diff, State: state}
   389  		if err := tf.Transform(&g); err != nil {
   390  			t.Fatalf("err: %s", err)
   391  		}
   392  	}
   393  
   394  	actual := strings.TrimSpace(g.String())
   395  	expected := strings.TrimSpace(testTransformPruneDestroyTaintedStr)
   396  	if actual != expected {
   397  		t.Fatalf("bad:\n\n%s", actual)
   398  	}
   399  }
   400  
   401  const testTransformDestroyBasicStr = `
   402  aws_instance.bar
   403    aws_instance.bar (destroy)
   404    aws_instance.foo
   405  aws_instance.bar (destroy)
   406  aws_instance.foo
   407    aws_instance.foo (destroy)
   408  aws_instance.foo (destroy)
   409    aws_instance.bar (destroy)
   410  `
   411  
   412  const testTransformPruneDestroyBasicStr = `
   413  aws_instance.bar
   414    aws_instance.foo
   415  aws_instance.foo
   416  `
   417  
   418  const testTransformPruneDestroyBasicDiffStr = `
   419  aws_instance.bar
   420    aws_instance.foo
   421  aws_instance.foo
   422  `
   423  
   424  const testTransformPruneDestroyCountStr = `
   425  aws_instance.bar
   426    aws_instance.bar (destroy)
   427    aws_instance.foo
   428  aws_instance.bar (destroy)
   429  aws_instance.foo
   430  `
   431  
   432  const testTransformPruneDestroyCountDecStr = `
   433  aws_instance.bar
   434    aws_instance.bar (destroy)
   435    aws_instance.foo
   436  aws_instance.bar (destroy)
   437  aws_instance.foo
   438  `
   439  
   440  const testTransformPruneDestroyCountStateStr = `
   441  aws_instance.bar
   442    aws_instance.foo
   443  aws_instance.foo
   444  `
   445  
   446  const testTransformPruneDestroyPrefixStr = `
   447  aws_instance.foo
   448  aws_instance.foo-bar
   449    aws_instance.foo-bar (destroy)
   450  aws_instance.foo-bar (destroy)
   451  `
   452  
   453  const testTransformPruneDestroyTaintedStr = `
   454  aws_instance.bar
   455    aws_instance.foo
   456  aws_instance.foo
   457  `
   458  
   459  const testTransformCreateBeforeDestroyBasicStr = `
   460  aws_instance.web
   461  aws_instance.web (destroy)
   462    aws_instance.web
   463    aws_load_balancer.lb
   464    aws_load_balancer.lb (destroy)
   465  aws_load_balancer.lb
   466    aws_instance.web
   467    aws_load_balancer.lb (destroy)
   468  aws_load_balancer.lb (destroy)
   469  `
   470  
   471  const testTransformCreateBeforeDestroyTwiceStr = `
   472  aws_autoscale.bar
   473    aws_lc.foo
   474  aws_autoscale.bar (destroy)
   475    aws_autoscale.bar
   476  aws_lc.foo
   477  aws_lc.foo (destroy)
   478    aws_autoscale.bar
   479    aws_autoscale.bar (destroy)
   480    aws_lc.foo
   481  `