@@ -23,6 +23,11 @@ import (
23
23
// updateGoldenFiles is a flag that can be set to update golden files.
24
24
var updateGoldenFiles = flag .Bool ("update" ,false ,"Update golden files" )
25
25
26
+ var namespaces = []string {
27
+ "default" ,
28
+ "coder" ,
29
+ }
30
+
26
31
var testCases = []testCase {
27
32
{
28
33
name :"default_values" ,
@@ -116,6 +121,7 @@ var testCases = []testCase{
116
121
117
122
type testCase struct {
118
123
name string // Name of the test case. This is used to control which values and golden file are used.
124
+ namespace string // Namespace is the name of the namespace the resources should be generated within
119
125
expectedError string // Expected error from running `helm template`.
120
126
}
121
127
@@ -124,7 +130,11 @@ func (tc testCase) valuesFilePath() string {
124
130
}
125
131
126
132
func (tc testCase )goldenFilePath ()string {
127
- return filepath .Join ("./testdata" ,tc .name + ".golden" )
133
+ if tc .namespace == "default" {
134
+ return filepath .Join ("./testdata" ,tc .name + ".golden" )
135
+ }
136
+
137
+ return filepath .Join ("./testdata" ,tc .name + "_" + tc .namespace + ".golden" )
128
138
}
129
139
130
140
func TestRenderChart (t * testing.T ) {
@@ -146,35 +156,41 @@ func TestRenderChart(t *testing.T) {
146
156
147
157
for _ ,tc := range testCases {
148
158
tc := tc
149
- t .Run (tc .name ,func (t * testing.T ) {
150
- t .Parallel ()
151
159
152
- // Ensure that the values file exists.
153
- valuesFilePath := tc .valuesFilePath ()
154
- if _ ,err := os .Stat (valuesFilePath );os .IsNotExist (err ) {
155
- t .Fatalf ("values file %q does not exist" ,valuesFilePath )
156
- }
160
+ for _ ,ns := range namespaces {
161
+ tc := tc
162
+ tc .namespace = ns
157
163
158
- // Run helm template with the values file.
159
- templateOutput ,err := runHelmTemplate (t ,helmPath ,".." ,valuesFilePath )
160
- if tc .expectedError != "" {
161
- require .Error (t ,err ,"helm template should have failed" )
162
- require .Contains (t ,templateOutput ,tc .expectedError ,"helm template output should contain expected error" )
163
- }else {
164
- require .NoError (t ,err ,"helm template should not have failed" )
165
- require .NotEmpty (t ,templateOutput ,"helm template output should not be empty" )
166
- goldenFilePath := tc .goldenFilePath ()
167
- goldenBytes ,err := os .ReadFile (goldenFilePath )
168
- require .NoError (t ,err ,"failed to read golden file %q" ,goldenFilePath )
169
-
170
- // Remove carriage returns to make tests pass on Windows.
171
- goldenBytes = bytes .Replace (goldenBytes , []byte ("\r " ), []byte ("" ),- 1 )
172
- expected := string (goldenBytes )
173
-
174
- require .NoError (t ,err ,"failed to load golden file %q" )
175
- require .Equal (t ,expected ,templateOutput )
176
- }
177
- })
164
+ t .Run (tc .namespace + "/" + tc .name ,func (t * testing.T ) {
165
+ t .Parallel ()
166
+
167
+ // Ensure that the values file exists.
168
+ valuesFilePath := tc .valuesFilePath ()
169
+ if _ ,err := os .Stat (valuesFilePath );os .IsNotExist (err ) {
170
+ t .Fatalf ("values file %q does not exist" ,valuesFilePath )
171
+ }
172
+
173
+ // Run helm template with the values file.
174
+ templateOutput ,err := runHelmTemplate (t ,helmPath ,".." ,valuesFilePath ,tc .namespace )
175
+ if tc .expectedError != "" {
176
+ require .Error (t ,err ,"helm template should have failed" )
177
+ require .Contains (t ,templateOutput ,tc .expectedError ,"helm template output should contain expected error" )
178
+ }else {
179
+ require .NoError (t ,err ,"helm template should not have failed" )
180
+ require .NotEmpty (t ,templateOutput ,"helm template output should not be empty" )
181
+ goldenFilePath := tc .goldenFilePath ()
182
+ goldenBytes ,err := os .ReadFile (goldenFilePath )
183
+ require .NoError (t ,err ,"failed to read golden file %q" ,goldenFilePath )
184
+
185
+ // Remove carriage returns to make tests pass on Windows.
186
+ goldenBytes = bytes .ReplaceAll (goldenBytes , []byte ("\r " ), []byte ("" ))
187
+ expected := string (goldenBytes )
188
+
189
+ require .NoError (t ,err ,"failed to load golden file %q" )
190
+ require .Equal (t ,expected ,templateOutput )
191
+ }
192
+ })
193
+ }
178
194
}
179
195
}
180
196
@@ -189,22 +205,28 @@ func TestUpdateGoldenFiles(t *testing.T) {
189
205
require .NoError (t ,err ,"failed to build Helm dependencies" )
190
206
191
207
for _ ,tc := range testCases {
208
+ tc := tc
192
209
if tc .expectedError != "" {
193
210
t .Logf ("skipping test case %q with render error" ,tc .name )
194
211
continue
195
212
}
196
213
197
- valuesPath := tc .valuesFilePath ()
198
- templateOutput ,err := runHelmTemplate (t ,helmPath ,".." ,valuesPath )
199
- if err != nil {
200
- t .Logf ("error running `helm template -f %q`: %v" ,valuesPath ,err )
201
- t .Logf ("output: %s" ,templateOutput )
202
- }
203
- require .NoError (t ,err ,"failed to run `helm template -f %q`" ,valuesPath )
214
+ for _ ,ns := range namespaces {
215
+ tc := tc
216
+ tc .namespace = ns
217
+
218
+ valuesPath := tc .valuesFilePath ()
219
+ templateOutput ,err := runHelmTemplate (t ,helmPath ,".." ,valuesPath ,tc .namespace )
220
+ if err != nil {
221
+ t .Logf ("error running `helm template -f %q`: %v" ,valuesPath ,err )
222
+ t .Logf ("output: %s" ,templateOutput )
223
+ }
224
+ require .NoError (t ,err ,"failed to run `helm template -f %q`" ,valuesPath )
204
225
205
- goldenFilePath := tc .goldenFilePath ()
206
- err = os .WriteFile (goldenFilePath , []byte (templateOutput ),0o644 )// nolint:gosec
207
- require .NoError (t ,err ,"failed to write golden file %q" ,goldenFilePath )
226
+ goldenFilePath := tc .goldenFilePath ()
227
+ err = os .WriteFile (goldenFilePath , []byte (templateOutput ),0o644 )// nolint:gosec
228
+ require .NoError (t ,err ,"failed to write golden file %q" ,goldenFilePath )
229
+ }
208
230
}
209
231
t .Log ("Golden files updated. Please review the changes and commit them." )
210
232
}
@@ -231,13 +253,13 @@ func updateHelmDependencies(t testing.TB, helmPath, chartDir string) error {
231
253
232
254
// runHelmTemplate runs helm template on the given chart with the given values and
233
255
// returns the raw output.
234
- func runHelmTemplate (t testing.TB ,helmPath ,chartDir ,valuesFilePath string ) (string ,error ) {
256
+ func runHelmTemplate (t testing.TB ,helmPath ,chartDir ,valuesFilePath , namespace string ) (string ,error ) {
235
257
// Ensure that valuesFilePath exists
236
258
if _ ,err := os .Stat (valuesFilePath );err != nil {
237
259
return "" ,xerrors .Errorf ("values file %q does not exist: %w" ,valuesFilePath ,err )
238
260
}
239
261
240
- cmd := exec .Command (helmPath ,"template" ,chartDir ,"-f" ,valuesFilePath ,"--namespace" ,"default" )
262
+ cmd := exec .Command (helmPath ,"template" ,chartDir ,"-f" ,valuesFilePath ,"--namespace" ,namespace )
241
263
t .Logf ("exec command: %v" ,cmd .Args )
242
264
out ,err := cmd .CombinedOutput ()
243
265
return string (out ),err