Skip to content

Commit 8f99daa

Browse files
maelvlswallrj
andcommitted
annot-exclusion: TestRemoveUnstructuredKeys wasn't testing anything
Co-authored-by: Richard Wall <[email protected]>
1 parent 93a02ec commit 8f99daa

File tree

1 file changed

+36
-42
lines changed

1 file changed

+36
-42
lines changed

pkg/datagatherer/k8s/dynamic_test.go

+36-42
Original file line numberDiff line numberDiff line change
@@ -1090,57 +1090,57 @@ func waitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
10901090
func TestRemoveUnstructuredKeys(t *testing.T) {
10911091
t.Run("remove single key", run_TestRemoveUnstructuredKeys(tc_RemoveUnstructuredKeys{
10921092
givenPath: []string{"metadata", "annotations"},
1093-
givenExclude: []string{"^key1$"},
1093+
givenExclude: []string{"^toexclude$"},
10941094
givenObj: map[string]interface{}{
10951095
"metadata": map[string]interface{}{
1096-
"name": "foo",
10971096
"annotations": map[string]interface{}{
1098-
"key1": "value1",
1099-
"key2": "value2",
1097+
"toexclude": "foo",
1098+
"tokeep": "bar",
11001099
},
11011100
},
11021101
},
11031102
expectObj: map[string]interface{}{
11041103
"metadata": map[string]interface{}{
1105-
"name": "foo",
1104+
"annotations": map[string]interface{}{
1105+
"tokeep": "bar",
1106+
},
11061107
},
11071108
},
11081109
}))
11091110

11101111
t.Run("remove keys using multiple regexes", run_TestRemoveUnstructuredKeys(tc_RemoveUnstructuredKeys{
11111112
givenPath: []string{"metadata", "annotations"},
1112-
givenExclude: []string{"^key1$", "^key2$"},
1113+
givenExclude: []string{"^toexclude1$", "^toexclude2$"},
11131114
givenObj: map[string]interface{}{
11141115
"metadata": map[string]interface{}{
1115-
"name": "foo",
11161116
"annotations": map[string]interface{}{
1117-
"key1": "value1",
1118-
"key2": "value2",
1117+
"toexclude1": "foo",
1118+
"toexclude2": "bar",
11191119
},
11201120
},
11211121
},
11221122
expectObj: map[string]interface{}{
1123-
"metadata": map[string]interface{}{
1124-
"name": "foo",
1125-
},
1123+
"metadata": map[string]interface{}{"annotations": map[string]interface{}{}},
11261124
},
11271125
}))
11281126

11291127
t.Run("remove multiple keys with a single regex", run_TestRemoveUnstructuredKeys(tc_RemoveUnstructuredKeys{
11301128
givenPath: []string{"metadata", "annotations"},
1131-
givenExclude: []string{"key.*"},
1129+
givenExclude: []string{"toexclude.*"},
11321130
givenObj: map[string]interface{}{
11331131
"metadata": map[string]interface{}{
1134-
"name": "foo",
11351132
"annotations": map[string]interface{}{
1136-
"key1": "value1",
1137-
"key2": "value2",
1133+
"toexclude1": "foo",
1134+
"toexclude2": "bar",
1135+
"tokeep": "baz",
11381136
},
11391137
},
11401138
},
11411139
expectObj: map[string]interface{}{
11421140
"metadata": map[string]interface{}{
1143-
"name": "foo",
1141+
"annotations": map[string]interface{}{
1142+
"tokeep": "baz",
1143+
},
11441144
},
11451145
},
11461146
}))
@@ -1150,19 +1150,15 @@ func TestRemoveUnstructuredKeys(t *testing.T) {
11501150
givenExclude: []string{},
11511151
givenObj: map[string]interface{}{
11521152
"metadata": map[string]interface{}{
1153-
"name": "foo",
11541153
"annotations": map[string]interface{}{
1155-
"key1": "value1",
1156-
"key2": "value2",
1154+
"tokeep1": "foo",
11571155
},
11581156
},
11591157
},
11601158
expectObj: map[string]interface{}{
11611159
"metadata": map[string]interface{}{
1162-
"name": "foo",
11631160
"annotations": map[string]interface{}{
1164-
"key1": "value1",
1165-
"key2": "value2",
1161+
"tokeep1": "foo",
11661162
},
11671163
},
11681164
},
@@ -1181,13 +1177,8 @@ func TestRemoveUnstructuredKeys(t *testing.T) {
11811177
t.Run("works when the leaf field is nil", run_TestRemoveUnstructuredKeys(tc_RemoveUnstructuredKeys{
11821178
givenPath: []string{"metadata", "annotations"},
11831179
givenExclude: []string{},
1184-
givenObj: map[string]interface{}{
1185-
"metadata": map[string]interface{}{
1186-
"name": "foo",
1187-
"annotations": nil,
1188-
},
1189-
},
1190-
expectObj: map[string]interface{}{"metadata": map[string]interface{}{"name": "foo"}},
1180+
givenObj: map[string]interface{}{"metadata": map[string]interface{}{"annotations": nil}},
1181+
expectObj: map[string]interface{}{"metadata": map[string]interface{}{"annotations": nil}},
11911182
}))
11921183

11931184
t.Run("works when leaf field is unexpectedly not nil and not a known map", run_TestRemoveUnstructuredKeys(tc_RemoveUnstructuredKeys{
@@ -1208,6 +1199,7 @@ func TestRemoveUnstructuredKeys(t *testing.T) {
12081199
t.Run("works when the intermediate field is nil", run_TestRemoveUnstructuredKeys(tc_RemoveUnstructuredKeys{
12091200
givenPath: []string{"metadata", "annotations"},
12101201
givenObj: map[string]interface{}{"metadata": nil},
1202+
expectObj: map[string]interface{}{"metadata": nil},
12111203
}))
12121204

12131205
t.Run("works when the intermediate field is unexpectedly not nil and not a map", run_TestRemoveUnstructuredKeys(tc_RemoveUnstructuredKeys{
@@ -1228,36 +1220,37 @@ func run_TestRemoveUnstructuredKeys(tc tc_RemoveUnstructuredKeys) func(*testing.
12281220
return func(t *testing.T) {
12291221
t.Helper()
12301222
RemoveUnstructuredKeys(toRegexps(tc.givenExclude), &unstructured.Unstructured{Object: tc.givenObj}, tc.givenPath...)
1223+
assert.Equal(t, tc.expectObj, tc.givenObj)
12311224
}
12321225
}
12331226

12341227
func TestRemoveTypedKeys(t *testing.T) {
12351228
t.Run("remove single key", run_TestRemoveTypedKeys(tc_TestRemoveTypedKeys{
1236-
givenExclude: []string{"^key1$"},
1237-
given: map[string]string{"key1": "value1", "key2": "value2"},
1238-
expected: map[string]string{"key2": "value2"},
1229+
givenExclude: []string{"^toexclude$"},
1230+
given: map[string]string{"toexclude": "foo", "tokeep": "bar"},
1231+
expected: map[string]string{"tokeep": "bar"},
12391232
}))
12401233

12411234
t.Run("remove keys using multiple regexes", run_TestRemoveTypedKeys(tc_TestRemoveTypedKeys{
1242-
givenExclude: []string{"^key1$", "^key2$"},
1243-
given: map[string]string{"key1": "value1", "key2": "value2"},
1244-
expected: map[string]string{},
1235+
givenExclude: []string{"^toexclude1$", "^toexclude2$"},
1236+
given: map[string]string{"toexclude1": "foo", "toexclude2": "bar", "tokeep": "baz"},
1237+
expected: map[string]string{"tokeep": "baz"},
12451238
}))
12461239

12471240
t.Run("remove multiple keys with a single regex", run_TestRemoveTypedKeys(tc_TestRemoveTypedKeys{
1248-
givenExclude: []string{"key.*"},
1249-
given: map[string]string{"key1": "value1", "key2": "value2"},
1250-
expected: map[string]string{},
1241+
givenExclude: []string{"^toexclude.*"},
1242+
given: map[string]string{"toexclude1": "foo", "toexclude2": "bar", "tokeep": "baz"},
1243+
expected: map[string]string{"tokeep": "baz"},
12511244
}))
12521245

12531246
t.Run("with no regex, the object is untouched", run_TestRemoveTypedKeys(tc_TestRemoveTypedKeys{
12541247
givenExclude: []string{},
1255-
given: map[string]string{"key1": "value1", "key2": "value2"},
1256-
expected: map[string]string{"key1": "value1", "key2": "value2"},
1248+
given: map[string]string{"tokeep1": "foo", "tokeep2": "bar"},
1249+
expected: map[string]string{"tokeep1": "foo", "tokeep2": "bar"},
12571250
}))
12581251

12591252
t.Run("works when the map is nil", run_TestRemoveTypedKeys(tc_TestRemoveTypedKeys{
1260-
givenExclude: []string{"^key1$"},
1253+
givenExclude: []string{"^toexclude$"},
12611254
given: nil,
12621255
expected: nil,
12631256
}))
@@ -1271,6 +1264,7 @@ type tc_TestRemoveTypedKeys struct {
12711264

12721265
func run_TestRemoveTypedKeys(tc tc_TestRemoveTypedKeys) func(t *testing.T) {
12731266
return func(t *testing.T) {
1267+
t.Helper()
12741268
RemoveTypedKeys(toRegexps(tc.givenExclude), tc.given)
12751269
assert.Equal(t, tc.expected, tc.given)
12761270
}

0 commit comments

Comments
 (0)