2014-06-27 20:09:01 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2015-11-27 16:10:42 +01:00
|
|
|
"bytes"
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2014-06-27 20:09:01 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-07-12 06:56:43 +02:00
|
|
|
"path/filepath"
|
2018-10-15 01:07:42 +02:00
|
|
|
"reflect"
|
2014-09-18 19:40:23 +02:00
|
|
|
"strings"
|
2014-09-21 18:42:48 +02:00
|
|
|
"testing"
|
2014-06-27 20:09:01 +02:00
|
|
|
|
2018-10-15 01:07:42 +02:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
"github.com/mitchellh/cli"
|
2018-09-30 17:01:59 +02:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
|
2018-10-15 01:07:42 +02:00
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2018-10-15 00:33:55 +02:00
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
2017-03-16 20:05:11 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/copy"
|
2018-09-30 17:01:59 +02:00
|
|
|
"github.com/hashicorp/terraform/providers"
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
"github.com/hashicorp/terraform/states"
|
2018-10-15 00:33:55 +02:00
|
|
|
"github.com/hashicorp/terraform/states/statefile"
|
2018-10-15 01:07:42 +02:00
|
|
|
"github.com/hashicorp/terraform/states/statemgr"
|
2014-06-27 20:09:01 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRefresh(t *testing.T) {
|
2014-09-17 20:15:07 +02:00
|
|
|
state := testState()
|
2014-07-08 06:12:12 +02:00
|
|
|
statePath := testStateFile(t, state)
|
2014-06-27 20:09:01 +02:00
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &RefreshCommand{
|
2014-07-13 05:37:30 +02:00
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-13 05:37:30 +02:00
|
|
|
},
|
2014-06-27 20:09:01 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 00:33:55 +02:00
|
|
|
p.GetSchemaReturn = refreshFixtureSchema()
|
2018-09-30 17:01:59 +02:00
|
|
|
p.ReadResourceFn = nil
|
|
|
|
p.ReadResourceResponse = providers.ReadResourceResponse{
|
|
|
|
NewState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.StringVal("yes"),
|
|
|
|
}),
|
|
|
|
}
|
2014-06-27 20:09:01 +02:00
|
|
|
|
|
|
|
args := []string{
|
2014-07-12 06:56:43 +02:00
|
|
|
"-state", statePath,
|
2014-06-27 20:09:01 +02:00
|
|
|
testFixturePath("refresh"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2018-09-30 17:01:59 +02:00
|
|
|
if !p.ReadResourceCalled {
|
|
|
|
t.Fatal("ReadResource should have been called")
|
2014-06-27 20:09:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(statePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-10-15 00:33:55 +02:00
|
|
|
newStateFile, err := statefile.Read(f)
|
2014-06-27 20:09:01 +02:00
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-10-15 00:33:55 +02:00
|
|
|
actual := strings.TrimSpace(newStateFile.State.String())
|
2014-09-18 19:40:23 +02:00
|
|
|
expected := strings.TrimSpace(testRefreshStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
2014-06-27 20:09:01 +02:00
|
|
|
}
|
|
|
|
}
|
2014-06-27 20:12:45 +02:00
|
|
|
|
2017-03-16 20:05:11 +01:00
|
|
|
func TestRefresh_empty(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("refresh-empty"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2017-03-16 20:05:11 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-30 17:01:59 +02:00
|
|
|
p.ReadResourceFn = nil
|
|
|
|
p.ReadResourceResponse = providers.ReadResourceResponse{
|
|
|
|
NewState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.StringVal("yes"),
|
|
|
|
}),
|
|
|
|
}
|
2017-03-16 20:05:11 +01:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
td,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2018-09-30 17:01:59 +02:00
|
|
|
if p.ReadResourceCalled {
|
|
|
|
t.Fatal("ReadResource should not have been called")
|
2017-03-16 20:05:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-03 21:32:40 +01:00
|
|
|
func TestRefresh_lockedState(t *testing.T) {
|
|
|
|
state := testState()
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
2017-02-06 19:26:03 +01:00
|
|
|
unlock, err := testLockState("./testdata", statePath)
|
2017-02-03 21:32:40 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer unlock()
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2017-02-03 21:32:40 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-10-15 00:33:55 +02:00
|
|
|
p.GetSchemaReturn = refreshFixtureSchema()
|
2018-09-30 17:01:59 +02:00
|
|
|
p.ReadResourceFn = nil
|
|
|
|
p.ReadResourceResponse = providers.ReadResourceResponse{
|
|
|
|
NewState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.StringVal("yes"),
|
|
|
|
}),
|
|
|
|
}
|
2017-02-03 21:32:40 +01:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
testFixturePath("refresh"),
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := c.Run(args); code == 0 {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
output := ui.ErrorWriter.String()
|
2017-02-15 20:01:18 +01:00
|
|
|
if !strings.Contains(output, "lock") {
|
2017-02-03 21:32:40 +01:00
|
|
|
t.Fatal("command output does not look like a lock error:", output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-12 06:56:43 +02:00
|
|
|
func TestRefresh_cwd(t *testing.T) {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if err := os.Chdir(testFixturePath("refresh")); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Chdir(cwd)
|
|
|
|
|
2014-09-17 20:15:07 +02:00
|
|
|
state := testState()
|
2014-07-12 06:56:43 +02:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &RefreshCommand{
|
2014-07-13 05:37:30 +02:00
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-13 05:37:30 +02:00
|
|
|
},
|
2014-07-12 06:56:43 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 00:33:55 +02:00
|
|
|
p.GetSchemaReturn = refreshFixtureSchema()
|
2018-09-30 17:01:59 +02:00
|
|
|
p.ReadResourceFn = nil
|
|
|
|
p.ReadResourceResponse = providers.ReadResourceResponse{
|
|
|
|
NewState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.StringVal("yes"),
|
|
|
|
}),
|
|
|
|
}
|
2014-07-12 06:56:43 +02:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2018-09-30 17:01:59 +02:00
|
|
|
if !p.ReadResourceCalled {
|
|
|
|
t.Fatal("ReadResource should have been called")
|
2014-07-12 06:56:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(statePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-10-15 00:33:55 +02:00
|
|
|
newStateFile, err := statefile.Read(f)
|
2014-07-12 06:56:43 +02:00
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-10-15 00:33:55 +02:00
|
|
|
actual := strings.TrimSpace(newStateFile.State.String())
|
2014-09-18 19:40:23 +02:00
|
|
|
expected := strings.TrimSpace(testRefreshCwdStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
2014-07-12 06:56:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRefresh_defaultState(t *testing.T) {
|
2018-10-15 01:07:42 +02:00
|
|
|
originalState := testState()
|
|
|
|
|
|
|
|
// Write the state file in a temporary directory with the
|
|
|
|
// default filename.
|
|
|
|
statePath := testStateFile(t, originalState)
|
|
|
|
|
|
|
|
localState := statemgr.NewFilesystem(statePath)
|
|
|
|
if err := localState.RefreshState(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s := localState.State()
|
|
|
|
if s == nil {
|
|
|
|
t.Fatal("empty test state")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p.GetSchemaReturn = refreshFixtureSchema()
|
|
|
|
p.ReadResourceFn = nil
|
|
|
|
p.ReadResourceResponse = providers.ReadResourceResponse{
|
|
|
|
NewState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.StringVal("yes"),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
testFixturePath("refresh"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.ReadResourceCalled {
|
|
|
|
t.Fatal("ReadResource should have been called")
|
|
|
|
}
|
|
|
|
|
|
|
|
newState := testStateRead(t, statePath)
|
|
|
|
|
|
|
|
actual := newState.RootModule().Resources["test_instance.foo"].Instances[addrs.NoKey].Current
|
|
|
|
expected := &states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte("{\n \"ami\": null,\n \"id\": \"yes\"\n }"),
|
|
|
|
Dependencies: []addrs.Referenceable{},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected))
|
|
|
|
}
|
|
|
|
|
|
|
|
backupState := testStateRead(t, statePath+DefaultBackupExtension)
|
|
|
|
|
|
|
|
actual = backupState.RootModule().Resources["test_instance.foo"].Instances[addrs.NoKey].Current
|
|
|
|
expected = originalState.RootModule().Resources["test_instance.foo"].Instances[addrs.NoKey].Current
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected))
|
|
|
|
}
|
2016-03-11 20:07:54 +01:00
|
|
|
}
|
|
|
|
|
2014-06-27 20:12:45 +02:00
|
|
|
func TestRefresh_outPath(t *testing.T) {
|
2018-10-15 01:07:42 +02:00
|
|
|
state := testState()
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
// Output path
|
|
|
|
outf, err := ioutil.TempFile(testingDir, "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
outPath := outf.Name()
|
|
|
|
outf.Close()
|
|
|
|
os.Remove(outPath)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p.GetSchemaReturn = refreshFixtureSchema()
|
|
|
|
p.ReadResourceFn = nil
|
|
|
|
p.ReadResourceResponse = providers.ReadResourceResponse{
|
|
|
|
NewState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.StringVal("yes"),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-state-out", outPath,
|
|
|
|
testFixturePath("refresh"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
newState := testStateRead(t, statePath)
|
|
|
|
if !reflect.DeepEqual(newState, state) {
|
|
|
|
t.Fatalf("bad: %#v", newState)
|
|
|
|
}
|
|
|
|
|
|
|
|
newState = testStateRead(t, outPath)
|
|
|
|
actual := newState.RootModule().Resources["test_instance.foo"].Instances[addrs.NoKey].Current
|
|
|
|
expected := &states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte("{\n \"ami\": null,\n \"id\": \"yes\"\n }"),
|
|
|
|
Dependencies: []addrs.Referenceable{},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected))
|
|
|
|
}
|
|
|
|
|
2018-11-17 01:09:25 +01:00
|
|
|
if _, err := os.Stat(outPath+DefaultBackupExtension); !os.IsNotExist(err) {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to test for backup file: %s", err)
|
|
|
|
}
|
|
|
|
t.Fatalf("backup file exists, but it should not because output file did not initially exist")
|
2018-10-15 01:07:42 +02:00
|
|
|
}
|
2014-06-27 20:12:45 +02:00
|
|
|
}
|
2014-07-18 20:37:27 +02:00
|
|
|
|
|
|
|
func TestRefresh_var(t *testing.T) {
|
2014-09-17 20:15:07 +02:00
|
|
|
state := testState()
|
2014-07-18 20:37:27 +02:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-18 20:37:27 +02:00
|
|
|
},
|
|
|
|
}
|
2018-10-15 00:33:55 +02:00
|
|
|
p.GetSchemaReturn = refreshVarFixtureSchema()
|
2014-07-18 20:37:27 +02:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-var", "foo=bar",
|
|
|
|
"-state", statePath,
|
|
|
|
testFixturePath("refresh-var"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.ConfigureCalled {
|
|
|
|
t.Fatal("configure should be called")
|
|
|
|
}
|
2018-09-30 17:01:59 +02:00
|
|
|
if got, want := p.ConfigureRequest.Config.GetAttr("value"), cty.StringVal("bar"); !want.RawEquals(got) {
|
|
|
|
t.Fatalf("wrong provider configuration\ngot: %#v\nwant: %#v", got, want)
|
2014-07-18 20:37:27 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-18 23:00:40 +02:00
|
|
|
|
|
|
|
func TestRefresh_varFile(t *testing.T) {
|
2014-09-17 20:15:07 +02:00
|
|
|
state := testState()
|
2014-07-18 23:00:40 +02:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-18 23:00:40 +02:00
|
|
|
},
|
|
|
|
}
|
2018-10-15 00:33:55 +02:00
|
|
|
p.GetSchemaReturn = refreshVarFixtureSchema()
|
2014-07-18 23:00:40 +02:00
|
|
|
|
|
|
|
varFilePath := testTempFile(t)
|
|
|
|
if err := ioutil.WriteFile(varFilePath, []byte(refreshVarFile), 0644); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-var-file", varFilePath,
|
|
|
|
"-state", statePath,
|
|
|
|
testFixturePath("refresh-var"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.ConfigureCalled {
|
|
|
|
t.Fatal("configure should be called")
|
|
|
|
}
|
2018-09-30 17:01:59 +02:00
|
|
|
if got, want := p.ConfigureRequest.Config.GetAttr("value"), cty.StringVal("bar"); !want.RawEquals(got) {
|
|
|
|
t.Fatalf("wrong provider configuration\ngot: %#v\nwant: %#v", got, want)
|
2014-07-18 23:00:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-05 18:32:01 +02:00
|
|
|
func TestRefresh_varFileDefault(t *testing.T) {
|
2014-09-17 20:15:07 +02:00
|
|
|
state := testState()
|
2014-08-05 18:32:01 +02:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-08-05 18:32:01 +02:00
|
|
|
},
|
|
|
|
}
|
2018-10-15 00:33:55 +02:00
|
|
|
p.GetSchemaReturn = refreshVarFixtureSchema()
|
2014-08-05 18:32:01 +02:00
|
|
|
|
|
|
|
varFileDir := testTempDir(t)
|
|
|
|
varFilePath := filepath.Join(varFileDir, "terraform.tfvars")
|
|
|
|
if err := ioutil.WriteFile(varFilePath, []byte(refreshVarFile), 0644); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if err := os.Chdir(varFileDir); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Chdir(cwd)
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
testFixturePath("refresh-var"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.ConfigureCalled {
|
|
|
|
t.Fatal("configure should be called")
|
|
|
|
}
|
2018-09-30 17:01:59 +02:00
|
|
|
if got, want := p.ConfigureRequest.Config.GetAttr("value"), cty.StringVal("bar"); !want.RawEquals(got) {
|
|
|
|
t.Fatalf("wrong provider configuration\ngot: %#v\nwant: %#v", got, want)
|
2014-08-05 18:32:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-23 09:32:46 +01:00
|
|
|
func TestRefresh_varsUnset(t *testing.T) {
|
|
|
|
// Disable test mode so input would be asked
|
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
|
|
|
|
defaultInputReader = bytes.NewBufferString("bar\n")
|
|
|
|
|
|
|
|
state := testState()
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2015-11-23 09:32:46 +01:00
|
|
|
},
|
|
|
|
}
|
2018-10-15 00:33:55 +02:00
|
|
|
p.GetSchemaReturn = &terraform.ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"test_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
|
|
"ami": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-11-23 09:32:46 +01:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
testFixturePath("refresh-unset-var"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-28 04:58:12 +02:00
|
|
|
func TestRefresh_backup(t *testing.T) {
|
2018-10-15 01:07:42 +02:00
|
|
|
state := testState()
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
// Output path
|
|
|
|
outf, err := ioutil.TempFile(testingDir, "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
outPath := outf.Name()
|
|
|
|
outf.Close()
|
|
|
|
os.Remove(outPath)
|
|
|
|
|
|
|
|
// Backup path
|
|
|
|
backupf, err := ioutil.TempFile(testingDir, "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
backupPath := backupf.Name()
|
|
|
|
backupf.Close()
|
|
|
|
os.Remove(backupPath)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p.GetSchemaReturn = refreshFixtureSchema()
|
|
|
|
p.ReadResourceFn = nil
|
|
|
|
p.ReadResourceResponse = providers.ReadResourceResponse{
|
|
|
|
NewState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.StringVal("changed"),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-state-out", outPath,
|
|
|
|
"-backup", backupPath,
|
|
|
|
testFixturePath("refresh"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
newState := testStateRead(t, statePath)
|
|
|
|
if !reflect.DeepEqual(newState, state) {
|
|
|
|
t.Fatalf("bad: %#v", newState)
|
|
|
|
}
|
|
|
|
|
|
|
|
newState = testStateRead(t, outPath)
|
|
|
|
actual := newState.RootModule().Resources["test_instance.foo"].Instances[addrs.NoKey].Current
|
|
|
|
expected := &states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte("{\n \"ami\": null,\n \"id\": \"changed\"\n }"),
|
|
|
|
Dependencies: []addrs.Referenceable{},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected))
|
|
|
|
}
|
|
|
|
|
2018-11-09 00:05:49 +01:00
|
|
|
backupState := testStateRead(t, backupPath)
|
2018-10-15 01:07:42 +02:00
|
|
|
actualStr := strings.TrimSpace(backupState.String())
|
|
|
|
expectedStr := strings.TrimSpace(state.String())
|
|
|
|
if actualStr != expectedStr {
|
|
|
|
t.Fatalf("bad:\n\n%s\n\n%s", actualStr, expectedStr)
|
|
|
|
}
|
2014-07-28 04:58:12 +02:00
|
|
|
}
|
|
|
|
|
2014-07-28 05:38:41 +02:00
|
|
|
func TestRefresh_disableBackup(t *testing.T) {
|
2018-10-15 01:07:42 +02:00
|
|
|
state := testState()
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
// Output path
|
|
|
|
outf, err := ioutil.TempFile(testingDir, "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
outPath := outf.Name()
|
|
|
|
outf.Close()
|
|
|
|
os.Remove(outPath)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p.GetSchemaReturn = refreshFixtureSchema()
|
|
|
|
p.ReadResourceFn = nil
|
|
|
|
p.ReadResourceResponse = providers.ReadResourceResponse{
|
|
|
|
NewState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.StringVal("yes"),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-state-out", outPath,
|
|
|
|
"-backup", "-",
|
|
|
|
testFixturePath("refresh"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
newState := testStateRead(t, statePath)
|
|
|
|
if !reflect.DeepEqual(newState, state) {
|
|
|
|
t.Fatalf("bad: %#v", newState)
|
|
|
|
}
|
|
|
|
|
|
|
|
newState = testStateRead(t, outPath)
|
|
|
|
actual := newState.RootModule().Resources["test_instance.foo"].Instances[addrs.NoKey].Current
|
|
|
|
expected := &states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte("{\n \"ami\": null,\n \"id\": \"yes\"\n }"),
|
|
|
|
Dependencies: []addrs.Referenceable{},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure there is no backup
|
|
|
|
_, err = os.Stat(outPath + DefaultBackupExtension)
|
|
|
|
if err == nil || !os.IsNotExist(err) {
|
|
|
|
t.Fatalf("backup should not exist")
|
|
|
|
}
|
|
|
|
_, err = os.Stat("-")
|
|
|
|
if err == nil || !os.IsNotExist(err) {
|
|
|
|
t.Fatalf("backup should not exist")
|
|
|
|
}
|
2014-07-28 05:38:41 +02:00
|
|
|
}
|
|
|
|
|
2015-06-15 17:40:56 +02:00
|
|
|
func TestRefresh_displaysOutputs(t *testing.T) {
|
|
|
|
state := testState()
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2015-06-15 17:40:56 +02:00
|
|
|
},
|
|
|
|
}
|
2018-10-15 00:33:55 +02:00
|
|
|
p.GetSchemaReturn = &terraform.ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"test_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
|
|
"ami": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-06-15 17:40:56 +02:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
testFixturePath("refresh-output"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that outputs were displayed
|
|
|
|
outputValue := "foo.example.com"
|
|
|
|
actual := ui.OutputWriter.String()
|
|
|
|
if !strings.Contains(actual, outputValue) {
|
|
|
|
t.Fatalf("Expected:\n%s\n\nTo include: %q", actual, outputValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
// newInstanceState creates a new states.ResourceInstanceObjectSrc with the
|
|
|
|
// given value for its single id attribute. It is named newInstanceState for
|
|
|
|
// historical reasons, because it was originally written for the poorly-named
|
|
|
|
// terraform.InstanceState type.
|
|
|
|
func newInstanceState(id string) *states.ResourceInstanceObjectSrc {
|
|
|
|
attrs := map[string]interface{}{
|
|
|
|
"id": id,
|
|
|
|
}
|
|
|
|
attrsJSON, err := json.Marshal(attrs)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to marshal attributes: %s", err)) // should never happen
|
|
|
|
}
|
|
|
|
return &states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: attrsJSON,
|
|
|
|
Status: states.ObjectReady,
|
2016-08-10 21:47:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-15 00:33:55 +02:00
|
|
|
// refreshFixtureSchema returns a schema suitable for processing the
|
|
|
|
// configuration in test-fixtures/refresh . This schema should be
|
|
|
|
// assigned to a mock provider named "test".
|
|
|
|
func refreshFixtureSchema() *terraform.ProviderSchema {
|
|
|
|
return &terraform.ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"test_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
|
|
"ami": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// refreshVarFixtureSchema returns a schema suitable for processing the
|
|
|
|
// configuration in test-fixtures/refresh-var . This schema should be
|
|
|
|
// assigned to a mock provider named "test".
|
|
|
|
func refreshVarFixtureSchema() *terraform.ProviderSchema {
|
|
|
|
return &terraform.ProviderSchema{
|
|
|
|
Provider: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"value": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"test_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-18 23:00:40 +02:00
|
|
|
const refreshVarFile = `
|
|
|
|
foo = "bar"
|
|
|
|
`
|
2014-09-18 19:40:23 +02:00
|
|
|
|
|
|
|
const testRefreshStr = `
|
|
|
|
test_instance.foo:
|
|
|
|
ID = yes
|
2017-11-08 03:18:01 +01:00
|
|
|
provider = provider.test
|
2014-09-18 19:40:23 +02:00
|
|
|
`
|
|
|
|
const testRefreshCwdStr = `
|
|
|
|
test_instance.foo:
|
|
|
|
ID = yes
|
2017-11-08 03:18:01 +01:00
|
|
|
provider = provider.test
|
2014-09-18 19:40:23 +02:00
|
|
|
`
|