From f83d0baafd5368df0d997dba7d77af29be5210af Mon Sep 17 00:00:00 2001 From: Sergey Semenov Date: Wed, 26 Feb 2020 12:48:42 +0700 Subject: [PATCH] Server tests refactoring Signed-off-by: Sergey Semenov --- .../common/updatepath/server_test.go | 162 ++++++++---------- .../common/updatepath/updatepath_test.go | 2 +- 2 files changed, 74 insertions(+), 90 deletions(-) diff --git a/pkg/networkservice/common/updatepath/server_test.go b/pkg/networkservice/common/updatepath/server_test.go index 14c2662c68..1d9654a87c 100644 --- a/pkg/networkservice/common/updatepath/server_test.go +++ b/pkg/networkservice/common/updatepath/server_test.go @@ -24,39 +24,14 @@ import ( "github.com/stretchr/testify/assert" "github.com/networkservicemesh/sdk/pkg/networkservice/common/updatepath" - "github.com/networkservicemesh/sdk/pkg/networkservice/core/next" ) -var serverTestData = []struct { - name string - nscName string - request *networkservice.NetworkServiceRequest - want *networkservice.Connection -}{ - { - "add new segment when index was in the last position", - "nsc-2", - &networkservice.NetworkServiceRequest{ - Connection: &networkservice.Connection{ - Id: "conn-2", - Path: &networkservice.Path{ - Index: 1, - PathSegments: []*networkservice.PathSegment{ - { - Name: "nsc-0", - Id: "conn-0", - }, { - Name: "nsc-1", - Id: "conn-1", - }, - }, - }, - }, - }, - &networkservice.Connection{ +func TestNewServer_IndexInLastPositionAddNewSegment(t *testing.T) { + request := &networkservice.NetworkServiceRequest{ + Connection: &networkservice.Connection{ Id: "conn-2", Path: &networkservice.Path{ - Index: 2, + Index: 1, PathSegments: []*networkservice.PathSegment{ { Name: "nsc-0", @@ -64,41 +39,41 @@ var serverTestData = []struct { }, { Name: "nsc-1", Id: "conn-1", - }, { - Name: "nsc-2", - Id: "conn-2", }, }, }, }, - }, - { - "override values when index is valid in path array", - "nsc-2", - &networkservice.NetworkServiceRequest{ - Connection: &networkservice.Connection{ - Id: "conn-2", - Path: &networkservice.Path{ - Index: 1, - PathSegments: []*networkservice.PathSegment{ - { - Name: "nsc-0", - Id: "conn-0", - }, { - Name: "nsc-1", - Id: "conn-1", - }, { - Name: "nsc-will-be-overridden", - Id: "conn-will-be-overridden", - }, - }, + } + expected := &networkservice.Connection{ + Id: "conn-2", + Path: &networkservice.Path{ + Index: 2, + PathSegments: []*networkservice.PathSegment{ + { + Name: "nsc-0", + Id: "conn-0", + }, { + Name: "nsc-1", + Id: "conn-1", + }, { + Name: "nsc-2", + Id: "conn-2", }, }, }, - &networkservice.Connection{ + } + server := updatepath.NewServer("nsc-2") + conn, err := server.Request(context.Background(), request) + assert.Nil(t, err) + assert.Equal(t, expected, conn) +} + +func TestNewServer_ValidIndexOverwriteValues(t *testing.T) { + request := &networkservice.NetworkServiceRequest{ + Connection: &networkservice.Connection{ Id: "conn-2", Path: &networkservice.Path{ - Index: 2, + Index: 1, PathSegments: []*networkservice.PathSegment{ { Name: "nsc-0", @@ -107,48 +82,57 @@ var serverTestData = []struct { Name: "nsc-1", Id: "conn-1", }, { - Name: "nsc-2", - Id: "conn-2", + Name: "nsc-will-be-overwritten", + Id: "conn-will-be-overwritten", }, }, }, }, - }, - { - "index is greater or equal to path length", - "nsc-1", - &networkservice.NetworkServiceRequest{ - Connection: &networkservice.Connection{ - Id: "conn-1", - Path: &networkservice.Path{ - Index: 2, - PathSegments: []*networkservice.PathSegment{ - { - Name: "nsc-0", - Id: "conn-0", - }, { - Name: "nsc-1", - Id: "conn-1", - }, - }, + } + expected := &networkservice.Connection{ + Id: "conn-2", + Path: &networkservice.Path{ + Index: 2, + PathSegments: []*networkservice.PathSegment{ + { + Name: "nsc-0", + Id: "conn-0", + }, { + Name: "nsc-1", + Id: "conn-1", + }, { + Name: "nsc-2", + Id: "conn-2", }, }, }, - nil, - }, -} - -func Test_updatePathServer_Request(t *testing.T) { - for _, data := range serverTestData { - test := data - t.Run(test.name, func(t *testing.T) { - testServerRequest(t, test.nscName, test.request, test.want) - }) } + server := updatepath.NewServer("nsc-2") + conn, err := server.Request(context.Background(), request) + assert.Nil(t, err) + assert.Equal(t, expected, conn) } -func testServerRequest(t *testing.T, nscName string, request *networkservice.NetworkServiceRequest, want *networkservice.Connection) { - client := next.NewNetworkServiceServer(updatepath.NewServer(nscName)) - got, _ := client.Request(context.Background(), request) - assert.Equal(t, want, got) +func TestNewServer_IndexGreaterThanArrayLength(t *testing.T) { + request := &networkservice.NetworkServiceRequest{ + Connection: &networkservice.Connection{ + Id: "conn-1", + Path: &networkservice.Path{ + Index: 2, + PathSegments: []*networkservice.PathSegment{ + { + Name: "nsc-0", + Id: "conn-0", + }, { + Name: "nsc-1", + Id: "conn-1", + }, + }, + }, + }, + } + server := updatepath.NewServer("nsc-2") + conn, err := server.Request(context.Background(), request) + assert.NotNil(t, err) + assert.Nil(t, conn) } diff --git a/pkg/networkservice/common/updatepath/updatepath_test.go b/pkg/networkservice/common/updatepath/updatepath_test.go index b578e464c2..4f9dfdce5b 100644 --- a/pkg/networkservice/common/updatepath/updatepath_test.go +++ b/pkg/networkservice/common/updatepath/updatepath_test.go @@ -28,7 +28,7 @@ import ( "github.com/networkservicemesh/sdk/pkg/networkservice/core/next" ) -func Test_updatePath_chained(t *testing.T) { +func TestChain(t *testing.T) { request := &networkservice.NetworkServiceRequest{ Connection: &networkservice.Connection{ Id: "conn-2",