github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/not-internal/command/state_show_test.go (about)

     1  package command
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/muratcelep/terraform/not-internal/addrs"
     8  	"github.com/muratcelep/terraform/not-internal/configs/configschema"
     9  	"github.com/muratcelep/terraform/not-internal/providers"
    10  	"github.com/muratcelep/terraform/not-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  	tmp, cwd := testCwd(t)
   151  	defer testFixCwd(t, tmp, cwd)
   152  
   153  	p := testProvider()
   154  	ui := new(cli.MockUi)
   155  	c := &StateShowCommand{
   156  		Meta: Meta{
   157  			testingOverrides: metaOverridesForProvider(p),
   158  			Ui:               ui,
   159  		},
   160  	}
   161  
   162  	args := []string{
   163  		"test_instance.foo",
   164  	}
   165  	if code := c.Run(args); code != 1 {
   166  		t.Fatalf("bad: %d", code)
   167  	}
   168  	if !strings.Contains(ui.ErrorWriter.String(), "No state file was found!") {
   169  		t.Fatalf("expected a no state file error, got: %s", ui.ErrorWriter.String())
   170  	}
   171  }
   172  
   173  func TestStateShow_emptyState(t *testing.T) {
   174  	state := states.NewState()
   175  	statePath := testStateFile(t, state)
   176  
   177  	p := testProvider()
   178  	ui := new(cli.MockUi)
   179  	c := &StateShowCommand{
   180  		Meta: Meta{
   181  			testingOverrides: metaOverridesForProvider(p),
   182  			Ui:               ui,
   183  		},
   184  	}
   185  
   186  	args := []string{
   187  		"-state", statePath,
   188  		"test_instance.foo",
   189  	}
   190  	if code := c.Run(args); code != 1 {
   191  		t.Fatalf("bad: %d", code)
   192  	}
   193  	if !strings.Contains(ui.ErrorWriter.String(), "No instance found for the given address!") {
   194  		t.Fatalf("expected a no instance found error, got: %s", ui.ErrorWriter.String())
   195  	}
   196  }
   197  
   198  func TestStateShow_configured_provider(t *testing.T) {
   199  	state := states.BuildState(func(s *states.SyncState) {
   200  		s.SetResourceInstanceCurrent(
   201  			addrs.Resource{
   202  				Mode: addrs.ManagedResourceMode,
   203  				Type: "test_instance",
   204  				Name: "foo",
   205  			}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
   206  			&states.ResourceInstanceObjectSrc{
   207  				AttrsJSON: []byte(`{"id":"bar","foo":"value","bar":"value"}`),
   208  				Status:    states.ObjectReady,
   209  			},
   210  			addrs.AbsProviderConfig{
   211  				Provider: addrs.NewDefaultProvider("test-beta"),
   212  				Module:   addrs.RootModule,
   213  			},
   214  		)
   215  	})
   216  	statePath := testStateFile(t, state)
   217  
   218  	p := testProvider()
   219  	p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
   220  		ResourceTypes: map[string]providers.Schema{
   221  			"test_instance": {
   222  				Block: &configschema.Block{
   223  					Attributes: map[string]*configschema.Attribute{
   224  						"id":  {Type: cty.String, Optional: true, Computed: true},
   225  						"foo": {Type: cty.String, Optional: true},
   226  						"bar": {Type: cty.String, Optional: true},
   227  					},
   228  				},
   229  			},
   230  		},
   231  	}
   232  
   233  	ui := new(cli.MockUi)
   234  	c := &StateShowCommand{
   235  		Meta: Meta{
   236  			testingOverrides: &testingOverrides{
   237  				Providers: map[addrs.Provider]providers.Factory{
   238  					addrs.NewDefaultProvider("test-beta"): providers.FactoryFixed(p),
   239  				},
   240  			},
   241  			Ui: ui,
   242  		},
   243  	}
   244  
   245  	args := []string{
   246  		"-state", statePath,
   247  		"test_instance.foo",
   248  	}
   249  	if code := c.Run(args); code != 0 {
   250  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   251  	}
   252  
   253  	// Test that outputs were displayed
   254  	expected := strings.TrimSpace(testStateShowOutput) + "\n"
   255  	actual := ui.OutputWriter.String()
   256  	if actual != expected {
   257  		t.Fatalf("Expected:\n%q\n\nTo equal:\n%q", actual, expected)
   258  	}
   259  }
   260  
   261  const testStateShowOutput = `
   262  # test_instance.foo:
   263  resource "test_instance" "foo" {
   264      bar = "value"
   265      foo = "value"
   266      id  = "bar"
   267  }
   268  `