Skip to content

Commit a47e910

Browse files
authored
Add a test for filtering a non secret resource (#208)
Signed-off-by: Charlie Egan <[email protected]>
1 parent 9cd330b commit a47e910

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

pkg/datagatherer/k8s/fieldfilter_test.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func TestSelectMissingSelectedField(t *testing.T) {
8686
}
8787
}
8888

89-
func TestRedact(t *testing.T) {
89+
func TestRedactSecret(t *testing.T) {
9090
resource := &unstructured.Unstructured{
9191
Object: map[string]interface{}{
9292
"apiVersion": "v1",
@@ -137,6 +137,48 @@ func TestRedact(t *testing.T) {
137137
}
138138
}
139139

140+
func TestRedactPod(t *testing.T) {
141+
resource := &unstructured.Unstructured{
142+
Object: map[string]interface{}{
143+
"apiVersion": "v1",
144+
"kind": "Pod",
145+
"metadata": map[string]interface{}{
146+
"name": "example",
147+
"namespace": "example",
148+
"managedFields": []interface{}{},
149+
},
150+
"spec": map[string]interface{}{
151+
"serviceAccountName": "example",
152+
},
153+
},
154+
}
155+
156+
fieldsToRedact := []string{
157+
"metadata.managedFields",
158+
}
159+
160+
err := Redact(fieldsToRedact, resource)
161+
if err != nil {
162+
t.Fatalf("unexpected error: %s", err)
163+
}
164+
165+
bytes, err := json.MarshalIndent(resource, "", " ")
166+
expectedJSON := `{
167+
"apiVersion": "v1",
168+
"kind": "Pod",
169+
"metadata": {
170+
"name": "example",
171+
"namespace": "example"
172+
},
173+
"spec": {
174+
"serviceAccountName": "example"
175+
}
176+
}`
177+
if string(bytes) != expectedJSON {
178+
t.Fatalf("unexpected JSON: \ngot \n%s\nwant\n%s", string(bytes), expectedJSON)
179+
}
180+
}
181+
140182
func TestRedactMissingField(t *testing.T) {
141183
resource := &unstructured.Unstructured{
142184
Object: map[string]interface{}{

0 commit comments

Comments
 (0)