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 test checking registering endpoint for alt match during request #919

Merged
merged 3 commits into from
May 19, 2021
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
152 changes: 152 additions & 0 deletions pkg/networkservice/chains/nsmgr/scale_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// 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 nsmgr_test

import (
"context"
"testing"
"time"

"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
"go.uber.org/goleak"

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

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

func TestCreateEndpointDuringRequest(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })

ctx, cancel := context.WithTimeout(context.Background(), time.Second)

defer cancel()
domain := sandbox.NewBuilder(t).
SetNodesCount(2).
SetRegistryProxySupplier(nil).
SetContext(ctx).
Build()

nsReg := &registry.NetworkService{
Name: "ns-1",
Matches: []*registry.Match{
{
SourceSelector: map[string]string{},
Routes: []*registry.Destination{
{DestinationSelector: map[string]string{"nodeName": "node[1]"}},
{DestinationSelector: map[string]string{"create-nse": "true"}},
},
},
},
}

_, err := domain.Nodes[0].NSRegistryClient.Register(ctx, nsReg)
require.NoError(t, err)

nseReg := &registry.NetworkServiceEndpoint{
Name: "nse-maker",
NetworkServiceNames: []string{nsReg.Name},
NetworkServiceLabels: map[string]*registry.NetworkServiceLabels{
nsReg.Name: {Labels: map[string]string{"create-nse": "true"}},
},
}

flag := atomic.Bool{}
requestCounter := &counterServer{}

makerServer := &nseMaker{
ctx: ctx,
domain: domain,
nseReg: &registry.NetworkServiceEndpoint{
Name: "nse-impl",
NetworkServiceNames: []string{nsReg.Name},
NetworkServiceLabels: map[string]*registry.NetworkServiceLabels{
nsReg.Name: {Labels: map[string]string{"nodeName": "node[1]"}},
},
},
serverFactory: func() networkservice.NetworkServiceServer {
// we can't use require false here
// because there will be several calls, and it's expected behavior
if flag.Swap(true) {
return nil
}

return requestCounter
},
}

_, err = domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, makerServer)
require.NoError(t, err)

nsc := domain.Nodes[1].NewClient(ctx, sandbox.GenerateTestToken)

// check first request
conn, err := nsc.Request(ctx, &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
NetworkService: nsReg.Name,
},
})
require.NoError(t, err)
require.True(t, flag.Load())
require.Equal(t, int32(1), requestCounter.Requests)

// check refresh
_, err = nsc.Request(ctx, &networkservice.NetworkServiceRequest{
Connection: conn,
})
require.NoError(t, err)
require.Equal(t, int32(2), requestCounter.Requests)

// check new request
_, err = nsc.Request(ctx, &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
NetworkService: nsReg.Name,
},
})
require.NoError(t, err)
require.Equal(t, int32(3), requestCounter.Requests)
}

type nseMaker struct {
ctx context.Context
domain *sandbox.Domain
nseReg *registry.NetworkServiceEndpoint
serverFactory func() networkservice.NetworkServiceServer
}

func (m *nseMaker) Request(_ context.Context, _ *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
endpoint := m.serverFactory()
if endpoint == nil {
return nil, errors.New("can't create new endpoint")
}

_, err := m.domain.Nodes[1].NewEndpoint(m.ctx, m.nseReg, sandbox.GenerateTestToken, endpoint)
if err != nil {
return nil, err
}

return nil, errors.New("can't provide requested network service")
}

func (m *nseMaker) Close(ctx context.Context, connection *networkservice.Connection) (*empty.Empty, error) {
return next.Server(ctx).Close(ctx, connection)
}
25 changes: 20 additions & 5 deletions pkg/networkservice/common/discover/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ func testServers(
func TestMatchEmptySourceSelector(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })

ctx, cancel := context.WithTimeout(context.Background(), testWait)
defer cancel()

nsName := networkServiceName()

nsServer, nseServer := testServers(t, nsName, endpoints(), fromFirewallMatch(), fromSomeMiddleAppMatch(), fromAnywhereMatch())
Expand All @@ -191,13 +194,16 @@ func TestMatchEmptySourceSelector(t *testing.T) {
}),
)

_, err := server.Request(context.Background(), request)
_, err := server.Request(ctx, request)
require.NoError(t, err)
}

func TestMatchNonEmptySourceSelector(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })

ctx, cancel := context.WithTimeout(context.Background(), testWait)
defer cancel()

nsName := networkServiceName()

nsServer, nseServer := testServers(t, nsName, endpoints(), fromFirewallMatch(), fromSomeMiddleAppMatch(), fromAnywhereMatch())
Expand Down Expand Up @@ -226,13 +232,16 @@ func TestMatchNonEmptySourceSelector(t *testing.T) {
}),
)

_, err := server.Request(context.Background(), request)
_, err := server.Request(ctx, request)
require.NoError(t, err)
}

func TestMatchEmptySourceSelectorGoingFirst(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })

ctx, cancel := context.WithTimeout(context.Background(), testWait)
defer cancel()

nsName := networkServiceName()

nsServer, nseServer := testServers(t, nsName, endpoints(), fromAnywhereMatch(), fromFirewallMatch(), fromSomeMiddleAppMatch())
Expand Down Expand Up @@ -261,13 +270,16 @@ func TestMatchEmptySourceSelectorGoingFirst(t *testing.T) {
}),
)

_, err := server.Request(context.Background(), request)
_, err := server.Request(ctx, request)
require.NoError(t, err)
}

func TestMatchNothing(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })

ctx, cancel := context.WithTimeout(context.Background(), testWait)
defer cancel()

nsName := networkServiceName()

nsServer, nseServer := testServers(t, nsName, endpoints(), fromFirewallMatch(), fromSomeMiddleAppMatch())
Expand All @@ -291,13 +303,16 @@ func TestMatchNothing(t *testing.T) {
}),
)

_, err := server.Request(context.Background(), request)
_, err := server.Request(ctx, request)
require.NoError(t, err)
}

func TestMatchSelectedNSE(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })

ctx, cancel := context.WithTimeout(context.Background(), testWait)
defer cancel()

nsName := networkServiceName()
nses := endpoints()

Expand All @@ -318,7 +333,7 @@ func TestMatchSelectedNSE(t *testing.T) {
}),
)

_, err := server.Request(context.Background(), request)
_, err := server.Request(ctx, request)
require.NoError(t, err)
}

Expand Down