From 694f0f3138a288323f47a9b058da3c1d1e82c2fb Mon Sep 17 00:00:00 2001 From: Vladimir Popov Date: Thu, 24 Jun 2021 13:54:44 +0700 Subject: [PATCH] Rework sandbox to use tokenTimeout instead of tokenGenerator Signed-off-by: Vladimir Popov --- pkg/networkservice/chains/nsmgr/heal_test.go | 44 ++++++------ pkg/networkservice/chains/nsmgr/scale_test.go | 8 +-- .../chains/nsmgr/single_test.go | 30 ++++----- pkg/networkservice/chains/nsmgr/suite_test.go | 50 +++++++------- pkg/networkservice/chains/nsmgr/utils_test.go | 4 +- .../chains/nsmgrproxy/server_test.go | 38 +++++------ .../common/connect/server_cancel_test.go | 12 ++-- .../common/refresh/client_test.go | 8 +-- pkg/registry/chains/memory/server_test.go | 21 ++---- pkg/registry/common/heal/find_test.go | 10 +-- pkg/tools/sandbox/builder.go | 63 +++++++++++------ pkg/tools/sandbox/domain.go | 67 +++++++++++++++++++ pkg/tools/sandbox/node.go | 53 ++++++++------- pkg/tools/sandbox/types.go | 39 ++--------- pkg/tools/sandbox/utils.go | 11 +-- 15 files changed, 257 insertions(+), 201 deletions(-) create mode 100644 pkg/tools/sandbox/domain.go diff --git a/pkg/networkservice/chains/nsmgr/heal_test.go b/pkg/networkservice/chains/nsmgr/heal_test.go index 94bd8a41d..e71da567d 100644 --- a/pkg/networkservice/chains/nsmgr/heal_test.go +++ b/pkg/networkservice/chains/nsmgr/heal_test.go @@ -71,7 +71,7 @@ func testNSMGRHealEndpoint(t *testing.T, nodeNum int) { SetRegistryProxySupplier(nil). Build() - nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.GenerateTestToken) + nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.DefaultTokenTimeout) nsReg, err := nsRegistryClient.Register(ctx, defaultRegistryService()) require.NoError(t, err) @@ -81,11 +81,11 @@ func testNSMGRHealEndpoint(t *testing.T, nodeNum int) { nseCtx, nseCtxCancel := context.WithCancel(context.Background()) counter := &counterServer{} - domain.Nodes[nodeNum].NewEndpoint(nseCtx, nseReg, sandbox.GenerateTestToken, counter) + domain.Nodes[nodeNum].NewEndpoint(nseCtx, nseReg, sandbox.DefaultTokenTimeout, counter) request := defaultRequest(nsReg.Name) - nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken) + nsc := domain.Nodes[0].NewClient(ctx, sandbox.DefaultTokenTimeout) conn, err := nsc.Request(ctx, request.Clone()) require.NoError(t, err) @@ -95,7 +95,7 @@ func testNSMGRHealEndpoint(t *testing.T, nodeNum int) { nseReg2 := defaultRegistryEndpoint(nsReg.Name) nseReg2.Name += "-2" - domain.Nodes[nodeNum].NewEndpoint(ctx, nseReg2, sandbox.GenerateTestToken, counter) + domain.Nodes[nodeNum].NewEndpoint(ctx, nseReg2, sandbox.DefaultTokenTimeout, counter) // Wait reconnecting to the new NSE require.Eventually(t, checkSecondRequestsReceived(counter.UniqueRequests), timeout, tick) @@ -150,24 +150,24 @@ func testNSMGRHealForwarder(t *testing.T, nodeNum int) { SetRegistryProxySupplier(nil). SetNodeSetup(func(ctx context.Context, node *sandbox.Node, nodeN int) { if nodeN != nodeNum { - sandbox.SetupDefaultNode(ctx, node, nsmgr.NewServer) + sandbox.SetupDefaultNode(ctx, node, sandbox.DefaultTokenTimeout, nsmgr.NewServer) } else { forwarderCtxCancel = setupCancelableForwarderNode(ctx, node) } }). Build() - nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.GenerateTestToken) + nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.DefaultTokenTimeout) nsReg, err := nsRegistryClient.Register(ctx, defaultRegistryService()) require.NoError(t, err) counter := &counterServer{} - domain.Nodes[1].NewEndpoint(ctx, defaultRegistryEndpoint(nsReg.Name), sandbox.GenerateTestToken, counter) + domain.Nodes[1].NewEndpoint(ctx, defaultRegistryEndpoint(nsReg.Name), sandbox.DefaultTokenTimeout, counter) request := defaultRequest(nsReg.Name) - nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken) + nsc := domain.Nodes[0].NewClient(ctx, sandbox.DefaultTokenTimeout) conn, err := nsc.Request(ctx, request.Clone()) require.NoError(t, err) @@ -178,7 +178,7 @@ func testNSMGRHealForwarder(t *testing.T, nodeNum int) { forwarderReg := ®istry.NetworkServiceEndpoint{ Name: sandbox.UniqueName("forwarder-2"), } - domain.Nodes[nodeNum].NewForwarder(ctx, forwarderReg, sandbox.GenerateTestToken) + domain.Nodes[nodeNum].NewForwarder(ctx, forwarderReg, sandbox.DefaultTokenTimeout) // Wait reconnecting through the new Forwarder require.Eventually(t, checkSecondRequestsReceived(counter.UniqueRequests), timeout, tick) @@ -198,12 +198,12 @@ func testNSMGRHealForwarder(t *testing.T, nodeNum int) { } func setupCancelableForwarderNode(ctx context.Context, node *sandbox.Node) context.CancelFunc { - node.NewNSMgr(ctx, sandbox.UniqueName("nsmgr"), nil, sandbox.GenerateTestToken, nsmgr.NewServer) + node.NewNSMgr(ctx, sandbox.UniqueName("nsmgr"), nil, sandbox.DefaultTokenTimeout, nsmgr.NewServer) forwarderCtx, forwarderCtxCancel := context.WithCancel(ctx) node.NewForwarder(forwarderCtx, ®istry.NetworkServiceEndpoint{ Name: sandbox.UniqueName("forwarder"), - }, sandbox.GenerateTestToken) + }, sandbox.DefaultTokenTimeout) return forwarderCtxCancel } @@ -246,14 +246,14 @@ func testNSMGRHealNSMgr(t *testing.T, nodeNum int, restored bool) { SetRegistryProxySupplier(nil). SetNodeSetup(func(ctx context.Context, node *sandbox.Node, nodeN int) { if nodeN != nodeNum { - sandbox.SetupDefaultNode(ctx, node, nsmgr.NewServer) + sandbox.SetupDefaultNode(ctx, node, sandbox.DefaultTokenTimeout, nsmgr.NewServer) } else { nsmgrCtxCancel = setupCancellableNSMgrNode(ctx, node) } }). Build() - nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.GenerateTestToken) + nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.DefaultTokenTimeout) nsReg, err := nsRegistryClient.Register(ctx, defaultRegistryService()) require.NoError(t, err) @@ -261,11 +261,11 @@ func testNSMGRHealNSMgr(t *testing.T, nodeNum int, restored bool) { nseReg := defaultRegistryEndpoint(nsReg.Name) counter := &counterServer{} - domain.Nodes[1].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, counter) + domain.Nodes[1].NewEndpoint(ctx, nseReg, sandbox.DefaultTokenTimeout, counter) request := defaultRequest(nsReg.Name) - nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken) + nsc := domain.Nodes[0].NewClient(ctx, sandbox.DefaultTokenTimeout) conn, err := nsc.Request(ctx, request.Clone()) require.NoError(t, err) @@ -274,7 +274,7 @@ func testNSMGRHealNSMgr(t *testing.T, nodeNum int, restored bool) { nseReg2 := defaultRegistryEndpoint(nsReg.Name) nseReg2.Name += "-2" - domain.Nodes[2].NewEndpoint(ctx, nseReg2, sandbox.GenerateTestToken, counter) + domain.Nodes[2].NewEndpoint(ctx, nseReg2, sandbox.DefaultTokenTimeout, counter) } nsmgrCtxCancel() @@ -287,7 +287,7 @@ func testNSMGRHealNSMgr(t *testing.T, nodeNum int, restored bool) { return grpcutils.CheckURLFree(domain.Nodes[0].NSMgr.URL) }, timeout, tick) - domain.Nodes[nodeNum].NewNSMgr(ctx, mgr.Name, mgr.URL, sandbox.GenerateTestToken, nsmgr.NewServer) + domain.Nodes[nodeNum].NewNSMgr(ctx, mgr.Name, mgr.URL, sandbox.DefaultTokenTimeout, nsmgr.NewServer) } if restored { @@ -323,11 +323,11 @@ func testNSMGRHealNSMgr(t *testing.T, nodeNum int, restored bool) { func setupCancellableNSMgrNode(ctx context.Context, node *sandbox.Node) context.CancelFunc { nsmgrCtx, nsmgrCtxCancel := context.WithCancel(ctx) - node.NewNSMgr(nsmgrCtx, sandbox.UniqueName("nsmgr"), nil, sandbox.GenerateTestToken, nsmgr.NewServer) + node.NewNSMgr(nsmgrCtx, sandbox.UniqueName("nsmgr"), nil, sandbox.DefaultTokenTimeout, nsmgr.NewServer) node.NewForwarder(ctx, ®istry.NetworkServiceEndpoint{ Name: sandbox.UniqueName("forwarder"), - }, sandbox.GenerateTestToken) + }, sandbox.DefaultTokenTimeout) return nsmgrCtxCancel } @@ -344,20 +344,20 @@ func TestNSMGR_CloseHeal(t *testing.T) { SetRegistryProxySupplier(nil). Build() - nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.GenerateTestToken) + nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.DefaultTokenTimeout) nsReg, err := nsRegistryClient.Register(ctx, defaultRegistryService()) require.NoError(t, err) nseCtx, nseCtxCancel := context.WithCancel(ctx) - domain.Nodes[0].NewEndpoint(nseCtx, defaultRegistryEndpoint(nsReg.Name), sandbox.GenerateTestToken) + domain.Nodes[0].NewEndpoint(nseCtx, defaultRegistryEndpoint(nsReg.Name), sandbox.DefaultTokenTimeout) request := defaultRequest(nsReg.Name) nscCtx, nscCtxCancel := context.WithCancel(ctx) - nsc := domain.Nodes[0].NewClient(nscCtx, sandbox.GenerateTestToken) + nsc := domain.Nodes[0].NewClient(nscCtx, sandbox.DefaultTokenTimeout) // 1. Request conn, err := nsc.Request(ctx, request.Clone()) diff --git a/pkg/networkservice/chains/nsmgr/scale_test.go b/pkg/networkservice/chains/nsmgr/scale_test.go index 0ea58fb9a..41e36006a 100644 --- a/pkg/networkservice/chains/nsmgr/scale_test.go +++ b/pkg/networkservice/chains/nsmgr/scale_test.go @@ -45,7 +45,7 @@ func TestCreateEndpointDuringRequest(t *testing.T) { SetRegistryProxySupplier(nil). Build() - nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.GenerateTestToken) + nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.DefaultTokenTimeout) nsReg := ®istry.NetworkService{ Name: "ns-1", @@ -95,9 +95,9 @@ func TestCreateEndpointDuringRequest(t *testing.T) { }, } - domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, makerServer) + domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.DefaultTokenTimeout, makerServer) - nsc := domain.Nodes[1].NewClient(ctx, sandbox.GenerateTestToken) + nsc := domain.Nodes[1].NewClient(ctx, sandbox.DefaultTokenTimeout) // check first request conn, err := nsc.Request(ctx, &networkservice.NetworkServiceRequest{ @@ -139,7 +139,7 @@ func (m *nseMaker) Request(_ context.Context, _ *networkservice.NetworkServiceRe return nil, errors.New("can't create new endpoint") } - m.domain.Nodes[1].NewEndpoint(m.ctx, m.nseReg, sandbox.GenerateTestToken, endpoint) + m.domain.Nodes[1].NewEndpoint(m.ctx, m.nseReg, sandbox.DefaultTokenTimeout, endpoint) return nil, errors.New("can't provide requested network service") } diff --git a/pkg/networkservice/chains/nsmgr/single_test.go b/pkg/networkservice/chains/nsmgr/single_test.go index 0699691cf..e2344dca7 100644 --- a/pkg/networkservice/chains/nsmgr/single_test.go +++ b/pkg/networkservice/chains/nsmgr/single_test.go @@ -49,14 +49,14 @@ func Test_DNSUsecase(t *testing.T) { SetRegistryProxySupplier(nil). Build() - nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.GenerateTestToken) + nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.DefaultTokenTimeout) nsReg, err := nsRegistryClient.Register(ctx, defaultRegistryService()) require.NoError(t, err) nseReg := defaultRegistryEndpoint(nsReg.Name) - nse := domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, dnscontext.NewServer( + nse := domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.DefaultTokenTimeout, dnscontext.NewServer( &networkservice.DNSConfig{ DnsServerIps: []string{"8.8.8.8"}, SearchDomains: []string{"my.domain1"}, @@ -75,7 +75,7 @@ func Test_DNSUsecase(t *testing.T) { const expectedCorefile = ". {\n\tforward . 8.8.4.4\n\tlog\n\treload\n}\nmy.domain1 {\n\tfanout . 8.8.4.4 8.8.8.8\n\tlog\n}" - nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken, dnscontext.NewClient( + nsc := domain.Nodes[0].NewClient(ctx, sandbox.DefaultTokenTimeout, dnscontext.NewClient( dnscontext.WithChainContext(ctx), dnscontext.WithCorefilePath(corefilePath), dnscontext.WithResolveConfigPath(resolveConfigPath), @@ -110,12 +110,12 @@ func Test_ShouldCorrectlyAddForwardersWithSameNames(t *testing.T) { SetNodesCount(1). SetRegistryProxySupplier(nil). SetNodeSetup(func(ctx context.Context, node *sandbox.Node, _ int) { - node.NewNSMgr(ctx, "nsmgr", nil, sandbox.GenerateTestToken, nsmgr.NewServer) + node.NewNSMgr(ctx, "nsmgr", nil, sandbox.DefaultTokenTimeout, nsmgr.NewServer) }). SetRegistryExpiryDuration(sandbox.RegistryExpiryDuration). Build() - nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.GenerateTestToken) + nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.DefaultTokenTimeout) nsReg, err := nsRegistryClient.Register(ctx, defaultRegistryService()) require.NoError(t, err) @@ -129,7 +129,7 @@ func Test_ShouldCorrectlyAddForwardersWithSameNames(t *testing.T) { var forwarders [3]*sandbox.EndpointEntry for i := range forwarderRegs { forwarderRegs[i] = forwarderReg.Clone() - forwarders[i] = domain.Nodes[0].NewForwarder(ctx, forwarderRegs[i], sandbox.GenerateTestToken) + forwarders[i] = domain.Nodes[0].NewForwarder(ctx, forwarderRegs[i], sandbox.DefaultTokenTimeout) } // 2. Wait for refresh @@ -158,7 +158,7 @@ func Test_ShouldCorrectlyAddEndpointsWithSameNames(t *testing.T) { SetRegistryExpiryDuration(sandbox.RegistryExpiryDuration). Build() - nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.GenerateTestToken) + nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.DefaultTokenTimeout) // 1. Add endpoints var nseRegs [2]*registry.NetworkServiceEndpoint @@ -170,14 +170,14 @@ func Test_ShouldCorrectlyAddEndpointsWithSameNames(t *testing.T) { nseRegs[i] = defaultRegistryEndpoint(nsReg.Name) nseRegs[i].NetworkServiceNames[0] = nsReg.Name - nses[i] = domain.Nodes[0].NewEndpoint(ctx, nseRegs[i], sandbox.GenerateTestToken) + nses[i] = domain.Nodes[0].NewEndpoint(ctx, nseRegs[i], sandbox.DefaultTokenTimeout) } // 2. Wait for refresh <-time.After(sandbox.RegistryExpiryDuration) // 3. Request - nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken) + nsc := domain.Nodes[0].NewClient(ctx, sandbox.DefaultTokenTimeout) for _, nseReg := range nseRegs { _, err := nsc.Request(ctx, defaultRequest(nseReg.NetworkServiceNames[0])) @@ -209,7 +209,7 @@ func Test_Local_NoURLUsecase(t *testing.T) { SetRegistrySupplier(nil). Build() - nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.GenerateTestToken) + nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.DefaultTokenTimeout) nsReg, err := nsRegistryClient.Register(ctx, defaultRegistryService()) require.NoError(t, err) @@ -218,9 +218,9 @@ func Test_Local_NoURLUsecase(t *testing.T) { request := defaultRequest(nsReg.Name) counter := &counterServer{} - domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, counter) + domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.DefaultTokenTimeout, counter) - nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken) + nsc := domain.Nodes[0].NewClient(ctx, sandbox.DefaultTokenTimeout) conn, err := nsc.Request(ctx, request.Clone()) require.NoError(t, err) @@ -269,7 +269,7 @@ func Test_ShouldParseNetworkServiceLabelsTemplate(t *testing.T) { SetNSMgrProxySupplier(nil). Build() - nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.GenerateTestToken) + nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.DefaultTokenTimeout) nsReg := defaultRegistryService() nsReg.Matches = []*registry.Match{ @@ -290,9 +290,9 @@ func Test_ShouldParseNetworkServiceLabelsTemplate(t *testing.T) { nseReg := defaultRegistryEndpoint(nsReg.Name) nseReg.NetworkServiceLabels = map[string]*registry.NetworkServiceLabels{nsReg.Name: {}} - nse := domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken) + nse := domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.DefaultTokenTimeout) - nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken) + nsc := domain.Nodes[0].NewClient(ctx, sandbox.DefaultTokenTimeout) require.NoError(t, err) req := defaultRequest(nsReg.Name) diff --git a/pkg/networkservice/chains/nsmgr/suite_test.go b/pkg/networkservice/chains/nsmgr/suite_test.go index 042565ab1..2fbba6262 100644 --- a/pkg/networkservice/chains/nsmgr/suite_test.go +++ b/pkg/networkservice/chains/nsmgr/suite_test.go @@ -83,7 +83,7 @@ func (s *nsmgrSuite) SetupSuite() { SetNSMgrProxySupplier(nil). Build() - s.nsRegistryClient = s.domain.NewNSRegistryClient(ctx, sandbox.GenerateTestToken) + s.nsRegistryClient = s.domain.NewNSRegistryClient(ctx, sandbox.DefaultTokenTimeout) } func (s *nsmgrSuite) Test_Remote_ParallelUsecase() { @@ -105,9 +105,9 @@ func (s *nsmgrSuite) Test_Remote_ParallelUsecase() { defer unregisterWG.Done() time.Sleep(time.Millisecond * 100) - nse = s.domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, counter) + nse = s.domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.DefaultTokenTimeout, counter) }() - nsc := s.domain.Nodes[1].NewClient(ctx, sandbox.GenerateTestToken) + nsc := s.domain.Nodes[1].NewClient(ctx, sandbox.DefaultTokenTimeout) request := defaultRequest(nsReg.Name) @@ -162,7 +162,7 @@ func (s *nsmgrSuite) Test_SelectsRestartingEndpointUsecase() { require.NoError(t, err) nseRegistryClient := registryclient.NewNetworkServiceEndpointRegistryClient(ctx, s.domain.Nodes[0].URL(), - registryclient.WithDialOptions(sandbox.DefaultDialOptions(sandbox.GenerateTestToken)...)) + registryclient.WithDialOptions(s.domain.DefaultDialOptions(sandbox.DefaultTokenTimeout)...)) nseReg, err = nseRegistryClient.Register(ctx, nseReg) require.NoError(t, err) @@ -175,7 +175,7 @@ func (s *nsmgrSuite) Test_SelectsRestartingEndpointUsecase() { }) // 3. Create client and request endpoint - nsc := s.domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken) + nsc := s.domain.Nodes[0].NewClient(ctx, sandbox.DefaultTokenTimeout) conn, err := nsc.Request(ctx, defaultRequest(nsReg.Name)) require.NoError(t, err) @@ -213,7 +213,7 @@ func (s *nsmgrSuite) Test_Remote_BusyEndpointsUsecase() { nseRegs[id] = defaultRegistryEndpoint(nsReg.Name) nseRegs[id].Name += strconv.Itoa(id) - nses[id] = s.domain.Nodes[1].NewEndpoint(ctx, nseRegs[id], sandbox.GenerateTestToken, injecterror.NewServer()) + nses[id] = s.domain.Nodes[1].NewEndpoint(ctx, nseRegs[id], sandbox.DefaultTokenTimeout, injecterror.NewServer()) wg.Done() }(i) } @@ -228,9 +228,9 @@ func (s *nsmgrSuite) Test_Remote_BusyEndpointsUsecase() { nseRegs[3] = defaultRegistryEndpoint(nsReg.Name) nseRegs[3].Name += strconv.Itoa(3) - nses[3] = s.domain.Nodes[1].NewEndpoint(ctx, nseRegs[3], sandbox.GenerateTestToken, counter) + nses[3] = s.domain.Nodes[1].NewEndpoint(ctx, nseRegs[3], sandbox.DefaultTokenTimeout, counter) }() - nsc := s.domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken) + nsc := s.domain.Nodes[0].NewClient(ctx, sandbox.DefaultTokenTimeout) request := defaultRequest(nsReg.Name) @@ -275,9 +275,9 @@ func (s *nsmgrSuite) Test_RemoteUsecase() { nseReg := defaultRegistryEndpoint(nsReg.Name) counter := &counterServer{} - nse := s.domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, counter) + nse := s.domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.DefaultTokenTimeout, counter) - nsc := s.domain.Nodes[1].NewClient(ctx, sandbox.GenerateTestToken) + nsc := s.domain.Nodes[1].NewClient(ctx, sandbox.DefaultTokenTimeout) request := defaultRequest(nsReg.Name) @@ -320,9 +320,9 @@ func (s *nsmgrSuite) Test_ConnectToDeadNSEUsecase() { nseReg := defaultRegistryEndpoint(nsReg.Name) counter := &counterServer{} - s.domain.Nodes[0].NewEndpoint(nseCtx, nseReg, sandbox.GenerateTestToken, counter) + s.domain.Nodes[0].NewEndpoint(nseCtx, nseReg, sandbox.DefaultTokenTimeout, counter) - nsc := s.domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken) + nsc := s.domain.Nodes[0].NewClient(ctx, sandbox.DefaultTokenTimeout) request := defaultRequest(nsReg.Name) @@ -358,9 +358,9 @@ func (s *nsmgrSuite) Test_LocalUsecase() { nseReg := defaultRegistryEndpoint(nsReg.Name) counter := &counterServer{} - nse := s.domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, counter) + nse := s.domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.DefaultTokenTimeout, counter) - nsc := s.domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken) + nsc := s.domain.Nodes[0].NewClient(ctx, sandbox.DefaultTokenTimeout) request := defaultRequest(nsReg.Name) @@ -405,7 +405,7 @@ func (s *nsmgrSuite) Test_PassThroughRemoteUsecase() { var nseRegs [nodesCount]*registry.NetworkServiceEndpoint var nses [nodesCount]*sandbox.EndpointEntry for i := range nseRegs { - nseRegs[i], nses[i] = newPassThroughEndpoint( + nseRegs[i], nses[i] = s.newPassThroughEndpoint( ctx, s.domain.Nodes[i], map[string]string{ @@ -418,7 +418,7 @@ func (s *nsmgrSuite) Test_PassThroughRemoteUsecase() { ) } - nsc := s.domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken) + nsc := s.domain.Nodes[0].NewClient(ctx, sandbox.DefaultTokenTimeout) request := defaultRequest(nsReg.Name) @@ -465,7 +465,7 @@ func (s *nsmgrSuite) Test_PassThroughLocalUsecase() { var nseRegs [nsesCount]*registry.NetworkServiceEndpoint var nses [nsesCount]*sandbox.EndpointEntry for i := range nseRegs { - nseRegs[i], nses[i] = newPassThroughEndpoint( + nseRegs[i], nses[i] = s.newPassThroughEndpoint( ctx, s.domain.Nodes[0], map[string]string{ @@ -478,7 +478,7 @@ func (s *nsmgrSuite) Test_PassThroughLocalUsecase() { ) } - nsc := s.domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken) + nsc := s.domain.Nodes[0].NewClient(ctx, sandbox.DefaultTokenTimeout) request := defaultRequest(nsReg.Name) @@ -527,7 +527,7 @@ func (s *nsmgrSuite) Test_PassThroughLocalUsecaseMultiLabel() { labelAvalue += "a" for j := 0; j < 3; j++ { labelBvalue += "b" - nseRegs[i*3+j], nses[i*3+j] = newPassThroughEndpoint( + nseRegs[i*3+j], nses[i*3+j] = s.newPassThroughEndpoint( ctx, s.domain.Nodes[0], map[string]string{ @@ -543,7 +543,7 @@ func (s *nsmgrSuite) Test_PassThroughLocalUsecaseMultiLabel() { labelBvalue = "" } - nsc := s.domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken) + nsc := s.domain.Nodes[0].NewClient(ctx, sandbox.DefaultTokenTimeout) request := defaultRequest(nsReg.Name) @@ -685,7 +685,7 @@ func multiLabelNS() *registry.NetworkService { } } -func additionalFunctionalityChain(ctx context.Context, clientURL *url.URL, clientName string, labels map[string]string) []networkservice.NetworkServiceServer { +func (s *nsmgrSuite) additionalFunctionalityChain(ctx context.Context, clientURL *url.URL, clientName string, labels map[string]string) []networkservice.NetworkServiceServer { return []networkservice.NetworkServiceServer{ chain.NewNetworkServiceServer( clienturl.NewServer(clientURL), @@ -699,13 +699,13 @@ func additionalFunctionalityChain(ctx context.Context, clientURL *url.URL, clien ), ), connect.WithDialTimeout(sandbox.DialTimeout), - connect.WithDialOptions(sandbox.DefaultDialOptions(sandbox.GenerateTestToken)...), + connect.WithDialOptions(s.domain.DefaultDialOptions(sandbox.DefaultTokenTimeout)...), ), ), } } -func newPassThroughEndpoint( +func (s *nsmgrSuite) newPassThroughEndpoint( ctx context.Context, node *sandbox.Node, labels map[string]string, @@ -725,12 +725,12 @@ func newPassThroughEndpoint( var additionalFunctionality []networkservice.NetworkServiceServer if hasClientFunctionality { - additionalFunctionality = additionalFunctionalityChain(ctx, node.URL(), name, labels) + additionalFunctionality = s.additionalFunctionalityChain(ctx, node.URL(), name, labels) } if counter != nil { additionalFunctionality = append(additionalFunctionality, counter) } - return nseReg, node.NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, additionalFunctionality...) + return nseReg, node.NewEndpoint(ctx, nseReg, sandbox.DefaultTokenTimeout, additionalFunctionality...) } diff --git a/pkg/networkservice/chains/nsmgr/utils_test.go b/pkg/networkservice/chains/nsmgr/utils_test.go index d0f9fd141..56727a12c 100644 --- a/pkg/networkservice/chains/nsmgr/utils_test.go +++ b/pkg/networkservice/chains/nsmgr/utils_test.go @@ -69,9 +69,9 @@ func testNSEAndClient( ctx, cancel := context.WithCancel(ctx) defer cancel() - nse := domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken) + nse := domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.DefaultTokenTimeout) - nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken) + nsc := domain.Nodes[0].NewClient(ctx, sandbox.DefaultTokenTimeout) request := defaultRequest(nseReg.NetworkServiceNames[0]) diff --git a/pkg/networkservice/chains/nsmgrproxy/server_test.go b/pkg/networkservice/chains/nsmgrproxy/server_test.go index 2a456120b..49c3ef007 100644 --- a/pkg/networkservice/chains/nsmgrproxy/server_test.go +++ b/pkg/networkservice/chains/nsmgrproxy/server_test.go @@ -64,7 +64,7 @@ func TestNSMGR_InterdomainUseCase(t *testing.T) { SetDNSResolver(dnsServer). Build() - nsRegistryClient := cluster2.NewNSRegistryClient(ctx, sandbox.GenerateTestToken) + nsRegistryClient := cluster2.NewNSRegistryClient(ctx, sandbox.DefaultTokenTimeout) nsReg := ®istry.NetworkService{ Name: "my-service-interdomain", @@ -78,9 +78,9 @@ func TestNSMGR_InterdomainUseCase(t *testing.T) { NetworkServiceNames: []string{nsReg.Name}, } - cluster2.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken) + cluster2.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.DefaultTokenTimeout) - nsc := cluster1.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken) + nsc := cluster1.Nodes[0].NewClient(ctx, sandbox.DefaultTokenTimeout) request := &networkservice.NetworkServiceRequest{ MechanismPreferences: []*networkservice.Mechanism{ @@ -138,7 +138,7 @@ func TestNSMGR_Interdomain_TwoNodesNSEs(t *testing.T) { SetDNSResolver(dnsServer). Build() - nsRegistryClient := cluster2.NewNSRegistryClient(ctx, sandbox.GenerateTestToken) + nsRegistryClient := cluster2.NewNSRegistryClient(ctx, sandbox.DefaultTokenTimeout) _, err := nsRegistryClient.Register(ctx, ®istry.NetworkService{ Name: "my-service-interdomain-1", @@ -154,15 +154,15 @@ func TestNSMGR_Interdomain_TwoNodesNSEs(t *testing.T) { Name: "final-endpoint-1", NetworkServiceNames: []string{"my-service-interdomain-1"}, } - cluster2.Nodes[0].NewEndpoint(ctx, nseReg1, sandbox.GenerateTestToken) + cluster2.Nodes[0].NewEndpoint(ctx, nseReg1, sandbox.DefaultTokenTimeout) nseReg2 := ®istry.NetworkServiceEndpoint{ Name: "final-endpoint-2", NetworkServiceNames: []string{"my-service-interdomain-2"}, } - cluster2.Nodes[0].NewEndpoint(ctx, nseReg2, sandbox.GenerateTestToken) + cluster2.Nodes[0].NewEndpoint(ctx, nseReg2, sandbox.DefaultTokenTimeout) - nsc := cluster1.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken) + nsc := cluster1.Nodes[0].NewClient(ctx, sandbox.DefaultTokenTimeout) request := &networkservice.NetworkServiceRequest{ MechanismPreferences: []*networkservice.Mechanism{ @@ -250,7 +250,7 @@ func TestNSMGR_FloatingInterdomainUseCase(t *testing.T) { SetRegistryProxySupplier(nil). Build() - nsRegistryClient := cluster2.NewNSRegistryClient(ctx, sandbox.GenerateTestToken) + nsRegistryClient := cluster2.NewNSRegistryClient(ctx, sandbox.DefaultTokenTimeout) nsReg := ®istry.NetworkService{ Name: "my-service-interdomain@" + floating.Name, @@ -264,9 +264,9 @@ func TestNSMGR_FloatingInterdomainUseCase(t *testing.T) { NetworkServiceNames: []string{"my-service-interdomain"}, } - cluster2.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken) + cluster2.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.DefaultTokenTimeout) - nsc := cluster1.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken) + nsc := cluster1.Nodes[0].NewClient(ctx, sandbox.DefaultTokenTimeout) request := &networkservice.NetworkServiceRequest{ MechanismPreferences: []*networkservice.Mechanism{ @@ -343,7 +343,7 @@ func TestNSMGR_FloatingInterdomain_FourClusters(t *testing.T) { // register first ednpoint - nsRegistryClient := cluster2.NewNSRegistryClient(ctx, sandbox.GenerateTestToken) + nsRegistryClient := cluster2.NewNSRegistryClient(ctx, sandbox.DefaultTokenTimeout) nsReg1 := ®istry.NetworkService{ Name: "my-service-interdomain-1@" + floating.Name, @@ -357,7 +357,7 @@ func TestNSMGR_FloatingInterdomain_FourClusters(t *testing.T) { NetworkServiceNames: []string{"my-service-interdomain-1"}, } - cluster2.Nodes[0].NewEndpoint(ctx, nseReg1, sandbox.GenerateTestToken) + cluster2.Nodes[0].NewEndpoint(ctx, nseReg1, sandbox.DefaultTokenTimeout) nsReg2 := ®istry.NetworkService{ Name: "my-service-interdomain-1@" + floating.Name, @@ -365,7 +365,7 @@ func TestNSMGR_FloatingInterdomain_FourClusters(t *testing.T) { // register second ednpoint - nsRegistryClient = cluster3.NewNSRegistryClient(ctx, sandbox.GenerateTestToken) + nsRegistryClient = cluster3.NewNSRegistryClient(ctx, sandbox.DefaultTokenTimeout) _, err = nsRegistryClient.Register(ctx, nsReg2) require.NoError(t, err) @@ -375,11 +375,11 @@ func TestNSMGR_FloatingInterdomain_FourClusters(t *testing.T) { NetworkServiceNames: []string{"my-service-interdomain-2"}, } - cluster3.Nodes[0].NewEndpoint(ctx, nseReg2, sandbox.GenerateTestToken) + cluster3.Nodes[0].NewEndpoint(ctx, nseReg2, sandbox.DefaultTokenTimeout) // connect to first endpoint from cluster2 - nsc := cluster1.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken) + nsc := cluster1.Nodes[0].NewClient(ctx, sandbox.DefaultTokenTimeout) request := &networkservice.NetworkServiceRequest{ MechanismPreferences: []*networkservice.Mechanism{ @@ -495,13 +495,13 @@ func Test_Interdomain_PassThroughUsecase(t *testing.T) { kernelmech.NewClient(), )), connect.WithDialTimeout(sandbox.DialTimeout), - connect.WithDialOptions(sandbox.DefaultDialOptions(sandbox.GenerateTestToken)...), + connect.WithDialOptions(clusters[i].DefaultDialOptions(sandbox.DefaultTokenTimeout)...), ), ), } } - nsRegistryClient := clusters[i].NewNSRegistryClient(ctx, sandbox.GenerateTestToken) + nsRegistryClient := clusters[i].NewNSRegistryClient(ctx, sandbox.DefaultTokenTimeout) nsReg, err := nsRegistryClient.Register(ctx, ®istry.NetworkService{ Name: fmt.Sprintf("my-service-remote-%v", i), @@ -512,10 +512,10 @@ func Test_Interdomain_PassThroughUsecase(t *testing.T) { Name: fmt.Sprintf("endpoint-%v", i), NetworkServiceNames: []string{nsReg.Name}, } - clusters[i].Nodes[0].NewEndpoint(ctx, nsesReg, sandbox.GenerateTestToken, additionalFunctionality...) + clusters[i].Nodes[0].NewEndpoint(ctx, nsesReg, sandbox.DefaultTokenTimeout, additionalFunctionality...) } - nsc := clusters[clusterCount-1].Nodes[0].NewClient(ctx, sandbox.GenerateTestToken) + nsc := clusters[clusterCount-1].Nodes[0].NewClient(ctx, sandbox.DefaultTokenTimeout) request := &networkservice.NetworkServiceRequest{ MechanismPreferences: []*networkservice.Mechanism{ diff --git a/pkg/networkservice/common/connect/server_cancel_test.go b/pkg/networkservice/common/connect/server_cancel_test.go index d4727c2ee..a75b0e679 100644 --- a/pkg/networkservice/common/connect/server_cancel_test.go +++ b/pkg/networkservice/common/connect/server_cancel_test.go @@ -56,7 +56,7 @@ func TestConnect_CancelDuringRequest(t *testing.T) { SetRegistryProxySupplier(nil). Build() - nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.GenerateTestToken) + nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.DefaultTokenTimeout) service1Name := "my-service-endpoint" _, err = nsRegistryClient.Register(ctx, ®istry.NetworkService{ @@ -76,7 +76,7 @@ func TestConnect_CancelDuringRequest(t *testing.T) { } nscCtx, nscCancel := context.WithCancel(ctx) var flag atomic.Bool - domain.Nodes[0].NewEndpoint(ctx, nseReg1, sandbox.GenerateTestToken, checkrequest.NewServer(t, func(*testing.T, *networkservice.NetworkServiceRequest) { + domain.Nodes[0].NewEndpoint(ctx, nseReg1, sandbox.DefaultTokenTimeout, checkrequest.NewServer(t, func(*testing.T, *networkservice.NetworkServiceRequest) { if flag.Load() { nscCancel() } @@ -100,13 +100,13 @@ func TestConnect_CancelDuringRequest(t *testing.T) { Name: "endpoint-2", NetworkServiceNames: []string{service2Name}, } - domain.Nodes[0].NewEndpoint(ctx, nseReg2, sandbox.GenerateTestToken, + domain.Nodes[0].NewEndpoint(ctx, nseReg2, sandbox.DefaultTokenTimeout, chain.NewNetworkServiceServer( clienturl.NewServer(domain.Nodes[0].URL()), connect.NewServer(ctx, clientFactory, connect.WithDialTimeout(sandbox.DialTimeout), - connect.WithDialOptions(sandbox.DefaultDialOptions(sandbox.GenerateTestToken)...), + connect.WithDialOptions(domain.DefaultDialOptions(sandbox.DefaultTokenTimeout)...), ), ), ) @@ -121,8 +121,8 @@ func TestConnect_CancelDuringRequest(t *testing.T) { Context: &networkservice.ConnectionContext{}, }, } - nsc1 := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken) - nsc2 := domain.Nodes[0].NewClient(nscCtx, sandbox.GenerateTestToken) + nsc1 := domain.Nodes[0].NewClient(ctx, sandbox.DefaultTokenTimeout) + nsc2 := domain.Nodes[0].NewClient(nscCtx, sandbox.DefaultTokenTimeout) _, err = nsc1.Request(ctx, request.Clone()) require.NoError(t, err) diff --git a/pkg/networkservice/common/refresh/client_test.go b/pkg/networkservice/common/refresh/client_test.go index 9a131c797..97dcff082 100644 --- a/pkg/networkservice/common/refresh/client_test.go +++ b/pkg/networkservice/common/refresh/client_test.go @@ -234,10 +234,9 @@ func TestRefreshClient_Sandbox(t *testing.T) { domain := sandbox.NewBuilder(ctx, t). SetNodesCount(2). SetRegistryProxySupplier(nil). - SetTokenGenerateFunc(sandbox.GenerateTestToken). Build() - nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.GenerateTestToken) + nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.DefaultTokenTimeout) nsReg := ®istry.NetworkService{ Name: "my-service-remote", @@ -252,10 +251,9 @@ func TestRefreshClient_Sandbox(t *testing.T) { } refreshSrv := newRefreshTesterServer(t, sandboxMinDuration, sandboxExpireTimeout) - domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, refreshSrv) + domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.DefaultTokenTimeout, refreshSrv) - nscTokenGenerator := sandbox.GenerateExpiringToken(sandboxExpireTimeout) - nsc := domain.Nodes[1].NewClient(ctx, nscTokenGenerator) + nsc := domain.Nodes[1].NewClient(ctx, sandboxExpireTimeout) refreshSrv.beforeRequest("test-conn") _, err = nsc.Request(ctx, mkRequest("test-conn", nil)) diff --git a/pkg/registry/chains/memory/server_test.go b/pkg/registry/chains/memory/server_test.go index f621e1f38..f07255680 100644 --- a/pkg/registry/chains/memory/server_test.go +++ b/pkg/registry/chains/memory/server_test.go @@ -19,20 +19,17 @@ package memory_test import ( "context" + "testing" + "time" "github.com/networkservicemesh/api/pkg/api/networkservice/payload" "github.com/networkservicemesh/api/pkg/api/registry" - "github.com/networkservicemesh/sdk/pkg/tools/grpcutils" "github.com/networkservicemesh/sdk/pkg/tools/sandbox" "github.com/stretchr/testify/require" "go.uber.org/goleak" - "google.golang.org/grpc" - - "testing" - "time" ) func Test_RegistryMemory_ShouldSetDefaultPayload(t *testing.T) { @@ -47,23 +44,17 @@ func Test_RegistryMemory_ShouldSetDefaultPayload(t *testing.T) { SetNSMgrProxySupplier(nil). Build() - // start grpc client connection and register it - cc, err := grpc.DialContext(ctx, grpcutils.URLToTarget(domain.Registry.URL), sandbox.DefaultDialOptions(sandbox.GenerateTestToken)...) - require.NoError(t, err) - defer func() { - _ = cc.Close() - }() + nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.DefaultTokenTimeout) - nsrc := registry.NewNetworkServiceRegistryClient(cc) - ns, err := nsrc.Register(ctx, ®istry.NetworkService{ + ns, err := nsRegistryClient.Register(ctx, ®istry.NetworkService{ Name: "ns-1", }) require.NoError(t, err) - nsrfc, err := nsrc.Find(ctx, ®istry.NetworkServiceQuery{NetworkService: ns}) + stream, err := nsRegistryClient.Find(ctx, ®istry.NetworkServiceQuery{NetworkService: ns}) require.NoError(t, err) - ns, err = nsrfc.Recv() + ns, err = stream.Recv() require.NoError(t, err) require.Equal(t, payload.IP, ns.Payload) diff --git a/pkg/registry/common/heal/find_test.go b/pkg/registry/common/heal/find_test.go index 12a0d82d9..48e73dc40 100644 --- a/pkg/registry/common/heal/find_test.go +++ b/pkg/registry/common/heal/find_test.go @@ -45,10 +45,10 @@ func TestHealClient_FindTest(t *testing.T) { SetRegistryProxySupplier(nil). SetNSMgrProxySupplier(nil). SetNodeSetup(func(ctx context.Context, node *sandbox.Node, nodeNum int) { - node.NewNSMgr(nsmgrCtx, sandbox.UniqueName("nsmgr"), nil, sandbox.GenerateTestToken, nsmgr.NewServer) + node.NewNSMgr(nsmgrCtx, sandbox.UniqueName("nsmgr"), nil, sandbox.DefaultTokenTimeout, nsmgr.NewServer) node.NewForwarder(ctx, ®istry.NetworkServiceEndpoint{ Name: sandbox.UniqueName("forwarder"), - }, sandbox.GenerateTestToken) + }, sandbox.DefaultTokenTimeout) }). Build() @@ -56,7 +56,7 @@ func TestHealClient_FindTest(t *testing.T) { findCtx, findCancel := context.WithCancel(ctx) nsRegistryClient := registryclient.NewNetworkServiceRegistryClient(ctx, domain.Nodes[0].URL(), - registryclient.WithDialOptions(sandbox.DefaultDialOptions(sandbox.GenerateTestToken)...)) + registryclient.WithDialOptions(domain.DefaultDialOptions(sandbox.DefaultTokenTimeout)...)) nsStream, err := nsRegistryClient.Find(findCtx, ®istry.NetworkServiceQuery{ NetworkService: new(registry.NetworkService), @@ -65,7 +65,7 @@ func TestHealClient_FindTest(t *testing.T) { require.NoError(t, err) nseRegistryClient := registryclient.NewNetworkServiceEndpointRegistryClient(ctx, domain.Nodes[0].URL(), - registryclient.WithDialOptions(sandbox.DefaultDialOptions(sandbox.GenerateTestToken)...)) + registryclient.WithDialOptions(domain.DefaultDialOptions(sandbox.DefaultTokenTimeout)...)) nseStream, err := nseRegistryClient.Find(findCtx, ®istry.NetworkServiceEndpointQuery{ NetworkServiceEndpoint: new(registry.NetworkServiceEndpoint), @@ -81,7 +81,7 @@ func TestHealClient_FindTest(t *testing.T) { return grpcutils.CheckURLFree(mgr.URL) }, time.Second, 100*time.Millisecond) - mgr = domain.Nodes[0].NewNSMgr(ctx, mgr.Name, mgr.URL, sandbox.GenerateTestToken, nsmgr.NewServer) + mgr = domain.Nodes[0].NewNSMgr(ctx, mgr.Name, mgr.URL, sandbox.DefaultTokenTimeout, nsmgr.NewServer) // 3. Register new NS, NSE _, err = mgr.NetworkServiceRegistryServer().Register(ctx, ®istry.NetworkService{ diff --git a/pkg/tools/sandbox/builder.go b/pkg/tools/sandbox/builder.go index 4d28d9259..acd36604c 100644 --- a/pkg/tools/sandbox/builder.go +++ b/pkg/tools/sandbox/builder.go @@ -38,7 +38,6 @@ import ( "github.com/networkservicemesh/sdk/pkg/registry/common/dnsresolve" "github.com/networkservicemesh/sdk/pkg/tools/grpcutils" "github.com/networkservicemesh/sdk/pkg/tools/log" - "github.com/networkservicemesh/sdk/pkg/tools/token" ) // Builder implements builder pattern for building NSM Domain @@ -54,9 +53,11 @@ type Builder struct { supplyRegistryProxy SupplyRegistryProxyFunc setupNode SetupNodeFunc + supplyTokenGenerator SupplyTokenGeneratorFunc + name string dnsResolver dnsresolve.Resolver - generateTokenFunc token.GeneratorFunc + tokenTimeout time.Duration registryExpiryDuration time.Duration useUnixSockets bool @@ -74,16 +75,13 @@ func NewBuilder(ctx context.Context, t *testing.T) *Builder { supplyNSMgrProxy: nsmgrproxy.NewServer, supplyRegistry: memory.NewServer, supplyRegistryProxy: proxydns.NewServer, + supplyTokenGenerator: GenerateExpiringToken, name: "cluster.local", dnsResolver: new(FakeDNSResolver), - generateTokenFunc: GenerateTestToken, + tokenTimeout: DefaultTokenTimeout, registryExpiryDuration: time.Minute, } - b.setupNode = func(ctx context.Context, node *Node, _ int) { - SetupDefaultNode(ctx, node, b.supplyNSMgr) - } - return b } @@ -125,6 +123,12 @@ func (b *Builder) SetNodeSetup(f SetupNodeFunc) *Builder { return b } +// SetTokenGeneratorSupplier replaces default token generator supplier to custom func +func (b *Builder) SetTokenGeneratorSupplier(f SupplyTokenGeneratorFunc) *Builder { + b.supplyTokenGenerator = f + return b +} + // SetDNSDomainName sets DNS domain name for the building NSM domain func (b *Builder) SetDNSDomainName(name string) *Builder { b.name = name @@ -137,9 +141,9 @@ func (b *Builder) SetDNSResolver(d dnsresolve.Resolver) *Builder { return b } -// SetTokenGenerateFunc sets function for the token generation -func (b *Builder) SetTokenGenerateFunc(f token.GeneratorFunc) *Builder { - b.generateTokenFunc = f +// SetTokenTimeout sets token timeout +func (b *Builder) SetTokenTimeout(tokenTimeout time.Duration) *Builder { + b.tokenTimeout = tokenTimeout return b } @@ -164,8 +168,10 @@ func (b *Builder) UseUnixSockets() *Builder { // Build builds Domain and Supplier func (b *Builder) Build() *Domain { b.domain = &Domain{ - Name: b.name, - DNSResolver: b.dnsResolver, + t: b.t, + Name: b.name, + DNSResolver: b.dnsResolver, + supplyTokenGenerator: b.supplyTokenGenerator, } if b.useUnixSockets { @@ -198,6 +204,12 @@ func (b *Builder) Build() *Domain { b.domain.RegistryProxy = b.newRegistryProxy() b.domain.Registry = b.newRegistry() b.domain.NSMgrProxy = b.newNSMgrProxy() + + if b.setupNode == nil { + b.setupNode = func(ctx context.Context, node *Node, _ int) { + SetupDefaultNode(ctx, node, b.tokenTimeout, b.supplyNSMgr) + } + } for i := 0; i < b.nodesCount; i++ { b.domain.Nodes = append(b.domain.Nodes, b.newNode(i)) } @@ -232,9 +244,14 @@ func (b *Builder) newRegistryProxy() *RegistryEntry { return nil } - registryProxy := b.supplyRegistryProxy(b.ctx, b.dnsResolver, DefaultDialOptions(b.generateTokenFunc)...) serveURL := b.domain.supplyURL("reg-proxy") + registryProxy := b.supplyRegistryProxy( + b.ctx, + b.dnsResolver, + b.domain.DefaultDialOptions(b.tokenTimeout)..., + ) + serve(b.ctx, b.t, serveURL, registryProxy.Register) log.FromContext(b.ctx).Debugf("%s: registry-proxy-dns on: %v", b.name, serveURL) @@ -255,9 +272,15 @@ func (b *Builder) newRegistry() *RegistryEntry { nsmgrProxyURL = b.domain.NSMgrProxy.URL } - registry := b.supplyRegistry(b.ctx, b.registryExpiryDuration, nsmgrProxyURL, DefaultDialOptions(b.generateTokenFunc)...) serveURL := b.domain.supplyURL("reg") + registry := b.supplyRegistry( + b.ctx, + b.registryExpiryDuration, + nsmgrProxyURL, + b.domain.DefaultDialOptions(b.tokenTimeout)..., + ) + serve(b.ctx, b.t, serveURL, registry.Register) log.FromContext(b.ctx).Debugf("%s: registry on: %v", b.name, serveURL) @@ -274,17 +297,20 @@ func (b *Builder) newNSMgrProxy() *NSMgrEntry { } name := UniqueName("nsmgr-proxy") + tokenGenerator := b.supplyTokenGenerator(b.tokenTimeout) + dialOptions := b.domain.DefaultDialOptions(b.tokenTimeout) + mgr := b.supplyNSMgrProxy(b.ctx, b.domain.Registry.URL, b.domain.RegistryProxy.URL, - b.generateTokenFunc, + tokenGenerator, nsmgrproxy.WithListenOn(b.domain.NSMgrProxy.URL), nsmgrproxy.WithName(name), nsmgrproxy.WithConnectOptions( connect.WithDialTimeout(DialTimeout), - connect.WithDialOptions(DefaultDialOptions(b.generateTokenFunc)...)), + connect.WithDialOptions(dialOptions...)), nsmgrproxy.WithRegistryConnectOptions( - registryconnect.WithDialOptions(DefaultDialOptions(b.generateTokenFunc)...), + registryconnect.WithDialOptions(dialOptions...), ), ) @@ -301,8 +327,7 @@ func (b *Builder) newNSMgrProxy() *NSMgrEntry { func (b *Builder) newNode(nodeNum int) *Node { node := &Node{ - t: b.t, - domain: b.domain, + Domain: b.domain, } b.setupNode(b.ctx, node, nodeNum) diff --git a/pkg/tools/sandbox/domain.go b/pkg/tools/sandbox/domain.go new file mode 100644 index 000000000..aa3d56669 --- /dev/null +++ b/pkg/tools/sandbox/domain.go @@ -0,0 +1,67 @@ +// Copyright (c) 2021 Doc.ai and/or its affiliates. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package sandbox + +import ( + "context" + "net/url" + "testing" + "time" + + registryapi "github.com/networkservicemesh/api/pkg/api/registry" + "google.golang.org/grpc" + + registryclient "github.com/networkservicemesh/sdk/pkg/registry/chains/client" + "github.com/networkservicemesh/sdk/pkg/registry/common/dnsresolve" +) + +// Domain contains attached to domain nodes, registry +type Domain struct { + t *testing.T + + Nodes []*Node + NSMgrProxy *NSMgrEntry + Registry *RegistryEntry + RegistryProxy *RegistryEntry + + DNSResolver dnsresolve.Resolver + Name string + + supplyURL func(prefix string) *url.URL + supplyTokenGenerator SupplyTokenGeneratorFunc +} + +// NewNSRegistryClient creates new NS registry client for the domain +func (d *Domain) NewNSRegistryClient(ctx context.Context, tokenTimeout time.Duration) registryapi.NetworkServiceRegistryClient { + var registryURL *url.URL + switch { + case d.Registry != nil: + registryURL = d.Registry.URL + case len(d.Nodes) != 0: + registryURL = d.Nodes[0].URL() + default: + return nil + } + + return registryclient.NewNetworkServiceRegistryClient(ctx, registryURL, + registryclient.WithDialOptions(d.DefaultDialOptions(tokenTimeout)...)) +} + +// DefaultDialOptions returns default dial options for the domain +func (d *Domain) DefaultDialOptions(tokenTimeout time.Duration) []grpc.DialOption { + return DefaultDialOptions(d.supplyTokenGenerator(tokenTimeout)) +} diff --git a/pkg/tools/sandbox/node.go b/pkg/tools/sandbox/node.go index 5ad505fa1..45f0ef6ea 100644 --- a/pkg/tools/sandbox/node.go +++ b/pkg/tools/sandbox/node.go @@ -19,11 +19,12 @@ package sandbox import ( "context" "net/url" - "testing" + "time" + + "github.com/stretchr/testify/require" "github.com/networkservicemesh/api/pkg/api/networkservice" registryapi "github.com/networkservicemesh/api/pkg/api/registry" - "github.com/stretchr/testify/require" "github.com/networkservicemesh/sdk/pkg/networkservice/chains/client" "github.com/networkservicemesh/sdk/pkg/networkservice/chains/endpoint" @@ -37,15 +38,13 @@ import ( registryclient "github.com/networkservicemesh/sdk/pkg/registry/chains/client" "github.com/networkservicemesh/sdk/pkg/tools/addressof" "github.com/networkservicemesh/sdk/pkg/tools/log" - "github.com/networkservicemesh/sdk/pkg/tools/token" ) // Node is a NSMgr with Forwarder, NSE registry clients type Node struct { - t *testing.T - domain *Domain - NSMgr *NSMgrEntry + + *Domain } // URL returns node NSMgr URL @@ -60,14 +59,14 @@ func (n *Node) NewNSMgr( ctx context.Context, name string, serveURL *url.URL, - generatorFunc token.GeneratorFunc, + tokenTimeout time.Duration, supplyNSMgr SupplyNSMgrFunc, ) *NSMgrEntry { if serveURL == nil { - serveURL = n.domain.supplyURL("nsmgr") + serveURL = n.supplyURL("nsmgr") } - dialOptions := DefaultDialOptions(generatorFunc) + dialOptions := n.DefaultDialOptions(tokenTimeout) options := []nsmgr.Option{ nsmgr.WithName(name), @@ -77,8 +76,8 @@ func (n *Node) NewNSMgr( connect.WithDialOptions(dialOptions...)), } - if n.domain.Registry != nil { - options = append(options, nsmgr.WithRegistry(n.domain.Registry.URL, dialOptions...)) + if n.Registry != nil { + options = append(options, nsmgr.WithRegistry(n.Registry.URL, dialOptions...)) } if serveURL.Scheme != "unix" { @@ -86,14 +85,14 @@ func (n *Node) NewNSMgr( } entry := &NSMgrEntry{ - Nsmgr: supplyNSMgr(ctx, generatorFunc, options...), + Nsmgr: supplyNSMgr(ctx, n.supplyTokenGenerator(tokenTimeout), options...), Name: name, URL: serveURL, } serve(ctx, n.t, serveURL, entry.Register) - log.FromContext(ctx).Debugf("%s: NSMgr %s on %v", n.domain.Name, name, serveURL) + log.FromContext(ctx).Debugf("%s: NSMgr %s on %v", n.Name, name, serveURL) n.NSMgr = entry @@ -104,14 +103,14 @@ func (n *Node) NewNSMgr( func (n *Node) NewForwarder( ctx context.Context, nse *registryapi.NetworkServiceEndpoint, - generatorFunc token.GeneratorFunc, + tokenTimeout time.Duration, additionalFunctionality ...networkservice.NetworkServiceServer, ) *EndpointEntry { if nse.Url == "" { - nse.Url = n.domain.supplyURL("forwarder").String() + nse.Url = n.supplyURL("forwarder").String() } - dialOptions := DefaultDialOptions(generatorFunc) + dialOptions := n.DefaultDialOptions(tokenTimeout) entry := new(EndpointEntry) additionalFunctionality = append(additionalFunctionality, @@ -134,7 +133,7 @@ func (n *Node) NewForwarder( *entry = *n.newEndpoint( ctx, nse, - generatorFunc, + tokenTimeout, registryclient.NewNetworkServiceEndpointRegistryInterposeClient(ctx, n.URL(), registryclient.WithDialOptions(dialOptions...)), additionalFunctionality..., @@ -147,19 +146,19 @@ func (n *Node) NewForwarder( func (n *Node) NewEndpoint( ctx context.Context, nse *registryapi.NetworkServiceEndpoint, - generatorFunc token.GeneratorFunc, + tokenTimeout time.Duration, additionalFunctionality ...networkservice.NetworkServiceServer, ) *EndpointEntry { if nse.Url == "" { - nse.Url = n.domain.supplyURL("nse").String() + nse.Url = n.supplyURL("nse").String() } return n.newEndpoint( ctx, nse, - generatorFunc, + tokenTimeout, registryclient.NewNetworkServiceEndpointRegistryClient(ctx, n.URL(), - registryclient.WithDialOptions(DefaultDialOptions(generatorFunc)...)), + registryclient.WithDialOptions(n.DefaultDialOptions(tokenTimeout)...)), additionalFunctionality..., ) } @@ -167,12 +166,14 @@ func (n *Node) NewEndpoint( func (n *Node) newEndpoint( ctx context.Context, nse *registryapi.NetworkServiceEndpoint, - generatorFunc token.GeneratorFunc, + tokenTimeout time.Duration, registryClient registryapi.NetworkServiceEndpointRegistryClient, additionalFunctionality ...networkservice.NetworkServiceServer, ) *EndpointEntry { + tokenGenerator := n.supplyTokenGenerator(tokenTimeout) + name := nse.Name - ep := endpoint.NewServer(ctx, generatorFunc, + ep := endpoint.NewServer(ctx, tokenGenerator, endpoint.WithName(name), endpoint.WithAdditionalFunctionality(additionalFunctionality...), ) @@ -189,7 +190,7 @@ func (n *Node) newEndpoint( nse.ExpirationTime = reg.ExpirationTime nse.NetworkServiceLabels = reg.NetworkServiceLabels - log.FromContext(ctx).Debugf("%s: endpoint %s on %v", n.domain.Name, nse.Name, serveURL) + log.FromContext(ctx).Debugf("%s: endpoint %s on %v", n.Name, nse.Name, serveURL) return &EndpointEntry{ Name: name, @@ -202,13 +203,13 @@ func (n *Node) newEndpoint( // NewClient starts a new client and connects it to the node NSMgr func (n *Node) NewClient( ctx context.Context, - generatorFunc token.GeneratorFunc, + tokenTimeout time.Duration, additionalFunctionality ...networkservice.NetworkServiceClient, ) networkservice.NetworkServiceClient { return client.NewClient( ctx, n.URL(), - client.WithDialOptions(DefaultDialOptions(generatorFunc)...), + client.WithDialOptions(n.DefaultDialOptions(tokenTimeout)...), client.WithDialTimeout(DialTimeout), client.WithAuthorizeClient(authorize.NewClient(authorize.Any())), client.WithAdditionalFunctionality(additionalFunctionality...), diff --git a/pkg/tools/sandbox/types.go b/pkg/tools/sandbox/types.go index e21a747ee..87163fbee 100644 --- a/pkg/tools/sandbox/types.go +++ b/pkg/tools/sandbox/types.go @@ -28,26 +28,28 @@ import ( "github.com/networkservicemesh/sdk/pkg/networkservice/chains/nsmgr" "github.com/networkservicemesh/sdk/pkg/networkservice/chains/nsmgrproxy" "github.com/networkservicemesh/sdk/pkg/registry" - registryclient "github.com/networkservicemesh/sdk/pkg/registry/chains/client" "github.com/networkservicemesh/sdk/pkg/registry/common/dnsresolve" "github.com/networkservicemesh/sdk/pkg/tools/token" ) -// SupplyNSMgrProxyFunc nsmgr proxy +// SupplyNSMgrProxyFunc supplies NSMgr proxy type SupplyNSMgrProxyFunc func(ctx context.Context, regURL, proxyURL *url.URL, tokenGenerator token.GeneratorFunc, options ...nsmgrproxy.Option) nsmgr.Nsmgr -// SupplyNSMgrFunc supplies NSMGR +// SupplyNSMgrFunc supplies NSMgr type SupplyNSMgrFunc func(ctx context.Context, tokenGenerator token.GeneratorFunc, options ...nsmgr.Option) nsmgr.Nsmgr // SupplyRegistryFunc supplies Registry type SupplyRegistryFunc func(ctx context.Context, expiryDuration time.Duration, proxyRegistryURL *url.URL, options ...grpc.DialOption) registry.Registry -// SupplyRegistryProxyFunc supplies registry proxy +// SupplyRegistryProxyFunc supplies Registry proxy type SupplyRegistryProxyFunc func(ctx context.Context, dnsResolver dnsresolve.Resolver, options ...grpc.DialOption) registry.Registry // SetupNodeFunc setups each node on Builder.Build() stage type SetupNodeFunc func(ctx context.Context, node *Node, nodeNum int) +// SupplyTokenGeneratorFunc supplies token generator +type SupplyTokenGeneratorFunc func(tokenTimeout time.Duration) token.GeneratorFunc + // RegistryEntry is pair of registry.Registry and url.URL type RegistryEntry struct { URL *url.URL @@ -71,32 +73,3 @@ type EndpointEntry struct { endpoint.Endpoint registryapi.NetworkServiceEndpointRegistryClient } - -// Domain contains attached to domain nodes, registry -type Domain struct { - Nodes []*Node - NSMgrProxy *NSMgrEntry - Registry *RegistryEntry - RegistryProxy *RegistryEntry - - DNSResolver dnsresolve.Resolver - Name string - - supplyURL func(prefix string) *url.URL -} - -// NewNSRegistryClient creates new NS registry client for the domain -func (d *Domain) NewNSRegistryClient(ctx context.Context, generatorFunc token.GeneratorFunc) registryapi.NetworkServiceRegistryClient { - var registryURL *url.URL - switch { - case d.Registry != nil: - registryURL = d.Registry.URL - case len(d.Nodes) != 0: - registryURL = d.Nodes[0].URL() - default: - return nil - } - - return registryclient.NewNetworkServiceRegistryClient(ctx, registryURL, - registryclient.WithDialOptions(DefaultDialOptions(generatorFunc)...)) -} diff --git a/pkg/tools/sandbox/utils.go b/pkg/tools/sandbox/utils.go index a7e4a006e..9e227efa5 100644 --- a/pkg/tools/sandbox/utils.go +++ b/pkg/tools/sandbox/utils.go @@ -23,12 +23,11 @@ import ( "github.com/edwarnicke/grpcfd" "github.com/google/uuid" + registryapi "github.com/networkservicemesh/api/pkg/api/registry" "google.golang.org/grpc" "google.golang.org/grpc/credentials" "google.golang.org/grpc/credentials/insecure" - registryapi "github.com/networkservicemesh/api/pkg/api/registry" - "github.com/networkservicemesh/sdk/pkg/tools/opentracing" "github.com/networkservicemesh/sdk/pkg/tools/token" ) @@ -38,6 +37,8 @@ const ( RegistryExpiryDuration = time.Second // DialTimeout is a default dial timeout for the sandbox tests DialTimeout = 500 * time.Millisecond + // DefaultTokenTimeout is a default token timeout for sandbox testing + DefaultTokenTimeout = time.Hour ) type insecurePerRPCCredentials struct { @@ -109,10 +110,10 @@ func UniqueName(prefix string) string { } // SetupDefaultNode setups NSMgr and default Forwarder on the given node -func SetupDefaultNode(ctx context.Context, node *Node, supplyNSMgr SupplyNSMgrFunc) { - node.NewNSMgr(ctx, UniqueName("nsmgr"), nil, GenerateTestToken, supplyNSMgr) +func SetupDefaultNode(ctx context.Context, node *Node, tokenTimeout time.Duration, supplyNSMgr SupplyNSMgrFunc) { + node.NewNSMgr(ctx, UniqueName("nsmgr"), nil, tokenTimeout, supplyNSMgr) node.NewForwarder(ctx, ®istryapi.NetworkServiceEndpoint{ Name: UniqueName("forwarder"), - }, GenerateTestToken) + }, tokenTimeout) }