github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/internal/tests/integration/datasources_file_test.go (about) 1 package integration 2 3 import ( 4 "path/filepath" 5 "testing" 6 7 "gotest.tools/v3/fs" 8 ) 9 10 func setupDatasourcesFileTest(t *testing.T) *fs.Dir { 11 tmpDir := fs.NewDir(t, "gomplate-inttests", 12 fs.WithFiles(map[string]string{ 13 "config.json": `{"foo": {"bar": "baz"}}`, 14 "ajsonfile": `{"foo": {"bar": "baz"}}`, 15 "encrypted.json": `{ 16 "_public_key": "dfcf98785869cdfc4a59273bbdfe1bfcf6c44850a11ea9d84db21c89a802c057", 17 "password": "EJ[1:Cb1AY94Dl76xwHHrnJyh+Y+fAeovijPlFQZXSAuvZBc=:oCGZM6lbeXXOl2ONSKfLQ0AgaltrTpNU:VjegqQPPkOK1hSylMAbmcfusQImfkHCWZw==]" 18 }`, 19 "config.yml": "foo:\n bar: baz\n", 20 "config2.yml": "foo: bar\n", 21 "foo.csv": `A,B 22 A1,B1 23 A2,"foo"" 24 bar" 25 `, 26 "test.env": `FOO=a regular unquoted value 27 export BAR=another value, exports are ignored 28 29 # comments are totally ignored, as are blank lines 30 FOO.BAR = "values can be double-quoted, and shell\nescapes are supported" 31 32 BAZ = "variable expansion: ${FOO}" 33 QUX='single quotes ignore $variables' 34 `, 35 "test.cue": `package core 36 37 import "regexp" 38 39 matches: regexp.FindSubmatch(#"^([^:]*):(\d+)$"#, "localhost:443") 40 41 one: 1 42 two: 2 43 44 // A field using quotes. 45 "two-and-a-half": 2.5 46 47 list: [ 48 1, 49 2, 50 3, 51 ] 52 `, 53 }), 54 fs.WithDir("sortorder", fs.WithFiles(map[string]string{ 55 "template": `aws_zones = { 56 {{- range $key, $value := (ds "core").cloud.aws.zones }} 57 {{ $key }} = "{{ $value }}" 58 {{- end }} 59 } 60 `, 61 "core.yaml": `cloud: 62 aws: 63 zones: 64 zonea: true 65 zoneb: false 66 zonec: true 67 zoned: true 68 zonee: false 69 zonef: false 70 `, 71 })), 72 ) 73 74 t.Cleanup(tmpDir.Remove) 75 76 return tmpDir 77 } 78 79 func TestDatasources_File(t *testing.T) { 80 tmpDir := setupDatasourcesFileTest(t) 81 82 o, e, err := cmd(t, 83 "-d", "config="+tmpDir.Join("config.json"), 84 "-i", `{{(datasource "config").foo.bar}}`).run() 85 assertSuccess(t, o, e, err, "baz") 86 87 o, e, err = cmd(t, "-d", "dir="+tmpDir.Path()+string(filepath.Separator), 88 "-i", `{{ (datasource "dir" "config.json").foo.bar }}`).run() 89 assertSuccess(t, o, e, err, "baz") 90 91 o, e, err = cmd(t, "-d", "config=config.json", 92 "-i", `{{ (ds "config").foo.bar }}`).withDir(tmpDir.Path()).run() 93 assertSuccess(t, o, e, err, "baz") 94 95 o, e, err = cmd(t, "-d", "config.json", 96 "-i", `{{ (ds "config").foo.bar }}`).withDir(tmpDir.Path()).run() 97 assertSuccess(t, o, e, err, "baz") 98 99 o, e, err = cmd(t, "-i", 100 `foo{{defineDatasource "config" `+"`"+tmpDir.Join("config.json")+"`"+`}}bar{{(datasource "config").foo.bar}}`). 101 run() 102 assertSuccess(t, o, e, err, "foobarbaz") 103 104 o, e, err = cmd(t, "-i", 105 `foo{{defineDatasource "config" `+"`"+tmpDir.Join("ajsonfile")+"?type=application/json`"+`}}bar{{(datasource "config").foo.bar}}`). 106 run() 107 assertSuccess(t, o, e, err, "foobarbaz") 108 109 o, e, err = cmd(t, "-d", "config="+tmpDir.Join("config.yml"), 110 "-i", `{{(datasource "config").foo.bar}}`).run() 111 assertSuccess(t, o, e, err, "baz") 112 113 o, e, err = cmd(t, "-d", "config="+tmpDir.Join("config2.yml"), 114 "-i", `{{(ds "config").foo}}`).run() 115 assertSuccess(t, o, e, err, "bar") 116 117 o, e, err = cmd(t, "-c", "config="+tmpDir.Join("config2.yml"), 118 "-i", `{{ .config.foo}}`).run() 119 assertSuccess(t, o, e, err, "bar") 120 121 o, e, err = cmd(t, "-c", ".="+tmpDir.Join("config2.yml"), 122 "-i", `{{ .foo}} {{ (ds ".").foo }}`).run() 123 assertSuccess(t, o, e, err, "bar bar") 124 125 o, e, err = cmd(t, "-d", "config="+tmpDir.Join("config2.yml"), 126 "-i", `{{ if (datasourceReachable "bogus") }}bogus!{{ end -}} 127 {{ if (datasourceReachable "config") -}} 128 {{ (ds "config").foo -}} 129 {{ end }}`).run() 130 assertSuccess(t, o, e, err, "bar") 131 132 o, e, err = cmd(t, "-d", "csv="+tmpDir.Join("foo.csv"), 133 "-i", `{{ index (index (ds "csv") 2) 1 }}`).run() 134 assertSuccess(t, o, e, err, "foo\"\nbar") 135 136 o, e, err = cmd(t, "-d", "config="+tmpDir.Join("config2.yml"), 137 "-i", `{{ include "config" }}`).run() 138 assertSuccess(t, o, e, err, "foo: bar\n") 139 140 o, e, err = cmd(t, "-d", "dir="+tmpDir.Path()+"/", 141 "-i", `{{ range (ds "dir") }}{{ . }} {{ end }}`).run() 142 assertSuccess(t, o, e, err, "ajsonfile config.json config.yml config2.yml encrypted.json foo.csv sortorder test.cue test.env ") 143 144 o, e, err = cmd(t, "-d", "enc="+tmpDir.Join("encrypted.json"), 145 "-i", `{{ (ds "enc").password }}`). 146 withEnv("EJSON_KEY", "553da5790efd7ddc0e4829b69069478eec9ddddb17b69eca9801da37445b62bf"). 147 run() 148 assertSuccess(t, o, e, err, "swordfish") 149 150 o, e, err = cmd(t, "-d", "core="+tmpDir.Join("sortorder", "core.yaml"), 151 "-f", tmpDir.Join("sortorder", "template")).run() 152 assertSuccess(t, o, e, err, `aws_zones = { 153 zonea = "true" 154 zoneb = "false" 155 zonec = "true" 156 zoned = "true" 157 zonee = "false" 158 zonef = "false" 159 } 160 `) 161 162 o, e, err = cmd(t, "-d", "envfile="+tmpDir.Join("test.env"), 163 "-i", `{{ (ds "envfile") | data.ToJSONPretty " " }}`).run() 164 assertSuccess(t, o, e, err, `{ 165 "BAR": "another value, exports are ignored", 166 "BAZ": "variable expansion: a regular unquoted value", 167 "FOO": "a regular unquoted value", 168 "FOO.BAR": "values can be double-quoted, and shell\nescapes are supported", 169 "QUX": "single quotes ignore $variables" 170 }`) 171 172 o, e, err = cmd(t, "-d", "cuedata="+tmpDir.Join("test.cue"), 173 "-i", `{{ (ds "cuedata").matches | data.ToJSONPretty " " }}`).run() 174 assertSuccess(t, o, e, err, `[ 175 "localhost:443", 176 "localhost", 177 "443" 178 ]`) 179 } 180 181 func TestDatasources_File_Directory(t *testing.T) { 182 tmpDir := setupDatasourcesFileTest(t) 183 184 o, e, err := cmd(t, 185 "-d", "data=sortorder/", 186 "-i", `{{ range (ds "data") }}{{ if strings.HasSuffix ".yaml" . -}} 187 {{ . }} root key: {{ range $k, $v := ds "data" . }}{{ $k }}{{ end }} 188 {{- end }}{{ end }}`). 189 withDir(tmpDir.Path()).run() 190 assertSuccess(t, o, e, err, "core.yaml root key: cloud") 191 }