terraform/internal/command/plan_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

2014 lines
53 KiB
Go
Raw Permalink Normal View History

// Copyright IBM Corp. 2014, 2026
// SPDX-License-Identifier: BUSL-1.1
2014-06-19 16:51:05 -04:00
package command
import (
"bytes"
"context"
"fmt"
2014-06-19 16:51:05 -04:00
"io/ioutil"
"os"
"path"
2014-07-12 00:03:56 -04:00
"path/filepath"
2014-09-18 13:40:23 -04:00
"strings"
"sync"
2014-06-19 16:51:05 -04:00
"testing"
"time"
2014-06-19 16:51:05 -04:00
"github.com/davecgh/go-spew/spew"
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/terraform/internal/addrs"
backendinit "github.com/hashicorp/terraform/internal/backend/init"
2022-12-14 16:23:31 -05:00
"github.com/hashicorp/terraform/internal/checks"
PSS: Allow pluggable state store configuration to be stored in a plan file (#37956) * refactor: Rename Meta's backendState field to backendConfigState This helps with navigating ambiguity around the word backend. The new name should indicate that the value represents a `backend` block, not a more general interpretation of what a backend is. * fix: Only set backendConfigState to synthetic object if it's nil due to a lack of data. Don't change it if pluggable state storage is in use. * feat: Enable recording a state store's details in an Operation, and using that data when creating a plan file. * fix: Include provider config when writing a plan file using pluggable state storage * fix: Having `backendConfigState` be nil may be valid, but it definitely isn't valid for `stateStoreConfigState` to be nil When backendConfigState is nil it means that an implied local backend is in use, i.e. there is no backend block in the config. * test: Add integration test showing that a plan command creates a plan file with the expected state_store configuration data * refactor: Apply suggestion from @radeksimko Co-authored-by: Radek Simko <radeksimko@users.noreply.github.com> * fix: Allow panics to occur if an unexpected implementation of `backend.Backend` is encountered when managing a state store * docs: Add code comment explaining the current situation with passing backend config state to downstream logic. In future this should be simplified, either via refactoring or changes affecting the implied local backend --------- Co-authored-by: Radek Simko <radeksimko@users.noreply.github.com>
2025-12-11 06:41:36 -05:00
"github.com/hashicorp/terraform/internal/command/clistate"
"github.com/hashicorp/terraform/internal/configs/configschema"
"github.com/hashicorp/terraform/internal/plans"
"github.com/hashicorp/terraform/internal/providers"
testing_provider "github.com/hashicorp/terraform/internal/providers/testing"
"github.com/hashicorp/terraform/internal/states"
PSS: Allow pluggable state store configuration to be stored in a plan file (#37956) * refactor: Rename Meta's backendState field to backendConfigState This helps with navigating ambiguity around the word backend. The new name should indicate that the value represents a `backend` block, not a more general interpretation of what a backend is. * fix: Only set backendConfigState to synthetic object if it's nil due to a lack of data. Don't change it if pluggable state storage is in use. * feat: Enable recording a state store's details in an Operation, and using that data when creating a plan file. * fix: Include provider config when writing a plan file using pluggable state storage * fix: Having `backendConfigState` be nil may be valid, but it definitely isn't valid for `stateStoreConfigState` to be nil When backendConfigState is nil it means that an implied local backend is in use, i.e. there is no backend block in the config. * test: Add integration test showing that a plan command creates a plan file with the expected state_store configuration data * refactor: Apply suggestion from @radeksimko Co-authored-by: Radek Simko <radeksimko@users.noreply.github.com> * fix: Allow panics to occur if an unexpected implementation of `backend.Backend` is encountered when managing a state store * docs: Add code comment explaining the current situation with passing backend config state to downstream logic. In future this should be simplified, either via refactoring or changes affecting the implied local backend --------- Co-authored-by: Radek Simko <radeksimko@users.noreply.github.com>
2025-12-11 06:41:36 -05:00
"github.com/hashicorp/terraform/internal/states/statefile"
"github.com/hashicorp/terraform/internal/tfdiags"
2014-06-19 16:51:05 -04:00
)
func TestPlan(t *testing.T) {
td := t.TempDir()
testCopyDir(t, testFixturePath("plan"), td)
t.Chdir(td)
p := planFixtureProvider()
2021-02-22 11:55:00 -05:00
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{}
2021-02-22 11:55:00 -05:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
}
2017-02-03 16:02:22 -05:00
func TestPlan_lockedState(t *testing.T) {
td := t.TempDir()
testCopyDir(t, testFixturePath("plan"), td)
t.Chdir(td)
2017-02-03 16:02:22 -05:00
unlock, err := testLockState(t, testDataDir, filepath.Join(td, DefaultStateFilename))
2017-02-03 16:02:22 -05:00
if err != nil {
t.Fatal(err)
}
defer unlock()
p := planFixtureProvider()
2021-02-22 11:55:00 -05:00
view, done := testView(t)
2017-02-03 16:02:22 -05:00
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
2017-02-03 16:02:22 -05:00
},
}
args := []string{}
2021-02-22 11:55:00 -05:00
code := c.Run(args)
if code == 0 {
t.Fatal("expected error", done(t).Stdout())
2017-02-03 16:02:22 -05:00
}
2021-02-22 11:55:00 -05:00
output := done(t).Stderr()
if !strings.Contains(output, "lock") {
2017-02-03 16:02:22 -05:00
t.Fatal("command output does not look like a lock error:", output)
}
}
func TestPlan_plan(t *testing.T) {
tmp := t.TempDir()
t.Chdir(tmp)
planPath := testPlanFileNoop(t)
p := testProvider()
2021-02-22 11:55:00 -05:00
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{planPath}
2021-02-22 11:55:00 -05:00
code := c.Run(args)
output := done(t)
if code != 1 {
t.Fatalf("wrong exit status %d; want 1\nstderr: %s", code, output.Stderr())
}
}
func TestPlan_destroy(t *testing.T) {
td := t.TempDir()
testCopyDir(t, testFixturePath("plan"), td)
t.Chdir(td)
originalState := states.BuildState(func(s *states.SyncState) {
s.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_instance",
Name: "foo",
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
&states.ResourceInstanceObjectSrc{
AttrsJSON: []byte(`{"id":"bar"}`),
Status: states.ObjectReady,
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
})
outPath := testTempFile(t)
statePath := testStateFile(t, originalState)
p := planFixtureProvider()
2021-02-22 11:55:00 -05:00
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{
"-destroy",
"-out", outPath,
"-state", statePath,
}
2021-02-22 11:55:00 -05:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
plan := testReadPlan(t, outPath)
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-09-18 20:16:09 -04:00
2014-06-20 14:47:02 -04:00
func TestPlan_noState(t *testing.T) {
td := t.TempDir()
testCopyDir(t, testFixturePath("plan"), td)
t.Chdir(td)
p := planFixtureProvider()
2021-02-22 11:55:00 -05:00
view, done := testView(t)
2014-06-20 14:47:02 -04:00
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
2014-06-19 16:51:05 -04:00
}
args := []string{}
2021-02-22 11:55:00 -05:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
2014-06-19 16:51:05 -04:00
}
2014-06-26 12:56:29 -04:00
// Verify that refresh was called
if p.ReadResourceCalled {
t.Fatal("ReadResource should not be called")
2014-06-26 12:56:29 -04:00
}
2014-06-19 16:51:05 -04:00
// Verify that the provider was called with the existing state
actual := p.PlanResourceChangeRequest.PriorState
2025-03-04 10:33:43 -05:00
expected := cty.NullVal(p.GetProviderSchemaResponse.ResourceTypes["test_instance"].Body.ImpliedType())
if !expected.RawEquals(actual) {
t.Fatalf("wrong prior state\ngot: %#v\nwant: %#v", actual, expected)
2014-06-19 16:51:05 -04:00
}
}
2014-06-19 16:51:28 -04:00
func TestPlan_generatedConfigPath(t *testing.T) {
td := t.TempDir()
testCopyDir(t, testFixturePath("plan-import-config-gen"), td)
t.Chdir(td)
genPath := filepath.Join(td, "generated.tf")
p := planFixtureProvider()
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{
{
TypeName: "test_instance",
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("bar"),
}),
Private: nil,
},
},
}
args := []string{
"-generate-config-out", genPath,
}
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
testFileEquals(t, genPath, filepath.Join(td, "generated.tf.expected"))
}
2014-06-27 01:23:51 -04:00
func TestPlan_outPath(t *testing.T) {
td := t.TempDir()
testCopyDir(t, testFixturePath("plan"), td)
t.Chdir(td)
outPath := filepath.Join(td, "test.plan")
2014-06-27 01:23:51 -04:00
p := planFixtureProvider()
2021-02-22 11:55:00 -05:00
view, done := testView(t)
2014-06-27 01:23:51 -04:00
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
2014-06-27 01:23:51 -04:00
}
2021-01-12 16:13:10 -05:00
p.PlanResourceChangeResponse = &providers.PlanResourceChangeResponse{
PlannedState: cty.NullVal(cty.EmptyObject),
2014-06-27 01:23:51 -04:00
}
args := []string{
"-out", outPath,
}
2021-02-22 11:55:00 -05:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
2014-06-27 01:23:51 -04:00
}
testReadPlan(t, outPath) // will call t.Fatal itself if the file cannot be read
2014-06-27 01:23:51 -04:00
}
func TestPlan_outPathNoChange(t *testing.T) {
td := t.TempDir()
testCopyDir(t, testFixturePath("plan"), td)
t.Chdir(td)
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{
// 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"}]}`),
Status: states.ObjectReady,
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
})
statePath := testStateFile(t, originalState)
outPath := filepath.Join(td, "test.plan")
p := planFixtureProvider()
2021-02-22 11:55:00 -05:00
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{
"-out", outPath,
"-state", statePath,
}
2021-02-22 11:55:00 -05:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
plan := testReadPlan(t, outPath)
if !plan.Changes.Empty() {
t.Fatalf("Expected empty plan to be written to plan file, got: %s", spew.Sdump(plan))
}
}
2022-12-14 16:23:31 -05:00
func TestPlan_outPathWithError(t *testing.T) {
td := t.TempDir()
testCopyDir(t, testFixturePath("plan-fail-condition"), td)
t.Chdir(td)
2022-12-14 16:23:31 -05:00
outPath := filepath.Join(td, "test.plan")
p := planFixtureProvider()
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
p.PlanResourceChangeResponse = &providers.PlanResourceChangeResponse{
PlannedState: cty.NullVal(cty.EmptyObject),
}
args := []string{
"-out", outPath,
}
code := c.Run(args)
output := done(t)
if code == 0 {
t.Fatal("expected non-zero exit status", output)
}
plan := testReadPlan(t, outPath) // will call t.Fatal itself if the file cannot be read
if !plan.Errored {
t.Fatal("plan should be marked with Errored")
}
if plan.Checks == nil {
t.Fatal("plan contains no checks")
}
// the checks should only contain one failure
results := plan.Checks.ConfigResults.Elements()
if len(results) != 1 {
t.Fatal("incorrect number of check results", len(results))
}
if results[0].Value.Status != checks.StatusFail {
t.Errorf("incorrect status, got %s", results[0].Value.Status)
}
}
2017-01-18 23:50:45 -05: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 := t.TempDir()
testCopyDir(t, testFixturePath("plan-out-backend"), td)
t.Chdir(td)
2017-01-18 23:50:45 -05: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-18 23:50:45 -05:00
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
})
2017-01-18 23:50:45 -05:00
// Set up our backend state
2017-01-18 23:50:45 -05:00
dataState, srv := testBackendState(t, originalState, 200)
defer srv.Close()
testStateFileRemote(t, dataState)
outPath := "foo"
p := testProvider()
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
2021-01-12 16:13:10 -05:00
ResourceTypes: map[string]providers.Schema{
"test_instance": {
2025-03-04 10:33:43 -05:00
Body: &configschema.Block{
2021-01-12 16:13:10 -05:00
Attributes: map[string]*configschema.Attribute{
"id": {
Type: cty.String,
Computed: true,
},
"ami": {
Type: cty.String,
Optional: true,
},
},
},
},
},
}
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
return providers.PlanResourceChangeResponse{
PlannedState: req.ProposedNewState,
}
}
2021-02-22 11:55:00 -05:00
view, done := testView(t)
2017-01-18 23:50:45 -05:00
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
2017-01-18 23:50:45 -05:00
},
}
args := []string{
"-out", outPath,
}
2021-02-22 11:55:00 -05: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-18 23:50:45 -05:00
}
plan := testReadPlan(t, outPath)
if !plan.Changes.Empty() {
t.Fatalf("Expected empty plan to be written to plan file, got: %s", spew.Sdump(plan))
2017-01-18 23:50:45 -05:00
}
if plan.Backend == nil {
t.Fatal("unexpected nil Backend")
}
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-18 23:50:45 -05: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-18 23:50:45 -05:00
}
}
// When using "-out" with a backend, the plan should encode the backend config
// and also the selected workspace, if workspaces are supported by the backend.
//
// This test demonstrates that setting the workspace in the backend plan
// responds to the selected workspace, versus other tests that show the same process
// when defaulting to the default workspace when there's a lack of information about
// the selected workspace.
//
// To test planning with a non-default workspace we need to use a backend that supports
// workspaces. In this test the `inmem` backend is used.
func TestPlan_outBackend_withWorkspace(t *testing.T) {
// Create a temporary working directory
td := t.TempDir()
testCopyDir(t, testFixturePath("plan-out-backend-workspace"), td)
t.Chdir(td)
// These values are coupled with the test fixture used above.
expectedBackendType := "inmem"
expectedWorkspace := "custom-workspace"
outPath := "foo"
p := testProvider()
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Body: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {
Type: cty.String,
Computed: true,
},
"ami": {
Type: cty.String,
Optional: 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{
"-out", outPath,
}
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())
}
plan := testReadPlan(t, outPath)
if got, want := plan.Backend.Type, expectedBackendType; got != want {
t.Errorf("wrong backend type %q; want %q", got, want)
}
if got, want := plan.Backend.Workspace, expectedWorkspace; got != want {
t.Errorf("wrong backend workspace %q; want %q", got, want)
}
}
PSS: Allow pluggable state store configuration to be stored in a plan file (#37956) * refactor: Rename Meta's backendState field to backendConfigState This helps with navigating ambiguity around the word backend. The new name should indicate that the value represents a `backend` block, not a more general interpretation of what a backend is. * fix: Only set backendConfigState to synthetic object if it's nil due to a lack of data. Don't change it if pluggable state storage is in use. * feat: Enable recording a state store's details in an Operation, and using that data when creating a plan file. * fix: Include provider config when writing a plan file using pluggable state storage * fix: Having `backendConfigState` be nil may be valid, but it definitely isn't valid for `stateStoreConfigState` to be nil When backendConfigState is nil it means that an implied local backend is in use, i.e. there is no backend block in the config. * test: Add integration test showing that a plan command creates a plan file with the expected state_store configuration data * refactor: Apply suggestion from @radeksimko Co-authored-by: Radek Simko <radeksimko@users.noreply.github.com> * fix: Allow panics to occur if an unexpected implementation of `backend.Backend` is encountered when managing a state store * docs: Add code comment explaining the current situation with passing backend config state to downstream logic. In future this should be simplified, either via refactoring or changes affecting the implied local backend --------- Co-authored-by: Radek Simko <radeksimko@users.noreply.github.com>
2025-12-11 06:41:36 -05:00
// When using "-out" with a state store, the plan should encode the state store config
func TestPlan_outStateStore(t *testing.T) {
// Create a temporary working directory with state_store config
td := t.TempDir()
testCopyDir(t, testFixturePath("plan-out-state-store"), td)
t.Chdir(td)
// Make state that resembles the resource defined in the test fixture
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,
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
})
var stateBuf bytes.Buffer
if err := statefile.Write(statefile.New(originalState, "", 1), &stateBuf); err != nil {
t.Fatalf("error during test setup: %s", err)
}
stateBytes := stateBuf.Bytes()
// Make a mock provider that:
// 1) will return the state defined above.
// 2) has a schema for the resource being managed in this test.
mock := mockPluggableStateStorageProvider()
mock.MockStates = map[string]interface{}{
"default": stateBytes,
}
mock.GetProviderSchemaResponse.ResourceTypes = map[string]providers.Schema{
"test_instance": {
Body: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {
Type: cty.String,
Computed: true,
},
"ami": {
Type: cty.String,
Optional: true,
},
},
},
},
}
mock.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
return providers.PlanResourceChangeResponse{
PlannedState: req.ProposedNewState,
}
}
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
AllowExperimentalFeatures: true,
testingOverrides: metaOverridesForProvider(mock),
View: view,
},
}
outPath := "foo"
args := []string{
"-out", outPath,
"-no-color",
}
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())
}
plan := testReadPlan(t, outPath)
if !plan.Changes.Empty() {
t.Fatalf("Expected empty plan to be written to plan file, got: %s", spew.Sdump(plan))
}
if plan.Backend != nil {
t.Errorf("expected the plan file to not describe a backend, but got %#v", plan.Backend)
}
if plan.StateStore == nil {
t.Errorf("expected the plan file to describe a state store, but it's empty: %#v", plan.StateStore)
}
if got, want := plan.StateStore.Workspace, "default"; got != want {
t.Errorf("wrong workspace %q; want %q", got, want)
}
{
// Comparing the plan's description of the state store
// to the backend state file's description of the state store:
statePath := ".terraform/terraform.tfstate"
sMgr := &clistate.LocalState{Path: statePath}
if err := sMgr.RefreshState(); err != nil {
t.Fatal(err)
}
s := sMgr.State() // The plan should resemble this.
if !plan.StateStore.Provider.Version.Equal(s.StateStore.Provider.Version) {
t.Fatalf("wrong provider version, got %q; want %q",
plan.StateStore.Provider.Version,
s.StateStore.Provider.Version,
)
}
if !plan.StateStore.Provider.Source.Equals(*s.StateStore.Provider.Source) {
t.Fatalf("wrong provider source, got %q; want %q",
plan.StateStore.Provider.Source,
s.StateStore.Provider.Source,
)
}
// Is the provider config data correct?
providerSchema := mock.GetProviderSchemaResponse.Provider
providerTy := providerSchema.Body.ImpliedType()
pGot, err := plan.StateStore.Provider.Config.Decode(providerTy)
if err != nil {
t.Fatalf("failed to decode provider config in plan: %s", err)
}
pWant, err := s.StateStore.Provider.Config(providerSchema.Body)
if err != nil {
t.Fatalf("failed to decode cached provider config: %s", err)
}
if !pWant.RawEquals(pGot) {
t.Errorf("wrong provider config\ngot: %#v\nwant: %#v", pGot, pWant)
}
// Is the store config data correct?
storeSchema := mock.GetProviderSchemaResponse.StateStores["test_store"]
ty := storeSchema.Body.ImpliedType()
sGot, err := plan.StateStore.Config.Decode(ty)
if err != nil {
t.Fatalf("failed to decode state store config in plan: %s", err)
}
sWant, err := s.StateStore.Config(storeSchema.Body)
if err != nil {
t.Fatalf("failed to decode cached state store config: %s", err)
}
if !sWant.RawEquals(sGot) {
t.Errorf("wrong state store config\ngot: %#v\nwant: %#v", sGot, sWant)
}
}
}
func TestPlan_refreshFalse(t *testing.T) {
// Create a temporary working directory that is empty
td := t.TempDir()
testCopyDir(t, testFixturePath("plan-existing-state"), td)
t.Chdir(td)
p := planFixtureProvider()
2021-02-22 11:55:00 -05:00
view, done := testView(t)
2014-06-26 12:56:29 -04:00
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
2014-06-26 12:56:29 -04:00
}
args := []string{
"-refresh=false",
}
2021-02-22 11:55:00 -05:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
2014-06-26 12:56:29 -04:00
}
if p.ReadResourceCalled {
t.Fatal("ReadResource should not have been called")
2014-06-26 12:56:29 -04:00
}
}
func TestPlan_refreshTrue(t *testing.T) {
// Create a temporary working directory that is empty
td := t.TempDir()
testCopyDir(t, testFixturePath("plan-existing-state"), td)
t.Chdir(td)
p := planFixtureProvider()
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{
"-refresh=true",
}
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
if !p.ReadResourceCalled {
t.Fatalf("ReadResource should have been called")
}
}
// A consumer relies on the fact that running
// terraform plan -refresh=false -refresh=true gives the same result as
// terraform plan -refresh=true.
// While the flag logic itself is handled by the stdlib flags package (and code
// in main() that is tested elsewhere), we verify the overall plan command
// behaviour here in case we accidentally break this with additional logic.
func TestPlan_refreshFalseRefreshTrue(t *testing.T) {
// Create a temporary working directory that is empty
td := t.TempDir()
testCopyDir(t, testFixturePath("plan-existing-state"), td)
t.Chdir(td)
p := planFixtureProvider()
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{
"-refresh=false",
"-refresh=true",
}
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
if !p.ReadResourceCalled {
t.Fatal("ReadResource should have been called")
}
}
2014-06-20 14:47:02 -04:00
func TestPlan_state(t *testing.T) {
// Create a temporary working directory that is empty
td := t.TempDir()
testCopyDir(t, testFixturePath("plan"), td)
t.Chdir(td)
2014-09-18 13:40:23 -04:00
originalState := testState()
statePath := testStateFile(t, originalState)
2014-06-19 16:51:05 -04:00
p := planFixtureProvider()
2021-02-22 11:55:00 -05:00
view, done := testView(t)
2014-06-20 14:47:02 -04:00
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
2014-06-19 16:51:05 -04:00
}
args := []string{
"-state", statePath,
}
2021-02-22 11:55:00 -05:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
2014-06-19 16:51:05 -04:00
}
// Verify that the provider was called with the existing state
actual := p.PlanResourceChangeRequest.PriorState
expected := cty.ObjectVal(map[string]cty.Value{
2018-10-14 18:36:19 -04:00
"id": cty.StringVal("bar"),
"ami": cty.NullVal(cty.String),
2022-12-21 10:47:07 -05:00
"network_interface": cty.ListValEmpty(cty.Object(map[string]cty.Type{
2018-10-14 18:36:19 -04:00
"device_index": cty.String,
"description": cty.String,
2022-12-21 10:47:07 -05:00
})),
})
if !expected.RawEquals(actual) {
t.Fatalf("wrong prior state\ngot: %#v\nwant: %#v", actual, expected)
2014-06-19 16:51:05 -04:00
}
}
2014-07-12 00:03:56 -04:00
func TestPlan_stateDefault(t *testing.T) {
// Create a temporary working directory that is empty
td := t.TempDir()
testCopyDir(t, testFixturePath("plan"), td)
t.Chdir(td)
// Generate state and move it to the default path
2014-09-18 13:40:23 -04:00
originalState := testState()
statePath := testStateFile(t, originalState)
os.Rename(statePath, path.Join(td, "terraform.tfstate"))
2014-07-12 00:03:56 -04:00
p := planFixtureProvider()
2021-02-22 11:55:00 -05:00
view, done := testView(t)
2014-07-12 00:03:56 -04:00
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
2014-07-12 00:03:56 -04:00
}
args := []string{}
2021-02-22 11:55:00 -05:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
2014-07-12 00:03:56 -04:00
}
// Verify that the provider was called with the existing state
actual := p.PlanResourceChangeRequest.PriorState
expected := cty.ObjectVal(map[string]cty.Value{
2018-10-14 18:36:19 -04:00
"id": cty.StringVal("bar"),
"ami": cty.NullVal(cty.String),
2022-12-21 10:47:07 -05:00
"network_interface": cty.ListValEmpty(cty.Object(map[string]cty.Type{
2018-10-14 18:36:19 -04:00
"device_index": cty.String,
"description": cty.String,
2022-12-21 10:47:07 -05:00
})),
})
if !expected.RawEquals(actual) {
t.Fatalf("wrong prior state\ngot: %#v\nwant: %#v", actual, expected)
2014-07-12 00:03:56 -04:00
}
}
2014-07-18 14:37:27 -04: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 }()
td := t.TempDir()
testCopyDir(t, testFixturePath("plan-invalid"), td)
t.Chdir(td)
p := testProvider()
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
2021-01-12 16:13:10 -05:00
ResourceTypes: map[string]providers.Schema{
"test_instance": {
2025-03-04 10:33:43 -05:00
Body: &configschema.Block{
2021-01-12 16:13:10 -05:00
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
},
},
},
},
}
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
return providers.PlanResourceChangeResponse{
PlannedState: req.ProposedNewState,
}
}
2021-02-22 11:55:00 -05:00
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
2021-02-22 11:55:00 -05: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())
}
2021-02-22 11:55:00 -05:00
actual := output.Stderr()
if want := "Error: Invalid count argument"; !strings.Contains(actual, want) {
t.Fatalf("unexpected error output\ngot:\n%s\n\nshould contain: %s", actual, want)
}
if want := "9: count = timestamp()"; !strings.Contains(actual, want) {
t.Fatalf("unexpected error output\ngot:\n%s\n\nshould contain: %s", actual, want)
}
}
2014-07-18 14:37:27 -04:00
func TestPlan_vars(t *testing.T) {
// Create a temporary working directory that is empty
td := t.TempDir()
testCopyDir(t, testFixturePath("plan-vars"), td)
t.Chdir(td)
p := planVarsFixtureProvider()
2021-02-22 11:55:00 -05:00
view, done := testView(t)
2014-07-18 14:37:27 -04:00
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
2014-07-18 14:37:27 -04:00
},
}
actual := ""
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
actual = req.ProposedNewState.GetAttr("value").AsString()
resp.PlannedState = req.ProposedNewState
return
2014-07-18 14:37:27 -04:00
}
args := []string{
"-var", "foo=bar",
}
2021-02-22 11:55:00 -05:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
2014-07-18 14:37:27 -04:00
}
if actual != "bar" {
t.Fatal("didn't work")
}
}
2014-07-18 17:00:40 -04:00
func TestPlan_varsInvalid(t *testing.T) {
testCases := []struct {
args []string
wantErr string
}{
{
[]string{"-var", "foo"},
`The given -var option "foo" is not correctly specified.`,
},
{
[]string{"-var", "foo = bar"},
`Variable name "foo " is invalid due to trailing space.`,
},
}
// Create a temporary working directory that is empty
td := t.TempDir()
testCopyDir(t, testFixturePath("plan-vars"), td)
t.Chdir(td)
for _, tc := range testCases {
t.Run(strings.Join(tc.args, " "), func(t *testing.T) {
p := planVarsFixtureProvider()
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
code := c.Run(tc.args)
output := done(t)
if code != 1 {
t.Fatalf("bad: %d\n\n%s", code, output.Stdout())
}
got := output.Stderr()
if !strings.Contains(got, tc.wantErr) {
t.Fatalf("bad error output, want %q, got:\n%s", tc.wantErr, got)
}
})
}
}
func TestPlan_varsUnset(t *testing.T) {
// Create a temporary working directory that is empty
td := t.TempDir()
testCopyDir(t, testFixturePath("plan-vars"), td)
t.Chdir(td)
// 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.
// 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()
p := planVarsFixtureProvider()
2021-02-22 11:55:00 -05:00
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{}
2021-02-22 11:55:00 -05:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
}
// 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) {
// Create a temporary working directory that is empty
td := t.TempDir()
testCopyDir(t, testFixturePath("plan"), td)
t.Chdir(td)
// 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
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
2021-01-12 16:13:10 -05:00
Provider: providers.Schema{
2025-03-04 10:33:43 -05:00
Body: &configschema.Block{
2021-01-12 16:13:10 -05:00
Attributes: map[string]*configschema.Attribute{
"region": {Type: cty.String, Required: true},
},
},
},
2021-01-12 16:13:10 -05:00
ResourceTypes: map[string]providers.Schema{
"test_instance": {
2025-03-04 10:33:43 -05:00
Body: &configschema.Block{
2021-01-12 16:13:10 -05:00
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},
},
},
},
},
},
},
},
DataSources: map[string]providers.Schema{
"test_data_source": {
2025-03-04 10:33:43 -05:00
Body: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {
Type: cty.String,
Required: true,
},
"valid": {
Type: cty.Bool,
Computed: true,
},
},
},
},
},
}
2021-02-22 11:55:00 -05:00
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{}
2021-02-22 11:55:00 -05:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
}
// Test that terraform properly merges provider configuration that's split
// between config files and interactive input variables.
// https://github.com/hashicorp/terraform/issues/28956
func TestPlan_providerConfigMerge(t *testing.T) {
td := t.TempDir()
testCopyDir(t, testFixturePath("plan-provider-input"), td)
t.Chdir(td)
// 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 and a nested block
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
Provider: providers.Schema{
2025-03-04 10:33:43 -05:00
Body: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"region": {Type: cty.String, Required: true},
"url": {Type: cty.String, Required: true},
},
BlockTypes: map[string]*configschema.NestedBlock{
"auth": {
Nesting: configschema.NestingList,
Block: configschema.Block{
Attributes: map[string]*configschema.Attribute{
"user": {Type: cty.String, Required: true},
"password": {Type: cty.String, Required: true},
},
},
},
},
},
},
ResourceTypes: map[string]providers.Schema{
"test_instance": {
2025-03-04 10:33:43 -05:00
Body: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
},
},
},
},
}
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{}
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
if !p.ConfigureProviderCalled {
t.Fatal("configure provider not called")
}
// For this test, we want to confirm that we've sent the expected config
// value *to* the provider.
got := p.ConfigureProviderRequest.Config
want := cty.ObjectVal(map[string]cty.Value{
"auth": cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"user": cty.StringVal("one"),
"password": cty.StringVal("onepw"),
}),
cty.ObjectVal(map[string]cty.Value{
"user": cty.StringVal("two"),
"password": cty.StringVal("twopw"),
}),
}),
"region": cty.StringVal("us-east-1"),
"url": cty.StringVal("example.com"),
})
if !got.RawEquals(want) {
t.Fatal("wrong provider config")
}
}
2014-07-18 17:00:40 -04:00
func TestPlan_varFile(t *testing.T) {
// Create a temporary working directory that is empty
td := t.TempDir()
testCopyDir(t, testFixturePath("plan-vars"), td)
t.Chdir(td)
2014-07-18 17:00:40 -04:00
varFilePath := testTempFile(t)
if err := ioutil.WriteFile(varFilePath, []byte(planVarFile), 0644); err != nil {
t.Fatalf("err: %s", err)
}
p := planVarsFixtureProvider()
2021-02-22 11:55:00 -05:00
view, done := testView(t)
2014-07-18 17:00:40 -04:00
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
2014-07-18 17:00:40 -04:00
},
}
actual := ""
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
actual = req.ProposedNewState.GetAttr("value").AsString()
resp.PlannedState = req.ProposedNewState
return
2014-07-18 17:00:40 -04:00
}
args := []string{
"-var-file", varFilePath,
}
2021-02-22 11:55:00 -05:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
2014-07-18 17:00:40 -04:00
}
if actual != "bar" {
t.Fatal("didn't work")
}
}
func TestPlan_varFileDefault(t *testing.T) {
// Create a temporary working directory that is empty
td := t.TempDir()
testCopyDir(t, testFixturePath("plan-vars"), td)
t.Chdir(td)
varFilePath := filepath.Join(td, "terraform.tfvars")
if err := ioutil.WriteFile(varFilePath, []byte(planVarFile), 0644); err != nil {
t.Fatalf("err: %s", err)
}
p := planVarsFixtureProvider()
2021-02-22 11:55:00 -05:00
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
actual := ""
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
actual = req.ProposedNewState.GetAttr("value").AsString()
resp.PlannedState = req.ProposedNewState
return
}
args := []string{}
2021-02-22 11:55:00 -05:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
if actual != "bar" {
t.Fatal("didn't work")
}
}
func TestPlan_varFileWithDecls(t *testing.T) {
// Create a temporary working directory that is empty
td := t.TempDir()
testCopyDir(t, testFixturePath("plan-vars"), td)
t.Chdir(td)
varFilePath := testTempFile(t)
if err := ioutil.WriteFile(varFilePath, []byte(planVarFileWithDecl), 0644); err != nil {
t.Fatalf("err: %s", err)
}
p := planVarsFixtureProvider()
2021-02-22 11:55:00 -05:00
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{
"-var-file", varFilePath,
}
2021-02-22 11:55:00 -05:00
code := c.Run(args)
output := done(t)
if code == 0 {
t.Fatalf("succeeded; want failure\n\n%s", output.Stdout())
}
2021-02-22 11:55:00 -05:00
msg := output.Stderr()
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)
}
}
func TestPlan_detailedExitcode(t *testing.T) {
td := t.TempDir()
testCopyDir(t, testFixturePath("plan"), td)
t.Chdir(td)
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())
}
})
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())
}
})
}
func TestPlan_detailedExitcode_emptyDiff(t *testing.T) {
td := t.TempDir()
testCopyDir(t, testFixturePath("plan-emptydiff"), td)
t.Chdir(td)
p := testProvider()
2021-02-22 11:55:00 -05:00
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{"-detailed-exitcode"}
2021-02-22 11:55:00 -05:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
}
func TestPlan_shutdown(t *testing.T) {
// Create a temporary working directory that is empty
td := t.TempDir()
testCopyDir(t, testFixturePath("apply-shutdown"), td)
t.Chdir(td)
cancelled := make(chan struct{})
shutdownCh := make(chan struct{})
p := testProvider()
2021-02-22 11:55:00 -05:00
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
ShutdownCh: shutdownCh,
},
}
p.StopFn = func() error {
close(cancelled)
return nil
}
var once sync.Once
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
once.Do(func() {
shutdownCh <- struct{}{}
})
// Because of the internal lock in the MockProvider, we can't
// coordinate directly with the calling of Stop, and making the
// 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)
s := req.ProposedNewState.AsValueMap()
s["ami"] = cty.StringVal("bar")
resp.PlannedState = cty.ObjectVal(s)
return
}
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
2021-01-12 16:13:10 -05:00
ResourceTypes: map[string]providers.Schema{
"test_instance": {
2025-03-04 10:33:43 -05:00
Body: &configschema.Block{
2021-01-12 16:13:10 -05:00
Attributes: map[string]*configschema.Attribute{
"ami": {Type: cty.String, Optional: true},
},
},
},
},
}
code := c.Run([]string{})
2021-02-22 11:55:00 -05:00
output := done(t)
if code != 1 {
2021-02-22 11:55:00 -05:00
t.Errorf("wrong exit code %d; want 1\noutput:\n%s", code, output.Stdout())
}
select {
case <-cancelled:
default:
t.Error("command not cancelled")
}
}
func TestPlan_init_required(t *testing.T) {
td := t.TempDir()
testCopyDir(t, testFixturePath("plan"), td)
t.Chdir(td)
2021-02-22 11:55:00 -05:00
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
// Running plan without setting testingOverrides is similar to plan without init
View: view,
},
}
2021-03-22 16:38:56 -04:00
args := []string{"-no-color"}
2021-02-22 11:55:00 -05:00
code := c.Run(args)
output := done(t)
if code != 1 {
t.Fatalf("expected error, got success")
}
2021-02-22 11:55:00 -05:00
got := output.Stderr()
backend/local: Check dependency lock consistency before any operations In historical versions of Terraform the responsibility to check this was inside the terraform.NewContext function, along with various other assorted concerns that made that function particularly complicated. More recently, we reduced the responsibility of the "terraform" package only to instantiating particular named plugins, assuming that its caller is responsible for selecting appropriate versions of any providers that _are_ external. However, until this commit we were just assuming that "terraform init" had correctly selected appropriate plugins and recorded them in the lock file, and so nothing was dealing with the problem of ensuring that there haven't been any changes to the lock file or config since the most recent "terraform init" which would cause us to need to re-evaluate those decisions. Part of the game here is to slightly extend the role of the dependency locks object to also carry information about a subset of provider addresses whose lock entries we're intentionally disregarding as part of the various little edge-case features we have for overridding providers: dev_overrides, "unmanaged providers", and the testing overrides in our own unit tests. This is an in-memory-only annotation, never included in the serialized plan files on disk. I had originally intended to create a new package to encapsulate all of this plugin-selection logic, including both the version constraint checking here and also the handling of the provider factory functions, but as an interim step I've just made version constraint consistency checks the responsibility of the backend/local package, which means that we'll always catch problems as part of preparing for local operations, while not imposing these additional checks on commands that _don't_ run local operations, such as "terraform apply" when in remote operations mode.
2021-09-29 20:31:43 -04:00
if !(strings.Contains(got, "terraform init") && strings.Contains(got, "provider registry.terraform.io/hashicorp/test: required by this configuration but no version is selected")) {
2021-02-22 11:55:00 -05:00
t.Fatal("wrong error message in output:", got)
}
}
// Config with multiple resources, targeting plan of a subset
func TestPlan_targeted(t *testing.T) {
td := t.TempDir()
testCopyDir(t, testFixturePath("apply-targeted"), td)
t.Chdir(td)
p := testProvider()
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
2025-03-04 10:33:43 -05:00
Body: &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,
}
}
2021-02-17 13:01:30 -05:00
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{
"-target", "test_instance.foo",
"-target", "test_instance.baz",
}
2021-02-22 11:55:00 -05:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
2021-02-22 11:55:00 -05:00
if got, want := output.Stdout(), "3 to add, 0 to change, 0 to destroy"; !strings.Contains(got, want) {
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)
t.Chdir(td)
2021-02-22 11:55:00 -05:00
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
View: view,
},
}
args := []string{
"-target", target,
}
2021-02-22 11:55:00 -05:00
code := c.Run(args)
output := done(t)
if code != 1 {
t.Fatalf("bad: %d\n\n%s", code, output.Stdout())
}
2021-02-22 11:55:00 -05:00
got := output.Stderr()
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)
}
})
}
}
func TestPlan_replace(t *testing.T) {
td := t.TempDir()
testCopyDir(t, testFixturePath("plan-replace"), td)
t.Chdir(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": {
2025-03-04 10:33:43 -05:00
Body: &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)
}
}
// 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 := t.TempDir()
testCopyDir(t, testFixturePath("parallelism"), td)
t.Chdir(td)
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())
// This just makes go vet happy, in reality the function will never exit if
// begin() isn't called inside ApplyResourceChangeFn.
defer begin()
// 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 := &testing_provider.MockProvider{}
provider.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
ResourceTypes: map[string]providers.Schema{
2025-03-04 10:33:43 -05:00
name + "_instance": {Body: &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 := t.TempDir()
testCopyDir(t, testFixturePath("plan"), td)
t.Chdir(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)
}
}
})
}
func TestPlan_jsonGoldenReference(t *testing.T) {
// Create a temporary working directory that is empty
td := t.TempDir()
testCopyDir(t, testFixturePath("plan"), td)
t.Chdir(td)
p := planFixtureProvider()
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{
"-json",
}
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
checkGoldenReference(t, output, "plan")
}
// Tests related to how plan command behaves when there are query files in the configuration path
func TestPlan_QueryFiles(t *testing.T) {
// a plan succeeds regardless of valid or invalid
// tfquery files in the configuration path
t.Run("with invalid query files in the config path", func(t *testing.T) {
td := t.TempDir()
testCopyDir(t, testFixturePath("query/invalid-syntax"), td)
t.Chdir(td)
p := planFixtureProvider()
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{}
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
})
// the duplicate in the query should not matter because query files are not processed
t.Run("with duplicate variables across query and plan file", func(t *testing.T) {
td := t.TempDir()
testCopyDir(t, testFixturePath("query/duplicate-variables"), td)
t.Chdir(td)
p := planFixtureProvider()
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{"-var", "instance_name=foo"}
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
})
}
// planFixtureSchema returns a schema suitable for processing the
// configuration in testdata/plan . This schema should be
// assigned to a mock provider named "test".
func planFixtureSchema() *providers.GetProviderSchemaResponse {
return &providers.GetProviderSchemaResponse{
2021-01-12 16:13:10 -05:00
ResourceTypes: map[string]providers.Schema{
"test_instance": {
2025-03-04 10:33:43 -05:00
Body: &configschema.Block{
2021-01-12 16:13:10 -05:00
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},
},
},
},
},
},
},
},
DataSources: map[string]providers.Schema{
"test_data_source": {
2025-03-04 10:33:43 -05:00
Body: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {
Type: cty.String,
Required: true,
},
"valid": {
Type: cty.Bool,
Computed: true,
},
},
},
},
},
}
}
// planFixtureProvider returns a mock provider that is configured for basic
// operation with the configuration in testdata/plan. This mock has
2021-01-12 16:13:10 -05:00
// GetSchemaResponse and PlanResourceChangeFn populated, with the plan
// step just passing through the new object proposed by Terraform Core.
func planFixtureProvider() *testing_provider.MockProvider {
p := testProvider()
p.GetProviderSchemaResponse = planFixtureSchema()
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
return providers.PlanResourceChangeResponse{
PlannedState: req.ProposedNewState,
}
}
p.ReadDataSourceFn = func(req providers.ReadDataSourceRequest) providers.ReadDataSourceResponse {
return providers.ReadDataSourceResponse{
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("zzzzz"),
"valid": cty.BoolVal(true),
}),
}
}
return p
}
// planVarsFixtureSchema returns a schema suitable for processing the
// configuration in testdata/plan-vars . This schema should be
// assigned to a mock provider named "test".
func planVarsFixtureSchema() *providers.GetProviderSchemaResponse {
return &providers.GetProviderSchemaResponse{
2021-01-12 16:13:10 -05:00
ResourceTypes: map[string]providers.Schema{
"test_instance": {
2025-03-04 10:33:43 -05:00
Body: &configschema.Block{
2021-01-12 16:13:10 -05:00
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 testdata/plan-vars. This mock has
2021-01-12 16:13:10 -05:00
// GetSchemaResponse and PlanResourceChangeFn populated, with the plan
// step just passing through the new object proposed by Terraform Core.
func planVarsFixtureProvider() *testing_provider.MockProvider {
p := testProvider()
p.GetProviderSchemaResponse = planVarsFixtureSchema()
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
return providers.PlanResourceChangeResponse{
PlannedState: req.ProposedNewState,
}
}
p.ReadDataSourceFn = func(req providers.ReadDataSourceRequest) providers.ReadDataSourceResponse {
return providers.ReadDataSourceResponse{
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("zzzzz"),
"valid": cty.BoolVal(true),
}),
}
}
return p
}
// 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() *testing_provider.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,
}
}
p.ReadDataSourceFn = func(req providers.ReadDataSourceRequest) providers.ReadDataSourceResponse {
return providers.ReadDataSourceResponse{
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("zzzzz"),
"valid": cty.BoolVal(true),
}),
}
}
return p
}
2014-07-18 17:00:40 -04:00
const planVarFile = `
foo = "bar"
`
2014-09-18 13:40:23 -04:00
const planVarFileWithDecl = `
foo = "bar"
variable "nope" {
}
`