2014-06-19 22:51:05 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2015-11-10 19:31:15 +01:00
|
|
|
"bytes"
|
2021-05-05 20:13:20 +02:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2014-06-19 22:51:05 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2021-02-02 16:35:45 +01:00
|
|
|
"path"
|
2014-07-12 06:03:56 +02:00
|
|
|
"path/filepath"
|
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/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"
|
2021-05-17 17:42:17 +02:00
|
|
|
backendinit "github.com/hashicorp/terraform/internal/backend/init"
|
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"
|
2021-05-05 20:13:20 +02:00
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
2014-06-19 22:51:05 +02:00
|
|
|
)
|
|
|
|
|
2014-07-12 05:51:26 +02:00
|
|
|
func TestPlan(t *testing.T) {
|
2020-06-18 23:09:20 +02:00
|
|
|
td := tempDir(t)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("plan"), td)
|
2020-06-18 23:09:20 +02:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
2014-07-12 05:51:26 +02:00
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
p := planFixtureProvider()
|
2021-02-22 17:55:00 +01:00
|
|
|
view, done := testView(t)
|
2014-07-12 05:51:26 +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),
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2014-07-13 05:37:30 +02:00
|
|
|
},
|
2014-07-12 05:51:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
2021-02-22 17:55:00 +01:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2016-12-09 17:34:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-03 22:02:22 +01:00
|
|
|
func TestPlan_lockedState(t *testing.T) {
|
2020-06-18 23:09:20 +02:00
|
|
|
td := tempDir(t)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("plan"), td)
|
2020-06-18 23:09:20 +02:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
2017-02-03 22:02:22 +01:00
|
|
|
|
2020-06-18 23:09:20 +02:00
|
|
|
unlock, err := testLockState(testDataDir, filepath.Join(td, DefaultStateFilename))
|
2017-02-03 22:02:22 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer unlock()
|
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
p := planFixtureProvider()
|
2021-02-22 17:55:00 +01:00
|
|
|
view, done := testView(t)
|
2017-02-03 22:02:22 +01:00
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2017-02-03 22:02:22 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
2021-02-22 17:55:00 +01:00
|
|
|
code := c.Run(args)
|
|
|
|
if code == 0 {
|
|
|
|
t.Fatal("expected error", done(t).Stdout())
|
2017-02-03 22:02:22 +01:00
|
|
|
}
|
|
|
|
|
2021-02-22 17:55:00 +01:00
|
|
|
output := done(t).Stderr()
|
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()
|
2021-02-22 17:55:00 +01:00
|
|
|
view, done := testView(t)
|
2016-12-09 17:34:38 +01:00
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2016-12-09 17:34:38 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{planPath}
|
2021-02-22 17:55:00 +01:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 1 {
|
|
|
|
t.Fatalf("wrong exit status %d; want 1\nstderr: %s", code, output.Stderr())
|
2014-07-12 05:51:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 18:12:05 +02:00
|
|
|
func TestPlan_destroy(t *testing.T) {
|
2021-02-02 16:35:45 +01:00
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("plan"), 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-07-01 18:12:05 +02:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-04-02 18:58:44 +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-07-01 18:12:05 +02:00
|
|
|
outPath := testTempFile(t)
|
|
|
|
statePath := testStateFile(t, originalState)
|
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
p := planFixtureProvider()
|
2021-02-22 17:55:00 +01:00
|
|
|
view, done := testView(t)
|
2014-07-01 18:12:05 +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),
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2014-07-13 05:37:30 +02:00
|
|
|
},
|
2014-07-01 18:12:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-destroy",
|
|
|
|
"-out", outPath,
|
|
|
|
"-state", statePath,
|
|
|
|
}
|
2021-02-22 17:55:00 +01:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
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) {
|
2021-02-02 16:35:45 +01:00
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("plan"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
2016-05-11 02:03:58 +02:00
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
p := planFixtureProvider()
|
2021-02-22 17:55:00 +01:00
|
|
|
view, done := testView(t)
|
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),
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2014-07-13 05:37:30 +02:00
|
|
|
},
|
2014-06-19 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
2021-02-02 16:35:45 +01:00
|
|
|
args := []string{}
|
2021-02-22 17:55:00 +01:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2014-06-19 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
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
|
2021-02-18 16:13:43 +01:00
|
|
|
expected := cty.NullVal(p.GetProviderSchemaResponse.ResourceTypes["test_instance"].Block.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) {
|
2021-02-02 16:35:45 +01:00
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("plan"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
2017-02-07 18:11:48 +01:00
|
|
|
|
2018-03-28 19:08:38 +02:00
|
|
|
outPath := filepath.Join(td, "test.plan")
|
2014-06-27 07:23:51 +02:00
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
p := planFixtureProvider()
|
2021-02-22 17:55:00 +01:00
|
|
|
view, done := testView(t)
|
2014-06-27 07:23:51 +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),
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2014-07-13 05:37:30 +02:00
|
|
|
},
|
2014-06-27 07:23:51 +02:00
|
|
|
}
|
|
|
|
|
2021-01-12 22:13:10 +01:00
|
|
|
p.PlanResourceChangeResponse = &providers.PlanResourceChangeResponse{
|
2018-09-30 16:54:32 +02:00
|
|
|
PlannedState: cty.NullVal(cty.EmptyObject),
|
2014-06-27 07:23:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-out", outPath,
|
|
|
|
}
|
2021-02-22 17:55:00 +01:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2014-06-27 07:23:51 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2021-02-02 16:35:45 +01:00
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("plan"), 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{
|
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
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-04-02 18:58:44 +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
|
|
|
)
|
|
|
|
})
|
2016-01-20 23:00:20 +01:00
|
|
|
statePath := testStateFile(t, originalState)
|
|
|
|
|
2018-03-28 19:08:38 +02:00
|
|
|
outPath := filepath.Join(td, "test.plan")
|
2016-01-20 23:00:20 +01:00
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
p := planFixtureProvider()
|
2021-02-22 17:55:00 +01:00
|
|
|
view, done := testView(t)
|
2016-01-20 23:00:20 +01:00
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2016-01-20 23:00:20 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-out", outPath,
|
|
|
|
"-state", statePath,
|
|
|
|
}
|
2021-02-22 17:55:00 +01:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2016-01-20 23:00:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("plan-out-backend"), td)
|
2017-01-19 05:50:45 +01:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2020-04-02 18:58:44 +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","ami":"bar"}`),
|
|
|
|
Status: states.ObjectReady,
|
2017-01-19 05:50:45 +01:00
|
|
|
},
|
2020-04-02 18:58:44 +02:00
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
|
|
Module: addrs.RootModule,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
2017-01-19 05:50:45 +01:00
|
|
|
|
2021-01-26 20:39:11 +01:00
|
|
|
// Set up our backend state
|
2017-01-19 05:50:45 +01:00
|
|
|
dataState, srv := testBackendState(t, originalState, 200)
|
|
|
|
defer srv.Close()
|
|
|
|
testStateFileRemote(t, dataState)
|
|
|
|
|
|
|
|
outPath := "foo"
|
|
|
|
p := testProvider()
|
2021-02-18 16:13:43 +01:00
|
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
2021-01-12 22:13:10 +01:00
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2018-11-08 02:43:46 +01: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-11-08 02:43:46 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
|
|
|
|
return providers.PlanResourceChangeResponse{
|
|
|
|
PlannedState: req.ProposedNewState,
|
|
|
|
}
|
|
|
|
}
|
2021-02-22 17:55:00 +01:00
|
|
|
view, done := testView(t)
|
2017-01-19 05:50:45 +01:00
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2017-01-19 05:50:45 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-out", outPath,
|
|
|
|
}
|
2021-02-22 17:55:00 +01:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Logf("stdout: %s", output.Stdout())
|
|
|
|
t.Fatalf("plan command failed with exit code %d\n\n%s", code, output.Stderr())
|
2017-01-19 05:50:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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-11-08 02:43:46 +01:00
|
|
|
if got, want := plan.Backend.Type, "http"; got != want {
|
|
|
|
t.Errorf("wrong backend type %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
if got, want := plan.Backend.Workspace, "default"; got != want {
|
|
|
|
t.Errorf("wrong backend workspace %q; want %q", got, want)
|
2017-01-19 05:50:45 +01:00
|
|
|
}
|
2018-11-08 02:43:46 +01:00
|
|
|
{
|
|
|
|
httpBackend := backendinit.Backend("http")()
|
|
|
|
schema := httpBackend.ConfigSchema()
|
|
|
|
got, err := plan.Backend.Config.Decode(schema.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to decode backend config in plan: %s", err)
|
|
|
|
}
|
|
|
|
want, err := dataState.Backend.Config(schema)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to decode cached config: %s", err)
|
|
|
|
}
|
|
|
|
if !want.RawEquals(got) {
|
|
|
|
t.Errorf("wrong backend config\ngot: %#v\nwant: %#v", got, want)
|
|
|
|
}
|
2017-01-19 05:50:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-30 16:54:32 +02:00
|
|
|
func TestPlan_refreshFalse(t *testing.T) {
|
2021-02-02 16:35:45 +01:00
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("plan"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
2017-02-07 18:11:48 +01:00
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
p := planFixtureProvider()
|
2021-02-22 17:55:00 +01:00
|
|
|
view, done := testView(t)
|
2014-06-26 18:56:29 +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),
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2014-07-13 05:37:30 +02:00
|
|
|
},
|
2014-06-26 18:56:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-refresh=false",
|
|
|
|
}
|
2021-02-22 17:55:00 +01:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2014-06-26 18:56:29 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2021-02-02 16:35:45 +01:00
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("plan"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
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()
|
2021-02-22 17:55:00 +01:00
|
|
|
view, done := testView(t)
|
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),
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2014-07-13 05:37:30 +02:00
|
|
|
},
|
2014-06-19 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
}
|
2021-02-22 17:55:00 +01:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
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
|
|
|
|
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) {
|
2021-02-02 16:35:45 +01:00
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("plan"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
// Generate state and move it to the default path
|
2014-09-18 19:40:23 +02:00
|
|
|
originalState := testState()
|
2018-03-28 19:08:38 +02:00
|
|
|
statePath := testStateFile(t, originalState)
|
2021-02-02 16:35:45 +01:00
|
|
|
os.Rename(statePath, path.Join(td, "terraform.tfstate"))
|
2014-07-12 06:03:56 +02:00
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
p := planFixtureProvider()
|
2021-02-22 17:55:00 +01:00
|
|
|
view, done := testView(t)
|
2014-07-12 06:03:56 +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),
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2014-07-13 05:37:30 +02:00
|
|
|
},
|
2014-07-12 06:03:56 +02:00
|
|
|
}
|
|
|
|
|
2021-02-02 16:35:45 +01:00
|
|
|
args := []string{}
|
2021-02-22 17:55:00 +01:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2014-07-12 06:03:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 }()
|
|
|
|
|
2020-06-18 23:09:20 +02:00
|
|
|
td := tempDir(t)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("plan-invalid"), td)
|
2020-06-18 23:09:20 +02:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
2016-09-04 00:26:49 +02:00
|
|
|
|
|
|
|
p := testProvider()
|
2021-02-18 16:13:43 +01:00
|
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
2021-01-12 22:13:10 +01:00
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2018-10-14 17:29:52 +02:00
|
|
|
"test_instance": {
|
2021-01-12 22:13:10 +01:00
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"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,
|
|
|
|
}
|
|
|
|
}
|
2021-02-22 17:55:00 +01:00
|
|
|
view, done := testView(t)
|
2016-09-04 00:26:49 +02:00
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2016-09-04 00:26:49 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-02-22 17:55:00 +01:00
|
|
|
args := []string{"-no-color"}
|
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 1 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2016-09-04 00:26:49 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 17:55:00 +01:00
|
|
|
actual := output.Stderr()
|
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)
|
2021-02-26 22:31:12 +01:00
|
|
|
}
|
|
|
|
if want := "9: count = timestamp()"; !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) {
|
2021-02-02 16:35:45 +01:00
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("plan-vars"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
2017-02-07 18:11:48 +01:00
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
p := planVarsFixtureProvider()
|
2021-02-22 17:55:00 +01:00
|
|
|
view, done := testView(t)
|
2014-07-18 20:37:27 +02:00
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2014-07-18 20:37:27 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := ""
|
2020-10-08 19:42:06 +02:00
|
|
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
|
|
|
|
actual = req.ProposedNewState.GetAttr("value").AsString()
|
|
|
|
resp.PlannedState = req.ProposedNewState
|
|
|
|
return
|
2014-07-18 20:37:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-var", "foo=bar",
|
|
|
|
}
|
2021-02-22 17:55:00 +01:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2014-07-18 20:37:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2021-02-02 16:35:45 +01:00
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("plan-vars"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
2017-02-07 18:11:48 +01:00
|
|
|
|
2019-10-08 21:08:27 +02:00
|
|
|
// The plan command will prompt for interactive input of var.foo.
|
|
|
|
// We'll answer "bar" to that prompt, which should then allow this
|
|
|
|
// configuration to apply even though var.foo doesn't have a
|
|
|
|
// default value and there are no -var arguments on our command line.
|
2020-08-31 21:45:39 +02:00
|
|
|
|
|
|
|
// This will (helpfully) panic if more than one variable is requested during plan:
|
|
|
|
// https://github.com/hashicorp/terraform/issues/26027
|
|
|
|
close := testInteractiveInput(t, []string{"bar"})
|
|
|
|
defer close()
|
2015-11-10 19:31:15 +01:00
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
p := planVarsFixtureProvider()
|
2021-02-22 17:55:00 +01:00
|
|
|
view, done := testView(t)
|
2015-11-10 19:31:15 +01:00
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2015-11-10 19:31:15 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-02-02 16:35:45 +01:00
|
|
|
args := []string{}
|
2021-02-22 17:55:00 +01:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2015-11-10 19:31:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 21:45:39 +02:00
|
|
|
// This test adds a required argument to the test provider to validate
|
|
|
|
// processing of user input:
|
|
|
|
// https://github.com/hashicorp/terraform/issues/26035
|
|
|
|
func TestPlan_providerArgumentUnset(t *testing.T) {
|
2021-02-02 16:35:45 +01:00
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("plan"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
2020-08-31 21:45:39 +02:00
|
|
|
|
|
|
|
// Disable test mode so input would be asked
|
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
|
|
|
|
// The plan command will prompt for interactive input of provider.test.region
|
|
|
|
defaultInputReader = bytes.NewBufferString("us-east-1\n")
|
|
|
|
|
|
|
|
p := planFixtureProvider()
|
|
|
|
// override the planFixtureProvider schema to include a required provider argument
|
2021-02-18 16:13:43 +01:00
|
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
2021-01-12 22:13:10 +01:00
|
|
|
Provider: providers.Schema{
|
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"region": {Type: cty.String, Required: true},
|
|
|
|
},
|
2020-08-31 21:45:39 +02:00
|
|
|
},
|
|
|
|
},
|
2021-01-12 22:13:10 +01:00
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2020-08-31 21:45:39 +02:00
|
|
|
"test_instance": {
|
2021-01-12 22:13:10 +01:00
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
|
|
"ami": {Type: cty.String, Optional: true, Computed: 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},
|
|
|
|
},
|
2020-08-31 21:45:39 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-02-22 17:55:00 +01:00
|
|
|
view, done := testView(t)
|
2020-08-31 21:45:39 +02:00
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2020-08-31 21:45:39 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-02-02 16:35:45 +01:00
|
|
|
args := []string{}
|
2021-02-22 17:55:00 +01:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2020-08-31 21:45:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-18 23:00:40 +02:00
|
|
|
func TestPlan_varFile(t *testing.T) {
|
2021-02-02 16:35:45 +01:00
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("plan-vars"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
2017-02-07 18:11:48 +01:00
|
|
|
|
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()
|
2021-02-22 17:55:00 +01:00
|
|
|
view, done := testView(t)
|
2014-07-18 23:00:40 +02:00
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2014-07-18 23:00:40 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := ""
|
2020-10-08 19:42:06 +02:00
|
|
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
|
|
|
|
actual = req.ProposedNewState.GetAttr("value").AsString()
|
|
|
|
resp.PlannedState = req.ProposedNewState
|
|
|
|
return
|
2014-07-18 23:00:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-var-file", varFilePath,
|
|
|
|
}
|
2021-02-22 17:55:00 +01:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2014-07-18 23:00:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if actual != "bar" {
|
|
|
|
t.Fatal("didn't work")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-05 18:32:01 +02:00
|
|
|
func TestPlan_varFileDefault(t *testing.T) {
|
2021-02-02 16:35:45 +01:00
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("plan-vars"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
2014-08-05 18:32:01 +02:00
|
|
|
|
2021-02-02 16:35:45 +01:00
|
|
|
varFilePath := filepath.Join(td, "terraform.tfvars")
|
|
|
|
if err := ioutil.WriteFile(varFilePath, []byte(planVarFile), 0644); err != nil {
|
2014-08-05 18:32:01 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
p := planVarsFixtureProvider()
|
2021-02-22 17:55:00 +01:00
|
|
|
view, done := testView(t)
|
2014-08-05 18:32:01 +02:00
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2014-08-05 18:32:01 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := ""
|
2020-10-08 19:42:06 +02:00
|
|
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
|
|
|
|
actual = req.ProposedNewState.GetAttr("value").AsString()
|
|
|
|
resp.PlannedState = req.ProposedNewState
|
|
|
|
return
|
2014-08-05 18:32:01 +02:00
|
|
|
}
|
|
|
|
|
2021-02-02 16:35:45 +01:00
|
|
|
args := []string{}
|
2021-02-22 17:55:00 +01:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2014-08-05 18:32:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if actual != "bar" {
|
|
|
|
t.Fatal("didn't work")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 02:51:48 +01:00
|
|
|
func TestPlan_varFileWithDecls(t *testing.T) {
|
2021-02-02 16:35:45 +01:00
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("plan-vars"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
2019-02-23 02:51:48 +01:00
|
|
|
|
|
|
|
varFilePath := testTempFile(t)
|
|
|
|
if err := ioutil.WriteFile(varFilePath, []byte(planVarFileWithDecl), 0644); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p := planVarsFixtureProvider()
|
2021-02-22 17:55:00 +01:00
|
|
|
view, done := testView(t)
|
2019-02-23 02:51:48 +01:00
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2019-02-23 02:51:48 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-var-file", varFilePath,
|
|
|
|
}
|
2021-02-22 17:55:00 +01:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code == 0 {
|
|
|
|
t.Fatalf("succeeded; want failure\n\n%s", output.Stdout())
|
2019-02-23 02:51:48 +01:00
|
|
|
}
|
|
|
|
|
2021-02-22 17:55:00 +01:00
|
|
|
msg := output.Stderr()
|
2019-02-23 02:51:48 +01:00
|
|
|
if got, want := msg, "Variable declaration in .tfvars file"; !strings.Contains(got, want) {
|
|
|
|
t.Fatalf("missing expected error message\nwant message containing %q\ngot:\n%s", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-01 18:38:19 +02:00
|
|
|
func TestPlan_detailedExitcode(t *testing.T) {
|
2020-06-18 23:09:20 +02:00
|
|
|
td := tempDir(t)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("plan"), td)
|
2020-06-18 23:09:20 +02:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
2015-04-01 18:38:19 +02:00
|
|
|
|
2021-05-05 20:13:20 +02:00
|
|
|
t.Run("return 1", func(t *testing.T) {
|
|
|
|
view, done := testView(t)
|
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
// Running plan without setting testingOverrides is similar to plan without init
|
|
|
|
View: view,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
code := c.Run([]string{"-detailed-exitcode"})
|
|
|
|
output := done(t)
|
|
|
|
if code != 1 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
|
|
|
}
|
|
|
|
})
|
2015-04-01 18:38:19 +02:00
|
|
|
|
2021-05-05 20:13:20 +02:00
|
|
|
t.Run("return 2", func(t *testing.T) {
|
|
|
|
p := planFixtureProvider()
|
|
|
|
view, done := testView(t)
|
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
View: view,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
code := c.Run([]string{"-detailed-exitcode"})
|
|
|
|
output := done(t)
|
|
|
|
if code != 2 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
|
|
|
}
|
|
|
|
})
|
2015-04-01 18:38:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPlan_detailedExitcode_emptyDiff(t *testing.T) {
|
2020-06-18 23:09:20 +02:00
|
|
|
td := tempDir(t)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("plan-emptydiff"), td)
|
2020-06-18 23:09:20 +02:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
2015-04-01 18:38:19 +02:00
|
|
|
|
|
|
|
p := testProvider()
|
2021-02-22 17:55:00 +01:00
|
|
|
view, done := testView(t)
|
2015-04-01 18:38:19 +02:00
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2015-04-01 18:38:19 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"-detailed-exitcode"}
|
2021-02-22 17:55:00 +01:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2015-04-01 18:38:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-01 17:27:32 +01:00
|
|
|
func TestPlan_shutdown(t *testing.T) {
|
2021-02-02 16:35:45 +01:00
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("apply-shutdown"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
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()
|
2021-02-22 17:55:00 +01:00
|
|
|
view, done := testView(t)
|
2017-12-01 17:27:32 +01:00
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2017-12-01 17:27:32 +01:00
|
|
|
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
|
|
|
|
|
2020-10-08 19:42:06 +02:00
|
|
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
|
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)
|
|
|
|
|
2020-10-08 19:42:06 +02:00
|
|
|
s := req.ProposedNewState.AsValueMap()
|
|
|
|
s["ami"] = cty.StringVal("bar")
|
|
|
|
resp.PlannedState = cty.ObjectVal(s)
|
|
|
|
return
|
2017-12-01 17:27:32 +01:00
|
|
|
}
|
2020-10-08 19:42:06 +02:00
|
|
|
|
2021-02-18 16:13:43 +01:00
|
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
2021-01-12 22:13:10 +01:00
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2018-10-14 18:21:31 +02:00
|
|
|
"test_instance": {
|
2021-01-12 22:13:10 +01:00
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"ami": {Type: cty.String, Optional: true},
|
|
|
|
},
|
2018-10-14 18:21:31 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-12-01 17:27:32 +01:00
|
|
|
|
2021-02-02 16:35:45 +01:00
|
|
|
code := c.Run([]string{})
|
2021-02-22 17:55:00 +01:00
|
|
|
output := done(t)
|
2020-10-28 19:33:27 +01:00
|
|
|
if code != 1 {
|
2021-02-22 17:55:00 +01:00
|
|
|
t.Errorf("wrong exit code %d; want 1\noutput:\n%s", code, output.Stdout())
|
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:
|
2018-11-07 23:48:55 +01:00
|
|
|
t.Error("command not cancelled")
|
2017-12-01 17:27:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 22:29:27 +02:00
|
|
|
func TestPlan_init_required(t *testing.T) {
|
2020-06-18 23:09:20 +02:00
|
|
|
td := tempDir(t)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("plan"), td)
|
2020-06-18 23:09:20 +02:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
2020-04-21 22:29:27 +02:00
|
|
|
|
2021-02-22 17:55:00 +01:00
|
|
|
view, done := testView(t)
|
2020-04-21 22:29:27 +02:00
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
// Running plan without setting testingOverrides is similar to plan without init
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2020-04-21 22:29:27 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-22 21:38:56 +01:00
|
|
|
args := []string{"-no-color"}
|
2021-02-22 17:55:00 +01:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 1 {
|
2020-04-21 22:29:27 +02:00
|
|
|
t.Fatalf("expected error, got success")
|
|
|
|
}
|
2021-02-22 17:55:00 +01:00
|
|
|
got := output.Stderr()
|
2021-03-22 18:17:50 +01:00
|
|
|
if !strings.Contains(got, `Error: Could not load plugin`) {
|
2021-02-22 17:55:00 +01:00
|
|
|
t.Fatal("wrong error message in output:", got)
|
2020-04-21 22:29:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 19:29:42 +01:00
|
|
|
// Config with multiple resources, targeting plan of a subset
|
|
|
|
func TestPlan_targeted(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("apply-targeted"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
p := testProvider()
|
2021-02-18 16:13:43 +01:00
|
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
2021-02-08 19:29:42 +01:00
|
|
|
ResourceTypes: map[string]providers.Schema{
|
|
|
|
"test_instance": {
|
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Computed: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
|
|
|
|
return providers.PlanResourceChangeResponse{
|
|
|
|
PlannedState: req.ProposedNewState,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
view, done := testView(t)
|
2021-02-08 19:29:42 +01:00
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2021-02-08 19:29:42 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-target", "test_instance.foo",
|
|
|
|
"-target", "test_instance.baz",
|
|
|
|
}
|
2021-02-22 17:55:00 +01:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2021-02-08 19:29:42 +01:00
|
|
|
}
|
|
|
|
|
2021-02-22 17:55:00 +01:00
|
|
|
if got, want := output.Stdout(), "3 to add, 0 to change, 0 to destroy"; !strings.Contains(got, want) {
|
2021-02-08 19:29:42 +01:00
|
|
|
t.Fatalf("bad change summary, want %q, got:\n%s", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Diagnostics for invalid -target flags
|
|
|
|
func TestPlan_targetFlagsDiags(t *testing.T) {
|
|
|
|
testCases := map[string]string{
|
|
|
|
"test_instance.": "Dot must be followed by attribute name.",
|
|
|
|
"test_instance": "Resource specification must include a resource type and name.",
|
|
|
|
}
|
|
|
|
|
|
|
|
for target, wantDiag := range testCases {
|
|
|
|
t.Run(target, func(t *testing.T) {
|
|
|
|
td := testTempDir(t)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2021-02-22 17:55:00 +01:00
|
|
|
view, done := testView(t)
|
2021-02-08 19:29:42 +01:00
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
2021-02-12 02:52:10 +01:00
|
|
|
View: view,
|
2021-02-08 19:29:42 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-target", target,
|
|
|
|
}
|
2021-02-22 17:55:00 +01:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 1 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stdout())
|
2021-02-08 19:29:42 +01:00
|
|
|
}
|
|
|
|
|
2021-02-22 17:55:00 +01:00
|
|
|
got := output.Stderr()
|
2021-02-08 19:29:42 +01:00
|
|
|
if !strings.Contains(got, target) {
|
|
|
|
t.Fatalf("bad error output, want %q, got:\n%s", target, got)
|
|
|
|
}
|
|
|
|
if !strings.Contains(got, wantDiag) {
|
|
|
|
t.Fatalf("bad error output, want %q, got:\n%s", wantDiag, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-30 23:46:22 +02:00
|
|
|
func TestPlan_replace(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("plan-replace"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
originalState := states.BuildState(func(s *states.SyncState) {
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "a",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"hello"}`),
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
},
|
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
|
|
Module: addrs.RootModule,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
statePath := testStateFile(t, originalState)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
|
|
|
ResourceTypes: map[string]providers.Schema{
|
|
|
|
"test_instance": {
|
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Computed: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
|
|
|
|
return providers.PlanResourceChangeResponse{
|
|
|
|
PlannedState: req.ProposedNewState,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
view, done := testView(t)
|
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
View: view,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-no-color",
|
|
|
|
"-replace", "test_instance.a",
|
|
|
|
}
|
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("wrong exit code %d\n\n%s", code, output.Stderr())
|
|
|
|
}
|
|
|
|
|
|
|
|
stdout := output.Stdout()
|
|
|
|
if got, want := stdout, "1 to add, 0 to change, 1 to destroy"; !strings.Contains(got, want) {
|
|
|
|
t.Errorf("wrong plan summary\ngot output:\n%s\n\nwant substring: %s", got, want)
|
|
|
|
}
|
|
|
|
if got, want := stdout, "test_instance.a will be replaced, as requested"; !strings.Contains(got, want) {
|
|
|
|
t.Errorf("missing replace explanation\ngot output:\n%s\n\nwant substring: %s", got, want)
|
|
|
|
}
|
2021-05-05 20:13:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the parallelism flag allows no more than the desired number of
|
|
|
|
// concurrent calls to PlanResourceChange.
|
|
|
|
func TestPlan_parallelism(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("parallelism"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
2021-04-30 23:46:22 +02:00
|
|
|
|
2021-05-05 20:13:20 +02:00
|
|
|
par := 4
|
|
|
|
|
|
|
|
// started is a semaphore that we use to ensure that we never have more
|
|
|
|
// than "par" plan operations happening concurrently
|
|
|
|
started := make(chan struct{}, par)
|
|
|
|
|
|
|
|
// beginCtx is used as a starting gate to hold back PlanResourceChange
|
|
|
|
// calls until we reach the desired concurrency. The cancel func "begin" is
|
|
|
|
// called once we reach the desired concurrency, allowing all apply calls
|
|
|
|
// to proceed in unison.
|
|
|
|
beginCtx, begin := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
// Since our mock provider has its own mutex preventing concurrent calls
|
|
|
|
// to ApplyResourceChange, we need to use a number of separate providers
|
|
|
|
// here. They will all have the same mock implementation function assigned
|
|
|
|
// but crucially they will each have their own mutex.
|
|
|
|
providerFactories := map[addrs.Provider]providers.Factory{}
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
name := fmt.Sprintf("test%d", i)
|
|
|
|
provider := &terraform.MockProvider{}
|
|
|
|
provider.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
|
|
|
ResourceTypes: map[string]providers.Schema{
|
|
|
|
name + "_instance": {Block: &configschema.Block{}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
provider.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
|
|
|
|
// If we ever have more than our intended parallelism number of
|
|
|
|
// plan operations running concurrently, the semaphore will fail.
|
|
|
|
select {
|
|
|
|
case started <- struct{}{}:
|
|
|
|
defer func() {
|
|
|
|
<-started
|
|
|
|
}()
|
|
|
|
default:
|
|
|
|
t.Fatal("too many concurrent apply operations")
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we never reach our intended parallelism, the context will
|
|
|
|
// never be canceled and the test will time out.
|
|
|
|
if len(started) >= par {
|
|
|
|
begin()
|
|
|
|
}
|
|
|
|
<-beginCtx.Done()
|
|
|
|
|
|
|
|
// do some "work"
|
|
|
|
// Not required for correctness, but makes it easier to spot a
|
|
|
|
// failure when there is more overlap.
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
return providers.PlanResourceChangeResponse{
|
|
|
|
PlannedState: req.ProposedNewState,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
providerFactories[addrs.NewDefaultProvider(name)] = providers.FactoryFixed(provider)
|
|
|
|
}
|
|
|
|
testingOverrides := &testingOverrides{
|
|
|
|
Providers: providerFactories,
|
|
|
|
}
|
|
|
|
|
|
|
|
view, done := testView(t)
|
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: testingOverrides,
|
|
|
|
View: view,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
fmt.Sprintf("-parallelism=%d", par),
|
|
|
|
}
|
|
|
|
|
|
|
|
res := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if res != 0 {
|
|
|
|
t.Fatal(output.Stdout())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPlan_warnings(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("plan"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
t.Run("full warnings", func(t *testing.T) {
|
|
|
|
p := planWarningsFixtureProvider()
|
|
|
|
view, done := testView(t)
|
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
View: view,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
code := c.Run([]string{})
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
|
|
|
}
|
|
|
|
// the output should contain 3 warnings (returned by planWarningsFixtureProvider())
|
|
|
|
wantWarnings := []string{
|
|
|
|
"warning 1",
|
|
|
|
"warning 2",
|
|
|
|
"warning 3",
|
|
|
|
}
|
|
|
|
for _, want := range wantWarnings {
|
|
|
|
if !strings.Contains(output.Stdout(), want) {
|
|
|
|
t.Errorf("missing warning %s", want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("compact warnings", func(t *testing.T) {
|
|
|
|
p := planWarningsFixtureProvider()
|
|
|
|
view, done := testView(t)
|
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
View: view,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
code := c.Run([]string{"-compact-warnings"})
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
|
|
|
}
|
|
|
|
// the output should contain 3 warnings (returned by planWarningsFixtureProvider())
|
|
|
|
// and the message that plan was run with -compact-warnings
|
|
|
|
wantWarnings := []string{
|
|
|
|
"warning 1",
|
|
|
|
"warning 2",
|
|
|
|
"warning 3",
|
|
|
|
"To see the full warning notes, run Terraform without -compact-warnings.",
|
|
|
|
}
|
|
|
|
for _, want := range wantWarnings {
|
|
|
|
if !strings.Contains(output.Stdout(), want) {
|
|
|
|
t.Errorf("missing warning %s", want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-04-30 23:46:22 +02:00
|
|
|
}
|
|
|
|
|
2018-10-14 17:29:52 +02:00
|
|
|
// planFixtureSchema returns a schema suitable for processing the
|
2019-06-30 09:38:36 +02:00
|
|
|
// configuration in testdata/plan . This schema should be
|
2018-10-14 17:29:52 +02:00
|
|
|
// assigned to a mock provider named "test".
|
2021-02-18 16:13:43 +01:00
|
|
|
func planFixtureSchema() *providers.GetProviderSchemaResponse {
|
|
|
|
return &providers.GetProviderSchemaResponse{
|
2021-01-12 22:13:10 +01:00
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2018-10-14 17:29:52 +02:00
|
|
|
"test_instance": {
|
2021-01-12 22:13:10 +01:00
|
|
|
Block: &configschema.Block{
|
|
|
|
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},
|
|
|
|
},
|
2018-10-14 17:29:52 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// planFixtureProvider returns a mock provider that is configured for basic
|
2019-06-30 09:38:36 +02:00
|
|
|
// operation with the configuration in testdata/plan. This mock has
|
2021-01-12 22:13:10 +01:00
|
|
|
// GetSchemaResponse and PlanResourceChangeFn populated, with the plan
|
2018-10-14 17:29:52 +02:00
|
|
|
// step just passing through the new object proposed by Terraform Core.
|
|
|
|
func planFixtureProvider() *terraform.MockProvider {
|
|
|
|
p := testProvider()
|
2021-02-18 16:13:43 +01:00
|
|
|
p.GetProviderSchemaResponse = planFixtureSchema()
|
2018-10-14 17:29:52 +02:00
|
|
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
|
|
|
|
return providers.PlanResourceChangeResponse{
|
|
|
|
PlannedState: req.ProposedNewState,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// planVarsFixtureSchema returns a schema suitable for processing the
|
2019-06-30 09:38:36 +02:00
|
|
|
// configuration in testdata/plan-vars . This schema should be
|
2018-10-14 17:29:52 +02:00
|
|
|
// assigned to a mock provider named "test".
|
2021-02-18 16:13:43 +01:00
|
|
|
func planVarsFixtureSchema() *providers.GetProviderSchemaResponse {
|
|
|
|
return &providers.GetProviderSchemaResponse{
|
2021-01-12 22:13:10 +01:00
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2018-10-14 17:29:52 +02:00
|
|
|
"test_instance": {
|
2021-01-12 22:13:10 +01:00
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
|
|
"value": {Type: cty.String, Optional: true},
|
|
|
|
},
|
2018-10-14 17:29:52 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// planVarsFixtureProvider returns a mock provider that is configured for basic
|
2019-06-30 09:38:36 +02:00
|
|
|
// operation with the configuration in testdata/plan-vars. This mock has
|
2021-01-12 22:13:10 +01:00
|
|
|
// GetSchemaResponse and PlanResourceChangeFn populated, with the plan
|
2018-10-14 17:29:52 +02:00
|
|
|
// step just passing through the new object proposed by Terraform Core.
|
|
|
|
func planVarsFixtureProvider() *terraform.MockProvider {
|
|
|
|
p := testProvider()
|
2021-02-18 16:13:43 +01:00
|
|
|
p.GetProviderSchemaResponse = planVarsFixtureSchema()
|
2018-10-14 17:29:52 +02:00
|
|
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
|
|
|
|
return providers.PlanResourceChangeResponse{
|
|
|
|
PlannedState: req.ProposedNewState,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2021-05-05 20:13:20 +02:00
|
|
|
// planFixtureProvider returns a mock provider that is configured for basic
|
|
|
|
// operation with the configuration in testdata/plan. This mock has
|
|
|
|
// GetSchemaResponse and PlanResourceChangeFn populated, returning 3 warnings.
|
|
|
|
func planWarningsFixtureProvider() *terraform.MockProvider {
|
|
|
|
p := testProvider()
|
|
|
|
p.GetProviderSchemaResponse = planFixtureSchema()
|
|
|
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
|
|
|
|
return providers.PlanResourceChangeResponse{
|
|
|
|
Diagnostics: tfdiags.Diagnostics{
|
|
|
|
tfdiags.SimpleWarning("warning 1"),
|
|
|
|
tfdiags.SimpleWarning("warning 2"),
|
|
|
|
tfdiags.SimpleWarning("warning 3"),
|
|
|
|
},
|
|
|
|
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
|
|
|
|
2019-02-23 02:51:48 +01:00
|
|
|
const planVarFileWithDecl = `
|
|
|
|
foo = "bar"
|
|
|
|
|
|
|
|
variable "nope" {
|
|
|
|
}
|
|
|
|
`
|