2018-06-20 01:30:59 +02:00
|
|
|
package planfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/go-test/deep"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
2021-05-17 21:00:50 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
2021-06-24 23:53:43 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/lang/marks"
|
2021-05-17 21:33:17 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/plans"
|
2018-06-20 01:30:59 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTFPlanRoundTrip(t *testing.T) {
|
|
|
|
objTy := cty.Object(map[string]cty.Type{
|
|
|
|
"id": cty.String,
|
|
|
|
})
|
|
|
|
|
|
|
|
plan := &plans.Plan{
|
|
|
|
VariableValues: map[string]plans.DynamicValue{
|
|
|
|
"foo": mustNewDynamicValueStr("foo value"),
|
|
|
|
},
|
|
|
|
Changes: &plans.Changes{
|
2018-09-11 01:26:55 +02:00
|
|
|
Outputs: []*plans.OutputChangeSrc{
|
|
|
|
{
|
|
|
|
Addr: addrs.OutputValue{Name: "bar"}.Absolute(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 23:24:45 +02:00
|
|
|
ChangeSrc: plans.ChangeSrc{
|
2018-06-20 01:30:59 +02:00
|
|
|
Action: plans.Create,
|
2018-12-20 20:32:52 +01:00
|
|
|
After: mustDynamicOutputValue("bar value"),
|
2018-06-20 01:30:59 +02:00
|
|
|
},
|
|
|
|
Sensitive: false,
|
|
|
|
},
|
2018-09-11 01:26:55 +02:00
|
|
|
{
|
|
|
|
Addr: addrs.OutputValue{Name: "baz"}.Absolute(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 23:24:45 +02:00
|
|
|
ChangeSrc: plans.ChangeSrc{
|
2018-06-20 01:30:59 +02:00
|
|
|
Action: plans.NoOp,
|
2018-12-20 20:32:52 +01:00
|
|
|
Before: mustDynamicOutputValue("baz value"),
|
|
|
|
After: mustDynamicOutputValue("baz value"),
|
2018-06-20 01:30:59 +02:00
|
|
|
},
|
|
|
|
Sensitive: false,
|
|
|
|
},
|
2018-09-11 01:26:55 +02:00
|
|
|
{
|
|
|
|
Addr: addrs.OutputValue{Name: "secret"}.Absolute(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 23:24:45 +02:00
|
|
|
ChangeSrc: plans.ChangeSrc{
|
2018-06-20 01:30:59 +02:00
|
|
|
Action: plans.Update,
|
2018-12-20 20:32:52 +01:00
|
|
|
Before: mustDynamicOutputValue("old secret value"),
|
|
|
|
After: mustDynamicOutputValue("new secret value"),
|
2018-06-20 01:30:59 +02:00
|
|
|
},
|
|
|
|
Sensitive: true,
|
|
|
|
},
|
|
|
|
},
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
Resources: []*plans.ResourceInstanceChangeSrc{
|
2018-06-20 01:30:59 +02:00
|
|
|
{
|
|
|
|
Addr: addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "woot",
|
|
|
|
}.Instance(addrs.IntKey(0)).Absolute(addrs.RootModuleInstance),
|
2020-02-13 21:32:58 +01:00
|
|
|
ProviderAddr: addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
ChangeSrc: plans.ChangeSrc{
|
2018-09-29 00:21:39 +02:00
|
|
|
Action: plans.DeleteThenCreate,
|
2018-06-20 01:30:59 +02:00
|
|
|
Before: mustNewDynamicValue(cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.StringVal("foo-bar-baz"),
|
2021-03-24 21:45:46 +01:00
|
|
|
"boop": cty.ListVal([]cty.Value{
|
|
|
|
cty.StringVal("beep"),
|
|
|
|
}),
|
2018-06-20 01:30:59 +02:00
|
|
|
}), objTy),
|
|
|
|
After: mustNewDynamicValue(cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.UnknownVal(cty.String),
|
2021-03-24 21:45:46 +01:00
|
|
|
"boop": cty.ListVal([]cty.Value{
|
|
|
|
cty.StringVal("beep"),
|
|
|
|
cty.StringVal("honk"),
|
|
|
|
}),
|
2018-06-20 01:30:59 +02:00
|
|
|
}), objTy),
|
2021-03-24 21:45:46 +01:00
|
|
|
AfterValMarks: []cty.PathValueMarks{
|
|
|
|
{
|
|
|
|
Path: cty.GetAttrPath("boop").IndexInt(1),
|
2021-06-24 23:53:43 +02:00
|
|
|
Marks: cty.NewValueMarks(marks.Sensitive),
|
2021-03-24 21:45:46 +01:00
|
|
|
},
|
|
|
|
},
|
2018-06-20 01:30:59 +02:00
|
|
|
},
|
2021-03-24 21:45:46 +01:00
|
|
|
RequiredReplace: cty.NewPathSet(
|
|
|
|
cty.GetAttrPath("boop"),
|
|
|
|
),
|
2021-04-28 21:02:34 +02:00
|
|
|
ActionReason: plans.ResourceInstanceReplaceBecauseCannotUpdate,
|
2018-06-20 01:30:59 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Addr: addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "woot",
|
|
|
|
}.Instance(addrs.IntKey(0)).Absolute(addrs.RootModuleInstance),
|
|
|
|
DeposedKey: "foodface",
|
2020-02-13 21:32:58 +01:00
|
|
|
ProviderAddr: addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
ChangeSrc: plans.ChangeSrc{
|
2018-06-20 01:30:59 +02:00
|
|
|
Action: plans.Delete,
|
|
|
|
Before: mustNewDynamicValue(cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.StringVal("bar-baz-foo"),
|
|
|
|
}), objTy),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-07-10 23:34:32 +02:00
|
|
|
TargetAddrs: []addrs.Targetable{
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "woot",
|
|
|
|
}.Absolute(addrs.RootModuleInstance),
|
|
|
|
},
|
2018-06-20 01:30:59 +02:00
|
|
|
ProviderSHA256s: map[string][]byte{
|
|
|
|
"test": []byte{
|
|
|
|
0xba, 0x5e, 0x1e, 0x55, 0xb0, 0x1d, 0xfa, 0xce,
|
|
|
|
0xef, 0xfe, 0xc7, 0xed, 0x1a, 0xbe, 0x11, 0xed,
|
|
|
|
0x5c, 0xa1, 0xab, 0x1e, 0xda, 0x7a, 0xba, 0x5e,
|
|
|
|
0x70, 0x7a, 0x11, 0xed, 0xb0, 0x07, 0xab, 0x1e,
|
|
|
|
},
|
|
|
|
},
|
2018-07-05 22:21:38 +02:00
|
|
|
Backend: plans.Backend{
|
|
|
|
Type: "local",
|
|
|
|
Config: mustNewDynamicValue(
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"foo": cty.StringVal("bar"),
|
|
|
|
}),
|
|
|
|
cty.Object(map[string]cty.Type{
|
|
|
|
"foo": cty.String,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
Workspace: "default",
|
|
|
|
},
|
2018-06-20 01:30:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err := writeTfplan(plan, &buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
newPlan, err := readTfplan(&buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
oldDepth := deep.MaxDepth
|
|
|
|
oldCompare := deep.CompareUnexportedFields
|
|
|
|
deep.MaxDepth = 20
|
|
|
|
deep.CompareUnexportedFields = true
|
|
|
|
defer func() {
|
|
|
|
deep.MaxDepth = oldDepth
|
|
|
|
deep.CompareUnexportedFields = oldCompare
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
for _, problem := range deep.Equal(newPlan, plan) {
|
|
|
|
t.Error(problem)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-20 20:32:52 +01:00
|
|
|
func mustDynamicOutputValue(val string) plans.DynamicValue {
|
|
|
|
ret, err := plans.NewDynamicValue(cty.StringVal(val), cty.DynamicPseudoType)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2018-06-20 01:30:59 +02:00
|
|
|
func mustNewDynamicValue(val cty.Value, ty cty.Type) plans.DynamicValue {
|
|
|
|
ret, err := plans.NewDynamicValue(val, ty)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustNewDynamicValueStr(val string) plans.DynamicValue {
|
|
|
|
realVal := cty.StringVal(val)
|
|
|
|
ret, err := plans.NewDynamicValue(realVal, cty.String)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
2018-12-20 20:32:52 +01:00
|
|
|
|
|
|
|
// TestTFPlanRoundTripDestroy ensures that encoding and decoding null values for
|
|
|
|
// destroy doesn't leave us with any nil values.
|
|
|
|
func TestTFPlanRoundTripDestroy(t *testing.T) {
|
|
|
|
objTy := cty.Object(map[string]cty.Type{
|
|
|
|
"id": cty.String,
|
|
|
|
})
|
|
|
|
|
|
|
|
plan := &plans.Plan{
|
|
|
|
Changes: &plans.Changes{
|
|
|
|
Outputs: []*plans.OutputChangeSrc{
|
|
|
|
{
|
|
|
|
Addr: addrs.OutputValue{Name: "bar"}.Absolute(addrs.RootModuleInstance),
|
|
|
|
ChangeSrc: plans.ChangeSrc{
|
|
|
|
Action: plans.Delete,
|
|
|
|
Before: mustDynamicOutputValue("output"),
|
|
|
|
After: mustNewDynamicValue(cty.NullVal(cty.String), cty.String),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Resources: []*plans.ResourceInstanceChangeSrc{
|
|
|
|
{
|
|
|
|
Addr: addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "woot",
|
|
|
|
}.Instance(addrs.IntKey(0)).Absolute(addrs.RootModuleInstance),
|
2020-02-13 21:32:58 +01:00
|
|
|
ProviderAddr: addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
2018-12-20 20:32:52 +01:00
|
|
|
ChangeSrc: plans.ChangeSrc{
|
|
|
|
Action: plans.Delete,
|
|
|
|
Before: mustNewDynamicValue(cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.StringVal("foo-bar-baz"),
|
|
|
|
}), objTy),
|
|
|
|
After: mustNewDynamicValue(cty.NullVal(objTy), objTy),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TargetAddrs: []addrs.Targetable{
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "woot",
|
|
|
|
}.Absolute(addrs.RootModuleInstance),
|
|
|
|
},
|
|
|
|
Backend: plans.Backend{
|
|
|
|
Type: "local",
|
|
|
|
Config: mustNewDynamicValue(
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"foo": cty.StringVal("bar"),
|
|
|
|
}),
|
|
|
|
cty.Object(map[string]cty.Type{
|
|
|
|
"foo": cty.String,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
Workspace: "default",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err := writeTfplan(plan, &buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
newPlan, err := readTfplan(&buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rics := range newPlan.Changes.Resources {
|
|
|
|
ric, err := rics.Decode(objTy)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ric.After == cty.NilVal {
|
|
|
|
t.Fatalf("unexpected nil After value: %#v\n", ric)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, ocs := range newPlan.Changes.Outputs {
|
|
|
|
oc, err := ocs.Decode()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if oc.After == cty.NilVal {
|
|
|
|
t.Fatalf("unexpected nil After value: %#v\n", ocs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|