github.com/tarrant/terraform@v0.3.8-0.20150402012457-f68c9eee638e/command/show_test.go (about)

     1  package command
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/config/module"
    10  	"github.com/hashicorp/terraform/terraform"
    11  	"github.com/mitchellh/cli"
    12  )
    13  
    14  func TestShow(t *testing.T) {
    15  	ui := new(cli.MockUi)
    16  	c := &ShowCommand{
    17  		Meta: Meta{
    18  			ContextOpts: testCtxConfig(testProvider()),
    19  			Ui:          ui,
    20  		},
    21  	}
    22  
    23  	args := []string{
    24  		"bad",
    25  		"bad",
    26  	}
    27  	if code := c.Run(args); code != 1 {
    28  		t.Fatalf("bad: \n%s", ui.OutputWriter.String())
    29  	}
    30  }
    31  
    32  func TestShow_noArgs(t *testing.T) {
    33  	// Create the default state
    34  	td, err := ioutil.TempDir("", "tf")
    35  	if err != nil {
    36  		t.Fatalf("err: %s", err)
    37  	}
    38  	statePath := filepath.Join(td, DefaultStateFilename)
    39  
    40  	f, err := os.Create(statePath)
    41  	if err != nil {
    42  		t.Fatalf("err: %s", err)
    43  	}
    44  	err = terraform.WriteState(testState(), f)
    45  	f.Close()
    46  	if err != nil {
    47  		t.Fatalf("err: %s", err)
    48  	}
    49  
    50  	// Change to the temporary directory
    51  	cwd, err := os.Getwd()
    52  	if err != nil {
    53  		t.Fatalf("err: %s", err)
    54  	}
    55  	if err := os.Chdir(filepath.Dir(statePath)); err != nil {
    56  		t.Fatalf("err: %s", err)
    57  	}
    58  	defer os.Chdir(cwd)
    59  
    60  	ui := new(cli.MockUi)
    61  	c := &ShowCommand{
    62  		Meta: Meta{
    63  			ContextOpts: testCtxConfig(testProvider()),
    64  			Ui:          ui,
    65  		},
    66  	}
    67  
    68  	args := []string{}
    69  	if code := c.Run(args); code != 0 {
    70  		t.Fatalf("bad: \n%s", ui.OutputWriter.String())
    71  	}
    72  }
    73  
    74  func TestShow_noArgsNoState(t *testing.T) {
    75  	// Create the default state
    76  	td, err := ioutil.TempDir("", "tf")
    77  	if err != nil {
    78  		t.Fatalf("err: %s", err)
    79  	}
    80  	statePath := filepath.Join(td, DefaultStateFilename)
    81  
    82  	// Change to the temporary directory
    83  	cwd, err := os.Getwd()
    84  	if err != nil {
    85  		t.Fatalf("err: %s", err)
    86  	}
    87  	if err := os.Chdir(filepath.Dir(statePath)); err != nil {
    88  		t.Fatalf("err: %s", err)
    89  	}
    90  	defer os.Chdir(cwd)
    91  
    92  	ui := new(cli.MockUi)
    93  	c := &ShowCommand{
    94  		Meta: Meta{
    95  			ContextOpts: testCtxConfig(testProvider()),
    96  			Ui:          ui,
    97  		},
    98  	}
    99  
   100  	args := []string{}
   101  	if code := c.Run(args); code != 0 {
   102  		t.Fatalf("bad: \n%s", ui.OutputWriter.String())
   103  	}
   104  }
   105  
   106  func TestShow_plan(t *testing.T) {
   107  	planPath := testPlanFile(t, &terraform.Plan{
   108  		Module: new(module.Tree),
   109  	})
   110  
   111  	ui := new(cli.MockUi)
   112  	c := &ShowCommand{
   113  		Meta: Meta{
   114  			ContextOpts: testCtxConfig(testProvider()),
   115  			Ui:          ui,
   116  		},
   117  	}
   118  
   119  	args := []string{
   120  		planPath,
   121  	}
   122  	if code := c.Run(args); code != 0 {
   123  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   124  	}
   125  }
   126  
   127  func TestShow_state(t *testing.T) {
   128  	originalState := testState()
   129  	statePath := testStateFile(t, originalState)
   130  
   131  	ui := new(cli.MockUi)
   132  	c := &ShowCommand{
   133  		Meta: Meta{
   134  			ContextOpts: testCtxConfig(testProvider()),
   135  			Ui:          ui,
   136  		},
   137  	}
   138  
   139  	args := []string{
   140  		statePath,
   141  	}
   142  	if code := c.Run(args); code != 0 {
   143  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   144  	}
   145  }