Skip to content

Commit

Permalink
Improve registry chains testing with sandbox (#668)
Browse files Browse the repository at this point in the history
* Rework sandbox to node API

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Rework tests to the new sandbox

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Add Builder.setupNode

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Cleanup NSEs on context close

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Add sandbox registry client, rework endpoint Register/Unregister

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Rework endpoint registration in sandbox

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Increase default registry expiration

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Use NSE registry client in sandbox

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* NSE should be registered after all NS registered

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Fix imports aliaces

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
  • Loading branch information
Vladimir Popov authored Feb 8, 2021
1 parent d1280dd commit 64f6aa2
Show file tree
Hide file tree
Showing 10 changed files with 335 additions and 235 deletions.
59 changes: 32 additions & 27 deletions pkg/networkservice/chains/nsmgr/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/cls"
kernelmech "github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/kernel"
"github.com/networkservicemesh/api/pkg/api/networkservice/payload"
"github.com/networkservicemesh/api/pkg/api/registry"

"github.com/networkservicemesh/sdk/pkg/networkservice/common/clienturl"
Expand All @@ -49,6 +48,7 @@ import (

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

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

Expand Down Expand Up @@ -77,10 +77,10 @@ func TestNSMGR_RemoteUsecase_Parallel(t *testing.T) {
Name: "final-endpoint",
NetworkServiceNames: []string{"my-service-remote"},
}
_, err := sandbox.NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, domain.Nodes[0].NSMgr, counter)
_, err := domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, counter)
require.NoError(t, err)
}()
nsc := sandbox.NewClient(ctx, sandbox.GenerateTestToken, domain.Nodes[1].NSMgr.URL)
nsc := domain.Nodes[1].NewClient(ctx, sandbox.GenerateTestToken)

conn, err := nsc.Request(ctx, request.Clone())
require.NoError(t, err)
Expand Down Expand Up @@ -136,10 +136,10 @@ func TestNSMGR_SelectsRestartingEndpoint(t *testing.T) {
NetworkServiceNames: []string{"ns-1"},
}

_, err := sandbox.NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, domain.Nodes[0].NSMgr, &restartingEndpoint{startTime: time.Now().Add(time.Second * 2)})
_, err := domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, &restartingEndpoint{startTime: time.Now().Add(time.Second * 2)})
require.NoError(t, err)

nsc := sandbox.NewClient(ctx, sandbox.GenerateTestToken, domain.Nodes[0].NSMgr.URL)
nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken)

conn, err := nsc.Request(ctx, request.Clone())
require.NoError(t, err)
Expand All @@ -151,7 +151,8 @@ func TestNSMGR_SelectsRestartingEndpoint(t *testing.T) {

func TestNSMGR_RemoteUsecase_BusyEndpoints(t *testing.T) {
defer goleak.VerifyNone(t, goleak.IgnoreCurrent())
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

domain := sandbox.NewBuilder(t).
Expand Down Expand Up @@ -181,7 +182,7 @@ func TestNSMGR_RemoteUsecase_BusyEndpoints(t *testing.T) {
Name: "final-endpoint-" + strconv.Itoa(id),
NetworkServiceNames: []string{"my-service-remote"},
}
_, err := sandbox.NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, domain.Nodes[1].NSMgr, newBusyEndpoint())
_, err := domain.Nodes[1].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, newBusyEndpoint())
require.NoError(t, err)
wg.Done()
}(i)
Expand All @@ -193,10 +194,10 @@ func TestNSMGR_RemoteUsecase_BusyEndpoints(t *testing.T) {
Name: "final-endpoint-3",
NetworkServiceNames: []string{"my-service-remote"},
}
_, err := sandbox.NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, domain.Nodes[1].NSMgr, counter)
_, err := domain.Nodes[1].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, counter)
require.NoError(t, err)
}()
nsc := sandbox.NewClient(ctx, sandbox.GenerateTestToken, domain.Nodes[0].NSMgr.URL)
nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken)

conn, err := nsc.Request(ctx, request.Clone())
require.NoError(t, err)
Expand All @@ -218,6 +219,7 @@ func TestNSMGR_RemoteUsecase_BusyEndpoints(t *testing.T) {

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

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

Expand All @@ -234,7 +236,7 @@ func TestNSMGR_RemoteUsecase(t *testing.T) {
}

counter := &counterServer{}
_, err := sandbox.NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, domain.Nodes[0].NSMgr, counter)
_, err := domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, counter)
require.NoError(t, err)

request := &networkservice.NetworkServiceRequest{
Expand All @@ -248,7 +250,7 @@ func TestNSMGR_RemoteUsecase(t *testing.T) {
},
}

nsc := sandbox.NewClient(ctx, sandbox.GenerateTestToken, domain.Nodes[1].NSMgr.URL)
nsc := domain.Nodes[1].NewClient(ctx, sandbox.GenerateTestToken)

conn, err := nsc.Request(ctx, request.Clone())
require.NoError(t, err)
Expand Down Expand Up @@ -295,10 +297,10 @@ func TestNSMGR_ConnectToDeadNSE(t *testing.T) {
counter := &counterServer{}

nseCtx, killNse := context.WithCancel(ctx)
_, err := sandbox.NewEndpoint(nseCtx, nseReg, sandbox.GenerateTestToken, domain.Nodes[0].NSMgr, counter)
_, err := domain.Nodes[0].NewEndpoint(nseCtx, nseReg, sandbox.GenerateTestToken, counter)
require.NoError(t, err)

nsc := sandbox.NewClient(ctx, sandbox.GenerateTestToken, domain.Nodes[0].NSMgr.URL)
nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken)

request := &networkservice.NetworkServiceRequest{
MechanismPreferences: []*networkservice.Mechanism{
Expand Down Expand Up @@ -329,6 +331,7 @@ func TestNSMGR_ConnectToDeadNSE(t *testing.T) {

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

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

Expand All @@ -345,10 +348,10 @@ func TestNSMGR_LocalUsecase(t *testing.T) {
}

counter := &counterServer{}
_, err := sandbox.NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, domain.Nodes[0].NSMgr, counter)
_, err := domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, counter)
require.NoError(t, err)

nsc := sandbox.NewClient(ctx, sandbox.GenerateTestToken, domain.Nodes[0].NSMgr.URL)
nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken)

request := &networkservice.NetworkServiceRequest{
MechanismPreferences: []*networkservice.Mechanism{
Expand Down Expand Up @@ -387,6 +390,7 @@ func TestNSMGR_LocalUsecase(t *testing.T) {

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

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

Expand Down Expand Up @@ -421,11 +425,11 @@ func TestNSMGR_PassThroughRemote(t *testing.T) {
Name: fmt.Sprintf("endpoint-%v", i),
NetworkServiceNames: []string{fmt.Sprintf("my-service-remote-%v", i)},
}
_, err := sandbox.NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, domain.Nodes[i].NSMgr, additionalFunctionality...)
_, err := domain.Nodes[i].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, additionalFunctionality...)
require.NoError(t, err)
}

nsc := sandbox.NewClient(ctx, sandbox.GenerateTestToken, domain.Nodes[nodesCount-1].NSMgr.URL)
nsc := domain.Nodes[nodesCount-1].NewClient(ctx, sandbox.GenerateTestToken)

request := &networkservice.NetworkServiceRequest{
MechanismPreferences: []*networkservice.Mechanism{
Expand All @@ -449,6 +453,7 @@ func TestNSMGR_PassThroughRemote(t *testing.T) {

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

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

Expand Down Expand Up @@ -482,11 +487,11 @@ func TestNSMGR_PassThroughLocal(t *testing.T) {
Name: fmt.Sprintf("endpoint-%v", i),
NetworkServiceNames: []string{fmt.Sprintf("my-service-remote-%v", i)},
}
_, err := sandbox.NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, domain.Nodes[0].NSMgr, additionalFunctionality...)
_, err := domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, additionalFunctionality...)
require.NoError(t, err)
}

nsc := sandbox.NewClient(ctx, sandbox.GenerateTestToken, domain.Nodes[0].NSMgr.URL)
nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken)

request := &networkservice.NetworkServiceRequest{
MechanismPreferences: []*networkservice.Mechanism{
Expand Down Expand Up @@ -552,10 +557,10 @@ func testNSEAndClient(
ctx, cancel := context.WithCancel(ctx)
defer cancel()

_, err := sandbox.NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, domain.Nodes[0].NSMgr)
_, err := domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken)
require.NoError(t, err)

nsc := sandbox.NewClient(ctx, sandbox.GenerateTestToken, domain.Nodes[0].NSMgr.URL)
nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken)

conn, err := nsc.Request(ctx, &networkservice.NetworkServiceRequest{
MechanismPreferences: []*networkservice.Mechanism{
Expand All @@ -570,16 +575,16 @@ func testNSEAndClient(
_, err = nsc.Close(ctx, conn)
require.NoError(t, err)

_, err = domain.Nodes[0].NSMgr.NetworkServiceEndpointRegistryServer().Unregister(ctx, nseReg)
_, err = domain.Nodes[0].EndpointRegistryClient.Unregister(ctx, nseReg)
require.NoError(t, err)

for _, name := range nseReg.NetworkServiceNames {
_, err = domain.Nodes[0].NSMgr.NetworkServiceRegistryServer().Unregister(ctx, &registry.NetworkService{
Name: name,
Payload: payload.IP,
require.Eventually(t, func() bool {
stream, err := domain.Nodes[0].NSRegistryClient.Find(ctx, &registry.NetworkServiceQuery{
NetworkService: new(registry.NetworkService),
})
require.NoError(t, err)
}
return len(registry.ReadNetworkServiceList(stream)) == 0
}, 100*time.Millisecond, 10*time.Millisecond)
}

type passThroughClient struct {
Expand Down
13 changes: 8 additions & 5 deletions pkg/networkservice/chains/nsmgrproxy/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 All @@ -21,19 +21,22 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"
"go.uber.org/goleak"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/cls"
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/kernel"
"github.com/networkservicemesh/api/pkg/api/registry"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"

"github.com/networkservicemesh/sdk/pkg/tools/sandbox"
)

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

const remoteRegistryDomain = "domain2.local.registry"

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

Expand All @@ -60,10 +63,10 @@ func TestNSMGR_InterdomainUseCase(t *testing.T) {
NetworkServiceNames: []string{"my-service-interdomain"},
}

_, err := sandbox.NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, domain2.Nodes[0].NSMgr)
_, err := domain2.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken)
require.NoError(t, err)

nsc := sandbox.NewClient(ctx, sandbox.GenerateTestToken, domain1.Nodes[0].NSMgr.URL)
nsc := domain1.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken)

request := &networkservice.NetworkServiceRequest{
MechanismPreferences: []*networkservice.Mechanism{
Expand Down
6 changes: 3 additions & 3 deletions pkg/registry/chains/memory/server.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 @@ -36,10 +36,10 @@ import (
)

// NewServer creates new registry server based on memory storage
func NewServer(ctx context.Context, proxyRegistryURL *url.URL, options ...grpc.DialOption) registryserver.Registry {
func NewServer(ctx context.Context, expiryDuration time.Duration, proxyRegistryURL *url.URL, options ...grpc.DialOption) registryserver.Registry {
nseChain := chain.NewNetworkServiceEndpointRegistryServer(
setid.NewNetworkServiceEndpointRegistryServer(),
expire.NewNetworkServiceEndpointRegistryServer(time.Minute),
expire.NewNetworkServiceEndpointRegistryServer(expiryDuration),
memory.NewNetworkServiceEndpointRegistryServer(),
proxy.NewNetworkServiceEndpointRegistryServer(proxyRegistryURL),
connect.NewNetworkServiceEndpointRegistryServer(ctx, func(ctx context.Context, cc grpc.ClientConnInterface) registry.NetworkServiceEndpointRegistryClient {
Expand Down
Loading

0 comments on commit 64f6aa2

Please sign in to comment.