mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
cfb440ea60
Init should only _add_ values, not remove them. During graph execution, there are steps that expect that a state isn't being actively pruned out from under it. Namely: writing deposed states. Writing deposed states has no way to handle if a state changes underneath it because the only way to uniquely identify a deposed state is its index in the deposed array. When destroying deposed resources, we set the value to `<nil>`. If the array is pruned before the next deposed destroy, then the indexes have changed, and this can cause a crash. This PR does the following (with more details below): * `init()` no longer prunes. * `ReadState()` always prunes before returning. I can't think of a scenario where this is unsafe since generally we can always START from a pruned state, its just causing problems to prune mid-execution. * Exported State APIs updated to be robust against nil ModuleStates. Instead, I think we should adopt the following semantics for init/prune in our structures that support it (Diff, for example). By having consistent semantics around these functions, we can avoid this in the future and have set expectations working with them. * `init()` (in anything) will only ever be additive, and won't change ordering or existing values. It won't remove values. * `prune()` is destructive, expectedly. * Functions on a structure must not assume a pruned structure 100% of the time. They must be robust to handle nils. This is especially important because in many cases values such as `Modules` in state are exported so end users can simply modify them outside of the exported APIs. This PR may expose us to unknown crashes but I've tried to cover our cases in exposed APIs by checking for nil.
272 lines
5.5 KiB
Go
272 lines
5.5 KiB
Go
package terraform
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/dag"
|
|
)
|
|
|
|
func TestOrphanResourceTransformer(t *testing.T) {
|
|
mod := testModule(t, "transform-orphan-basic")
|
|
state := &State{
|
|
Modules: []*ModuleState{
|
|
&ModuleState{
|
|
Path: RootModulePath,
|
|
Resources: map[string]*ResourceState{
|
|
"aws_instance.web": &ResourceState{
|
|
Type: "aws_instance",
|
|
Primary: &InstanceState{
|
|
ID: "foo",
|
|
},
|
|
},
|
|
|
|
// The orphan
|
|
"aws_instance.db": &ResourceState{
|
|
Type: "aws_instance",
|
|
Primary: &InstanceState{
|
|
ID: "foo",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
g := Graph{Path: RootModulePath}
|
|
{
|
|
tf := &ConfigTransformer{Module: mod}
|
|
if err := tf.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
{
|
|
tf := &OrphanResourceTransformer{
|
|
Concrete: testOrphanResourceConcreteFunc,
|
|
State: state, Module: mod,
|
|
}
|
|
if err := tf.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
expected := strings.TrimSpace(testTransformOrphanResourceBasicStr)
|
|
if actual != expected {
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
}
|
|
}
|
|
|
|
func TestOrphanResourceTransformer_nilModule(t *testing.T) {
|
|
mod := testModule(t, "transform-orphan-basic")
|
|
state := &State{
|
|
Modules: []*ModuleState{nil},
|
|
}
|
|
|
|
g := Graph{Path: RootModulePath}
|
|
{
|
|
tf := &ConfigTransformer{Module: mod}
|
|
if err := tf.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
{
|
|
tf := &OrphanResourceTransformer{
|
|
Concrete: testOrphanResourceConcreteFunc,
|
|
State: state, Module: mod,
|
|
}
|
|
if err := tf.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestOrphanResourceTransformer_countGood(t *testing.T) {
|
|
mod := testModule(t, "transform-orphan-count")
|
|
state := &State{
|
|
Modules: []*ModuleState{
|
|
&ModuleState{
|
|
Path: RootModulePath,
|
|
Resources: map[string]*ResourceState{
|
|
"aws_instance.foo.0": &ResourceState{
|
|
Type: "aws_instance",
|
|
Primary: &InstanceState{
|
|
ID: "foo",
|
|
},
|
|
},
|
|
|
|
"aws_instance.foo.1": &ResourceState{
|
|
Type: "aws_instance",
|
|
Primary: &InstanceState{
|
|
ID: "foo",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
g := Graph{Path: RootModulePath}
|
|
{
|
|
tf := &ConfigTransformer{Module: mod}
|
|
if err := tf.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
{
|
|
tf := &OrphanResourceTransformer{
|
|
Concrete: testOrphanResourceConcreteFunc,
|
|
State: state, Module: mod,
|
|
}
|
|
if err := tf.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
expected := strings.TrimSpace(testTransformOrphanResourceCountStr)
|
|
if actual != expected {
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
}
|
|
}
|
|
|
|
func TestOrphanResourceTransformer_countBad(t *testing.T) {
|
|
mod := testModule(t, "transform-orphan-count-empty")
|
|
state := &State{
|
|
Modules: []*ModuleState{
|
|
&ModuleState{
|
|
Path: RootModulePath,
|
|
Resources: map[string]*ResourceState{
|
|
"aws_instance.foo.0": &ResourceState{
|
|
Type: "aws_instance",
|
|
Primary: &InstanceState{
|
|
ID: "foo",
|
|
},
|
|
},
|
|
|
|
"aws_instance.foo.1": &ResourceState{
|
|
Type: "aws_instance",
|
|
Primary: &InstanceState{
|
|
ID: "foo",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
g := Graph{Path: RootModulePath}
|
|
{
|
|
tf := &ConfigTransformer{Module: mod}
|
|
if err := tf.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
{
|
|
tf := &OrphanResourceTransformer{
|
|
Concrete: testOrphanResourceConcreteFunc,
|
|
State: state, Module: mod,
|
|
}
|
|
if err := tf.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
expected := strings.TrimSpace(testTransformOrphanResourceCountBadStr)
|
|
if actual != expected {
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
}
|
|
}
|
|
|
|
func TestOrphanResourceTransformer_modules(t *testing.T) {
|
|
mod := testModule(t, "transform-orphan-modules")
|
|
state := &State{
|
|
Modules: []*ModuleState{
|
|
&ModuleState{
|
|
Path: RootModulePath,
|
|
Resources: map[string]*ResourceState{
|
|
"aws_instance.foo": &ResourceState{
|
|
Type: "aws_instance",
|
|
Primary: &InstanceState{
|
|
ID: "foo",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
|
|
&ModuleState{
|
|
Path: []string{"root", "child"},
|
|
Resources: map[string]*ResourceState{
|
|
"aws_instance.web": &ResourceState{
|
|
Type: "aws_instance",
|
|
Primary: &InstanceState{
|
|
ID: "foo",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
g := Graph{Path: RootModulePath}
|
|
{
|
|
tf := &ConfigTransformer{Module: mod}
|
|
if err := tf.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
{
|
|
tf := &OrphanResourceTransformer{
|
|
Concrete: testOrphanResourceConcreteFunc,
|
|
State: state, Module: mod,
|
|
}
|
|
if err := tf.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
expected := strings.TrimSpace(testTransformOrphanResourceModulesStr)
|
|
if actual != expected {
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
}
|
|
}
|
|
|
|
const testTransformOrphanResourceBasicStr = `
|
|
aws_instance.db (orphan)
|
|
aws_instance.web
|
|
`
|
|
|
|
const testTransformOrphanResourceCountStr = `
|
|
aws_instance.foo
|
|
`
|
|
|
|
const testTransformOrphanResourceCountBadStr = `
|
|
aws_instance.foo[0] (orphan)
|
|
aws_instance.foo[1] (orphan)
|
|
`
|
|
|
|
const testTransformOrphanResourceModulesStr = `
|
|
aws_instance.foo
|
|
module.child.aws_instance.web (orphan)
|
|
`
|
|
|
|
func testOrphanResourceConcreteFunc(a *NodeAbstractResource) dag.Vertex {
|
|
return &testOrphanResourceConcrete{a}
|
|
}
|
|
|
|
type testOrphanResourceConcrete struct {
|
|
*NodeAbstractResource
|
|
}
|
|
|
|
func (n *testOrphanResourceConcrete) Name() string {
|
|
return fmt.Sprintf("%s (orphan)", n.NodeAbstractResource.Name())
|
|
}
|