From e6f4ce2056e0654f15e33bdcfa7dd5c7633ce96c Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Thu, 25 Nov 2021 15:32:34 +0700 Subject: [PATCH] add remote tests Signed-off-by: Nikita Skrynnik --- internal/tests/suite_combinatronics_test.go | 58 +++++++++++++-------- internal/tests/suite_kernel_test.go | 22 ++++++-- internal/tests/suite_setup_test.go | 8 +-- internal/tests/suite_util_test.go | 5 +- 4 files changed, 60 insertions(+), 33 deletions(-) diff --git a/internal/tests/suite_combinatronics_test.go b/internal/tests/suite_combinatronics_test.go index ac7ab90e..8ea6241e 100644 --- a/internal/tests/suite_combinatronics_test.go +++ b/internal/tests/suite_combinatronics_test.go @@ -20,7 +20,11 @@ package tests import ( "context" + "io/ioutil" "net" + "net/url" + "os" + "path/filepath" "strings" "testing" "time" @@ -38,9 +42,9 @@ import ( "github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/wireguard" "github.com/networkservicemesh/api/pkg/api/networkservice/payload" "github.com/networkservicemesh/api/pkg/api/registry" - "github.com/networkservicemesh/sdk/pkg/registry/core/adapters" "github.com/networkservicemesh/api/pkg/api/networkservice" + "github.com/networkservicemesh/sdk/pkg/registry/core/adapters" "github.com/networkservicemesh/sdk/pkg/tools/log" "github.com/networkservicemesh/sdk/pkg/tools/log/logruslogger" "github.com/networkservicemesh/sdk/pkg/tools/spiffejwt" @@ -108,14 +112,18 @@ func (f *ForwarderTestSuite) TestCombinations() { payload.IP: { kernel.MECHANISM, memif.MECHANISM, - // wireguard.MECHANISM, + wireguard.MECHANISM, }, payload.Ethernet: { kernel.MECHANISM, memif.MECHANISM, - // vxlan.MECHANISM, + vxlan.MECHANISM, }, } + isRemote := map[string]bool{ + wireguard.MECHANISM: true, + vxlan.MECHANISM: true, + } for _, pl := range []string{payload.Ethernet, payload.IP} { payloadName := pl f.T().Run(strings.Title(strings.ToLower(payloadName)), func(t *testing.T) { @@ -133,21 +141,16 @@ func (f *ForwarderTestSuite) TestCombinations() { defer cancel() ctx = log.WithFields(ctx, map[string]interface{}{"test": t.Name()}) ctx = log.WithLog(ctx, logruslogger.New(ctx)) - networkserviceName := "ns" - - _, err = adapters.NetworkServiceEndpointServerToClient(f.registryServer).Register(ctx, ®istry.NetworkServiceEndpoint{ - Name: "nse", - NetworkServiceNames: []string{"ns"}, - Url: f.config.ConnectTo.String(), - }) - f.Require().NoError(err) + networkserviceName := "ns" + t.Name() - _, err = adapters.NetworkServiceServerToClient(f.registryNSServer).Register(ctx, ®istry.NetworkService{ - Name: "ns", - Payload: payloadName, - }) + tmpDir, err := ioutil.TempDir("", "nse") f.Require().NoError(err) + defer func(tmpDir string) { _ = os.Remove(tmpDir) }(tmpDir) + nseURL := url.URL{Scheme: "unix", Path: filepath.Join(tmpDir, "listen.on")} + if isRemote[endpointMechanism] { + nseURL = url.URL{Scheme: "tcp", Host: "127.0.0.1:0"} + } testRequest := &networkservice.NetworkServiceRequest{ Connection: &networkservice.Connection{ NetworkService: networkserviceName, @@ -164,11 +167,26 @@ func (f *ForwarderTestSuite) TestCombinations() { ep := epFunc(ctx) networkservice.RegisterNetworkServiceServer(server, ep) networkservice.RegisterMonitorConnectionServer(server, ep) - registry.RegisterNetworkServiceEndpointRegistryServer(server, f.registryServer) - registry.RegisterNetworkServiceRegistryServer(server, f.registryNSServer) - serverErrCh := f.ListenAndServe(ctx, server) + + serverErrCh := f.ListenAndServe(f.ctx, &nseURL, server) + f.Require().Len(serverErrCh, 0) log.FromContext(ctx).Infof("Launching %s test server (took : %s)", t.Name(), time.Since(now)) + _, err = adapters.NetworkServiceEndpointServerToClient(f.registryServer).Register(ctx, ®istry.NetworkServiceEndpoint{ + Name: "nse-" + t.Name(), + NetworkServiceNames: []string{networkserviceName}, + Url: nseURL.String(), + }) + f.Require().NoError(err) + + _, err = adapters.NetworkServiceServerToClient(f.registryNSServer).Register(ctx, ®istry.NetworkService{ + Name: networkserviceName, + Payload: payloadName, + }) + f.Require().NoError(err) + + log.FromContext(ctx).Infof("Registered ns and nse for server %s (took : %s)", t.Name(), time.Since(now)) + // ******************************************************************************** log.FromContext(f.ctx).Infof("Sending Request to forwarder (time since start: %s)", time.Since(starttime)) // ******************************************************************************** @@ -207,9 +225,7 @@ func (f *ForwarderTestSuite) TestCombinations() { // ******************************************************************************** log.FromContext(f.ctx).Infof("Canceling ctx to end test (time since start: %s)", time.Since(starttime)) // ******************************************************************************** - cancel() - err = <-serverErrCh - require.NoError(t, err) + require.Len(t, serverErrCh, 0) // ******************************************************************************** log.FromContext(f.ctx).Infof("%s completed (time since start: %s)", t.Name(), time.Since(starttime)) // ******************************************************************************** diff --git a/internal/tests/suite_kernel_test.go b/internal/tests/suite_kernel_test.go index 8d748236..8a9b30d3 100644 --- a/internal/tests/suite_kernel_test.go +++ b/internal/tests/suite_kernel_test.go @@ -23,6 +23,7 @@ import ( "fmt" "net" "os" + "runtime" "github.com/edwarnicke/exechelper" "github.com/pkg/errors" @@ -80,14 +81,21 @@ func newKernelVerifiableEndpoint(ctx context.Context, panic(fmt.Sprintf("unable to get root netNs: %+v", err)) } endpointNSName := fmt.Sprintf("nse-%s", randstr.Hex(4)) + runtime.LockOSThread() + defer runtime.UnlockOSThread() endpointNSHandle, err := netns.NewNamed(endpointNSName) if err != nil { panic(fmt.Sprintf("unable create netNs %s: %+v", endpointNSName, err)) } - go func(endpointNsName string) { + if err := netns.Set(rootNSHandle); err != nil { + panic(fmt.Sprintf("unable to set rootNs: %+v", err)) + } + go func(endpointNsName string, endpointNSHandle netns.NsHandle) { <-ctx.Done() + _ = endpointNSHandle.Close() _ = netns.DeleteNamed(endpointNsName) - }(endpointNSName) + }(endpointNSName, endpointNSHandle) + return &kernelVerifiableEndpoint{ ctx: ctx, endpointNSName: endpointNSName, @@ -150,14 +158,20 @@ func newKernelVerifiableClient(ctx context.Context, sutCC grpc.ClientConnInterfa panic(fmt.Sprintf("unable to get root netNs: %+v", err)) } clientNSName := fmt.Sprintf("client-%s", randstr.Hex(4)) + runtime.LockOSThread() + defer runtime.UnlockOSThread() clientNSHandle, err := netns.NewNamed(clientNSName) if err != nil { panic(fmt.Sprintf("unable create netNs %s: %+v", clientNSName, err)) } - go func(clientNSName string) { + if err := netns.Set(rootNSHandle); err != nil { + panic(fmt.Sprintf("unable to set rootNs: %+v", err)) + } + go func(clientNSName string, clientNSHandle netns.NsHandle) { <-ctx.Done() + _ = clientNSHandle.Close() _ = netns.DeleteNamed(clientNSName) - }(clientNSName) + }(clientNSName, clientNSHandle) rv := &kernelVerifiableClient{ ctx: ctx, diff --git a/internal/tests/suite_setup_test.go b/internal/tests/suite_setup_test.go index 30e2f10a..00946890 100644 --- a/internal/tests/suite_setup_test.go +++ b/internal/tests/suite_setup_test.go @@ -147,12 +147,8 @@ func (f *ForwarderTestSuite) SetupSuite() { registry.RegisterNetworkServiceEndpointRegistryServer(server, f.registryServer) registry.RegisterNetworkServiceRegistryServer(server, f.registryNSServer) - ctx, cancel := context.WithCancel(f.ctx) - defer func(cancel context.CancelFunc, serverErrCh <-chan error) { - cancel() - err = <-serverErrCh - f.Require().NoError(err) - }(cancel, f.ListenAndServe(ctx, server)) + f.Require().Len(f.ListenAndServe(f.ctx, &f.config.ConnectTo, server), 0) + ctx := f.ctx recv, err := adapters.NetworkServiceEndpointServerToClient(memrg).Find(ctx, ®istry.NetworkServiceEndpointQuery{ NetworkServiceEndpoint: ®istry.NetworkServiceEndpoint{ diff --git a/internal/tests/suite_util_test.go b/internal/tests/suite_util_test.go index 1ff26bba..a31dc85e 100644 --- a/internal/tests/suite_util_test.go +++ b/internal/tests/suite_util_test.go @@ -21,6 +21,7 @@ package tests import ( "context" "net" + "net/url" "time" "github.com/pkg/errors" @@ -37,8 +38,8 @@ const ( serverIP = "10.0.2.3" ) -func (f *ForwarderTestSuite) ListenAndServe(ctx context.Context, server *grpc.Server) <-chan error { - errCh := grpcutils.ListenAndServe(ctx, &f.config.ConnectTo, server) +func (f *ForwarderTestSuite) ListenAndServe(ctx context.Context, listenOn *url.URL, server *grpc.Server) <-chan error { + errCh := grpcutils.ListenAndServe(ctx, listenOn, server) select { case err, ok := <-errCh: f.Require().True(ok)