github.com/ns1/terraform@v0.7.10-0.20161109153551-8949419bef40/command/apply_destroy_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 TestApply_destroy(t *testing.T) { 13 originalState := &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 }, 23 }, 24 }, 25 }, 26 }, 27 } 28 29 statePath := testStateFile(t, originalState) 30 31 p := testProvider() 32 ui := new(cli.MockUi) 33 c := &ApplyCommand{ 34 Destroy: true, 35 Meta: Meta{ 36 ContextOpts: testCtxConfig(p), 37 Ui: ui, 38 }, 39 } 40 41 // Run the apply command pointing to our existing state 42 args := []string{ 43 "-force", 44 "-state", statePath, 45 testFixturePath("apply"), 46 } 47 if code := c.Run(args); code != 0 { 48 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 49 } 50 51 // Verify a new state exists 52 if _, err := os.Stat(statePath); err != nil { 53 t.Fatalf("err: %s", err) 54 } 55 56 f, err := os.Open(statePath) 57 if err != nil { 58 t.Fatalf("err: %s", err) 59 } 60 defer f.Close() 61 62 state, err := terraform.ReadState(f) 63 if err != nil { 64 t.Fatalf("err: %s", err) 65 } 66 if state == nil { 67 t.Fatal("state should not be nil") 68 } 69 70 actualStr := strings.TrimSpace(state.String()) 71 expectedStr := strings.TrimSpace(testApplyDestroyStr) 72 if actualStr != expectedStr { 73 t.Fatalf("bad:\n\n%s\n\n%s", actualStr, expectedStr) 74 } 75 76 // Should have a backup file 77 f, err = os.Open(statePath + DefaultBackupExtension) 78 if err != nil { 79 t.Fatalf("err: %s", err) 80 } 81 82 backupState, err := terraform.ReadState(f) 83 f.Close() 84 if err != nil { 85 t.Fatalf("err: %s", err) 86 } 87 88 actualStr = strings.TrimSpace(backupState.String()) 89 expectedStr = strings.TrimSpace(originalState.String()) 90 if actualStr != expectedStr { 91 t.Fatalf("bad:\n\n%s\n\n%s", actualStr, expectedStr) 92 } 93 } 94 95 func TestApply_destroyPlan(t *testing.T) { 96 planPath := testPlanFile(t, &terraform.Plan{ 97 Module: testModule(t, "apply"), 98 }) 99 100 p := testProvider() 101 ui := new(cli.MockUi) 102 c := &ApplyCommand{ 103 Destroy: true, 104 Meta: Meta{ 105 ContextOpts: testCtxConfig(p), 106 Ui: ui, 107 }, 108 } 109 110 // Run the apply command pointing to our existing state 111 args := []string{ 112 planPath, 113 } 114 if code := c.Run(args); code != 1 { 115 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 116 } 117 } 118 119 func TestApply_destroyTargeted(t *testing.T) { 120 originalState := &terraform.State{ 121 Modules: []*terraform.ModuleState{ 122 &terraform.ModuleState{ 123 Path: []string{"root"}, 124 Resources: map[string]*terraform.ResourceState{ 125 "test_instance.foo": &terraform.ResourceState{ 126 Type: "test_instance", 127 Primary: &terraform.InstanceState{ 128 ID: "i-ab123", 129 }, 130 }, 131 "test_load_balancer.foo": &terraform.ResourceState{ 132 Type: "test_load_balancer", 133 Primary: &terraform.InstanceState{ 134 ID: "lb-abc123", 135 }, 136 }, 137 }, 138 }, 139 }, 140 } 141 142 statePath := testStateFile(t, originalState) 143 144 p := testProvider() 145 ui := new(cli.MockUi) 146 c := &ApplyCommand{ 147 Destroy: true, 148 Meta: Meta{ 149 ContextOpts: testCtxConfig(p), 150 Ui: ui, 151 }, 152 } 153 154 // Run the apply command pointing to our existing state 155 args := []string{ 156 "-force", 157 "-target", "test_instance.foo", 158 "-state", statePath, 159 testFixturePath("apply-destroy-targeted"), 160 } 161 if code := c.Run(args); code != 0 { 162 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 163 } 164 165 // Verify a new state exists 166 if _, err := os.Stat(statePath); err != nil { 167 t.Fatalf("err: %s", err) 168 } 169 170 f, err := os.Open(statePath) 171 if err != nil { 172 t.Fatalf("err: %s", err) 173 } 174 defer f.Close() 175 176 state, err := terraform.ReadState(f) 177 if err != nil { 178 t.Fatalf("err: %s", err) 179 } 180 if state == nil { 181 t.Fatal("state should not be nil") 182 } 183 184 actualStr := strings.TrimSpace(state.String()) 185 expectedStr := strings.TrimSpace(testApplyDestroyStr) 186 if actualStr != expectedStr { 187 t.Fatalf("bad:\n\n%s\n\n%s", actualStr, expectedStr) 188 } 189 190 // Should have a backup file 191 f, err = os.Open(statePath + DefaultBackupExtension) 192 if err != nil { 193 t.Fatalf("err: %s", err) 194 } 195 196 backupState, err := terraform.ReadState(f) 197 f.Close() 198 if err != nil { 199 t.Fatalf("err: %s", err) 200 } 201 202 actualStr = strings.TrimSpace(backupState.String()) 203 expectedStr = strings.TrimSpace(originalState.String()) 204 if actualStr != expectedStr { 205 t.Fatalf("bad:\n\nactual:\n%s\n\nexpected:\nb%s", actualStr, expectedStr) 206 } 207 } 208 209 const testApplyDestroyStr = ` 210 <no state> 211 `