forked from networkservicemesh/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Artem Belov <artem.belov@xored.com>
- Loading branch information
Artem Belov
committed
Mar 9, 2021
1 parent
009fa1e
commit 063706d
Showing
6 changed files
with
212 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) 2020-2021 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 localbypass implements a chain element to set NSMgr URL to endpoints on registration and set back endpoints | ||
// URLs on find | ||
package localbypass | ||
|
||
import ( | ||
"context" | ||
"github.com/golang/protobuf/ptypes/empty" | ||
"github.com/networkservicemesh/api/pkg/api/registry" | ||
"google.golang.org/grpc" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/registry/core/next" | ||
"github.com/networkservicemesh/sdk/pkg/tools/stringurl" | ||
) | ||
|
||
type localBypassNSEClient struct { | ||
url string | ||
nseURLs stringurl.Map | ||
} | ||
|
||
// NewNetworkServiceEndpointRegistryClient creates new instance of NetworkServiceEndpointRegistryClient which | ||
// checks endpoints URLs on find | ||
func NewNetworkServiceEndpointRegistryClient(u string) registry.NetworkServiceEndpointRegistryClient { | ||
return &localBypassNSEClient{ | ||
url: u, | ||
} | ||
} | ||
|
||
func (rc *localBypassNSEClient) Register(ctx context.Context, in *registry.NetworkServiceEndpoint, opts ...grpc.CallOption) (*registry.NetworkServiceEndpoint, error) { | ||
return next.NetworkServiceEndpointRegistryClient(ctx).Register(ctx, in, opts...) | ||
} | ||
|
||
func (rc *localBypassNSEClient) Find(ctx context.Context, in *registry.NetworkServiceEndpointQuery, opts ...grpc.CallOption) (registry.NetworkServiceEndpointRegistry_FindClient, error) { | ||
client, err := next.NetworkServiceEndpointRegistryClient(ctx).Find(ctx, in, opts...) | ||
if err != nil { | ||
return client, err | ||
} | ||
return &localBypassNSEFindClient{ | ||
url: rc.url, | ||
NetworkServiceEndpointRegistry_FindClient: client, | ||
}, nil | ||
} | ||
|
||
func (rc *localBypassNSEClient) Unregister(ctx context.Context, in *registry.NetworkServiceEndpoint, opts ...grpc.CallOption) (*empty.Empty, error) { | ||
return next.NetworkServiceEndpointRegistryClient(ctx).Unregister(ctx, in, opts...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright (c) 2021 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 localbypass_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.uber.org/goleak" | ||
|
||
"github.com/networkservicemesh/api/pkg/api/registry" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/registry/common/localbypass" | ||
"github.com/networkservicemesh/sdk/pkg/registry/common/memory" | ||
"github.com/networkservicemesh/sdk/pkg/registry/core/adapters" | ||
"github.com/networkservicemesh/sdk/pkg/registry/core/next" | ||
) | ||
|
||
func TestLocalBypassNSEClient_NSEUrl(t *testing.T) { | ||
defer goleak.VerifyNone(t, goleak.IgnoreCurrent()) | ||
|
||
server := next.NewNetworkServiceEndpointRegistryClient( | ||
localbypass.NewNetworkServiceEndpointRegistryClient(nsmgrURL), | ||
adapters.NetworkServiceEndpointServerToClient(next.NewNetworkServiceEndpointRegistryServer( | ||
memory.NewNetworkServiceEndpointRegistryServer(), | ||
)), | ||
) | ||
|
||
// 1. Register | ||
nse, err := server.Register(context.Background(), ®istry.NetworkServiceEndpoint{ | ||
Name: "nse-1", | ||
Url: nseURL, | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, nseURL, nse.Url) | ||
|
||
// 2. Find | ||
stream, err := server.Find(context.Background(), ®istry.NetworkServiceEndpointQuery{ | ||
NetworkServiceEndpoint: new(registry.NetworkServiceEndpoint), | ||
}) | ||
require.NoError(t, err) | ||
|
||
findNSE, err := stream.Recv() | ||
require.NoError(t, err) | ||
require.Equal(t, nseURL, findNSE.Url) | ||
} | ||
|
||
func TestLocalBypassNSEClient_NSMgrUrl(t *testing.T) { | ||
defer goleak.VerifyNone(t, goleak.IgnoreCurrent()) | ||
|
||
server := next.NewNetworkServiceEndpointRegistryClient( | ||
localbypass.NewNetworkServiceEndpointRegistryClient(nsmgrURL), | ||
adapters.NetworkServiceEndpointServerToClient(next.NewNetworkServiceEndpointRegistryServer( | ||
memory.NewNetworkServiceEndpointRegistryServer(), | ||
)), | ||
) | ||
|
||
// 1. Register | ||
nse, err := server.Register(context.Background(), ®istry.NetworkServiceEndpoint{ | ||
Name: "nse-1", | ||
Url: nsmgrURL, | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, nsmgrURL, nse.Url) | ||
|
||
// 2. Find | ||
stream, err := server.Find(context.Background(), ®istry.NetworkServiceEndpointQuery{ | ||
NetworkServiceEndpoint: new(registry.NetworkServiceEndpoint), | ||
}) | ||
require.NoError(t, err) | ||
|
||
_, err = stream.Recv() | ||
require.Error(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) 2021 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 localbypass_test | ||
|
||
const ( | ||
nsmgrURL = "tcp://0.0.0.0" | ||
nseURL = "tcp://1.1.1.1" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (c) 2020-2021 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 localbypass | ||
|
||
import ( | ||
"errors" | ||
"github.com/networkservicemesh/api/pkg/api/registry" | ||
) | ||
|
||
type localBypassNSEFindClient struct { | ||
url string | ||
registry.NetworkServiceEndpointRegistry_FindClient | ||
} | ||
|
||
func (s *localBypassNSEFindClient) Recv() (*registry.NetworkServiceEndpoint, error) { | ||
nse, err := s.NetworkServiceEndpointRegistry_FindClient.Recv() | ||
if err != nil { | ||
return nse, err | ||
} | ||
if nse.Url == s.url { | ||
return nil, errors.New("NSMgr found unregistered endpoint") | ||
} | ||
return nse, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters