From 37dfda93ceec08889a222496b8021d3eb3eb6da2 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Mon, 7 May 2018 17:58:43 -0700 Subject: [PATCH] core: Fix ReferenceTransformer tests The changes to our GraphNodeReferencable and GraphNodeReferencer interfaces were not also reflected in our testing structs here, and so these tests were no longer working. This updates these implementations to the new required signatures, adapting the simplified model used in the structs to generate local variable references, since the reference model no longer uses the arbitrary strings that this test originally depended on. We also remove some of the tests here since the functionality they were testing no longer applies: inter-module dependencies are now handled by the graph nodes producing extra ReferencableAddrs, and the "backup" form is no longer used because our new address model is able to distinguish resources from resource instances without the need for the magical backup reference forms we previously used. --- terraform/transform_reference_test.go | 146 +++++++------------------- 1 file changed, 39 insertions(+), 107 deletions(-) diff --git a/terraform/transform_reference_test.go b/terraform/transform_reference_test.go index dc03cdd1a..ad32b1376 100644 --- a/terraform/transform_reference_test.go +++ b/terraform/transform_reference_test.go @@ -29,7 +29,7 @@ func TestReferenceTransformer_simple(t *testing.T) { actual := strings.TrimSpace(g.String()) expected := strings.TrimSpace(testTransformRefBasicStr) if actual != expected { - t.Fatalf("bad:\n\n%s", actual) + t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected) } } @@ -52,7 +52,7 @@ func TestReferenceTransformer_self(t *testing.T) { actual := strings.TrimSpace(g.String()) expected := strings.TrimSpace(testTransformRefBasicStr) if actual != expected { - t.Fatalf("bad:\n\n%s", actual) + t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected) } } @@ -85,105 +85,7 @@ func TestReferenceTransformer_path(t *testing.T) { actual := strings.TrimSpace(g.String()) expected := strings.TrimSpace(testTransformRefPathStr) if actual != expected { - t.Fatalf("bad:\n\n%s", actual) - } -} - -func TestReferenceTransformer_backup(t *testing.T) { - g := Graph{Path: addrs.RootModuleInstance} - g.Add(&graphNodeRefParentTest{ - NameValue: "A", - Names: []string{"A"}, - }) - g.Add(&graphNodeRefChildTest{ - NameValue: "B", - Refs: []string{"C/A"}, - }) - - tf := &ReferenceTransformer{} - if err := tf.Transform(&g); err != nil { - t.Fatalf("err: %s", err) - } - - actual := strings.TrimSpace(g.String()) - expected := strings.TrimSpace(testTransformRefBackupStr) - if actual != expected { - t.Fatalf("bad:\n\n%s", actual) - } -} - -func TestReferenceTransformer_backupPrimary(t *testing.T) { - g := Graph{Path: addrs.RootModuleInstance} - g.Add(&graphNodeRefParentTest{ - NameValue: "A", - Names: []string{"A"}, - }) - g.Add(&graphNodeRefChildTest{ - NameValue: "B", - Refs: []string{"C/A"}, - }) - g.Add(&graphNodeRefParentTest{ - NameValue: "C", - Names: []string{"C"}, - }) - - tf := &ReferenceTransformer{} - if err := tf.Transform(&g); err != nil { - t.Fatalf("err: %s", err) - } - - actual := strings.TrimSpace(g.String()) - expected := strings.TrimSpace(testTransformRefBackupPrimaryStr) - if actual != expected { - t.Fatalf("bad:\n\n%s", actual) - } -} - -func TestReferenceTransformer_modulePath(t *testing.T) { - g := Graph{Path: addrs.RootModuleInstance} - g.Add(&graphNodeRefParentTest{ - NameValue: "A", - Names: []string{"A"}, - PathValue: []string{"foo"}, - }) - g.Add(&graphNodeRefChildTest{ - NameValue: "B", - Refs: []string{"module.foo"}, - }) - - tf := &ReferenceTransformer{} - if err := tf.Transform(&g); err != nil { - t.Fatalf("err: %s", err) - } - - actual := strings.TrimSpace(g.String()) - expected := strings.TrimSpace(testTransformRefModulePathStr) - if actual != expected { - t.Fatalf("bad:\n\n%s", actual) - } -} - -func TestReferenceTransformer_modulePathNormalized(t *testing.T) { - g := Graph{Path: addrs.RootModuleInstance} - g.Add(&graphNodeRefParentTest{ - NameValue: "A", - Names: []string{"A"}, - PathValue: []string{"root", "foo"}, - }) - g.Add(&graphNodeRefChildTest{ - NameValue: "B", - Refs: []string{"module.foo"}, - }) - - tf := &ReferenceTransformer{} - if err := tf.Transform(&g); err != nil { - t.Fatalf("err: %s", err) - } - - actual := strings.TrimSpace(g.String()) - expected := strings.TrimSpace(testTransformRefModulePathStr) - if actual != expected { - t.Fatalf("bad:\n\n%s", actual) + t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected) } } @@ -281,9 +183,23 @@ type graphNodeRefParentTest struct { Names []string } -func (n *graphNodeRefParentTest) Name() string { return n.NameValue } -func (n *graphNodeRefParentTest) ReferenceableName() []string { return n.Names } -func (n *graphNodeRefParentTest) Path() []string { return n.PathValue } +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) +} type graphNodeRefChildTest struct { NameValue string @@ -291,9 +207,25 @@ type graphNodeRefChildTest struct { Refs []string } -func (n *graphNodeRefChildTest) Name() string { return n.NameValue } -func (n *graphNodeRefChildTest) References() []string { return n.Refs } -func (n *graphNodeRefChildTest) Path() []string { return n.PathValue } +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) +} const testTransformRefBasicStr = ` A