Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into pr-heal-squash
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Belov committed Mar 9, 2021
2 parents 5b59e5f + fcad90b commit 8bf3895
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 203 deletions.
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,7 @@ issues:
linters:
- structcheck
text: "is unused"
- path: pkg/networkservice/common/discover/server_test.go
linters:
- dupl
text: "lines are duplicate of"
8 changes: 5 additions & 3 deletions pkg/networkservice/chains/nsmgr/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ func NewServer(ctx context.Context, nsmRegistration *registryapi.NetworkServiceE

nseRegistry := newRemoteNSEServer(registryCC)
if nseRegistry == nil {
nseRegistry = memory.NewNetworkServiceEndpointRegistryServer() // Memory registry to store result inside.
nseRegistry = registrychain.NewNetworkServiceEndpointRegistryServer(
memory.NewNetworkServiceEndpointRegistryServer(), // Memory registry to store result inside
setid.NewNetworkServiceEndpointRegistryServer(), // Assign ID
)
}

localBypassRegistryServer := localbypass.NewNetworkServiceEndpointRegistryServer(nsmRegistration.Url)
Expand Down Expand Up @@ -135,12 +138,11 @@ func NewServer(ctx context.Context, nsmRegistration *registryapi.NetworkServiceE
nseChain := registrychain.NewNamedNetworkServiceEndpointRegistryServer(
nsmRegistration.Name+".NetworkServiceEndpointRegistry",
expire.NewNetworkServiceEndpointRegistryServer(ctx, time.Minute),
setid.NewNetworkServiceEndpointRegistryServer(), // Assign ID
registryrecvfd.NewNetworkServiceEndpointRegistryServer(), // Allow to receive a passed files
urlsRegistryServer, // Store endpoints URLs
interposeRegistryServer, // Store cross connect NSEs
localBypassRegistryServer, // Perform URL transformations
nseRegistry, // Register NSE inside Remote registry with ID assigned
nseRegistry, // Register NSE inside Remote registry
)
rv.Registry = registry.NewServer(nsChain, nseChain)

Expand Down
195 changes: 93 additions & 102 deletions pkg/networkservice/common/discover/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,24 +126,45 @@ func fromSomeMiddleAppMatch() *registry.Match {
}
}

func testServers(
t *testing.T,
nsName string,
nses []*registry.NetworkServiceEndpoint,
matches ...*registry.Match,
) (registry.NetworkServiceRegistryServer, registry.NetworkServiceEndpointRegistryServer) {
nsServer := memory.NewNetworkServiceRegistryServer()
if nsName != "" {
_, err := nsServer.Register(context.Background(), &registry.NetworkService{
Name: nsName,
Matches: matches,
})
require.NoError(t, err)
}

nseServer := registrynext.NewNetworkServiceEndpointRegistryServer(
memory.NewNetworkServiceEndpointRegistryServer(),
setid.NewNetworkServiceEndpointRegistryServer(),
)
for i, nse := range nses {
var err error
nses[i], err = nseServer.Register(context.Background(), nse)
require.NoError(t, err)
}

return nsServer, nseServer
}

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

nsName := networkServiceName()
nsServer := memory.NewNetworkServiceRegistryServer()
_, err := nsServer.Register(context.Background(), &registry.NetworkService{
Name: nsName,
Matches: []*registry.Match{fromFirewallMatch(), fromSomeMiddleAppMatch(), fromAnywhereMatch()},

nsServer, nseServer := testServers(t, nsName, endpoints(), fromFirewallMatch(), fromSomeMiddleAppMatch(), fromAnywhereMatch())

want := labels(nsName, map[string]string{
"app": "firewall",
})
require.Nil(t, err)
nseServer := registrynext.NewNetworkServiceEndpointRegistryServer(setid.NewNetworkServiceEndpointRegistryServer(), memory.NewNetworkServiceEndpointRegistryServer())
for _, nse := range endpoints() {
_, err = nseServer.Register(context.Background(), nse)
require.Nil(t, err)
}
want := labels(nsName,
map[string]string{
"app": "firewall",
})

request := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
NetworkService: nsName,
Expand All @@ -159,24 +180,21 @@ func TestMatchEmptySourceSelector(t *testing.T) {
require.Equal(t, want, nses[0].NetworkServiceLabels)
}),
)
_, err = server.Request(context.Background(), request)
require.Nil(t, err)

_, err := server.Request(context.Background(), request)
require.NoError(t, err)
}

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

nsName := networkServiceName()
nsServer := memory.NewNetworkServiceRegistryServer()
_, err := nsServer.Register(context.Background(), &registry.NetworkService{
Name: nsName,
Matches: []*registry.Match{fromFirewallMatch(), fromSomeMiddleAppMatch(), fromAnywhereMatch()},

nsServer, nseServer := testServers(t, nsName, endpoints(), fromFirewallMatch(), fromSomeMiddleAppMatch(), fromAnywhereMatch())

want := labels(nsName, map[string]string{
"app": "some-middle-app",
})
require.Nil(t, err)
nseServer := registrynext.NewNetworkServiceEndpointRegistryServer(setid.NewNetworkServiceEndpointRegistryServer(), memory.NewNetworkServiceEndpointRegistryServer())
for _, nse := range endpoints() {
_, err = nseServer.Register(context.Background(), nse)
require.Nil(t, err)
}

request := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Expand All @@ -186,10 +204,7 @@ func TestMatchNonEmptySourceSelector(t *testing.T) {
},
},
}
want := labels(nsName,
map[string]string{
"app": "some-middle-app",
})

server := next.NewNetworkServiceServer(
discover.NewServer(adapters.NetworkServiceServerToClient(nsServer), adapters.NetworkServiceEndpointServerToClient(nseServer)),
checkcontext.NewServer(t, func(t *testing.T, ctx context.Context) {
Expand All @@ -198,28 +213,22 @@ func TestMatchNonEmptySourceSelector(t *testing.T) {
require.Equal(t, want, nses[0].NetworkServiceLabels)
}),
)
_, err = server.Request(context.Background(), request)

_, err := server.Request(context.Background(), request)
require.Nil(t, err)
}

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

nsName := networkServiceName()
nsServer := memory.NewNetworkServiceRegistryServer()
_, err := nsServer.Register(context.Background(), &registry.NetworkService{
Name: nsName,
Matches: []*registry.Match{fromAnywhereMatch(), fromFirewallMatch(), fromSomeMiddleAppMatch()},

nsServer, nseServer := testServers(t, nsName, endpoints(), fromAnywhereMatch(), fromFirewallMatch(), fromSomeMiddleAppMatch())

want := labels(nsName, map[string]string{
"app": "firewall",
})
require.Nil(t, err)
nseServer := registrynext.NewNetworkServiceEndpointRegistryServer(setid.NewNetworkServiceEndpointRegistryServer(), memory.NewNetworkServiceEndpointRegistryServer())
for _, nse := range endpoints() {
_, err = nseServer.Register(context.Background(), nse)
require.Nil(t, err)
}
want := labels(nsName,
map[string]string{
"app": "firewall",
})

request := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
NetworkService: nsName,
Expand All @@ -228,6 +237,7 @@ func TestMatchEmptySourceSelectorGoingFirst(t *testing.T) {
},
},
}

server := next.NewNetworkServiceServer(
discover.NewServer(adapters.NetworkServiceServerToClient(nsServer), adapters.NetworkServiceEndpointServerToClient(nseServer)),
checkcontext.NewServer(t, func(t *testing.T, ctx context.Context) {
Expand All @@ -236,24 +246,17 @@ func TestMatchEmptySourceSelectorGoingFirst(t *testing.T) {
require.Equal(t, want, nses[0].NetworkServiceLabels)
}),
)
_, err = server.Request(context.Background(), request)
require.Nil(t, err)

_, err := server.Request(context.Background(), request)
require.NoError(t, err)
}

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

nsName := networkServiceName()
nsServer := memory.NewNetworkServiceRegistryServer()
_, err := nsServer.Register(context.Background(), &registry.NetworkService{
Name: nsName,
Matches: []*registry.Match{fromFirewallMatch(), fromSomeMiddleAppMatch()},
})
require.Nil(t, err)
nseServer := registrynext.NewNetworkServiceEndpointRegistryServer(setid.NewNetworkServiceEndpointRegistryServer(), memory.NewNetworkServiceEndpointRegistryServer())
for _, nse := range endpoints() {
_, err = nseServer.Register(context.Background(), nse)
require.Nil(t, err)
}

nsServer, nseServer := testServers(t, nsName, endpoints(), fromFirewallMatch(), fromSomeMiddleAppMatch())

request := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Expand All @@ -271,54 +274,47 @@ func TestMatchNothing(t *testing.T) {
require.Len(t, nses, 3)
}),
)
_, err = server.Request(context.Background(), request)
require.Nil(t, err)

_, err := server.Request(context.Background(), request)
require.NoError(t, err)
}

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

nsName := networkServiceName()
nsServer := memory.NewNetworkServiceRegistryServer()
_, err := nsServer.Register(context.Background(), &registry.NetworkService{
Name: nsName,
Matches: []*registry.Match{fromAnywhereMatch(), fromFirewallMatch(), fromSomeMiddleAppMatch()},
})
require.Nil(t, err)
nseServer := registrynext.NewNetworkServiceEndpointRegistryServer(setid.NewNetworkServiceEndpointRegistryServer(), memory.NewNetworkServiceEndpointRegistryServer())
var last *registry.NetworkServiceEndpoint
for _, nse := range endpoints() {
last, err = nseServer.Register(context.Background(), nse)
require.Nil(t, err)
}
nses := endpoints()

nsServer, nseServer := testServers(t, nsName, nses, fromAnywhereMatch(), fromFirewallMatch(), fromSomeMiddleAppMatch())

request := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
NetworkServiceEndpointName: last.Name,
NetworkServiceEndpointName: nses[0].Name,
},
}

server := next.NewNetworkServiceServer(
discover.NewServer(adapters.NetworkServiceServerToClient(nsServer), adapters.NetworkServiceEndpointServerToClient(nseServer)),
checkcontext.NewServer(t, func(t *testing.T, ctx context.Context) {
require.NotNil(t, clienturlctx.ClientURL(ctx))
}),
)
_, err = server.Request(context.Background(), request)
require.Nil(t, err)

_, err := server.Request(context.Background(), request)
require.NoError(t, err)
}

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

nsName := networkServiceName()
nsServer := memory.NewNetworkServiceRegistryServer()

nseServer := registrynext.NewNetworkServiceEndpointRegistryServer(setid.NewNetworkServiceEndpointRegistryServer(), memory.NewNetworkServiceEndpointRegistryServer())
for _, nse := range endpoints() {
_, err := nseServer.Register(context.Background(), nse)
require.Nil(t, err)
}
want := labels(nsName,
map[string]string{
"app": "firewall",
})
nsServer, nseServer := testServers(t, "", endpoints())

want := labels(nsName, map[string]string{
"app": "firewall",
})

request := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
NetworkService: nsName,
Expand All @@ -335,28 +331,25 @@ func TestNoMatchServiceFound(t *testing.T) {
require.Equal(t, want, nses[0].NetworkServiceLabels)
}),
)

ctx, cancel := context.WithTimeout(context.Background(), time.Second/2)
defer cancel()

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

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

nsName := networkServiceName()
nsServer := memory.NewNetworkServiceRegistryServer()
_, err := nsServer.Register(context.Background(), &registry.NetworkService{
Name: nsName,
Matches: []*registry.Match{fromFirewallMatch(), fromSomeMiddleAppMatch(), fromAnywhereMatch()},
})
require.Nil(t, err)

nseServer := registrynext.NewNetworkServiceEndpointRegistryServer(setid.NewNetworkServiceEndpointRegistryServer(), memory.NewNetworkServiceEndpointRegistryServer())
nsServer, nseServer := testServers(t, nsName, []*registry.NetworkServiceEndpoint{}, fromFirewallMatch(), fromSomeMiddleAppMatch(), fromAnywhereMatch())

want := labels(nsName, map[string]string{
"app": "firewall",
})

want := labels(nsName,
map[string]string{
"app": "firewall",
})
request := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
NetworkService: nsName,
Expand All @@ -372,20 +365,18 @@ func TestNoMatchServiceEndpointFound(t *testing.T) {
require.Equal(t, want, nses[0].NetworkServiceLabels)
}),
)

ctx, cancel := context.WithTimeout(context.Background(), time.Second/2)
defer cancel()
_, err = server.Request(ctx, request)

_, 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(),
)
nsServer, nseServer := testServers(t, "", []*registry.NetworkServiceEndpoint{})

nsName := networkServiceName()
server := next.NewNetworkServiceServer(
Expand Down
2 changes: 1 addition & 1 deletion pkg/registry/chains/memory/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ import (
// NewServer creates new registry server based on memory storage
func NewServer(ctx context.Context, expiryDuration time.Duration, proxyRegistryURL *url.URL, options ...grpc.DialOption) registryserver.Registry {
nseChain := chain.NewNetworkServiceEndpointRegistryServer(
setid.NewNetworkServiceEndpointRegistryServer(),
expire.NewNetworkServiceEndpointRegistryServer(ctx, expiryDuration),
memory.NewNetworkServiceEndpointRegistryServer(),
setid.NewNetworkServiceEndpointRegistryServer(),
proxy.NewNetworkServiceEndpointRegistryServer(proxyRegistryURL),
connect.NewNetworkServiceEndpointRegistryServer(ctx, func(ctx context.Context, cc grpc.ClientConnInterface) registry.NetworkServiceEndpointRegistryClient {
return chain.NewNetworkServiceEndpointRegistryClient(
Expand Down
5 changes: 5 additions & 0 deletions pkg/registry/common/interpose/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/url"

"github.com/golang/protobuf/ptypes/empty"
"github.com/google/uuid"
"github.com/pkg/errors"

"github.com/networkservicemesh/api/pkg/api/registry"
Expand All @@ -46,6 +47,10 @@ func (s *interposeRegistryServer) Register(ctx context.Context, nse *registry.Ne
return next.NetworkServiceEndpointRegistryServer(ctx).Register(ctx, nse)
}

if _, ok := s.interposeURLs.Load(nse.Name); !ok {
nse.Name = interposeName(uuid.New().String())
}

u, err := url.Parse(nse.Url)
if err != nil {
return nil, errors.Wrapf(err, "cannot register cross NSE with passed URL: %s", nse.Url)
Expand Down
Loading

0 comments on commit 8bf3895

Please sign in to comment.