mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-28 01:41:48 -06:00
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
|
// testCwdDir is used to change the current working directory
|
||||||
// into a test directory that should be remoted after
|
// into a test directory that should be remoted after
|
||||||
func testCwd(t *testing.T) (string, string) {
|
func testCwd(t *testing.T) (string, string) {
|
||||||
tmp, err := ioutil.TempDir("", "remote")
|
tmp, err := ioutil.TempDir("", "tf")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cwd, err := os.Getwd()
|
cwd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.Chdir(tmp); err != nil {
|
if err := os.Chdir(tmp); err != nil {
|
||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return tmp, cwd
|
return tmp, cwd
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,6 +200,7 @@ func testFixCwd(t *testing.T, tmp, cwd string) {
|
|||||||
if err := os.Chdir(cwd); err != nil {
|
if err := os.Chdir(cwd); err != nil {
|
||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.RemoveAll(tmp); err != nil {
|
if err := os.RemoveAll(tmp); err != nil {
|
||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,6 @@ import (
|
|||||||
|
|
||||||
"github.com/hashicorp/terraform/config"
|
"github.com/hashicorp/terraform/config"
|
||||||
"github.com/hashicorp/terraform/config/module"
|
"github.com/hashicorp/terraform/config/module"
|
||||||
"github.com/hashicorp/terraform/remote"
|
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -102,27 +101,29 @@ func (c *InitCommand) Run(args []string) int {
|
|||||||
"path": remotePath,
|
"path": remotePath,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure remote state is not already enabled
|
state, err := c.State()
|
||||||
haveLocal, err := remote.HaveLocalState()
|
|
||||||
if err != nil {
|
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
|
return 1
|
||||||
}
|
}
|
||||||
if haveLocal {
|
if state != nil {
|
||||||
c.Ui.Error("Remote state is already enabled. Aborting.")
|
s := state.State()
|
||||||
return 1
|
if !s.Empty() {
|
||||||
}
|
c.Ui.Error(fmt.Sprintf(
|
||||||
|
"State file already exists and is not empty! Please remove this\n" +
|
||||||
// Check if we have the non-managed state file
|
"state file before initializing. Note that removing the state file\n" +
|
||||||
haveNonManaged, err := remote.ExistsFile(DefaultStateFilename)
|
"may result in a loss of information since Terraform uses this\n" +
|
||||||
if err != nil {
|
"to track your infrastructure."))
|
||||||
c.Ui.Error(fmt.Sprintf("Failed to check for state file: %v", err))
|
return 1
|
||||||
return 1
|
}
|
||||||
}
|
if s.IsRemote() {
|
||||||
if haveNonManaged {
|
c.Ui.Error(fmt.Sprintf(
|
||||||
c.Ui.Error(fmt.Sprintf("Existing state file '%s' found. Aborting.",
|
"State file already exists with remote state enabled! Please remove this\n" +
|
||||||
DefaultStateFilename))
|
"state file before initializing. Note that removing the state file\n" +
|
||||||
return 1
|
"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
|
// Initialize a blank state file with remote enabled
|
||||||
|
@ -181,3 +181,76 @@ func TestInit_remoteState(t *testing.T) {
|
|||||||
t.Fatalf("missing state")
|
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
Block a user