2023-05-02 10:33:06 -05:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-09-20 07:16:53 -05:00
|
|
|
package tofu
|
2016-11-05 19:58:38 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2018-05-04 21:24:06 -05:00
|
|
|
|
core: refactoring.ImpliedMoveStatements replaces NodeCountBoundary
Going back a long time we've had a special magic behavior which tries to
recognize a situation where a module author either added or removed the
"count" argument from a resource that already has instances, and to
silently rename the zeroth or no-key instance so that we don't plan to
destroy and recreate the associated object.
Now we have a more general idea of "move statements", and specifically
the idea of "implied" move statements which replicates the same heuristic
we used to use for this behavior, we can treat this magic renaming rule as
just another "move statement", special only in that Terraform generates it
automatically rather than it being written out explicitly in the
configuration.
In return for wiring that in, we can now remove altogether the
NodeCountBoundary graph node type and its associated graph transformer,
CountBoundaryTransformer. We handle moves as a preprocessing step before
building the plan graph, so we no longer need to include any special nodes
in the graph to deal with that situation.
The test updates here are mainly for the graph builders themselves, to
acknowledge that indeed we're no longer inserting the NodeCountBoundary
vertices. The vertices that NodeCountBoundary previously depended on now
become dependencies of the special "root" vertex, although in many cases
here we don't see that explicitly because of the transitive reduction
algorithm, which notices when there's already an equivalent indirect
dependency chain and removes the redundant edge.
We already have plenty of test coverage for these "count boundary" cases
in the context tests whose names start with TestContext2Plan_count and
TestContext2Apply_resourceCount, all of which continued to pass here
without any modification and so are not visible in the diff. The test
functions particularly relevant to this situation are:
- TestContext2Plan_countIncreaseFromNotSet
- TestContext2Plan_countDecreaseToOne
- TestContext2Plan_countOneIndex
- TestContext2Apply_countDecreaseToOneCorrupted
The last of those in particular deals with the situation where we have
both a no-key instance _and_ a zero-key instance in the prior state, which
is interesting here because to exercises an intentional interaction
between refactoring.ImpliedMoveStatements and refactoring.ApplyMoves,
where we intentionally generate an implied move statement that produces
a collision and then expect ApplyMoves to deal with it in the same way as
it would deal with all other collisions, and thus ensure we handle both
the explicit and implied collisions in the same way.
This does affect some UI-level tests, because a nice side-effect of this
new treatment of this old feature is that we can now report explicitly
in the UI that we're assigning new addresses to these objects, whereas
before we just said nothing and hoped the user would just guess what had
happened and why they therefore weren't seeing a diff.
The backend/local plan tests actually had a pre-existing bug where they
were using a state with a different instance key than the config called
for but getting away with it because we'd previously silently fix it up.
That's still fixed up, but now done with an explicit mention in the UI
and so I made the state consistent with the configuration here so that the
tests would be able to recognize _real_ differences where present, as
opposed to the errant difference caused by that inconsistency.
2021-09-17 17:32:32 -05:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
2023-09-20 06:35:35 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/addrs"
|
|
|
|
"github.com/opentofu/opentofu/internal/configs/configschema"
|
|
|
|
"github.com/opentofu/opentofu/internal/providers"
|
2016-11-05 19:58:38 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPlanGraphBuilder_impl(t *testing.T) {
|
|
|
|
var _ GraphBuilder = new(PlanGraphBuilder)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPlanGraphBuilder(t *testing.T) {
|
2018-08-17 14:32:35 -05:00
|
|
|
awsProvider := &MockProvider{
|
2021-02-18 09:13:43 -06:00
|
|
|
GetProviderSchemaResponse: &providers.GetProviderSchemaResponse{
|
2021-01-11 14:45:50 -06:00
|
|
|
Provider: providers.Schema{Block: simpleTestSchema()},
|
|
|
|
ResourceTypes: map[string]providers.Schema{
|
|
|
|
"aws_security_group": {Block: simpleTestSchema()},
|
|
|
|
"aws_instance": {Block: simpleTestSchema()},
|
|
|
|
"aws_load_balancer": {Block: simpleTestSchema()},
|
2018-05-31 14:39:45 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-01-11 14:45:50 -06:00
|
|
|
openstackProvider := mockProviderWithResourceTypeSchema("openstack_floating_ip", simpleTestSchema())
|
2021-08-31 12:58:05 -05:00
|
|
|
plugins := newContextPlugins(map[addrs.Provider]providers.Factory{
|
|
|
|
addrs.NewDefaultProvider("aws"): providers.FactoryFixed(awsProvider),
|
|
|
|
addrs.NewDefaultProvider("openstack"): providers.FactoryFixed(openstackProvider),
|
|
|
|
}, nil)
|
2018-05-31 14:39:45 -05:00
|
|
|
|
2016-11-05 19:58:38 -05:00
|
|
|
b := &PlanGraphBuilder{
|
2022-06-06 13:46:59 -05:00
|
|
|
Config: testModule(t, "graph-builder-plan-basic"),
|
|
|
|
Plugins: plugins,
|
|
|
|
Operation: walkPlan,
|
2016-11-05 19:58:38 -05:00
|
|
|
}
|
|
|
|
|
2018-05-04 21:24:06 -05:00
|
|
|
g, err := b.Build(addrs.RootModuleInstance)
|
2016-11-05 19:58:38 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-05-04 21:24:06 -05:00
|
|
|
if g.Path.String() != addrs.RootModuleInstance.String() {
|
|
|
|
t.Fatalf("wrong module path %q", g.Path)
|
2016-11-05 19:58:38 -05:00
|
|
|
}
|
|
|
|
|
core: refactoring.ImpliedMoveStatements replaces NodeCountBoundary
Going back a long time we've had a special magic behavior which tries to
recognize a situation where a module author either added or removed the
"count" argument from a resource that already has instances, and to
silently rename the zeroth or no-key instance so that we don't plan to
destroy and recreate the associated object.
Now we have a more general idea of "move statements", and specifically
the idea of "implied" move statements which replicates the same heuristic
we used to use for this behavior, we can treat this magic renaming rule as
just another "move statement", special only in that Terraform generates it
automatically rather than it being written out explicitly in the
configuration.
In return for wiring that in, we can now remove altogether the
NodeCountBoundary graph node type and its associated graph transformer,
CountBoundaryTransformer. We handle moves as a preprocessing step before
building the plan graph, so we no longer need to include any special nodes
in the graph to deal with that situation.
The test updates here are mainly for the graph builders themselves, to
acknowledge that indeed we're no longer inserting the NodeCountBoundary
vertices. The vertices that NodeCountBoundary previously depended on now
become dependencies of the special "root" vertex, although in many cases
here we don't see that explicitly because of the transitive reduction
algorithm, which notices when there's already an equivalent indirect
dependency chain and removes the redundant edge.
We already have plenty of test coverage for these "count boundary" cases
in the context tests whose names start with TestContext2Plan_count and
TestContext2Apply_resourceCount, all of which continued to pass here
without any modification and so are not visible in the diff. The test
functions particularly relevant to this situation are:
- TestContext2Plan_countIncreaseFromNotSet
- TestContext2Plan_countDecreaseToOne
- TestContext2Plan_countOneIndex
- TestContext2Apply_countDecreaseToOneCorrupted
The last of those in particular deals with the situation where we have
both a no-key instance _and_ a zero-key instance in the prior state, which
is interesting here because to exercises an intentional interaction
between refactoring.ImpliedMoveStatements and refactoring.ApplyMoves,
where we intentionally generate an implied move statement that produces
a collision and then expect ApplyMoves to deal with it in the same way as
it would deal with all other collisions, and thus ensure we handle both
the explicit and implied collisions in the same way.
This does affect some UI-level tests, because a nice side-effect of this
new treatment of this old feature is that we can now report explicitly
in the UI that we're assigning new addresses to these objects, whereas
before we just said nothing and hoped the user would just guess what had
happened and why they therefore weren't seeing a diff.
The backend/local plan tests actually had a pre-existing bug where they
were using a state with a different instance key than the config called
for but getting away with it because we'd previously silently fix it up.
That's still fixed up, but now done with an explicit mention in the UI
and so I made the state consistent with the configuration here so that the
tests would be able to recognize _real_ differences where present, as
opposed to the errant difference caused by that inconsistency.
2021-09-17 17:32:32 -05:00
|
|
|
got := strings.TrimSpace(g.String())
|
|
|
|
want := strings.TrimSpace(testPlanGraphBuilderStr)
|
|
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
|
|
t.Fatalf("wrong result\n%s", diff)
|
2016-11-05 19:58:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 19:15:23 -05:00
|
|
|
func TestPlanGraphBuilder_dynamicBlock(t *testing.T) {
|
2021-01-11 14:45:50 -06:00
|
|
|
provider := mockProviderWithResourceTypeSchema("test_thing", &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Computed: true},
|
|
|
|
"list": {Type: cty.List(cty.String), Computed: true},
|
|
|
|
},
|
|
|
|
BlockTypes: map[string]*configschema.NestedBlock{
|
|
|
|
"nested": {
|
|
|
|
Nesting: configschema.NestingList,
|
|
|
|
Block: configschema.Block{
|
2019-03-18 19:15:23 -05:00
|
|
|
Attributes: map[string]*configschema.Attribute{
|
2021-01-11 14:45:50 -06:00
|
|
|
"foo": {Type: cty.String, Optional: true},
|
2019-03-18 19:15:23 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-01-11 14:45:50 -06:00
|
|
|
})
|
2021-08-31 12:58:05 -05:00
|
|
|
plugins := newContextPlugins(map[addrs.Provider]providers.Factory{
|
|
|
|
addrs.NewDefaultProvider("test"): providers.FactoryFixed(provider),
|
|
|
|
}, nil)
|
2019-03-18 19:15:23 -05:00
|
|
|
|
|
|
|
b := &PlanGraphBuilder{
|
2022-06-06 13:46:59 -05:00
|
|
|
Config: testModule(t, "graph-builder-plan-dynblock"),
|
|
|
|
Plugins: plugins,
|
|
|
|
Operation: walkPlan,
|
2019-03-18 19:15:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
g, err := b.Build(addrs.RootModuleInstance)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if g.Path.String() != addrs.RootModuleInstance.String() {
|
|
|
|
t.Fatalf("wrong module path %q", g.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test is here to make sure we properly detect references inside
|
|
|
|
// the special "dynamic" block construct. The most important thing here
|
|
|
|
// is that at the end test_thing.c depends on both test_thing.a and
|
|
|
|
// test_thing.b. Other details might shift over time as other logic in
|
|
|
|
// the graph builders changes.
|
core: refactoring.ImpliedMoveStatements replaces NodeCountBoundary
Going back a long time we've had a special magic behavior which tries to
recognize a situation where a module author either added or removed the
"count" argument from a resource that already has instances, and to
silently rename the zeroth or no-key instance so that we don't plan to
destroy and recreate the associated object.
Now we have a more general idea of "move statements", and specifically
the idea of "implied" move statements which replicates the same heuristic
we used to use for this behavior, we can treat this magic renaming rule as
just another "move statement", special only in that Terraform generates it
automatically rather than it being written out explicitly in the
configuration.
In return for wiring that in, we can now remove altogether the
NodeCountBoundary graph node type and its associated graph transformer,
CountBoundaryTransformer. We handle moves as a preprocessing step before
building the plan graph, so we no longer need to include any special nodes
in the graph to deal with that situation.
The test updates here are mainly for the graph builders themselves, to
acknowledge that indeed we're no longer inserting the NodeCountBoundary
vertices. The vertices that NodeCountBoundary previously depended on now
become dependencies of the special "root" vertex, although in many cases
here we don't see that explicitly because of the transitive reduction
algorithm, which notices when there's already an equivalent indirect
dependency chain and removes the redundant edge.
We already have plenty of test coverage for these "count boundary" cases
in the context tests whose names start with TestContext2Plan_count and
TestContext2Apply_resourceCount, all of which continued to pass here
without any modification and so are not visible in the diff. The test
functions particularly relevant to this situation are:
- TestContext2Plan_countIncreaseFromNotSet
- TestContext2Plan_countDecreaseToOne
- TestContext2Plan_countOneIndex
- TestContext2Apply_countDecreaseToOneCorrupted
The last of those in particular deals with the situation where we have
both a no-key instance _and_ a zero-key instance in the prior state, which
is interesting here because to exercises an intentional interaction
between refactoring.ImpliedMoveStatements and refactoring.ApplyMoves,
where we intentionally generate an implied move statement that produces
a collision and then expect ApplyMoves to deal with it in the same way as
it would deal with all other collisions, and thus ensure we handle both
the explicit and implied collisions in the same way.
This does affect some UI-level tests, because a nice side-effect of this
new treatment of this old feature is that we can now report explicitly
in the UI that we're assigning new addresses to these objects, whereas
before we just said nothing and hoped the user would just guess what had
happened and why they therefore weren't seeing a diff.
The backend/local plan tests actually had a pre-existing bug where they
were using a state with a different instance key than the config called
for but getting away with it because we'd previously silently fix it up.
That's still fixed up, but now done with an explicit mention in the UI
and so I made the state consistent with the configuration here so that the
tests would be able to recognize _real_ differences where present, as
opposed to the errant difference caused by that inconsistency.
2021-09-17 17:32:32 -05:00
|
|
|
got := strings.TrimSpace(g.String())
|
|
|
|
want := strings.TrimSpace(`
|
2023-10-03 02:49:38 -05:00
|
|
|
provider["registry.opentofu.org/hashicorp/test"]
|
|
|
|
provider["registry.opentofu.org/hashicorp/test"] (close)
|
2020-05-12 09:54:43 -05:00
|
|
|
test_thing.c (expand)
|
2019-03-18 19:15:23 -05:00
|
|
|
root
|
2023-10-03 02:49:38 -05:00
|
|
|
provider["registry.opentofu.org/hashicorp/test"] (close)
|
2020-05-12 09:54:43 -05:00
|
|
|
test_thing.a (expand)
|
2023-10-03 02:49:38 -05:00
|
|
|
provider["registry.opentofu.org/hashicorp/test"]
|
2020-05-12 09:54:43 -05:00
|
|
|
test_thing.b (expand)
|
2023-10-03 02:49:38 -05:00
|
|
|
provider["registry.opentofu.org/hashicorp/test"]
|
2020-05-12 09:54:43 -05:00
|
|
|
test_thing.c (expand)
|
|
|
|
test_thing.a (expand)
|
|
|
|
test_thing.b (expand)
|
2019-03-18 19:15:23 -05:00
|
|
|
`)
|
core: refactoring.ImpliedMoveStatements replaces NodeCountBoundary
Going back a long time we've had a special magic behavior which tries to
recognize a situation where a module author either added or removed the
"count" argument from a resource that already has instances, and to
silently rename the zeroth or no-key instance so that we don't plan to
destroy and recreate the associated object.
Now we have a more general idea of "move statements", and specifically
the idea of "implied" move statements which replicates the same heuristic
we used to use for this behavior, we can treat this magic renaming rule as
just another "move statement", special only in that Terraform generates it
automatically rather than it being written out explicitly in the
configuration.
In return for wiring that in, we can now remove altogether the
NodeCountBoundary graph node type and its associated graph transformer,
CountBoundaryTransformer. We handle moves as a preprocessing step before
building the plan graph, so we no longer need to include any special nodes
in the graph to deal with that situation.
The test updates here are mainly for the graph builders themselves, to
acknowledge that indeed we're no longer inserting the NodeCountBoundary
vertices. The vertices that NodeCountBoundary previously depended on now
become dependencies of the special "root" vertex, although in many cases
here we don't see that explicitly because of the transitive reduction
algorithm, which notices when there's already an equivalent indirect
dependency chain and removes the redundant edge.
We already have plenty of test coverage for these "count boundary" cases
in the context tests whose names start with TestContext2Plan_count and
TestContext2Apply_resourceCount, all of which continued to pass here
without any modification and so are not visible in the diff. The test
functions particularly relevant to this situation are:
- TestContext2Plan_countIncreaseFromNotSet
- TestContext2Plan_countDecreaseToOne
- TestContext2Plan_countOneIndex
- TestContext2Apply_countDecreaseToOneCorrupted
The last of those in particular deals with the situation where we have
both a no-key instance _and_ a zero-key instance in the prior state, which
is interesting here because to exercises an intentional interaction
between refactoring.ImpliedMoveStatements and refactoring.ApplyMoves,
where we intentionally generate an implied move statement that produces
a collision and then expect ApplyMoves to deal with it in the same way as
it would deal with all other collisions, and thus ensure we handle both
the explicit and implied collisions in the same way.
This does affect some UI-level tests, because a nice side-effect of this
new treatment of this old feature is that we can now report explicitly
in the UI that we're assigning new addresses to these objects, whereas
before we just said nothing and hoped the user would just guess what had
happened and why they therefore weren't seeing a diff.
The backend/local plan tests actually had a pre-existing bug where they
were using a state with a different instance key than the config called
for but getting away with it because we'd previously silently fix it up.
That's still fixed up, but now done with an explicit mention in the UI
and so I made the state consistent with the configuration here so that the
tests would be able to recognize _real_ differences where present, as
opposed to the errant difference caused by that inconsistency.
2021-09-17 17:32:32 -05:00
|
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
|
|
t.Fatalf("wrong result\n%s", diff)
|
2019-03-18 19:15:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 12:07:16 -05:00
|
|
|
func TestPlanGraphBuilder_attrAsBlocks(t *testing.T) {
|
2021-01-11 14:45:50 -06:00
|
|
|
provider := mockProviderWithResourceTypeSchema("test_thing", &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Computed: true},
|
|
|
|
"nested": {
|
|
|
|
Type: cty.List(cty.Object(map[string]cty.Type{
|
|
|
|
"foo": cty.String,
|
|
|
|
})),
|
|
|
|
Optional: true,
|
2019-03-28 12:07:16 -05:00
|
|
|
},
|
|
|
|
},
|
2021-01-11 14:45:50 -06:00
|
|
|
})
|
2021-08-31 12:58:05 -05:00
|
|
|
plugins := newContextPlugins(map[addrs.Provider]providers.Factory{
|
|
|
|
addrs.NewDefaultProvider("test"): providers.FactoryFixed(provider),
|
|
|
|
}, nil)
|
2019-03-28 12:07:16 -05:00
|
|
|
|
|
|
|
b := &PlanGraphBuilder{
|
2022-06-06 13:46:59 -05:00
|
|
|
Config: testModule(t, "graph-builder-plan-attr-as-blocks"),
|
|
|
|
Plugins: plugins,
|
|
|
|
Operation: walkPlan,
|
2019-03-28 12:07:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
g, err := b.Build(addrs.RootModuleInstance)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if g.Path.String() != addrs.RootModuleInstance.String() {
|
|
|
|
t.Fatalf("wrong module path %q", g.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test is here to make sure we properly detect references inside
|
|
|
|
// the "nested" block that is actually defined in the schema as a
|
|
|
|
// list-of-objects attribute. This requires some special effort
|
|
|
|
// inside lang.ReferencesInBlock to make sure it searches blocks of
|
|
|
|
// type "nested" along with an attribute named "nested".
|
core: refactoring.ImpliedMoveStatements replaces NodeCountBoundary
Going back a long time we've had a special magic behavior which tries to
recognize a situation where a module author either added or removed the
"count" argument from a resource that already has instances, and to
silently rename the zeroth or no-key instance so that we don't plan to
destroy and recreate the associated object.
Now we have a more general idea of "move statements", and specifically
the idea of "implied" move statements which replicates the same heuristic
we used to use for this behavior, we can treat this magic renaming rule as
just another "move statement", special only in that Terraform generates it
automatically rather than it being written out explicitly in the
configuration.
In return for wiring that in, we can now remove altogether the
NodeCountBoundary graph node type and its associated graph transformer,
CountBoundaryTransformer. We handle moves as a preprocessing step before
building the plan graph, so we no longer need to include any special nodes
in the graph to deal with that situation.
The test updates here are mainly for the graph builders themselves, to
acknowledge that indeed we're no longer inserting the NodeCountBoundary
vertices. The vertices that NodeCountBoundary previously depended on now
become dependencies of the special "root" vertex, although in many cases
here we don't see that explicitly because of the transitive reduction
algorithm, which notices when there's already an equivalent indirect
dependency chain and removes the redundant edge.
We already have plenty of test coverage for these "count boundary" cases
in the context tests whose names start with TestContext2Plan_count and
TestContext2Apply_resourceCount, all of which continued to pass here
without any modification and so are not visible in the diff. The test
functions particularly relevant to this situation are:
- TestContext2Plan_countIncreaseFromNotSet
- TestContext2Plan_countDecreaseToOne
- TestContext2Plan_countOneIndex
- TestContext2Apply_countDecreaseToOneCorrupted
The last of those in particular deals with the situation where we have
both a no-key instance _and_ a zero-key instance in the prior state, which
is interesting here because to exercises an intentional interaction
between refactoring.ImpliedMoveStatements and refactoring.ApplyMoves,
where we intentionally generate an implied move statement that produces
a collision and then expect ApplyMoves to deal with it in the same way as
it would deal with all other collisions, and thus ensure we handle both
the explicit and implied collisions in the same way.
This does affect some UI-level tests, because a nice side-effect of this
new treatment of this old feature is that we can now report explicitly
in the UI that we're assigning new addresses to these objects, whereas
before we just said nothing and hoped the user would just guess what had
happened and why they therefore weren't seeing a diff.
The backend/local plan tests actually had a pre-existing bug where they
were using a state with a different instance key than the config called
for but getting away with it because we'd previously silently fix it up.
That's still fixed up, but now done with an explicit mention in the UI
and so I made the state consistent with the configuration here so that the
tests would be able to recognize _real_ differences where present, as
opposed to the errant difference caused by that inconsistency.
2021-09-17 17:32:32 -05:00
|
|
|
got := strings.TrimSpace(g.String())
|
|
|
|
want := strings.TrimSpace(`
|
2023-10-03 02:49:38 -05:00
|
|
|
provider["registry.opentofu.org/hashicorp/test"]
|
|
|
|
provider["registry.opentofu.org/hashicorp/test"] (close)
|
2020-05-12 09:54:43 -05:00
|
|
|
test_thing.b (expand)
|
2019-03-28 12:07:16 -05:00
|
|
|
root
|
2023-10-03 02:49:38 -05:00
|
|
|
provider["registry.opentofu.org/hashicorp/test"] (close)
|
2020-05-12 09:54:43 -05:00
|
|
|
test_thing.a (expand)
|
2023-10-03 02:49:38 -05:00
|
|
|
provider["registry.opentofu.org/hashicorp/test"]
|
2020-05-12 09:54:43 -05:00
|
|
|
test_thing.b (expand)
|
|
|
|
test_thing.a (expand)
|
2019-03-28 12:07:16 -05:00
|
|
|
`)
|
core: refactoring.ImpliedMoveStatements replaces NodeCountBoundary
Going back a long time we've had a special magic behavior which tries to
recognize a situation where a module author either added or removed the
"count" argument from a resource that already has instances, and to
silently rename the zeroth or no-key instance so that we don't plan to
destroy and recreate the associated object.
Now we have a more general idea of "move statements", and specifically
the idea of "implied" move statements which replicates the same heuristic
we used to use for this behavior, we can treat this magic renaming rule as
just another "move statement", special only in that Terraform generates it
automatically rather than it being written out explicitly in the
configuration.
In return for wiring that in, we can now remove altogether the
NodeCountBoundary graph node type and its associated graph transformer,
CountBoundaryTransformer. We handle moves as a preprocessing step before
building the plan graph, so we no longer need to include any special nodes
in the graph to deal with that situation.
The test updates here are mainly for the graph builders themselves, to
acknowledge that indeed we're no longer inserting the NodeCountBoundary
vertices. The vertices that NodeCountBoundary previously depended on now
become dependencies of the special "root" vertex, although in many cases
here we don't see that explicitly because of the transitive reduction
algorithm, which notices when there's already an equivalent indirect
dependency chain and removes the redundant edge.
We already have plenty of test coverage for these "count boundary" cases
in the context tests whose names start with TestContext2Plan_count and
TestContext2Apply_resourceCount, all of which continued to pass here
without any modification and so are not visible in the diff. The test
functions particularly relevant to this situation are:
- TestContext2Plan_countIncreaseFromNotSet
- TestContext2Plan_countDecreaseToOne
- TestContext2Plan_countOneIndex
- TestContext2Apply_countDecreaseToOneCorrupted
The last of those in particular deals with the situation where we have
both a no-key instance _and_ a zero-key instance in the prior state, which
is interesting here because to exercises an intentional interaction
between refactoring.ImpliedMoveStatements and refactoring.ApplyMoves,
where we intentionally generate an implied move statement that produces
a collision and then expect ApplyMoves to deal with it in the same way as
it would deal with all other collisions, and thus ensure we handle both
the explicit and implied collisions in the same way.
This does affect some UI-level tests, because a nice side-effect of this
new treatment of this old feature is that we can now report explicitly
in the UI that we're assigning new addresses to these objects, whereas
before we just said nothing and hoped the user would just guess what had
happened and why they therefore weren't seeing a diff.
The backend/local plan tests actually had a pre-existing bug where they
were using a state with a different instance key than the config called
for but getting away with it because we'd previously silently fix it up.
That's still fixed up, but now done with an explicit mention in the UI
and so I made the state consistent with the configuration here so that the
tests would be able to recognize _real_ differences where present, as
opposed to the errant difference caused by that inconsistency.
2021-09-17 17:32:32 -05:00
|
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
|
|
t.Fatalf("wrong result\n%s", diff)
|
2019-03-28 12:07:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-17 11:21:29 -06:00
|
|
|
func TestPlanGraphBuilder_targetModule(t *testing.T) {
|
|
|
|
b := &PlanGraphBuilder{
|
2021-08-31 12:58:05 -05:00
|
|
|
Config: testModule(t, "graph-builder-plan-target-module-provider"),
|
|
|
|
Plugins: simpleMockPluginLibrary(),
|
2018-05-04 21:24:06 -05:00
|
|
|
Targets: []addrs.Targetable{
|
|
|
|
addrs.RootModuleInstance.Child("child2", addrs.NoKey),
|
|
|
|
},
|
2022-06-06 13:46:59 -05:00
|
|
|
Operation: walkPlan,
|
2017-02-17 11:21:29 -06:00
|
|
|
}
|
|
|
|
|
2018-05-04 21:24:06 -05:00
|
|
|
g, err := b.Build(addrs.RootModuleInstance)
|
2017-02-17 11:21:29 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Graph: %s", g.String())
|
|
|
|
|
2023-10-03 02:49:38 -05:00
|
|
|
testGraphNotContains(t, g, `module.child1.provider["registry.opentofu.org/hashicorp/test"]`)
|
2018-05-10 11:56:32 -05:00
|
|
|
testGraphNotContains(t, g, "module.child1.test_object.foo")
|
2017-02-17 11:21:29 -06:00
|
|
|
}
|
|
|
|
|
2019-06-12 10:07:32 -05:00
|
|
|
func TestPlanGraphBuilder_forEach(t *testing.T) {
|
2021-01-11 14:45:50 -06:00
|
|
|
awsProvider := mockProviderWithResourceTypeSchema("aws_instance", simpleTestSchema())
|
2019-06-12 10:07:32 -05:00
|
|
|
|
2021-08-31 12:58:05 -05:00
|
|
|
plugins := newContextPlugins(map[addrs.Provider]providers.Factory{
|
|
|
|
addrs.NewDefaultProvider("aws"): providers.FactoryFixed(awsProvider),
|
|
|
|
}, nil)
|
2019-06-12 10:07:32 -05:00
|
|
|
|
|
|
|
b := &PlanGraphBuilder{
|
2022-06-06 13:46:59 -05:00
|
|
|
Config: testModule(t, "plan-for-each"),
|
|
|
|
Plugins: plugins,
|
|
|
|
Operation: walkPlan,
|
2019-06-12 10:07:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
g, err := b.Build(addrs.RootModuleInstance)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if g.Path.String() != addrs.RootModuleInstance.String() {
|
|
|
|
t.Fatalf("wrong module path %q", g.Path)
|
|
|
|
}
|
|
|
|
|
core: refactoring.ImpliedMoveStatements replaces NodeCountBoundary
Going back a long time we've had a special magic behavior which tries to
recognize a situation where a module author either added or removed the
"count" argument from a resource that already has instances, and to
silently rename the zeroth or no-key instance so that we don't plan to
destroy and recreate the associated object.
Now we have a more general idea of "move statements", and specifically
the idea of "implied" move statements which replicates the same heuristic
we used to use for this behavior, we can treat this magic renaming rule as
just another "move statement", special only in that Terraform generates it
automatically rather than it being written out explicitly in the
configuration.
In return for wiring that in, we can now remove altogether the
NodeCountBoundary graph node type and its associated graph transformer,
CountBoundaryTransformer. We handle moves as a preprocessing step before
building the plan graph, so we no longer need to include any special nodes
in the graph to deal with that situation.
The test updates here are mainly for the graph builders themselves, to
acknowledge that indeed we're no longer inserting the NodeCountBoundary
vertices. The vertices that NodeCountBoundary previously depended on now
become dependencies of the special "root" vertex, although in many cases
here we don't see that explicitly because of the transitive reduction
algorithm, which notices when there's already an equivalent indirect
dependency chain and removes the redundant edge.
We already have plenty of test coverage for these "count boundary" cases
in the context tests whose names start with TestContext2Plan_count and
TestContext2Apply_resourceCount, all of which continued to pass here
without any modification and so are not visible in the diff. The test
functions particularly relevant to this situation are:
- TestContext2Plan_countIncreaseFromNotSet
- TestContext2Plan_countDecreaseToOne
- TestContext2Plan_countOneIndex
- TestContext2Apply_countDecreaseToOneCorrupted
The last of those in particular deals with the situation where we have
both a no-key instance _and_ a zero-key instance in the prior state, which
is interesting here because to exercises an intentional interaction
between refactoring.ImpliedMoveStatements and refactoring.ApplyMoves,
where we intentionally generate an implied move statement that produces
a collision and then expect ApplyMoves to deal with it in the same way as
it would deal with all other collisions, and thus ensure we handle both
the explicit and implied collisions in the same way.
This does affect some UI-level tests, because a nice side-effect of this
new treatment of this old feature is that we can now report explicitly
in the UI that we're assigning new addresses to these objects, whereas
before we just said nothing and hoped the user would just guess what had
happened and why they therefore weren't seeing a diff.
The backend/local plan tests actually had a pre-existing bug where they
were using a state with a different instance key than the config called
for but getting away with it because we'd previously silently fix it up.
That's still fixed up, but now done with an explicit mention in the UI
and so I made the state consistent with the configuration here so that the
tests would be able to recognize _real_ differences where present, as
opposed to the errant difference caused by that inconsistency.
2021-09-17 17:32:32 -05:00
|
|
|
got := strings.TrimSpace(g.String())
|
2019-06-12 10:07:32 -05:00
|
|
|
// We're especially looking for the edge here, where aws_instance.bat
|
|
|
|
// has a dependency on aws_instance.boo
|
core: refactoring.ImpliedMoveStatements replaces NodeCountBoundary
Going back a long time we've had a special magic behavior which tries to
recognize a situation where a module author either added or removed the
"count" argument from a resource that already has instances, and to
silently rename the zeroth or no-key instance so that we don't plan to
destroy and recreate the associated object.
Now we have a more general idea of "move statements", and specifically
the idea of "implied" move statements which replicates the same heuristic
we used to use for this behavior, we can treat this magic renaming rule as
just another "move statement", special only in that Terraform generates it
automatically rather than it being written out explicitly in the
configuration.
In return for wiring that in, we can now remove altogether the
NodeCountBoundary graph node type and its associated graph transformer,
CountBoundaryTransformer. We handle moves as a preprocessing step before
building the plan graph, so we no longer need to include any special nodes
in the graph to deal with that situation.
The test updates here are mainly for the graph builders themselves, to
acknowledge that indeed we're no longer inserting the NodeCountBoundary
vertices. The vertices that NodeCountBoundary previously depended on now
become dependencies of the special "root" vertex, although in many cases
here we don't see that explicitly because of the transitive reduction
algorithm, which notices when there's already an equivalent indirect
dependency chain and removes the redundant edge.
We already have plenty of test coverage for these "count boundary" cases
in the context tests whose names start with TestContext2Plan_count and
TestContext2Apply_resourceCount, all of which continued to pass here
without any modification and so are not visible in the diff. The test
functions particularly relevant to this situation are:
- TestContext2Plan_countIncreaseFromNotSet
- TestContext2Plan_countDecreaseToOne
- TestContext2Plan_countOneIndex
- TestContext2Apply_countDecreaseToOneCorrupted
The last of those in particular deals with the situation where we have
both a no-key instance _and_ a zero-key instance in the prior state, which
is interesting here because to exercises an intentional interaction
between refactoring.ImpliedMoveStatements and refactoring.ApplyMoves,
where we intentionally generate an implied move statement that produces
a collision and then expect ApplyMoves to deal with it in the same way as
it would deal with all other collisions, and thus ensure we handle both
the explicit and implied collisions in the same way.
This does affect some UI-level tests, because a nice side-effect of this
new treatment of this old feature is that we can now report explicitly
in the UI that we're assigning new addresses to these objects, whereas
before we just said nothing and hoped the user would just guess what had
happened and why they therefore weren't seeing a diff.
The backend/local plan tests actually had a pre-existing bug where they
were using a state with a different instance key than the config called
for but getting away with it because we'd previously silently fix it up.
That's still fixed up, but now done with an explicit mention in the UI
and so I made the state consistent with the configuration here so that the
tests would be able to recognize _real_ differences where present, as
opposed to the errant difference caused by that inconsistency.
2021-09-17 17:32:32 -05:00
|
|
|
want := strings.TrimSpace(testPlanGraphBuilderForEachStr)
|
|
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
|
|
t.Fatalf("wrong result\n%s", diff)
|
2019-06-12 10:07:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-05 19:58:38 -05:00
|
|
|
const testPlanGraphBuilderStr = `
|
2020-05-12 09:54:43 -05:00
|
|
|
aws_instance.web (expand)
|
|
|
|
aws_security_group.firewall (expand)
|
2016-11-05 19:58:38 -05:00
|
|
|
var.foo
|
2020-05-12 09:54:43 -05:00
|
|
|
aws_load_balancer.weblb (expand)
|
|
|
|
aws_instance.web (expand)
|
|
|
|
aws_security_group.firewall (expand)
|
2023-10-03 02:49:38 -05:00
|
|
|
provider["registry.opentofu.org/hashicorp/aws"]
|
2020-05-12 09:54:43 -05:00
|
|
|
local.instance_id (expand)
|
|
|
|
aws_instance.web (expand)
|
|
|
|
openstack_floating_ip.random (expand)
|
2023-10-03 02:49:38 -05:00
|
|
|
provider["registry.opentofu.org/hashicorp/openstack"]
|
2022-06-15 21:03:10 -05:00
|
|
|
output.instance_id (expand)
|
2020-05-12 09:54:43 -05:00
|
|
|
local.instance_id (expand)
|
2023-10-03 02:49:38 -05:00
|
|
|
provider["registry.opentofu.org/hashicorp/aws"]
|
2020-05-12 09:54:43 -05:00
|
|
|
openstack_floating_ip.random (expand)
|
2023-10-03 02:49:38 -05:00
|
|
|
provider["registry.opentofu.org/hashicorp/aws"] (close)
|
2020-05-12 09:54:43 -05:00
|
|
|
aws_load_balancer.weblb (expand)
|
2023-10-03 02:49:38 -05:00
|
|
|
provider["registry.opentofu.org/hashicorp/openstack"]
|
|
|
|
provider["registry.opentofu.org/hashicorp/openstack"] (close)
|
2020-05-12 09:54:43 -05:00
|
|
|
openstack_floating_ip.random (expand)
|
2017-04-12 16:25:15 -05:00
|
|
|
root
|
2022-06-15 21:03:10 -05:00
|
|
|
output.instance_id (expand)
|
2023-10-03 02:49:38 -05:00
|
|
|
provider["registry.opentofu.org/hashicorp/aws"] (close)
|
|
|
|
provider["registry.opentofu.org/hashicorp/openstack"] (close)
|
2016-11-05 19:58:38 -05:00
|
|
|
var.foo
|
|
|
|
`
|
2019-06-12 10:07:32 -05:00
|
|
|
const testPlanGraphBuilderForEachStr = `
|
2020-05-12 09:54:43 -05:00
|
|
|
aws_instance.bar (expand)
|
2023-10-03 02:49:38 -05:00
|
|
|
provider["registry.opentofu.org/hashicorp/aws"]
|
2020-05-12 09:54:43 -05:00
|
|
|
aws_instance.bar2 (expand)
|
2023-10-03 02:49:38 -05:00
|
|
|
provider["registry.opentofu.org/hashicorp/aws"]
|
2020-05-12 09:54:43 -05:00
|
|
|
aws_instance.bat (expand)
|
|
|
|
aws_instance.boo (expand)
|
|
|
|
aws_instance.baz (expand)
|
2023-10-03 02:49:38 -05:00
|
|
|
provider["registry.opentofu.org/hashicorp/aws"]
|
2020-05-12 09:54:43 -05:00
|
|
|
aws_instance.boo (expand)
|
2023-10-03 02:49:38 -05:00
|
|
|
provider["registry.opentofu.org/hashicorp/aws"]
|
2020-05-12 09:54:43 -05:00
|
|
|
aws_instance.foo (expand)
|
2023-10-03 02:49:38 -05:00
|
|
|
provider["registry.opentofu.org/hashicorp/aws"]
|
|
|
|
provider["registry.opentofu.org/hashicorp/aws"]
|
|
|
|
provider["registry.opentofu.org/hashicorp/aws"] (close)
|
2020-05-12 09:54:43 -05:00
|
|
|
aws_instance.bar (expand)
|
|
|
|
aws_instance.bar2 (expand)
|
|
|
|
aws_instance.bat (expand)
|
|
|
|
aws_instance.baz (expand)
|
|
|
|
aws_instance.foo (expand)
|
2019-06-12 10:07:32 -05:00
|
|
|
root
|
2023-10-03 02:49:38 -05:00
|
|
|
provider["registry.opentofu.org/hashicorp/aws"] (close)
|
2019-06-12 10:07:32 -05:00
|
|
|
`
|