2015-03-05 05:42:26 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2015-03-05 23:55:15 +01:00
|
|
|
"archive/tar"
|
2015-03-06 23:49:22 +01:00
|
|
|
"bytes"
|
2015-03-05 23:55:15 +01:00
|
|
|
"compress/gzip"
|
|
|
|
"io"
|
|
|
|
"os"
|
2016-08-26 22:38:02 +02:00
|
|
|
"path/filepath"
|
2015-03-05 23:55:15 +01:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
2017-03-16 18:47:48 +01:00
|
|
|
"strings"
|
2015-03-05 05:42:26 +01:00
|
|
|
"testing"
|
|
|
|
|
2016-07-25 21:56:56 +02:00
|
|
|
atlas "github.com/hashicorp/atlas-go/v1"
|
2017-03-16 18:47:48 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/copy"
|
2015-03-05 05:42:26 +01:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
2015-03-05 23:55:15 +01:00
|
|
|
func TestPush_good(t *testing.T) {
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
// Create remote state file, this should be pulled
|
|
|
|
conf, srv := testRemoteState(t, testState(), 200)
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
// Persist local remote state
|
|
|
|
s := terraform.NewState()
|
|
|
|
s.Serial = 5
|
|
|
|
s.Remote = conf
|
|
|
|
testStateFileRemote(t, s)
|
|
|
|
|
|
|
|
// Path where the archive will be "uploaded" to
|
|
|
|
archivePath := testTempFile(t)
|
|
|
|
defer os.Remove(archivePath)
|
|
|
|
|
|
|
|
client := &mockPushClient{File: archivePath}
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PushCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2015-03-05 23:55:15 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2015-04-10 01:46:25 +02:00
|
|
|
"-vcs=false",
|
2015-03-05 23:55:15 +01:00
|
|
|
testFixturePath("push"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := testArchiveStr(t, archivePath)
|
2015-03-06 08:10:28 +01:00
|
|
|
expected := []string{
|
|
|
|
".terraform/",
|
|
|
|
".terraform/terraform.tfstate",
|
|
|
|
"main.tf",
|
|
|
|
}
|
2015-03-05 23:55:15 +01:00
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
2015-03-06 09:06:54 +01:00
|
|
|
|
2016-07-18 19:52:10 +02:00
|
|
|
variables := make(map[string]interface{})
|
2015-03-06 09:06:54 +01:00
|
|
|
if !reflect.DeepEqual(client.UpsertOptions.Variables, variables) {
|
|
|
|
t.Fatalf("bad: %#v", client.UpsertOptions)
|
|
|
|
}
|
2015-03-07 00:12:39 +01:00
|
|
|
|
|
|
|
if client.UpsertOptions.Name != "foo" {
|
|
|
|
t.Fatalf("bad: %#v", client.UpsertOptions)
|
|
|
|
}
|
2015-03-05 23:55:15 +01:00
|
|
|
}
|
|
|
|
|
2017-03-16 18:52:58 +01:00
|
|
|
func TestPush_goodBackendInit(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("push-backend-new"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
// init backend
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
ci := &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if code := ci.Run(nil); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Path where the archive will be "uploaded" to
|
|
|
|
archivePath := testTempFile(t)
|
|
|
|
defer os.Remove(archivePath)
|
|
|
|
|
|
|
|
client := &mockPushClient{File: archivePath}
|
|
|
|
ui = new(cli.MockUi)
|
|
|
|
c := &PushCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2017-03-16 18:52:58 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-vcs=false",
|
|
|
|
td,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := testArchiveStr(t, archivePath)
|
|
|
|
expected := []string{
|
|
|
|
// Expected weird behavior, doesn't affect unpackaging
|
|
|
|
".terraform/",
|
|
|
|
".terraform/",
|
|
|
|
".terraform/terraform.tfstate",
|
|
|
|
".terraform/terraform.tfstate",
|
|
|
|
"main.tf",
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
variables := make(map[string]interface{})
|
|
|
|
if !reflect.DeepEqual(client.UpsertOptions.Variables, variables) {
|
|
|
|
t.Fatalf("bad: %#v", client.UpsertOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
if client.UpsertOptions.Name != "hello" {
|
|
|
|
t.Fatalf("bad: %#v", client.UpsertOptions)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 07:11:21 +02:00
|
|
|
func TestPush_noUploadModules(t *testing.T) {
|
|
|
|
// Path where the archive will be "uploaded" to
|
|
|
|
archivePath := testTempFile(t)
|
|
|
|
defer os.Remove(archivePath)
|
|
|
|
|
|
|
|
client := &mockPushClient{File: archivePath}
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PushCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2016-08-24 07:11:21 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Path of the test. We have to do some renaming to avoid our own
|
|
|
|
// VCS getting in the way.
|
2016-08-24 08:16:14 +02:00
|
|
|
path := testFixturePath("push-no-upload")
|
2016-08-26 22:38:02 +02:00
|
|
|
defer os.RemoveAll(filepath.Join(path, ".terraform"))
|
2016-08-24 07:11:21 +02:00
|
|
|
|
|
|
|
// Move into that directory
|
|
|
|
defer testChdir(t, path)()
|
|
|
|
|
2016-08-26 22:38:02 +02:00
|
|
|
// Do a "terraform get"
|
|
|
|
{
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &GetCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2016-08-26 22:38:02 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := c.Run([]string{}); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 07:11:21 +02:00
|
|
|
// Create remote state file, this should be pulled
|
|
|
|
conf, srv := testRemoteState(t, testState(), 200)
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
// Persist local remote state
|
|
|
|
s := terraform.NewState()
|
|
|
|
s.Serial = 5
|
|
|
|
s.Remote = conf
|
2016-08-24 07:51:37 +02:00
|
|
|
defer os.Remove(testStateFileRemote(t, s))
|
2016-08-24 07:11:21 +02:00
|
|
|
|
|
|
|
args := []string{
|
2016-08-25 23:09:34 +02:00
|
|
|
"-vcs=false",
|
2016-08-24 07:11:21 +02:00
|
|
|
"-name=mitchellh/tf-test",
|
|
|
|
"-upload-modules=false",
|
|
|
|
path,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2016-08-25 23:09:34 +02:00
|
|
|
// NOTE: The duplicates below are not ideal but are how things work
|
|
|
|
// currently due to how we manually add the files to the archive. This
|
|
|
|
// is definitely a "bug" we can fix in the future.
|
2016-08-24 07:11:21 +02:00
|
|
|
actual := testArchiveStr(t, archivePath)
|
|
|
|
expected := []string{
|
2016-08-24 19:39:50 +02:00
|
|
|
".terraform/",
|
2016-08-25 23:09:34 +02:00
|
|
|
".terraform/",
|
|
|
|
".terraform/terraform.tfstate",
|
2016-08-24 07:11:21 +02:00
|
|
|
".terraform/terraform.tfstate",
|
2016-08-24 08:12:52 +02:00
|
|
|
"child/",
|
|
|
|
"child/main.tf",
|
2016-08-24 07:11:21 +02:00
|
|
|
"main.tf",
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-06 23:49:22 +01:00
|
|
|
func TestPush_input(t *testing.T) {
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
// Create remote state file, this should be pulled
|
|
|
|
conf, srv := testRemoteState(t, testState(), 200)
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
// Persist local remote state
|
|
|
|
s := terraform.NewState()
|
|
|
|
s.Serial = 5
|
|
|
|
s.Remote = conf
|
|
|
|
testStateFileRemote(t, s)
|
|
|
|
|
|
|
|
// Path where the archive will be "uploaded" to
|
|
|
|
archivePath := testTempFile(t)
|
|
|
|
defer os.Remove(archivePath)
|
|
|
|
|
|
|
|
client := &mockPushClient{File: archivePath}
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PushCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2015-03-06 23:49:22 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable test mode so input would be asked and setup the
|
|
|
|
// input reader/writers.
|
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
defaultInputReader = bytes.NewBufferString("foo\n")
|
|
|
|
defaultInputWriter = new(bytes.Buffer)
|
|
|
|
|
|
|
|
args := []string{
|
2015-04-10 01:46:25 +02:00
|
|
|
"-vcs=false",
|
2015-03-06 23:49:22 +01:00
|
|
|
testFixturePath("push-input"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2016-07-18 19:52:10 +02:00
|
|
|
variables := map[string]interface{}{
|
2015-03-06 23:49:22 +01:00
|
|
|
"foo": "foo",
|
|
|
|
}
|
2016-07-28 17:11:53 +02:00
|
|
|
|
2015-03-06 23:49:22 +01:00
|
|
|
if !reflect.DeepEqual(client.UpsertOptions.Variables, variables) {
|
2015-06-26 00:29:46 +02:00
|
|
|
t.Fatalf("bad: %#v", client.UpsertOptions.Variables)
|
2015-03-06 23:49:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-28 17:11:53 +02:00
|
|
|
// We want a variable from atlas to fill a missing variable locally
|
2015-03-07 00:03:33 +01:00
|
|
|
func TestPush_inputPartial(t *testing.T) {
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
// Create remote state file, this should be pulled
|
|
|
|
conf, srv := testRemoteState(t, testState(), 200)
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
// Persist local remote state
|
|
|
|
s := terraform.NewState()
|
|
|
|
s.Serial = 5
|
|
|
|
s.Remote = conf
|
|
|
|
testStateFileRemote(t, s)
|
|
|
|
|
|
|
|
// Path where the archive will be "uploaded" to
|
|
|
|
archivePath := testTempFile(t)
|
|
|
|
defer os.Remove(archivePath)
|
|
|
|
|
|
|
|
client := &mockPushClient{
|
2016-07-28 17:11:53 +02:00
|
|
|
File: archivePath,
|
|
|
|
GetResult: map[string]atlas.TFVar{
|
|
|
|
"foo": atlas.TFVar{Key: "foo", Value: "bar"},
|
|
|
|
},
|
2015-03-07 00:03:33 +01:00
|
|
|
}
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PushCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2015-03-07 00:03:33 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable test mode so input would be asked and setup the
|
|
|
|
// input reader/writers.
|
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
defaultInputReader = bytes.NewBufferString("foo\n")
|
|
|
|
defaultInputWriter = new(bytes.Buffer)
|
|
|
|
|
|
|
|
args := []string{
|
2015-04-10 01:46:25 +02:00
|
|
|
"-vcs=false",
|
2015-03-07 00:03:33 +01:00
|
|
|
testFixturePath("push-input-partial"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2016-07-28 17:11:53 +02:00
|
|
|
expectedTFVars := []atlas.TFVar{
|
|
|
|
{Key: "bar", Value: "foo"},
|
|
|
|
{Key: "foo", Value: "bar"},
|
2015-03-07 00:03:33 +01:00
|
|
|
}
|
2016-07-28 17:11:53 +02:00
|
|
|
if !reflect.DeepEqual(client.UpsertOptions.TFVars, expectedTFVars) {
|
|
|
|
t.Logf("expected: %#v", expectedTFVars)
|
|
|
|
t.Fatalf("got: %#v", client.UpsertOptions.TFVars)
|
2015-03-07 00:03:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-29 21:24:13 +02:00
|
|
|
// This tests that the push command will override Atlas variables
|
|
|
|
// if requested.
|
|
|
|
func TestPush_localOverride(t *testing.T) {
|
2015-04-08 00:34:06 +02:00
|
|
|
// Disable test mode so input would be asked and setup the
|
|
|
|
// input reader/writers.
|
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
defaultInputReader = bytes.NewBufferString("nope\n")
|
|
|
|
defaultInputWriter = new(bytes.Buffer)
|
|
|
|
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
// Create remote state file, this should be pulled
|
|
|
|
conf, srv := testRemoteState(t, testState(), 200)
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
// Persist local remote state
|
|
|
|
s := terraform.NewState()
|
|
|
|
s.Serial = 5
|
|
|
|
s.Remote = conf
|
|
|
|
testStateFileRemote(t, s)
|
|
|
|
|
|
|
|
// Path where the archive will be "uploaded" to
|
|
|
|
archivePath := testTempFile(t)
|
|
|
|
defer os.Remove(archivePath)
|
|
|
|
|
|
|
|
client := &mockPushClient{File: archivePath}
|
2015-05-09 01:54:38 +02:00
|
|
|
// Provided vars should override existing ones
|
2016-07-28 17:11:53 +02:00
|
|
|
client.GetResult = map[string]atlas.TFVar{
|
|
|
|
"foo": atlas.TFVar{
|
|
|
|
Key: "foo",
|
|
|
|
Value: "old",
|
|
|
|
},
|
2015-05-09 01:54:38 +02:00
|
|
|
}
|
2015-04-08 00:34:06 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PushCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2015-04-08 00:34:06 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
|
2015-06-29 21:24:13 +02:00
|
|
|
path := testFixturePath("push-tfvars")
|
|
|
|
args := []string{
|
|
|
|
"-var-file", path + "/terraform.tfvars",
|
|
|
|
"-vcs=false",
|
2015-06-29 22:53:05 +02:00
|
|
|
"-overwrite=foo",
|
2015-06-29 21:24:13 +02:00
|
|
|
path,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := testArchiveStr(t, archivePath)
|
|
|
|
expected := []string{
|
|
|
|
".terraform/",
|
|
|
|
".terraform/terraform.tfstate",
|
|
|
|
"main.tf",
|
|
|
|
"terraform.tfvars",
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
if client.UpsertOptions.Name != "foo" {
|
|
|
|
t.Fatalf("bad: %#v", client.UpsertOptions)
|
|
|
|
}
|
|
|
|
|
2016-07-28 17:11:53 +02:00
|
|
|
expectedTFVars := pushTFVars()
|
2016-07-25 21:56:56 +02:00
|
|
|
|
2016-07-28 17:11:53 +02:00
|
|
|
if !reflect.DeepEqual(client.UpsertOptions.TFVars, expectedTFVars) {
|
|
|
|
t.Logf("expected: %#v", expectedTFVars)
|
|
|
|
t.Fatalf("got: %#v", client.UpsertOptions.TFVars)
|
2015-06-29 21:24:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-04 22:48:31 +02:00
|
|
|
// This tests that the push command will override Atlas variables
|
|
|
|
// even if we don't have it defined locally
|
|
|
|
func TestPush_remoteOverride(t *testing.T) {
|
|
|
|
// Disable test mode so input would be asked and setup the
|
|
|
|
// input reader/writers.
|
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
defaultInputReader = bytes.NewBufferString("nope\n")
|
|
|
|
defaultInputWriter = new(bytes.Buffer)
|
|
|
|
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
// Create remote state file, this should be pulled
|
|
|
|
conf, srv := testRemoteState(t, testState(), 200)
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
// Persist local remote state
|
|
|
|
s := terraform.NewState()
|
|
|
|
s.Serial = 5
|
|
|
|
s.Remote = conf
|
|
|
|
testStateFileRemote(t, s)
|
|
|
|
|
|
|
|
// Path where the archive will be "uploaded" to
|
|
|
|
archivePath := testTempFile(t)
|
|
|
|
defer os.Remove(archivePath)
|
|
|
|
|
|
|
|
client := &mockPushClient{File: archivePath}
|
|
|
|
// Provided vars should override existing ones
|
|
|
|
client.GetResult = map[string]atlas.TFVar{
|
|
|
|
"remote": atlas.TFVar{
|
|
|
|
Key: "remote",
|
|
|
|
Value: "old",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PushCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2016-08-04 22:48:31 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
|
|
|
|
path := testFixturePath("push-tfvars")
|
|
|
|
args := []string{
|
|
|
|
"-var-file", path + "/terraform.tfvars",
|
|
|
|
"-vcs=false",
|
|
|
|
"-overwrite=remote",
|
|
|
|
"-var",
|
|
|
|
"remote=new",
|
|
|
|
path,
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := testArchiveStr(t, archivePath)
|
|
|
|
expected := []string{
|
|
|
|
".terraform/",
|
|
|
|
".terraform/terraform.tfstate",
|
|
|
|
"main.tf",
|
|
|
|
"terraform.tfvars",
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
if client.UpsertOptions.Name != "foo" {
|
|
|
|
t.Fatalf("bad: %#v", client.UpsertOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
found := false
|
|
|
|
// find the "remote" var and make sure we're going to set it
|
|
|
|
for _, tfVar := range client.UpsertOptions.TFVars {
|
|
|
|
if tfVar.Key == "remote" {
|
|
|
|
found = true
|
|
|
|
if tfVar.Value != "new" {
|
|
|
|
t.Log("'remote' variable should be set to 'new'")
|
|
|
|
t.Fatalf("sending instead: %#v", tfVar)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
t.Fatal("'remote' variable not being sent to atlas")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-29 21:24:13 +02:00
|
|
|
// This tests that the push command prefers Atlas variables over
|
|
|
|
// local ones.
|
|
|
|
func TestPush_preferAtlas(t *testing.T) {
|
|
|
|
// Disable test mode so input would be asked and setup the
|
|
|
|
// input reader/writers.
|
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
defaultInputReader = bytes.NewBufferString("nope\n")
|
|
|
|
defaultInputWriter = new(bytes.Buffer)
|
|
|
|
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
// Create remote state file, this should be pulled
|
|
|
|
conf, srv := testRemoteState(t, testState(), 200)
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
// Persist local remote state
|
|
|
|
s := terraform.NewState()
|
|
|
|
s.Serial = 5
|
|
|
|
s.Remote = conf
|
|
|
|
testStateFileRemote(t, s)
|
|
|
|
|
|
|
|
// Path where the archive will be "uploaded" to
|
|
|
|
archivePath := testTempFile(t)
|
|
|
|
defer os.Remove(archivePath)
|
|
|
|
|
|
|
|
client := &mockPushClient{File: archivePath}
|
|
|
|
// Provided vars should override existing ones
|
2016-07-28 17:11:53 +02:00
|
|
|
client.GetResult = map[string]atlas.TFVar{
|
|
|
|
"foo": atlas.TFVar{
|
|
|
|
Key: "foo",
|
|
|
|
Value: "old",
|
|
|
|
},
|
2015-06-29 21:24:13 +02:00
|
|
|
}
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PushCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2015-06-29 21:24:13 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
|
|
|
|
path := testFixturePath("push-tfvars")
|
|
|
|
args := []string{
|
|
|
|
"-var-file", path + "/terraform.tfvars",
|
|
|
|
"-vcs=false",
|
|
|
|
path,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := testArchiveStr(t, archivePath)
|
|
|
|
expected := []string{
|
|
|
|
".terraform/",
|
|
|
|
".terraform/terraform.tfstate",
|
|
|
|
"main.tf",
|
|
|
|
"terraform.tfvars",
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
if client.UpsertOptions.Name != "foo" {
|
|
|
|
t.Fatalf("bad: %#v", client.UpsertOptions)
|
|
|
|
}
|
|
|
|
|
2016-07-28 17:11:53 +02:00
|
|
|
// change the expected response to match our change
|
|
|
|
expectedTFVars := pushTFVars()
|
|
|
|
for i, v := range expectedTFVars {
|
|
|
|
if v.Key == "foo" {
|
|
|
|
expectedTFVars[i] = atlas.TFVar{Key: "foo", Value: "old"}
|
|
|
|
}
|
|
|
|
}
|
2016-07-25 21:56:56 +02:00
|
|
|
|
2016-07-28 17:11:53 +02:00
|
|
|
if !reflect.DeepEqual(expectedTFVars, client.UpsertOptions.TFVars) {
|
|
|
|
t.Logf("expected: %#v", expectedTFVars)
|
|
|
|
t.Fatalf("got: %#v", client.UpsertOptions.TFVars)
|
2015-06-29 21:24:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This tests that the push command will send the variables in tfvars
|
|
|
|
func TestPush_tfvars(t *testing.T) {
|
|
|
|
// Disable test mode so input would be asked and setup the
|
|
|
|
// input reader/writers.
|
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
defaultInputReader = bytes.NewBufferString("nope\n")
|
|
|
|
defaultInputWriter = new(bytes.Buffer)
|
|
|
|
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
// Create remote state file, this should be pulled
|
|
|
|
conf, srv := testRemoteState(t, testState(), 200)
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
// Persist local remote state
|
|
|
|
s := terraform.NewState()
|
|
|
|
s.Serial = 5
|
|
|
|
s.Remote = conf
|
|
|
|
testStateFileRemote(t, s)
|
|
|
|
|
|
|
|
// Path where the archive will be "uploaded" to
|
|
|
|
archivePath := testTempFile(t)
|
|
|
|
defer os.Remove(archivePath)
|
|
|
|
|
|
|
|
client := &mockPushClient{File: archivePath}
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PushCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2015-06-29 21:24:13 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:34:06 +02:00
|
|
|
path := testFixturePath("push-tfvars")
|
|
|
|
args := []string{
|
|
|
|
"-var-file", path + "/terraform.tfvars",
|
2015-04-10 01:46:25 +02:00
|
|
|
"-vcs=false",
|
2016-08-04 23:19:02 +02:00
|
|
|
"-var",
|
2016-10-06 22:37:28 +02:00
|
|
|
"bar=[1,2]",
|
2015-04-08 00:34:06 +02:00
|
|
|
path,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := testArchiveStr(t, archivePath)
|
|
|
|
expected := []string{
|
|
|
|
".terraform/",
|
|
|
|
".terraform/terraform.tfstate",
|
|
|
|
"main.tf",
|
|
|
|
"terraform.tfvars",
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
if client.UpsertOptions.Name != "foo" {
|
|
|
|
t.Fatalf("bad: %#v", client.UpsertOptions)
|
|
|
|
}
|
|
|
|
|
2016-07-27 02:37:41 +02:00
|
|
|
//now check TFVars
|
2016-07-28 17:11:53 +02:00
|
|
|
tfvars := pushTFVars()
|
2016-08-04 23:19:02 +02:00
|
|
|
// update bar to match cli value
|
|
|
|
for i, v := range tfvars {
|
|
|
|
if v.Key == "bar" {
|
2016-10-06 22:37:28 +02:00
|
|
|
tfvars[i].Value = "[1, 2]"
|
2016-08-04 23:19:02 +02:00
|
|
|
tfvars[i].IsHCL = true
|
|
|
|
}
|
|
|
|
}
|
2016-07-25 21:56:56 +02:00
|
|
|
|
2016-07-27 02:37:41 +02:00
|
|
|
for i, expected := range tfvars {
|
|
|
|
got := client.UpsertOptions.TFVars[i]
|
|
|
|
if got != expected {
|
|
|
|
t.Logf("%2d expected: %#v", i, expected)
|
2016-08-04 23:19:02 +02:00
|
|
|
t.Fatalf(" got: %#v", got)
|
2016-07-27 02:37:41 +02:00
|
|
|
}
|
2015-04-08 00:34:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-07 00:14:41 +01:00
|
|
|
func TestPush_name(t *testing.T) {
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
// Create remote state file, this should be pulled
|
|
|
|
conf, srv := testRemoteState(t, testState(), 200)
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
// Persist local remote state
|
|
|
|
s := terraform.NewState()
|
|
|
|
s.Serial = 5
|
|
|
|
s.Remote = conf
|
|
|
|
testStateFileRemote(t, s)
|
|
|
|
|
|
|
|
// Path where the archive will be "uploaded" to
|
|
|
|
archivePath := testTempFile(t)
|
|
|
|
defer os.Remove(archivePath)
|
|
|
|
|
|
|
|
client := &mockPushClient{File: archivePath}
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PushCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2015-03-07 00:14:41 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-name", "bar",
|
2015-04-10 01:46:25 +02:00
|
|
|
"-vcs=false",
|
2015-03-07 00:14:41 +01:00
|
|
|
testFixturePath("push"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if client.UpsertOptions.Name != "bar" {
|
|
|
|
t.Fatalf("bad: %#v", client.UpsertOptions)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-05 05:42:26 +01:00
|
|
|
func TestPush_noState(t *testing.T) {
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PushCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2015-03-05 05:42:26 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPush_noRemoteState(t *testing.T) {
|
2017-03-16 18:47:48 +01:00
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("push-no-remote"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2015-03-05 05:42:26 +01:00
|
|
|
state := &terraform.State{
|
|
|
|
Modules: []*terraform.ModuleState{
|
|
|
|
&terraform.ModuleState{
|
|
|
|
Path: []string{"root"},
|
|
|
|
Resources: map[string]*terraform.ResourceState{
|
|
|
|
"test_instance.foo": &terraform.ResourceState{
|
|
|
|
Type: "test_instance",
|
|
|
|
Primary: &terraform.InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
2017-03-16 18:47:48 +01:00
|
|
|
// Path where the archive will be "uploaded" to
|
|
|
|
archivePath := testTempFile(t)
|
|
|
|
defer os.Remove(archivePath)
|
|
|
|
|
|
|
|
client := &mockPushClient{File: archivePath}
|
2015-03-05 05:42:26 +01:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PushCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
Ui: ui,
|
|
|
|
},
|
2017-03-16 18:47:48 +01:00
|
|
|
client: client,
|
2015-03-05 05:42:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2017-03-16 18:47:48 +01:00
|
|
|
"-vcs=false",
|
2015-03-05 05:42:26 +01:00
|
|
|
"-state", statePath,
|
2017-03-16 18:47:48 +01:00
|
|
|
td,
|
2015-03-05 05:42:26 +01:00
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
2017-03-16 18:47:48 +01:00
|
|
|
|
|
|
|
errStr := ui.ErrorWriter.String()
|
2017-03-16 22:34:59 +01:00
|
|
|
if !strings.Contains(errStr, "remote backend") {
|
2017-03-16 18:47:48 +01:00
|
|
|
t.Fatalf("bad: %s", errStr)
|
|
|
|
}
|
2015-03-05 05:42:26 +01:00
|
|
|
}
|
2015-03-05 23:55:15 +01:00
|
|
|
|
|
|
|
func TestPush_plan(t *testing.T) {
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
// Create remote state file, this should be pulled
|
|
|
|
conf, srv := testRemoteState(t, testState(), 200)
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
// Persist local remote state
|
|
|
|
s := terraform.NewState()
|
|
|
|
s.Serial = 5
|
|
|
|
s.Remote = conf
|
|
|
|
testStateFileRemote(t, s)
|
|
|
|
|
|
|
|
// Create a plan
|
|
|
|
planPath := testPlanFile(t, &terraform.Plan{
|
|
|
|
Module: testModule(t, "apply"),
|
|
|
|
})
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PushCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2015-03-05 23:55:15 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{planPath}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testArchiveStr(t *testing.T, path string) []string {
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
// Ungzip
|
|
|
|
gzipR, err := gzip.NewReader(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accumulator
|
|
|
|
result := make([]string, 0, 10)
|
|
|
|
|
|
|
|
// Untar
|
|
|
|
tarR := tar.NewReader(gzipR)
|
|
|
|
for {
|
|
|
|
header, err := tarR.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, header.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(result)
|
|
|
|
return result
|
|
|
|
}
|
2016-07-25 21:56:56 +02:00
|
|
|
|
2017-02-25 00:37:06 +01:00
|
|
|
// we always quote map keys to be safe
|
2016-07-28 17:11:53 +02:00
|
|
|
func pushTFVars() []atlas.TFVar {
|
|
|
|
return []atlas.TFVar{
|
2016-10-18 17:11:12 +02:00
|
|
|
{Key: "bar", Value: "foo", IsHCL: false},
|
|
|
|
{Key: "baz", Value: `{
|
2017-02-25 00:37:06 +01:00
|
|
|
"A" = "a"
|
2016-10-18 17:11:12 +02:00
|
|
|
}`, IsHCL: true},
|
|
|
|
{Key: "fob", Value: `["a", "quotes \"in\" quotes"]`, IsHCL: true},
|
|
|
|
{Key: "foo", Value: "bar", IsHCL: false},
|
2016-07-28 17:11:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-25 21:56:56 +02:00
|
|
|
// the structure returned from the push-tfvars test fixture
|
2016-07-28 17:11:53 +02:00
|
|
|
func pushTFVarsMap() map[string]atlas.TFVar {
|
|
|
|
vars := make(map[string]atlas.TFVar)
|
|
|
|
for _, v := range pushTFVars() {
|
|
|
|
vars[v.Key] = v
|
2016-07-25 21:56:56 +02:00
|
|
|
}
|
2016-07-28 17:11:53 +02:00
|
|
|
return vars
|
2016-07-25 21:56:56 +02:00
|
|
|
}
|