2018-06-20 01:30:59 +02:00
|
|
|
package planfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/go-test/deep"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
|
|
"github.com/hashicorp/terraform/plans"
|
|
|
|
)
|
|
|
|
|
|
|
|
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),
|
2018-06-22 00:26:37 +02:00
|
|
|
ProviderAddr: addrs.ProviderConfig{
|
2019-12-06 14:00:18 +01:00
|
|
|
Type: addrs.NewLegacyProvider("test"),
|
2018-06-22 00:26:37 +02:00
|
|
|
}.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-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"),
|
|
|
|
}), objTy),
|
|
|
|
After: mustNewDynamicValue(cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.UnknownVal(cty.String),
|
|
|
|
}), objTy),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Addr: addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "woot",
|
|
|
|
}.Instance(addrs.IntKey(0)).Absolute(addrs.RootModuleInstance),
|
|
|
|
DeposedKey: "foodface",
|
2018-06-22 00:26:37 +02:00
|
|
|
ProviderAddr: addrs.ProviderConfig{
|
2019-12-06 14:00:18 +01:00
|
|
|
Type: addrs.NewLegacyProvider("test"),
|
2018-06-22 00:26:37 +02:00
|
|
|
}.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.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),
|
|
|
|
ProviderAddr: addrs.ProviderConfig{
|
2019-12-06 14:00:18 +01:00
|
|
|
Type: addrs.NewLegacyProvider("test"),
|
2018-12-20 20:32:52 +01:00
|
|
|
}.Absolute(addrs.RootModuleInstance),
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|