github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/command/state_push_test.go (about) 1 package command 2 3 import ( 4 "bytes" 5 "os" 6 "testing" 7 8 "github.com/hashicorp/terraform/backend" 9 "github.com/hashicorp/terraform/backend/remote-state/inmem" 10 "github.com/hashicorp/terraform/helper/copy" 11 "github.com/hashicorp/terraform/terraform" 12 "github.com/mitchellh/cli" 13 ) 14 15 func TestStatePush_empty(t *testing.T) { 16 // Create a temporary working directory that is empty 17 td := tempDir(t) 18 copy.CopyDir(testFixturePath("state-push-good"), td) 19 defer os.RemoveAll(td) 20 defer testChdir(t, td)() 21 22 expected := testStateRead(t, "replace.tfstate") 23 24 p := testProvider() 25 ui := new(cli.MockUi) 26 c := &StatePushCommand{ 27 Meta: Meta{ 28 testingOverrides: metaOverridesForProvider(p), 29 Ui: ui, 30 }, 31 } 32 33 args := []string{"replace.tfstate"} 34 if code := c.Run(args); code != 0 { 35 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 36 } 37 38 actual := testStateRead(t, "local-state.tfstate") 39 if !actual.Equal(expected) { 40 t.Fatalf("bad: %#v", actual) 41 } 42 } 43 44 func TestStatePush_replaceMatch(t *testing.T) { 45 // Create a temporary working directory that is empty 46 td := tempDir(t) 47 copy.CopyDir(testFixturePath("state-push-replace-match"), td) 48 defer os.RemoveAll(td) 49 defer testChdir(t, td)() 50 51 expected := testStateRead(t, "replace.tfstate") 52 53 p := testProvider() 54 ui := new(cli.MockUi) 55 c := &StatePushCommand{ 56 Meta: Meta{ 57 testingOverrides: metaOverridesForProvider(p), 58 Ui: ui, 59 }, 60 } 61 62 args := []string{"replace.tfstate"} 63 if code := c.Run(args); code != 0 { 64 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 65 } 66 67 actual := testStateRead(t, "local-state.tfstate") 68 if !actual.Equal(expected) { 69 t.Fatalf("bad: %#v", actual) 70 } 71 } 72 73 func TestStatePush_replaceMatchStdin(t *testing.T) { 74 // Create a temporary working directory that is empty 75 td := tempDir(t) 76 copy.CopyDir(testFixturePath("state-push-replace-match"), td) 77 defer os.RemoveAll(td) 78 defer testChdir(t, td)() 79 80 expected := testStateRead(t, "replace.tfstate") 81 82 // Setup the replacement to come from stdin 83 var buf bytes.Buffer 84 if err := terraform.WriteState(expected, &buf); err != nil { 85 t.Fatalf("err: %s", err) 86 } 87 defer testStdinPipe(t, &buf)() 88 89 p := testProvider() 90 ui := new(cli.MockUi) 91 c := &StatePushCommand{ 92 Meta: Meta{ 93 testingOverrides: metaOverridesForProvider(p), 94 Ui: ui, 95 }, 96 } 97 98 args := []string{"-"} 99 if code := c.Run(args); code != 0 { 100 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 101 } 102 103 actual := testStateRead(t, "local-state.tfstate") 104 if !actual.Equal(expected) { 105 t.Fatalf("bad: %#v", actual) 106 } 107 } 108 109 func TestStatePush_lineageMismatch(t *testing.T) { 110 // Create a temporary working directory that is empty 111 td := tempDir(t) 112 copy.CopyDir(testFixturePath("state-push-bad-lineage"), td) 113 defer os.RemoveAll(td) 114 defer testChdir(t, td)() 115 116 expected := testStateRead(t, "local-state.tfstate") 117 118 p := testProvider() 119 ui := new(cli.MockUi) 120 c := &StatePushCommand{ 121 Meta: Meta{ 122 testingOverrides: metaOverridesForProvider(p), 123 Ui: ui, 124 }, 125 } 126 127 args := []string{"replace.tfstate"} 128 if code := c.Run(args); code != 1 { 129 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 130 } 131 132 actual := testStateRead(t, "local-state.tfstate") 133 if !actual.Equal(expected) { 134 t.Fatalf("bad: %#v", actual) 135 } 136 } 137 138 func TestStatePush_serialNewer(t *testing.T) { 139 // Create a temporary working directory that is empty 140 td := tempDir(t) 141 copy.CopyDir(testFixturePath("state-push-serial-newer"), td) 142 defer os.RemoveAll(td) 143 defer testChdir(t, td)() 144 145 expected := testStateRead(t, "local-state.tfstate") 146 147 p := testProvider() 148 ui := new(cli.MockUi) 149 c := &StatePushCommand{ 150 Meta: Meta{ 151 testingOverrides: metaOverridesForProvider(p), 152 Ui: ui, 153 }, 154 } 155 156 args := []string{"replace.tfstate"} 157 if code := c.Run(args); code != 1 { 158 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 159 } 160 161 actual := testStateRead(t, "local-state.tfstate") 162 if !actual.Equal(expected) { 163 t.Fatalf("bad: %#v", actual) 164 } 165 } 166 167 func TestStatePush_serialOlder(t *testing.T) { 168 // Create a temporary working directory that is empty 169 td := tempDir(t) 170 copy.CopyDir(testFixturePath("state-push-serial-older"), td) 171 defer os.RemoveAll(td) 172 defer testChdir(t, td)() 173 174 expected := testStateRead(t, "replace.tfstate") 175 176 p := testProvider() 177 ui := new(cli.MockUi) 178 c := &StatePushCommand{ 179 Meta: Meta{ 180 testingOverrides: metaOverridesForProvider(p), 181 Ui: ui, 182 }, 183 } 184 185 args := []string{"replace.tfstate"} 186 if code := c.Run(args); code != 0 { 187 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 188 } 189 190 actual := testStateRead(t, "local-state.tfstate") 191 if !actual.Equal(expected) { 192 t.Fatalf("bad: %#v", actual) 193 } 194 } 195 196 func TestStatePush_forceRemoteState(t *testing.T) { 197 td := tempDir(t) 198 copy.CopyDir(testFixturePath("inmem-backend"), td) 199 defer os.RemoveAll(td) 200 defer testChdir(t, td)() 201 defer inmem.Reset() 202 203 s := terraform.NewState() 204 statePath := testStateFile(t, s) 205 206 // init the backend 207 ui := new(cli.MockUi) 208 initCmd := &InitCommand{ 209 Meta: Meta{Ui: ui}, 210 } 211 if code := initCmd.Run([]string{}); code != 0 { 212 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 213 } 214 215 // create a new workspace 216 ui = new(cli.MockUi) 217 newCmd := &WorkspaceNewCommand{ 218 Meta: Meta{Ui: ui}, 219 } 220 if code := newCmd.Run([]string{"test"}); code != 0 { 221 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter) 222 } 223 224 // put a dummy state in place, so we have something to force 225 b := backend.TestBackendConfig(t, inmem.New(), nil) 226 sMgr, err := b.State("test") 227 if err != nil { 228 t.Fatal(err) 229 } 230 if err := sMgr.WriteState(terraform.NewState()); err != nil { 231 t.Fatal(err) 232 } 233 if err := sMgr.PersistState(); err != nil { 234 t.Fatal(err) 235 } 236 237 // push our local state to that new workspace 238 ui = new(cli.MockUi) 239 c := &StatePushCommand{ 240 Meta: Meta{Ui: ui}, 241 } 242 243 args := []string{"-force", statePath} 244 if code := c.Run(args); code != 0 { 245 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 246 } 247 }