Skip to content

Commit

Permalink
Added tests for pkg/registry/core/next
Browse files Browse the repository at this point in the history
Signed-off-by: Sergey Semenov <sergey.semenov@xored.com>
  • Loading branch information
semenov-spirent committed Feb 18, 2020
1 parent c4d75cf commit 31c6aca
Show file tree
Hide file tree
Showing 3 changed files with 259 additions and 0 deletions.
66 changes: 66 additions & 0 deletions pkg/registry/core/next/tests/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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 tests

import (
"context"
"fmt"
"testing"

"github.com/networkservicemesh/api/pkg/api/registry"

"github.com/networkservicemesh/sdk/pkg/registry/core/adapters"
"github.com/networkservicemesh/sdk/pkg/registry/core/next"

"github.com/stretchr/testify/assert"
)

func TestNewNetworkServiceRegistryClientShouldNotPanic(t *testing.T) {
assert.NotPanics(t, func() {
_, _ = next.NewRegistryClient().RegisterNSE(context.Context(nil), nil)
})
}

func TestRegistryClientBranches(t *testing.T) {
samples := [][]registry.NetworkServiceRegistryClient{
{visitRegistryClient()},
{visitRegistryClient(), visitRegistryClient()},
{visitRegistryClient(), visitRegistryClient(), visitRegistryClient()},
{emptyRegistryClient(), visitRegistryClient(), visitRegistryClient()},
{visitRegistryClient(), emptyRegistryClient(), visitRegistryClient()},
{visitRegistryClient(), visitRegistryClient(), emptyRegistryClient()},
{adapters.NewRegistryServerToClient(visitRegistryServer())},
{adapters.NewRegistryServerToClient(visitRegistryServer()), visitRegistryClient()},
{visitRegistryClient(), adapters.NewRegistryServerToClient(visitRegistryServer()), visitRegistryClient()},
{visitRegistryClient(), visitRegistryClient(), adapters.NewRegistryServerToClient(visitRegistryServer())},
{visitRegistryClient(), adapters.NewRegistryServerToClient(emptyRegistryServer()), visitRegistryClient()},
{visitRegistryClient(), adapters.NewRegistryServerToClient(visitRegistryServer()), emptyRegistryClient()},
}
expects := []int{1, 2, 3, 0, 1, 2, 1, 2, 3, 3, 1, 2}
for i, sample := range samples {
msg := fmt.Sprintf("sample index: %v", i)
s := next.NewRegistryClient(sample...)

ctx := visit(context.Background())
_, _ = s.RegisterNSE(ctx, nil)
assert.Equal(t, expects[i], visitValue(ctx), msg)

ctx = visit(context.Background())
_, _ = s.RemoveNSE(ctx, nil)
assert.Equal(t, expects[i], visitValue(ctx), msg)
}
}
69 changes: 69 additions & 0 deletions pkg/registry/core/next/tests/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) 2020 Doc.ai and/or its affiliates.
//
// Copyright (c) 2020 Cisco Systems, Inc.
//
// 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 tests

import (
"context"
"fmt"

"github.com/stretchr/testify/assert"

"github.com/networkservicemesh/api/pkg/api/registry"

"github.com/networkservicemesh/sdk/pkg/registry/core/next"

"testing"

"github.com/networkservicemesh/sdk/pkg/registry/core/adapters"
)

func TestNewNetworkServiceServerShouldNotPanic(t *testing.T) {
assert.NotPanics(t, func() {
_, _ = next.NewRegistryServer().RegisterNSE(context.Context(nil), nil)
})
}

func TestServerBranches(t *testing.T) {
servers := [][]registry.NetworkServiceRegistryServer{
{visitRegistryServer()},
{visitRegistryServer(), visitRegistryServer()},
{visitRegistryServer(), visitRegistryServer(), visitRegistryServer()},
{emptyRegistryServer(), visitRegistryServer(), visitRegistryServer()},
{visitRegistryServer(), emptyRegistryServer(), visitRegistryServer()},
{visitRegistryServer(), visitRegistryServer(), emptyRegistryServer()},
{adapters.NewRegistryClientToServer(visitRegistryClient())},
{adapters.NewRegistryClientToServer(visitRegistryClient()), visitRegistryServer()},
{visitRegistryServer(), adapters.NewRegistryClientToServer(visitRegistryClient()), visitRegistryServer()},
{visitRegistryServer(), visitRegistryServer(), adapters.NewRegistryClientToServer(visitRegistryClient())},
{visitRegistryServer(), adapters.NewRegistryClientToServer(emptyRegistryClient()), visitRegistryServer()},
{visitRegistryServer(), adapters.NewRegistryClientToServer(visitRegistryClient()), emptyRegistryServer()},
}
expects := []int{1, 2, 3, 0, 1, 2, 1, 2, 3, 3, 1, 2}
for i, sample := range servers {
s := next.NewRegistryServer(sample...)

ctx := visit(context.Background())
_, _ = s.RegisterNSE(ctx, nil)
assert.Equal(t, expects[i], visitValue(ctx), fmt.Sprintf("sample index: %v", i))

ctx = visit(context.Background())
_, _ = s.RemoveNSE(ctx, nil)
assert.Equal(t, expects[i], visitValue(ctx), fmt.Sprintf("sample index: %v", i))
}
}
124 changes: 124 additions & 0 deletions pkg/registry/core/next/tests/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// 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 tests contains tests for package 'next'
package tests

import (
"context"

"github.com/networkservicemesh/api/pkg/api/registry"

"github.com/networkservicemesh/sdk/pkg/registry/core/next"

"github.com/golang/protobuf/ptypes/empty"

"google.golang.org/grpc"
)

type contextKeyType string

const (
visitKey contextKeyType = "visitKey"
)

func visit(ctx context.Context) context.Context {
if v, ok := ctx.Value(visitKey).(*int); ok {
*v++
return ctx
}
val := 0
return context.WithValue(ctx, visitKey, &val)
}

func visitValue(ctx context.Context) int {
if v, ok := ctx.Value(visitKey).(*int); ok {
return *v
}
return 0
}

type testVisitNetworkServiceRegistryServer struct{}

func visitRegistryServer() registry.NetworkServiceRegistryServer {
return &testVisitNetworkServiceRegistryServer{}
}

func (t *testVisitNetworkServiceRegistryServer) RegisterNSE(ctx context.Context, r *registry.NSERegistration) (*registry.NSERegistration, error) {
return next.RegistryServer(visit(ctx)).RegisterNSE(ctx, r)
}

func (t *testVisitNetworkServiceRegistryServer) BulkRegisterNSE(registry.NetworkServiceRegistry_BulkRegisterNSEServer) error {
return nil
}

func (t *testVisitNetworkServiceRegistryServer) RemoveNSE(ctx context.Context, r *registry.RemoveNSERequest) (*empty.Empty, error) {
return next.RegistryServer(visit(ctx)).RemoveNSE(ctx, r)
}

type testVisitNetworkServiceRegistryClient struct{}

func visitRegistryClient() registry.NetworkServiceRegistryClient {
return &testVisitNetworkServiceRegistryClient{}
}

func (t *testVisitNetworkServiceRegistryClient) RegisterNSE(ctx context.Context, r *registry.NSERegistration, _ ...grpc.CallOption) (*registry.NSERegistration, error) {
return next.RegistryClient(visit(ctx)).RegisterNSE(ctx, r)
}

func (t *testVisitNetworkServiceRegistryClient) BulkRegisterNSE(ctx context.Context, _ ...grpc.CallOption) (registry.NetworkServiceRegistry_BulkRegisterNSEClient, error) {
return next.RegistryClient(visit(ctx)).BulkRegisterNSE(ctx)
}

func (t *testVisitNetworkServiceRegistryClient) RemoveNSE(ctx context.Context, r *registry.RemoveNSERequest, _ ...grpc.CallOption) (*empty.Empty, error) {
return next.RegistryClient(visit(ctx)).RemoveNSE(ctx, r)
}

type testEmptyRegistryClient struct{}

func (t *testEmptyRegistryClient) RegisterNSE(context.Context, *registry.NSERegistration, ...grpc.CallOption) (*registry.NSERegistration, error) {
return nil, nil
}

func (t *testEmptyRegistryClient) BulkRegisterNSE(context.Context, ...grpc.CallOption) (registry.NetworkServiceRegistry_BulkRegisterNSEClient, error) {
return nil, nil
}

func (t *testEmptyRegistryClient) RemoveNSE(context.Context, *registry.RemoveNSERequest, ...grpc.CallOption) (*empty.Empty, error) {
return nil, nil
}

func emptyRegistryClient() registry.NetworkServiceRegistryClient {
return &testEmptyRegistryClient{}
}

type testEmptyRegistryServer struct{}

func (t *testEmptyRegistryServer) RegisterNSE(context.Context, *registry.NSERegistration) (*registry.NSERegistration, error) {
return nil, nil
}

func (t *testEmptyRegistryServer) BulkRegisterNSE(registry.NetworkServiceRegistry_BulkRegisterNSEServer) error {
return nil
}

func (t *testEmptyRegistryServer) RemoveNSE(ctx context.Context, r *registry.RemoveNSERequest) (*empty.Empty, error) {
return nil, nil
}

func emptyRegistryServer() registry.NetworkServiceRegistryServer {
return &testEmptyRegistryServer{}
}

0 comments on commit 31c6aca

Please sign in to comment.