github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/command/untaint_test.go (about)

     1  package command
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/mitchellh/cli"
    10  )
    11  
    12  func TestUntaint(t *testing.T) {
    13  	state := &terraform.State{
    14  		Modules: []*terraform.ModuleState{
    15  			&terraform.ModuleState{
    16  				Path: []string{"root"},
    17  				Resources: map[string]*terraform.ResourceState{
    18  					"test_instance.foo": &terraform.ResourceState{
    19  						Type: "test_instance",
    20  						Primary: &terraform.InstanceState{
    21  							ID:      "bar",
    22  							Tainted: true,
    23  						},
    24  					},
    25  				},
    26  			},
    27  		},
    28  	}
    29  	statePath := testStateFile(t, state)
    30  
    31  	ui := new(cli.MockUi)
    32  	c := &UntaintCommand{
    33  		Meta: Meta{
    34  			Ui: ui,
    35  		},
    36  	}
    37  
    38  	args := []string{
    39  		"-state", statePath,
    40  		"test_instance.foo",
    41  	}
    42  	if code := c.Run(args); code != 0 {
    43  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    44  	}
    45  
    46  	expected := strings.TrimSpace(`
    47  test_instance.foo:
    48    ID = bar
    49  	`)
    50  	testStateOutput(t, statePath, expected)
    51  }
    52  
    53  func TestUntaint_backup(t *testing.T) {
    54  	// Get a temp cwd
    55  	tmp, cwd := testCwd(t)
    56  	defer testFixCwd(t, tmp, cwd)
    57  
    58  	// Write the temp state
    59  	state := &terraform.State{
    60  		Modules: []*terraform.ModuleState{
    61  			&terraform.ModuleState{
    62  				Path: []string{"root"},
    63  				Resources: map[string]*terraform.ResourceState{
    64  					"test_instance.foo": &terraform.ResourceState{
    65  						Type: "test_instance",
    66  						Primary: &terraform.InstanceState{
    67  							ID:      "bar",
    68  							Tainted: true,
    69  						},
    70  					},
    71  				},
    72  			},
    73  		},
    74  	}
    75  	path := testStateFileDefault(t, state)
    76  
    77  	ui := new(cli.MockUi)
    78  	c := &UntaintCommand{
    79  		Meta: Meta{
    80  			Ui: ui,
    81  		},
    82  	}
    83  
    84  	args := []string{
    85  		"test_instance.foo",
    86  	}
    87  	if code := c.Run(args); code != 0 {
    88  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    89  	}
    90  
    91  	// Backup is still tainted
    92  	testStateOutput(t, path+".backup", strings.TrimSpace(`
    93  test_instance.foo: (tainted)
    94    ID = bar
    95  	`))
    96  
    97  	// State is untainted
    98  	testStateOutput(t, path, strings.TrimSpace(`
    99  test_instance.foo:
   100    ID = bar
   101  	`))
   102  }
   103  
   104  func TestUntaint_backupDisable(t *testing.T) {
   105  	// Get a temp cwd
   106  	tmp, cwd := testCwd(t)
   107  	defer testFixCwd(t, tmp, cwd)
   108  
   109  	// Write the temp state
   110  	state := &terraform.State{
   111  		Modules: []*terraform.ModuleState{
   112  			&terraform.ModuleState{
   113  				Path: []string{"root"},
   114  				Resources: map[string]*terraform.ResourceState{
   115  					"test_instance.foo": &terraform.ResourceState{
   116  						Type: "test_instance",
   117  						Primary: &terraform.InstanceState{
   118  							ID:      "bar",
   119  							Tainted: true,
   120  						},
   121  					},
   122  				},
   123  			},
   124  		},
   125  	}
   126  	path := testStateFileDefault(t, state)
   127  
   128  	ui := new(cli.MockUi)
   129  	c := &UntaintCommand{
   130  		Meta: Meta{
   131  			Ui: ui,
   132  		},
   133  	}
   134  
   135  	args := []string{
   136  		"-backup", "-",
   137  		"test_instance.foo",
   138  	}
   139  	if code := c.Run(args); code != 0 {
   140  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   141  	}
   142  
   143  	if _, err := os.Stat(path + ".backup"); err == nil {
   144  		t.Fatal("backup path should not exist")
   145  	}
   146  
   147  	testStateOutput(t, path, strings.TrimSpace(`
   148  test_instance.foo:
   149    ID = bar
   150  	`))
   151  }
   152  
   153  func TestUntaint_badState(t *testing.T) {
   154  	ui := new(cli.MockUi)
   155  	c := &UntaintCommand{
   156  		Meta: Meta{
   157  			Ui: ui,
   158  		},
   159  	}
   160  
   161  	args := []string{
   162  		"-state", "i-should-not-exist-ever",
   163  		"foo",
   164  	}
   165  	if code := c.Run(args); code != 1 {
   166  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   167  	}
   168  }
   169  
   170  func TestUntaint_defaultState(t *testing.T) {
   171  	// Get a temp cwd
   172  	tmp, cwd := testCwd(t)
   173  	defer testFixCwd(t, tmp, cwd)
   174  
   175  	// Write the temp state
   176  	state := &terraform.State{
   177  		Modules: []*terraform.ModuleState{
   178  			&terraform.ModuleState{
   179  				Path: []string{"root"},
   180  				Resources: map[string]*terraform.ResourceState{
   181  					"test_instance.foo": &terraform.ResourceState{
   182  						Type: "test_instance",
   183  						Primary: &terraform.InstanceState{
   184  							ID:      "bar",
   185  							Tainted: true,
   186  						},
   187  					},
   188  				},
   189  			},
   190  		},
   191  	}
   192  	path := testStateFileDefault(t, state)
   193  
   194  	ui := new(cli.MockUi)
   195  	c := &UntaintCommand{
   196  		Meta: Meta{
   197  			Ui: ui,
   198  		},
   199  	}
   200  
   201  	args := []string{
   202  		"test_instance.foo",
   203  	}
   204  	if code := c.Run(args); code != 0 {
   205  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   206  	}
   207  
   208  	testStateOutput(t, path, strings.TrimSpace(`
   209  test_instance.foo:
   210    ID = bar
   211  	`))
   212  }
   213  
   214  func TestUntaint_missing(t *testing.T) {
   215  	state := &terraform.State{
   216  		Modules: []*terraform.ModuleState{
   217  			&terraform.ModuleState{
   218  				Path: []string{"root"},
   219  				Resources: map[string]*terraform.ResourceState{
   220  					"test_instance.foo": &terraform.ResourceState{
   221  						Type: "test_instance",
   222  						Primary: &terraform.InstanceState{
   223  							ID:      "bar",
   224  							Tainted: true,
   225  						},
   226  					},
   227  				},
   228  			},
   229  		},
   230  	}
   231  	statePath := testStateFile(t, state)
   232  
   233  	ui := new(cli.MockUi)
   234  	c := &UntaintCommand{
   235  		Meta: Meta{
   236  			Ui: ui,
   237  		},
   238  	}
   239  
   240  	args := []string{
   241  		"-state", statePath,
   242  		"test_instance.bar",
   243  	}
   244  	if code := c.Run(args); code == 0 {
   245  		t.Fatalf("bad: %d\n\n%s", code, ui.OutputWriter.String())
   246  	}
   247  }
   248  
   249  func TestUntaint_missingAllow(t *testing.T) {
   250  	state := &terraform.State{
   251  		Modules: []*terraform.ModuleState{
   252  			&terraform.ModuleState{
   253  				Path: []string{"root"},
   254  				Resources: map[string]*terraform.ResourceState{
   255  					"test_instance.foo": &terraform.ResourceState{
   256  						Type: "test_instance",
   257  						Primary: &terraform.InstanceState{
   258  							ID:      "bar",
   259  							Tainted: true,
   260  						},
   261  					},
   262  				},
   263  			},
   264  		},
   265  	}
   266  	statePath := testStateFile(t, state)
   267  
   268  	ui := new(cli.MockUi)
   269  	c := &UntaintCommand{
   270  		Meta: Meta{
   271  			Ui: ui,
   272  		},
   273  	}
   274  
   275  	args := []string{
   276  		"-allow-missing",
   277  		"-state", statePath,
   278  		"test_instance.bar",
   279  	}
   280  	if code := c.Run(args); code != 0 {
   281  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   282  	}
   283  }
   284  
   285  func TestUntaint_stateOut(t *testing.T) {
   286  	// Get a temp cwd
   287  	tmp, cwd := testCwd(t)
   288  	defer testFixCwd(t, tmp, cwd)
   289  
   290  	// Write the temp state
   291  	state := &terraform.State{
   292  		Modules: []*terraform.ModuleState{
   293  			&terraform.ModuleState{
   294  				Path: []string{"root"},
   295  				Resources: map[string]*terraform.ResourceState{
   296  					"test_instance.foo": &terraform.ResourceState{
   297  						Type: "test_instance",
   298  						Primary: &terraform.InstanceState{
   299  							ID:      "bar",
   300  							Tainted: true,
   301  						},
   302  					},
   303  				},
   304  			},
   305  		},
   306  	}
   307  	path := testStateFileDefault(t, state)
   308  
   309  	ui := new(cli.MockUi)
   310  	c := &UntaintCommand{
   311  		Meta: Meta{
   312  			Ui: ui,
   313  		},
   314  	}
   315  
   316  	args := []string{
   317  		"-state-out", "foo",
   318  		"test_instance.foo",
   319  	}
   320  	if code := c.Run(args); code != 0 {
   321  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   322  	}
   323  
   324  	testStateOutput(t, path, strings.TrimSpace(`
   325  test_instance.foo: (tainted)
   326    ID = bar
   327  	`))
   328  	testStateOutput(t, "foo", strings.TrimSpace(`
   329  test_instance.foo:
   330    ID = bar
   331  	`))
   332  }
   333  
   334  func TestUntaint_module(t *testing.T) {
   335  	state := &terraform.State{
   336  		Modules: []*terraform.ModuleState{
   337  			&terraform.ModuleState{
   338  				Path: []string{"root"},
   339  				Resources: map[string]*terraform.ResourceState{
   340  					"test_instance.foo": &terraform.ResourceState{
   341  						Type: "test_instance",
   342  						Primary: &terraform.InstanceState{
   343  							ID:      "bar",
   344  							Tainted: true,
   345  						},
   346  					},
   347  				},
   348  			},
   349  			&terraform.ModuleState{
   350  				Path: []string{"root", "child"},
   351  				Resources: map[string]*terraform.ResourceState{
   352  					"test_instance.blah": &terraform.ResourceState{
   353  						Type: "test_instance",
   354  						Primary: &terraform.InstanceState{
   355  							ID:      "bar",
   356  							Tainted: true,
   357  						},
   358  					},
   359  				},
   360  			},
   361  		},
   362  	}
   363  	statePath := testStateFile(t, state)
   364  
   365  	ui := new(cli.MockUi)
   366  	c := &UntaintCommand{
   367  		Meta: Meta{
   368  			Ui: ui,
   369  		},
   370  	}
   371  
   372  	args := []string{
   373  		"-module=child",
   374  		"-state", statePath,
   375  		"test_instance.blah",
   376  	}
   377  	if code := c.Run(args); code != 0 {
   378  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   379  	}
   380  
   381  	testStateOutput(t, statePath, strings.TrimSpace(`
   382  test_instance.foo: (tainted)
   383    ID = bar
   384  
   385  module.child:
   386    test_instance.blah:
   387      ID = bar
   388  	`))
   389  }