github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/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  			{
    18  				Path: []string{"root"},
    19  				Outputs: map[string]*terraform.OutputState{
    20  					"foo": {
    21  						Value: "bar",
    22  						Type:  "string",
    23  					},
    24  				},
    25  			},
    26  		},
    27  	}
    28  
    29  	statePath := testStateFile(t, originalState)
    30  
    31  	ui := new(cli.MockUi)
    32  	c := &OutputCommand{
    33  		Meta: Meta{
    34  			testingOverrides: metaOverridesForProvider(testProvider()),
    35  			Ui:               ui,
    36  		},
    37  	}
    38  
    39  	args := []string{
    40  		"-state", statePath,
    41  		"foo",
    42  	}
    43  	if code := c.Run(args); code != 0 {
    44  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
    45  	}
    46  
    47  	actual := strings.TrimSpace(ui.OutputWriter.String())
    48  	if actual != "bar" {
    49  		t.Fatalf("bad: %#v", actual)
    50  	}
    51  }
    52  
    53  func TestModuleOutput(t *testing.T) {
    54  	originalState := &terraform.State{
    55  		Modules: []*terraform.ModuleState{
    56  			{
    57  				Path: []string{"root"},
    58  				Outputs: map[string]*terraform.OutputState{
    59  					"foo": {
    60  						Value: "bar",
    61  						Type:  "string",
    62  					},
    63  				},
    64  			},
    65  			{
    66  				Path: []string{"root", "my_module"},
    67  				Outputs: map[string]*terraform.OutputState{
    68  					"blah": {
    69  						Value: "tastatur",
    70  						Type:  "string",
    71  					},
    72  				},
    73  			},
    74  		},
    75  	}
    76  
    77  	statePath := testStateFile(t, originalState)
    78  
    79  	ui := new(cli.MockUi)
    80  	c := &OutputCommand{
    81  		Meta: Meta{
    82  			testingOverrides: metaOverridesForProvider(testProvider()),
    83  			Ui:               ui,
    84  		},
    85  	}
    86  
    87  	args := []string{
    88  		"-state", statePath,
    89  		"-module", "my_module",
    90  		"blah",
    91  	}
    92  
    93  	if code := c.Run(args); code != 0 {
    94  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
    95  	}
    96  
    97  	actual := strings.TrimSpace(ui.OutputWriter.String())
    98  	if actual != "tastatur" {
    99  		t.Fatalf("bad: %#v", actual)
   100  	}
   101  }
   102  
   103  func TestModuleOutputs(t *testing.T) {
   104  	originalState := &terraform.State{
   105  		Modules: []*terraform.ModuleState{
   106  			{
   107  				Path: []string{"root"},
   108  				Outputs: map[string]*terraform.OutputState{
   109  					"foo": {
   110  						Value: "bar",
   111  						Type:  "string",
   112  					},
   113  				},
   114  			},
   115  			{
   116  				Path: []string{"root", "my_module"},
   117  				Outputs: map[string]*terraform.OutputState{
   118  					"blah": {
   119  						Value: "tastatur",
   120  						Type:  "string",
   121  					},
   122  				},
   123  			},
   124  		},
   125  	}
   126  
   127  	statePath := testStateFile(t, originalState)
   128  
   129  	ui := new(cli.MockUi)
   130  	c := &OutputCommand{
   131  		Meta: Meta{
   132  			testingOverrides: metaOverridesForProvider(testProvider()),
   133  			Ui:               ui,
   134  		},
   135  	}
   136  
   137  	args := []string{
   138  		"-state", statePath,
   139  		"-module", "my_module",
   140  	}
   141  
   142  	if code := c.Run(args); code != 0 {
   143  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   144  	}
   145  
   146  	actual := strings.TrimSpace(ui.OutputWriter.String())
   147  	if actual != "blah = tastatur" {
   148  		t.Fatalf("bad: %#v", actual)
   149  	}
   150  }
   151  
   152  func TestOutput_nestedListAndMap(t *testing.T) {
   153  	originalState := &terraform.State{
   154  		Modules: []*terraform.ModuleState{
   155  			{
   156  				Path: []string{"root"},
   157  				Outputs: map[string]*terraform.OutputState{
   158  					"foo": {
   159  						Value: []interface{}{
   160  							map[string]interface{}{
   161  								"key":  "value",
   162  								"key2": "value2",
   163  							},
   164  							map[string]interface{}{
   165  								"key": "value",
   166  							},
   167  						},
   168  						Type: "list",
   169  					},
   170  				},
   171  			},
   172  		},
   173  	}
   174  
   175  	statePath := testStateFile(t, originalState)
   176  
   177  	ui := new(cli.MockUi)
   178  	c := &OutputCommand{
   179  		Meta: Meta{
   180  			testingOverrides: metaOverridesForProvider(testProvider()),
   181  			Ui:               ui,
   182  		},
   183  	}
   184  
   185  	args := []string{
   186  		"-state", statePath,
   187  	}
   188  	if code := c.Run(args); code != 0 {
   189  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   190  	}
   191  
   192  	actual := strings.TrimSpace(ui.OutputWriter.String())
   193  	expected := "foo = [\n    {\n        key = value,\n        key2 = value2\n    },\n    {\n        key = value\n    }\n]"
   194  	if actual != expected {
   195  		t.Fatalf("bad:\n%#v\n%#v", expected, actual)
   196  	}
   197  }
   198  
   199  func TestOutput_json(t *testing.T) {
   200  	originalState := &terraform.State{
   201  		Modules: []*terraform.ModuleState{
   202  			{
   203  				Path: []string{"root"},
   204  				Outputs: map[string]*terraform.OutputState{
   205  					"foo": {
   206  						Value: "bar",
   207  						Type:  "string",
   208  					},
   209  				},
   210  			},
   211  		},
   212  	}
   213  
   214  	statePath := testStateFile(t, originalState)
   215  
   216  	ui := new(cli.MockUi)
   217  	c := &OutputCommand{
   218  		Meta: Meta{
   219  			testingOverrides: metaOverridesForProvider(testProvider()),
   220  			Ui:               ui,
   221  		},
   222  	}
   223  
   224  	args := []string{
   225  		"-state", statePath,
   226  		"-json",
   227  	}
   228  	if code := c.Run(args); code != 0 {
   229  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   230  	}
   231  
   232  	actual := strings.TrimSpace(ui.OutputWriter.String())
   233  	expected := "{\n    \"foo\": {\n        \"sensitive\": false,\n        \"type\": \"string\",\n        \"value\": \"bar\"\n    }\n}"
   234  	if actual != expected {
   235  		t.Fatalf("bad:\n%#v\n%#v", expected, actual)
   236  	}
   237  }
   238  
   239  func TestMissingModuleOutput(t *testing.T) {
   240  	originalState := &terraform.State{
   241  		Modules: []*terraform.ModuleState{
   242  			{
   243  				Path: []string{"root"},
   244  				Outputs: map[string]*terraform.OutputState{
   245  					"foo": {
   246  						Value: "bar",
   247  						Type:  "string",
   248  					},
   249  				},
   250  			},
   251  		},
   252  	}
   253  
   254  	statePath := testStateFile(t, originalState)
   255  
   256  	ui := new(cli.MockUi)
   257  	c := &OutputCommand{
   258  		Meta: Meta{
   259  			testingOverrides: metaOverridesForProvider(testProvider()),
   260  			Ui:               ui,
   261  		},
   262  	}
   263  
   264  	args := []string{
   265  		"-state", statePath,
   266  		"-module", "not_existing_module",
   267  		"blah",
   268  	}
   269  
   270  	if code := c.Run(args); code != 1 {
   271  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   272  	}
   273  }
   274  
   275  func TestOutput_badVar(t *testing.T) {
   276  	originalState := &terraform.State{
   277  		Modules: []*terraform.ModuleState{
   278  			{
   279  				Path: []string{"root"},
   280  				Outputs: map[string]*terraform.OutputState{
   281  					"foo": {
   282  						Value: "bar",
   283  						Type:  "string",
   284  					},
   285  				},
   286  			},
   287  		},
   288  	}
   289  
   290  	statePath := testStateFile(t, originalState)
   291  
   292  	ui := new(cli.MockUi)
   293  	c := &OutputCommand{
   294  		Meta: Meta{
   295  			testingOverrides: metaOverridesForProvider(testProvider()),
   296  			Ui:               ui,
   297  		},
   298  	}
   299  
   300  	args := []string{
   301  		"-state", statePath,
   302  		"bar",
   303  	}
   304  	if code := c.Run(args); code != 1 {
   305  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   306  	}
   307  }
   308  
   309  func TestOutput_blank(t *testing.T) {
   310  	originalState := &terraform.State{
   311  		Modules: []*terraform.ModuleState{
   312  			{
   313  				Path: []string{"root"},
   314  				Outputs: map[string]*terraform.OutputState{
   315  					"foo": {
   316  						Value: "bar",
   317  						Type:  "string",
   318  					},
   319  					"name": {
   320  						Value: "john-doe",
   321  						Type:  "string",
   322  					},
   323  				},
   324  			},
   325  		},
   326  	}
   327  
   328  	statePath := testStateFile(t, originalState)
   329  
   330  	ui := new(cli.MockUi)
   331  	c := &OutputCommand{
   332  		Meta: Meta{
   333  			testingOverrides: metaOverridesForProvider(testProvider()),
   334  			Ui:               ui,
   335  		},
   336  	}
   337  
   338  	args := []string{
   339  		"-state", statePath,
   340  		"",
   341  	}
   342  
   343  	if code := c.Run(args); code != 0 {
   344  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   345  	}
   346  
   347  	expectedOutput := "foo = bar\nname = john-doe\n"
   348  	output := ui.OutputWriter.String()
   349  	if output != expectedOutput {
   350  		t.Fatalf("Expected output: %#v\ngiven: %#v", expectedOutput, output)
   351  	}
   352  }
   353  
   354  func TestOutput_manyArgs(t *testing.T) {
   355  	ui := new(cli.MockUi)
   356  	c := &OutputCommand{
   357  		Meta: Meta{
   358  			testingOverrides: metaOverridesForProvider(testProvider()),
   359  			Ui:               ui,
   360  		},
   361  	}
   362  
   363  	args := []string{
   364  		"bad",
   365  		"bad",
   366  	}
   367  	if code := c.Run(args); code != 1 {
   368  		t.Fatalf("bad: \n%s", ui.OutputWriter.String())
   369  	}
   370  }
   371  
   372  func TestOutput_noArgs(t *testing.T) {
   373  	ui := new(cli.MockUi)
   374  	c := &OutputCommand{
   375  		Meta: Meta{
   376  			testingOverrides: metaOverridesForProvider(testProvider()),
   377  			Ui:               ui,
   378  		},
   379  	}
   380  
   381  	args := []string{}
   382  	if code := c.Run(args); code != 1 {
   383  		t.Fatalf("bad: \n%s", ui.OutputWriter.String())
   384  	}
   385  }
   386  
   387  func TestOutput_noState(t *testing.T) {
   388  	originalState := &terraform.State{}
   389  	statePath := testStateFile(t, originalState)
   390  
   391  	ui := new(cli.MockUi)
   392  	c := &OutputCommand{
   393  		Meta: Meta{
   394  			testingOverrides: metaOverridesForProvider(testProvider()),
   395  			Ui:               ui,
   396  		},
   397  	}
   398  
   399  	args := []string{
   400  		"-state", statePath,
   401  		"foo",
   402  	}
   403  	if code := c.Run(args); code != 1 {
   404  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   405  	}
   406  }
   407  
   408  func TestOutput_noVars(t *testing.T) {
   409  	originalState := &terraform.State{
   410  		Modules: []*terraform.ModuleState{
   411  			{
   412  				Path:    []string{"root"},
   413  				Outputs: map[string]*terraform.OutputState{},
   414  			},
   415  		},
   416  	}
   417  
   418  	statePath := testStateFile(t, originalState)
   419  
   420  	ui := new(cli.MockUi)
   421  	c := &OutputCommand{
   422  		Meta: Meta{
   423  			testingOverrides: metaOverridesForProvider(testProvider()),
   424  			Ui:               ui,
   425  		},
   426  	}
   427  
   428  	args := []string{
   429  		"-state", statePath,
   430  		"bar",
   431  	}
   432  	if code := c.Run(args); code != 1 {
   433  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   434  	}
   435  }
   436  
   437  func TestOutput_stateDefault(t *testing.T) {
   438  	originalState := &terraform.State{
   439  		Modules: []*terraform.ModuleState{
   440  			{
   441  				Path: []string{"root"},
   442  				Outputs: map[string]*terraform.OutputState{
   443  					"foo": {
   444  						Value: "bar",
   445  						Type:  "string",
   446  					},
   447  				},
   448  			},
   449  		},
   450  	}
   451  
   452  	// Write the state file in a temporary directory with the
   453  	// default filename.
   454  	td, err := ioutil.TempDir("", "tf")
   455  	if err != nil {
   456  		t.Fatalf("err: %s", err)
   457  	}
   458  	statePath := filepath.Join(td, DefaultStateFilename)
   459  
   460  	f, err := os.Create(statePath)
   461  	if err != nil {
   462  		t.Fatalf("err: %s", err)
   463  	}
   464  	err = terraform.WriteState(originalState, f)
   465  	f.Close()
   466  	if err != nil {
   467  		t.Fatalf("err: %s", err)
   468  	}
   469  
   470  	// Change to that directory
   471  	cwd, err := os.Getwd()
   472  	if err != nil {
   473  		t.Fatalf("err: %s", err)
   474  	}
   475  	if err := os.Chdir(filepath.Dir(statePath)); err != nil {
   476  		t.Fatalf("err: %s", err)
   477  	}
   478  	defer os.Chdir(cwd)
   479  
   480  	ui := new(cli.MockUi)
   481  	c := &OutputCommand{
   482  		Meta: Meta{
   483  			testingOverrides: metaOverridesForProvider(testProvider()),
   484  			Ui:               ui,
   485  		},
   486  	}
   487  
   488  	args := []string{
   489  		"foo",
   490  	}
   491  	if code := c.Run(args); code != 0 {
   492  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   493  	}
   494  
   495  	actual := strings.TrimSpace(ui.OutputWriter.String())
   496  	if actual != "bar" {
   497  		t.Fatalf("bad: %#v", actual)
   498  	}
   499  }