@@ -179,7 +179,9 @@ func TestPush_inputPartial(t *testing.T) {
179179 }
180180}
181181
182- func TestPush_inputTfvars (t * testing.T ) {
182+ // This tests that the push command will override Atlas variables
183+ // if requested.
184+ func TestPush_localOverride (t * testing.T ) {
183185 // Disable test mode so input would be asked and setup the
184186 // input reader/writers.
185187 test = false
@@ -219,6 +221,154 @@ func TestPush_inputTfvars(t *testing.T) {
219221 client : client ,
220222 }
221223
224+ path := testFixturePath ("push-tfvars" )
225+ args := []string {
226+ "-var-file" , path + "/terraform.tfvars" ,
227+ "-vcs=false" ,
228+ "-set=foo" ,
229+ path ,
230+ }
231+ if code := c .Run (args ); code != 0 {
232+ t .Fatalf ("bad: %d\n \n %s" , code , ui .ErrorWriter .String ())
233+ }
234+
235+ actual := testArchiveStr (t , archivePath )
236+ expected := []string {
237+ ".terraform/" ,
238+ ".terraform/terraform.tfstate" ,
239+ "main.tf" ,
240+ "terraform.tfvars" ,
241+ }
242+ if ! reflect .DeepEqual (actual , expected ) {
243+ t .Fatalf ("bad: %#v" , actual )
244+ }
245+
246+ if client .UpsertOptions .Name != "foo" {
247+ t .Fatalf ("bad: %#v" , client .UpsertOptions )
248+ }
249+
250+ variables := map [string ]string {
251+ "foo" : "bar" ,
252+ "bar" : "foo" ,
253+ }
254+ if ! reflect .DeepEqual (client .UpsertOptions .Variables , variables ) {
255+ t .Fatalf ("bad: %#v" , client .UpsertOptions )
256+ }
257+ }
258+
259+ // This tests that the push command prefers Atlas variables over
260+ // local ones.
261+ func TestPush_preferAtlas (t * testing.T ) {
262+ // Disable test mode so input would be asked and setup the
263+ // input reader/writers.
264+ test = false
265+ defer func () { test = true }()
266+ defaultInputReader = bytes .NewBufferString ("nope\n " )
267+ defaultInputWriter = new (bytes.Buffer )
268+
269+ tmp , cwd := testCwd (t )
270+ defer testFixCwd (t , tmp , cwd )
271+
272+ // Create remote state file, this should be pulled
273+ conf , srv := testRemoteState (t , testState (), 200 )
274+ defer srv .Close ()
275+
276+ // Persist local remote state
277+ s := terraform .NewState ()
278+ s .Serial = 5
279+ s .Remote = conf
280+ testStateFileRemote (t , s )
281+
282+ // Path where the archive will be "uploaded" to
283+ archivePath := testTempFile (t )
284+ defer os .Remove (archivePath )
285+
286+ client := & mockPushClient {File : archivePath }
287+ // Provided vars should override existing ones
288+ client .GetResult = map [string ]string {
289+ "foo" : "old" ,
290+ }
291+ ui := new (cli.MockUi )
292+ c := & PushCommand {
293+ Meta : Meta {
294+ ContextOpts : testCtxConfig (testProvider ()),
295+ Ui : ui ,
296+ },
297+
298+ client : client ,
299+ }
300+
301+ path := testFixturePath ("push-tfvars" )
302+ args := []string {
303+ "-var-file" , path + "/terraform.tfvars" ,
304+ "-vcs=false" ,
305+ path ,
306+ }
307+ if code := c .Run (args ); code != 0 {
308+ t .Fatalf ("bad: %d\n \n %s" , code , ui .ErrorWriter .String ())
309+ }
310+
311+ actual := testArchiveStr (t , archivePath )
312+ expected := []string {
313+ ".terraform/" ,
314+ ".terraform/terraform.tfstate" ,
315+ "main.tf" ,
316+ "terraform.tfvars" ,
317+ }
318+ if ! reflect .DeepEqual (actual , expected ) {
319+ t .Fatalf ("bad: %#v" , actual )
320+ }
321+
322+ if client .UpsertOptions .Name != "foo" {
323+ t .Fatalf ("bad: %#v" , client .UpsertOptions )
324+ }
325+
326+ variables := map [string ]string {
327+ "foo" : "old" ,
328+ "bar" : "foo" ,
329+ }
330+ if ! reflect .DeepEqual (client .UpsertOptions .Variables , variables ) {
331+ t .Fatalf ("bad: %#v" , client .UpsertOptions )
332+ }
333+ }
334+
335+ // This tests that the push command will send the variables in tfvars
336+ func TestPush_tfvars (t * testing.T ) {
337+ // Disable test mode so input would be asked and setup the
338+ // input reader/writers.
339+ test = false
340+ defer func () { test = true }()
341+ defaultInputReader = bytes .NewBufferString ("nope\n " )
342+ defaultInputWriter = new (bytes.Buffer )
343+
344+ tmp , cwd := testCwd (t )
345+ defer testFixCwd (t , tmp , cwd )
346+
347+ // Create remote state file, this should be pulled
348+ conf , srv := testRemoteState (t , testState (), 200 )
349+ defer srv .Close ()
350+
351+ // Persist local remote state
352+ s := terraform .NewState ()
353+ s .Serial = 5
354+ s .Remote = conf
355+ testStateFileRemote (t , s )
356+
357+ // Path where the archive will be "uploaded" to
358+ archivePath := testTempFile (t )
359+ defer os .Remove (archivePath )
360+
361+ client := & mockPushClient {File : archivePath }
362+ ui := new (cli.MockUi )
363+ c := & PushCommand {
364+ Meta : Meta {
365+ ContextOpts : testCtxConfig (testProvider ()),
366+ Ui : ui ,
367+ },
368+
369+ client : client ,
370+ }
371+
222372 path := testFixturePath ("push-tfvars" )
223373 args := []string {
224374 "-var-file" , path + "/terraform.tfvars" ,
0 commit comments