opentofu/terraform/graph_test.go
Paul Hinze 52c4bfbe98 core: fix deadlock when dependable node replaced with non-dependable one
In #2884, Terraform would hang on graphs with an orphaned resource
depended on an orphaned module.

This is because orphan module nodes (which are dependable) were getting
expanded (replaced) with GraphNodeBasicSubgraph nodes (which are _not_
dependable).

The old `graph.Replace()` code resulted in GraphNodeBasicSubgraph being
entered into the lookaside table, even though it is not dependable.

This resulted in an untraversable edge in the graph, so the graph would
hang and wait forever.

Now, we remove entries from the lookaside table when a dependable node
is being replaced with a non-dependable node. This means we lose an
edge, but we can move forward. It's ~probably~ never correct to be
replacing depenable nodes with non-dependable ones, but this tweak
seemed preferable to tossing a panic in there.
2015-08-10 15:50:36 -05:00

99 lines
2.0 KiB
Go

package terraform
import (
"reflect"
"strings"
"testing"
)
func TestGraphAdd(t *testing.T) {
// Test Add since we override it and want to make sure we don't break it.
var g Graph
g.Add(42)
g.Add(84)
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testGraphAddStr)
if actual != expected {
t.Fatalf("bad: %s", actual)
}
}
func TestGraphConnectDependent(t *testing.T) {
var g Graph
g.Add(&testGraphDependable{VertexName: "a"})
b := g.Add(&testGraphDependable{
VertexName: "b",
DependentOnMock: []string{"a"},
})
if missing := g.ConnectDependent(b); len(missing) > 0 {
t.Fatalf("bad: %#v", missing)
}
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testGraphConnectDepsStr)
if actual != expected {
t.Fatalf("bad: %s", actual)
}
}
func TestGraphReplace_DependableWithNonDependable(t *testing.T) {
var g Graph
a := g.Add(&testGraphDependable{VertexName: "a"})
b := g.Add(&testGraphDependable{
VertexName: "b",
DependentOnMock: []string{"a"},
})
newA := "non-dependable-a"
if missing := g.ConnectDependent(b); len(missing) > 0 {
t.Fatalf("bad: %#v", missing)
}
if !g.Replace(a, newA) {
t.Fatalf("failed to replace")
}
c := g.Add(&testGraphDependable{
VertexName: "c",
DependentOnMock: []string{"a"},
})
// This should fail by reporting missing, since a node with dependable
// name "a" is no longer in the graph.
missing := g.ConnectDependent(c)
expected := []string{"a"}
if !reflect.DeepEqual(expected, missing) {
t.Fatalf("expected: %#v, got: %#v", expected, missing)
}
}
type testGraphDependable struct {
VertexName string
DependentOnMock []string
}
func (v *testGraphDependable) Name() string {
return v.VertexName
}
func (v *testGraphDependable) DependableName() []string {
return []string{v.VertexName}
}
func (v *testGraphDependable) DependentOn() []string {
return v.DependentOnMock
}
const testGraphAddStr = `
42
84
`
const testGraphConnectDepsStr = `
a
b
a
`