command/init: put remote state config at proper path [GH-2927]

This commit is contained in:
Mitchell Hashimoto 2016-01-19 17:13:19 -08:00
parent 79fafbdbe8
commit 6a972a7713
2 changed files with 42 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/hashicorp/go-getter"
@ -52,6 +53,11 @@ func (c *InitCommand) Run(args []string) int {
}
}
// Set the state out path to be the path requested for the module
// to be copied. This ensures any remote states gets setup in the
// proper directory.
c.Meta.dataDir = filepath.Join(path, DefaultDataDirectory)
source := args[0]
// Get our pwd since we need it

View File

@ -179,6 +179,42 @@ func TestInit_remoteState(t *testing.T) {
}
}
func TestInit_remoteStateSubdir(t *testing.T) {
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)
subdir := filepath.Join(tmp, "subdir")
s := terraform.NewState()
conf, srv := testRemoteState(t, s, 200)
defer srv.Close()
ui := new(cli.MockUi)
c := &InitCommand{
Meta: Meta{
ContextOpts: testCtxConfig(testProvider()),
Ui: ui,
},
}
args := []string{
"-backend", "http",
"-backend-config", "address=" + conf.Config["address"],
testFixturePath("init"),
subdir,
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
}
if _, err := os.Stat(filepath.Join(subdir, "hello.tf")); err != nil {
t.Fatalf("err: %s", err)
}
if _, err := os.Stat(filepath.Join(subdir, DefaultDataDir, DefaultStateFilename)); err != nil {
t.Fatalf("missing state: %s", err)
}
}
func TestInit_remoteStateWithLocal(t *testing.T) {
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)