github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/output_test.go (about)

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