about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2018-06-26T10·25+0200
committerVincent Ambo <github@tazj.in>2018-06-26T19·06+0200
commit6d8214bfc8eb9733f0adf58a629c77950c34e80d (patch)
treee915ac77b68d50c76e80eb4b1d5b3a0808444640
parent4a6f087fbfe84731db0a8e5084364ad0f6ad5212 (diff)
test(context): Introduce an explicit value merging test
Introduces a test which will merge variables defined at every possible
layer together and ensure that the loaded context configuration is as
expected.

The test data provides an actual resource set template that can be
tested locally from a kontemplate source checkout:

    kontemplate template context/testdata/merging/context.yaml --var cliVar=cliVar
-rw-r--r--context/context_test.go22
-rw-r--r--context/testdata/merging/context.yaml15
-rw-r--r--context/testdata/merging/import-vars.yaml4
-rw-r--r--context/testdata/merging/resource/default.yaml5
-rw-r--r--context/testdata/merging/resource/output.yaml5
5 files changed, 51 insertions, 0 deletions
diff --git a/context/context_test.go b/context/context_test.go
index e700cf41978a..34c77cc9b7c6 100644
--- a/context/context_test.go
+++ b/context/context_test.go
@@ -293,3 +293,25 @@ func TestSetInvalidVariablesFromArguments(t *testing.T) {
 		t.Error("Expected invalid variable to return an error")
 	}
 }
+
+// This test ensures that variables are merged in the correct order.
+// Please consult the test data in `testdata/merging`.
+func TestValueMergePrecedence(t *testing.T) {
+	cliVars:= []string{"cliVar=cliVar"}
+	ctx, _ := LoadContext("testdata/merging/context.yaml", &cliVars)
+
+	expected := map[string]interface{}{
+		"defaultVar": "defaultVar",
+		"importVar": "importVar",
+		"globalVar": "globalVar",
+		"includeVar": "includeVar",
+		"cliVar": "cliVar",
+	}
+
+	result := ctx.ResourceSets[0].Values
+
+	if !reflect.DeepEqual(expected, result) {
+		t.Errorf("Merged values did not match expected result: \n%v", result)
+		t.Fail()
+	}
+}
diff --git a/context/testdata/merging/context.yaml b/context/testdata/merging/context.yaml
new file mode 100644
index 000000000000..df30d3d8cbe3
--- /dev/null
+++ b/context/testdata/merging/context.yaml
@@ -0,0 +1,15 @@
+# This context file is intended to test the merge hierarchy of
+# variables defined at different levels.
+---
+context: merging.in.kontemplate.works
+global:
+  globalVar: globalVar
+  includeVar: should be overridden (global)
+  cliVar: should be overridden (global)
+import:
+  - import-vars.yaml
+include:
+  - name: resource
+    values:
+      includeVar: includeVar
+      cliVar: should be overridden (include)
diff --git a/context/testdata/merging/import-vars.yaml b/context/testdata/merging/import-vars.yaml
new file mode 100644
index 000000000000..2a51352571a6
--- /dev/null
+++ b/context/testdata/merging/import-vars.yaml
@@ -0,0 +1,4 @@
+importVar: importVar
+globalVar: should be overridden (import)
+includeVar: should be overridden (import)
+cliVar: should be overridden (import)
diff --git a/context/testdata/merging/resource/default.yaml b/context/testdata/merging/resource/default.yaml
new file mode 100644
index 000000000000..040a19aaba25
--- /dev/null
+++ b/context/testdata/merging/resource/default.yaml
@@ -0,0 +1,5 @@
+defaultVar: defaultVar
+importVar: should be overridden (default)
+globalVar: should be overridden (default)
+includeVar: should be overridden (default)
+cliVar: should be overridden (default)
diff --git a/context/testdata/merging/resource/output.yaml b/context/testdata/merging/resource/output.yaml
new file mode 100644
index 000000000000..5920b2720780
--- /dev/null
+++ b/context/testdata/merging/resource/output.yaml
@@ -0,0 +1,5 @@
+defaultVar: {{ .defaultVar }}
+importVar: {{ .importVar }}
+globalVar: {{ .globalVar }}
+includeVar: {{ .includeVar }}
+cliVar: {{ .cliVar }}