Skip to content

Commit

Permalink
Rework sandbox to clock
Browse files Browse the repository at this point in the history
Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
  • Loading branch information
Vladimir Popov committed Mar 29, 2021
1 parent 0abef45 commit cc1ae41
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 90 deletions.
42 changes: 21 additions & 21 deletions pkg/networkservice/chains/nsmgr/server_heal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func testNSMGRHealEndpoint(t *testing.T, restored bool) {
nseCtx, nseCtxCancel := context.WithTimeout(context.Background(), time.Second)
defer nseCtxCancel()

nse, err := domain.Nodes[0].NewEndpoint(nseCtx, nseReg, sandbox.GenerateExpiringToken(time.Second), counter)
nse, err := domain.Nodes[0].NewEndpoint(nseCtx, nseReg, sandbox.GenerateExpiringToken(ctx, time.Second), counter)
require.NoError(t, err)

request := &networkservice.NetworkServiceRequest{
Expand All @@ -86,7 +86,7 @@ func testNSMGRHealEndpoint(t *testing.T, restored bool) {
},
}

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

conn, err := nsc.Request(ctx, request.Clone())
require.NoError(t, err)
Expand All @@ -106,7 +106,7 @@ func testNSMGRHealEndpoint(t *testing.T, restored bool) {
if restored {
nseReg2.Url = nse.URL.String()
}
_, err = domain.Nodes[0].NewEndpoint(ctx, nseReg2, sandbox.GenerateTestToken, counter)
_, err = domain.Nodes[0].NewEndpoint(ctx, nseReg2, sandbox.GenerateTestToken(ctx), counter)
require.NoError(t, err)

// Wait NSE expired and reconnecting to the new NSE
Expand Down Expand Up @@ -134,7 +134,7 @@ func TestNSMGR_HealLocalForwarder(t *testing.T) {
nil,
{
ForwarderCtx: forwarderCtx,
ForwarderGenerateTokenFunc: sandbox.GenerateExpiringToken(time.Second),
ForwarderGenerateTokenFunc: sandbox.GenerateExpiringToken(forwarderCtx, time.Second),
},
}

Expand All @@ -149,7 +149,7 @@ func TestNSMGR_HealLocalForwarderRestored(t *testing.T) {
nil,
{
ForwarderCtx: forwarderCtx,
ForwarderGenerateTokenFunc: sandbox.GenerateExpiringToken(time.Second),
ForwarderGenerateTokenFunc: sandbox.GenerateExpiringToken(forwarderCtx, time.Second),
},
}

Expand All @@ -163,7 +163,7 @@ func TestNSMGR_HealRemoteForwarder(t *testing.T) {
customConfig := []*sandbox.NodeConfig{
{
ForwarderCtx: forwarderCtx,
ForwarderGenerateTokenFunc: sandbox.GenerateExpiringToken(time.Second),
ForwarderGenerateTokenFunc: sandbox.GenerateExpiringToken(forwarderCtx, time.Second),
},
}

Expand All @@ -177,7 +177,7 @@ func TestNSMGR_HealRemoteForwarderRestored(t *testing.T) {
customConfig := []*sandbox.NodeConfig{
{
ForwarderCtx: forwarderCtx,
ForwarderGenerateTokenFunc: sandbox.GenerateExpiringToken(time.Second),
ForwarderGenerateTokenFunc: sandbox.GenerateExpiringToken(forwarderCtx, time.Second),
},
}

Expand All @@ -204,7 +204,7 @@ func testNSMGRHealForwarder(t *testing.T, nodeNum int, restored bool, customConf
}

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

request := &networkservice.NetworkServiceRequest{
Expand All @@ -218,7 +218,7 @@ func testNSMGRHealForwarder(t *testing.T, nodeNum int, restored bool, customConf
},
}

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

conn, err := nsc.Request(ctx, request.Clone())
require.NoError(t, err)
Expand All @@ -239,7 +239,7 @@ func testNSMGRHealForwarder(t *testing.T, nodeNum int, restored bool, customConf
if restored {
forwarderReg.Url = domain.Nodes[nodeNum].Forwarder[0].URL.String()
}
_, err = domain.Nodes[nodeNum].NewForwarder(ctx, forwarderReg, sandbox.GenerateTestToken)
_, err = domain.Nodes[nodeNum].NewForwarder(ctx, forwarderReg, sandbox.GenerateTestToken(ctx))
require.NoError(t, err)

// Wait Cross NSE expired and reconnecting through the new Cross NSE
Expand Down Expand Up @@ -285,9 +285,9 @@ func TestNSMGR_HealRemoteNSMgrRestored(t *testing.T) {
customConfig := []*sandbox.NodeConfig{
{
NsmgrCtx: nsmgrCtx,
NsmgrGenerateTokenFunc: sandbox.GenerateExpiringToken(time.Second),
NsmgrGenerateTokenFunc: sandbox.GenerateExpiringToken(nsmgrCtx, time.Second),
ForwarderCtx: nsmgrCtx,
ForwarderGenerateTokenFunc: sandbox.GenerateExpiringToken(time.Second),
ForwarderGenerateTokenFunc: sandbox.GenerateExpiringToken(nsmgrCtx, time.Second),
},
}

Expand All @@ -314,7 +314,7 @@ func testNSMGRHealNSMgr(t *testing.T, nodeNum int, customConfig []*sandbox.NodeC
}

counter := &counterServer{}
nse, err := domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, counter)
nse, err := domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken(ctx), counter)
require.NoError(t, err)

request := &networkservice.NetworkServiceRequest{
Expand All @@ -328,7 +328,7 @@ func testNSMGRHealNSMgr(t *testing.T, nodeNum int, customConfig []*sandbox.NodeC
},
}

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

conn, err := nsc.Request(ctx, request.Clone())
require.NoError(t, err)
Expand All @@ -342,14 +342,14 @@ func testNSMGRHealNSMgr(t *testing.T, nodeNum int, customConfig []*sandbox.NodeC

require.Eventually(t, checkURLFree(domain.Nodes[nodeNum].NSMgr.URL.Host), timeout, tick)

restoredNSMgrEntry, restoredNSMgrResources := builder.NewNSMgr(ctx, domain.Nodes[nodeNum], domain.Nodes[nodeNum].NSMgr.URL.Host, domain.Registry.URL, sandbox.GenerateTestToken)
restoredNSMgrEntry, restoredNSMgrResources := builder.NewNSMgr(ctx, domain.Nodes[nodeNum], domain.Nodes[nodeNum].NSMgr.URL.Host, domain.Registry.URL, sandbox.GenerateTestToken(ctx))
domain.Nodes[nodeNum].NSMgr = restoredNSMgrEntry
domain.AddResources(restoredNSMgrResources)

forwarderReg := &registry.NetworkServiceEndpoint{
Name: "forwarder-restored",
}
_, err = domain.Nodes[nodeNum].NewForwarder(ctx, forwarderReg, sandbox.GenerateTestToken)
_, err = domain.Nodes[nodeNum].NewForwarder(ctx, forwarderReg, sandbox.GenerateTestToken(ctx))
require.NoError(t, err)

nseReg.Url = nse.URL.String()
Expand Down Expand Up @@ -383,9 +383,9 @@ func TestNSMGR_HealRemoteNSMgr(t *testing.T) {
customConfig := []*sandbox.NodeConfig{
{
NsmgrCtx: nsmgrCtx,
NsmgrGenerateTokenFunc: sandbox.GenerateExpiringToken(time.Second),
NsmgrGenerateTokenFunc: sandbox.GenerateExpiringToken(nsmgrCtx, time.Second),
ForwarderCtx: nsmgrCtx,
ForwarderGenerateTokenFunc: sandbox.GenerateExpiringToken(time.Second),
ForwarderGenerateTokenFunc: sandbox.GenerateExpiringToken(nsmgrCtx, time.Second),
},
}

Expand All @@ -407,7 +407,7 @@ func TestNSMGR_HealRemoteNSMgr(t *testing.T) {
}

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

request := &networkservice.NetworkServiceRequest{
Expand All @@ -421,7 +421,7 @@ func TestNSMGR_HealRemoteNSMgr(t *testing.T) {
},
}

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

conn, err := nsc.Request(ctx, request.Clone())
require.NoError(t, err)
Expand All @@ -435,7 +435,7 @@ func TestNSMGR_HealRemoteNSMgr(t *testing.T) {
Name: "final-endpoint-2",
NetworkServiceNames: []string{"my-service-remote"},
}
_, err = domain.Nodes[2].NewEndpoint(ctx, nseReg2, sandbox.GenerateTestToken, counter)
_, err = domain.Nodes[2].NewEndpoint(ctx, nseReg2, sandbox.GenerateTestToken(ctx), counter)
require.NoError(t, err)

// Wait Cross NSE expired and reconnecting through the new Cross NSE
Expand Down
58 changes: 29 additions & 29 deletions pkg/networkservice/chains/nsmgr/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ func TestNSMGR_RemoteUsecase_Parallel(t *testing.T) {
Name: "final-endpoint",
NetworkServiceNames: []string{"my-service-remote"},
}
_, err := domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, counter)
_, err := domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken(ctx), counter)
require.NoError(t, err)
}()
nsc := domain.Nodes[1].NewClient(ctx, sandbox.GenerateTestToken)
nsc := domain.Nodes[1].NewClient(ctx, sandbox.GenerateTestToken(ctx))

conn, err := nsc.Request(ctx, request.Clone())
require.NoError(t, err)
Expand Down Expand Up @@ -155,12 +155,12 @@ func TestNSMGR_SelectsRestartingEndpoint(t *testing.T) {
// 2. Postpone endpoint start
time.AfterFunc(time.Second, func() {
s := grpc.NewServer()
endpoint.NewServer(ctx, sandbox.GenerateTestToken).Register(s)
endpoint.NewServer(ctx, sandbox.GenerateTestToken(ctx)).Register(s)
_ = s.Serve(netListener)
})

// 3. Create client and request endpoint
nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken)
nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken(ctx))

conn, err := nsc.Request(ctx, request.Clone())
require.NoError(t, err)
Expand Down Expand Up @@ -203,7 +203,7 @@ func TestNSMGR_RemoteUsecase_BusyEndpoints(t *testing.T) {
Name: "final-endpoint-" + strconv.Itoa(id),
NetworkServiceNames: []string{"my-service-remote"},
}
_, err := domain.Nodes[1].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, newBusyEndpoint())
_, err := domain.Nodes[1].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken(ctx), newBusyEndpoint())
require.NoError(t, err)
wg.Done()
}(i)
Expand All @@ -215,10 +215,10 @@ func TestNSMGR_RemoteUsecase_BusyEndpoints(t *testing.T) {
Name: "final-endpoint-3",
NetworkServiceNames: []string{"my-service-remote"},
}
_, err := domain.Nodes[1].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, counter)
_, err := domain.Nodes[1].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken(ctx), counter)
require.NoError(t, err)
}()
nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken)
nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken(ctx))

conn, err := nsc.Request(ctx, request.Clone())
require.NoError(t, err)
Expand Down Expand Up @@ -257,7 +257,7 @@ func TestNSMGR_RemoteUsecase(t *testing.T) {
}

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

request := &networkservice.NetworkServiceRequest{
Expand All @@ -271,7 +271,7 @@ func TestNSMGR_RemoteUsecase(t *testing.T) {
},
}

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

conn, err := nsc.Request(ctx, request.Clone())
require.NoError(t, err)
Expand Down Expand Up @@ -318,10 +318,10 @@ func TestNSMGR_ConnectToDeadNSE(t *testing.T) {
counter := &counterServer{}

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

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

request := &networkservice.NetworkServiceRequest{
MechanismPreferences: []*networkservice.Mechanism{
Expand Down Expand Up @@ -371,10 +371,10 @@ func TestNSMGR_LocalUsecase(t *testing.T) {
}

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

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

request := &networkservice.NetworkServiceRequest{
MechanismPreferences: []*networkservice.Mechanism{
Expand Down Expand Up @@ -433,10 +433,10 @@ func TestNSMGR_PassThroughRemote(t *testing.T) {
chain.NewNetworkServiceServer(
clienturl.NewServer(domain.Nodes[i].NSMgr.URL),
connect.NewServer(ctx,
sandbox.NewCrossConnectClientFactory(sandbox.GenerateTestToken,
sandbox.NewCrossConnectClientFactory(sandbox.GenerateTestToken(ctx),
newPassTroughClient(fmt.Sprintf("my-service-remote-%v", i-1)),
kernel.NewClient()),
connect.WithDialOptions(sandbox.DefaultDialOptions(sandbox.GenerateTestToken)...),
connect.WithDialOptions(sandbox.DefaultDialOptions(ctx, sandbox.GenerateTestToken(ctx))...),
),
),
}
Expand All @@ -445,11 +445,11 @@ func TestNSMGR_PassThroughRemote(t *testing.T) {
Name: fmt.Sprintf("endpoint-%v", i),
NetworkServiceNames: []string{fmt.Sprintf("my-service-remote-%v", i)},
}
_, err := domain.Nodes[i].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, additionalFunctionality...)
_, err := domain.Nodes[i].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken(ctx), additionalFunctionality...)
require.NoError(t, err)
}

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

request := &networkservice.NetworkServiceRequest{
MechanismPreferences: []*networkservice.Mechanism{
Expand Down Expand Up @@ -492,10 +492,10 @@ func TestNSMGR_PassThroughLocal(t *testing.T) {
chain.NewNetworkServiceServer(
clienturl.NewServer(domain.Nodes[0].NSMgr.URL),
connect.NewServer(ctx,
sandbox.NewCrossConnectClientFactory(sandbox.GenerateTestToken,
sandbox.NewCrossConnectClientFactory(sandbox.GenerateTestToken(ctx),
newPassTroughClient(fmt.Sprintf("my-service-remote-%v", i-1)),
kernel.NewClient()),
connect.WithDialOptions(sandbox.DefaultDialOptions(sandbox.GenerateTestToken)...),
connect.WithDialOptions(sandbox.DefaultDialOptions(ctx, sandbox.GenerateTestToken(ctx))...),
),
),
}
Expand All @@ -504,11 +504,11 @@ func TestNSMGR_PassThroughLocal(t *testing.T) {
Name: fmt.Sprintf("endpoint-%v", i),
NetworkServiceNames: []string{fmt.Sprintf("my-service-remote-%v", i)},
}
_, err := domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, additionalFunctionality...)
_, err := domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken(ctx), additionalFunctionality...)
require.NoError(t, err)
}

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

request := &networkservice.NetworkServiceRequest{
MechanismPreferences: []*networkservice.Mechanism{
Expand Down Expand Up @@ -556,15 +556,15 @@ func TestNSMGR_ShouldCorrectlyAddForwardersWithSameNames(t *testing.T) {

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

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

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

// 2. Wait for refresh
Expand Down Expand Up @@ -606,17 +606,17 @@ func TestNSMGR_ShouldCorrectlyAddEndpointsWithSameNames(t *testing.T) {
Name: "endpoint",
}

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

// 1. Add endpoints
nse1Reg := nseReg.Clone()
nse1Reg.NetworkServiceNames = []string{"service-1"}
_, err := domain.Nodes[0].NewEndpoint(ctx, nse1Reg, sandbox.GenerateTestToken)
_, err := domain.Nodes[0].NewEndpoint(ctx, nse1Reg, sandbox.GenerateTestToken(ctx))
require.NoError(t, err)

nse2Reg := nseReg.Clone()
nse2Reg.NetworkServiceNames = []string{"service-2"}
_, err = domain.Nodes[0].NewEndpoint(ctx, nse2Reg, sandbox.GenerateTestToken)
_, err = domain.Nodes[0].NewEndpoint(ctx, nse2Reg, sandbox.GenerateTestToken(ctx))
require.NoError(t, err)

// 2. Wait for refresh
Expand Down Expand Up @@ -689,10 +689,10 @@ func testNSEAndClient(
ctx, cancel := context.WithCancel(ctx)
defer cancel()

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

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

conn, err := nsc.Request(ctx, &networkservice.NetworkServiceRequest{
MechanismPreferences: []*networkservice.Mechanism{
Expand Down
4 changes: 2 additions & 2 deletions pkg/networkservice/chains/nsmgrproxy/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ func TestNSMGR_InterdomainUseCase(t *testing.T) {
NetworkServiceNames: []string{"my-service-interdomain"},
}

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

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

request := &networkservice.NetworkServiceRequest{
MechanismPreferences: []*networkservice.Mechanism{
Expand Down
Loading

0 comments on commit cc1ae41

Please sign in to comment.