github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/command/state_show_test.go (about)

     1  package command
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/terraform"
     8  	"github.com/mitchellh/cli"
     9  )
    10  
    11  func TestStateShow(t *testing.T) {
    12  	state := &terraform.State{
    13  		Modules: []*terraform.ModuleState{
    14  			&terraform.ModuleState{
    15  				Path: []string{"root"},
    16  				Resources: map[string]*terraform.ResourceState{
    17  					"test_instance.foo": &terraform.ResourceState{
    18  						Type: "test_instance",
    19  						Primary: &terraform.InstanceState{
    20  							ID: "bar",
    21  							Attributes: map[string]string{
    22  								"foo": "value",
    23  								"bar": "value",
    24  							},
    25  						},
    26  					},
    27  				},
    28  			},
    29  		},
    30  	}
    31  
    32  	statePath := testStateFile(t, state)
    33  
    34  	p := testProvider()
    35  	ui := new(cli.MockUi)
    36  	c := &StateShowCommand{
    37  		Meta: Meta{
    38  			testingOverrides: metaOverridesForProvider(p),
    39  			Ui:               ui,
    40  		},
    41  	}
    42  
    43  	args := []string{
    44  		"-state", statePath,
    45  		"test_instance.foo",
    46  	}
    47  	if code := c.Run(args); code != 0 {
    48  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    49  	}
    50  
    51  	// Test that outputs were displayed
    52  	expected := strings.TrimSpace(testStateShowOutput) + "\n"
    53  	actual := ui.OutputWriter.String()
    54  	if actual != expected {
    55  		t.Fatalf("Expected:\n%q\n\nTo equal: %q", actual, expected)
    56  	}
    57  }
    58  
    59  func TestStateShow_multi(t *testing.T) {
    60  	state := &terraform.State{
    61  		Modules: []*terraform.ModuleState{
    62  			&terraform.ModuleState{
    63  				Path: []string{"root"},
    64  				Resources: map[string]*terraform.ResourceState{
    65  					"test_instance.foo.0": &terraform.ResourceState{
    66  						Type: "test_instance",
    67  						Primary: &terraform.InstanceState{
    68  							ID: "bar",
    69  							Attributes: map[string]string{
    70  								"foo": "value",
    71  								"bar": "value",
    72  							},
    73  						},
    74  					},
    75  					"test_instance.foo.1": &terraform.ResourceState{
    76  						Type: "test_instance",
    77  						Primary: &terraform.InstanceState{
    78  							ID: "bar",
    79  							Attributes: map[string]string{
    80  								"foo": "value",
    81  								"bar": "value",
    82  							},
    83  						},
    84  					},
    85  				},
    86  			},
    87  		},
    88  	}
    89  
    90  	statePath := testStateFile(t, state)
    91  
    92  	p := testProvider()
    93  	ui := new(cli.MockUi)
    94  	c := &StateShowCommand{
    95  		Meta: Meta{
    96  			testingOverrides: metaOverridesForProvider(p),
    97  			Ui:               ui,
    98  		},
    99  	}
   100  
   101  	args := []string{
   102  		"-state", statePath,
   103  		"test_instance.foo",
   104  	}
   105  	if code := c.Run(args); code != 1 {
   106  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   107  	}
   108  }
   109  
   110  func TestStateShow_noState(t *testing.T) {
   111  	tmp, cwd := testCwd(t)
   112  	defer testFixCwd(t, tmp, cwd)
   113  
   114  	p := testProvider()
   115  	ui := new(cli.MockUi)
   116  	c := &StateShowCommand{
   117  		Meta: Meta{
   118  			testingOverrides: metaOverridesForProvider(p),
   119  			Ui:               ui,
   120  		},
   121  	}
   122  
   123  	args := []string{}
   124  	if code := c.Run(args); code != 1 {
   125  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   126  	}
   127  }
   128  
   129  func TestStateShow_emptyState(t *testing.T) {
   130  	state := terraform.NewState()
   131  
   132  	statePath := testStateFile(t, state)
   133  
   134  	p := testProvider()
   135  	ui := new(cli.MockUi)
   136  	c := &StateShowCommand{
   137  		Meta: Meta{
   138  			testingOverrides: metaOverridesForProvider(p),
   139  			Ui:               ui,
   140  		},
   141  	}
   142  
   143  	args := []string{
   144  		"-state", statePath,
   145  		"test_instance.foo",
   146  	}
   147  	if code := c.Run(args); code != 0 {
   148  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   149  	}
   150  }
   151  
   152  func TestStateShow_emptyStateWithModule(t *testing.T) {
   153  	// empty state with empty module
   154  	state := terraform.NewState()
   155  
   156  	mod := &terraform.ModuleState{
   157  		Path: []string{"root", "mod"},
   158  	}
   159  	state.Modules = append(state.Modules, mod)
   160  
   161  	statePath := testStateFile(t, state)
   162  
   163  	p := testProvider()
   164  	ui := new(cli.MockUi)
   165  	c := &StateShowCommand{
   166  		Meta: Meta{
   167  			testingOverrides: metaOverridesForProvider(p),
   168  			Ui:               ui,
   169  		},
   170  	}
   171  
   172  	args := []string{
   173  		"-state", statePath,
   174  	}
   175  	if code := c.Run(args); code != 0 {
   176  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   177  	}
   178  }
   179  
   180  const testStateShowOutput = `
   181  id  = bar
   182  bar = value
   183  foo = value
   184  `