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

     1  package command
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/internal/addrs"
     8  	"github.com/hashicorp/terraform/internal/configs/configschema"
     9  	"github.com/hashicorp/terraform/internal/providers"
    10  	"github.com/hashicorp/terraform/internal/states"
    11  	"github.com/mitchellh/cli"
    12  	"github.com/zclconf/go-cty/cty"
    13  )
    14  
    15  func TestStateShow(t *testing.T) {
    16  	state := states.BuildState(func(s *states.SyncState) {
    17  		s.SetResourceInstanceCurrent(
    18  			addrs.Resource{
    19  				Mode: addrs.ManagedResourceMode,
    20  				Type: "test_instance",
    21  				Name: "foo",
    22  			}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
    23  			&states.ResourceInstanceObjectSrc{
    24  				AttrsJSON: []byte(`{"id":"bar","foo":"value","bar":"value"}`),
    25  				Status:    states.ObjectReady,
    26  			},
    27  			addrs.AbsProviderConfig{
    28  				Provider: addrs.NewDefaultProvider("test"),
    29  				Module:   addrs.RootModule,
    30  			},
    31  		)
    32  	})
    33  	statePath := testStateFile(t, state)
    34  
    35  	p := testProvider()
    36  	p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
    37  		ResourceTypes: map[string]providers.Schema{
    38  			"test_instance": {
    39  				Block: &configschema.Block{
    40  					Attributes: map[string]*configschema.Attribute{
    41  						"id":  {Type: cty.String, Optional: true, Computed: true},
    42  						"foo": {Type: cty.String, Optional: true},
    43  						"bar": {Type: cty.String, Optional: true},
    44  					},
    45  				},
    46  			},
    47  		},
    48  	}
    49  
    50  	ui := new(cli.MockUi)
    51  	c := &StateShowCommand{
    52  		Meta: Meta{
    53  			testingOverrides: metaOverridesForProvider(p),
    54  			Ui:               ui,
    55  		},
    56  	}
    57  
    58  	args := []string{
    59  		"-state", statePath,
    60  		"test_instance.foo",
    61  	}
    62  	if code := c.Run(args); code != 0 {
    63  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    64  	}
    65  
    66  	// Test that outputs were displayed
    67  	expected := strings.TrimSpace(testStateShowOutput) + "\n"
    68  	actual := ui.OutputWriter.String()
    69  	if actual != expected {
    70  		t.Fatalf("Expected:\n%q\n\nTo equal:\n%q", actual, expected)
    71  	}
    72  }
    73  
    74  func TestStateShow_multi(t *testing.T) {
    75  	submod, _ := addrs.ParseModuleInstanceStr("module.sub")
    76  	state := states.BuildState(func(s *states.SyncState) {
    77  		s.SetResourceInstanceCurrent(
    78  			addrs.Resource{
    79  				Mode: addrs.ManagedResourceMode,
    80  				Type: "test_instance",
    81  				Name: "foo",
    82  			}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
    83  			&states.ResourceInstanceObjectSrc{
    84  				AttrsJSON: []byte(`{"id":"bar","foo":"value","bar":"value"}`),
    85  				Status:    states.ObjectReady,
    86  			},
    87  			addrs.AbsProviderConfig{
    88  				Provider: addrs.NewDefaultProvider("test"),
    89  				Module:   addrs.RootModule,
    90  			},
    91  		)
    92  		s.SetResourceInstanceCurrent(
    93  			addrs.Resource{
    94  				Mode: addrs.ManagedResourceMode,
    95  				Type: "test_instance",
    96  				Name: "foo",
    97  			}.Instance(addrs.NoKey).Absolute(submod),
    98  			&states.ResourceInstanceObjectSrc{
    99  				AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
   100  				Status:    states.ObjectReady,
   101  			},
   102  			addrs.AbsProviderConfig{
   103  				Provider: addrs.NewDefaultProvider("test"),
   104  				Module:   submod.Module(),
   105  			},
   106  		)
   107  	})
   108  	statePath := testStateFile(t, state)
   109  
   110  	p := testProvider()
   111  	p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
   112  		ResourceTypes: map[string]providers.Schema{
   113  			"test_instance": {
   114  				Block: &configschema.Block{
   115  					Attributes: map[string]*configschema.Attribute{
   116  						"id":  {Type: cty.String, Optional: true, Computed: true},
   117  						"foo": {Type: cty.String, Optional: true},
   118  						"bar": {Type: cty.String, Optional: true},
   119  					},
   120  				},
   121  			},
   122  		},
   123  	}
   124  
   125  	ui := new(cli.MockUi)
   126  	c := &StateShowCommand{
   127  		Meta: Meta{
   128  			testingOverrides: metaOverridesForProvider(p),
   129  			Ui:               ui,
   130  		},
   131  	}
   132  
   133  	args := []string{
   134  		"-state", statePath,
   135  		"test_instance.foo",
   136  	}
   137  	if code := c.Run(args); code != 0 {
   138  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   139  	}
   140  
   141  	// Test that outputs were displayed
   142  	expected := strings.TrimSpace(testStateShowOutput) + "\n"
   143  	actual := ui.OutputWriter.String()
   144  	if actual != expected {
   145  		t.Fatalf("Expected:\n%q\n\nTo equal:\n%q", actual, expected)
   146  	}
   147  }
   148  
   149  func TestStateShow_noState(t *testing.T) {
   150  	testCwd(t)
   151  
   152  	p := testProvider()
   153  	ui := new(cli.MockUi)
   154  	c := &StateShowCommand{
   155  		Meta: Meta{
   156  			testingOverrides: metaOverridesForProvider(p),
   157  			Ui:               ui,
   158  		},
   159  	}
   160  
   161  	args := []string{
   162  		"test_instance.foo",
   163  	}
   164  	if code := c.Run(args); code != 1 {
   165  		t.Fatalf("bad: %d", code)
   166  	}
   167  	if !strings.Contains(ui.ErrorWriter.String(), "No state file was found!") {
   168  		t.Fatalf("expected a no state file error, got: %s", ui.ErrorWriter.String())
   169  	}
   170  }
   171  
   172  func TestStateShow_emptyState(t *testing.T) {
   173  	state := states.NewState()
   174  	statePath := testStateFile(t, state)
   175  
   176  	p := testProvider()
   177  	ui := new(cli.MockUi)
   178  	c := &StateShowCommand{
   179  		Meta: Meta{
   180  			testingOverrides: metaOverridesForProvider(p),
   181  			Ui:               ui,
   182  		},
   183  	}
   184  
   185  	args := []string{
   186  		"-state", statePath,
   187  		"test_instance.foo",
   188  	}
   189  	if code := c.Run(args); code != 1 {
   190  		t.Fatalf("bad: %d", code)
   191  	}
   192  	if !strings.Contains(ui.ErrorWriter.String(), "No instance found for the given address!") {
   193  		t.Fatalf("expected a no instance found error, got: %s", ui.ErrorWriter.String())
   194  	}
   195  }
   196  
   197  func TestStateShow_configured_provider(t *testing.T) {
   198  	state := states.BuildState(func(s *states.SyncState) {
   199  		s.SetResourceInstanceCurrent(
   200  			addrs.Resource{
   201  				Mode: addrs.ManagedResourceMode,
   202  				Type: "test_instance",
   203  				Name: "foo",
   204  			}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
   205  			&states.ResourceInstanceObjectSrc{
   206  				AttrsJSON: []byte(`{"id":"bar","foo":"value","bar":"value"}`),
   207  				Status:    states.ObjectReady,
   208  			},
   209  			addrs.AbsProviderConfig{
   210  				Provider: addrs.NewDefaultProvider("test-beta"),
   211  				Module:   addrs.RootModule,
   212  			},
   213  		)
   214  	})
   215  	statePath := testStateFile(t, state)
   216  
   217  	p := testProvider()
   218  	p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
   219  		ResourceTypes: map[string]providers.Schema{
   220  			"test_instance": {
   221  				Block: &configschema.Block{
   222  					Attributes: map[string]*configschema.Attribute{
   223  						"id":  {Type: cty.String, Optional: true, Computed: true},
   224  						"foo": {Type: cty.String, Optional: true},
   225  						"bar": {Type: cty.String, Optional: true},
   226  					},
   227  				},
   228  			},
   229  		},
   230  	}
   231  
   232  	ui := new(cli.MockUi)
   233  	c := &StateShowCommand{
   234  		Meta: Meta{
   235  			testingOverrides: &testingOverrides{
   236  				Providers: map[addrs.Provider]providers.Factory{
   237  					addrs.NewDefaultProvider("test-beta"): providers.FactoryFixed(p),
   238  				},
   239  			},
   240  			Ui: ui,
   241  		},
   242  	}
   243  
   244  	args := []string{
   245  		"-state", statePath,
   246  		"test_instance.foo",
   247  	}
   248  	if code := c.Run(args); code != 0 {
   249  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   250  	}
   251  
   252  	// Test that outputs were displayed
   253  	expected := strings.TrimSpace(testStateShowOutput) + "\n"
   254  	actual := ui.OutputWriter.String()
   255  	if actual != expected {
   256  		t.Fatalf("Expected:\n%q\n\nTo equal:\n%q", actual, expected)
   257  	}
   258  }
   259  
   260  const testStateShowOutput = `
   261  # test_instance.foo:
   262  resource "test_instance" "foo" {
   263      bar = "value"
   264      foo = "value"
   265      id  = "bar"
   266  }
   267  `