Skip to content

Commit 29190ec

Browse files
drekledcwangmit01
authored andcommitted
Cleaning up tests to be more resilient
Small refactor of self render
1 parent 5fce8b7 commit 29190ec

File tree

2 files changed

+84
-77
lines changed

2 files changed

+84
-77
lines changed

mhlib/app.go

+45-39
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"io/ioutil"
2222
"os"
2323
"regexp"
24+
"strings"
2425
"text/template"
2526

2627
"github.com/imdario/mergo"
@@ -218,7 +219,7 @@ func (a *App) render(configFile string) (*string, *string, *[]byte, error) {
218219
// Self-render the main.yaml with gomplate functions and datasources
219220
// This does not apply to the app.yaml files.
220221
contents := string(data)
221-
renderedContents, err := selfRender(contents, true)
222+
renderedContents, err := selfRender(contents)
222223
if err != nil {
223224
return nil, nil, nil, fmt.Errorf("Failed to selfRender configFile %v: %v", configFile, err)
224225
}
@@ -241,7 +242,6 @@ func (a *App) render(configFile string) (*string, *string, *[]byte, error) {
241242
// combining literal with app.yaml
242243
data = append(data, appData...)
243244

244-
245245
// Fakechart to send to helm rendering engine
246246
fakeChart := &chart.Chart{
247247
Metadata: &chart.Metadata{
@@ -294,64 +294,70 @@ func (a *App) render(configFile string) (*string, *string, *[]byte, error) {
294294
return &chart, &chartVersion, &overrides, nil
295295
}
296296

297-
298-
func selfRender(templateValuesStr string, enableGomplate bool) (string, error) {
297+
func selfRender(templateValuesStr string) (string, error) {
299298
/*
300-
This function will accept an input string and run it through the
301-
templating engine as both the values dictionary as well as the
302-
template string. It will repeat the process until the templating
303-
result stops changing.
299+
This function will accept an input string and run it through the
300+
templating engine as both the values dictionary as well as the
301+
template string. It will repeat the process until the templating
302+
result stops changing.
304303
*/
305304

306-
type Values map[string]interface{}
305+
type gomplateConfig struct {
306+
Gomplate gomplate.Config `yaml:"gomplate,omitempty"`
307+
}
308+
var gomp gomplateConfig
309+
err := yaml.Unmarshal([]byte(templateValuesStr), &gomp)
310+
if err != nil {
311+
return "", err
312+
}
313+
314+
funcs := template.FuncMap{
315+
//Add case insensitive handling as marshalled inline structures are public by default, but may be lowercase templates
316+
"MyEq": strings.EqualFold,
317+
}
318+
if len(gomp.Gomplate.DataSources) > 0 {
319+
d, err := data.NewData(gomp.Gomplate.DataSources, gomp.Gomplate.DataSourceHeaders)
320+
if err != nil {
321+
return "", err
322+
}
323+
for k, v := range gomplate.Funcs(d) {
324+
funcs[k] = v
325+
}
326+
}
327+
tmpl := template.New("SelfTemplate").
328+
Delims("[[", "]]").
329+
Option("missingkey=error").
330+
Funcs(funcs)
307331

308332
lastRender := templateValuesStr
309333
for i := 0; i < 10; i++ {
310334

311335
// Unmarshal the file as a values dict
312-
vals := Values{}
313-
err := yaml.Unmarshal([]byte(templateValuesStr), &vals)
314-
if err != nil { return "", err }
315-
316-
tmpl := template.New("SelfTemplate")
317-
tmpl.Delims("[[", "]]")
318-
319-
if enableGomplate {
320-
// Read the defined gomplate datasources and
321-
// datasourcehaders from the values dict
322-
var dataSources []string
323-
var dataSourceHeaders []string
324-
var gompData gomplate.Config
325-
yaml.Unmarshal([]byte(templateValuesStr), &gompData)
326-
dataSources = gompData.DataSources
327-
dataSourceHeaders = gompData.DataSourceHeaders
328-
329-
// Access gomplate datasources and function library
330-
d, err := data.NewData(dataSources, dataSourceHeaders)
331-
if err != nil { return "", err }
332-
333-
// Configure go/text/template to use gomplate
334-
// datasources and function library.
335-
tmpl.Option("missingkey=error")
336-
tmpl.Funcs(gomplate.Funcs(d))
336+
vals := map[string]interface{}{}
337+
err := yaml.Unmarshal([]byte(lastRender), &vals)
338+
if err != nil {
339+
return "", err
337340
}
338341

339342
// Run the the file through the tempating engine as both values
340343
// file and template file
341-
tmpl.Parse(string(templateValuesStr))
342-
if err != nil { return "", err }
344+
tmpl.Parse(string(lastRender))
345+
if err != nil {
346+
return "", err
347+
}
343348
out := new(bytes.Buffer)
344349
err = tmpl.Execute(out, vals)
345-
if err != nil { return "", err }
350+
if err != nil {
351+
return "", err
352+
}
346353

347354
newRender := out.String()
348355
if lastRender == newRender {
349356
return newRender, nil // self-templating succeeded
350357
} else {
351358
lastRender = newRender
352-
templateValuesStr = newRender
353359
}
354360
}
355361

356-
return templateValuesStr, errors.New("Self-templating failed")
362+
return lastRender, errors.New("Self-templating failed")
357363
}

mhlib/app_test.go

+39-38
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,68 @@
11
package mhlib
22

33
import (
4+
"fmt"
5+
"net"
46
"testing"
57
)
68

7-
89
func TestSelfRender(t *testing.T) {
910

10-
// With datasources
11+
// Without datasources
1112
templateString := `
12-
gomplate:
13-
datasources:
14-
- "http_obj=https://httpbin.org/get"
15-
datasourceheaders: []
1613
values:
1714
a: foo
1815
b: '[[ .values.a ]]bar' # foobar
1916
c: '[[ .values.b ]]baz' # foobarbaz
20-
f: 'Func Test: [[ net.LookupIP "example.com" ]]'
2117
`
2218
expected := `
23-
gomplate:
24-
datasources:
25-
- "http_obj=https://httpbin.org/get"
26-
datasourceheaders: []
2719
values:
2820
a: foo
2921
b: 'foobar' # foobar
3022
c: 'foobarbaz' # foobarbaz
31-
f: 'Func Test: 10.114.236.103'
3223
`
33-
out, err := selfRender(templateString, true)
34-
if err != nil {
35-
t.Log(err)
36-
t.Fail()
37-
}
38-
if out != expected {
39-
t.Logf("Actual: %s\nExpected: %s\n", out, expected)
40-
t.Fail()
24+
if out, err := selfRender(templateString); out != expected {
25+
if err != nil {
26+
t.Log(err)
27+
t.FailNow()
28+
}
29+
t.Logf("\nActual: %s\nExpected: %s\n", out, expected)
30+
t.FailNow()
4131
}
4232

43-
// Without datasources
44-
templateString = `
45-
values:
46-
a: foo
47-
b: '[[ .values.a ]]bar' # foobar
48-
c: '[[ .values.b ]]baz' # foobarbaz
49-
`
50-
expected = `
33+
}
34+
35+
func TestSelfRenderDataSources(t *testing.T) {
36+
37+
// With datasources
38+
templateString := `
39+
gomplate:
40+
datasources:
41+
- "http_obj=https://httpbin.org/get"
42+
datasourceheaders: []
5143
values:
52-
a: foo
53-
b: 'foobar' # foobar
54-
c: 'foobarbaz' # foobarbaz
44+
a: 'Func Test: [[ net.LookupIP "example.com" ]]'
5545
`
56-
out, err = selfRender(templateString, true)
46+
47+
ips, err := net.LookupIP("example.com")
5748
if err != nil {
5849
t.Log(err)
59-
t.Fail()
50+
t.FailNow()
6051
}
61-
if out != expected {
62-
t.Logf("Actual: %s\nExpected: %s\n", out, expected)
63-
t.Fail()
52+
expected := fmt.Sprintf(`
53+
gomplate:
54+
datasources:
55+
- "http_obj=https://httpbin.org/get"
56+
datasourceheaders: []
57+
values:
58+
a: 'Func Test: %s'
59+
`, ips[0].String())
60+
if out, err := selfRender(templateString); out != expected {
61+
if err != nil {
62+
t.Log(err)
63+
t.FailNow()
64+
}
65+
t.Logf("\nActual: %s\nExpected: %s\n", out, expected)
66+
t.FailNow()
6467
}
65-
66-
6768
}

0 commit comments

Comments
 (0)