mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-25 08:21:07 -06:00
command/plan: default state path
This commit is contained in:
parent
04f7281e8c
commit
6bf543cb07
@ -9,6 +9,9 @@ import (
|
||||
"github.com/mitchellh/cli"
|
||||
)
|
||||
|
||||
// DefaultStateFilename is the default filename used for the state file.
|
||||
const DefaultStateFilename = "terraform.tfstate"
|
||||
|
||||
func ContextArg(
|
||||
path string,
|
||||
statePath string,
|
||||
|
@ -10,7 +10,17 @@ import (
|
||||
)
|
||||
|
||||
// This is the directory where our test fixtures are.
|
||||
const fixtureDir = "./test-fixtures"
|
||||
var fixtureDir = "./test-fixtures"
|
||||
|
||||
func init() {
|
||||
// Expand the fixture dir on init because we change the working
|
||||
// directory in some tests.
|
||||
var err error
|
||||
fixtureDir, err = filepath.Abs(fixtureDir)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testFixturePath(name string) string {
|
||||
return filepath.Join(fixtureDir, name)
|
||||
|
@ -27,7 +27,7 @@ func (c *PlanCommand) Run(args []string) int {
|
||||
cmdFlags.BoolVar(&destroy, "destroy", false, "destroy")
|
||||
cmdFlags.BoolVar(&refresh, "refresh", true, "refresh")
|
||||
cmdFlags.StringVar(&outPath, "out", "", "path")
|
||||
cmdFlags.StringVar(&statePath, "state", "", "path")
|
||||
cmdFlags.StringVar(&statePath, "state", DefaultStateFilename, "path")
|
||||
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
||||
if err := cmdFlags.Parse(args); err != nil {
|
||||
return 1
|
||||
@ -51,6 +51,15 @@ func (c *PlanCommand) Run(args []string) int {
|
||||
}
|
||||
}
|
||||
|
||||
// If the default state path doesn't exist, ignore it.
|
||||
if statePath != "" {
|
||||
if _, err := os.Stat(statePath); err != nil {
|
||||
if os.IsNotExist(err) && statePath == DefaultStateFilename {
|
||||
statePath = ""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Load up the state
|
||||
var state *terraform.State
|
||||
if statePath != "" {
|
||||
@ -70,7 +79,7 @@ func (c *PlanCommand) Run(args []string) int {
|
||||
|
||||
b, err := config.LoadDir(path)
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Error loading blueprint: %s", err))
|
||||
c.Ui.Error(fmt.Sprintf("Error loading config: %s", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,7 @@ package command
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
@ -207,3 +208,62 @@ func TestPlan_state(t *testing.T) {
|
||||
t.Fatalf("bad: %#v", p.DiffState)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlan_stateDefault(t *testing.T) {
|
||||
originalState := &terraform.State{
|
||||
Resources: map[string]*terraform.ResourceState{
|
||||
"test_instance.foo": &terraform.ResourceState{
|
||||
ID: "bar",
|
||||
Type: "test_instance",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Write the state file in a temporary directory with the
|
||||
// default filename.
|
||||
td, err := ioutil.TempDir("", "tf")
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
statePath := filepath.Join(td, DefaultStateFilename)
|
||||
|
||||
f, err := os.Create(statePath)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
err = terraform.WriteState(originalState, f)
|
||||
f.Close()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
// Change to that directory
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
if err := os.Chdir(filepath.Dir(statePath)); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
defer os.Chdir(cwd)
|
||||
|
||||
p := testProvider()
|
||||
ui := new(cli.MockUi)
|
||||
c := &PlanCommand{
|
||||
ContextOpts: testCtxConfig(p),
|
||||
Ui: ui,
|
||||
}
|
||||
|
||||
args := []string{
|
||||
testFixturePath("plan"),
|
||||
}
|
||||
if code := c.Run(args); code != 0 {
|
||||
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||
}
|
||||
|
||||
// Verify that the provider was called with the existing state
|
||||
expectedState := originalState.Resources["test_instance.foo"]
|
||||
if !reflect.DeepEqual(p.DiffState, expectedState) {
|
||||
t.Fatalf("bad: %#v", p.DiffState)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user