-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathmain.go
173 lines (159 loc) · 4.84 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
// 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 (
"flag"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"time"
yaml "gopkg.in/yaml.v2"
"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/integration"
"github.com/scionproto/scion/go/lib/log"
"github.com/scionproto/scion/go/lib/util"
)
const (
name = "end2end_integration"
cmd = "./bin/end2end"
)
var (
subset string
attempts int
runAll bool
timeout = &util.DurWrap{Duration: 5 * time.Second}
)
func main() {
os.Exit(realMain())
}
func realMain() int {
addFlags()
if err := integration.Init(name); err != nil {
fmt.Fprintf(os.Stderr, "Failed to init: %s\n", err)
return 1
}
defer log.LogPanicAndExit()
defer log.Flush()
clientArgs := []string{"-log.console", "debug", "-attempts", strconv.Itoa(attempts),
"-timeout", timeout.String(),
"-local", integration.SrcAddrPattern + ":0",
"-remote", integration.DstAddrPattern + ":" + integration.ServerPortReplace}
serverArgs := []string{"-log.console", "debug", "-mode", "server",
"-local", integration.DstAddrPattern + ":0"}
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.BoolVar(&runAll, "all", false, "Run all tests, instead of exiting on first error.")
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)")
}
// 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 {
// First run all servers
var lastErr error
dsts := integration.ExtractUniqueDsts(pairs)
for _, dst := range dsts {
s, err := integration.StartServer(in, dst)
if err != nil {
log.Error(fmt.Sprintf("Error in server: %s", dst.String()), "err", err)
return err
}
defer s.Close()
}
// Now start the clients for srcDest pair
for i, conn := range pairs {
testInfo := fmt.Sprintf("%v -> %v (%v/%v)", conn.Src.IA, conn.Dst.IA, i+1, len(pairs))
log.Info(fmt.Sprintf("Test %v: %s", in.Name(), testInfo))
t := integration.DefaultRunTimeout + timeout.Duration*time.Duration(attempts)
if err := integration.RunClient(in, conn, t); err != nil {
log.Error(fmt.Sprintf("Error in client: %s", testInfo), "err", err)
lastErr = err
if !runAll {
return err
}
}
}
return lastErr
})
}
// 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
}
raw, err := ioutil.ReadFile("gen/as_list.yml")
if err != nil {
return nil, err
}
var ases ASList
if err := yaml.Unmarshal(raw, &ases); err != nil {
return nil, err
}
parts := strings.Split(subset, "-")
if len(parts) != 2 {
return nil, common.NewBasicError("Invalid subset", nil, "subset", subset)
}
return filter(parts[0], parts[1], pairs, ases), nil
}
// filter returns the list of ASes that are part of the desired subset.
func filter(src, dst string, pairs []integration.IAPair, ases ASList) []integration.IAPair {
var res []integration.IAPair
for _, pair := range pairs {
filter := !ases.contains(src != "noncore", pair.Src.IA)
filter = filter || !ases.contains(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
}
// ASList contains all ASes of the current topology.
type ASList struct {
Core []addr.IA `yaml:"Core"`
NonCore []addr.IA `yaml:"Non-core"`
}
func (l *ASList) contains(core bool, ia addr.IA) bool {
ases := l.Core
if !core {
ases = l.NonCore
}
for _, as := range ases {
if ia.Equal(as) {
return true
}
}
return false
}