|
1 | 1 | package command
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "os"
|
5 | 6 | "strings"
|
6 | 7 | "testing"
|
@@ -114,6 +115,123 @@ func TestApply_destroy(t *testing.T) {
|
114 | 115 | }
|
115 | 116 | }
|
116 | 117 |
|
| 118 | +func TestApply_destroyApproveNo(t *testing.T) { |
| 119 | + // Create a temporary working directory that is empty |
| 120 | + td := tempDir(t) |
| 121 | + testCopyDir(t, testFixturePath("apply"), td) |
| 122 | + defer os.RemoveAll(td) |
| 123 | + defer testChdir(t, td)() |
| 124 | + |
| 125 | + // Create some existing state |
| 126 | + originalState := states.BuildState(func(s *states.SyncState) { |
| 127 | + s.SetResourceInstanceCurrent( |
| 128 | + addrs.Resource{ |
| 129 | + Mode: addrs.ManagedResourceMode, |
| 130 | + Type: "test_instance", |
| 131 | + Name: "foo", |
| 132 | + }.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance), |
| 133 | + &states.ResourceInstanceObjectSrc{ |
| 134 | + AttrsJSON: []byte(`{"id":"bar"}`), |
| 135 | + Status: states.ObjectReady, |
| 136 | + }, |
| 137 | + addrs.AbsProviderConfig{ |
| 138 | + Provider: addrs.NewDefaultProvider("test"), |
| 139 | + Module: addrs.RootModule, |
| 140 | + }, |
| 141 | + ) |
| 142 | + }) |
| 143 | + statePath := testStateFile(t, originalState) |
| 144 | + |
| 145 | + // Disable test mode so input would be asked |
| 146 | + test = false |
| 147 | + defer func() { test = true }() |
| 148 | + |
| 149 | + // Answer approval request with "no" |
| 150 | + defaultInputReader = bytes.NewBufferString("no\n") |
| 151 | + defaultInputWriter = new(bytes.Buffer) |
| 152 | + |
| 153 | + p := applyFixtureProvider() |
| 154 | + ui := new(cli.MockUi) |
| 155 | + c := &ApplyCommand{ |
| 156 | + Destroy: true, |
| 157 | + Meta: Meta{ |
| 158 | + testingOverrides: metaOverridesForProvider(p), |
| 159 | + Ui: ui, |
| 160 | + }, |
| 161 | + } |
| 162 | + |
| 163 | + args := []string{ |
| 164 | + "-state", statePath, |
| 165 | + } |
| 166 | + if code := c.Run(args); code != 1 { |
| 167 | + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) |
| 168 | + } |
| 169 | + if got, want := ui.OutputWriter.String(), "Destroy cancelled"; !strings.Contains(got, want) { |
| 170 | + t.Fatalf("expected output to include %q, but was:\n%s", want, got) |
| 171 | + } |
| 172 | + |
| 173 | + state := testStateRead(t, statePath) |
| 174 | + if state == nil { |
| 175 | + t.Fatal("state should not be nil") |
| 176 | + } |
| 177 | + actualStr := strings.TrimSpace(state.String()) |
| 178 | + expectedStr := strings.TrimSpace(originalState.String()) |
| 179 | + if actualStr != expectedStr { |
| 180 | + t.Fatalf("bad:\n\n%s\n\n%s", actualStr, expectedStr) |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +func TestApply_destroyApproveYes(t *testing.T) { |
| 185 | + // Create a temporary working directory that is empty |
| 186 | + td := tempDir(t) |
| 187 | + testCopyDir(t, testFixturePath("apply"), td) |
| 188 | + defer os.RemoveAll(td) |
| 189 | + defer testChdir(t, td)() |
| 190 | + |
| 191 | + statePath := testTempFile(t) |
| 192 | + |
| 193 | + p := applyFixtureProvider() |
| 194 | + |
| 195 | + // Disable test mode so input would be asked |
| 196 | + test = false |
| 197 | + defer func() { test = true }() |
| 198 | + |
| 199 | + // Answer approval request with "yes" |
| 200 | + defaultInputReader = bytes.NewBufferString("yes\n") |
| 201 | + defaultInputWriter = new(bytes.Buffer) |
| 202 | + |
| 203 | + ui := new(cli.MockUi) |
| 204 | + c := &ApplyCommand{ |
| 205 | + Destroy: true, |
| 206 | + Meta: Meta{ |
| 207 | + testingOverrides: metaOverridesForProvider(p), |
| 208 | + Ui: ui, |
| 209 | + }, |
| 210 | + } |
| 211 | + |
| 212 | + args := []string{ |
| 213 | + "-state", statePath, |
| 214 | + } |
| 215 | + if code := c.Run(args); code != 0 { |
| 216 | + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) |
| 217 | + } |
| 218 | + |
| 219 | + if _, err := os.Stat(statePath); err != nil { |
| 220 | + t.Fatalf("err: %s", err) |
| 221 | + } |
| 222 | + |
| 223 | + state := testStateRead(t, statePath) |
| 224 | + if state == nil { |
| 225 | + t.Fatal("state should not be nil") |
| 226 | + } |
| 227 | + |
| 228 | + actualStr := strings.TrimSpace(state.String()) |
| 229 | + expectedStr := strings.TrimSpace(testApplyDestroyStr) |
| 230 | + if actualStr != expectedStr { |
| 231 | + t.Fatalf("bad:\n\n%s\n\n%s", actualStr, expectedStr) |
| 232 | + } |
| 233 | +} |
| 234 | + |
117 | 235 | func TestApply_destroyLockedState(t *testing.T) {
|
118 | 236 | originalState := states.BuildState(func(s *states.SyncState) {
|
119 | 237 | s.SetResourceInstanceCurrent(
|
|
0 commit comments