-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
213 lines (179 loc) · 4.86 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
package main
import (
"context"
"errors"
"flag"
"fmt"
"log"
"os"
"os/exec"
"runtime"
"strings"
"time"
)
var (
checksFlag = flag.String("checks", "", "comma-separated list of checks to execute")
allChecksFlag = flag.Bool("all", false, "execute all possible checks")
listChecksFlag = flag.Bool("list", false, "list possible checks")
execTimeout = 120 * time.Second
buildTimeout = 45 * time.Second
timeFormat = "2006-01-02 15:04:05.999"
)
func main() {
flag.Parse()
ctx := context.Background()
// status(fmt.Sprintf("Gathering simulations for %s/%s", runtime.GOOS, runtime.GOARCH))
choices, err := gatherChoices(ctx)
if err != nil {
log.Fatalf("gather choices: %v", err)
}
if *listChecksFlag {
fmt.Printf("checks available for %s/%s:\n\n", runtime.GOOS, runtime.GOARCH)
for _, c := range choices {
fmt.Printf("* %s: %s\n", c.name, c.desc)
}
os.Exit(0)
}
selected := []choice{}
if *allChecksFlag {
selected = append(selected, choices...)
} else if *checksFlag != "" {
for _, s := range strings.Split(*checksFlag, ",") {
var found *choice
for _, c := range choices {
if c.name == s {
found = &c
break
}
}
if found != nil {
selected = append(selected, *found)
} else {
fmt.Printf("%s is not an available test on this platform: %v", s, choices)
os.Exit(2)
}
}
}
if len(selected) == 0 {
selected, err = selectChoices(ctx, choices)
if err != nil {
log.Fatalf("show choices: %v", err)
}
}
if len(selected) == 0 {
msg("また会おうね")
os.Exit(0)
}
status(fmt.Sprintf("Building %d selected simulations", len(selected)))
if err = buildSimulations(ctx, selected); err != nil {
log.Printf("build failed: %v", err)
os.Exit(1)
}
status(fmt.Sprintf("Executing %d selected simulations", len(selected)))
if err = runSimulations(ctx, selected); err != nil {
log.Printf("run failed: %v", err)
os.Exit(2)
}
}
type choice struct {
name string
desc string
}
func gatherChoices(ctx context.Context) ([]choice, error) {
dirs, err := os.ReadDir("cmd")
if err != nil {
return nil, fmt.Errorf("readdir: %w", err)
}
choices := []choice{}
for _, d := range dirs {
c := d.Name()
cmd := exec.CommandContext(ctx, "go", "doc", "./cmd/"+c)
out, err := cmd.CombinedOutput()
if exitErr, ok := err.(*exec.ExitError); ok {
if exitErr.ExitCode() == 1 {
continue
}
return choices, fmt.Errorf("%s failed: %v\n%s", cmd, err, out)
}
choices = append(choices, choice{name: c, desc: strings.TrimSpace(string(out))})
}
return choices, nil
}
func buildSimulations(ctx context.Context, checks []choice) error {
failed := 0
if err := os.MkdirAll("out", 0o700); err != nil {
return fmt.Errorf("mkdir: %w", err)
}
if err := os.Chdir("out"); err != nil {
return fmt.Errorf("chdir: %w", err)
}
for i, c := range checks {
ctx, cancel := context.WithTimeout(context.Background(), buildTimeout)
defer cancel()
cmd := exec.CommandContext(ctx, "go", "build", "../cmd/"+c.name)
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("#%d: build failed: %v\n%s", i, err, out)
failed++
continue
}
}
return nil
}
func runSimulations(ctx context.Context, checks []choice) error {
su, err := exec.LookPath("sudo")
if err != nil {
su, err = exec.LookPath("doas")
if err != nil {
su = "su"
}
}
failed := map[string]string{}
for i, c := range checks {
if _, err := os.Stat(c.name); err != nil {
log.Printf("%s not found (build failure?) - skipping", c.name)
failed[c.name] = "MISSING"
continue
}
title := fmt.Sprintf("[%d of %d] %s at %s", i+1, len(checks), c, time.Now().Format(timeFormat))
announce(title)
subtitle(c.desc)
ctx, cancel := context.WithTimeout(context.Background(), execTimeout)
defer cancel()
cmd := exec.CommandContext(ctx, "./"+c.name)
if strings.HasSuffix(c.name, "-root") {
notice(fmt.Sprintf("This simulation requires root privileges - will use %s", su))
cmd = exec.CommandContext(ctx, su, "./"+c.name)
}
log.Printf("executing %v", cmd.Args)
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
log.Printf("%s timed out as expected: %v", c.name, err)
} else {
if exiterr, ok := err.(*exec.ExitError); ok {
failed[c.name] = fmt.Sprintf("EXITCODE_%d", exiterr.ExitCode())
} else {
failed[c.name] = fmt.Sprintf("ERR_%s", err)
}
log.Printf("%s failed: %v", c.name, err)
}
} else {
log.Printf("%s exited successfully", c.name)
}
// Make it easier to disambiguate in the logs
time.Sleep(1 * time.Second)
}
title := fmt.Sprintf("CHECKS COMPLETE - %d of %d checks failed", len(failed), len(checks))
announce(title)
for _, c := range checks {
state := failed[c.name]
if state == "" {
state = "SUCCESS"
}
fmt.Printf("%-30.30s: %s\n", c.name, state)
}
return nil
}