diff --git a/pkg/networkservice/common/interpose/server_test.go b/pkg/networkservice/common/interpose/server_test.go index 5b283a7e9..b9faff297 100644 --- a/pkg/networkservice/common/interpose/server_test.go +++ b/pkg/networkservice/common/interpose/server_test.go @@ -54,8 +54,6 @@ func TestInterposeServer(t *testing.T) { client := next.NewNetworkServiceClient( updatepath.NewClient("client"), adapters.NewServerToClient(next.NewNetworkServiceServer( - // actual `connect.NewServer()` should not update `request.Connection.Path.PathIndex` - new(restorePathServer), updatepath.NewServer("nsmgr"), clienturl.NewServer(&nseURL), interposeServer, @@ -66,11 +64,9 @@ func TestInterposeServer(t *testing.T) { }), )), adapters.NewServerToClient(next.NewNetworkServiceServer( - new(restorePathServer), updatepath.NewServer("interpose-nse"), )), adapters.NewServerToClient(next.NewNetworkServiceServer( - new(restorePathServer), updatepath.NewServer("nsmgr"), clienturl.NewServer(&nseURL), interposeServer, @@ -81,7 +77,6 @@ func TestInterposeServer(t *testing.T) { }), )), adapters.NewServerToClient(next.NewNetworkServiceServer( - new(restorePathServer), updatepath.NewServer("endpoint"), touchServer, )), @@ -119,22 +114,6 @@ func TestInterposeServer(t *testing.T) { require.True(t, touchServer.touched) } -type restorePathServer struct{} - -func (s *restorePathServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) { - pathIndex := request.Connection.Path.Index - conn, err := next.Server(ctx).Request(ctx, request) - request.Connection.Path.Index = pathIndex - return conn, err -} - -func (s *restorePathServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) { - pathIndex := conn.Path.Index - _, err := next.Server(ctx).Close(ctx, conn) - conn.Path.Index = pathIndex - return &empty.Empty{}, err -} - type touchServer struct { touched bool } diff --git a/pkg/networkservice/common/updatepath/client.go b/pkg/networkservice/common/updatepath/client.go index 318970881..53d15da7d 100644 --- a/pkg/networkservice/common/updatepath/client.go +++ b/pkg/networkservice/common/updatepath/client.go @@ -55,7 +55,6 @@ func (i *updatePathClient) Request(ctx context.Context, request *networkservice. return nil, err } - conn.Id = conn.Path.PathSegments[index].Id conn.Path.Index = index return conn, err diff --git a/pkg/networkservice/common/updatepath/client_test.go b/pkg/networkservice/common/updatepath/client_test.go new file mode 100644 index 000000000..891fc1f14 --- /dev/null +++ b/pkg/networkservice/common/updatepath/client_test.go @@ -0,0 +1,208 @@ +// Copyright (c) 2020 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 updatepath_test provides a tests for package 'setid' +package updatepath_test + +import ( + "context" + "testing" + + "github.com/golang/protobuf/ptypes/empty" + "github.com/stretchr/testify/require" + "google.golang.org/grpc" + + "github.com/networkservicemesh/api/pkg/api/networkservice" + "go.uber.org/goleak" + + "github.com/networkservicemesh/sdk/pkg/networkservice/common/updatepath" + "github.com/networkservicemesh/sdk/pkg/networkservice/core/next" + "github.com/networkservicemesh/sdk/pkg/networkservice/utils/checks/checkrequest" +) + +const ( + pathSegmentID1 string = "36ce7f0c-9f6d-40a4-8b39-6b56ff07eea9" + pathSegmentID2 string = "ece490ea-dfe8-4512-a3ca-5be7b39515c5" + connectionID string = "54ec76a7-d642-43ca-87bc-ab51765f575a" +) + +func request(connectionID string, pathIndex uint32) *networkservice.NetworkServiceRequest { + return &networkservice.NetworkServiceRequest{ + Connection: &networkservice.Connection{ + Id: connectionID, + Path: &networkservice.Path{ + Index: pathIndex, + PathSegments: []*networkservice.PathSegment{ + { + Name: "nse-1", + Id: pathSegmentID1, + }, + { + Name: "nse-2", + Id: pathSegmentID2, + }, + }, + }, + }, + } +} + +func TestNewClient_SetNewConnectionId(t *testing.T) { + defer goleak.VerifyNone(t, goleak.IgnoreCurrent()) + + client := updatepath.NewClient("nse-3") + + conn, err := client.Request(context.Background(), request(connectionID, 1)) + require.NoError(t, err) + require.NotNil(t, conn) + require.Equal(t, 3, len(conn.Path.PathSegments)) + require.Equal(t, "nse-3", conn.Path.PathSegments[2].Name) + require.NotEqual(t, conn.Id, connectionID) +} + +func TestNewClient_SetNewConnectionId2(t *testing.T) { + defer goleak.VerifyNone(t) + + client := next.NewNetworkServiceClient( + updatepath.NewClient("nse-2"), + checkrequest.NewClient(t, func(t *testing.T, request *networkservice.NetworkServiceRequest) { + require.Equal(t, 1, int(request.Connection.Path.Index)) + }), + ) + + conn, err := client.Request(context.Background(), request(connectionID, 0)) + require.NoError(t, err) + require.NotNil(t, conn) + require.Equal(t, conn.Id, pathSegmentID2) +} + +func TestNewClient_SetNewConnectionIdSecondReq(t *testing.T) { + defer goleak.VerifyNone(t) + + client := updatepath.NewClient("nse-1") + + conn, err := client.Request(context.Background(), request(connectionID, 0)) + require.NoError(t, err) + require.NotNil(t, conn) + require.Equal(t, conn.Id, pathSegmentID1) + + firstConnID := conn.Id + + conn, err = client.Request(context.Background(), &networkservice.NetworkServiceRequest{ + Connection: conn, + }) + require.NoError(t, err) + require.NotNil(t, conn) + require.Equal(t, firstConnID, conn.Id) +} + +func TestNewClient_PathSegmentNameEqualClientName(t *testing.T) { + defer goleak.VerifyNone(t, goleak.IgnoreCurrent()) + + client := updatepath.NewClient("nse-2") + + conn, err := client.Request(context.Background(), request(connectionID, 1)) + require.NoError(t, err) + require.NotNil(t, conn) + require.NotEqual(t, conn.Id, connectionID) +} + +func TestNewClient_PathSegmentIdEqualConnectionId(t *testing.T) { + defer goleak.VerifyNone(t, goleak.IgnoreCurrent()) + + client := updatepath.NewClient("nse-2") + + conn, err := client.Request(context.Background(), request(pathSegmentID2, 1)) + require.NoError(t, err) + require.NotNil(t, conn) + require.Equal(t, conn.Id, pathSegmentID2) +} + +func TestNewClient_PathSegmentNameAndIDEqualClientNameAndID(t *testing.T) { + defer goleak.VerifyNone(t) + + client := updatepath.NewClient("nse-2") + + conn, err := client.Request(context.Background(), request(pathSegmentID2, 1)) + require.NoError(t, err) + require.NotNil(t, conn) + require.Equal(t, conn.Id, pathSegmentID2) +} + +func TestNewClient_InvalidIndex(t *testing.T) { + defer goleak.VerifyNone(t) + + client := updatepath.NewClient("nse-3") + + conn, err := client.Request(context.Background(), request(connectionID, 2)) + require.Error(t, err) + require.Nil(t, conn) +} + +func TestNewClient_NoConnection(t *testing.T) { + defer goleak.VerifyNone(t) + + client := updatepath.NewClient("nse-3") + + conn, err := client.Request(context.Background(), &networkservice.NetworkServiceRequest{}) + require.NoError(t, err) + require.NotNil(t, conn) + require.NotEqual(t, conn.Id, connectionID) +} + +func TestNewClient_RestoreIndex(t *testing.T) { + defer goleak.VerifyNone(t, goleak.IgnoreCurrent()) + + client := next.NewNetworkServiceClient( + updatepath.NewClient("nse-1"), + newPathIndexClient(t, 0), + updatepath.NewClient("nse-2"), + newPathIndexClient(t, 1), + updatepath.NewClient("nse-2"), + newPathIndexClient(t, 1), + updatepath.NewClient("nse-3"), + newPathIndexClient(t, 2), + ) + + _, err := client.Request(context.Background(), request(connectionID, 0)) + require.NoError(t, err) +} + +type pathIndexClient struct { + t *testing.T + index uint32 +} + +func newPathIndexClient(t *testing.T, index uint32) *pathIndexClient { + return &pathIndexClient{ + t: t, + index: index, + } +} + +func (c *pathIndexClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) { + require.Equal(c.t, c.index, request.Connection.Path.Index) + conn, err := next.Client(ctx).Request(ctx, request, opts...) + if err != nil { + return nil, err + } + require.Equal(c.t, c.index, conn.Path.Index) + return conn, nil +} + +func (c *pathIndexClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) { + return next.Client(ctx).Close(ctx, conn, opts...) +} diff --git a/pkg/networkservice/common/updatepath/common.go b/pkg/networkservice/common/updatepath/common.go index fc5d74974..c4681f268 100644 --- a/pkg/networkservice/common/updatepath/common.go +++ b/pkg/networkservice/common/updatepath/common.go @@ -68,7 +68,7 @@ func updatePath(conn *networkservice.Connection, segmentName string) (*networkse nextIndex := int(path.Index) + 1 if nextIndex > len(path.GetPathSegments()) { // We have index > segments count - return nil, 0, errors.Errorf("Path.Index+1==%d should be less or equal len(Path.PathSegments)==%d", + return nil, 0, errors.Errorf("NetworkServiceRequest.Connection.Path.Index+1==%d should be less or equal len(NetworkServiceRequest.Connection.Path.PathSegement)==%d", nextIndex, len(path.GetPathSegments())) } @@ -99,5 +99,5 @@ func updatePath(conn *networkservice.Connection, segmentName string) (*networkse conn.Id = path.GetPathSegments()[conn.Path.Index].Id } - return conn, conn.Path.Index - 1, nil + return conn, path.Index - 1, nil } diff --git a/pkg/networkservice/common/updatepath/server.go b/pkg/networkservice/common/updatepath/server.go index 0d1bcd5ec..45c679052 100644 --- a/pkg/networkservice/common/updatepath/server.go +++ b/pkg/networkservice/common/updatepath/server.go @@ -55,7 +55,6 @@ func (i *updatePathServer) Request(ctx context.Context, request *networkservice. return nil, err } - conn.Id = conn.Path.PathSegments[index].Id conn.Path.Index = index return conn, err diff --git a/pkg/networkservice/common/updatepath/server_test.go b/pkg/networkservice/common/updatepath/server_test.go new file mode 100644 index 000000000..5e750b1f7 --- /dev/null +++ b/pkg/networkservice/common/updatepath/server_test.go @@ -0,0 +1,141 @@ +// Copyright (c) 2020 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 updatepath_test + +import ( + "context" + "testing" + + "github.com/golang/protobuf/ptypes/empty" + "github.com/stretchr/testify/require" + "go.uber.org/goleak" + + "github.com/networkservicemesh/api/pkg/api/networkservice" + + "github.com/networkservicemesh/sdk/pkg/networkservice/common/updatepath" + "github.com/networkservicemesh/sdk/pkg/networkservice/core/next" +) + +func TestNewServer_SetNewConnectionId(t *testing.T) { + defer goleak.VerifyNone(t) + + server := updatepath.NewServer("nse-3") + + conn, err := server.Request(context.Background(), request(connectionID, 1)) + require.NoError(t, err) + require.NotNil(t, conn) + require.NotEqual(t, conn.Id, connectionID) +} + +func TestNewServer_SetNewPathId(t *testing.T) { + defer goleak.VerifyNone(t) + + server := updatepath.NewServer("nse-3") + + conn, err := server.Request(context.Background(), request(connectionID, 0)) + require.NoError(t, err) + require.NotNil(t, conn) + require.Equal(t, conn.Path.PathSegments[1].Name, "nse-3") // Check name is replaced. + require.Equal(t, conn.Id, conn.Path.PathSegments[1].Id) +} + +func TestNewServer_PathSegmentNameEqualClientName(t *testing.T) { + defer goleak.VerifyNone(t, goleak.IgnoreCurrent()) + + server := updatepath.NewServer("nse-2") + + conn, err := server.Request(context.Background(), request(connectionID, 1)) + require.NoError(t, err) + require.NotNil(t, conn) + require.NotEqual(t, conn.Id, connectionID) +} + +func TestNewServer_PathSegmentIdEqualConnectionId(t *testing.T) { + defer goleak.VerifyNone(t, goleak.IgnoreCurrent()) + + server := updatepath.NewServer("nse-3") + + conn, err := server.Request(context.Background(), request(pathSegmentID2, 1)) + require.NoError(t, err) + require.NotNil(t, conn) + require.NotEqual(t, conn.Id, pathSegmentID2) +} + +func TestNewServer_PathSegmentNameIDEqualClientNameID(t *testing.T) { + defer goleak.VerifyNone(t, goleak.IgnoreCurrent()) + + server := updatepath.NewServer("nse-2") + + conn, err := server.Request(context.Background(), request(pathSegmentID2, 1)) + require.NoError(t, err) + require.NotNil(t, conn) + require.Equal(t, conn.Id, pathSegmentID2) +} + +func TestNewServer_InvalidIndex(t *testing.T) { + defer goleak.VerifyNone(t, goleak.IgnoreCurrent()) + + server := updatepath.NewServer("nse-3") + + conn, err := server.Request(context.Background(), request(connectionID, 2)) + require.Error(t, err) + require.Nil(t, conn) +} + +func TestNewServer_RestoreIndex(t *testing.T) { + defer goleak.VerifyNone(t, goleak.IgnoreCurrent()) + + server := next.NewNetworkServiceServer( + updatepath.NewServer("nse-1"), + newPathIndexServer(t, 0), + updatepath.NewServer("nse-2"), + newPathIndexServer(t, 1), + updatepath.NewServer("nse-2"), + newPathIndexServer(t, 1), + updatepath.NewServer("nse-3"), + newPathIndexServer(t, 2), + ) + + _, err := server.Request(context.Background(), request(connectionID, 0)) + require.NoError(t, err) +} + +type pathIndexServer struct { + t *testing.T + index uint32 +} + +func newPathIndexServer(t *testing.T, index uint32) *pathIndexServer { + return &pathIndexServer{ + t: t, + index: index, + } +} + +func (s *pathIndexServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) { + require.Equal(s.t, s.index, request.Connection.Path.Index) + conn, err := next.Server(ctx).Request(ctx, request) + if err != nil { + return nil, err + } + require.Equal(s.t, s.index, conn.Path.Index) + return conn, nil +} + +func (s *pathIndexServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) { + return next.Server(ctx).Close(ctx, conn) +}