summaryrefslogtreecommitdiff
path: root/internal/tests/integration/datasources_merge_test.go
blob: 96d9c62197f22c36871b869dc3561353c20adb2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package integration

import (
	"net/http"
	"net/http/httptest"
	"testing"

	"gotest.tools/v3/fs"
)

func setupDatasourcesMergeTest(t *testing.T) (*fs.Dir, *httptest.Server) {
	tmpDir := fs.NewDir(t, "gomplate-inttests",
		fs.WithFiles(map[string]string{
			"config.json": `{"foo": {"bar": "baz"}, "isDefault": false, "isOverride": true}`,
			"default.yml": "foo:\n  bar: qux\nother: true\nisDefault: true\nisOverride: false\n",
		}),
	)
	t.Cleanup(tmpDir.Remove)

	mux := http.NewServeMux()
	mux.HandleFunc("/foo.json", typeHandler("application/json", `{"foo": "bar"}`))
	mux.HandleFunc("/1.env", typeHandler("application/x-env", "FOO=1\nBAR=2\n"))
	mux.HandleFunc("/2.env", typeHandler("application/x-env", "FOO=3\n"))
	// this file is served with a misleading content type and extension, for
	// testing overriding the type
	mux.HandleFunc("/wrongtype.txt", typeHandler("text/html", `{"foo": "bar"}`))
	mux.HandleFunc("/params", paramHandler(t))

	srv := httptest.NewServer(mux)
	t.Cleanup(srv.Close)

	return tmpDir, srv
}

func TestDatasources_Merge(t *testing.T) {
	tmpDir, srv := setupDatasourcesMergeTest(t)

	t.Run("from two aliased datasources", func(t *testing.T) {
		o, e, err := cmd(t,
			"-d", "user="+tmpDir.Join("config.json"),
			"-d", "default="+tmpDir.Join("default.yml"),
			"-d", "config=merge:user|default",
			"-i", `{{ ds "config" | toJSON }}`,
		).run()
		assertSuccess(t, o, e, err, `{"foo":{"bar":"baz"},"isDefault":false,"isOverride":true,"other":true}`)
	})

	t.Run("with dynamic datasource", func(t *testing.T) {
		o, e, err := cmd(t,
			"-d", "default="+tmpDir.Join("default.yml"),
			"-d", "config=merge:user|default",
			"-i", `{{ defineDatasource "user" `+"`"+tmpDir.Join("config.json")+"`"+` }}{{ ds "config" | toJSON }}`,
		).run()
		assertSuccess(t, o, e, err, `{"foo":{"bar":"baz"},"isDefault":false,"isOverride":true,"other":true}`)
	})

	t.Run("with inline datasource", func(t *testing.T) {
		o, e, err := cmd(t,
			"-d", "default="+tmpDir.Join("default.yml"),
			"-d", "config=merge:"+srv.URL+"/foo.json|default",
			"-i", `{{ ds "config" | toJSON }}`,
		).run()
		assertSuccess(t, o, e, err, `{"foo":"bar","isDefault":true,"isOverride":false,"other":true}`)
	})

	t.Run("with two inline env datasources", func(t *testing.T) {
		o, e, err := cmd(t,
			"-c", "merged=merge:"+srv.URL+"/2.env|"+srv.URL+"/1.env",
			"-i", `FOO is {{ .merged.FOO }}`,
		).run()
		assertSuccess(t, o, e, err, `FOO is 3`)
	})

	t.Run("inline merge with inline datasource", func(t *testing.T) {
		o, e, err := cmd(t,
			"-c", "default="+tmpDir.Join("default.yml"),
			"-i", `{{ defineDatasource "merged" "merge:`+srv.URL+`/foo.json|default" -}}
{{ ds "merged" | toJSON }}`,
		).run()
		assertSuccess(t, o, e, err, `{"foo":"bar","isDefault":true,"isOverride":false,"other":true}`)
	})

	t.Run("with overridden type", func(t *testing.T) {
		o, e, err := cmd(t,
			"-d", "default="+tmpDir.Join("default.yml"),
			"-d", "wrongtype="+srv.URL+"/wrongtype.txt?type=application/json",
			"-d", "config=merge:wrongtype|default",
			"-i", `{{ ds "config" | toJSON }}`,
		).run()
		assertSuccess(t, o, e, err, `{"foo":"bar","isDefault":true,"isOverride":false,"other":true}`)
	})

	t.Run("type overridden by env var", func(t *testing.T) {
		o, e, err := cmd(t,
			"-d", "default="+tmpDir.Join("default.yml"),
			"-d", "wrongtype="+srv.URL+"/wrongtype.txt?_=application/json",
			"-d", "config=merge:wrongtype|default",
			"-i", `{{ ds "config" | toJSON }}`,
		).withEnv("GOMPLATE_TYPE_PARAM", "_").run()
		assertSuccess(t, o, e, err, `{"foo":"bar","isDefault":true,"isOverride":false,"other":true}`)

		o, e, err = cmd(t,
			"-c", "default="+tmpDir.Join("default.yml"),
			"-c", "params="+srv.URL+"/params?foo=bar&type=http&_type=application/json",
			"-c", "merged=merge:params|default",
			"-i", `{{ .merged | toJSON }}`,
		).withEnv("GOMPLATE_TYPE_PARAM", "_type").run()
		assertSuccess(t, o, e, err, `{"foo":["bar"],"isDefault":true,"isOverride":false,"other":true,"type":["http"]}`)
	})

	t.Run("from stdin", func(t *testing.T) {
		o, e, err := cmd(t,
			"-d", "stdindata=stdin:///in.json",
			"-d", "filedata="+srv.URL+"/foo.json",
			"-d", "merged=merge:stdindata|filedata",
			"-i", `{{ ds "merged" | toJSON }}`,
		).withStdin(`{"baz": "qux"}`).run()
		assertSuccess(t, o, e, err, `{"baz":"qux","foo":"bar"}`)
	})
}