Skip to content

Commit

Permalink
[sdk#740] Rework sandbox (#811)
Browse files Browse the repository at this point in the history
* Rework sandbox

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Remove default NS, NSE registry clients for the node

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Add node.URL()

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Fix sandbox new* methods

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
  • Loading branch information
Vladimir Popov authored Jun 23, 2021
1 parent ff6a8e3 commit 6e69831
Show file tree
Hide file tree
Showing 20 changed files with 861 additions and 794 deletions.
317 changes: 171 additions & 146 deletions pkg/networkservice/chains/nsmgr/heal_test.go

Large diffs are not rendered by default.

15 changes: 6 additions & 9 deletions pkg/networkservice/chains/nsmgr/scale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ func TestCreateEndpointDuringRequest(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)

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

nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.GenerateTestToken)

nsReg := &registry.NetworkService{
Name: "ns-1",
Matches: []*registry.Match{
Expand All @@ -59,7 +60,7 @@ func TestCreateEndpointDuringRequest(t *testing.T) {
},
}

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

nseReg := &registry.NetworkServiceEndpoint{
Expand Down Expand Up @@ -94,8 +95,7 @@ func TestCreateEndpointDuringRequest(t *testing.T) {
},
}

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

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

Expand Down Expand Up @@ -139,10 +139,7 @@ func (m *nseMaker) Request(_ context.Context, _ *networkservice.NetworkServiceRe
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
}
m.domain.Nodes[1].NewEndpoint(m.ctx, m.nseReg, sandbox.GenerateTestToken, endpoint)

return nil, errors.New("can't provide requested network service")
}
Expand Down
103 changes: 46 additions & 57 deletions pkg/networkservice/chains/nsmgr/single_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/goleak"

"github.com/networkservicemesh/sdk/pkg/networkservice/chains/nsmgr"
"github.com/networkservicemesh/sdk/pkg/networkservice/connectioncontext/dnscontext"
"github.com/networkservicemesh/sdk/pkg/tools/clientinfo"
"github.com/networkservicemesh/sdk/pkg/tools/sandbox"
Expand All @@ -42,19 +43,20 @@ func Test_DNSUsecase(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

domain := sandbox.NewBuilder(t).
domain := sandbox.NewBuilder(ctx, t).
SetNodesCount(1).
SetNSMgrProxySupplier(nil).
SetRegistryProxySupplier(nil).
SetContext(ctx).
Build()

nsReg, err := domain.Nodes[0].NSRegistryClient.Register(ctx, defaultRegistryService())
nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.GenerateTestToken)

nsReg, err := nsRegistryClient.Register(ctx, defaultRegistryService())
require.NoError(t, err)

nseReg := defaultRegistryEndpoint(nsReg.Name)

_, err = domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, dnscontext.NewServer(
nse := domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, dnscontext.NewServer(
&networkservice.DNSConfig{
DnsServerIps: []string{"8.8.8.8"},
SearchDomains: []string{"my.domain1"},
Expand All @@ -64,7 +66,6 @@ func Test_DNSUsecase(t *testing.T) {
SearchDomains: []string{"my.domain1"},
},
))
require.NoError(t, err)

corefilePath := filepath.Join(t.TempDir(), "corefile")
resolveConfigPath := filepath.Join(t.TempDir(), "resolv.conf")
Expand Down Expand Up @@ -95,7 +96,7 @@ func Test_DNSUsecase(t *testing.T) {
_, err = nsc.Close(ctx, conn)
require.NoError(t, err)

_, err = domain.Nodes[0].EndpointRegistryClient.Unregister(ctx, nseReg)
_, err = nse.Unregister(ctx, nseReg)
require.NoError(t, err)
}

Expand All @@ -105,55 +106,44 @@ func Test_ShouldCorrectlyAddForwardersWithSameNames(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

domain := sandbox.NewBuilder(t).
domain := sandbox.NewBuilder(ctx, t).
SetNodesCount(1).
SetRegistryProxySupplier(nil).
SetNodeSetup(nil).
SetNodeSetup(func(ctx context.Context, node *sandbox.Node, _ int) {
node.NewNSMgr(ctx, "nsmgr", nil, sandbox.GenerateTestToken, nsmgr.NewServer)
}).
SetRegistryExpiryDuration(sandbox.RegistryExpiryDuration).
SetContext(ctx).
Build()

nsReg, err := domain.Nodes[0].NSRegistryClient.Register(ctx, defaultRegistryService())
nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.GenerateTestToken)

nsReg, err := nsRegistryClient.Register(ctx, defaultRegistryService())
require.NoError(t, err)

forwarderReg := &registry.NetworkServiceEndpoint{
Name: "forwarder",
}

// 1. Add forwarders
forwarder1Reg := forwarderReg.Clone()
_, err = domain.Nodes[0].NewForwarder(ctx, forwarder1Reg, sandbox.GenerateTestToken)
require.NoError(t, err)

forwarder2Reg := forwarderReg.Clone()
_, err = domain.Nodes[0].NewForwarder(ctx, forwarder2Reg, sandbox.GenerateTestToken)
require.NoError(t, err)

forwarder3Reg := forwarderReg.Clone()
_, err = domain.Nodes[0].NewForwarder(ctx, forwarder3Reg, sandbox.GenerateTestToken)
require.NoError(t, err)
var forwarderRegs [3]*registry.NetworkServiceEndpoint
var forwarders [3]*sandbox.EndpointEntry
for i := range forwarderRegs {
forwarderRegs[i] = forwarderReg.Clone()
forwarders[i] = domain.Nodes[0].NewForwarder(ctx, forwarderRegs[i], sandbox.GenerateTestToken)
}

// 2. Wait for refresh
<-time.After(sandbox.RegistryExpiryDuration)

nseReg := defaultRegistryEndpoint(nsReg.Name)

testNSEAndClient(ctx, t, domain, nseReg.Clone())

// 3. Delete first forwarder
_, err = domain.Nodes[0].ForwarderRegistryClient.Unregister(ctx, forwarder1Reg)
require.NoError(t, err)

testNSEAndClient(ctx, t, domain, nseReg.Clone())

// 4. Delete last forwarder
_, err = domain.Nodes[0].ForwarderRegistryClient.Unregister(ctx, forwarder3Reg)
require.NoError(t, err)

testNSEAndClient(ctx, t, domain, nseReg.Clone())
// 3. Delete first forwarder, last forwarder, middle forwarder
for _, i := range []int{0, 2, 1} {
testNSEAndClient(ctx, t, domain, nseReg.Clone())

_, err = domain.Nodes[0].ForwarderRegistryClient.Unregister(ctx, forwarder2Reg)
require.NoError(t, err)
_, err = forwarders[i].Unregister(ctx, forwarderRegs[i])
require.NoError(t, err)
}
}

func Test_ShouldCorrectlyAddEndpointsWithSameNames(t *testing.T) {
Expand All @@ -162,26 +152,25 @@ func Test_ShouldCorrectlyAddEndpointsWithSameNames(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

domain := sandbox.NewBuilder(t).
domain := sandbox.NewBuilder(ctx, t).
SetNodesCount(1).
SetRegistryProxySupplier(nil).
SetRegistryExpiryDuration(sandbox.RegistryExpiryDuration).
SetContext(ctx).
Build()

nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.GenerateTestToken)

// 1. Add endpoints
nseRegs := make([]*registry.NetworkServiceEndpoint, 2)
var nseRegs [2]*registry.NetworkServiceEndpoint
var nses [2]*sandbox.EndpointEntry
for i := range nseRegs {
nsReg, err := domain.Nodes[0].NSRegistryClient.Register(ctx, defaultRegistryService())
nsReg, err := nsRegistryClient.Register(ctx, defaultRegistryService())
require.NoError(t, err)

nseRegs[i] = defaultRegistryEndpoint(nsReg.Name)
nseRegs[i].NetworkServiceNames[0] = nsReg.Name

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

nseRegs = append(nseRegs, nseRegs[i])
nses[i] = domain.Nodes[0].NewEndpoint(ctx, nseRegs[i], sandbox.GenerateTestToken)
}

// 2. Wait for refresh
Expand All @@ -196,8 +185,8 @@ func Test_ShouldCorrectlyAddEndpointsWithSameNames(t *testing.T) {
}

// 3. Delete endpoints
for _, nseReg := range nseRegs {
_, err := domain.Nodes[0].EndpointRegistryClient.Unregister(ctx, nseReg)
for i, nseReg := range nseRegs {
_, err := nses[i].Unregister(ctx, nseReg)
require.NoError(t, err)
}
}
Expand All @@ -212,24 +201,24 @@ func Test_Local_NoURLUsecase(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

domain := sandbox.NewBuilder(t).
domain := sandbox.NewBuilder(ctx, t).
SetNodesCount(1).
UseUnixSockets().
SetContext(ctx).
SetNSMgrProxySupplier(nil).
SetRegistryProxySupplier(nil).
SetRegistrySupplier(nil).
Build()

nsReg, err := domain.Nodes[0].NSRegistryClient.Register(ctx, defaultRegistryService())
nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.GenerateTestToken)

nsReg, err := nsRegistryClient.Register(ctx, defaultRegistryService())
require.NoError(t, err)

nseReg := defaultRegistryEndpoint(nsReg.Name)
request := defaultRequest(nsReg.Name)
counter := &counterServer{}

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

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

Expand Down Expand Up @@ -274,13 +263,14 @@ func Test_ShouldParseNetworkServiceLabelsTemplate(t *testing.T) {
want := map[string]string{}
clientinfo.AddClientInfo(ctx, want)

domain := sandbox.NewBuilder(t).
domain := sandbox.NewBuilder(ctx, t).
SetNodesCount(1).
SetRegistryProxySupplier(nil).
SetNSMgrProxySupplier(nil).
SetContext(ctx).
Build()

nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.GenerateTestToken)

nsReg := defaultRegistryService()
nsReg.Matches = []*registry.Match{
{
Expand All @@ -294,14 +284,13 @@ func Test_ShouldParseNetworkServiceLabelsTemplate(t *testing.T) {
},
}

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

nseReg := defaultRegistryEndpoint(nsReg.Name)
nseReg.NetworkServiceLabels = map[string]*registry.NetworkServiceLabels{nsReg.Name: {}}

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

nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken)
require.NoError(t, err)
Expand All @@ -316,6 +305,6 @@ func Test_ShouldParseNetworkServiceLabelsTemplate(t *testing.T) {
// Test for endpoint labels setting
require.Equal(t, want, nseReg.NetworkServiceLabels[nsReg.Name].Labels)

_, err = domain.Nodes[0].EndpointRegistryClient.Unregister(ctx, nseReg)
_, err = nse.Unregister(ctx, nseReg)
require.NoError(t, err)
}
Loading

0 comments on commit 6e69831

Please sign in to comment.