Skip to content

Commit 46a545d

Browse files
authored
Update list to filter resources using label selectors (#488)
Signed-off-by: jtucci <[email protected]>
1 parent 7e78376 commit 46a545d

File tree

2 files changed

+73
-22
lines changed

2 files changed

+73
-22
lines changed

pkg/test/step.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
k8serrors "k8s.io/apimachinery/pkg/api/errors"
14+
"k8s.io/apimachinery/pkg/api/meta"
1415
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1516
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1617
"k8s.io/apimachinery/pkg/labels"
@@ -230,7 +231,7 @@ func (s *Step) GetTimeout() int {
230231
return timeout
231232
}
232233

233-
func list(cl client.Client, gvk schema.GroupVersionKind, namespace string) ([]unstructured.Unstructured, error) {
234+
func list(cl client.Client, gvk schema.GroupVersionKind, namespace string, labelsMap map[string]string) ([]unstructured.Unstructured, error) {
234235
list := unstructured.UnstructuredList{}
235236
list.SetGroupVersionKind(gvk)
236237

@@ -239,6 +240,10 @@ func list(cl client.Client, gvk schema.GroupVersionKind, namespace string) ([]un
239240
listOptions = append(listOptions, client.InNamespace(namespace))
240241
}
241242

243+
if len(labelsMap) > 0 {
244+
listOptions = append(listOptions, client.MatchingLabels(labelsMap))
245+
}
246+
242247
if err := cl.List(context.TODO(), &list, listOptions...); err != nil {
243248
return []unstructured.Unstructured{}, err
244249
}
@@ -268,35 +273,39 @@ func (s *Step) CheckResource(expected runtime.Object, namespace string) []error
268273
gvk := expected.GetObjectKind().GroupVersionKind()
269274

270275
actuals := []unstructured.Unstructured{}
271-
272276
if name != "" {
273277
actual := unstructured.Unstructured{}
274278
actual.SetGroupVersionKind(gvk)
275279

276-
err = cl.Get(context.TODO(), client.ObjectKey{
280+
if err := cl.Get(context.TODO(), client.ObjectKey{
277281
Namespace: namespace,
278282
Name: name,
279-
}, &actual)
283+
}, &actual); err != nil {
284+
return append(testErrors, err)
285+
}
280286

281287
actuals = append(actuals, actual)
282288
} else {
283-
actuals, err = list(cl, gvk, namespace)
284-
if len(actuals) == 0 {
289+
m, err := meta.Accessor(expected)
290+
if err != nil {
291+
return append(testErrors, err)
292+
}
293+
matches, err := list(cl, gvk, namespace, m.GetLabels())
294+
if err != nil {
295+
return append(testErrors, err)
296+
}
297+
if len(matches) == 0 {
285298
testErrors = append(testErrors, fmt.Errorf("no resources matched of kind: %s", gvk.String()))
286299
}
300+
actuals = append(actuals, matches...)
287301
}
288-
if err != nil {
289-
return append(testErrors, err)
290-
}
291-
292302
expectedObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(expected)
293303
if err != nil {
294304
return append(testErrors, err)
295305
}
296306

297307
for _, actual := range actuals {
298308
actual := actual
299-
300309
tmpTestErrors := []error{}
301310

302311
if err := testutils.IsSubset(expectedObj, actual.UnstructuredContent()); err != nil {
@@ -358,7 +367,11 @@ func (s *Step) CheckResourceAbsent(expected runtime.Object, namespace string) er
358367

359368
actuals = []unstructured.Unstructured{actual}
360369
} else {
361-
actuals, err = list(cl, gvk, namespace)
370+
m, err := meta.Accessor(expected)
371+
if err != nil {
372+
return err
373+
}
374+
actuals, err = list(cl, gvk, namespace, m.GetLabels())
362375
if err != nil {
363376
return err
364377
}

pkg/test/step_test.go

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,34 +136,71 @@ func TestStepDeleteExisting(t *testing.T) {
136136
func TestCheckResource(t *testing.T) {
137137
for _, test := range []struct {
138138
testName string
139-
actual runtime.Object
139+
actual []runtime.Object
140140
expected runtime.Object
141141
shouldError bool
142142
}{
143143
{
144144
testName: "resource matches",
145-
actual: testutils.NewPod("hello", ""),
145+
actual: []runtime.Object{
146+
testutils.NewPod("hello", ""),
147+
},
146148
expected: testutils.NewPod("hello", ""),
147149
},
150+
{
151+
testName: "resource matches with labels",
152+
actual: []runtime.Object{
153+
testutils.WithSpec(t, testutils.NewPod("deploy-8b2d", ""),
154+
map[string]interface{}{
155+
"containers": nil,
156+
"serviceAccountName": "invalid",
157+
}),
158+
testutils.WithSpec(
159+
t,
160+
testutils.WithLabels(
161+
t,
162+
testutils.NewPod("deploy-8c2z", ""),
163+
map[string]string{"label": "my-label"},
164+
),
165+
map[string]interface{}{
166+
"containers": nil,
167+
"serviceAccountName": "valid",
168+
},
169+
),
170+
},
171+
172+
expected: testutils.WithSpec(
173+
t,
174+
testutils.WithLabels(
175+
t,
176+
testutils.NewPod("", ""),
177+
map[string]string{"label": "my-label"},
178+
),
179+
map[string]interface{}{
180+
"containers": nil,
181+
"serviceAccountName": "valid",
182+
},
183+
),
184+
},
148185
{
149186
testName: "resource mis-match",
150-
actual: testutils.NewPod("hello", ""),
187+
actual: []runtime.Object{testutils.NewPod("hello", "")},
151188
expected: testutils.WithSpec(t, testutils.NewPod("hello", ""), map[string]interface{}{"invalid": "key"}),
152189
shouldError: true,
153190
},
154191
{
155192
testName: "resource subset match",
156-
actual: testutils.WithSpec(t, testutils.NewPod("hello", ""), map[string]interface{}{
193+
actual: []runtime.Object{testutils.WithSpec(t, testutils.NewPod("hello", ""), map[string]interface{}{
157194
"containers": nil,
158195
"restartPolicy": "OnFailure",
159-
}),
196+
})},
160197
expected: testutils.WithSpec(t, testutils.NewPod("hello", ""), map[string]interface{}{
161198
"restartPolicy": "OnFailure",
162199
}),
163200
},
164201
{
165202
testName: "resource does not exist",
166-
actual: testutils.NewPod("other", ""),
203+
actual: []runtime.Object{testutils.NewPod("other", "")},
167204
expected: testutils.NewPod("hello", ""),
168205
shouldError: true,
169206
},
@@ -174,19 +211,20 @@ func TestCheckResource(t *testing.T) {
174211
fakeDiscovery := testutils.FakeDiscoveryClient()
175212
namespace := testNamespace
176213

177-
_, _, err := testutils.Namespaced(fakeDiscovery, test.actual, namespace)
178-
assert.Nil(t, err)
214+
for _, actualObj := range test.actual {
215+
_, _, err := testutils.Namespaced(fakeDiscovery, actualObj, namespace)
216+
assert.Nil(t, err)
217+
}
179218

180219
step := Step{
181220
Logger: testutils.NewTestLogger(t, ""),
182221
Client: func(bool) (client.Client, error) {
183-
return fake.NewClientBuilder().WithScheme(scheme.Scheme).WithRuntimeObjects(test.actual).Build(), nil
222+
return fake.NewClientBuilder().WithScheme(scheme.Scheme).WithRuntimeObjects(test.actual...).Build(), nil
184223
},
185224
DiscoveryClient: func() (discovery.DiscoveryInterface, error) { return fakeDiscovery, nil },
186225
}
187226

188227
errors := step.CheckResource(test.expected, namespace)
189-
190228
if test.shouldError {
191229
assert.NotEqual(t, []error{}, errors)
192230
} else {

0 commit comments

Comments
 (0)