Skip to content

Commit

Permalink
Server tests refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Sergey Semenov <[email protected]>
  • Loading branch information
semenov-spirent committed Feb 26, 2020
1 parent d431623 commit f83d0ba
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 90 deletions.
162 changes: 73 additions & 89 deletions pkg/networkservice/common/updatepath/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,81 +24,56 @@ 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",
Id: "conn-0",
}, {
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",
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion pkg/networkservice/common/updatepath/updatepath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f83d0ba

Please sign in to comment.