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. Kubernetes-commit: 74e84dbf5a339a3830ddd172fe8767ca0952cbb8
This commit is contained in:
parent
3a0b77ee9b
commit
ae92d5f0bd
4
go.mod
4
go.mod
|
@ -29,10 +29,10 @@ require (
|
|||
github.com/stretchr/testify v1.10.0
|
||||
golang.org/x/sys v0.31.0
|
||||
gopkg.in/evanphx/json-patch.v4 v4.12.0
|
||||
k8s.io/api v0.0.0-20250423231958-d18a46229505
|
||||
k8s.io/api v0.0.0-20250424191346-22b26cd1ddab
|
||||
k8s.io/apimachinery v0.0.0-20250423231524-954960919938
|
||||
k8s.io/cli-runtime v0.0.0-20250424000510-540348251743
|
||||
k8s.io/client-go v0.0.0-20250423232513-451ac0fcb5bd
|
||||
k8s.io/client-go v0.0.0-20250424032241-1601be3d146a
|
||||
k8s.io/component-base v0.0.0-20250423233653-88ca0abd8349
|
||||
k8s.io/component-helpers v0.0.0-20250423233808-8b0a2e4e6cfd
|
||||
k8s.io/klog/v2 v2.130.1
|
||||
|
|
8
go.sum
8
go.sum
|
@ -196,14 +196,14 @@ gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
|
|||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
k8s.io/api v0.0.0-20250423231958-d18a46229505 h1:CzcGzeX7BpE8DYfaOipLutwMTAZHcii4Hf4OrE65sX8=
|
||||
k8s.io/api v0.0.0-20250423231958-d18a46229505/go.mod h1:PEZtCkQxQ/XohDih7vofdKAdIIiS9kFruj1lF/EUMLs=
|
||||
k8s.io/api v0.0.0-20250424191346-22b26cd1ddab h1:COtSQ1RUz1DYEA2HSx5IqXIy9dFXI3s/I6b6QKI5gDg=
|
||||
k8s.io/api v0.0.0-20250424191346-22b26cd1ddab/go.mod h1:PEZtCkQxQ/XohDih7vofdKAdIIiS9kFruj1lF/EUMLs=
|
||||
k8s.io/apimachinery v0.0.0-20250423231524-954960919938 h1:yoIMbzO4of8M4auqFKjNsbFlHJG9jCuoD+4sUJUPdn4=
|
||||
k8s.io/apimachinery v0.0.0-20250423231524-954960919938/go.mod h1:tJ77gZ1upNffdrQVxg+oIoEmvSIyTbz3RIPi9HKw+nw=
|
||||
k8s.io/cli-runtime v0.0.0-20250424000510-540348251743 h1:VxHfYkzCue96k3EJCVJXSRgMfqQ5jLBwGHjMQHp9Z7Y=
|
||||
k8s.io/cli-runtime v0.0.0-20250424000510-540348251743/go.mod h1:Xa4Pc4cf6RC4+/F6JZjw78d9mRdtp2HAkCiZZO5DupY=
|
||||
k8s.io/client-go v0.0.0-20250423232513-451ac0fcb5bd h1:rR8S6D2gcZ8zGdIobs1g8HLnwbSVvnTOXaI9LaS/TR4=
|
||||
k8s.io/client-go v0.0.0-20250423232513-451ac0fcb5bd/go.mod h1:NKnTaoOD5AOCgg0t9ZlglMkD6GXYAdkN5RfI1BYA4lo=
|
||||
k8s.io/client-go v0.0.0-20250424032241-1601be3d146a h1:/6PNswbel23ch5g9ojx1XSB3wigNHURhSF3ZFbE0ys0=
|
||||
k8s.io/client-go v0.0.0-20250424032241-1601be3d146a/go.mod h1:DyOICfF91RiP68/b0ZgrwTUtUget/i+CuGFY0i/i4g8=
|
||||
k8s.io/component-base v0.0.0-20250423233653-88ca0abd8349 h1:TdFXFBAukREkHLOoqrybaIpwLRgmdc4ntO4nqP9ly3A=
|
||||
k8s.io/component-base v0.0.0-20250423233653-88ca0abd8349/go.mod h1:d9qYA5xnPzVV2QGRVxRQkFCFhomjWs865vF8Nvfvhf8=
|
||||
k8s.io/component-helpers v0.0.0-20250423233808-8b0a2e4e6cfd h1:VtCZS7YCNPK1Q89k7bZTBkRGXtEdfYJulGi5s3VgDYA=
|
||||
|
|
|
@ -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: <none>
|
||||
Annotations: <none>
|
||||
|
@ -381,12 +415,16 @@ Annotations: <none>
|
|||
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) {
|
||||
|
|
Loading…
Reference in New Issue