2016-09-14 13:44:41 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2018-05-04 21:24:06 -05: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 16:24:45 -05:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
2021-05-17 14:00:50 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
2021-05-17 14:33:17 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/plans"
|
2016-09-14 13:44:41 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDiffTransformer_nilDiff(t *testing.T) {
|
2018-05-04 21:24:06 -05:00
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
2016-09-14 13:44:41 -05:00
|
|
|
tf := &DiffTransformer{}
|
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(g.Vertices()) > 0 {
|
|
|
|
t.Fatal("graph should be empty")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDiffTransformer(t *testing.T) {
|
2018-05-04 21:24:06 -05:00
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
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 16:24:45 -05:00
|
|
|
|
|
|
|
beforeVal, err := plans.NewDynamicValue(cty.StringVal(""), cty.String)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
afterVal, err := plans.NewDynamicValue(cty.StringVal(""), cty.String)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-09-14 13:44:41 -05:00
|
|
|
tf := &DiffTransformer{
|
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 16:24:45 -05:00
|
|
|
Changes: &plans.Changes{
|
|
|
|
Resources: []*plans.ResourceInstanceChangeSrc{
|
|
|
|
{
|
|
|
|
Addr: addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
2020-02-13 14:32:58 -06:00
|
|
|
ProviderAddr: addrs.AbsProviderConfig{
|
2020-04-01 12:13:40 -05:00
|
|
|
Provider: addrs.NewDefaultProvider("aws"),
|
2020-03-10 20:21:19 -05:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 14:32:58 -06: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 16:24:45 -05:00
|
|
|
ChangeSrc: plans.ChangeSrc{
|
|
|
|
Action: plans.Update,
|
|
|
|
Before: beforeVal,
|
|
|
|
After: afterVal,
|
2016-09-14 13:44:41 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTransformDiffBasicStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-16 19:41:07 -05:00
|
|
|
func TestDiffTransformer_noOpChange(t *testing.T) {
|
|
|
|
// "No-op" changes are how we record explicitly in a plan that we did
|
|
|
|
// indeed visit a particular resource instance during the planning phase
|
|
|
|
// and concluded that no changes were needed, as opposed to the resource
|
|
|
|
// instance not existing at all or having been excluded from planning
|
|
|
|
// entirely.
|
|
|
|
//
|
|
|
|
// We must include nodes for resource instances with no-op changes in the
|
|
|
|
// apply graph, even though they won't take any external actions, because
|
|
|
|
// there are some secondary effects such as precondition/postcondition
|
|
|
|
// checks that can refer to objects elsewhere and so might have their
|
|
|
|
// results changed even if the resource instance they are attached to
|
|
|
|
// didn't actually change directly itself.
|
|
|
|
|
2022-10-26 11:04:08 -05:00
|
|
|
// aws_instance.foo has a precondition, so should be included in the final
|
|
|
|
// graph. aws_instance.bar has no conditions, so there is nothing to
|
|
|
|
// execute during apply and it should not be included in the graph.
|
|
|
|
m := testModuleInline(t, map[string]string{
|
|
|
|
"main.tf": `
|
|
|
|
resource "aws_instance" "bar" {
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_instance" "foo" {
|
|
|
|
test_string = "ok"
|
|
|
|
|
|
|
|
lifecycle {
|
|
|
|
precondition {
|
|
|
|
condition = self.test_string != ""
|
|
|
|
error_message = "resource error"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`})
|
|
|
|
|
2022-06-16 19:41:07 -05:00
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
|
|
|
|
|
|
|
beforeVal, err := plans.NewDynamicValue(cty.StringVal(""), cty.String)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tf := &DiffTransformer{
|
2022-10-26 11:04:08 -05:00
|
|
|
Config: m,
|
2022-06-16 19:41:07 -05:00
|
|
|
Changes: &plans.Changes{
|
|
|
|
Resources: []*plans.ResourceInstanceChangeSrc{
|
|
|
|
{
|
|
|
|
Addr: addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
ProviderAddr: addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("aws"),
|
|
|
|
Module: addrs.RootModule,
|
|
|
|
},
|
|
|
|
ChangeSrc: plans.ChangeSrc{
|
|
|
|
// A "no-op" change has the no-op action and has the
|
|
|
|
// same object as both Before and After.
|
|
|
|
Action: plans.NoOp,
|
|
|
|
Before: beforeVal,
|
|
|
|
After: beforeVal,
|
|
|
|
},
|
|
|
|
},
|
2022-10-26 11:04:08 -05:00
|
|
|
{
|
|
|
|
Addr: addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "bar",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
ProviderAddr: addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("aws"),
|
|
|
|
Module: addrs.RootModule,
|
|
|
|
},
|
|
|
|
ChangeSrc: plans.ChangeSrc{
|
|
|
|
// A "no-op" change has the no-op action and has the
|
|
|
|
// same object as both Before and After.
|
|
|
|
Action: plans.NoOp,
|
|
|
|
Before: beforeVal,
|
|
|
|
After: beforeVal,
|
|
|
|
},
|
|
|
|
},
|
2022-06-16 19:41:07 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTransformDiffBasicStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-14 13:44:41 -05:00
|
|
|
const testTransformDiffBasicStr = `
|
|
|
|
aws_instance.foo
|
|
|
|
`
|