Skip to content

Commit 74e84db

Browse files
authored
Add more test cases to TestDescribeSecret test (kubernetes#131422)
* Converted to parameterised tests. * Added test case for sorting with casing. * Formatted code. * Added test case for keys that contain numbers.
1 parent 5da29d0 commit 74e84db

File tree

1 file changed

+63
-25
lines changed

1 file changed

+63
-25
lines changed

staging/src/k8s.io/kubectl/pkg/describe/describe_test.go

Lines changed: 63 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package describe
1919
import (
2020
"bytes"
2121
"fmt"
22+
"maps"
2223
"reflect"
2324
"strings"
2425
"testing"
@@ -350,43 +351,80 @@ func TestDescribeTopologySpreadConstraints(t *testing.T) {
350351
}
351352

352353
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"},
357366
},
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"},
361383
},
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)
374384
}
375385

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
377411
Namespace: foo
378412
Labels: <none>
379413
Annotations: <none>
380414
381415
Type:
382416
383417
Data
384-
====
385-
password: 16 bytes
386-
username: 8 bytes
387-
`
418+
====`
388419

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+
}
390428
}
391429

392430
func TestDescribeNamespace(t *testing.T) {

0 commit comments

Comments
 (0)