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"
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
2015-03-05 05:42:26 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"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{
|
|
|
|
ContextOpts: testCtxConfig(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
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
|
|
|
|
|
|
|
variables := make(map[string]string)
|
|
|
|
if !reflect.DeepEqual(client.UpsertOptions.Variables, variables) {
|
|
|
|
t.Fatalf("bad: %#v", client.UpsertOptions)
|
|
|
|
}
|
2015-03-05 23:55:15 +01:00
|
|
|
}
|
|
|
|
|
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{
|
|
|
|
ContextOpts: testCtxConfig(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
|
|
|
|
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{
|
|
|
|
testFixturePath("push-input"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
variables := map[string]string{
|
|
|
|
"foo": "foo",
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(client.UpsertOptions.Variables, variables) {
|
|
|
|
t.Fatalf("bad: %#v", client.UpsertOptions)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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{
|
|
|
|
File: archivePath,
|
|
|
|
GetResult: map[string]string{"foo": "bar"},
|
|
|
|
}
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PushCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
ContextOpts: testCtxConfig(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
|
|
|
|
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{
|
|
|
|
testFixturePath("push-input-partial"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
variables := map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"bar": "foo",
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(client.UpsertOptions.Variables, variables) {
|
|
|
|
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{
|
|
|
|
ContextOpts: testCtxConfig(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPush_noRemoteState(t *testing.T) {
|
|
|
|
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)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PushCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
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{
|
|
|
|
ContextOpts: testCtxConfig(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|