github.com/wuhuizuo/gomplate@v3.5.0+incompatible/tests/integration/datasources_file_test.go (about) 1 //+build integration 2 3 package integration 4 5 import ( 6 . "gopkg.in/check.v1" 7 8 "github.com/gotestyourself/gotestyourself/fs" 9 "github.com/gotestyourself/gotestyourself/icmd" 10 ) 11 12 type FileDatasourcesSuite struct { 13 tmpDir *fs.Dir 14 } 15 16 var _ = Suite(&FileDatasourcesSuite{}) 17 18 func (s *FileDatasourcesSuite) SetUpSuite(c *C) { 19 s.tmpDir = fs.NewDir(c, "gomplate-inttests", 20 fs.WithFiles(map[string]string{ 21 "config.json": `{"foo": {"bar": "baz"}}`, 22 "encrypted.json": `{ 23 "_public_key": "dfcf98785869cdfc4a59273bbdfe1bfcf6c44850a11ea9d84db21c89a802c057", 24 "password": "EJ[1:Cb1AY94Dl76xwHHrnJyh+Y+fAeovijPlFQZXSAuvZBc=:oCGZM6lbeXXOl2ONSKfLQ0AgaltrTpNU:VjegqQPPkOK1hSylMAbmcfusQImfkHCWZw==]" 25 }`, 26 "config.yml": "foo:\n bar: baz\n", 27 "config2.yml": "foo: bar\n", 28 "foo.csv": `A,B 29 A1,B1 30 A2,"foo"" 31 bar" 32 `, 33 "test.env": `FOO=a regular unquoted value 34 export BAR=another value, exports are ignored 35 36 # comments are totally ignored, as are blank lines 37 FOO.BAR = "values can be double-quoted, and shell\nescapes are supported" 38 39 BAZ = "variable expansion: ${FOO}" 40 QUX='single quotes ignore $variables' 41 `, 42 }), 43 fs.WithDir("sortorder", fs.WithFiles(map[string]string{ 44 "template": `aws_zones = { 45 {{- range $key, $value := (ds "core").cloud.aws.zones }} 46 {{ $key }} = "{{ $value }}" 47 {{- end }} 48 } 49 `, 50 "core.yaml": `cloud: 51 aws: 52 zones: 53 zonea: true 54 zoneb: false 55 zonec: true 56 zoned: true 57 zonee: false 58 zonef: false 59 `, 60 })), 61 ) 62 } 63 64 func (s *FileDatasourcesSuite) TearDownSuite(c *C) { 65 s.tmpDir.Remove() 66 } 67 68 func (s *FileDatasourcesSuite) TestFileDatasources(c *C) { 69 result := icmd.RunCommand(GomplateBin, 70 "-d", "config="+s.tmpDir.Join("config.json"), 71 "-i", `{{(datasource "config").foo.bar}}`, 72 ) 73 result.Assert(c, icmd.Expected{ExitCode: 0, Out: "baz"}) 74 75 result = icmd.RunCmd(icmd.Command(GomplateBin, 76 "-d", "config=config.json", 77 "-i", `{{ (ds "config").foo.bar }}`, 78 ), func(c *icmd.Cmd) { 79 c.Dir = s.tmpDir.Path() 80 }) 81 result.Assert(c, icmd.Expected{ExitCode: 0, Out: "baz"}) 82 83 result = icmd.RunCmd(icmd.Command(GomplateBin, 84 "-d", "config.json", 85 "-i", `{{ (ds "config").foo.bar }}`, 86 ), func(c *icmd.Cmd) { 87 c.Dir = s.tmpDir.Path() 88 }) 89 result.Assert(c, icmd.Expected{ExitCode: 0, Out: "baz"}) 90 91 result = icmd.RunCommand(GomplateBin, 92 "-i", `foo{{defineDatasource "config" `+"`"+s.tmpDir.Join("config.json")+"`"+`}}bar{{(datasource "config").foo.bar}}`, 93 ) 94 result.Assert(c, icmd.Expected{ExitCode: 0, Out: "foobarbaz"}) 95 96 result = icmd.RunCommand(GomplateBin, 97 "-d", "config="+s.tmpDir.Join("config.yml"), 98 "-i", `{{(datasource "config").foo.bar}}`, 99 ) 100 result.Assert(c, icmd.Expected{ExitCode: 0, Out: "baz"}) 101 102 result = icmd.RunCommand(GomplateBin, 103 "-d", "config="+s.tmpDir.Join("config2.yml"), 104 "-i", `{{(ds "config").foo}}`, 105 ) 106 result.Assert(c, icmd.Expected{ExitCode: 0, Out: "bar"}) 107 108 result = icmd.RunCommand(GomplateBin, 109 "-c", "config="+s.tmpDir.Join("config2.yml"), 110 "-i", `{{ .config.foo}}`, 111 ) 112 result.Assert(c, icmd.Expected{ExitCode: 0, Out: "bar"}) 113 114 result = icmd.RunCommand(GomplateBin, 115 "-c", ".="+s.tmpDir.Join("config2.yml"), 116 "-i", `{{ .foo}} {{ (ds ".").foo }}`, 117 ) 118 result.Assert(c, icmd.Expected{ExitCode: 0, Out: "bar bar"}) 119 120 result = icmd.RunCommand(GomplateBin, 121 "-d", "config="+s.tmpDir.Join("config2.yml"), 122 "-i", `{{ if (datasourceReachable "bogus") }}bogus!{{ end -}} 123 {{ if (datasourceReachable "config") -}} 124 {{ (ds "config").foo -}} 125 {{ end }}`, 126 ) 127 result.Assert(c, icmd.Expected{ExitCode: 0, Out: "bar"}) 128 129 result = icmd.RunCommand(GomplateBin, 130 "-d", "csv="+s.tmpDir.Join("foo.csv"), 131 "-i", `{{ index (index (ds "csv") 2) 1 }}`, 132 ) 133 result.Assert(c, icmd.Expected{ExitCode: 0, Out: `foo" 134 bar`}) 135 136 result = icmd.RunCommand(GomplateBin, 137 "-d", "config="+s.tmpDir.Join("config2.yml"), 138 "-i", `{{ include "config" }}`, 139 ) 140 result.Assert(c, icmd.Expected{ExitCode: 0, Out: `foo: bar`}) 141 142 result = icmd.RunCommand(GomplateBin, 143 "-d", "dir="+s.tmpDir.Path()+"/", 144 "-i", `{{ range (ds "dir") }}{{ . }} {{ end }}`, 145 ) 146 result.Assert(c, icmd.Expected{ExitCode: 0, Out: `config.json config.yml config2.yml encrypted.json foo.csv`}) 147 148 result = icmd.RunCmd(icmd.Command(GomplateBin, 149 "-d", "enc="+s.tmpDir.Join("encrypted.json"), 150 "-i", `{{ (ds "enc").password }}`, 151 ), func(c *icmd.Cmd) { 152 c.Env = []string{ 153 "EJSON_KEY=553da5790efd7ddc0e4829b69069478eec9ddddb17b69eca9801da37445b62bf", 154 } 155 }) 156 result.Assert(c, icmd.Expected{ExitCode: 0, Out: "swordfish"}) 157 158 result = icmd.RunCommand(GomplateBin, 159 "-d", "core="+s.tmpDir.Join("sortorder", "core.yaml"), 160 "-f", s.tmpDir.Join("sortorder", "template"), 161 ) 162 result.Assert(c, icmd.Expected{ExitCode: 0, Out: `aws_zones = { 163 zonea = "true" 164 zoneb = "false" 165 zonec = "true" 166 zoned = "true" 167 zonee = "false" 168 zonef = "false" 169 } 170 `}) 171 result = icmd.RunCommand(GomplateBin, 172 "-d", "envfile="+s.tmpDir.Join("test.env"), 173 "-i", `{{ (ds "envfile") | data.ToJSONPretty " " }}`, 174 ) 175 result.Assert(c, icmd.Expected{ExitCode: 0, Out: `{ 176 "BAR": "another value, exports are ignored", 177 "BAZ": "variable expansion: a regular unquoted value", 178 "FOO": "a regular unquoted value", 179 "FOO.BAR": "values can be double-quoted, and shell\nescapes are supported", 180 "QUX": "single quotes ignore $variables" 181 }`}) 182 }