github.com/miquella/terraform@v0.6.17-0.20160517195040-40db82f25ec0/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  			ContextOpts: testCtxConfig(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  			ContextOpts: testCtxConfig(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  			ContextOpts: testCtxConfig(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  const testStateShowOutput = `
   130  id  = bar
   131  bar = value
   132  foo = value
   133  `