command/init: remove dependency on remote package
This commit is contained in:
parent
330364f668
commit
622690583c
|
@ -178,17 +178,20 @@ func testTempDir(t *testing.T) string {
|
|||
// testCwdDir is used to change the current working directory
|
||||
// into a test directory that should be remoted after
|
||||
func testCwd(t *testing.T) (string, string) {
|
||||
tmp, err := ioutil.TempDir("", "remote")
|
||||
tmp, err := ioutil.TempDir("", "tf")
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
|
||||
if err := os.Chdir(tmp); err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
|
||||
return tmp, cwd
|
||||
}
|
||||
|
||||
|
@ -197,6 +200,7 @@ func testFixCwd(t *testing.T, tmp, cwd string) {
|
|||
if err := os.Chdir(cwd); err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
|
||||
if err := os.RemoveAll(tmp); err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
|
|
|
@ -8,7 +8,6 @@ import (
|
|||
|
||||
"github.com/hashicorp/terraform/config"
|
||||
"github.com/hashicorp/terraform/config/module"
|
||||
"github.com/hashicorp/terraform/remote"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
)
|
||||
|
||||
|
@ -102,27 +101,29 @@ func (c *InitCommand) Run(args []string) int {
|
|||
"path": remotePath,
|
||||
}
|
||||
|
||||
// Ensure remote state is not already enabled
|
||||
haveLocal, err := remote.HaveLocalState()
|
||||
state, err := c.State()
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Failed to check for local state: %v", err))
|
||||
c.Ui.Error(fmt.Sprintf("Error checking for state: %s", err))
|
||||
return 1
|
||||
}
|
||||
if haveLocal {
|
||||
c.Ui.Error("Remote state is already enabled. Aborting.")
|
||||
return 1
|
||||
}
|
||||
|
||||
// Check if we have the non-managed state file
|
||||
haveNonManaged, err := remote.ExistsFile(DefaultStateFilename)
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Failed to check for state file: %v", err))
|
||||
return 1
|
||||
}
|
||||
if haveNonManaged {
|
||||
c.Ui.Error(fmt.Sprintf("Existing state file '%s' found. Aborting.",
|
||||
DefaultStateFilename))
|
||||
return 1
|
||||
if state != nil {
|
||||
s := state.State()
|
||||
if !s.Empty() {
|
||||
c.Ui.Error(fmt.Sprintf(
|
||||
"State file already exists and is not empty! Please remove this\n" +
|
||||
"state file before initializing. Note that removing the state file\n" +
|
||||
"may result in a loss of information since Terraform uses this\n" +
|
||||
"to track your infrastructure."))
|
||||
return 1
|
||||
}
|
||||
if s.IsRemote() {
|
||||
c.Ui.Error(fmt.Sprintf(
|
||||
"State file already exists with remote state enabled! Please remove this\n" +
|
||||
"state file before initializing. Note that removing the state file\n" +
|
||||
"may result in a loss of information since Terraform uses this\n" +
|
||||
"to track your infrastructure."))
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize a blank state file with remote enabled
|
||||
|
|
|
@ -181,3 +181,76 @@ func TestInit_remoteState(t *testing.T) {
|
|||
t.Fatalf("missing state")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInit_remoteStateWithLocal(t *testing.T) {
|
||||
tmp, cwd := testCwd(t)
|
||||
defer testFixCwd(t, tmp, cwd)
|
||||
|
||||
statePath := filepath.Join(tmp, DefaultStateFilename)
|
||||
|
||||
// Write some state
|
||||
f, err := os.Create(statePath)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
err = terraform.WriteState(testState(), f)
|
||||
f.Close()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
ui := new(cli.MockUi)
|
||||
c := &InitCommand{
|
||||
Meta: Meta{
|
||||
ContextOpts: testCtxConfig(testProvider()),
|
||||
Ui: ui,
|
||||
},
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"-backend", "http",
|
||||
"-address", "http://google.com",
|
||||
testFixturePath("init"),
|
||||
}
|
||||
if code := c.Run(args); code == 0 {
|
||||
t.Fatalf("should have failed: \n%s", ui.OutputWriter.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestInit_remoteStateWithRemote(t *testing.T) {
|
||||
tmp, cwd := testCwd(t)
|
||||
defer testFixCwd(t, tmp, cwd)
|
||||
|
||||
statePath := filepath.Join(tmp, DefaultDataDir, DefaultStateFilename)
|
||||
if err := os.MkdirAll(filepath.Dir(statePath), 0755); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
// Write some state
|
||||
f, err := os.Create(statePath)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
err = terraform.WriteState(testState(), f)
|
||||
f.Close()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
ui := new(cli.MockUi)
|
||||
c := &InitCommand{
|
||||
Meta: Meta{
|
||||
ContextOpts: testCtxConfig(testProvider()),
|
||||
Ui: ui,
|
||||
},
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"-backend", "http",
|
||||
"-address", "http://google.com",
|
||||
testFixturePath("init"),
|
||||
}
|
||||
if code := c.Run(args); code == 0 {
|
||||
t.Fatalf("should have failed: \n%s", ui.OutputWriter.String())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue