@@ -247,6 +247,68 @@ func TestPEMBlocksFromFile(t *testing.T) {
247
247
}
248
248
}
249
249
250
+ func TestPEMBlocksFromFileEnv (t * testing.T ) {
251
+
252
+ // create temp file
253
+ file , err := ioutil .TempFile (os .TempDir (), "test" )
254
+ if err != nil {
255
+ t .Fatalf ("Unable to create temp file." )
256
+ }
257
+ defer os .Remove (file .Name ())
258
+
259
+ numberOfCertificates := 3
260
+ var pems []byte
261
+ for i := 0 ; i < numberOfCertificates ; i ++ {
262
+ publicKeyCert , _ , err := util .GenerateMockPublicPrivateKeyPairPEM (true )
263
+ if err != nil {
264
+ t .Fatalf ("Enable to generate a signer certificate: %v" , err )
265
+ }
266
+ pems = append (pems , publicKeyCert ... )
267
+ }
268
+
269
+ // write temp file
270
+ if err := ioutil .WriteFile (file .Name (), pems , 0666 ); err != nil {
271
+ t .Fatalf ("Unable to write to temp file: %v" , err )
272
+ }
273
+
274
+ testCases := []struct {
275
+ name string
276
+ data string
277
+ }{
278
+ {"Override" , "---\n Inner:\n Multiple:\n File: wrong_file" },
279
+ {"NoFileElement" , "---\n Inner:\n Multiple:\n " },
280
+ // {"NoElementAtAll", "---\nInner:\n"}, test case for another time
281
+ }
282
+
283
+ for _ , tc := range testCases {
284
+ t .Run (tc .name , func (t * testing.T ) {
285
+
286
+ envVar := "VIPERUTIL_INNER_MULTIPLE_FILE"
287
+ envVal := file .Name ()
288
+ os .Setenv (envVar , envVal )
289
+ defer os .Unsetenv (envVar )
290
+ config := viper .New ()
291
+ config .SetEnvPrefix (Prefix )
292
+ config .AutomaticEnv ()
293
+ replacer := strings .NewReplacer ("." , "_" )
294
+ config .SetEnvKeyReplacer (replacer )
295
+ config .SetConfigType ("yaml" )
296
+
297
+ if err := config .ReadConfig (bytes .NewReader ([]byte (tc .data ))); err != nil {
298
+ t .Fatalf ("Error reading config: %v" , err )
299
+ }
300
+ var uconf stringFromFileConfig
301
+ if err := EnhancedExactUnmarshal (config , & uconf ); err != nil {
302
+ t .Fatalf ("Failed to unmarshall: %v" , err )
303
+ }
304
+
305
+ if len (uconf .Inner .Multiple ) != 3 {
306
+ t .Fatalf (`Expected: "%v", Actual: "%v"` , numberOfCertificates , len (uconf .Inner .Multiple ))
307
+ }
308
+ })
309
+ }
310
+ }
311
+
250
312
func TestStringFromFileNotSpecified (t * testing.T ) {
251
313
252
314
yaml := fmt .Sprintf ("---\n Inner:\n Single:\n File:\n " )
@@ -280,33 +342,45 @@ func TestStringFromFileEnv(t *testing.T) {
280
342
t .Fatalf ("Unable to write to temp file." )
281
343
}
282
344
283
- envVar := "VIPERUTIL_INNER_SINGLE_FILE"
284
- envVal := file .Name ()
285
- os .Setenv (envVar , envVal )
286
- defer os .Unsetenv (envVar )
287
- config := viper .New ()
288
- config .SetEnvPrefix (Prefix )
289
- config .AutomaticEnv ()
290
- replacer := strings .NewReplacer ("." , "_" )
291
- config .SetEnvKeyReplacer (replacer )
292
- config .SetConfigType ("yaml" )
293
-
294
- data := "---\n Inner:\n Single:\n File: wrong_file"
295
-
296
- if err = config .ReadConfig (bytes .NewReader ([]byte (data ))); err != nil {
297
- t .Fatalf ("Error reading %s plugin config: %s" , Prefix , err )
345
+ testCases := []struct {
346
+ name string
347
+ data string
348
+ }{
349
+ {"Override" , "---\n Inner:\n Single:\n File: wrong_file" },
350
+ {"NoFileElement" , "---\n Inner:\n Single:\n " },
351
+ // {"NoElementAtAll", "---\nInner:\n"}, test case for another time
298
352
}
299
353
300
- var uconf stringFromFileConfig
354
+ for _ , tc := range testCases {
355
+ t .Run (tc .name , func (t * testing.T ) {
356
+ envVar := "VIPERUTIL_INNER_SINGLE_FILE"
357
+ envVal := file .Name ()
358
+ os .Setenv (envVar , envVal )
359
+ defer os .Unsetenv (envVar )
360
+ config := viper .New ()
361
+ config .SetEnvPrefix (Prefix )
362
+ config .AutomaticEnv ()
363
+ replacer := strings .NewReplacer ("." , "_" )
364
+ config .SetEnvKeyReplacer (replacer )
365
+ config .SetConfigType ("yaml" )
366
+
367
+ if err = config .ReadConfig (bytes .NewReader ([]byte (tc .data ))); err != nil {
368
+ t .Fatalf ("Error reading %s plugin config: %s" , Prefix , err )
369
+ }
301
370
302
- err = EnhancedExactUnmarshal (config , & uconf )
303
- if err != nil {
304
- t .Fatalf ("Failed to unmarshal with: %s" , err )
305
- }
371
+ var uconf stringFromFileConfig
306
372
307
- t .Log (uconf .Inner .Single )
373
+ err = EnhancedExactUnmarshal (config , & uconf )
374
+ if err != nil {
375
+ t .Fatalf ("Failed to unmarshal with: %s" , err )
376
+ }
308
377
309
- if ! reflect .DeepEqual (uconf .Inner .Single , expectedValue ) {
310
- t .Fatalf (`Expected: "%v", Actual: "%v"` , expectedValue , uconf .Inner .Single )
378
+ t .Log (uconf .Inner .Single )
379
+
380
+ if ! reflect .DeepEqual (uconf .Inner .Single , expectedValue ) {
381
+ t .Fatalf (`Expected: "%v", Actual: "%v"` , expectedValue , uconf .Inner .Single )
382
+ }
383
+ })
311
384
}
385
+
312
386
}
0 commit comments