From f644d2a4fa6cf6db786ae56942670654aa989338 Mon Sep 17 00:00:00 2001 From: Mikhail Avramenko Date: Tue, 12 Oct 2021 10:17:43 +0700 Subject: [PATCH] added unit tests for find logic Signed-off-by: Mikhail Avramenko --- pkg/registry/etcd/ns_server.go | 2 +- pkg/registry/etcd/ns_server_test.go | 28 +++++++++++++++++++++++++++- pkg/registry/etcd/nse_server.go | 2 +- pkg/registry/etcd/nse_server_test.go | 26 ++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 3 deletions(-) diff --git a/pkg/registry/etcd/ns_server.go b/pkg/registry/etcd/ns_server.go index a4bc3f9..1be5392 100644 --- a/pkg/registry/etcd/ns_server.go +++ b/pkg/registry/etcd/ns_server.go @@ -155,7 +155,7 @@ func (n *etcdNSRegistryServer) Unregister(ctx context.Context, request *registry if err != nil { return nil, err } - err = n.client.NetworkservicemeshV1().NetworkServices("").Delete( + err = n.client.NetworkservicemeshV1().NetworkServices(n.ns).Delete( ctx, request.Name, metav1.DeleteOptions{}, diff --git a/pkg/registry/etcd/ns_server_test.go b/pkg/registry/etcd/ns_server_test.go index 199b37f..1fc6fea 100644 --- a/pkg/registry/etcd/ns_server_test.go +++ b/pkg/registry/etcd/ns_server_test.go @@ -40,7 +40,7 @@ func Test_NSReRegister(t *testing.T) { require.NoError(t, err) } -func Test_K8sNERegistry_ShouldMatchMetadataToName(t *testing.T) { +func Test_K8sNSRegistry_ShouldMatchMetadataToName(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() @@ -68,3 +68,29 @@ func Test_K8sNERegistry_ShouldMatchMetadataToName(t *testing.T) { require.Equal(t, "ns-1", nse.Name) } + +func Test_K8sNSRegistry_Find(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + + var myClientset = fake.NewSimpleClientset() + _, err := myClientset.NetworkservicemeshV1().NetworkServices("some namespace").Create(ctx, &v1.NetworkService{ + ObjectMeta: metav1.ObjectMeta{ + Name: "ns-1", + }, + }, metav1.CreateOptions{}) + require.NoError(t, err) + + c := adapters.NetworkServiceServerToClient(etcd.NewNetworkServiceRegistryServer(ctx, "", myClientset)) + stream, err := c.Find(ctx, ®istry.NetworkServiceQuery{ + NetworkService: ®istry.NetworkService{ + Name: "ns-1", + }, + }) + require.NoError(t, err) + + nse, err := stream.Recv() + require.NoError(t, err) + + require.Equal(t, "ns-1", nse.Name) +} diff --git a/pkg/registry/etcd/nse_server.go b/pkg/registry/etcd/nse_server.go index fd6f367..4e6da32 100644 --- a/pkg/registry/etcd/nse_server.go +++ b/pkg/registry/etcd/nse_server.go @@ -106,7 +106,7 @@ func (n *etcdNSERegistryServer) Unregister(ctx context.Context, request *registr if err != nil { return nil, err } - err = n.client.NetworkservicemeshV1().NetworkServiceEndpoints("").Delete( + err = n.client.NetworkservicemeshV1().NetworkServiceEndpoints(n.ns).Delete( ctx, request.Name, metav1.DeleteOptions{}) diff --git a/pkg/registry/etcd/nse_server_test.go b/pkg/registry/etcd/nse_server_test.go index ef5a4e2..4c0c49f 100644 --- a/pkg/registry/etcd/nse_server_test.go +++ b/pkg/registry/etcd/nse_server_test.go @@ -65,3 +65,29 @@ func Test_K8sNSERegistry_ShouldMatchMetadataToName(t *testing.T) { require.Equal(t, "nse-1", nse.Name) } + +func Test_K8sNSERegistry_Find(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + + var myClientset = fake.NewSimpleClientset() + _, err := myClientset.NetworkservicemeshV1().NetworkServiceEndpoints("some namespace").Create(ctx, &v1.NetworkServiceEndpoint{ + ObjectMeta: metav1.ObjectMeta{ + Name: "nse-1", + }, + }, metav1.CreateOptions{}) + require.NoError(t, err) + + c := adapters.NetworkServiceEndpointServerToClient(etcd.NewNetworkServiceEndpointRegistryServer(ctx, "", myClientset)) + stream, err := c.Find(ctx, ®istry.NetworkServiceEndpointQuery{ + NetworkServiceEndpoint: ®istry.NetworkServiceEndpoint{ + Name: "nse-1", + }, + }) + require.NoError(t, err) + + nse, err := stream.Recv() + require.NoError(t, err) + + require.Equal(t, "nse-1", nse.Name) +}