@@ -19,6 +19,7 @@ package describe
19
19
import (
20
20
"bytes"
21
21
"fmt"
22
+ "maps"
22
23
"reflect"
23
24
"strings"
24
25
"testing"
@@ -350,43 +351,80 @@ func TestDescribeTopologySpreadConstraints(t *testing.T) {
350
351
}
351
352
352
353
func TestDescribeSecret (t * testing.T ) {
353
- fake := fake .NewSimpleClientset (& corev1.Secret {
354
- ObjectMeta : metav1.ObjectMeta {
355
- Name : "bar" ,
356
- Namespace : "foo" ,
354
+ testCases := []struct {
355
+ description string
356
+ data map [string ][]byte // secret key -> secret in bytes
357
+ expected []string
358
+ }{
359
+ {
360
+ description : "alphabetical ordering" ,
361
+ data : map [string ][]byte {
362
+ "username" : []byte ("YWRtaW4=" ),
363
+ "password" : []byte ("MWYyZDFlMmU2N2Rm" ),
364
+ },
365
+ expected : []string {"password" , "username" },
357
366
},
358
- Data : map [string ][]byte {
359
- "username" : []byte ("YWRtaW4=" ),
360
- "password" : []byte ("MWYyZDFlMmU2N2Rm" ),
367
+ {
368
+ description : "uppercase takes precedence" ,
369
+ data : map [string ][]byte {
370
+ "text" : []byte ("a3ViZXJuZXRlcwo=" ),
371
+ "Text" : []byte ("dGhpcyBpcyBhIHRlc3QK" ),
372
+ "tExt" : []byte ("d2VpcmQgY2FzaW5nCg==" ),
373
+ },
374
+ expected : []string {"Text" , "tExt" , "text" },
375
+ },
376
+ {
377
+ description : "numbers take precedence" ,
378
+ data : map [string ][]byte {
379
+ "key_1" : []byte ("c29tZV9zZWNyZXQK" ),
380
+ "1_key" : []byte ("c29tZV90ZXh0Cg==" ),
381
+ },
382
+ expected : []string {"1_key" , "key_1" },
361
383
},
362
- })
363
- c := & describeClient {T : t , Namespace : "foo" , Interface : fake }
364
- d := SecretDescriber {c }
365
- out , err := d .Describe ("foo" , "bar" , DescriberSettings {})
366
- if err != nil {
367
- t .Errorf ("unexpected error: %v" , err )
368
- }
369
- if ! strings .Contains (out , "bar" ) || ! strings .Contains (out , "foo" ) || ! strings .Contains (out , "username" ) || ! strings .Contains (out , "8 bytes" ) || ! strings .Contains (out , "password" ) || ! strings .Contains (out , "16 bytes" ) {
370
- t .Errorf ("unexpected out: %s" , out )
371
- }
372
- if strings .Contains (out , "YWRtaW4=" ) || strings .Contains (out , "MWYyZDFlMmU2N2Rm" ) {
373
- t .Errorf ("sensitive data should not be shown, unexpected out: %s" , out )
374
384
}
375
385
376
- expectedOut := `Name: bar
386
+ for _ , testCase := range testCases {
387
+ t .Run (testCase .description , func (t * testing.T ) {
388
+ secret := & corev1.Secret {
389
+ ObjectMeta : metav1.ObjectMeta {
390
+ Name : "bar" ,
391
+ Namespace : "foo" ,
392
+ },
393
+ Data : testCase .data ,
394
+ }
395
+ fake := fake .NewSimpleClientset (secret )
396
+ c := & describeClient {T : t , Namespace : "foo" , Interface : fake }
397
+ d := SecretDescriber {c }
398
+
399
+ out , err := d .Describe ("foo" , "bar" , DescriberSettings {})
400
+ if err != nil {
401
+ t .Errorf ("unexpected error: %v" , err )
402
+ }
403
+
404
+ for value := range maps .Values (testCase .data ) {
405
+ if strings .Contains (out , string (value )) {
406
+ t .Errorf ("sensitive data should not be shown, unexpected out: %s" , out )
407
+ }
408
+ }
409
+
410
+ expectedOut := `Name: bar
377
411
Namespace: foo
378
412
Labels: <none>
379
413
Annotations: <none>
380
414
381
415
Type:
382
416
383
417
Data
384
- ====
385
- password: 16 bytes
386
- username: 8 bytes
387
- `
418
+ ====`
388
419
389
- assert .Equal (t , expectedOut , out )
420
+ for _ , expectedKey := range testCase .expected {
421
+ expectedOut = fmt .Sprintf ("%s\n %s: %d bytes" , expectedOut , expectedKey , len (testCase .data [expectedKey ]))
422
+ }
423
+ expectedOut = fmt .Sprintf ("%s\n " , expectedOut )
424
+
425
+ assert .Equal (t , expectedOut , out )
426
+ })
427
+ }
390
428
}
391
429
392
430
func TestDescribeNamespace (t * testing.T ) {
0 commit comments