github.com/cbroglie/terraform@v0.7.0-rc3.0.20170410193827-735dfc416d46/command/state_rm_test.go (about) 1 package command 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/hashicorp/terraform/terraform" 9 "github.com/mitchellh/cli" 10 ) 11 12 func TestStateRm(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 Attributes: map[string]string{ 23 "foo": "value", 24 "bar": "value", 25 }, 26 }, 27 }, 28 29 "test_instance.bar": &terraform.ResourceState{ 30 Type: "test_instance", 31 Primary: &terraform.InstanceState{ 32 ID: "foo", 33 Attributes: map[string]string{ 34 "foo": "value", 35 "bar": "value", 36 }, 37 }, 38 }, 39 }, 40 }, 41 }, 42 } 43 44 statePath := testStateFile(t, state) 45 46 p := testProvider() 47 ui := new(cli.MockUi) 48 c := &StateRmCommand{ 49 Meta: Meta{ 50 ContextOpts: testCtxConfig(p), 51 Ui: ui, 52 }, 53 } 54 55 args := []string{ 56 "-state", statePath, 57 "test_instance.foo", 58 } 59 if code := c.Run(args); code != 0 { 60 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 61 } 62 63 // Test it is correct 64 testStateOutput(t, statePath, testStateRmOutput) 65 66 // Test we have backups 67 backups := testStateBackups(t, filepath.Dir(statePath)) 68 if len(backups) != 1 { 69 t.Fatalf("bad: %#v", backups) 70 } 71 testStateOutput(t, backups[0], testStateRmOutputOriginal) 72 } 73 74 func TestStateRm_backupExplicit(t *testing.T) { 75 td := tempDir(t) 76 defer os.RemoveAll(td) 77 backupPath := filepath.Join(td, "backup") 78 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 Meta: Meta{ 116 ContextOpts: testCtxConfig(p), 117 Ui: ui, 118 }, 119 } 120 121 args := []string{ 122 "-backup", backupPath, 123 "-state", statePath, 124 "test_instance.foo", 125 } 126 if code := c.Run(args); code != 0 { 127 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 128 } 129 130 // Test it is correct 131 testStateOutput(t, statePath, testStateRmOutput) 132 133 // Test we have backups 134 backups := testStateBackups(t, filepath.Dir(statePath)) 135 if len(backups) != 1 { 136 t.Fatalf("bad: %#v", backups) 137 } 138 testStateOutput(t, backups[0], testStateRmOutputOriginal) 139 testStateOutput(t, backupPath, testStateRmOutputOriginal) 140 } 141 142 func TestStateRm_noState(t *testing.T) { 143 tmp, cwd := testCwd(t) 144 defer testFixCwd(t, tmp, cwd) 145 146 p := testProvider() 147 ui := new(cli.MockUi) 148 c := &StateRmCommand{ 149 Meta: Meta{ 150 ContextOpts: testCtxConfig(p), 151 Ui: ui, 152 }, 153 } 154 155 args := []string{} 156 if code := c.Run(args); code != 1 { 157 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 158 } 159 } 160 161 const testStateRmOutputOriginal = ` 162 test_instance.bar: 163 ID = foo 164 bar = value 165 foo = value 166 test_instance.foo: 167 ID = bar 168 bar = value 169 foo = value 170 ` 171 172 const testStateRmOutput = ` 173 test_instance.bar: 174 ID = foo 175 bar = value 176 foo = value 177 `