2014-06-19 22:51:05 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2015-11-10 19:31:15 +01:00
|
|
|
"bytes"
|
2014-06-19 22:51:05 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-07-12 06:03:56 +02:00
|
|
|
"path/filepath"
|
2017-01-19 05:50:45 +01:00
|
|
|
"reflect"
|
2014-09-18 19:40:23 +02:00
|
|
|
"strings"
|
2017-12-20 21:51:02 +01:00
|
|
|
"sync"
|
2014-06-19 22:51:05 +02:00
|
|
|
"testing"
|
2017-12-20 21:51:02 +01:00
|
|
|
"time"
|
2014-06-19 22:51:05 +02:00
|
|
|
|
2018-10-14 18:21:31 +02:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2018-09-30 16:54:32 +02:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"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"
|
2018-10-14 17:29:52 +02:00
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
2017-01-19 05:50:45 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/copy"
|
2018-10-14 18:21:31 +02:00
|
|
|
"github.com/hashicorp/terraform/plans"
|
2018-09-30 16:54:32 +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"
|
2014-06-19 22:51:05 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2014-07-12 05:51:26 +02:00
|
|
|
func TestPlan(t *testing.T) {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if err := os.Chdir(testFixturePath("plan")); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Chdir(cwd)
|
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
p := planFixtureProvider()
|
2014-07-12 05:51:26 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PlanCommand{
|
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 05:51:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
2016-12-09 17:34:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-03 22:02:22 +01:00
|
|
|
func TestPlan_lockedState(t *testing.T) {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testPath := testFixturePath("plan")
|
2017-02-06 19:26:03 +01:00
|
|
|
unlock, err := testLockState("./testdata", filepath.Join(testPath, DefaultStateFilename))
|
2017-02-03 22:02:22 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer unlock()
|
|
|
|
|
|
|
|
if err := os.Chdir(testPath); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Chdir(cwd)
|
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
p := planFixtureProvider()
|
2017-02-03 22:02:22 +01:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2017-02-03 22:02:22 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
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:02:22 +01:00
|
|
|
t.Fatal("command output does not look like a lock error:", output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 17:34:38 +01:00
|
|
|
func TestPlan_plan(t *testing.T) {
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
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)
|
2016-12-09 17:34:38 +01:00
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2016-12-09 17:34:38 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{planPath}
|
2018-10-14 18:21:31 +02:00
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("wrong exit status %d; want 1\nstderr: %s", code, ui.ErrorWriter.String())
|
2014-07-12 05:51:26 +02:00
|
|
|
}
|
2016-12-12 19:45:26 +01:00
|
|
|
|
2018-09-30 16:54:32 +02:00
|
|
|
if p.ReadResourceCalled {
|
|
|
|
t.Fatal("ReadResource should not have been called")
|
2016-12-12 19:45:26 +01:00
|
|
|
}
|
2014-07-12 05:51:26 +02:00
|
|
|
}
|
|
|
|
|
2014-07-01 18:12:05 +02:00
|
|
|
func TestPlan_destroy(t *testing.T) {
|
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-07-01 18:12:05 +02: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
|
|
|
addrs.ProviderConfig{Type: "test"}.Absolute(addrs.RootModuleInstance),
|
|
|
|
)
|
|
|
|
})
|
2014-07-01 18:12:05 +02:00
|
|
|
outPath := testTempFile(t)
|
|
|
|
statePath := testStateFile(t, originalState)
|
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
p := planFixtureProvider()
|
2014-07-01 18:12:05 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PlanCommand{
|
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-01 18:12:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-destroy",
|
|
|
|
"-out", outPath,
|
|
|
|
"-state", statePath,
|
|
|
|
testFixturePath("plan"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2018-09-30 16:54:32 +02:00
|
|
|
if !p.ReadResourceCalled {
|
|
|
|
t.Fatal("ReadResource should have been called")
|
2014-07-08 06:12:12 +02:00
|
|
|
}
|
|
|
|
|
2014-07-01 18:12:05 +02:00
|
|
|
plan := testReadPlan(t, outPath)
|
2018-10-14 18:21:31 +02:00
|
|
|
for _, rc := range plan.Changes.Resources {
|
|
|
|
if got, want := rc.Action, plans.Delete; got != want {
|
|
|
|
t.Fatalf("wrong action %s for %s; want %s\nplanned change: %s", got, rc.Addr, want, spew.Sdump(rc))
|
2014-07-01 18:12:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-19 02:16:09 +02:00
|
|
|
|
2014-06-20 20:47:02 +02:00
|
|
|
func TestPlan_noState(t *testing.T) {
|
2016-05-11 02:03:58 +02:00
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
p := planFixtureProvider()
|
2014-06-19 22:51:05 +02:00
|
|
|
ui := new(cli.MockUi)
|
2014-06-20 20:47:02 +02:00
|
|
|
c := &PlanCommand{
|
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-19 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2014-06-20 20:47:02 +02:00
|
|
|
testFixturePath("plan"),
|
2014-06-19 22:51:05 +02:00
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2014-06-26 18:56:29 +02:00
|
|
|
// Verify that refresh was called
|
2018-09-30 16:54:32 +02:00
|
|
|
if p.ReadResourceCalled {
|
|
|
|
t.Fatal("ReadResource should not be called")
|
2014-06-26 18:56:29 +02:00
|
|
|
}
|
|
|
|
|
2014-06-19 22:51:05 +02:00
|
|
|
// Verify that the provider was called with the existing state
|
2018-09-30 16:54:32 +02:00
|
|
|
actual := p.PlanResourceChangeRequest.PriorState
|
2018-10-14 17:29:52 +02:00
|
|
|
expected := cty.NullVal(p.GetSchemaReturn.ResourceTypes["test_instance"].ImpliedType())
|
2018-09-30 16:54:32 +02:00
|
|
|
if !expected.RawEquals(actual) {
|
|
|
|
t.Fatalf("wrong prior state\ngot: %#v\nwant: %#v", actual, expected)
|
2014-06-19 22:51:05 +02:00
|
|
|
}
|
|
|
|
}
|
2014-06-19 22:51:28 +02:00
|
|
|
|
2014-06-27 07:23:51 +02:00
|
|
|
func TestPlan_outPath(t *testing.T) {
|
2017-02-07 18:11:48 +01:00
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
2018-03-28 19:08:38 +02:00
|
|
|
td := testTempDir(t)
|
|
|
|
outPath := filepath.Join(td, "test.plan")
|
2014-06-27 07:23:51 +02:00
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
p := planFixtureProvider()
|
2014-06-27 07:23:51 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PlanCommand{
|
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 07:23:51 +02:00
|
|
|
}
|
|
|
|
|
2018-09-30 16:54:32 +02:00
|
|
|
p.PlanResourceChangeResponse = providers.PlanResourceChangeResponse{
|
|
|
|
PlannedState: cty.NullVal(cty.EmptyObject),
|
2014-06-27 07:23:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-out", outPath,
|
|
|
|
testFixturePath("plan"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2018-10-14 18:21:31 +02:00
|
|
|
testReadPlan(t, outPath) // will call t.Fatal itself if the file cannot be read
|
2014-06-27 07:23:51 +02:00
|
|
|
}
|
|
|
|
|
2016-01-20 23:00:20 +01:00
|
|
|
func TestPlan_outPathNoChange(t *testing.T) {
|
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{
|
2018-10-14 18:21:31 +02:00
|
|
|
// Aside from "id" (which is computed) the values here must
|
|
|
|
// exactly match the values in the "plan" test fixture in order
|
|
|
|
// to produce the empty plan we need for this test.
|
|
|
|
AttrsJSON: []byte(`{"id":"bar","ami":"bar","network_interface":[{"description":"Main network interface","device_index":"0"}]}`),
|
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
|
|
|
Status: states.ObjectReady,
|
2016-01-20 23:00:20 +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
|
|
|
addrs.ProviderConfig{Type: "test"}.Absolute(addrs.RootModuleInstance),
|
|
|
|
)
|
|
|
|
})
|
2016-01-20 23:00:20 +01:00
|
|
|
statePath := testStateFile(t, originalState)
|
|
|
|
|
2018-03-28 19:08:38 +02:00
|
|
|
td := testTempDir(t)
|
|
|
|
outPath := filepath.Join(td, "test.plan")
|
2016-01-20 23:00:20 +01:00
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
p := planFixtureProvider()
|
2016-01-20 23:00:20 +01:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2016-01-20 23:00:20 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-out", outPath,
|
|
|
|
"-state", statePath,
|
|
|
|
testFixturePath("plan"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
plan := testReadPlan(t, outPath)
|
2018-10-14 18:21:31 +02:00
|
|
|
if !plan.Changes.Empty() {
|
|
|
|
t.Fatalf("Expected empty plan to be written to plan file, got: %s", spew.Sdump(plan))
|
2016-01-20 23:00:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
// When using "-out" with a backend, the plan should encode the backend config
|
|
|
|
func TestPlan_outBackend(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("plan-out-backend"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
// Our state
|
|
|
|
originalState := &terraform.State{
|
|
|
|
Modules: []*terraform.ModuleState{
|
|
|
|
&terraform.ModuleState{
|
|
|
|
Path: []string{"root"},
|
|
|
|
Resources: map[string]*terraform.ResourceState{
|
|
|
|
"test_instance.foo": &terraform.ResourceState{
|
|
|
|
Type: "test_instance",
|
|
|
|
Primary: &terraform.InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
originalState.Init()
|
|
|
|
|
|
|
|
// Setup our backend state
|
|
|
|
dataState, srv := testBackendState(t, originalState, 200)
|
|
|
|
defer srv.Close()
|
|
|
|
testStateFileRemote(t, dataState)
|
|
|
|
|
|
|
|
outPath := "foo"
|
|
|
|
p := testProvider()
|
2018-10-14 18:21:31 +02:00
|
|
|
ui := cli.NewMockUi()
|
2017-01-19 05:50:45 +01:00
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2017-01-19 05:50:45 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-out", outPath,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
2018-10-14 18:21:31 +02:00
|
|
|
t.Logf("stdout: %s", ui.OutputWriter.String())
|
2017-01-19 05:50:45 +01:00
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
plan := testReadPlan(t, outPath)
|
2018-10-14 18:21:31 +02:00
|
|
|
if !plan.Changes.Empty() {
|
|
|
|
t.Fatalf("Expected empty plan to be written to plan file, got: %s", spew.Sdump(plan))
|
2017-01-19 05:50:45 +01:00
|
|
|
}
|
|
|
|
|
2018-10-14 18:21:31 +02:00
|
|
|
if plan.Backend.Type == "" || plan.Backend.Config == nil {
|
2017-01-19 05:50:45 +01:00
|
|
|
t.Fatal("should have backend info")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(plan.Backend, dataState.Backend) {
|
2018-10-14 18:21:31 +02:00
|
|
|
t.Fatalf("wrong backend config in plan\ngot: %swant: %s", spew.Sdump(plan.Backend), spew.Sdump(dataState.Backend))
|
2017-01-19 05:50:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-30 16:54:32 +02:00
|
|
|
func TestPlan_refreshFalse(t *testing.T) {
|
2017-02-07 18:11:48 +01:00
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
p := planFixtureProvider()
|
2014-06-26 18:56:29 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PlanCommand{
|
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-26 18:56:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-refresh=false",
|
|
|
|
testFixturePath("plan"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2018-09-30 16:54:32 +02:00
|
|
|
if p.ReadResourceCalled {
|
|
|
|
t.Fatal("ReadResource should not have been called")
|
2014-06-26 18:56:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:47:02 +02:00
|
|
|
func TestPlan_state(t *testing.T) {
|
2014-09-18 19:40:23 +02:00
|
|
|
originalState := testState()
|
2018-03-28 19:08:38 +02:00
|
|
|
statePath := testStateFile(t, originalState)
|
2014-06-19 22:51:05 +02:00
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
p := planFixtureProvider()
|
2014-06-19 22:51:05 +02:00
|
|
|
ui := new(cli.MockUi)
|
2014-06-20 20:47:02 +02:00
|
|
|
c := &PlanCommand{
|
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-19 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
2014-06-20 20:47:02 +02:00
|
|
|
testFixturePath("plan"),
|
2014-06-19 22:51:05 +02:00
|
|
|
}
|
|
|
|
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
|
2018-09-30 16:54:32 +02:00
|
|
|
actual := p.PlanResourceChangeRequest.PriorState
|
|
|
|
expected := cty.ObjectVal(map[string]cty.Value{
|
2018-10-15 00:36:19 +02:00
|
|
|
"id": cty.StringVal("bar"),
|
|
|
|
"ami": cty.NullVal(cty.String),
|
2018-10-14 17:29:52 +02:00
|
|
|
"network_interface": cty.NullVal(cty.List(cty.Object(map[string]cty.Type{
|
2018-10-15 00:36:19 +02:00
|
|
|
"device_index": cty.String,
|
|
|
|
"description": cty.String,
|
2018-10-14 17:29:52 +02:00
|
|
|
}))),
|
2018-09-30 16:54:32 +02:00
|
|
|
})
|
|
|
|
if !expected.RawEquals(actual) {
|
|
|
|
t.Fatalf("wrong prior state\ngot: %#v\nwant: %#v", actual, expected)
|
2014-06-19 22:51:05 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-12 06:03:56 +02:00
|
|
|
|
|
|
|
func TestPlan_stateDefault(t *testing.T) {
|
2014-09-18 19:40:23 +02:00
|
|
|
originalState := testState()
|
2018-03-28 19:08:38 +02:00
|
|
|
statePath := testStateFile(t, originalState)
|
2014-07-12 06:03:56 +02:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
p := planFixtureProvider()
|
2014-07-12 06:03:56 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PlanCommand{
|
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:03:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2018-03-28 19:08:38 +02:00
|
|
|
"-state", statePath,
|
2014-07-12 06:03:56 +02:00
|
|
|
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
|
2018-09-30 16:54:32 +02:00
|
|
|
actual := p.PlanResourceChangeRequest.PriorState
|
|
|
|
expected := cty.ObjectVal(map[string]cty.Value{
|
2018-10-15 00:36:19 +02:00
|
|
|
"id": cty.StringVal("bar"),
|
|
|
|
"ami": cty.NullVal(cty.String),
|
2018-10-14 17:29:52 +02:00
|
|
|
"network_interface": cty.NullVal(cty.List(cty.Object(map[string]cty.Type{
|
2018-10-15 00:36:19 +02:00
|
|
|
"device_index": cty.String,
|
|
|
|
"description": cty.String,
|
2018-10-14 17:29:52 +02:00
|
|
|
}))),
|
2018-09-30 16:54:32 +02:00
|
|
|
})
|
|
|
|
if !expected.RawEquals(actual) {
|
|
|
|
t.Fatalf("wrong prior state\ngot: %#v\nwant: %#v", actual, expected)
|
2014-07-12 06:03:56 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-18 20:37:27 +02:00
|
|
|
|
2016-09-04 00:26:49 +02:00
|
|
|
func TestPlan_validate(t *testing.T) {
|
|
|
|
// This is triggered by not asking for input so we have to set this to false
|
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if err := os.Chdir(testFixturePath("plan-invalid")); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Chdir(cwd)
|
|
|
|
|
|
|
|
p := testProvider()
|
2018-10-14 17:29:52 +02:00
|
|
|
p.GetSchemaReturn = &terraform.ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"test_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
2018-10-15 00:36:19 +02:00
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
2018-10-14 17:29:52 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
|
|
|
|
return providers.PlanResourceChangeResponse{
|
|
|
|
PlannedState: req.ProposedNewState,
|
|
|
|
}
|
|
|
|
}
|
2016-09-04 00:26:49 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2016-09-04 00:26:49 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := ui.ErrorWriter.String()
|
2018-10-14 18:21:31 +02:00
|
|
|
if want := "Error: Invalid count argument"; !strings.Contains(actual, want) {
|
|
|
|
t.Fatalf("unexpected error output\ngot:\n%s\n\nshould contain: %s", actual, want)
|
2016-09-04 00:26:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-18 20:37:27 +02:00
|
|
|
func TestPlan_vars(t *testing.T) {
|
2017-02-07 18:11:48 +01:00
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
p := planVarsFixtureProvider()
|
2014-07-18 20:37:27 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-18 20:37:27 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := ""
|
|
|
|
p.DiffFn = func(
|
2014-09-17 20:15:07 +02:00
|
|
|
info *terraform.InstanceInfo,
|
|
|
|
s *terraform.InstanceState,
|
2014-09-18 01:33:24 +02:00
|
|
|
c *terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
|
2014-07-18 20:37:27 +02:00
|
|
|
if v, ok := c.Config["value"]; ok {
|
|
|
|
actual = v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-var", "foo=bar",
|
|
|
|
testFixturePath("plan-vars"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if actual != "bar" {
|
|
|
|
t.Fatal("didn't work")
|
|
|
|
}
|
|
|
|
}
|
2014-07-18 23:00:40 +02:00
|
|
|
|
2015-11-10 19:31:15 +01:00
|
|
|
func TestPlan_varsUnset(t *testing.T) {
|
2017-02-07 18:11:48 +01:00
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
2015-11-10 19:31:15 +01:00
|
|
|
// Disable test mode so input would be asked
|
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
|
|
|
|
defaultInputReader = bytes.NewBufferString("bar\n")
|
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
p := planVarsFixtureProvider()
|
2015-11-10 19:31:15 +01:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2015-11-10 19:31:15 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
testFixturePath("plan-vars"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-18 23:00:40 +02:00
|
|
|
func TestPlan_varFile(t *testing.T) {
|
2017-02-07 18:11:48 +01:00
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
2014-07-18 23:00:40 +02:00
|
|
|
varFilePath := testTempFile(t)
|
|
|
|
if err := ioutil.WriteFile(varFilePath, []byte(planVarFile), 0644); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
p := planVarsFixtureProvider()
|
2014-07-18 23:00:40 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-18 23:00:40 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := ""
|
|
|
|
p.DiffFn = func(
|
2014-09-17 20:15:07 +02:00
|
|
|
info *terraform.InstanceInfo,
|
|
|
|
s *terraform.InstanceState,
|
2014-09-18 01:33:24 +02:00
|
|
|
c *terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
|
2014-07-18 23:00:40 +02:00
|
|
|
if v, ok := c.Config["value"]; ok {
|
|
|
|
actual = v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-var-file", varFilePath,
|
|
|
|
testFixturePath("plan-vars"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if actual != "bar" {
|
|
|
|
t.Fatal("didn't work")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-05 18:32:01 +02:00
|
|
|
func TestPlan_varFileDefault(t *testing.T) {
|
|
|
|
varFileDir := testTempDir(t)
|
|
|
|
varFilePath := filepath.Join(varFileDir, "terraform.tfvars")
|
|
|
|
if err := ioutil.WriteFile(varFilePath, []byte(planVarFile), 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)
|
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
p := planVarsFixtureProvider()
|
2014-08-05 18:32:01 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-08-05 18:32:01 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := ""
|
|
|
|
p.DiffFn = func(
|
2014-09-17 20:15:07 +02:00
|
|
|
info *terraform.InstanceInfo,
|
|
|
|
s *terraform.InstanceState,
|
2014-09-18 01:33:24 +02:00
|
|
|
c *terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
|
2014-08-05 18:32:01 +02:00
|
|
|
if v, ok := c.Config["value"]; ok {
|
|
|
|
actual = v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
testFixturePath("plan-vars"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if actual != "bar" {
|
|
|
|
t.Fatal("didn't work")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-01 18:38:19 +02:00
|
|
|
func TestPlan_detailedExitcode(t *testing.T) {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if err := os.Chdir(testFixturePath("plan")); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Chdir(cwd)
|
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
p := planFixtureProvider()
|
2015-04-01 18:38:19 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2015-04-01 18:38:19 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"-detailed-exitcode"}
|
|
|
|
if code := c.Run(args); code != 2 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPlan_detailedExitcode_emptyDiff(t *testing.T) {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if err := os.Chdir(testFixturePath("plan-emptydiff")); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Chdir(cwd)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2015-04-01 18:38:19 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"-detailed-exitcode"}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-01 17:27:32 +01:00
|
|
|
func TestPlan_shutdown(t *testing.T) {
|
2017-12-20 21:51:02 +01:00
|
|
|
cancelled := make(chan struct{})
|
2017-12-01 17:27:32 +01:00
|
|
|
shutdownCh := make(chan struct{})
|
2018-02-10 01:13:34 +01:00
|
|
|
|
2017-12-01 17:27:32 +01:00
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
ShutdownCh: shutdownCh,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-12-03 04:36:43 +01:00
|
|
|
p.StopFn = func() error {
|
2017-12-20 21:51:02 +01:00
|
|
|
close(cancelled)
|
2017-12-03 04:36:43 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-20 21:51:02 +01:00
|
|
|
var once sync.Once
|
|
|
|
|
2017-12-01 17:27:32 +01:00
|
|
|
p.DiffFn = func(
|
|
|
|
*terraform.InstanceInfo,
|
|
|
|
*terraform.InstanceState,
|
|
|
|
*terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
|
|
|
|
|
2017-12-20 21:51:02 +01:00
|
|
|
once.Do(func() {
|
2017-12-01 17:27:32 +01:00
|
|
|
shutdownCh <- struct{}{}
|
2017-12-20 21:51:02 +01:00
|
|
|
})
|
2017-12-01 17:27:32 +01:00
|
|
|
|
2018-01-31 21:24:07 +01:00
|
|
|
// Because of the internal lock in the MockProvider, we can't
|
2018-10-14 18:21:31 +02:00
|
|
|
// coordinate directly with the calling of Stop, and making the
|
2018-01-31 21:24:07 +01:00
|
|
|
// MockProvider concurrent is disruptive to a lot of existing tests.
|
|
|
|
// Wait here a moment to help make sure the main goroutine gets to the
|
|
|
|
// Stop call before we exit, or the plan may finish before it can be
|
|
|
|
// canceled.
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
|
2017-12-01 17:27:32 +01:00
|
|
|
return &terraform.InstanceDiff{
|
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
|
|
"ami": &terraform.ResourceAttrDiff{
|
|
|
|
New: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
2018-10-14 18:21:31 +02:00
|
|
|
p.GetSchemaReturn = &terraform.ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"test_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"ami": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-12-01 17:27:32 +01:00
|
|
|
|
2018-10-14 18:21:31 +02:00
|
|
|
code := c.Run([]string{
|
|
|
|
// Unfortunately it seems like this test can inadvertently pick up
|
|
|
|
// leftover state from other tests without this. Ideally we should
|
|
|
|
// find which test is leaving a terraform.tfstate behind and stop it
|
|
|
|
// doing that, but this will stop this test flapping for now.
|
|
|
|
"-state=nonexistent.tfstate",
|
|
|
|
testFixturePath("apply-shutdown"),
|
|
|
|
})
|
|
|
|
if code != 1 {
|
2018-02-10 01:13:34 +01:00
|
|
|
// FIXME: we should be able to avoid the error during evaluation
|
|
|
|
// the early exit isn't caught before the interpolation is evaluated
|
2018-10-14 18:21:31 +02:00
|
|
|
t.Fatalf("wrong exit code %d; want 1\noutput:\n%s", code, ui.OutputWriter.String())
|
2017-12-01 17:27:32 +01:00
|
|
|
}
|
|
|
|
|
2017-12-20 21:51:02 +01:00
|
|
|
select {
|
|
|
|
case <-cancelled:
|
2018-01-31 21:24:07 +01:00
|
|
|
default:
|
2017-12-01 17:27:32 +01:00
|
|
|
t.Fatal("command not cancelled")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
// planFixtureSchema returns a schema suitable for processing the
|
|
|
|
// configuration in test-fixtures/plan . This schema should be
|
|
|
|
// assigned to a mock provider named "test".
|
|
|
|
func planFixtureSchema() *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},
|
|
|
|
},
|
|
|
|
BlockTypes: map[string]*configschema.NestedBlock{
|
|
|
|
"network_interface": {
|
|
|
|
Nesting: configschema.NestingList,
|
|
|
|
Block: configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"device_index": {Type: cty.String, Optional: true},
|
|
|
|
"description": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// planFixtureProvider returns a mock provider that is configured for basic
|
|
|
|
// operation with the configuration in test-fixtures/plan. This mock has
|
|
|
|
// GetSchemaReturn and PlanResourceChangeFn populated, with the plan
|
|
|
|
// step just passing through the new object proposed by Terraform Core.
|
|
|
|
func planFixtureProvider() *terraform.MockProvider {
|
|
|
|
p := testProvider()
|
|
|
|
p.GetSchemaReturn = planFixtureSchema()
|
|
|
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
|
|
|
|
return providers.PlanResourceChangeResponse{
|
|
|
|
PlannedState: req.ProposedNewState,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// planVarsFixtureSchema returns a schema suitable for processing the
|
|
|
|
// configuration in test-fixtures/plan-vars . This schema should be
|
|
|
|
// assigned to a mock provider named "test".
|
|
|
|
func planVarsFixtureSchema() *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},
|
|
|
|
"value": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// planVarsFixtureProvider returns a mock provider that is configured for basic
|
|
|
|
// operation with the configuration in test-fixtures/plan-vars. This mock has
|
|
|
|
// GetSchemaReturn and PlanResourceChangeFn populated, with the plan
|
|
|
|
// step just passing through the new object proposed by Terraform Core.
|
|
|
|
func planVarsFixtureProvider() *terraform.MockProvider {
|
|
|
|
p := testProvider()
|
|
|
|
p.GetSchemaReturn = planVarsFixtureSchema()
|
|
|
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
|
|
|
|
return providers.PlanResourceChangeResponse{
|
|
|
|
PlannedState: req.ProposedNewState,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2014-07-18 23:00:40 +02:00
|
|
|
const planVarFile = `
|
|
|
|
foo = "bar"
|
|
|
|
`
|
2014-09-18 19:40:23 +02:00
|
|
|
|
|
|
|
const testPlanNoStateStr = `
|
|
|
|
<not created>
|
|
|
|
`
|
|
|
|
|
|
|
|
const testPlanStateStr = `
|
|
|
|
ID = bar
|
2016-04-21 21:59:10 +02:00
|
|
|
Tainted = false
|
2014-09-18 19:40:23 +02:00
|
|
|
`
|
|
|
|
|
|
|
|
const testPlanStateDefaultStr = `
|
|
|
|
ID = bar
|
2016-04-21 21:59:10 +02:00
|
|
|
Tainted = false
|
2014-09-18 19:40:23 +02:00
|
|
|
`
|