mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-04 13:17:43 -06:00
8d193ad268
Historically the responsibility for making sure that all of the available providers are of suitable versions and match the appropriate checksums has been split rather inexplicably over multiple different layers, with some of the checks happening as late as creating a terraform.Context. We're gradually iterating towards making that all be handled in one place, but in this step we're just cleaning up some old remnants from the main "terraform" package, which is now no longer responsible for any version or checksum verification and instead just assumes it's been provided with suitable factory functions by its caller. We do still have a pre-check here to make sure that we at least have a factory function for each plugin the configuration seems to depend on, because if we don't do that up front then it ends up getting caught instead deep inside the Terraform runtime, often inside a concurrent graph walk and thus it's not deterministic which codepath will happen to catch it on a particular run. As of this commit, this actually does leave some holes in our checks: the command package is using the dependency lock file to make sure we have exactly the provider packages we expect (exact versions and checksums), which is the most crucial part, but we don't yet have any spot where we make sure that the lock file is consistent with the current configuration, and we are no longer preserving the provider checksums as part of a saved plan. Both of those will come in subsequent commits. While it's unusual to have a series of commits that briefly subtracts functionality and then adds back in equivalent functionality later, the lock file checking is the only part that's crucial for security reasons, with everything else mainly just being to give better feedback when folks seem to be using Terraform incorrectly. The other bits are therefore mostly cosmetic and okay to be absent briefly as we work towards a better design that is clearer about where that responsibility belongs.
332 lines
8.6 KiB
Go
332 lines
8.6 KiB
Go
package planfile
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
|
|
"github.com/go-test/deep"
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
|
"github.com/hashicorp/terraform/internal/lang/marks"
|
|
"github.com/hashicorp/terraform/internal/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{
|
|
Outputs: []*plans.OutputChangeSrc{
|
|
{
|
|
Addr: addrs.OutputValue{Name: "bar"}.Absolute(addrs.RootModuleInstance),
|
|
ChangeSrc: plans.ChangeSrc{
|
|
Action: plans.Create,
|
|
After: mustDynamicOutputValue("bar value"),
|
|
},
|
|
Sensitive: false,
|
|
},
|
|
{
|
|
Addr: addrs.OutputValue{Name: "baz"}.Absolute(addrs.RootModuleInstance),
|
|
ChangeSrc: plans.ChangeSrc{
|
|
Action: plans.NoOp,
|
|
Before: mustDynamicOutputValue("baz value"),
|
|
After: mustDynamicOutputValue("baz value"),
|
|
},
|
|
Sensitive: false,
|
|
},
|
|
{
|
|
Addr: addrs.OutputValue{Name: "secret"}.Absolute(addrs.RootModuleInstance),
|
|
ChangeSrc: plans.ChangeSrc{
|
|
Action: plans.Update,
|
|
Before: mustDynamicOutputValue("old secret value"),
|
|
After: mustDynamicOutputValue("new secret value"),
|
|
},
|
|
Sensitive: true,
|
|
},
|
|
},
|
|
Resources: []*plans.ResourceInstanceChangeSrc{
|
|
{
|
|
Addr: addrs.Resource{
|
|
Mode: addrs.ManagedResourceMode,
|
|
Type: "test_thing",
|
|
Name: "woot",
|
|
}.Instance(addrs.IntKey(0)).Absolute(addrs.RootModuleInstance),
|
|
PrevRunAddr: addrs.Resource{
|
|
Mode: addrs.ManagedResourceMode,
|
|
Type: "test_thing",
|
|
Name: "woot",
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
ProviderAddr: addrs.AbsProviderConfig{
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
Module: addrs.RootModule,
|
|
},
|
|
ChangeSrc: plans.ChangeSrc{
|
|
Action: plans.DeleteThenCreate,
|
|
Before: mustNewDynamicValue(cty.ObjectVal(map[string]cty.Value{
|
|
"id": cty.StringVal("foo-bar-baz"),
|
|
"boop": cty.ListVal([]cty.Value{
|
|
cty.StringVal("beep"),
|
|
}),
|
|
}), objTy),
|
|
After: mustNewDynamicValue(cty.ObjectVal(map[string]cty.Value{
|
|
"id": cty.UnknownVal(cty.String),
|
|
"boop": cty.ListVal([]cty.Value{
|
|
cty.StringVal("beep"),
|
|
cty.StringVal("honk"),
|
|
}),
|
|
}), objTy),
|
|
AfterValMarks: []cty.PathValueMarks{
|
|
{
|
|
Path: cty.GetAttrPath("boop").IndexInt(1),
|
|
Marks: cty.NewValueMarks(marks.Sensitive),
|
|
},
|
|
},
|
|
},
|
|
RequiredReplace: cty.NewPathSet(
|
|
cty.GetAttrPath("boop"),
|
|
),
|
|
ActionReason: plans.ResourceInstanceReplaceBecauseCannotUpdate,
|
|
},
|
|
{
|
|
Addr: addrs.Resource{
|
|
Mode: addrs.ManagedResourceMode,
|
|
Type: "test_thing",
|
|
Name: "woot",
|
|
}.Instance(addrs.IntKey(1)).Absolute(addrs.RootModuleInstance),
|
|
PrevRunAddr: addrs.Resource{
|
|
Mode: addrs.ManagedResourceMode,
|
|
Type: "test_thing",
|
|
Name: "woot",
|
|
}.Instance(addrs.IntKey(1)).Absolute(addrs.RootModuleInstance),
|
|
DeposedKey: "foodface",
|
|
ProviderAddr: addrs.AbsProviderConfig{
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
Module: addrs.RootModule,
|
|
},
|
|
ChangeSrc: plans.ChangeSrc{
|
|
Action: plans.Delete,
|
|
Before: mustNewDynamicValue(cty.ObjectVal(map[string]cty.Value{
|
|
"id": cty.StringVal("bar-baz-foo"),
|
|
}), objTy),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
DriftedResources: []*plans.ResourceInstanceChangeSrc{
|
|
{
|
|
Addr: addrs.Resource{
|
|
Mode: addrs.ManagedResourceMode,
|
|
Type: "test_thing",
|
|
Name: "woot",
|
|
}.Instance(addrs.IntKey(0)).Absolute(addrs.RootModuleInstance),
|
|
PrevRunAddr: addrs.Resource{
|
|
Mode: addrs.ManagedResourceMode,
|
|
Type: "test_thing",
|
|
Name: "woot",
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
ProviderAddr: addrs.AbsProviderConfig{
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
Module: addrs.RootModule,
|
|
},
|
|
ChangeSrc: plans.ChangeSrc{
|
|
Action: plans.DeleteThenCreate,
|
|
Before: mustNewDynamicValue(cty.ObjectVal(map[string]cty.Value{
|
|
"id": cty.StringVal("foo-bar-baz"),
|
|
"boop": cty.ListVal([]cty.Value{
|
|
cty.StringVal("beep"),
|
|
}),
|
|
}), objTy),
|
|
After: mustNewDynamicValue(cty.ObjectVal(map[string]cty.Value{
|
|
"id": cty.UnknownVal(cty.String),
|
|
"boop": cty.ListVal([]cty.Value{
|
|
cty.StringVal("beep"),
|
|
cty.StringVal("bonk"),
|
|
}),
|
|
}), objTy),
|
|
AfterValMarks: []cty.PathValueMarks{
|
|
{
|
|
Path: cty.GetAttrPath("boop").IndexInt(1),
|
|
Marks: cty.NewValueMarks(marks.Sensitive),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
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)
|
|
}
|
|
|
|
{
|
|
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)
|
|
}
|
|
}
|
|
|
|
func mustDynamicOutputValue(val string) plans.DynamicValue {
|
|
ret, err := plans.NewDynamicValue(cty.StringVal(val), cty.DynamicPseudoType)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return ret
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// 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),
|
|
PrevRunAddr: addrs.Resource{
|
|
Mode: addrs.ManagedResourceMode,
|
|
Type: "test_thing",
|
|
Name: "woot",
|
|
}.Instance(addrs.IntKey(0)).Absolute(addrs.RootModuleInstance),
|
|
ProviderAddr: addrs.AbsProviderConfig{
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
Module: addrs.RootModule,
|
|
},
|
|
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),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
DriftedResources: []*plans.ResourceInstanceChangeSrc{},
|
|
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)
|
|
}
|
|
}
|
|
}
|