github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/terraform/transform_reference_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"reflect"
     5  	"sort"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/dag"
    10  )
    11  
    12  func TestReferenceTransformer_simple(t *testing.T) {
    13  	g := Graph{Path: RootModulePath}
    14  	g.Add(&graphNodeRefParentTest{
    15  		NameValue: "A",
    16  		Names:     []string{"A"},
    17  	})
    18  	g.Add(&graphNodeRefChildTest{
    19  		NameValue: "B",
    20  		Refs:      []string{"A"},
    21  	})
    22  
    23  	tf := &ReferenceTransformer{}
    24  	if err := tf.Transform(&g); err != nil {
    25  		t.Fatalf("err: %s", err)
    26  	}
    27  
    28  	actual := strings.TrimSpace(g.String())
    29  	expected := strings.TrimSpace(testTransformRefBasicStr)
    30  	if actual != expected {
    31  		t.Fatalf("bad:\n\n%s", actual)
    32  	}
    33  }
    34  
    35  func TestReferenceTransformer_self(t *testing.T) {
    36  	g := Graph{Path: RootModulePath}
    37  	g.Add(&graphNodeRefParentTest{
    38  		NameValue: "A",
    39  		Names:     []string{"A"},
    40  	})
    41  	g.Add(&graphNodeRefChildTest{
    42  		NameValue: "B",
    43  		Refs:      []string{"A", "B"},
    44  	})
    45  
    46  	tf := &ReferenceTransformer{}
    47  	if err := tf.Transform(&g); err != nil {
    48  		t.Fatalf("err: %s", err)
    49  	}
    50  
    51  	actual := strings.TrimSpace(g.String())
    52  	expected := strings.TrimSpace(testTransformRefBasicStr)
    53  	if actual != expected {
    54  		t.Fatalf("bad:\n\n%s", actual)
    55  	}
    56  }
    57  
    58  func TestReferenceTransformer_path(t *testing.T) {
    59  	g := Graph{Path: RootModulePath}
    60  	g.Add(&graphNodeRefParentTest{
    61  		NameValue: "A",
    62  		Names:     []string{"A"},
    63  	})
    64  	g.Add(&graphNodeRefChildTest{
    65  		NameValue: "B",
    66  		Refs:      []string{"A"},
    67  	})
    68  	g.Add(&graphNodeRefParentTest{
    69  		NameValue: "child.A",
    70  		PathValue: []string{"root", "child"},
    71  		Names:     []string{"A"},
    72  	})
    73  	g.Add(&graphNodeRefChildTest{
    74  		NameValue: "child.B",
    75  		PathValue: []string{"root", "child"},
    76  		Refs:      []string{"A"},
    77  	})
    78  
    79  	tf := &ReferenceTransformer{}
    80  	if err := tf.Transform(&g); err != nil {
    81  		t.Fatalf("err: %s", err)
    82  	}
    83  
    84  	actual := strings.TrimSpace(g.String())
    85  	expected := strings.TrimSpace(testTransformRefPathStr)
    86  	if actual != expected {
    87  		t.Fatalf("bad:\n\n%s", actual)
    88  	}
    89  }
    90  
    91  func TestReferenceTransformer_backup(t *testing.T) {
    92  	g := Graph{Path: RootModulePath}
    93  	g.Add(&graphNodeRefParentTest{
    94  		NameValue: "A",
    95  		Names:     []string{"A"},
    96  	})
    97  	g.Add(&graphNodeRefChildTest{
    98  		NameValue: "B",
    99  		Refs:      []string{"C/A"},
   100  	})
   101  
   102  	tf := &ReferenceTransformer{}
   103  	if err := tf.Transform(&g); err != nil {
   104  		t.Fatalf("err: %s", err)
   105  	}
   106  
   107  	actual := strings.TrimSpace(g.String())
   108  	expected := strings.TrimSpace(testTransformRefBackupStr)
   109  	if actual != expected {
   110  		t.Fatalf("bad:\n\n%s", actual)
   111  	}
   112  }
   113  
   114  func TestReferenceTransformer_backupPrimary(t *testing.T) {
   115  	g := Graph{Path: RootModulePath}
   116  	g.Add(&graphNodeRefParentTest{
   117  		NameValue: "A",
   118  		Names:     []string{"A"},
   119  	})
   120  	g.Add(&graphNodeRefChildTest{
   121  		NameValue: "B",
   122  		Refs:      []string{"C/A"},
   123  	})
   124  	g.Add(&graphNodeRefParentTest{
   125  		NameValue: "C",
   126  		Names:     []string{"C"},
   127  	})
   128  
   129  	tf := &ReferenceTransformer{}
   130  	if err := tf.Transform(&g); err != nil {
   131  		t.Fatalf("err: %s", err)
   132  	}
   133  
   134  	actual := strings.TrimSpace(g.String())
   135  	expected := strings.TrimSpace(testTransformRefBackupPrimaryStr)
   136  	if actual != expected {
   137  		t.Fatalf("bad:\n\n%s", actual)
   138  	}
   139  }
   140  
   141  func TestReferenceTransformer_modulePath(t *testing.T) {
   142  	g := Graph{Path: RootModulePath}
   143  	g.Add(&graphNodeRefParentTest{
   144  		NameValue: "A",
   145  		Names:     []string{"A"},
   146  		PathValue: []string{"foo"},
   147  	})
   148  	g.Add(&graphNodeRefChildTest{
   149  		NameValue: "B",
   150  		Refs:      []string{"module.foo"},
   151  	})
   152  
   153  	tf := &ReferenceTransformer{}
   154  	if err := tf.Transform(&g); err != nil {
   155  		t.Fatalf("err: %s", err)
   156  	}
   157  
   158  	actual := strings.TrimSpace(g.String())
   159  	expected := strings.TrimSpace(testTransformRefModulePathStr)
   160  	if actual != expected {
   161  		t.Fatalf("bad:\n\n%s", actual)
   162  	}
   163  }
   164  
   165  func TestReferenceTransformer_modulePathNormalized(t *testing.T) {
   166  	g := Graph{Path: RootModulePath}
   167  	g.Add(&graphNodeRefParentTest{
   168  		NameValue: "A",
   169  		Names:     []string{"A"},
   170  		PathValue: []string{"root", "foo"},
   171  	})
   172  	g.Add(&graphNodeRefChildTest{
   173  		NameValue: "B",
   174  		Refs:      []string{"module.foo"},
   175  	})
   176  
   177  	tf := &ReferenceTransformer{}
   178  	if err := tf.Transform(&g); err != nil {
   179  		t.Fatalf("err: %s", err)
   180  	}
   181  
   182  	actual := strings.TrimSpace(g.String())
   183  	expected := strings.TrimSpace(testTransformRefModulePathStr)
   184  	if actual != expected {
   185  		t.Fatalf("bad:\n\n%s", actual)
   186  	}
   187  }
   188  
   189  func TestReferenceMapReferences(t *testing.T) {
   190  	cases := map[string]struct {
   191  		Nodes  []dag.Vertex
   192  		Check  dag.Vertex
   193  		Result []string
   194  	}{
   195  		"simple": {
   196  			Nodes: []dag.Vertex{
   197  				&graphNodeRefParentTest{
   198  					NameValue: "A",
   199  					Names:     []string{"A"},
   200  				},
   201  			},
   202  			Check: &graphNodeRefChildTest{
   203  				NameValue: "foo",
   204  				Refs:      []string{"A"},
   205  			},
   206  			Result: []string{"A"},
   207  		},
   208  	}
   209  
   210  	for tn, tc := range cases {
   211  		t.Run(tn, func(t *testing.T) {
   212  			rm := NewReferenceMap(tc.Nodes)
   213  			result, _ := rm.References(tc.Check)
   214  
   215  			var resultStr []string
   216  			for _, v := range result {
   217  				resultStr = append(resultStr, dag.VertexName(v))
   218  			}
   219  
   220  			sort.Strings(resultStr)
   221  			sort.Strings(tc.Result)
   222  			if !reflect.DeepEqual(resultStr, tc.Result) {
   223  				t.Fatalf("bad: %#v", resultStr)
   224  			}
   225  		})
   226  	}
   227  }
   228  
   229  func TestReferenceMapReferencedBy(t *testing.T) {
   230  	cases := map[string]struct {
   231  		Nodes  []dag.Vertex
   232  		Check  dag.Vertex
   233  		Result []string
   234  	}{
   235  		"simple": {
   236  			Nodes: []dag.Vertex{
   237  				&graphNodeRefChildTest{
   238  					NameValue: "A",
   239  					Refs:      []string{"A"},
   240  				},
   241  				&graphNodeRefChildTest{
   242  					NameValue: "B",
   243  					Refs:      []string{"A"},
   244  				},
   245  				&graphNodeRefChildTest{
   246  					NameValue: "C",
   247  					Refs:      []string{"B"},
   248  				},
   249  			},
   250  			Check: &graphNodeRefParentTest{
   251  				NameValue: "foo",
   252  				Names:     []string{"A"},
   253  			},
   254  			Result: []string{"A", "B"},
   255  		},
   256  	}
   257  
   258  	for tn, tc := range cases {
   259  		t.Run(tn, func(t *testing.T) {
   260  			rm := NewReferenceMap(tc.Nodes)
   261  			result := rm.ReferencedBy(tc.Check)
   262  
   263  			var resultStr []string
   264  			for _, v := range result {
   265  				resultStr = append(resultStr, dag.VertexName(v))
   266  			}
   267  
   268  			sort.Strings(resultStr)
   269  			sort.Strings(tc.Result)
   270  			if !reflect.DeepEqual(resultStr, tc.Result) {
   271  				t.Fatalf("bad: %#v", resultStr)
   272  			}
   273  		})
   274  	}
   275  }
   276  
   277  type graphNodeRefParentTest struct {
   278  	NameValue string
   279  	PathValue []string
   280  	Names     []string
   281  }
   282  
   283  func (n *graphNodeRefParentTest) Name() string                { return n.NameValue }
   284  func (n *graphNodeRefParentTest) ReferenceableName() []string { return n.Names }
   285  func (n *graphNodeRefParentTest) Path() []string              { return n.PathValue }
   286  
   287  type graphNodeRefChildTest struct {
   288  	NameValue string
   289  	PathValue []string
   290  	Refs      []string
   291  }
   292  
   293  func (n *graphNodeRefChildTest) Name() string         { return n.NameValue }
   294  func (n *graphNodeRefChildTest) References() []string { return n.Refs }
   295  func (n *graphNodeRefChildTest) Path() []string       { return n.PathValue }
   296  
   297  const testTransformRefBasicStr = `
   298  A
   299  B
   300    A
   301  `
   302  
   303  const testTransformRefBackupStr = `
   304  A
   305  B
   306    A
   307  `
   308  
   309  const testTransformRefBackupPrimaryStr = `
   310  A
   311  B
   312    C
   313  C
   314  `
   315  
   316  const testTransformRefModulePathStr = `
   317  A
   318  B
   319    A
   320  `
   321  
   322  const testTransformRefPathStr = `
   323  A
   324  B
   325    A
   326  child.A
   327  child.B
   328    child.A
   329  `