2014-10-01 06:49:24 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2021-02-03 21:05:05 +01:00
|
|
|
"bytes"
|
2014-10-01 06:49:24 +02:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2018-09-30 17:52:29 +02:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2014-10-01 06:49:24 +02:00
|
|
|
"github.com/mitchellh/cli"
|
2018-05-23 04:33:45 +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
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
2018-09-30 17:52:29 +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-09-30 17:20:32 +02:00
|
|
|
"github.com/hashicorp/terraform/states/statefile"
|
2014-10-01 06:49:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestApply_destroy(t *testing.T) {
|
2021-02-03 20:10:14 +01:00
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("apply"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
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
|
|
|
originalState := states.BuildState(func(s *states.SyncState) {
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"bar"}`),
|
|
|
|
Status: states.ObjectReady,
|
2014-10-01 06:49:24 +02:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-04-01 21:55:25 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
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
|
|
|
)
|
|
|
|
})
|
2014-10-01 06:49:24 +02:00
|
|
|
statePath := testStateFile(t, originalState)
|
|
|
|
|
|
|
|
p := testProvider()
|
2021-01-12 22:13:10 +01:00
|
|
|
p.GetSchemaResponse = &providers.GetSchemaResponse{
|
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2018-09-30 17:20:32 +02:00
|
|
|
"test_instance": {
|
2021-01-12 22:13:10 +01:00
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Computed: true},
|
|
|
|
"ami": {Type: cty.String, Optional: true},
|
|
|
|
},
|
2018-09-30 17:20:32 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-05-23 04:33:45 +02:00
|
|
|
|
2014-10-01 06:49:24 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
|
|
|
Destroy: true,
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-10-01 06:49:24 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the apply command pointing to our existing state
|
|
|
|
args := []string{
|
2018-02-01 01:14:42 +01:00
|
|
|
"-auto-approve",
|
2014-10-01 06:49:24 +02:00
|
|
|
"-state", statePath,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
2017-02-15 17:53:19 +01:00
|
|
|
t.Log(ui.OutputWriter.String())
|
2014-10-01 06:49:24 +02:00
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify a new state exists
|
|
|
|
if _, err := os.Stat(statePath); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(statePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2018-09-30 17:20:32 +02:00
|
|
|
stateFile, err := statefile.Read(f)
|
2014-10-01 06:49:24 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-09-30 17:20:32 +02:00
|
|
|
if stateFile.State == nil {
|
2014-10-01 06:49:24 +02:00
|
|
|
t.Fatal("state should not be nil")
|
|
|
|
}
|
|
|
|
|
2018-09-30 17:20:32 +02:00
|
|
|
actualStr := strings.TrimSpace(stateFile.State.String())
|
2014-10-01 06:49:24 +02:00
|
|
|
expectedStr := strings.TrimSpace(testApplyDestroyStr)
|
|
|
|
if actualStr != expectedStr {
|
|
|
|
t.Fatalf("bad:\n\n%s\n\n%s", actualStr, expectedStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should have a backup file
|
2015-09-11 20:56:20 +02:00
|
|
|
f, err = os.Open(statePath + DefaultBackupExtension)
|
2014-10-01 06:49:24 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-09-30 17:20:32 +02:00
|
|
|
backupStateFile, err := statefile.Read(f)
|
2014-10-01 06:49:24 +02:00
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-09-30 17:20:32 +02:00
|
|
|
actualStr = strings.TrimSpace(backupStateFile.State.String())
|
2014-10-01 06:49:24 +02:00
|
|
|
expectedStr = strings.TrimSpace(originalState.String())
|
|
|
|
if actualStr != expectedStr {
|
|
|
|
t.Fatalf("bad:\n\n%s\n\n%s", actualStr, expectedStr)
|
2021-02-03 21:05:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApply_destroyApproveNo(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("apply"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
// Create some existing state
|
|
|
|
originalState := states.BuildState(func(s *states.SyncState) {
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"bar"}`),
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
},
|
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
|
|
Module: addrs.RootModule,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
statePath := testStateFile(t, originalState)
|
|
|
|
|
|
|
|
// Disable test mode so input would be asked
|
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
|
|
|
|
// Answer approval request with "no"
|
|
|
|
defaultInputReader = bytes.NewBufferString("no\n")
|
|
|
|
defaultInputWriter = new(bytes.Buffer)
|
|
|
|
|
|
|
|
p := applyFixtureProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
|
|
|
Destroy: true,
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
if got, want := ui.OutputWriter.String(), "Destroy cancelled"; !strings.Contains(got, want) {
|
|
|
|
t.Fatalf("expected output to include %q, but was:\n%s", want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
state := testStateRead(t, statePath)
|
|
|
|
if state == nil {
|
|
|
|
t.Fatal("state should not be nil")
|
|
|
|
}
|
|
|
|
actualStr := strings.TrimSpace(state.String())
|
|
|
|
expectedStr := strings.TrimSpace(originalState.String())
|
|
|
|
if actualStr != expectedStr {
|
|
|
|
t.Fatalf("bad:\n\n%s\n\n%s", actualStr, expectedStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApply_destroyApproveYes(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("apply"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
|
|
|
p := applyFixtureProvider()
|
|
|
|
|
|
|
|
// Disable test mode so input would be asked
|
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
|
|
|
|
// Answer approval request with "yes"
|
|
|
|
defaultInputReader = bytes.NewBufferString("yes\n")
|
|
|
|
defaultInputWriter = new(bytes.Buffer)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
|
|
|
Destroy: true,
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(statePath); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
state := testStateRead(t, statePath)
|
|
|
|
if state == nil {
|
|
|
|
t.Fatal("state should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
actualStr := strings.TrimSpace(state.String())
|
|
|
|
expectedStr := strings.TrimSpace(testApplyDestroyStr)
|
|
|
|
if actualStr != expectedStr {
|
|
|
|
t.Fatalf("bad:\n\n%s\n\n%s", actualStr, expectedStr)
|
2014-10-01 06:49:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-03 22:06:01 +01:00
|
|
|
func TestApply_destroyLockedState(t *testing.T) {
|
2021-02-03 20:10:14 +01:00
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("apply"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
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
|
|
|
originalState := states.BuildState(func(s *states.SyncState) {
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"bar"}`),
|
|
|
|
Status: states.ObjectReady,
|
2017-02-03 22:06:01 +01:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-04-01 21:55:25 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
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
|
|
|
)
|
|
|
|
})
|
2017-02-03 22:06:01 +01:00
|
|
|
statePath := testStateFile(t, originalState)
|
|
|
|
|
2018-11-20 09:58:59 +01:00
|
|
|
unlock, err := testLockState(testDataDir, statePath)
|
2017-02-03 22:06:01 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer unlock()
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
|
|
|
Destroy: true,
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2017-02-03 22:06:01 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the apply command pointing to our existing state
|
|
|
|
args := []string{
|
2018-02-01 01:14:42 +01:00
|
|
|
"-auto-approve",
|
2017-02-03 22:06:01 +01:00
|
|
|
"-state", statePath,
|
|
|
|
}
|
|
|
|
|
|
|
|
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 22:06:01 +01:00
|
|
|
t.Fatal("command output does not look like a lock error:", output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-01 06:49:24 +02:00
|
|
|
func TestApply_destroyPlan(t *testing.T) {
|
2021-02-03 20:10:14 +01:00
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("apply"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
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
|
|
|
planPath := testPlanFileNoop(t)
|
2014-10-01 06:49:24 +02:00
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
|
|
|
Destroy: true,
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-10-01 06:49:24 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the apply command pointing to our existing state
|
|
|
|
args := []string{
|
|
|
|
planPath,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
2021-02-03 20:10:14 +01:00
|
|
|
output := ui.ErrorWriter.String()
|
|
|
|
if !strings.Contains(output, "plan file") {
|
|
|
|
t.Fatal("expected command output to refer to plan file, but got:", output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApply_destroyPath(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("apply"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
p := applyFixtureProvider()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
|
|
|
Destroy: true,
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-auto-approve",
|
|
|
|
testFixturePath("apply"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
output := ui.ErrorWriter.String()
|
|
|
|
if !strings.Contains(output, "-chdir") {
|
|
|
|
t.Fatal("expected command output to refer to -chdir flag, but got:", output)
|
|
|
|
}
|
2014-10-01 06:49:24 +02:00
|
|
|
}
|
|
|
|
|
2015-03-24 17:18:15 +01:00
|
|
|
func TestApply_destroyTargeted(t *testing.T) {
|
2021-02-03 20:10:14 +01:00
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("apply-destroy-targeted"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
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
|
|
|
originalState := states.BuildState(func(s *states.SyncState) {
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
2019-12-04 05:20:18 +01:00
|
|
|
}.Instance(addrs.IntKey(0)).Absolute(addrs.RootModuleInstance),
|
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
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"i-ab123"}`),
|
|
|
|
Status: states.ObjectReady,
|
2015-03-24 17:18:15 +01:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-04-01 21:55:25 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
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
|
|
|
)
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_load_balancer",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
2019-12-04 05:20:18 +01:00
|
|
|
AttrsJSON: []byte(`{"id":"i-abc123"}`),
|
2020-03-23 20:26:18 +01:00
|
|
|
Dependencies: []addrs.ConfigResource{mustResourceAddr("test_instance.foo")},
|
2019-12-04 05:20:18 +01:00
|
|
|
Status: states.ObjectReady,
|
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
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-04-01 21:55:25 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
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
|
|
|
)
|
|
|
|
})
|
2015-03-24 17:18:15 +01:00
|
|
|
statePath := testStateFile(t, originalState)
|
|
|
|
|
|
|
|
p := testProvider()
|
2021-01-12 22:13:10 +01:00
|
|
|
p.GetSchemaResponse = &providers.GetSchemaResponse{
|
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2018-05-23 04:33:45 +02:00
|
|
|
"test_instance": {
|
2021-01-12 22:13:10 +01:00
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Computed: true},
|
|
|
|
},
|
2018-05-23 04:33:45 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"test_load_balancer": {
|
2021-01-12 22:13:10 +01:00
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Computed: true},
|
|
|
|
"instances": {Type: cty.List(cty.String), Optional: true},
|
|
|
|
},
|
2018-05-23 04:33:45 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-10-14 16:59:15 +02:00
|
|
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
|
2018-09-30 17:52:29 +02:00
|
|
|
return providers.PlanResourceChangeResponse{
|
|
|
|
PlannedState: req.ProposedNewState,
|
|
|
|
}
|
|
|
|
}
|
2018-05-23 04:33:45 +02:00
|
|
|
|
2015-03-24 17:18:15 +01:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
|
|
|
Destroy: true,
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2015-03-24 17:18:15 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the apply command pointing to our existing state
|
|
|
|
args := []string{
|
2018-02-01 01:14:42 +01:00
|
|
|
"-auto-approve",
|
2015-03-24 17:18:15 +01:00
|
|
|
"-target", "test_instance.foo",
|
|
|
|
"-state", statePath,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify a new state exists
|
|
|
|
if _, err := os.Stat(statePath); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(statePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2018-09-30 17:52:29 +02:00
|
|
|
stateFile, err := statefile.Read(f)
|
2015-03-24 17:18:15 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-09-30 17:52:29 +02:00
|
|
|
if stateFile == nil || stateFile.State == nil {
|
2015-03-24 17:18:15 +01:00
|
|
|
t.Fatal("state should not be nil")
|
|
|
|
}
|
|
|
|
|
2018-09-30 17:52:29 +02:00
|
|
|
spew.Config.DisableMethods = true
|
|
|
|
if !stateFile.State.Empty() {
|
|
|
|
t.Fatalf("unexpected final state\ngot: %s\nwant: empty state", spew.Sdump(stateFile.State))
|
2015-03-24 17:18:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Should have a backup file
|
2015-09-11 20:56:20 +02:00
|
|
|
f, err = os.Open(statePath + DefaultBackupExtension)
|
2015-03-24 17:18:15 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-09-30 17:52:29 +02:00
|
|
|
backupStateFile, err := statefile.Read(f)
|
2015-03-24 17:18:15 +01:00
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-09-30 17:52:29 +02:00
|
|
|
actualStr := strings.TrimSpace(backupStateFile.State.String())
|
|
|
|
expectedStr := strings.TrimSpace(originalState.String())
|
2015-03-24 17:18:15 +01:00
|
|
|
if actualStr != expectedStr {
|
|
|
|
t.Fatalf("bad:\n\nactual:\n%s\n\nexpected:\nb%s", actualStr, expectedStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-01 06:49:24 +02:00
|
|
|
const testApplyDestroyStr = `
|
|
|
|
<no state>
|
|
|
|
`
|