This repository has been archived by the owner on May 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
suite_kernel_to_kernel_test.go
129 lines (113 loc) · 6.15 KB
/
suite_kernel_to_kernel_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// 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.
// +build !windows
package main_test
import (
"context"
"time"
"github.com/edwarnicke/grpcfd"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/sdk-vppagent/pkg/networkservice/mechanisms/kernel"
kernelmechanism "github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/kernel"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain"
"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
func (f *ForwarderTestSuite) TestKernelToKernel() {
starttime := time.Now()
// Create ctx for test
ctx, cancel := context.WithTimeout(f.ctx, contextTimeout)
defer cancel()
ctx = log.WithField(ctx, "test", f.T().Name())
networkserviceName := "k2kns"
// Create testRequest
testRequest := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
NetworkService: networkserviceName,
},
}
// ********************************************************************************
log.Entry(f.ctx).Infof("Launching %s test server (time since start: %s)", f.T().Name(), time.Since(starttime))
// ********************************************************************************
serverCreds := credentials.NewTLS(tlsconfig.MTLSServerConfig(f.x509source, f.x509bundle, tlsconfig.AuthorizeAny()))
serverCreds = grpcfd.TransportCredentials(serverCreds)
server := grpc.NewServer(grpc.Creds(serverCreds))
serverNSName := "k2k-server"
ep := f.server(ctx, serverNSName,
map[string]networkservice.NetworkServiceServer{
kernel.MECHANISM: chain.NewNetworkServiceServer(
kernelmechanism.NewServer(),
),
},
)
networkservice.RegisterNetworkServiceServer(server, ep)
networkservice.RegisterMonitorConnectionServer(server, ep)
serverErrCh := f.ListenAndServe(ctx, server)
// ********************************************************************************
log.Entry(f.ctx).Infof("Sending Request to forwarder (time since start: %s)", time.Since(starttime))
// ********************************************************************************
clientNSName := "k2k-client"
forwarderClient := f.client(ctx, kernelmechanism.NewClient(), clientNSName)
// Send Request
conn, err := forwarderClient.Request(ctx, testRequest)
f.Require().NoError(err)
f.NotNil(conn)
// ********************************************************************************
log.Entry(f.ctx).Infof("Checking we can ping both ways (time since start: %s)", time.Since(starttime))
// ********************************************************************************
f.pingKernel(ctx, conn.GetContext().GetIpContext().GetDstIpAddr(), clientNSName)
f.pingKernel(ctx, conn.GetContext().GetIpContext().GetSrcIpAddr(), serverNSName)
// ********************************************************************************
log.Entry(f.ctx).Infof("Checking that the expected interfaces exist (time since start: %s)", time.Since(starttime))
// ********************************************************************************
f.checkKernelInterface(networkserviceName, conn.GetContext().GetIpContext().GetSrcIpAddr(), clientNSName)
f.checkKernelInterface(networkserviceName, conn.GetContext().GetIpContext().GetDstIpAddr(), serverNSName)
// ********************************************************************************
log.Entry(f.ctx).Infof("Sending Close to forwarder (time since start: %s)", time.Since(starttime))
// ********************************************************************************
_, err = forwarderClient.Close(ctx, conn)
f.Require().NoError(err)
// A word about the sleep here. time.Sleep in tests is evil (in fact, its almost always evil :).
// However, vppagent is *async* wrt applying our changes. Meaning it takes our changes, returns, and then
// gets around to applying them. Normally its pretty zippy about it. However we've gotten *so* fast that
// its actually not always faster to apply them than we are to check them in this test.
//
// This sleep compensates for that.
//
// This sleep should *go away* shortly, when vppagent gets an option to fully apply the changes before
// returning from the grpc call. Till then, time.Sleep :(
// ********************************************************************************
log.Entry(f.ctx).Infof("Sleeping 200ms (time since start: %s)", time.Since(starttime))
// ********************************************************************************
time.Sleep(200 * time.Millisecond)
// ********************************************************************************
log.Entry(f.ctx).Infof("Checking that the expected interfaces no longer exist (time since start: %s)", time.Since(starttime))
// ********************************************************************************
f.checkNoKernelInterface(networkserviceName, clientNSName)
f.checkNoKernelInterface(networkserviceName, serverNSName)
// ********************************************************************************
log.Entry(f.ctx).Infof("Canceling ctx to end test (time since start: %s)", time.Since(starttime))
// ********************************************************************************
cancel()
// TODO put a proper select with timeout here
err = <-serverErrCh
f.Require().NoError(err, "This line")
// ********************************************************************************
log.Entry(f.ctx).Infof("%s completed (time since start: %s)", f.T().Name(), time.Since(starttime))
// ********************************************************************************
}