-
Notifications
You must be signed in to change notification settings - Fork 460
/
hive.go
241 lines (217 loc) · 7.78 KB
/
hive.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
package main
import (
"context"
"errors"
"flag"
"fmt"
"log/slog"
"os"
"os/signal"
"regexp"
"sort"
"strings"
"time"
"github.com/ethereum/hive/internal/libdocker"
"github.com/ethereum/hive/internal/libhive"
"github.com/lmittmann/tint"
)
type buildArgs map[string]string
func (args *buildArgs) String() string {
var kv []string
for k, v := range *args {
kv = append(kv, k+"="+v)
}
sort.Strings(kv)
return strings.Join(kv, ",")
}
// Set implements flag.Value.
func (args *buildArgs) Set(value string) error {
parts := strings.SplitN(value, "=", 2)
if len(parts) != 2 {
return errors.New("invalid build argument format, expected ARG=VALUE")
}
(*args)[parts[0]] = parts[1]
return nil
}
func main() {
var (
testResultsRoot = flag.String("results-root", "workspace/logs", "Target `directory` for results files and logs.")
loglevelFlag = flag.Int("loglevel", 3, "Log `level` for system events. Supports values 0-5.")
dockerEndpoint = flag.String("docker.endpoint", "", "Endpoint of the local Docker daemon.")
dockerNoCache = flag.String("docker.nocache", "", "Regular `expression` selecting the docker images to forcibly rebuild.")
dockerPull = flag.Bool("docker.pull", false, "Refresh base images when building images.")
dockerOutput = flag.Bool("docker.output", false, "Relay all docker output to stderr.")
simPattern = flag.String("sim", "", "Regular `expression` selecting the simulators to run.")
simTestPattern = flag.String("sim.limit", "", "Regular `expression` selecting tests/suites (interpreted by simulators).")
simParallelism = flag.Int("sim.parallelism", 1, "Max `number` of parallel clients/containers (interpreted by simulators).")
simRandomSeed = flag.Int("sim.randomseed", 0, "Randomness seed number (interpreted by simulators).")
simTestLimit = flag.Int("sim.testlimit", 0, "[DEPRECATED] Max `number` of tests to execute per client (interpreted by simulators).")
simTimeLimit = flag.Duration("sim.timelimit", 0, "Simulation `timeout`. Hive aborts the simulator if it exceeds this time.")
simLogLevel = flag.Int("sim.loglevel", 3, "Selects log `level` of client instances. Supports values 0-5.")
simDevMode = flag.Bool("dev", false, "Only starts the simulator API endpoint (listening at 127.0.0.1:3000 by default) without starting any simulators.")
simDevModeAPIEndpoint = flag.String("dev.addr", "127.0.0.1:3000", "Endpoint that the simulator API listens on")
useCredHelper = flag.Bool("docker.cred-helper", false, "configure docker authentication using locally-configured credential helper")
clientsFile = flag.String("client-file", "", `YAML `+"`file`"+` containing client configurations.`)
clients = flag.String("client", "go-ethereum", "Comma separated `list` of clients to use. Client names in the list may be given as\n"+
"just the client name, or a client_branch specifier. If a branch name is supplied,\n"+
"the client image will use the given git branch or docker tag. Multiple instances of\n"+
"a single client type may be requested with different branches.\n"+
"Example: \"besu_latest,besu_20.10.2\"\n")
clientTimeout = flag.Duration("client.checktimelimit", 3*time.Minute, "The `timeout` of waiting for clients to open up the RPC port.\n"+
"If a very long chain is imported, this timeout may need to be quite large.\n"+
"A lower value means that hive won't wait as long in case the node crashes and\n"+
"never opens the RPC port.")
)
// Add the sim.buildarg flag multiple times to allow multiple build arguments.
simBuildArgs := make(buildArgs)
flag.Var(&simBuildArgs, "sim.buildarg", "Argument to pass to the docker engine when building the simulator image, in the form of ARGNAME=VALUE.")
// Parse the flags and configure the logger.
flag.Parse()
terminal := os.Getenv("TERM")
tintHandler := tint.NewHandler(os.Stderr, &tint.Options{
Level: convertLogLevel(*loglevelFlag),
NoColor: terminal == "" || terminal == "dumb",
})
slog.SetDefault(slog.New(tintHandler))
if *simTestLimit > 0 {
slog.Warn("Option --sim.testlimit is deprecated and will have no effect.")
}
// Get the list of simulators.
inv, err := libhive.LoadInventory(".")
if err != nil {
fatal(err)
}
simList, err := inv.MatchSimulators(*simPattern)
if err != nil {
fatal("bad --sim regular expression:", err)
}
if *simPattern != "" && len(simList) == 0 {
fatal("no simulators for pattern", *simPattern)
}
if *simPattern != "" && *simDevMode {
slog.Warn("--sim is ignored when using --dev mode")
simList = nil
}
// Create the docker backends.
dockerConfig := &libdocker.Config{
Inventory: inv,
PullEnabled: *dockerPull,
UseCredentialHelper: *useCredHelper,
}
if *dockerNoCache != "" {
re, err := regexp.Compile(*dockerNoCache)
if err != nil {
fatal("bad --docker-nocache regular expression:", err)
}
dockerConfig.NoCachePattern = re
}
if *dockerOutput {
dockerConfig.ContainerOutput = os.Stderr
dockerConfig.BuildOutput = os.Stderr
}
builder, cb, err := libdocker.Connect(*dockerEndpoint, dockerConfig)
if err != nil {
fatal(err)
}
// Set up the context for CLI interrupts.
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
ctx, cancel := context.WithCancel(context.Background())
go func() {
<-sig
cancel()
}()
// Run.
env := libhive.SimEnv{
LogDir: *testResultsRoot,
SimLogLevel: *simLogLevel,
SimTestPattern: *simTestPattern,
SimParallelism: *simParallelism,
SimRandomSeed: *simRandomSeed,
SimDurationLimit: *simTimeLimit,
ClientStartTimeout: *clientTimeout,
}
runner := libhive.NewRunner(inv, builder, cb)
// Parse the client list.
// It can be supplied as a comma-separated list, or as a YAML file.
var clientList []libhive.ClientDesignator
if *clientsFile == "" {
clientList, err = libhive.ParseClientList(&inv, *clients)
if err != nil {
fatal("-client:", err)
}
} else {
clientList, err = parseClientsFile(&inv, *clientsFile)
if err != nil {
fatal("-client-file:", err)
}
// If YAML file is used, the list can be filtered by the -client flag.
if flagIsSet("client") {
filter := strings.Split(*clients, ",")
clientList = libhive.FilterClients(clientList, filter)
}
}
// Build clients and simulators.
if err := runner.Build(ctx, clientList, simList, simBuildArgs); err != nil {
fatal(err)
}
if *simDevMode {
runner.RunDevMode(ctx, env, *simDevModeAPIEndpoint)
return
}
// Run simulators.
var failCount int
for _, sim := range simList {
result, err := runner.Run(ctx, sim, env)
if err != nil {
fatal(err)
}
failCount += result.TestsFailed
slog.Info(fmt.Sprintf("simulation %s finished", sim), "suites", result.Suites, "tests", result.Tests, "failed", result.TestsFailed)
}
switch failCount {
case 0:
case 1:
fatal(errors.New("1 test failed"))
default:
fatal(fmt.Errorf("%d tests failed", failCount))
}
}
func fatal(args ...interface{}) {
fmt.Fprintln(os.Stderr, args...)
os.Exit(1)
}
func parseClientsFile(inv *libhive.Inventory, file string) ([]libhive.ClientDesignator, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
return libhive.ParseClientListYAML(inv, f)
}
func flagIsSet(name string) bool {
var found bool
flag.Visit(func(f *flag.Flag) {
if f.Name == name {
found = true
}
})
return found
}
// convertLogLevel maps log level from range 0-5 to the range used by package slog.
// Input levels are ordered in increasing amounts of messages, i.e. level zero is silent
// and level 5 means everything is printed.
func convertLogLevel(level int) slog.Level {
switch level {
case 0:
return 99
case 1:
return slog.LevelError
case 2:
return slog.LevelWarn
case 3:
return slog.LevelInfo
default:
return slog.LevelDebug
}
}