/
main.go
313 lines (290 loc) · 8.74 KB
/
main.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright 2018 ETH Zurich, Anapaya Systems
//
// 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 main
import (
"context"
"flag"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/integration"
"github.com/scionproto/scion/go/lib/log"
"github.com/scionproto/scion/go/lib/serrors"
"github.com/scionproto/scion/go/lib/snet"
"github.com/scionproto/scion/go/lib/util"
"github.com/scionproto/scion/go/pkg/app/feature"
)
var (
subset string
attempts int
timeout = &util.DurWrap{Duration: 10 * time.Second}
parallelism int
name string
cmd string
features string
)
func getCmd() (string, bool) {
return cmd, strings.Contains(cmd, "end2end")
}
func main() {
os.Exit(realMain())
}
func realMain() int {
addFlags()
if err := integration.Init(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to init: %s\n", err)
return 1
}
defer log.HandlePanic()
defer log.Flush()
if len(features) != 0 {
if _, err := feature.ParseDefault(strings.Split(features, ",")); err != nil {
fmt.Fprintf(os.Stderr, "Error parsing features: %s\n", err)
return 1
}
}
clientArgs := []string{
"-log.console", "debug",
"-attempts", strconv.Itoa(attempts),
"-timeout", timeout.String(),
"-sciond", integration.SCIOND,
"-local", integration.SrcAddrPattern + ":0",
"-remote", integration.DstAddrPattern + ":" + integration.ServerPortReplace,
}
serverArgs := []string{
"-mode", "server",
"-sciond", integration.SCIOND,
"-local", integration.DstAddrPattern + ":0",
}
if len(features) != 0 {
clientArgs = append(clientArgs, "--features", features)
serverArgs = append(serverArgs, "--features", features)
}
in := integration.NewBinaryIntegration(name, cmd, clientArgs, serverArgs)
pairs, err := getPairs()
if err != nil {
log.Error("Error selecting tests", "err", err)
return 1
}
if err := runTests(in, pairs); err != nil {
log.Error("Error during tests", "err", err)
return 1
}
return 0
}
// addFlags adds the necessary flags.
func addFlags() {
flag.IntVar(&attempts, "attempts", 1, "Number of attempts per client before giving up.")
flag.StringVar(&cmd, "cmd", "./bin/end2end",
"The end2end binary to run (default: ./bin/end2end)")
flag.StringVar(&name, "name", "end2end_integration",
"The name of the test that is running (default: end2end_integration)")
flag.Var(timeout, "timeout", "The timeout for each attempt")
flag.StringVar(&subset, "subset", "all", "Subset of pairs to run (all|core#core|"+
"noncore#localcore|noncore#core|noncore#noncore)")
flag.IntVar(¶llelism, "parallelism", 1, "How many end2end tests run in parallel.")
flag.StringVar(&features, "features", "",
fmt.Sprintf("enable development features (%v)", feature.String(&feature.Default{}, "|")))
}
// runTests runs the end2end tests for all pairs. In case of an error the
// function is terminated immediately.
func runTests(in integration.Integration, pairs []integration.IAPair) error {
return integration.ExecuteTimed(in.Name(), func() error {
// Make sure that all executed commands can write to the RPC server
// after shutdown.
defer time.Sleep(time.Second)
// Estimating the timeout we should have is hard. CI will abort after 10
// minutes anyway. Thus this value.
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
defer cancel()
// First run all servers
type srvResult struct {
cleaner func()
err error
}
// Start servers in parallel.
srvResults := make(chan srvResult)
for _, dst := range integration.ExtractUniqueDsts(pairs) {
go func(dst *snet.UDPAddr) {
defer log.HandlePanic()
srvCtx, cancel := context.WithCancel(ctx)
waiter, err := in.StartServer(srvCtx, dst)
if err != nil {
log.Error(fmt.Sprintf("Error in server: %s", dst.String()), "err", err)
}
cleaner := func() {
cancel()
if waiter != nil {
waiter.Wait()
}
}
srvResults <- srvResult{cleaner: cleaner, err: err}
}(dst)
}
// Wait for all servers being started.
var errs serrors.List
for range integration.ExtractUniqueDsts(pairs) {
res := <-srvResults
// We need to register a cleanup for all servers.
// Do not short-cut exit here.
if res.err != nil {
errs = append(errs, res.err)
}
defer res.cleaner()
}
if err := errs.ToError(); err != nil {
return err
}
// Start a done signal listener. This is how the end2end binary
// communicates with this integration test. This is solely used to print
// the progress of the test.
var ctrMtx sync.Mutex
var ctr int
socket, clean, err := integration.ListenDone(func(src, dst addr.IA) {
ctrMtx.Lock()
defer ctrMtx.Unlock()
ctr++
testInfo := fmt.Sprintf("%v -> %v (%v/%v)", src, dst, ctr, len(pairs))
log.Info(fmt.Sprintf("Test %v: %s", in.Name(), testInfo))
})
if err != nil {
return err
}
defer clean()
// CI collapses if parallelism is too high.
semaphore := make(chan struct{}, parallelism)
// Docker exec comes with a 1 second overhead. We group all the pairs by
// the clients. And run all pairs for a given client in one execution.
// Thus, reducing the overhead dramatically.
groups := integration.GroupBySource(pairs)
clientResults := make(chan error, len(groups))
for src, dsts := range groups {
go func(src *snet.UDPAddr, dsts []*snet.UDPAddr) {
defer log.HandlePanic()
semaphore <- struct{}{}
defer func() { <-semaphore }()
// Aggregate all the commands that need to be run.
cmds := make([]integration.Cmd, 0, len(dsts))
for _, dst := range dsts {
cmd, err := clientTemplate(socket).Template(src, dst)
if err != nil {
clientResults <- err
return
}
cmds = append(cmds, cmd)
}
var tester string
if *integration.Docker {
tester = integration.TesterID(src)
}
logFile := fmt.Sprintf("%s/client_%s.log", logDir(), src.IA.FileFmt(false))
err := integration.Run(ctx, integration.RunConfig{
Commands: cmds,
LogFile: logFile,
Tester: tester,
})
if err != nil {
err = serrors.WithCtx(err, "file", logFile)
}
clientResults <- err
}(src, dsts)
}
errs = nil
for range groups {
err := <-clientResults
if err != nil {
errs = append(errs, err)
}
}
return errs.ToError()
})
}
func clientTemplate(progressSock string) integration.Cmd {
bin, progress := getCmd()
cmd := integration.Cmd{
Binary: bin,
Args: []string{
"-log.console", "debug",
"-attempts", strconv.Itoa(attempts),
"-timeout", timeout.String(),
"-sciond", integration.SCIOND,
"-local", integration.SrcAddrPattern + ":0",
"-remote", integration.DstAddrPattern + ":" + integration.ServerPortReplace,
},
}
if len(features) != 0 {
cmd.Args = append(cmd.Args, "--features", features)
}
if progress {
cmd.Args = append(cmd.Args, "-progress", progressSock)
}
return cmd
}
// getPairs returns the pairs to test according to the specified subset.
func getPairs() ([]integration.IAPair, error) {
pairs := integration.IAPairs(integration.DispAddr)
if subset == "all" {
return pairs, nil
}
parts := strings.Split(subset, "#")
if len(parts) != 2 {
return nil, serrors.New("Invalid subset", "subset", subset)
}
return filter(parts[0], parts[1], pairs, integration.ASList), nil
}
// filter returns the list of ASes that are part of the desired subset.
func filter(src, dst string, pairs []integration.IAPair, ases *util.ASList) []integration.IAPair {
var res []integration.IAPair
s, err1 := addr.IAFromString(src)
d, err2 := addr.IAFromString(dst)
if err1 == nil && err2 == nil {
for _, pair := range pairs {
if pair.Src.IA.Equal(s) && pair.Dst.IA.Equal(d) {
res = append(res, pair)
return res
}
}
}
for _, pair := range pairs {
filter := !contains(ases, src != "noncore", pair.Src.IA)
filter = filter || !contains(ases, dst != "noncore", pair.Dst.IA)
if dst == "localcore" {
filter = filter || pair.Src.IA.I != pair.Dst.IA.I
}
if !filter {
res = append(res, pair)
}
}
return res
}
func contains(ases *util.ASList, core bool, ia addr.IA) bool {
l := ases.Core
if !core {
l = ases.NonCore
}
for _, as := range l {
if ia.Equal(as) {
return true
}
}
return false
}
func logDir() string {
return filepath.Join(integration.LogDir(), name)
}