github.com/spirius/terraform@v0.10.0-beta2.0.20170714185654-87b2c0cf8fea/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/terraform"
    10  	"github.com/mitchellh/cli"
    11  )
    12  
    13  func TestStateRm(t *testing.T) {
    14  	state := &terraform.State{
    15  		Modules: []*terraform.ModuleState{
    16  			&terraform.ModuleState{
    17  				Path: []string{"root"},
    18  				Resources: map[string]*terraform.ResourceState{
    19  					"test_instance.foo": &terraform.ResourceState{
    20  						Type: "test_instance",
    21  						Primary: &terraform.InstanceState{
    22  							ID: "bar",
    23  							Attributes: map[string]string{
    24  								"foo": "value",
    25  								"bar": "value",
    26  							},
    27  						},
    28  					},
    29  
    30  					"test_instance.bar": &terraform.ResourceState{
    31  						Type: "test_instance",
    32  						Primary: &terraform.InstanceState{
    33  							ID: "foo",
    34  							Attributes: map[string]string{
    35  								"foo": "value",
    36  								"bar": "value",
    37  							},
    38  						},
    39  					},
    40  				},
    41  			},
    42  		},
    43  	}
    44  
    45  	statePath := testStateFile(t, state)
    46  
    47  	p := testProvider()
    48  	ui := new(cli.MockUi)
    49  	c := &StateRmCommand{
    50  		Meta: Meta{
    51  			testingOverrides: metaOverridesForProvider(p),
    52  			Ui:               ui,
    53  		},
    54  	}
    55  
    56  	args := []string{
    57  		"-state", statePath,
    58  		"test_instance.foo",
    59  	}
    60  	if code := c.Run(args); code != 0 {
    61  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    62  	}
    63  
    64  	// Test it is correct
    65  	testStateOutput(t, statePath, testStateRmOutput)
    66  
    67  	// Test we have backups
    68  	backups := testStateBackups(t, filepath.Dir(statePath))
    69  	if len(backups) != 1 {
    70  		t.Fatalf("bad: %#v", backups)
    71  	}
    72  	testStateOutput(t, backups[0], testStateRmOutputOriginal)
    73  }
    74  
    75  func TestStateRmNoArgs(t *testing.T) {
    76  	state := &terraform.State{
    77  		Modules: []*terraform.ModuleState{
    78  			&terraform.ModuleState{
    79  				Path: []string{"root"},
    80  				Resources: map[string]*terraform.ResourceState{
    81  					"test_instance.foo": &terraform.ResourceState{
    82  						Type: "test_instance",
    83  						Primary: &terraform.InstanceState{
    84  							ID: "bar",
    85  							Attributes: map[string]string{
    86  								"foo": "value",
    87  								"bar": "value",
    88  							},
    89  						},
    90  					},
    91  
    92  					"test_instance.bar": &terraform.ResourceState{
    93  						Type: "test_instance",
    94  						Primary: &terraform.InstanceState{
    95  							ID: "foo",
    96  							Attributes: map[string]string{
    97  								"foo": "value",
    98  								"bar": "value",
    99  							},
   100  						},
   101  					},
   102  				},
   103  			},
   104  		},
   105  	}
   106  
   107  	statePath := testStateFile(t, state)
   108  
   109  	p := testProvider()
   110  	ui := new(cli.MockUi)
   111  	c := &StateRmCommand{
   112  		Meta: Meta{
   113  			testingOverrides: metaOverridesForProvider(p),
   114  			Ui:               ui,
   115  		},
   116  	}
   117  
   118  	args := []string{
   119  		"-state", statePath,
   120  	}
   121  	if code := c.Run(args); code != 1 {
   122  		t.Errorf("wrong exit status %d; want %d", code, 1)
   123  	}
   124  
   125  	if msg := ui.ErrorWriter.String(); !strings.Contains(msg, "At least one resource address") {
   126  		t.Errorf("not the error we were looking for:\n%s", msg)
   127  	}
   128  
   129  }
   130  
   131  func TestStateRm_backupExplicit(t *testing.T) {
   132  	td := tempDir(t)
   133  	defer os.RemoveAll(td)
   134  	backupPath := filepath.Join(td, "backup")
   135  
   136  	state := &terraform.State{
   137  		Modules: []*terraform.ModuleState{
   138  			&terraform.ModuleState{
   139  				Path: []string{"root"},
   140  				Resources: map[string]*terraform.ResourceState{
   141  					"test_instance.foo": &terraform.ResourceState{
   142  						Type: "test_instance",
   143  						Primary: &terraform.InstanceState{
   144  							ID: "bar",
   145  							Attributes: map[string]string{
   146  								"foo": "value",
   147  								"bar": "value",
   148  							},
   149  						},
   150  					},
   151  
   152  					"test_instance.bar": &terraform.ResourceState{
   153  						Type: "test_instance",
   154  						Primary: &terraform.InstanceState{
   155  							ID: "foo",
   156  							Attributes: map[string]string{
   157  								"foo": "value",
   158  								"bar": "value",
   159  							},
   160  						},
   161  					},
   162  				},
   163  			},
   164  		},
   165  	}
   166  
   167  	statePath := testStateFile(t, state)
   168  
   169  	p := testProvider()
   170  	ui := new(cli.MockUi)
   171  	c := &StateRmCommand{
   172  		Meta: Meta{
   173  			testingOverrides: metaOverridesForProvider(p),
   174  			Ui:               ui,
   175  		},
   176  	}
   177  
   178  	args := []string{
   179  		"-backup", backupPath,
   180  		"-state", statePath,
   181  		"test_instance.foo",
   182  	}
   183  	if code := c.Run(args); code != 0 {
   184  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   185  	}
   186  
   187  	// Test it is correct
   188  	testStateOutput(t, statePath, testStateRmOutput)
   189  
   190  	// Test backup
   191  	testStateOutput(t, backupPath, testStateRmOutputOriginal)
   192  }
   193  
   194  func TestStateRm_noState(t *testing.T) {
   195  	tmp, cwd := testCwd(t)
   196  	defer testFixCwd(t, tmp, cwd)
   197  
   198  	p := testProvider()
   199  	ui := new(cli.MockUi)
   200  	c := &StateRmCommand{
   201  		Meta: Meta{
   202  			testingOverrides: metaOverridesForProvider(p),
   203  			Ui:               ui,
   204  		},
   205  	}
   206  
   207  	args := []string{}
   208  	if code := c.Run(args); code != 1 {
   209  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   210  	}
   211  }
   212  
   213  const testStateRmOutputOriginal = `
   214  test_instance.bar:
   215    ID = foo
   216    bar = value
   217    foo = value
   218  test_instance.foo:
   219    ID = bar
   220    bar = value
   221    foo = value
   222  `
   223  
   224  const testStateRmOutput = `
   225  test_instance.bar:
   226    ID = foo
   227    bar = value
   228    foo = value
   229  `