Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discover should use strict name match #687

Merged
merged 1 commit into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions pkg/networkservice/common/discover/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ func (d *discoverCandidatesServer) discoverNetworkServiceEndpoint(ctx context.Co
}
nseList := registry.ReadNetworkServiceEndpointList(nseStream)

if len(nseList) != 0 {
return nseList[0], nil
for _, nse := range nseList {
if nse.Name == nseName {
return nse, nil
}
}

query.Watch = true
Expand All @@ -119,7 +121,16 @@ func (d *discoverCandidatesServer) discoverNetworkServiceEndpoint(ctx context.Co
if err != nil {
return nil, errors.WithStack(err)
}
return nseStream.Recv()
for {
var nse *registry.NetworkServiceEndpoint
if nse, err = nseStream.Recv(); err != nil {
return nil, errors.WithStack(err)
}

if nse.Name == nseName {
return nse, nil
}
}
}

func (d *discoverCandidatesServer) discoverNetworkServiceEndpoints(ctx context.Context, ns *registry.NetworkService, labels map[string]string) ([]*registry.NetworkServiceEndpoint, error) {
Expand Down Expand Up @@ -152,7 +163,7 @@ func (d *discoverCandidatesServer) discoverNetworkServiceEndpoints(ctx context.C
for {
var nse *registry.NetworkServiceEndpoint
if nse, err = nseStream.Recv(); err != nil {
return nil, err
return nil, errors.WithStack(err)
}

result = matchEndpoint(labels, ns, nse)
Expand All @@ -176,8 +187,10 @@ func (d *discoverCandidatesServer) discoverNetworkService(ctx context.Context, n
}
nsList := registry.ReadNetworkServiceList(nsStream)

if len(nsList) != 0 {
return nsList[0], nil
for _, ns := range nsList {
if ns.Name == name {
return ns, nil
}
}

ctx, cancelFind := context.WithCancel(ctx)
Expand All @@ -189,5 +202,14 @@ func (d *discoverCandidatesServer) discoverNetworkService(ctx context.Context, n
if err != nil {
return nil, errors.WithStack(err)
}
return nsStream.Recv()
for {
var ns *registry.NetworkService
if ns, err = nsStream.Recv(); err != nil {
return nil, errors.WithStack(err)
}

if ns.Name == name {
return ns, nil
}
}
}
121 changes: 120 additions & 1 deletion pkg/networkservice/common/discover/server_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020 Doc.ai and/or its affiliates.
// Copyright (c) 2020-2021 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -377,3 +377,122 @@ func TestNoMatchServiceEndpointFound(t *testing.T) {
_, err = server.Request(ctx, request)
require.Error(t, err)
}

func TestMatchExactService(t *testing.T) {
defer goleak.VerifyNone(t, goleak.IgnoreCurrent())

nsServer := memory.NewNetworkServiceRegistryServer()
nseServer := registrynext.NewNetworkServiceEndpointRegistryServer(
setid.NewNetworkServiceEndpointRegistryServer(),
memory.NewNetworkServiceEndpointRegistryServer(),
)

nsName := networkServiceName()
server := next.NewNetworkServiceServer(
discover.NewServer(
adapters.NetworkServiceServerToClient(nsServer),
adapters.NetworkServiceEndpointServerToClient(nseServer)),
checkcontext.NewServer(t, func(t *testing.T, ctx context.Context) {
nses := discover.Candidates(ctx).Endpoints
require.Len(t, nses, 1)
require.Equal(t, nsName, nses[0].NetworkServiceNames[0])
}),
)

// 1. Register NS, NSE with wrong name
wrongNSName := nsName + "-wrong"
_, err := nsServer.Register(context.Background(), &registry.NetworkService{
Name: wrongNSName,
})
require.NoError(t, err)
_, err = nseServer.Register(context.Background(), &registry.NetworkServiceEndpoint{
NetworkServiceNames: []string{wrongNSName},
})
require.NoError(t, err)

// 2. Try to discover NSE by the right NS name
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()

request := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
NetworkService: nsName,
},
}

_, err = server.Request(ctx, request.Clone())
require.Error(t, err)

// 3. Register NS, NSE with the right name
_, err = nsServer.Register(context.Background(), &registry.NetworkService{
Name: nsName,
})
require.NoError(t, err)
_, err = nseServer.Register(context.Background(), &registry.NetworkServiceEndpoint{
NetworkServiceNames: []string{nsName},
})
require.NoError(t, err)

// 4. Try to discover NSE by the right NS name
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()

_, err = server.Request(ctx, request.Clone())
require.NoError(t, err)
}

func TestMatchExactEndpoint(t *testing.T) {
defer goleak.VerifyNone(t, goleak.IgnoreCurrent())

nseServer := registrynext.NewNetworkServiceEndpointRegistryServer(
setid.NewNetworkServiceEndpointRegistryServer(),
memory.NewNetworkServiceEndpointRegistryServer(),
)

nseName := "final-endpoint"
u := "tcp://" + nseName
server := next.NewNetworkServiceServer(
discover.NewServer(
adapters.NetworkServiceServerToClient(memory.NewNetworkServiceRegistryServer()),
adapters.NetworkServiceEndpointServerToClient(nseServer)),
checkcontext.NewServer(t, func(t *testing.T, ctx context.Context) {
require.Equal(t, u, clienturlctx.ClientURL(ctx).String())
}),
)

// 1. Register NSE with wrong name
wrongNSEName := nseName + "-wrong"
wrongURL := u + "-wrong"
_, err := nseServer.Register(context.Background(), &registry.NetworkServiceEndpoint{
Name: wrongNSEName,
Url: wrongURL,
})
require.NoError(t, err)

// 2. Try to discover NSE by the right name
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()

request := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
NetworkServiceEndpointName: nseName,
},
}

_, err = server.Request(ctx, request.Clone())
require.Error(t, err)

// 3. Register NSE with the right name
_, err = nseServer.Register(context.Background(), &registry.NetworkServiceEndpoint{
Name: nseName,
Url: u,
})
require.NoError(t, err)

// 4. Try to discover NSE by the right name
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()

_, err = server.Request(ctx, request.Clone())
require.NoError(t, err)
}