github.com/willrstern/terraform@v0.6.7-0.20151106173844-fa471ddbb53a/command/output_test.go (about)

     1  package command
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/hashicorp/terraform/terraform"
    11  	"github.com/mitchellh/cli"
    12  )
    13  
    14  func TestOutput(t *testing.T) {
    15  	originalState := &terraform.State{
    16  		Modules: []*terraform.ModuleState{
    17  			&terraform.ModuleState{
    18  				Path: []string{"root"},
    19  				Outputs: map[string]string{
    20  					"foo": "bar",
    21  				},
    22  			},
    23  		},
    24  	}
    25  
    26  	statePath := testStateFile(t, originalState)
    27  
    28  	ui := new(cli.MockUi)
    29  	c := &OutputCommand{
    30  		Meta: Meta{
    31  			ContextOpts: testCtxConfig(testProvider()),
    32  			Ui:          ui,
    33  		},
    34  	}
    35  
    36  	args := []string{
    37  		"-state", statePath,
    38  		"foo",
    39  	}
    40  	if code := c.Run(args); code != 0 {
    41  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
    42  	}
    43  
    44  	actual := strings.TrimSpace(ui.OutputWriter.String())
    45  	if actual != "bar" {
    46  		t.Fatalf("bad: %#v", actual)
    47  	}
    48  }
    49  
    50  func TestModuleOutput(t *testing.T) {
    51  	originalState := &terraform.State{
    52  		Modules: []*terraform.ModuleState{
    53  			&terraform.ModuleState{
    54  				Path: []string{"root"},
    55  				Outputs: map[string]string{
    56  					"foo": "bar",
    57  				},
    58  			},
    59  			&terraform.ModuleState{
    60  				Path: []string{"root", "my_module"},
    61  				Outputs: map[string]string{
    62  					"blah": "tastatur",
    63  				},
    64  			},
    65  		},
    66  	}
    67  
    68  	statePath := testStateFile(t, originalState)
    69  
    70  	ui := new(cli.MockUi)
    71  	c := &OutputCommand{
    72  		Meta: Meta{
    73  			ContextOpts: testCtxConfig(testProvider()),
    74  			Ui:          ui,
    75  		},
    76  	}
    77  
    78  	args := []string{
    79  		"-state", statePath,
    80  		"-module", "my_module",
    81  		"blah",
    82  	}
    83  
    84  	if code := c.Run(args); code != 0 {
    85  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
    86  	}
    87  
    88  	actual := strings.TrimSpace(ui.OutputWriter.String())
    89  	if actual != "tastatur" {
    90  		t.Fatalf("bad: %#v", actual)
    91  	}
    92  }
    93  
    94  func TestMissingModuleOutput(t *testing.T) {
    95  	originalState := &terraform.State{
    96  		Modules: []*terraform.ModuleState{
    97  			&terraform.ModuleState{
    98  				Path: []string{"root"},
    99  				Outputs: map[string]string{
   100  					"foo": "bar",
   101  				},
   102  			},
   103  		},
   104  	}
   105  
   106  	statePath := testStateFile(t, originalState)
   107  
   108  	ui := new(cli.MockUi)
   109  	c := &OutputCommand{
   110  		Meta: Meta{
   111  			ContextOpts: testCtxConfig(testProvider()),
   112  			Ui:          ui,
   113  		},
   114  	}
   115  
   116  	args := []string{
   117  		"-state", statePath,
   118  		"-module", "not_existing_module",
   119  		"blah",
   120  	}
   121  
   122  	if code := c.Run(args); code != 1 {
   123  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   124  	}
   125  }
   126  
   127  func TestOutput_badVar(t *testing.T) {
   128  	originalState := &terraform.State{
   129  		Modules: []*terraform.ModuleState{
   130  			&terraform.ModuleState{
   131  				Path: []string{"root"},
   132  				Outputs: map[string]string{
   133  					"foo": "bar",
   134  				},
   135  			},
   136  		},
   137  	}
   138  
   139  	statePath := testStateFile(t, originalState)
   140  
   141  	ui := new(cli.MockUi)
   142  	c := &OutputCommand{
   143  		Meta: Meta{
   144  			ContextOpts: testCtxConfig(testProvider()),
   145  			Ui:          ui,
   146  		},
   147  	}
   148  
   149  	args := []string{
   150  		"-state", statePath,
   151  		"bar",
   152  	}
   153  	if code := c.Run(args); code != 1 {
   154  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   155  	}
   156  }
   157  
   158  func TestOutput_blank(t *testing.T) {
   159  	originalState := &terraform.State{
   160  		Modules: []*terraform.ModuleState{
   161  			&terraform.ModuleState{
   162  				Path: []string{"root"},
   163  				Outputs: map[string]string{
   164  					"foo":  "bar",
   165  					"name": "john-doe",
   166  				},
   167  			},
   168  		},
   169  	}
   170  
   171  	statePath := testStateFile(t, originalState)
   172  
   173  	ui := new(cli.MockUi)
   174  	c := &OutputCommand{
   175  		Meta: Meta{
   176  			ContextOpts: testCtxConfig(testProvider()),
   177  			Ui:          ui,
   178  		},
   179  	}
   180  
   181  	args := []string{
   182  		"-state", statePath,
   183  		"",
   184  	}
   185  
   186  	if code := c.Run(args); code != 0 {
   187  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   188  	}
   189  
   190  	expectedOutput := "foo = bar\nname = john-doe\n"
   191  	output := ui.OutputWriter.String()
   192  	if output != expectedOutput {
   193  		t.Fatalf("Expected output: %#v\ngiven: %#v", expectedOutput, output)
   194  	}
   195  }
   196  
   197  func TestOutput_manyArgs(t *testing.T) {
   198  	ui := new(cli.MockUi)
   199  	c := &OutputCommand{
   200  		Meta: Meta{
   201  			ContextOpts: testCtxConfig(testProvider()),
   202  			Ui:          ui,
   203  		},
   204  	}
   205  
   206  	args := []string{
   207  		"bad",
   208  		"bad",
   209  	}
   210  	if code := c.Run(args); code != 1 {
   211  		t.Fatalf("bad: \n%s", ui.OutputWriter.String())
   212  	}
   213  }
   214  
   215  func TestOutput_noArgs(t *testing.T) {
   216  	ui := new(cli.MockUi)
   217  	c := &OutputCommand{
   218  		Meta: Meta{
   219  			ContextOpts: testCtxConfig(testProvider()),
   220  			Ui:          ui,
   221  		},
   222  	}
   223  
   224  	args := []string{}
   225  	if code := c.Run(args); code != 1 {
   226  		t.Fatalf("bad: \n%s", ui.OutputWriter.String())
   227  	}
   228  }
   229  
   230  func TestOutput_noState(t *testing.T) {
   231  	originalState := &terraform.State{}
   232  	statePath := testStateFile(t, originalState)
   233  
   234  	ui := new(cli.MockUi)
   235  	c := &OutputCommand{
   236  		Meta: Meta{
   237  			ContextOpts: testCtxConfig(testProvider()),
   238  			Ui:          ui,
   239  		},
   240  	}
   241  
   242  	args := []string{
   243  		"-state", statePath,
   244  		"foo",
   245  	}
   246  	if code := c.Run(args); code != 1 {
   247  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   248  	}
   249  }
   250  
   251  func TestOutput_noVars(t *testing.T) {
   252  	originalState := &terraform.State{
   253  		Modules: []*terraform.ModuleState{
   254  			&terraform.ModuleState{
   255  				Path:    []string{"root"},
   256  				Outputs: map[string]string{},
   257  			},
   258  		},
   259  	}
   260  
   261  	statePath := testStateFile(t, originalState)
   262  
   263  	ui := new(cli.MockUi)
   264  	c := &OutputCommand{
   265  		Meta: Meta{
   266  			ContextOpts: testCtxConfig(testProvider()),
   267  			Ui:          ui,
   268  		},
   269  	}
   270  
   271  	args := []string{
   272  		"-state", statePath,
   273  		"bar",
   274  	}
   275  	if code := c.Run(args); code != 1 {
   276  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   277  	}
   278  }
   279  
   280  func TestOutput_stateDefault(t *testing.T) {
   281  	originalState := &terraform.State{
   282  		Modules: []*terraform.ModuleState{
   283  			&terraform.ModuleState{
   284  				Path: []string{"root"},
   285  				Outputs: map[string]string{
   286  					"foo": "bar",
   287  				},
   288  			},
   289  		},
   290  	}
   291  
   292  	// Write the state file in a temporary directory with the
   293  	// default filename.
   294  	td, err := ioutil.TempDir("", "tf")
   295  	if err != nil {
   296  		t.Fatalf("err: %s", err)
   297  	}
   298  	statePath := filepath.Join(td, DefaultStateFilename)
   299  
   300  	f, err := os.Create(statePath)
   301  	if err != nil {
   302  		t.Fatalf("err: %s", err)
   303  	}
   304  	err = terraform.WriteState(originalState, f)
   305  	f.Close()
   306  	if err != nil {
   307  		t.Fatalf("err: %s", err)
   308  	}
   309  
   310  	// Change to that directory
   311  	cwd, err := os.Getwd()
   312  	if err != nil {
   313  		t.Fatalf("err: %s", err)
   314  	}
   315  	if err := os.Chdir(filepath.Dir(statePath)); err != nil {
   316  		t.Fatalf("err: %s", err)
   317  	}
   318  	defer os.Chdir(cwd)
   319  
   320  	ui := new(cli.MockUi)
   321  	c := &OutputCommand{
   322  		Meta: Meta{
   323  			ContextOpts: testCtxConfig(testProvider()),
   324  			Ui:          ui,
   325  		},
   326  	}
   327  
   328  	args := []string{
   329  		"foo",
   330  	}
   331  	if code := c.Run(args); code != 0 {
   332  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   333  	}
   334  
   335  	actual := strings.TrimSpace(ui.OutputWriter.String())
   336  	if actual != "bar" {
   337  		t.Fatalf("bad: %#v", actual)
   338  	}
   339  }