Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checkopts NetworkServiceClient chain element #139

Merged
merged 1 commit into from
Feb 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions pkg/networkservice/utils/checks/checkopts/check_propogate_opts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2020 Cisco 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 checkopts - provides tools for checking to make sure that grpc.CallOptions are propagated through a client chain elements
package checkopts

import (
"testing"

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

"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/inject/injectopts"
)

// CheckPropogateOptsClient - returns NetworkServiceClient that checks to make sure the clientUnderTest is passing through the grpc.CallOptions
func CheckPropogateOptsClient(t *testing.T, clientUnderTest networkservice.NetworkServiceClient) networkservice.NetworkServiceClient {
return chain.NewNetworkServiceClient(
injectopts.NewClient(),
clientUnderTest,
NewClient(t),
)
}
64 changes: 64 additions & 0 deletions pkg/networkservice/utils/checks/checkopts/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) 2020 Cisco 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 checkopts

import (
"context"
"testing"

"github.com/golang/protobuf/ptypes/empty"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"

"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
)

type checkOptsClient struct {
*testing.T
opts []grpc.CallOption
}

// NewClient - returns a NetworkServiceClient that checks for the specified opts... being passed into it
// t - *testing.T for checks
// opts... grpc.CallOptions expected at the end of the list of opts... passed to Request/Close
func NewClient(t *testing.T, opts ...grpc.CallOption) networkservice.NetworkServiceClient {
if len(opts) == 0 {
opts = append(opts, grpc.EmptyCallOption{})
}
return &checkOptsClient{
T: t,
opts: opts,
}
}

func (c *checkOptsClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) {
require.GreaterOrEqual(c.T, len(opts), len(c.opts))
for i, opt := range c.opts {
assert.Equal(c.T, opt, opts[i+len(c.opts)-len(opts)])
}
return next.Client(ctx).Request(ctx, request, opts...)
}

func (c *checkOptsClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
require.GreaterOrEqual(c.T, len(opts), len(c.opts))
for i, opt := range c.opts {
assert.Equal(c.T, opt, opts[i+len(c.opts)-len(opts)])
}
return next.Client(ctx).Close(ctx, conn, opts...)
}