github.com/acm1/terraform@v0.6.2-0.20150729164239-1f314444f45c/command/init_test.go (about)

     1  package command
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/mitchellh/cli"
    10  )
    11  
    12  func TestInit(t *testing.T) {
    13  	dir := tempDir(t)
    14  
    15  	ui := new(cli.MockUi)
    16  	c := &InitCommand{
    17  		Meta: Meta{
    18  			ContextOpts: testCtxConfig(testProvider()),
    19  			Ui:          ui,
    20  		},
    21  	}
    22  
    23  	args := []string{
    24  		testFixturePath("init"),
    25  		dir,
    26  	}
    27  	if code := c.Run(args); code != 0 {
    28  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
    29  	}
    30  
    31  	if _, err := os.Stat(filepath.Join(dir, "hello.tf")); err != nil {
    32  		t.Fatalf("err: %s", err)
    33  	}
    34  }
    35  
    36  func TestInit_cwd(t *testing.T) {
    37  	dir := tempDir(t)
    38  	if err := os.MkdirAll(dir, 0755); err != nil {
    39  		t.Fatalf("err: %s", err)
    40  	}
    41  
    42  	// Change to the temporary directory
    43  	cwd, err := os.Getwd()
    44  	if err != nil {
    45  		t.Fatalf("err: %s", err)
    46  	}
    47  	if err := os.Chdir(dir); err != nil {
    48  		t.Fatalf("err: %s", err)
    49  	}
    50  	defer os.Chdir(cwd)
    51  
    52  	ui := new(cli.MockUi)
    53  	c := &InitCommand{
    54  		Meta: Meta{
    55  			ContextOpts: testCtxConfig(testProvider()),
    56  			Ui:          ui,
    57  		},
    58  	}
    59  
    60  	args := []string{
    61  		testFixturePath("init"),
    62  	}
    63  	if code := c.Run(args); code != 0 {
    64  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
    65  	}
    66  
    67  	if _, err := os.Stat("hello.tf"); err != nil {
    68  		t.Fatalf("err: %s", err)
    69  	}
    70  }
    71  
    72  func TestInit_multipleArgs(t *testing.T) {
    73  	ui := new(cli.MockUi)
    74  	c := &InitCommand{
    75  		Meta: Meta{
    76  			ContextOpts: testCtxConfig(testProvider()),
    77  			Ui:          ui,
    78  		},
    79  	}
    80  
    81  	args := []string{
    82  		"bad",
    83  		"bad",
    84  	}
    85  	if code := c.Run(args); code != 1 {
    86  		t.Fatalf("bad: \n%s", ui.OutputWriter.String())
    87  	}
    88  }
    89  
    90  func TestInit_noArgs(t *testing.T) {
    91  	ui := new(cli.MockUi)
    92  	c := &InitCommand{
    93  		Meta: Meta{
    94  			ContextOpts: testCtxConfig(testProvider()),
    95  			Ui:          ui,
    96  		},
    97  	}
    98  
    99  	args := []string{}
   100  	if code := c.Run(args); code != 1 {
   101  		t.Fatalf("bad: \n%s", ui.OutputWriter.String())
   102  	}
   103  }
   104  
   105  // https://github.com/hashicorp/terraform/issues/518
   106  func TestInit_dstInSrc(t *testing.T) {
   107  	dir := tempDir(t)
   108  	if err := os.MkdirAll(dir, 0755); err != nil {
   109  		t.Fatalf("err: %s", err)
   110  	}
   111  
   112  	// Change to the temporary directory
   113  	cwd, err := os.Getwd()
   114  	if err != nil {
   115  		t.Fatalf("err: %s", err)
   116  	}
   117  	if err := os.Chdir(dir); err != nil {
   118  		t.Fatalf("err: %s", err)
   119  	}
   120  	defer os.Chdir(cwd)
   121  
   122  	if _, err := os.Create("issue518.tf"); err != nil {
   123  		t.Fatalf("err: %s", err)
   124  	}
   125  
   126  	ui := new(cli.MockUi)
   127  	c := &InitCommand{
   128  		Meta: Meta{
   129  			ContextOpts: testCtxConfig(testProvider()),
   130  			Ui:          ui,
   131  		},
   132  	}
   133  
   134  	args := []string{
   135  		".",
   136  		"foo",
   137  	}
   138  	if code := c.Run(args); code != 0 {
   139  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   140  	}
   141  
   142  	if _, err := os.Stat(filepath.Join(dir, "foo", "issue518.tf")); err != nil {
   143  		t.Fatalf("err: %s", err)
   144  	}
   145  }
   146  
   147  func TestInit_remoteState(t *testing.T) {
   148  	tmp, cwd := testCwd(t)
   149  	defer testFixCwd(t, tmp, cwd)
   150  
   151  	s := terraform.NewState()
   152  	conf, srv := testRemoteState(t, s, 200)
   153  	defer srv.Close()
   154  
   155  	ui := new(cli.MockUi)
   156  	c := &InitCommand{
   157  		Meta: Meta{
   158  			ContextOpts: testCtxConfig(testProvider()),
   159  			Ui:          ui,
   160  		},
   161  	}
   162  
   163  	args := []string{
   164  		"-backend", "http",
   165  		"-backend-config", "address=" + conf.Config["address"],
   166  		testFixturePath("init"),
   167  		tmp,
   168  	}
   169  	if code := c.Run(args); code != 0 {
   170  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   171  	}
   172  
   173  	if _, err := os.Stat(filepath.Join(tmp, "hello.tf")); err != nil {
   174  		t.Fatalf("err: %s", err)
   175  	}
   176  
   177  	if _, err := os.Stat(filepath.Join(tmp, DefaultDataDir, DefaultStateFilename)); err != nil {
   178  		t.Fatalf("missing state: %s", err)
   179  	}
   180  }
   181  
   182  func TestInit_remoteStateWithLocal(t *testing.T) {
   183  	tmp, cwd := testCwd(t)
   184  	defer testFixCwd(t, tmp, cwd)
   185  
   186  	statePath := filepath.Join(tmp, DefaultStateFilename)
   187  
   188  	// Write some state
   189  	f, err := os.Create(statePath)
   190  	if err != nil {
   191  		t.Fatalf("err: %s", err)
   192  	}
   193  	err = terraform.WriteState(testState(), f)
   194  	f.Close()
   195  	if err != nil {
   196  		t.Fatalf("err: %s", err)
   197  	}
   198  
   199  	ui := new(cli.MockUi)
   200  	c := &InitCommand{
   201  		Meta: Meta{
   202  			ContextOpts: testCtxConfig(testProvider()),
   203  			Ui:          ui,
   204  		},
   205  	}
   206  
   207  	args := []string{
   208  		"-backend", "http",
   209  		"-backend-config", "address=http://google.com",
   210  		testFixturePath("init"),
   211  	}
   212  	if code := c.Run(args); code == 0 {
   213  		t.Fatalf("should have failed: \n%s", ui.OutputWriter.String())
   214  	}
   215  }
   216  
   217  func TestInit_remoteStateWithRemote(t *testing.T) {
   218  	tmp, cwd := testCwd(t)
   219  	defer testFixCwd(t, tmp, cwd)
   220  
   221  	statePath := filepath.Join(tmp, DefaultDataDir, DefaultStateFilename)
   222  	if err := os.MkdirAll(filepath.Dir(statePath), 0755); err != nil {
   223  		t.Fatalf("err: %s", err)
   224  	}
   225  
   226  	// Write some state
   227  	f, err := os.Create(statePath)
   228  	if err != nil {
   229  		t.Fatalf("err: %s", err)
   230  	}
   231  	err = terraform.WriteState(testState(), f)
   232  	f.Close()
   233  	if err != nil {
   234  		t.Fatalf("err: %s", err)
   235  	}
   236  
   237  	ui := new(cli.MockUi)
   238  	c := &InitCommand{
   239  		Meta: Meta{
   240  			ContextOpts: testCtxConfig(testProvider()),
   241  			Ui:          ui,
   242  		},
   243  	}
   244  
   245  	args := []string{
   246  		"-backend", "http",
   247  		"-backend-config", "address=http://google.com",
   248  		testFixturePath("init"),
   249  	}
   250  	if code := c.Run(args); code == 0 {
   251  		t.Fatalf("should have failed: \n%s", ui.OutputWriter.String())
   252  	}
   253  }