2014-07-13 19:25:42 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestOutput(t *testing.T) {
|
|
|
|
originalState := &terraform.State{
|
2014-09-17 20:15:07 +02:00
|
|
|
Modules: []*terraform.ModuleState{
|
2016-07-13 18:38:19 +02:00
|
|
|
{
|
2014-09-17 20:15:07 +02:00
|
|
|
Path: []string{"root"},
|
2016-05-12 02:05:02 +02:00
|
|
|
Outputs: map[string]*terraform.OutputState{
|
2016-07-13 18:38:19 +02:00
|
|
|
"foo": {
|
2016-05-12 02:05:02 +02:00
|
|
|
Value: "bar",
|
|
|
|
Type: "string",
|
|
|
|
},
|
2014-09-17 20:15:07 +02:00
|
|
|
},
|
|
|
|
},
|
2014-07-13 19:25:42 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
statePath := testStateFile(t, originalState)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &OutputCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2014-07-13 19:25:42 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"foo",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(ui.OutputWriter.String())
|
|
|
|
if actual != "bar" {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 16:46:12 +02:00
|
|
|
func TestModuleOutput(t *testing.T) {
|
|
|
|
originalState := &terraform.State{
|
|
|
|
Modules: []*terraform.ModuleState{
|
2016-07-13 18:38:19 +02:00
|
|
|
{
|
2015-05-27 16:46:12 +02:00
|
|
|
Path: []string{"root"},
|
2016-05-12 02:05:02 +02:00
|
|
|
Outputs: map[string]*terraform.OutputState{
|
2016-07-13 18:38:19 +02:00
|
|
|
"foo": {
|
2016-05-12 02:05:02 +02:00
|
|
|
Value: "bar",
|
|
|
|
Type: "string",
|
|
|
|
},
|
2015-05-27 16:46:12 +02:00
|
|
|
},
|
|
|
|
},
|
2016-07-13 18:38:19 +02:00
|
|
|
{
|
2015-05-27 16:46:12 +02:00
|
|
|
Path: []string{"root", "my_module"},
|
2016-05-12 02:05:02 +02:00
|
|
|
Outputs: map[string]*terraform.OutputState{
|
2016-07-13 18:38:19 +02:00
|
|
|
"blah": {
|
2016-05-12 02:05:02 +02:00
|
|
|
Value: "tastatur",
|
|
|
|
Type: "string",
|
|
|
|
},
|
2015-05-27 16:46:12 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
statePath := testStateFile(t, originalState)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &OutputCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2015-05-27 16:46:12 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-module", "my_module",
|
|
|
|
"blah",
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(ui.OutputWriter.String())
|
|
|
|
if actual != "tastatur" {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-29 21:13:50 +02:00
|
|
|
func TestModuleOutputs(t *testing.T) {
|
|
|
|
originalState := &terraform.State{
|
|
|
|
Modules: []*terraform.ModuleState{
|
|
|
|
{
|
|
|
|
Path: []string{"root"},
|
|
|
|
Outputs: map[string]*terraform.OutputState{
|
|
|
|
"foo": {
|
|
|
|
Value: "bar",
|
|
|
|
Type: "string",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Path: []string{"root", "my_module"},
|
|
|
|
Outputs: map[string]*terraform.OutputState{
|
|
|
|
"blah": {
|
|
|
|
Value: "tastatur",
|
|
|
|
Type: "string",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
statePath := testStateFile(t, originalState)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &OutputCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2016-07-29 21:13:50 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-module", "my_module",
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(ui.OutputWriter.String())
|
|
|
|
if actual != "blah = tastatur" {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-13 18:38:19 +02:00
|
|
|
func TestOutput_nestedListAndMap(t *testing.T) {
|
|
|
|
originalState := &terraform.State{
|
|
|
|
Modules: []*terraform.ModuleState{
|
|
|
|
{
|
|
|
|
Path: []string{"root"},
|
|
|
|
Outputs: map[string]*terraform.OutputState{
|
|
|
|
"foo": {
|
|
|
|
Value: []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"key": "value",
|
|
|
|
"key2": "value2",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"key": "value",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Type: "list",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
statePath := testStateFile(t, originalState)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &OutputCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2016-07-13 18:38:19 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(ui.OutputWriter.String())
|
|
|
|
expected := "foo = [\n {\n key = value,\n key2 = value2\n },\n {\n key = value\n }\n]"
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n%#v\n%#v", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOutput_json(t *testing.T) {
|
|
|
|
originalState := &terraform.State{
|
|
|
|
Modules: []*terraform.ModuleState{
|
|
|
|
{
|
|
|
|
Path: []string{"root"},
|
|
|
|
Outputs: map[string]*terraform.OutputState{
|
|
|
|
"foo": {
|
|
|
|
Value: "bar",
|
|
|
|
Type: "string",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
statePath := testStateFile(t, originalState)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &OutputCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2016-07-13 18:38:19 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-json",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(ui.OutputWriter.String())
|
|
|
|
expected := "{\n \"foo\": {\n \"sensitive\": false,\n \"type\": \"string\",\n \"value\": \"bar\"\n }\n}"
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n%#v\n%#v", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 16:46:12 +02:00
|
|
|
func TestMissingModuleOutput(t *testing.T) {
|
|
|
|
originalState := &terraform.State{
|
|
|
|
Modules: []*terraform.ModuleState{
|
2016-07-13 18:38:19 +02:00
|
|
|
{
|
2015-05-27 16:46:12 +02:00
|
|
|
Path: []string{"root"},
|
2016-05-12 02:05:02 +02:00
|
|
|
Outputs: map[string]*terraform.OutputState{
|
2016-07-13 18:38:19 +02:00
|
|
|
"foo": {
|
2016-05-12 02:05:02 +02:00
|
|
|
Value: "bar",
|
|
|
|
Type: "string",
|
|
|
|
},
|
2015-05-27 16:46:12 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
statePath := testStateFile(t, originalState)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &OutputCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2015-05-27 16:46:12 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-module", "not_existing_module",
|
|
|
|
"blah",
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-13 19:25:42 +02:00
|
|
|
func TestOutput_badVar(t *testing.T) {
|
|
|
|
originalState := &terraform.State{
|
2014-09-17 20:15:07 +02:00
|
|
|
Modules: []*terraform.ModuleState{
|
2016-07-13 18:38:19 +02:00
|
|
|
{
|
2014-09-17 20:15:07 +02:00
|
|
|
Path: []string{"root"},
|
2016-05-12 02:05:02 +02:00
|
|
|
Outputs: map[string]*terraform.OutputState{
|
2016-07-13 18:38:19 +02:00
|
|
|
"foo": {
|
2016-05-12 02:05:02 +02:00
|
|
|
Value: "bar",
|
|
|
|
Type: "string",
|
|
|
|
},
|
2014-09-17 20:15:07 +02:00
|
|
|
},
|
|
|
|
},
|
2014-07-13 19:25:42 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
statePath := testStateFile(t, originalState)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &OutputCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2014-07-13 19:25:42 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"bar",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-13 19:29:31 +02:00
|
|
|
func TestOutput_blank(t *testing.T) {
|
|
|
|
originalState := &terraform.State{
|
2014-09-17 20:15:07 +02:00
|
|
|
Modules: []*terraform.ModuleState{
|
2016-07-13 18:38:19 +02:00
|
|
|
{
|
2014-09-17 20:15:07 +02:00
|
|
|
Path: []string{"root"},
|
2016-05-12 02:05:02 +02:00
|
|
|
Outputs: map[string]*terraform.OutputState{
|
2016-07-13 18:38:19 +02:00
|
|
|
"foo": {
|
2016-05-12 02:05:02 +02:00
|
|
|
Value: "bar",
|
|
|
|
Type: "string",
|
|
|
|
},
|
2016-07-13 18:38:19 +02:00
|
|
|
"name": {
|
2016-05-12 02:05:02 +02:00
|
|
|
Value: "john-doe",
|
|
|
|
Type: "string",
|
|
|
|
},
|
2014-09-17 20:15:07 +02:00
|
|
|
},
|
|
|
|
},
|
2014-07-13 19:29:31 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
statePath := testStateFile(t, originalState)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &OutputCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2014-07-13 19:29:31 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"",
|
|
|
|
}
|
2015-08-03 13:44:41 +02:00
|
|
|
|
|
|
|
if code := c.Run(args); code != 0 {
|
2014-07-13 19:29:31 +02:00
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
2015-08-03 13:44:41 +02:00
|
|
|
|
|
|
|
expectedOutput := "foo = bar\nname = john-doe\n"
|
|
|
|
output := ui.OutputWriter.String()
|
|
|
|
if output != expectedOutput {
|
|
|
|
t.Fatalf("Expected output: %#v\ngiven: %#v", expectedOutput, output)
|
|
|
|
}
|
2014-07-13 19:29:31 +02:00
|
|
|
}
|
|
|
|
|
2014-07-13 19:25:42 +02:00
|
|
|
func TestOutput_manyArgs(t *testing.T) {
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &OutputCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2014-07-13 19:25:42 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"bad",
|
|
|
|
"bad",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOutput_noArgs(t *testing.T) {
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &OutputCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2014-07-13 19:25:42 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 00:35:27 +01:00
|
|
|
func TestOutput_noState(t *testing.T) {
|
|
|
|
originalState := &terraform.State{}
|
|
|
|
statePath := testStateFile(t, originalState)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &OutputCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2015-03-26 00:35:27 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"foo",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-13 19:25:42 +02:00
|
|
|
func TestOutput_noVars(t *testing.T) {
|
|
|
|
originalState := &terraform.State{
|
2014-09-17 20:15:07 +02:00
|
|
|
Modules: []*terraform.ModuleState{
|
2016-07-13 18:38:19 +02:00
|
|
|
{
|
2014-09-18 01:33:24 +02:00
|
|
|
Path: []string{"root"},
|
2016-05-12 02:05:02 +02:00
|
|
|
Outputs: map[string]*terraform.OutputState{},
|
2014-09-17 20:15:07 +02:00
|
|
|
},
|
|
|
|
},
|
2014-07-13 19:25:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
statePath := testStateFile(t, originalState)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &OutputCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2014-07-13 19:25:42 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"bar",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOutput_stateDefault(t *testing.T) {
|
|
|
|
originalState := &terraform.State{
|
2014-09-17 20:15:07 +02:00
|
|
|
Modules: []*terraform.ModuleState{
|
2016-07-13 18:38:19 +02:00
|
|
|
{
|
2014-09-17 20:15:07 +02:00
|
|
|
Path: []string{"root"},
|
2016-05-12 02:05:02 +02:00
|
|
|
Outputs: map[string]*terraform.OutputState{
|
2016-07-13 18:38:19 +02:00
|
|
|
"foo": {
|
2016-05-12 02:05:02 +02:00
|
|
|
Value: "bar",
|
|
|
|
Type: "string",
|
|
|
|
},
|
2014-09-17 20:15:07 +02:00
|
|
|
},
|
|
|
|
},
|
2014-07-13 19:25:42 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the state file in a temporary directory with the
|
|
|
|
// default filename.
|
2018-03-28 19:08:38 +02:00
|
|
|
td := testTempDir(t)
|
2014-07-13 19:25:42 +02:00
|
|
|
statePath := filepath.Join(td, DefaultStateFilename)
|
|
|
|
|
|
|
|
f, err := os.Create(statePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
err = terraform.WriteState(originalState, f)
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change to that directory
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if err := os.Chdir(filepath.Dir(statePath)); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Chdir(cwd)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &OutputCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2014-07-13 19:25:42 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"foo",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(ui.OutputWriter.String())
|
|
|
|
if actual != "bar" {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|