Skip to content

Commit

Permalink
Make updatepath restore path index on return (networkservicemesh#602)
Browse files Browse the repository at this point in the history
* Make updatepath restore path index on return

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

* Remove update path segment from updatetoken

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
Signed-off-by: Sergey Ershov <sergey.ershov@xored.com>
  • Loading branch information
Vladimir Popov authored and Sergey Ershov committed Dec 23, 2020
1 parent 0be11a6 commit a50484a
Show file tree
Hide file tree
Showing 6 changed files with 351 additions and 25 deletions.
21 changes: 0 additions & 21 deletions pkg/networkservice/common/interpose/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -81,7 +77,6 @@ func TestInterposeServer(t *testing.T) {
}),
)),
adapters.NewServerToClient(next.NewNetworkServiceServer(
new(restorePathServer),
updatepath.NewServer("endpoint"),
touchServer,
)),
Expand Down Expand Up @@ -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
}
Expand Down
1 change: 0 additions & 1 deletion pkg/networkservice/common/updatepath/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
208 changes: 208 additions & 0 deletions pkg/networkservice/common/updatepath/client_test.go
Original file line number Diff line number Diff line change
@@ -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...)
}
4 changes: 2 additions & 2 deletions pkg/networkservice/common/updatepath/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}

Expand Down Expand Up @@ -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
}
1 change: 0 additions & 1 deletion pkg/networkservice/common/updatepath/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit a50484a

Please sign in to comment.