2016-09-16 03:41:09 +02:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2016-11-05 02:40:09 +01:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
2016-09-16 03:41:09 +02:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2016-11-05 02:40:09 +01:00
|
|
|
|
2018-05-05 04:24:06 +02:00
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2016-11-05 02:40:09 +01:00
|
|
|
"github.com/hashicorp/terraform/dag"
|
2016-09-16 03:41:09 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestReferenceTransformer_simple(t *testing.T) {
|
2018-05-05 04:24:06 +02:00
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
2016-09-16 03:41:09 +02:00
|
|
|
g.Add(&graphNodeRefParentTest{
|
|
|
|
NameValue: "A",
|
|
|
|
Names: []string{"A"},
|
|
|
|
})
|
|
|
|
g.Add(&graphNodeRefChildTest{
|
|
|
|
NameValue: "B",
|
|
|
|
Refs: []string{"A"},
|
|
|
|
})
|
|
|
|
|
|
|
|
tf := &ReferenceTransformer{}
|
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTransformRefBasicStr)
|
|
|
|
if actual != expected {
|
2018-05-08 02:58:43 +02:00
|
|
|
t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected)
|
2016-09-16 03:41:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-19 06:31:03 +02:00
|
|
|
func TestReferenceTransformer_self(t *testing.T) {
|
2018-05-05 04:24:06 +02:00
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
2016-09-19 06:31:03 +02:00
|
|
|
g.Add(&graphNodeRefParentTest{
|
|
|
|
NameValue: "A",
|
|
|
|
Names: []string{"A"},
|
|
|
|
})
|
|
|
|
g.Add(&graphNodeRefChildTest{
|
|
|
|
NameValue: "B",
|
|
|
|
Refs: []string{"A", "B"},
|
|
|
|
})
|
|
|
|
|
|
|
|
tf := &ReferenceTransformer{}
|
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTransformRefBasicStr)
|
|
|
|
if actual != expected {
|
2018-05-08 02:58:43 +02:00
|
|
|
t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected)
|
2016-09-19 06:31:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-16 03:41:09 +02:00
|
|
|
func TestReferenceTransformer_path(t *testing.T) {
|
2018-05-05 04:24:06 +02:00
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
2016-09-16 03:41:09 +02:00
|
|
|
g.Add(&graphNodeRefParentTest{
|
|
|
|
NameValue: "A",
|
|
|
|
Names: []string{"A"},
|
|
|
|
})
|
|
|
|
g.Add(&graphNodeRefChildTest{
|
|
|
|
NameValue: "B",
|
|
|
|
Refs: []string{"A"},
|
|
|
|
})
|
|
|
|
g.Add(&graphNodeRefParentTest{
|
|
|
|
NameValue: "child.A",
|
|
|
|
PathValue: []string{"root", "child"},
|
|
|
|
Names: []string{"A"},
|
|
|
|
})
|
|
|
|
g.Add(&graphNodeRefChildTest{
|
|
|
|
NameValue: "child.B",
|
|
|
|
PathValue: []string{"root", "child"},
|
|
|
|
Refs: []string{"A"},
|
|
|
|
})
|
|
|
|
|
|
|
|
tf := &ReferenceTransformer{}
|
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTransformRefPathStr)
|
|
|
|
if actual != expected {
|
2018-05-08 02:58:43 +02:00
|
|
|
t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected)
|
2016-11-12 17:24:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-05 02:40:09 +01:00
|
|
|
func TestReferenceMapReferences(t *testing.T) {
|
|
|
|
cases := map[string]struct {
|
|
|
|
Nodes []dag.Vertex
|
|
|
|
Check dag.Vertex
|
|
|
|
Result []string
|
|
|
|
}{
|
|
|
|
"simple": {
|
|
|
|
Nodes: []dag.Vertex{
|
|
|
|
&graphNodeRefParentTest{
|
|
|
|
NameValue: "A",
|
|
|
|
Names: []string{"A"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Check: &graphNodeRefChildTest{
|
|
|
|
NameValue: "foo",
|
|
|
|
Refs: []string{"A"},
|
|
|
|
},
|
|
|
|
Result: []string{"A"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for tn, tc := range cases {
|
|
|
|
t.Run(tn, func(t *testing.T) {
|
|
|
|
rm := NewReferenceMap(tc.Nodes)
|
2020-02-24 23:42:32 +01:00
|
|
|
result := rm.References(tc.Check)
|
2016-11-05 02:58:03 +01:00
|
|
|
|
2016-11-05 02:40:09 +01:00
|
|
|
var resultStr []string
|
|
|
|
for _, v := range result {
|
|
|
|
resultStr = append(resultStr, dag.VertexName(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(resultStr)
|
|
|
|
sort.Strings(tc.Result)
|
|
|
|
if !reflect.DeepEqual(resultStr, tc.Result) {
|
|
|
|
t.Fatalf("bad: %#v", resultStr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-16 03:41:09 +02:00
|
|
|
type graphNodeRefParentTest struct {
|
|
|
|
NameValue string
|
|
|
|
PathValue []string
|
|
|
|
Names []string
|
|
|
|
}
|
|
|
|
|
2018-05-08 02:58:43 +02:00
|
|
|
var _ GraphNodeReferenceable = (*graphNodeRefParentTest)(nil)
|
|
|
|
|
|
|
|
func (n *graphNodeRefParentTest) Name() string {
|
|
|
|
return n.NameValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *graphNodeRefParentTest) ReferenceableAddrs() []addrs.Referenceable {
|
|
|
|
ret := make([]addrs.Referenceable, len(n.Names))
|
|
|
|
for i, name := range n.Names {
|
|
|
|
ret[i] = addrs.LocalValue{Name: name}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *graphNodeRefParentTest) Path() addrs.ModuleInstance {
|
|
|
|
return normalizeModulePath(n.PathValue)
|
|
|
|
}
|
2016-09-16 03:41:09 +02:00
|
|
|
|
2020-03-05 03:00:16 +01:00
|
|
|
func (n *graphNodeRefParentTest) ModulePath() addrs.Module {
|
|
|
|
return normalizeModulePath(n.PathValue).Module()
|
|
|
|
}
|
|
|
|
|
2016-09-16 03:41:09 +02:00
|
|
|
type graphNodeRefChildTest struct {
|
|
|
|
NameValue string
|
|
|
|
PathValue []string
|
|
|
|
Refs []string
|
|
|
|
}
|
|
|
|
|
2018-05-08 02:58:43 +02:00
|
|
|
var _ GraphNodeReferencer = (*graphNodeRefChildTest)(nil)
|
|
|
|
|
|
|
|
func (n *graphNodeRefChildTest) Name() string {
|
|
|
|
return n.NameValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *graphNodeRefChildTest) References() []*addrs.Reference {
|
|
|
|
ret := make([]*addrs.Reference, len(n.Refs))
|
|
|
|
for i, name := range n.Refs {
|
|
|
|
ret[i] = &addrs.Reference{
|
|
|
|
Subject: addrs.LocalValue{Name: name},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *graphNodeRefChildTest) Path() addrs.ModuleInstance {
|
|
|
|
return normalizeModulePath(n.PathValue)
|
|
|
|
}
|
2016-09-16 03:41:09 +02:00
|
|
|
|
2020-03-05 03:00:16 +01:00
|
|
|
func (n *graphNodeRefChildTest) ModulePath() addrs.Module {
|
|
|
|
return normalizeModulePath(n.PathValue).Module()
|
|
|
|
}
|
|
|
|
|
2016-09-16 03:41:09 +02:00
|
|
|
const testTransformRefBasicStr = `
|
|
|
|
A
|
|
|
|
B
|
|
|
|
A
|
|
|
|
`
|
|
|
|
|
2016-11-08 18:35:57 +01:00
|
|
|
const testTransformRefBackupStr = `
|
|
|
|
A
|
|
|
|
B
|
|
|
|
A
|
|
|
|
`
|
|
|
|
|
|
|
|
const testTransformRefBackupPrimaryStr = `
|
|
|
|
A
|
|
|
|
B
|
|
|
|
C
|
|
|
|
C
|
|
|
|
`
|
|
|
|
|
2016-11-12 17:24:09 +01:00
|
|
|
const testTransformRefModulePathStr = `
|
|
|
|
A
|
|
|
|
B
|
|
|
|
A
|
|
|
|
`
|
|
|
|
|
2016-09-16 03:41:09 +02:00
|
|
|
const testTransformRefPathStr = `
|
|
|
|
A
|
|
|
|
B
|
|
|
|
A
|
|
|
|
child.A
|
|
|
|
child.B
|
|
|
|
child.A
|
|
|
|
`
|