Add WithLabel list filter to serving client lib (#1054)

* add WithLabel list filter

Signed-off-by: Zbynek Roubalik <zroubali@redhat.com>

* adding tests

Signed-off-by: Zbynek Roubalik <zroubali@redhat.com>

* update changelog

Signed-off-by: Zbynek Roubalik <zroubali@redhat.com>
This commit is contained in:
Zbynek Roubalik 2020-10-09 16:26:21 +02:00 committed by GitHub
parent 145c9f0522
commit 4ebd7ed604
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 2 deletions

View File

@ -19,6 +19,10 @@
| 🎁
| Add Aliases to Help Command and Remove Aliases in Short
| https://github.com/knative/client/pull/1055[#1055]
| 🎁
| Add WithLabel list filter to serving client lib
| https://github.com/knative/client/pull/1054[#1054]
|===
## v0.18.0 (2020-10-07)

View File

@ -141,6 +141,13 @@ func WithService(service string) ListConfig {
}
}
// WithLabel filters on the provided label
func WithLabel(labelKey, labelValue string) ListConfig {
return func(lo *listConfigCollector) {
lo.Labels[labelKey] = labelValue
}
}
type knServingClient struct {
client clientv1.ServingV1Interface
namespace string

View File

@ -34,6 +34,7 @@ func TestMockKnClient(t *testing.T) {
// Record all services
recorder.GetService("hello", nil, nil)
recorder.ListServices(mock.Any(), nil, nil)
recorder.ListServices(mock.Any(), nil, nil)
recorder.CreateService(&servingv1.Service{}, nil)
recorder.UpdateService(&servingv1.Service{}, nil)
recorder.DeleteService("hello", time.Duration(10)*time.Second, nil)
@ -48,6 +49,7 @@ func TestMockKnClient(t *testing.T) {
// Call all services
client.GetService("hello")
client.ListServices(WithName("blub"))
client.ListServices(WithLabel("foo", "bar"))
client.CreateService(&servingv1.Service{})
client.UpdateService(&servingv1.Service{})
client.DeleteService("hello", time.Duration(10)*time.Second)

View File

@ -85,24 +85,45 @@ func TestListService(t *testing.T) {
serving, client := setup()
t.Run("list service returns a list of services", func(t *testing.T) {
labelKey := "labelKey"
labelValue := "labelValue"
labels := map[string]string{labelKey: labelValue}
incorrectLabels := map[string]string{"foo": "bar"}
service1 := newService("service-1")
service2 := newService("service-2")
service3 := newService("service-3-with-label")
service3.Labels = labels
service4 := newService("service-4-with-label")
service4.Labels = labels
service5 := newService("service-5-with-incorrect-label")
service5.Labels = incorrectLabels
serving.AddReactor("list", "services",
func(a clienttesting.Action) (bool, runtime.Object, error) {
assert.Equal(t, testNamespace, a.GetNamespace())
return true, &servingv1.ServiceList{Items: []servingv1.Service{*service1, *service2}}, nil
return true, &servingv1.ServiceList{Items: []servingv1.Service{*service1, *service2, *service3, *service4, *service5}}, nil
})
listServices, err := client.ListServices()
assert.NilError(t, err)
assert.Assert(t, len(listServices.Items) == 2)
assert.Assert(t, len(listServices.Items) == 5)
assert.Equal(t, listServices.Items[0].Name, "service-1")
assert.Equal(t, listServices.Items[1].Name, "service-2")
validateGroupVersionKind(t, listServices)
validateGroupVersionKind(t, &listServices.Items[0])
validateGroupVersionKind(t, &listServices.Items[1])
listFilteredServices, err := client.ListServices(WithLabel(labelKey, labelValue))
assert.NilError(t, err)
assert.Assert(t, len(listFilteredServices.Items) == 2)
assert.Equal(t, listFilteredServices.Items[0].Name, "service-3-with-label")
assert.Equal(t, listFilteredServices.Items[1].Name, "service-4-with-label")
validateGroupVersionKind(t, listFilteredServices)
validateGroupVersionKind(t, &listFilteredServices.Items[0])
validateGroupVersionKind(t, &listFilteredServices.Items[1])
})
}
func TestCreateService(t *testing.T) {