From 74e84dbf5a339a3830ddd172fe8767ca0952cbb8 Mon Sep 17 00:00:00 2001 From: Rodrey Date: Thu, 24 Apr 2025 20:00:50 +0100 Subject: [PATCH] Add more test cases to TestDescribeSecret test (#131422) * Converted to parameterised tests. * Added test case for sorting with casing. * Formatted code. * Added test case for keys that contain numbers. --- .../kubectl/pkg/describe/describe_test.go | 88 +++++++++++++------ 1 file changed, 63 insertions(+), 25 deletions(-) diff --git a/staging/src/k8s.io/kubectl/pkg/describe/describe_test.go b/staging/src/k8s.io/kubectl/pkg/describe/describe_test.go index 34abc489e02..6728125750a 100644 --- a/staging/src/k8s.io/kubectl/pkg/describe/describe_test.go +++ b/staging/src/k8s.io/kubectl/pkg/describe/describe_test.go @@ -19,6 +19,7 @@ package describe import ( "bytes" "fmt" + "maps" "reflect" "strings" "testing" @@ -350,30 +351,63 @@ func TestDescribeTopologySpreadConstraints(t *testing.T) { } func TestDescribeSecret(t *testing.T) { - fake := fake.NewSimpleClientset(&corev1.Secret{ - ObjectMeta: metav1.ObjectMeta{ - Name: "bar", - Namespace: "foo", + testCases := []struct { + description string + data map[string][]byte // secret key -> secret in bytes + expected []string + }{ + { + description: "alphabetical ordering", + data: map[string][]byte{ + "username": []byte("YWRtaW4="), + "password": []byte("MWYyZDFlMmU2N2Rm"), + }, + expected: []string{"password", "username"}, }, - Data: map[string][]byte{ - "username": []byte("YWRtaW4="), - "password": []byte("MWYyZDFlMmU2N2Rm"), + { + description: "uppercase takes precedence", + data: map[string][]byte{ + "text": []byte("a3ViZXJuZXRlcwo="), + "Text": []byte("dGhpcyBpcyBhIHRlc3QK"), + "tExt": []byte("d2VpcmQgY2FzaW5nCg=="), + }, + expected: []string{"Text", "tExt", "text"}, + }, + { + description: "numbers take precedence", + data: map[string][]byte{ + "key_1": []byte("c29tZV9zZWNyZXQK"), + "1_key": []byte("c29tZV90ZXh0Cg=="), + }, + expected: []string{"1_key", "key_1"}, }, - }) - c := &describeClient{T: t, Namespace: "foo", Interface: fake} - d := SecretDescriber{c} - out, err := d.Describe("foo", "bar", DescriberSettings{}) - if err != nil { - t.Errorf("unexpected error: %v", err) - } - 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") { - t.Errorf("unexpected out: %s", out) - } - if strings.Contains(out, "YWRtaW4=") || strings.Contains(out, "MWYyZDFlMmU2N2Rm") { - t.Errorf("sensitive data should not be shown, unexpected out: %s", out) } - expectedOut := `Name: bar + for _, testCase := range testCases { + t.Run(testCase.description, func(t *testing.T) { + secret := &corev1.Secret{ + ObjectMeta: metav1.ObjectMeta{ + Name: "bar", + Namespace: "foo", + }, + Data: testCase.data, + } + fake := fake.NewSimpleClientset(secret) + c := &describeClient{T: t, Namespace: "foo", Interface: fake} + d := SecretDescriber{c} + + out, err := d.Describe("foo", "bar", DescriberSettings{}) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + + for value := range maps.Values(testCase.data) { + if strings.Contains(out, string(value)) { + t.Errorf("sensitive data should not be shown, unexpected out: %s", out) + } + } + + expectedOut := `Name: bar Namespace: foo Labels: Annotations: @@ -381,12 +415,16 @@ Annotations: Type: Data -==== -password: 16 bytes -username: 8 bytes -` +====` - assert.Equal(t, expectedOut, out) + for _, expectedKey := range testCase.expected { + expectedOut = fmt.Sprintf("%s\n%s: %d bytes", expectedOut, expectedKey, len(testCase.data[expectedKey])) + } + expectedOut = fmt.Sprintf("%s\n", expectedOut) + + assert.Equal(t, expectedOut, out) + }) + } } func TestDescribeNamespace(t *testing.T) {