2023-05-02 11:33:06 -04:00
|
|
|
// Copyright IBM Corp. 2014, 2026
|
2023-08-10 18:43:27 -04:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-05-02 11:33:06 -04:00
|
|
|
|
2014-07-11 23:38:03 -04:00
|
|
|
package command
|
|
|
|
|
|
|
|
|
|
import (
|
2023-02-09 21:32:45 -05:00
|
|
|
"context"
|
2014-07-11 23:41:47 -04:00
|
|
|
"os"
|
2014-07-11 23:38:03 -04:00
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2023-02-09 21:32:45 -05:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2023-12-20 06:04:10 -05:00
|
|
|
"github.com/hashicorp/cli"
|
2018-10-13 11:29:58 -04: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 17:24:45 -04:00
|
|
|
|
2021-05-17 15:00:50 -04:00
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
2023-02-09 21:32:45 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/configs/configload"
|
|
|
|
|
"github.com/hashicorp/terraform/internal/configs/configschema"
|
|
|
|
|
"github.com/hashicorp/terraform/internal/initwd"
|
2021-05-17 15:33:17 -04:00
|
|
|
"github.com/hashicorp/terraform/internal/plans"
|
2023-02-09 21:32:45 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/providers"
|
|
|
|
|
"github.com/hashicorp/terraform/internal/registry"
|
2021-05-17 15:43:35 -04:00
|
|
|
"github.com/hashicorp/terraform/internal/states"
|
2023-02-09 21:32:45 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/terminal"
|
2014-07-11 23:38:03 -04:00
|
|
|
)
|
|
|
|
|
|
2023-02-09 21:32:45 -05:00
|
|
|
func TestGraph_planPhase(t *testing.T) {
|
2022-04-08 12:34:16 -04:00
|
|
|
td := t.TempDir()
|
2021-02-02 10:35:45 -05:00
|
|
|
testCopyDir(t, testFixturePath("graph"), td)
|
2025-07-16 11:04:10 -04:00
|
|
|
t.Chdir(td)
|
2017-02-07 12:11:48 -05:00
|
|
|
|
2014-07-11 23:38:03 -04:00
|
|
|
ui := new(cli.MockUi)
|
2023-02-09 21:32:45 -05:00
|
|
|
streams, closeStreams := terminal.StreamsForTesting(t)
|
2014-07-11 23:38:03 -04:00
|
|
|
c := &GraphCommand{
|
2014-07-12 23:37:30 -04:00
|
|
|
Meta: Meta{
|
2020-08-07 14:04:40 -04:00
|
|
|
testingOverrides: metaOverridesForProvider(applyFixtureProvider()),
|
2017-04-13 21:05:58 -04:00
|
|
|
Ui: ui,
|
2023-02-09 21:32:45 -05:00
|
|
|
Streams: streams,
|
2014-07-12 23:37:30 -04:00
|
|
|
},
|
2014-07-11 23:38:03 -04:00
|
|
|
}
|
|
|
|
|
|
2023-02-09 21:32:45 -05:00
|
|
|
args := []string{"-type=plan"}
|
2014-07-11 23:38:03 -04:00
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 21:32:45 -05:00
|
|
|
output := closeStreams(t)
|
|
|
|
|
if !strings.Contains(output.Stdout(), `provider[\"registry.terraform.io/hashicorp/test\"]`) {
|
|
|
|
|
t.Fatalf("doesn't look like digraph:\n%s\n\nstderr:\n%s", output.Stdout(), output.Stderr())
|
2014-07-11 23:38:03 -04:00
|
|
|
}
|
|
|
|
|
}
|
2024-12-10 09:03:04 -05:00
|
|
|
|
2024-12-10 07:40:26 -05:00
|
|
|
func TestGraph_cyclic(t *testing.T) {
|
|
|
|
|
td := t.TempDir()
|
|
|
|
|
testCopyDir(t, testFixturePath("graph-cyclic"), td)
|
2025-07-16 11:04:10 -04:00
|
|
|
t.Chdir(td)
|
2024-12-10 07:40:26 -05:00
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
args []string
|
|
|
|
|
expected string
|
2024-12-11 03:04:14 -05:00
|
|
|
|
|
|
|
|
// The cyclic errors do not maintain a consistent order, so we can't
|
|
|
|
|
// predict the exact output. We'll just check that the error messages
|
|
|
|
|
// are present for the things we know are cyclic.
|
|
|
|
|
errors []string
|
2024-12-10 07:40:26 -05:00
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "plan",
|
|
|
|
|
args: []string{"-type=plan"},
|
2024-12-10 09:03:04 -05:00
|
|
|
errors: []string{`Error: Cycle: test_instance.`,
|
|
|
|
|
`Error: Cycle: local.`},
|
2024-12-10 07:40:26 -05:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "plan with -draw-cycles option",
|
|
|
|
|
args: []string{"-draw-cycles", "-type=plan"},
|
|
|
|
|
expected: `digraph {
|
|
|
|
|
compound = "true"
|
|
|
|
|
newrank = "true"
|
|
|
|
|
subgraph "root" {
|
|
|
|
|
"[root] provider[\"registry.terraform.io/hashicorp/test\"]" [label = "provider[\"registry.terraform.io/hashicorp/test\"]", shape = "diamond"]
|
|
|
|
|
"[root] test_instance.bar (expand)" [label = "test_instance.bar", shape = "box"]
|
|
|
|
|
"[root] test_instance.foo (expand)" [label = "test_instance.foo", shape = "box"]
|
|
|
|
|
"[root] local.test1 (expand)" -> "[root] local.test2 (expand)"
|
|
|
|
|
"[root] local.test2 (expand)" -> "[root] local.test1 (expand)"
|
|
|
|
|
"[root] provider[\"registry.terraform.io/hashicorp/test\"] (close)" -> "[root] provider[\"registry.terraform.io/hashicorp/test\"]"
|
|
|
|
|
"[root] provider[\"registry.terraform.io/hashicorp/test\"] (close)" -> "[root] test_instance.bar (expand)"
|
|
|
|
|
"[root] provider[\"registry.terraform.io/hashicorp/test\"] (close)" -> "[root] test_instance.foo (expand)"
|
|
|
|
|
"[root] root" -> "[root] provider[\"registry.terraform.io/hashicorp/test\"] (close)"
|
|
|
|
|
"[root] test_instance.bar (expand)" -> "[root] provider[\"registry.terraform.io/hashicorp/test\"]"
|
|
|
|
|
"[root] test_instance.bar (expand)" -> "[root] test_instance.foo (expand)" [color = "red", penwidth = "2.0"]
|
|
|
|
|
"[root] test_instance.foo (expand)" -> "[root] provider[\"registry.terraform.io/hashicorp/test\"]"
|
|
|
|
|
"[root] test_instance.foo (expand)" -> "[root] test_instance.bar (expand)" [color = "red", penwidth = "2.0"]
|
|
|
|
|
}
|
|
|
|
|
}`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "apply",
|
|
|
|
|
args: []string{"-type=apply"},
|
2024-12-10 09:03:04 -05:00
|
|
|
// The cyclic errors do not maintain a consistent order, so we can't
|
|
|
|
|
// predict the exact output. We'll just check that the error messages
|
|
|
|
|
// are present for the things we know are cyclic.
|
|
|
|
|
errors: []string{`Error: Cycle: test_instance.`,
|
|
|
|
|
`Error: Cycle: local.`},
|
2024-12-10 07:40:26 -05:00
|
|
|
},
|
|
|
|
|
{
|
2024-12-10 09:03:04 -05:00
|
|
|
name: "apply with -draw-cycles option",
|
2024-12-10 07:40:26 -05:00
|
|
|
args: []string{"-draw-cycles", "-type=apply"},
|
|
|
|
|
expected: `digraph {
|
|
|
|
|
compound = "true"
|
|
|
|
|
newrank = "true"
|
|
|
|
|
subgraph "root" {
|
|
|
|
|
"[root] provider[\"registry.terraform.io/hashicorp/test\"]" [label = "provider[\"registry.terraform.io/hashicorp/test\"]", shape = "diamond"]
|
|
|
|
|
"[root] test_instance.bar (expand)" [label = "test_instance.bar", shape = "box"]
|
|
|
|
|
"[root] test_instance.foo (expand)" [label = "test_instance.foo", shape = "box"]
|
|
|
|
|
"[root] local.test1 (expand)" -> "[root] local.test2 (expand)"
|
|
|
|
|
"[root] local.test2 (expand)" -> "[root] local.test1 (expand)"
|
|
|
|
|
"[root] provider[\"registry.terraform.io/hashicorp/test\"] (close)" -> "[root] provider[\"registry.terraform.io/hashicorp/test\"]"
|
|
|
|
|
"[root] provider[\"registry.terraform.io/hashicorp/test\"] (close)" -> "[root] test_instance.bar (expand)"
|
|
|
|
|
"[root] provider[\"registry.terraform.io/hashicorp/test\"] (close)" -> "[root] test_instance.foo (expand)"
|
|
|
|
|
"[root] root" -> "[root] provider[\"registry.terraform.io/hashicorp/test\"] (close)"
|
|
|
|
|
"[root] test_instance.bar (expand)" -> "[root] provider[\"registry.terraform.io/hashicorp/test\"]"
|
|
|
|
|
"[root] test_instance.bar (expand)" -> "[root] test_instance.foo (expand)" [color = "red", penwidth = "2.0"]
|
|
|
|
|
"[root] test_instance.foo (expand)" -> "[root] provider[\"registry.terraform.io/hashicorp/test\"]"
|
|
|
|
|
"[root] test_instance.foo (expand)" -> "[root] test_instance.bar (expand)" [color = "red", penwidth = "2.0"]
|
|
|
|
|
}
|
|
|
|
|
}`,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
|
streams, closeStreams := terminal.StreamsForTesting(t)
|
|
|
|
|
c := &GraphCommand{
|
|
|
|
|
Meta: Meta{
|
|
|
|
|
testingOverrides: metaOverridesForProvider(applyFixtureProvider()),
|
|
|
|
|
Ui: ui,
|
|
|
|
|
Streams: streams,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-10 09:03:04 -05:00
|
|
|
code := c.Run(tt.args)
|
|
|
|
|
// If we expect errors, make sure they are present
|
|
|
|
|
if len(tt.errors) > 0 {
|
|
|
|
|
if code == 0 {
|
|
|
|
|
t.Fatalf("expected error, got none")
|
|
|
|
|
}
|
|
|
|
|
got := strings.TrimSpace(ui.ErrorWriter.String())
|
|
|
|
|
for _, err := range tt.errors {
|
|
|
|
|
if !strings.Contains(got, err) {
|
|
|
|
|
t.Fatalf("expected error:\n%s\n\nactual error:\n%s", err, got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
2024-12-10 07:40:26 -05:00
|
|
|
}
|
|
|
|
|
|
2024-12-10 09:03:04 -05:00
|
|
|
// If we don't expect errors, make sure the command ran successfully
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
|
}
|
2024-12-10 07:40:26 -05:00
|
|
|
output := closeStreams(t)
|
|
|
|
|
if strings.TrimSpace(output.Stdout()) != strings.TrimSpace(tt.expected) {
|
|
|
|
|
t.Fatalf("expected dot graph to match:\n%s", cmp.Diff(output.Stdout(), tt.expected))
|
|
|
|
|
}
|
2024-12-10 09:03:04 -05:00
|
|
|
|
2024-12-10 07:40:26 -05:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-11 23:38:03 -04:00
|
|
|
|
2014-07-11 23:41:47 -04:00
|
|
|
func TestGraph_multipleArgs(t *testing.T) {
|
2014-07-11 23:38:03 -04:00
|
|
|
ui := new(cli.MockUi)
|
2014-07-12 22:47:31 -04:00
|
|
|
c := &GraphCommand{
|
2014-07-12 23:37:30 -04:00
|
|
|
Meta: Meta{
|
2020-08-07 14:04:40 -04:00
|
|
|
testingOverrides: metaOverridesForProvider(applyFixtureProvider()),
|
2017-04-13 21:05:58 -04:00
|
|
|
Ui: ui,
|
2014-07-12 23:37:30 -04:00
|
|
|
},
|
2014-07-11 23:38:03 -04:00
|
|
|
}
|
|
|
|
|
|
2014-07-11 23:41:47 -04:00
|
|
|
args := []string{
|
|
|
|
|
"bad",
|
|
|
|
|
"bad",
|
|
|
|
|
}
|
2014-07-11 23:38:03 -04:00
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
|
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 21:32:45 -05:00
|
|
|
func TestGraph_noConfig(t *testing.T) {
|
2022-04-08 12:34:16 -04:00
|
|
|
td := t.TempDir()
|
2023-02-09 21:32:45 -05:00
|
|
|
os.MkdirAll(td, 0755)
|
2025-07-16 11:04:10 -04:00
|
|
|
t.Chdir(td)
|
2014-07-11 23:41:47 -04:00
|
|
|
|
2023-02-09 21:32:45 -05:00
|
|
|
streams, closeStreams := terminal.StreamsForTesting(t)
|
|
|
|
|
defer closeStreams(t)
|
|
|
|
|
ui := cli.NewMockUi()
|
2014-07-11 23:41:47 -04:00
|
|
|
c := &GraphCommand{
|
2014-07-12 23:37:30 -04:00
|
|
|
Meta: Meta{
|
2020-08-07 14:04:40 -04:00
|
|
|
testingOverrides: metaOverridesForProvider(applyFixtureProvider()),
|
2017-04-13 21:05:58 -04:00
|
|
|
Ui: ui,
|
2023-02-09 21:32:45 -05:00
|
|
|
Streams: streams,
|
2014-07-12 23:37:30 -04:00
|
|
|
},
|
2014-07-11 23:38:03 -04:00
|
|
|
}
|
|
|
|
|
|
2023-02-09 21:32:45 -05:00
|
|
|
// Running the graph command without a config should not panic,
|
|
|
|
|
// but this may be an error at some point in the future.
|
|
|
|
|
args := []string{"-type", "apply"}
|
2014-07-11 23:41:47 -04:00
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
2014-07-11 23:38:03 -04:00
|
|
|
}
|
|
|
|
|
}
|
2014-07-12 22:25:50 -04:00
|
|
|
|
2023-02-09 21:32:45 -05:00
|
|
|
func TestGraph_resourcesOnly(t *testing.T) {
|
|
|
|
|
wd := tempWorkingDirFixture(t, "graph-interesting")
|
2025-07-16 11:04:10 -04:00
|
|
|
t.Chdir(wd.RootModuleDir())
|
2023-02-09 21:32:45 -05:00
|
|
|
|
|
|
|
|
// The graph-interesting fixture has a child module, so we'll need to
|
|
|
|
|
// run the module installer just to get the working directory set up
|
|
|
|
|
// properly, as if the user has run "terraform init". This is really
|
|
|
|
|
// just building the working directory's index of module directories.
|
|
|
|
|
loader, cleanupLoader := configload.NewLoaderForTests(t)
|
|
|
|
|
t.Cleanup(cleanupLoader)
|
|
|
|
|
err := os.MkdirAll(".terraform/modules", 0700)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-02-26 11:33:01 -05:00
|
|
|
inst := initwd.NewModuleInstaller(".terraform/modules", loader, registry.NewClient(nil, nil), nil)
|
2023-02-09 21:32:45 -05:00
|
|
|
_, instDiags := inst.InstallModules(context.Background(), ".", "tests", true, false, initwd.ModuleInstallHooksImpl{})
|
|
|
|
|
if instDiags.HasErrors() {
|
|
|
|
|
t.Fatal(instDiags.Err())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
|
|
|
|
ResourceTypes: map[string]providers.Schema{
|
|
|
|
|
"foo": {
|
2025-03-04 10:33:43 -05:00
|
|
|
Body: &configschema.Block{
|
2023-02-09 21:32:45 -05:00
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
|
"arg": {
|
|
|
|
|
Type: cty.String,
|
|
|
|
|
Optional: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
2017-07-18 13:03:57 -04:00
|
|
|
|
2023-02-09 21:32:45 -05:00
|
|
|
ui := cli.NewMockUi()
|
|
|
|
|
streams, closeStreams := terminal.StreamsForTesting(t)
|
2017-07-18 13:03:57 -04:00
|
|
|
c := &GraphCommand{
|
|
|
|
|
Meta: Meta{
|
2023-02-09 21:32:45 -05:00
|
|
|
testingOverrides: &testingOverrides{
|
|
|
|
|
Providers: map[addrs.Provider]providers.Factory{
|
|
|
|
|
addrs.NewDefaultProvider("foo"): providers.FactoryFixed(p),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Ui: ui,
|
|
|
|
|
Streams: streams,
|
2017-07-18 13:03:57 -04:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 21:32:45 -05:00
|
|
|
// A "resources only" graph is the default behavior, with no extra arguments.
|
|
|
|
|
args := []string{}
|
2017-07-18 13:03:57 -04:00
|
|
|
if code := c.Run(args); code != 0 {
|
2023-02-09 21:32:45 -05:00
|
|
|
output := closeStreams(t)
|
|
|
|
|
t.Fatalf("unexpected error: \n%s", output.Stderr())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
output := closeStreams(t)
|
|
|
|
|
gotGraph := strings.TrimSpace(output.Stdout())
|
|
|
|
|
wantGraph := strings.TrimSpace(`
|
|
|
|
|
digraph G {
|
|
|
|
|
rankdir = "RL";
|
|
|
|
|
node [shape = rect, fontname = "sans-serif"];
|
|
|
|
|
"foo.bar" [label="foo.bar"];
|
|
|
|
|
"foo.baz" [label="foo.baz"];
|
|
|
|
|
"foo.boop" [label="foo.boop"];
|
|
|
|
|
subgraph "cluster_module.child" {
|
|
|
|
|
label = "module.child"
|
|
|
|
|
fontname = "sans-serif"
|
|
|
|
|
"module.child.foo.bleep" [label="foo.bleep"];
|
|
|
|
|
}
|
|
|
|
|
"foo.baz" -> "foo.bar";
|
|
|
|
|
"foo.boop" -> "module.child.foo.bleep";
|
|
|
|
|
"module.child.foo.bleep" -> "foo.bar";
|
|
|
|
|
}
|
|
|
|
|
`)
|
|
|
|
|
if diff := cmp.Diff(wantGraph, gotGraph); diff != "" {
|
|
|
|
|
t.Fatalf("wrong result\n%s", diff)
|
2017-07-18 13:03:57 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 21:32:45 -05:00
|
|
|
func TestGraph_applyPhaseSavedPlan(t *testing.T) {
|
2025-07-16 06:41:51 -04:00
|
|
|
tmp := t.TempDir()
|
|
|
|
|
t.Chdir(tmp)
|
2017-02-07 12:11:48 -05:00
|
|
|
|
2024-08-12 14:28:26 -04:00
|
|
|
emptyObj, err := plans.NewDynamicValue(cty.EmptyObjectVal, cty.EmptyObject)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nullEmptyObj, err := plans.NewDynamicValue(cty.NullVal((cty.EmptyObject)), cty.EmptyObject)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
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 17:24:45 -04:00
|
|
|
plan := &plans.Plan{
|
2024-08-12 14:28:26 -04:00
|
|
|
Changes: plans.NewChangesSrc(),
|
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 17:24:45 -04:00
|
|
|
}
|
|
|
|
|
plan.Changes.Resources = append(plan.Changes.Resources, &plans.ResourceInstanceChangeSrc{
|
|
|
|
|
Addr: addrs.Resource{
|
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
|
Type: "test_instance",
|
|
|
|
|
Name: "bar",
|
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
|
ChangeSrc: plans.ChangeSrc{
|
|
|
|
|
Action: plans.Delete,
|
2024-08-12 14:28:26 -04:00
|
|
|
Before: emptyObj,
|
|
|
|
|
After: nullEmptyObj,
|
2016-12-03 18:00:34 -05:00
|
|
|
},
|
2020-02-13 15:32:58 -05:00
|
|
|
ProviderAddr: addrs.AbsProviderConfig{
|
2020-10-05 08:33:49 -04:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 14:19:52 -04:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 15:32:58 -05:00
|
|
|
},
|
2014-07-12 22:25:50 -04:00
|
|
|
})
|
2024-08-12 14:28:26 -04:00
|
|
|
|
2025-11-28 11:12:46 -05:00
|
|
|
plan.Backend = &plans.Backend{
|
2018-10-13 11:29:58 -04:00
|
|
|
// Doesn't actually matter since we aren't going to activate the backend
|
|
|
|
|
// for this command anyway, but we need something here for the plan
|
|
|
|
|
// file writer to succeed.
|
2026-02-03 10:29:58 -05:00
|
|
|
Type: "placeholder",
|
|
|
|
|
Config: emptyObj,
|
|
|
|
|
Workspace: "default",
|
2018-10-13 11:29:58 -04: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 17:24:45 -04:00
|
|
|
_, configSnap := testModuleWithSnapshot(t, "graph")
|
|
|
|
|
|
|
|
|
|
planPath := testPlanFile(t, configSnap, states.NewState(), plan)
|
2014-07-12 22:25:50 -04:00
|
|
|
|
2023-02-09 21:32:45 -05:00
|
|
|
streams, closeStreams := terminal.StreamsForTesting(t)
|
|
|
|
|
ui := cli.NewMockUi()
|
2014-07-12 22:25:50 -04:00
|
|
|
c := &GraphCommand{
|
2014-07-12 23:37:30 -04:00
|
|
|
Meta: Meta{
|
2020-08-07 14:04:40 -04:00
|
|
|
testingOverrides: metaOverridesForProvider(applyFixtureProvider()),
|
2017-04-13 21:05:58 -04:00
|
|
|
Ui: ui,
|
2023-02-09 21:32:45 -05:00
|
|
|
Streams: streams,
|
2014-07-12 23:37:30 -04:00
|
|
|
},
|
2014-07-12 22:25:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
args := []string{
|
2021-02-02 13:09:30 -05:00
|
|
|
"-plan", planPath,
|
2014-07-12 22:25:50 -04:00
|
|
|
}
|
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 21:32:45 -05:00
|
|
|
output := closeStreams(t)
|
|
|
|
|
if !strings.Contains(output.Stdout(), `provider[\"registry.terraform.io/hashicorp/test\"]`) {
|
|
|
|
|
t.Fatalf("doesn't look like digraph:\n%s\n\nstderr:\n%s", output.Stdout(), output.Stderr())
|
2014-07-12 22:25:50 -04:00
|
|
|
}
|
|
|
|
|
}
|