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_memif_to_kernel_test.go
147 lines (130 loc) · 6.76 KB
/
suite_memif_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// 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"
"io/ioutil"
"os"
"time"
"github.com/edwarnicke/grpcfd"
"github.com/networkservicemesh/api/pkg/api/networkservice"
kernelmechanism "github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/kernel"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/recvfd"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain"
"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/sirupsen/logrus"
"github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"github.com/networkservicemesh/sdk-vppagent/pkg/networkservice/commit"
"github.com/networkservicemesh/sdk-vppagent/pkg/networkservice/connectioncontext"
"github.com/networkservicemesh/sdk-vppagent/pkg/networkservice/mechanisms/kernel"
"github.com/networkservicemesh/sdk-vppagent/pkg/networkservice/mechanisms/memif"
"github.com/networkservicemesh/sdk-vppagent/pkg/networkservice/vppagent"
)
func (f *ForwarderTestSuite) TestMemifToKernel() {
starttime := time.Now()
// Create ctx for test
ctx, cancel := context.WithTimeout(f.ctx, contextTimeout)
defer cancel()
ctx = log.WithField(ctx, "TestKernelToMemif", f.T().Name())
networkserviceName := "m2kns"
// 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 := "m2k-server"
tmpDir, err := ioutil.TempDir("", "forwarder.test-")
if err != nil {
logrus.Fatalf("error creating tmpDir %+v", err)
}
defer func(tmpDir string) { _ = os.Remove(tmpDir) }(tmpDir)
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 := "m2k-client"
forwarderClient := f.client(ctx, chain.NewNetworkServiceClient(
vppagent.NewClient(),
commit.NewClient(f.vppagentClientCC),
connectioncontext.NewClient(),
memif.NewClient(),
recvfd.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.pingVpp(ctx, conn.GetContext().GetIpContext().GetDstIpAddr(), f.vppagentClientRoot)
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().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)
// ********************************************************************************
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))
// ********************************************************************************
}