github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/command/state_rm_test.go (about)

     1  package command
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/helper/copy"
    10  	"github.com/hashicorp/terraform/terraform"
    11  	"github.com/mitchellh/cli"
    12  )
    13  
    14  func TestStateRm(t *testing.T) {
    15  	state := &terraform.State{
    16  		Modules: []*terraform.ModuleState{
    17  			&terraform.ModuleState{
    18  				Path: []string{"root"},
    19  				Resources: map[string]*terraform.ResourceState{
    20  					"test_instance.foo": &terraform.ResourceState{
    21  						Type: "test_instance",
    22  						Primary: &terraform.InstanceState{
    23  							ID: "bar",
    24  							Attributes: map[string]string{
    25  								"foo": "value",
    26  								"bar": "value",
    27  							},
    28  						},
    29  					},
    30  
    31  					"test_instance.bar": &terraform.ResourceState{
    32  						Type: "test_instance",
    33  						Primary: &terraform.InstanceState{
    34  							ID: "foo",
    35  							Attributes: map[string]string{
    36  								"foo": "value",
    37  								"bar": "value",
    38  							},
    39  						},
    40  					},
    41  				},
    42  			},
    43  		},
    44  	}
    45  
    46  	statePath := testStateFile(t, state)
    47  
    48  	p := testProvider()
    49  	ui := new(cli.MockUi)
    50  	c := &StateRmCommand{
    51  		StateMeta{
    52  			Meta: Meta{
    53  				testingOverrides: metaOverridesForProvider(p),
    54  				Ui:               ui,
    55  			},
    56  		},
    57  	}
    58  
    59  	args := []string{
    60  		"-state", statePath,
    61  		"test_instance.foo",
    62  	}
    63  	if code := c.Run(args); code != 0 {
    64  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    65  	}
    66  
    67  	// Test it is correct
    68  	testStateOutput(t, statePath, testStateRmOutput)
    69  
    70  	// Test we have backups
    71  	backups := testStateBackups(t, filepath.Dir(statePath))
    72  	if len(backups) != 1 {
    73  		t.Fatalf("bad: %#v", backups)
    74  	}
    75  	testStateOutput(t, backups[0], testStateRmOutputOriginal)
    76  }
    77  
    78  func TestStateRmNoArgs(t *testing.T) {
    79  	state := &terraform.State{
    80  		Modules: []*terraform.ModuleState{
    81  			&terraform.ModuleState{
    82  				Path: []string{"root"},
    83  				Resources: map[string]*terraform.ResourceState{
    84  					"test_instance.foo": &terraform.ResourceState{
    85  						Type: "test_instance",
    86  						Primary: &terraform.InstanceState{
    87  							ID: "bar",
    88  							Attributes: map[string]string{
    89  								"foo": "value",
    90  								"bar": "value",
    91  							},
    92  						},
    93  					},
    94  
    95  					"test_instance.bar": &terraform.ResourceState{
    96  						Type: "test_instance",
    97  						Primary: &terraform.InstanceState{
    98  							ID: "foo",
    99  							Attributes: map[string]string{
   100  								"foo": "value",
   101  								"bar": "value",
   102  							},
   103  						},
   104  					},
   105  				},
   106  			},
   107  		},
   108  	}
   109  
   110  	statePath := testStateFile(t, state)
   111  
   112  	p := testProvider()
   113  	ui := new(cli.MockUi)
   114  	c := &StateRmCommand{
   115  		StateMeta{
   116  			Meta: Meta{
   117  				testingOverrides: metaOverridesForProvider(p),
   118  				Ui:               ui,
   119  			},
   120  		},
   121  	}
   122  
   123  	args := []string{
   124  		"-state", statePath,
   125  	}
   126  	if code := c.Run(args); code != 1 {
   127  		t.Errorf("wrong exit status %d; want %d", code, 1)
   128  	}
   129  
   130  	if msg := ui.ErrorWriter.String(); !strings.Contains(msg, "At least one resource address") {
   131  		t.Errorf("not the error we were looking for:\n%s", msg)
   132  	}
   133  
   134  }
   135  
   136  func TestStateRm_backupExplicit(t *testing.T) {
   137  	td := tempDir(t)
   138  	defer os.RemoveAll(td)
   139  	backupPath := filepath.Join(td, "backup")
   140  
   141  	state := &terraform.State{
   142  		Modules: []*terraform.ModuleState{
   143  			&terraform.ModuleState{
   144  				Path: []string{"root"},
   145  				Resources: map[string]*terraform.ResourceState{
   146  					"test_instance.foo": &terraform.ResourceState{
   147  						Type: "test_instance",
   148  						Primary: &terraform.InstanceState{
   149  							ID: "bar",
   150  							Attributes: map[string]string{
   151  								"foo": "value",
   152  								"bar": "value",
   153  							},
   154  						},
   155  					},
   156  
   157  					"test_instance.bar": &terraform.ResourceState{
   158  						Type: "test_instance",
   159  						Primary: &terraform.InstanceState{
   160  							ID: "foo",
   161  							Attributes: map[string]string{
   162  								"foo": "value",
   163  								"bar": "value",
   164  							},
   165  						},
   166  					},
   167  				},
   168  			},
   169  		},
   170  	}
   171  
   172  	statePath := testStateFile(t, state)
   173  
   174  	p := testProvider()
   175  	ui := new(cli.MockUi)
   176  	c := &StateRmCommand{
   177  		StateMeta{
   178  			Meta: Meta{
   179  				testingOverrides: metaOverridesForProvider(p),
   180  				Ui:               ui,
   181  			},
   182  		},
   183  	}
   184  
   185  	args := []string{
   186  		"-backup", backupPath,
   187  		"-state", statePath,
   188  		"test_instance.foo",
   189  	}
   190  	if code := c.Run(args); code != 0 {
   191  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   192  	}
   193  
   194  	// Test it is correct
   195  	testStateOutput(t, statePath, testStateRmOutput)
   196  
   197  	// Test backup
   198  	testStateOutput(t, backupPath, testStateRmOutputOriginal)
   199  }
   200  
   201  func TestStateRm_noState(t *testing.T) {
   202  	tmp, cwd := testCwd(t)
   203  	defer testFixCwd(t, tmp, cwd)
   204  
   205  	p := testProvider()
   206  	ui := new(cli.MockUi)
   207  	c := &StateRmCommand{
   208  		StateMeta{
   209  			Meta: Meta{
   210  				testingOverrides: metaOverridesForProvider(p),
   211  				Ui:               ui,
   212  			},
   213  		},
   214  	}
   215  
   216  	args := []string{}
   217  	if code := c.Run(args); code != 1 {
   218  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   219  	}
   220  }
   221  
   222  func TestStateRm_needsInit(t *testing.T) {
   223  	td := tempDir(t)
   224  	copy.CopyDir(testFixturePath("backend-change"), td)
   225  	defer os.RemoveAll(td)
   226  	defer testChdir(t, td)()
   227  
   228  	p := testProvider()
   229  	ui := new(cli.MockUi)
   230  	c := &StateRmCommand{
   231  		StateMeta{
   232  			Meta: Meta{
   233  				testingOverrides: metaOverridesForProvider(p),
   234  				Ui:               ui,
   235  			},
   236  		},
   237  	}
   238  
   239  	args := []string{"foo"}
   240  	if code := c.Run(args); code == 0 {
   241  		t.Fatal("expected error\noutput:", ui.OutputWriter)
   242  	}
   243  
   244  	if !strings.Contains(ui.ErrorWriter.String(), "Initialization") {
   245  		t.Fatal("expected initialization error, got:\n", ui.ErrorWriter)
   246  	}
   247  }
   248  
   249  func TestStateRm_backendState(t *testing.T) {
   250  	td := tempDir(t)
   251  	copy.CopyDir(testFixturePath("backend-unchanged"), td)
   252  	defer os.RemoveAll(td)
   253  	defer testChdir(t, td)()
   254  
   255  	state := &terraform.State{
   256  		Modules: []*terraform.ModuleState{
   257  			&terraform.ModuleState{
   258  				Path: []string{"root"},
   259  				Resources: map[string]*terraform.ResourceState{
   260  					"test_instance.foo": &terraform.ResourceState{
   261  						Type: "test_instance",
   262  						Primary: &terraform.InstanceState{
   263  							ID: "bar",
   264  							Attributes: map[string]string{
   265  								"foo": "value",
   266  								"bar": "value",
   267  							},
   268  						},
   269  					},
   270  
   271  					"test_instance.bar": &terraform.ResourceState{
   272  						Type: "test_instance",
   273  						Primary: &terraform.InstanceState{
   274  							ID: "foo",
   275  							Attributes: map[string]string{
   276  								"foo": "value",
   277  								"bar": "value",
   278  							},
   279  						},
   280  					},
   281  				},
   282  			},
   283  		},
   284  	}
   285  
   286  	// the local backend state file is "foo"
   287  	statePath := "local-state.tfstate"
   288  	backupPath := "local-state.backup"
   289  
   290  	f, err := os.Create(statePath)
   291  	if err != nil {
   292  		t.Fatal(err)
   293  	}
   294  	defer f.Close()
   295  
   296  	if err := terraform.WriteState(state, f); err != nil {
   297  		t.Fatal(err)
   298  	}
   299  
   300  	p := testProvider()
   301  	ui := new(cli.MockUi)
   302  	c := &StateRmCommand{
   303  		StateMeta{
   304  			Meta: Meta{
   305  				testingOverrides: metaOverridesForProvider(p),
   306  				Ui:               ui,
   307  			},
   308  		},
   309  	}
   310  
   311  	args := []string{
   312  		"-backup", backupPath,
   313  		"test_instance.foo",
   314  	}
   315  	if code := c.Run(args); code != 0 {
   316  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   317  	}
   318  
   319  	// Test it is correct
   320  	testStateOutput(t, statePath, testStateRmOutput)
   321  
   322  	// Test backup
   323  	testStateOutput(t, backupPath, testStateRmOutputOriginal)
   324  }
   325  
   326  const testStateRmOutputOriginal = `
   327  test_instance.bar:
   328    ID = foo
   329    bar = value
   330    foo = value
   331  test_instance.foo:
   332    ID = bar
   333    bar = value
   334    foo = value
   335  `
   336  
   337  const testStateRmOutput = `
   338  test_instance.bar:
   339    ID = foo
   340    bar = value
   341    foo = value
   342  `