mirror of https://github.com/knative/client.git
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:
parent
145c9f0522
commit
4ebd7ed604
|
|
@ -19,6 +19,10 @@
|
||||||
| 🎁
|
| 🎁
|
||||||
| Add Aliases to Help Command and Remove Aliases in Short
|
| Add Aliases to Help Command and Remove Aliases in Short
|
||||||
| https://github.com/knative/client/pull/1055[#1055]
|
| 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)
|
## v0.18.0 (2020-10-07)
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
type knServingClient struct {
|
||||||
client clientv1.ServingV1Interface
|
client clientv1.ServingV1Interface
|
||||||
namespace string
|
namespace string
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,7 @@ func TestMockKnClient(t *testing.T) {
|
||||||
// Record all services
|
// Record all services
|
||||||
recorder.GetService("hello", nil, nil)
|
recorder.GetService("hello", nil, nil)
|
||||||
recorder.ListServices(mock.Any(), nil, nil)
|
recorder.ListServices(mock.Any(), nil, nil)
|
||||||
|
recorder.ListServices(mock.Any(), nil, nil)
|
||||||
recorder.CreateService(&servingv1.Service{}, nil)
|
recorder.CreateService(&servingv1.Service{}, nil)
|
||||||
recorder.UpdateService(&servingv1.Service{}, nil)
|
recorder.UpdateService(&servingv1.Service{}, nil)
|
||||||
recorder.DeleteService("hello", time.Duration(10)*time.Second, nil)
|
recorder.DeleteService("hello", time.Duration(10)*time.Second, nil)
|
||||||
|
|
@ -48,6 +49,7 @@ func TestMockKnClient(t *testing.T) {
|
||||||
// Call all services
|
// Call all services
|
||||||
client.GetService("hello")
|
client.GetService("hello")
|
||||||
client.ListServices(WithName("blub"))
|
client.ListServices(WithName("blub"))
|
||||||
|
client.ListServices(WithLabel("foo", "bar"))
|
||||||
client.CreateService(&servingv1.Service{})
|
client.CreateService(&servingv1.Service{})
|
||||||
client.UpdateService(&servingv1.Service{})
|
client.UpdateService(&servingv1.Service{})
|
||||||
client.DeleteService("hello", time.Duration(10)*time.Second)
|
client.DeleteService("hello", time.Duration(10)*time.Second)
|
||||||
|
|
|
||||||
|
|
@ -85,24 +85,45 @@ func TestListService(t *testing.T) {
|
||||||
serving, client := setup()
|
serving, client := setup()
|
||||||
|
|
||||||
t.Run("list service returns a list of services", func(t *testing.T) {
|
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")
|
service1 := newService("service-1")
|
||||||
service2 := newService("service-2")
|
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",
|
serving.AddReactor("list", "services",
|
||||||
func(a clienttesting.Action) (bool, runtime.Object, error) {
|
func(a clienttesting.Action) (bool, runtime.Object, error) {
|
||||||
assert.Equal(t, testNamespace, a.GetNamespace())
|
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()
|
listServices, err := client.ListServices()
|
||||||
assert.NilError(t, err)
|
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[0].Name, "service-1")
|
||||||
assert.Equal(t, listServices.Items[1].Name, "service-2")
|
assert.Equal(t, listServices.Items[1].Name, "service-2")
|
||||||
validateGroupVersionKind(t, listServices)
|
validateGroupVersionKind(t, listServices)
|
||||||
validateGroupVersionKind(t, &listServices.Items[0])
|
validateGroupVersionKind(t, &listServices.Items[0])
|
||||||
validateGroupVersionKind(t, &listServices.Items[1])
|
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) {
|
func TestCreateService(t *testing.T) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue