1
2
3
4 package parser_test
5
6 import (
7 "bytes"
8 _ "embed"
9 iofs "io/fs"
10 "os"
11 "sort"
12 "strings"
13 "testing"
14
15 "github.com/stretchr/testify/assert"
16 "github.com/stretchr/testify/require"
17 "sigs.k8s.io/kustomize/kyaml/fn/framework/parser"
18 )
19
20
21 var cm1String string
22
23
24 var cm2String string
25
26 var templateData = struct {
27 Name string `yaml:"name"`
28 }{Name: "tester"}
29
30 var cm1Success = strings.TrimSpace(`
31 apiVersion: v1
32 kind: ConfigMap
33 metadata:
34 name: appconfig
35 labels:
36 app: tester
37 data:
38 app: tester
39 `)
40
41 var cm2Success = strings.TrimSpace(`
42 apiVersion: v1
43 kind: ConfigMap
44 metadata:
45 name: env
46 labels:
47 app: tester
48 data:
49 env: production
50 `)
51
52 func TestTemplateFiles(t *testing.T) {
53 tests := []struct {
54 name string
55 paths []string
56 fs iofs.FS
57 expected []string
58 wantErr string
59 }{
60 {
61 name: "parses templates from file",
62 paths: []string{"testdata/cm1.template.yaml"},
63 expected: []string{cm1Success},
64 },
65 {
66 name: "accepts multiple inputs",
67 paths: []string{"testdata/cm1.template.yaml", "testdata/cm2.template.yaml"},
68 expected: []string{cm1Success, cm2Success},
69 },
70 {
71 name: "parses templates from directory",
72 paths: []string{"testdata"},
73 expected: []string{cm1Success, cm2Success},
74 },
75 {
76 name: "can be configured with an alternative FS",
77 fs: os.DirFS("testdata"),
78 paths: []string{"cm1.template.yaml"},
79 expected: []string{cm1Success},
80 },
81 {
82 name: "rejects non-.template.yaml files",
83 paths: []string{"testdata/ignore.yaml"},
84 wantErr: "file testdata/ignore.yaml does not have any of permitted extensions [.template.yaml]",
85 },
86 }
87 for _, tt := range tests {
88 t.Run(tt.name, func(t *testing.T) {
89 p := parser.TemplateFiles(tt.paths...)
90 if tt.fs != nil {
91 p = p.FromFS(tt.fs)
92 }
93 templates, err := p.Parse()
94 if tt.wantErr != "" {
95 require.EqualError(t, err, tt.wantErr)
96 return
97 }
98 require.NoError(t, err)
99
100 result := []string{}
101 for _, template := range templates {
102 w := bytes.NewBuffer([]byte{})
103 err := template.Execute(w, templateData)
104 require.NoError(t, err)
105 result = append(result, strings.TrimSpace(w.String()))
106 }
107 sort.Strings(tt.expected)
108 sort.Strings(result)
109 assert.Equal(t, len(result), len(tt.expected))
110 for i := range tt.expected {
111 assert.YAMLEq(t, tt.expected[i], result[i])
112 }
113 })
114 }
115 }
116
117 func TestTemplateParser_WithExtensions(t *testing.T) {
118 p := parser.TemplateFiles("testdata").WithExtensions(".json")
119 templates, err := p.Parse()
120 require.NoError(t, err)
121 require.Len(t, templates, 2)
122
123 p = p.WithExtensions(".yaml")
124 templates, err = p.Parse()
125 require.NoError(t, err)
126 require.Len(t, templates, 3)
127
128 p = p.WithExtensions(".yaml", ".json")
129 templates, err = p.Parse()
130 require.NoError(t, err)
131 require.Len(t, templates, 5)
132 }
133
134 func TestTemplateStrings(t *testing.T) {
135 tests := []struct {
136 name string
137 data []string
138 expected []string
139 }{
140 {
141 name: "parses templates from strings",
142 data: []string{cm1String},
143 expected: []string{cm1Success},
144 },
145 {
146 name: "accepts multiple inputs",
147 data: []string{cm1String, cm2String},
148 expected: []string{cm1Success, cm2Success},
149 },
150 }
151 for _, tt := range tests {
152 t.Run(tt.name, func(t *testing.T) {
153 p := parser.TemplateStrings(tt.data...)
154 templates, err := p.Parse()
155 require.NoError(t, err)
156
157 result := []string{}
158 for _, template := range templates {
159 w := bytes.NewBuffer([]byte{})
160 err := template.Execute(w, templateData)
161 require.NoError(t, err)
162 result = append(result, strings.TrimSpace(w.String()))
163 }
164 sort.Strings(tt.expected)
165 sort.Strings(result)
166 assert.Equal(t, len(result), len(tt.expected))
167 for i := range tt.expected {
168 assert.YAMLEq(t, tt.expected[i], result[i])
169 }
170 })
171 }
172 }
173
View as plain text