-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathmain.go
More file actions
331 lines (305 loc) · 11.2 KB
/
main.go
File metadata and controls
331 lines (305 loc) · 11.2 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package main
import (
"bytes"
"context"
"flag"
"fmt"
"io/fs"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"sort"
"strings"
_ "github.com/BurntSushi/toml"
_ "github.com/kisielk/errcheck/errcheck"
_ "honnef.co/go/tools/staticcheck"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/temporal"
"go.temporal.io/sdk/testsuite"
)
func main() {
if err := newBuilder().run(); err != nil {
log.Fatal(err)
}
}
const coverageDir = ".build/coverage"
type builder struct {
thisDir string
rootDir string
}
func newBuilder() *builder {
var b builder
// Find the root directory from this directory
_, thisFile, _, _ := runtime.Caller(0)
b.thisDir = filepath.Join(thisFile, "..")
b.rootDir = filepath.Join(b.thisDir, "../../../")
return &b
}
func (b *builder) run() error {
if len(os.Args) < 2 {
return fmt.Errorf("missing command name, 'check', 'integration-test', or 'unit-test' required")
}
switch os.Args[1] {
case "check":
return b.check()
case "integration-test":
return b.integrationTest()
case "merge-coverage-files":
return b.mergeCoverageFiles()
case "unit-test":
return b.unitTest()
default:
return fmt.Errorf("unrecognized command %q, 'check', 'integration-test', or 'unit-test' required", os.Args[1])
}
}
func (b *builder) check() error {
// Run go vet
if err := b.runCmd(b.cmdFromRoot("go", "vet", "./...")); err != nil {
return fmt.Errorf("go vet failed: %w", err)
}
// Run errcheck
if errCheck, err := b.getInstalledTool("github.com/kisielk/errcheck"); err != nil {
return fmt.Errorf("failed getting errcheck: %w", err)
} else if err := b.runCmd(b.cmdFromRoot(errCheck, "./...")); err != nil {
return fmt.Errorf("errcheck failed: %w", err)
}
// Run staticcheck
if staticCheck, err := b.getInstalledTool("honnef.co/go/tools/cmd/staticcheck"); err != nil {
return fmt.Errorf("failed getting staticcheck: %w", err)
} else if err := b.runCmd(b.cmdFromRoot(staticCheck, "./...")); err != nil {
return fmt.Errorf("staticcheck failed: %w", err)
}
// Run doclink check
if err := b.runCmd(b.cmdFromRoot("go", "run", "./internal/cmd/tools/doclink/doclink.go")); err != nil {
return fmt.Errorf("doclink check failed: %w", err)
}
return nil
}
func (b *builder) integrationTest() error {
// Supports some flags
flagSet := flag.NewFlagSet("integration-test", flag.ContinueOnError)
runFlag := flagSet.String("run", "", "Passed to go test as -run")
devServerFlag := flagSet.Bool("dev-server", false, "Use an embedded dev server")
coverageFileFlag := flagSet.String("coverage-file", "", "If set, enables coverage output to this filename")
if err := flagSet.Parse(os.Args[2:]); err != nil {
return fmt.Errorf("failed parsing flags: %w", err)
}
// Also accept coverage file as env var
if env := strings.TrimSpace(os.Getenv("TEMPORAL_COVERAGE_FILE")); *coverageFileFlag == "" && env != "" {
*coverageFileFlag = env
}
// Create coverage dir if doing coverage
if *coverageFileFlag != "" {
if err := os.MkdirAll(filepath.Join(b.rootDir, coverageDir), 0777); err != nil {
return fmt.Errorf("failed creating coverage dir: %w", err)
}
}
customKeyField := temporal.NewSearchAttributeKeyKeyword("CustomKeywordField")
customStringField := temporal.NewSearchAttributeKeyString("CustomStringField")
searchAttributes := temporal.NewSearchAttributes(
customKeyField.ValueSet("Keyword"),
customStringField.ValueSet("Text"),
)
// Start dev server if wanted
if *devServerFlag {
devServer, err := testsuite.StartDevServer(context.Background(), testsuite.DevServerOptions{
CachedDownload: testsuite.CachedDownload{
Version: "v1.7.0",
},
ClientOptions: &client.Options{
HostPort: "127.0.0.1:7233",
Namespace: "integration-test-namespace",
},
DBFilename: "temporal.sqlite",
LogLevel: "warn",
SearchAttributes: searchAttributes,
ExtraArgs: []string{
"--sqlite-pragma", "journal_mode=WAL",
"--sqlite-pragma", "synchronous=OFF",
"--dynamic-config-value", "frontend.enableExecuteMultiOperation=true",
"--dynamic-config-value", "frontend.enableUpdateWorkflowExecution=true",
"--dynamic-config-value", "frontend.enableUpdateWorkflowExecutionAsyncAccepted=true",
"--dynamic-config-value", "frontend.workerVersioningRuleAPIs=true",
"--dynamic-config-value", "frontend.workerVersioningDataAPIs=true",
"--dynamic-config-value", "frontend.workerVersioningWorkflowAPIs=true",
"--dynamic-config-value", "system.enableActivityEagerExecution=true",
"--dynamic-config-value", "system.enableEagerWorkflowStart=true",
"--dynamic-config-value", "system.forceSearchAttributesCacheRefreshOnRead=true",
"--dynamic-config-value", "worker.buildIdScavengerEnabled=true",
"--dynamic-config-value", "worker.removableBuildIdDurationSinceDefault=1",
"--dynamic-config-value", "system.enableDeployments=true",
"--dynamic-config-value", "system.enableDeploymentVersions=true",
"--dynamic-config-value", "matching.wv.VersionDrainageStatusVisibilityGracePeriod=10",
"--dynamic-config-value", "matching.wv.VersionDrainageStatusRefreshInterval=1",
"--dynamic-config-value", "matching.useNewMatcher=true",
"--dynamic-config-value", "frontend.activityAPIsEnabled=true",
"--http-port", "7243", // Nexus tests use the HTTP port directly
"--dynamic-config-value", `component.callbacks.allowedAddresses=[{"Pattern":"*","AllowInsecure":true}]`, // SDK tests use arbitrary callback URLs, permit that on the server
"--dynamic-config-value", `system.refreshNexusEndpointsMinWait="0s"`, // Make Nexus tests faster
"--dynamic-config-value", `component.nexusoperations.recordCancelRequestCompletionEvents=true`, // Defaults to false until after OSS 1.28 is released
"--dynamic-config-value", `history.enableRequestIdRefLinks=true`,
"--dynamic-config-value", "activity.enableStandalone=true",
"--dynamic-config-value", "history.enableChasm=true",
"--dynamic-config-value", "history.enableTransitionHistory=true",
"--dynamic-config-value", `component.nexusoperations.useSystemCallbackURL=false`,
"--dynamic-config-value", `component.nexusoperations.callback.endpoint.template="http://localhost:7243/namespaces/{{.NamespaceName}}/nexus/callback"`,
"--dynamic-config-value", "frontend.ListWorkersEnabled=true",
},
})
if err != nil {
return fmt.Errorf("failed starting dev server: %w", err)
}
defer func() { _ = devServer.Stop() }()
}
// Run integration test
args := []string{"go", "test", "-count", "1", "-race", "-v", "-timeout", "15m"}
env := append(os.Environ(), "DISABLE_SERVER_1_25_TESTS=1")
if *runFlag != "" {
args = append(args, "-run", *runFlag)
}
if *coverageFileFlag != "" {
args = append(args, "-coverprofile="+filepath.Join(b.rootDir, coverageDir, *coverageFileFlag), "-coverpkg=./...")
}
args = append(args, "./...")
if *devServerFlag {
args = append(args, "--", "-using-cli-dev-server")
env = append(env, "TEMPORAL_NAMESPACE=integration-test-namespace")
}
// Must run in test dir
cmd := b.cmdFromRoot(args...)
cmd.Dir = filepath.Join(cmd.Dir, "test")
cmd.Env = env
if err := b.runCmd(cmd); err != nil {
return fmt.Errorf("integration test failed: %w", err)
}
return nil
}
func (b *builder) mergeCoverageFiles() error {
// Only arg should be out file
if len(os.Args) != 3 {
return fmt.Errorf("merge-coverage-files requires single out file")
}
// Basically we make a new file with a "mode:" line header, then write all
// lines from all files except their "mode:" lines
log.Printf("Merging coverage files to %v", os.Args[2])
f, err := os.Create(os.Args[2])
if err != nil {
return err
}
defer f.Close()
if _, err := f.WriteString("mode: atomic\n"); err != nil {
return err
}
coverageDirEntries, err := os.ReadDir(filepath.Join(b.rootDir, coverageDir))
if err != nil {
return fmt.Errorf("failed reading coverage dir: %w", err)
}
for _, entry := range coverageDirEntries {
b, err := os.ReadFile(filepath.Join(b.rootDir, coverageDir, entry.Name()))
if err != nil {
return err
}
for _, line := range bytes.SplitAfter(b, []byte("\n")) {
if !bytes.HasPrefix(line, []byte("mode:")) && len(bytes.TrimSpace(line)) > 0 {
if _, err := f.Write(line); err != nil {
return err
}
}
}
}
return nil
}
func (b *builder) unitTest() error {
// Supports some flags
flagSet := flag.NewFlagSet("unit-test", flag.ContinueOnError)
runFlag := flagSet.String("run", "", "Passed to go test as -run")
coverageFlag := flagSet.Bool("coverage", false, "If set, enables coverage output")
if err := flagSet.Parse(os.Args[2:]); err != nil {
return fmt.Errorf("failed parsing flags: %w", err)
}
// Find every non ./test-prefixed package that has a test file
testDirMap := map[string]struct{}{}
var testDirs []string
err := fs.WalkDir(os.DirFS(b.rootDir), ".", func(p string, d fs.DirEntry, err error) error {
if (!strings.HasPrefix(p, "test") || strings.HasPrefix(p, "testsuite")) && strings.HasSuffix(p, "_test.go") {
dir := path.Dir(p)
if _, ok := testDirMap[dir]; !ok {
testDirMap[dir] = struct{}{}
testDirs = append(testDirs, dir)
}
}
return nil
})
if err != nil {
return fmt.Errorf("failed walking test dirs: %w", err)
}
sort.Strings(testDirs)
// Create coverage dir if doing coverage
if *coverageFlag {
if err := os.MkdirAll(filepath.Join(b.rootDir, coverageDir), 0777); err != nil {
return fmt.Errorf("failed creating coverage dir: %w", err)
}
}
// Run unit test for each dir
log.Printf("Running unit tests in dirs: %v", testDirs)
for _, testDir := range testDirs {
// Run unit test
args := []string{"go", "test", "-count", "1", "-race", "-v", "-timeout", "15m"}
if *runFlag != "" {
args = append(args, "-run", *runFlag)
}
if *coverageFlag {
args = append(
args,
"-coverprofile="+filepath.Join(b.rootDir, coverageDir, "unit-test-"+strings.ReplaceAll(testDir, "/", "-")+".out"),
"-coverpkg=./...",
)
}
args = append(args, ".")
cmd := b.cmdFromRoot(args...)
// Need to run inside directory
cmd.Dir = filepath.Join(b.rootDir, testDir)
if err := b.runCmd(cmd); err != nil {
return fmt.Errorf("unit test failed in %v: %w", testDir, err)
}
}
return nil
}
func (b *builder) cmdFromRoot(args ...string) *exec.Cmd {
cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = b.rootDir
return cmd
}
// Forwards stdout/stderr
func (b *builder) runCmd(cmd *exec.Cmd) error {
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
log.Printf("Running %v in %v with args %v", cmd.Path, cmd.Dir, cmd.Args[1:])
return cmd.Run()
}
func (b *builder) getInstalledTool(modPath string) (string, error) {
// Install
log.Printf("Installing %v", modPath)
cmd := exec.Command("go", "install", modPath)
cmd.Dir = b.thisDir
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("failed installing %q: %w", modPath, err)
}
// Get path to installed
cmd = exec.Command("go", "list", "-f", "{{.Target}}", modPath)
cmd.Dir = b.thisDir
out, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("failed listing path for tool %q", modPath)
}
file := strings.TrimSpace(string(out))
if file == "" {
return "", fmt.Errorf("cannot find target for tool %q", modPath)
} else if _, err := os.Stat(file); err != nil {
return "", fmt.Errorf("cannot stat %q for tool %q: %w", file, modPath, err)
}
return file, nil
}