-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
221 lines (193 loc) · 5.66 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
// Copyright 2023 Dolthub, Inc.
//
// 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"
"os/exec"
"path/filepath"
"strings"
"github.com/schollz/progressbar/v3"
"golang.org/x/sync/errgroup"
"github.com/dolthub/lambdabats/wire"
)
const S3BucketName = "dolt-cloud-test-run-artifacts"
const LambdaFunctionName = "dolt_bats_test_runner"
var OutputResults = OutputBatsResults
var OutputFormat = flag.String("F", "pretty", "format the test results output; either bats pretty format or tap")
var ExecutionStrategy = flag.String("s", "lambda", "execution strategy;\n lambda - run most tests remote, some locally;\n lambda_skip - run most tests remote, skip others;\n lambda_emulator - run all tests against a local lambda simulator")
var EnvCreds = flag.Bool("use-aws-environment-credentials", false, "by default we use hard-coded credentials which work for DoltHub developers; this uses credentials from the environment instead.")
var EnvVars []string
func PrintUsage() {
fmt.Println("usage: lambda-bats [-F pretty|tap] [-s lambda|lambda_skip|lambda_emulator] BATS_DIR_OR_FILES...")
fmt.Println("usage: lambda-bats login - SSO login to AWS as a developer. Must have AWS CLI installed.")
os.Exit(1)
}
func GetDoltSrcDir(args []string) (string, error) {
path := args[0]
f, err := os.Open(path)
if err != nil {
return "", err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return "", err
}
var doltDirPath string
if fi.IsDir() {
doltDirPath = filepath.Join(path, "../../")
} else {
doltDirPath = filepath.Join(filepath.Dir(path), "../../")
}
d, err := os.Open(doltDirPath)
if err != nil {
return "", err
}
defer d.Close()
fi, err = d.Stat()
if err != nil {
return "", err
}
if !fi.IsDir() {
return "", fmt.Errorf("could not find dolt src directory from first file argument: %s", path)
}
return doltDirPath, nil
}
func main() {
if len(os.Args) > 1 && os.Args[1] == "login" {
os.Exit(DoLogin())
}
flag.Func("env", "environment variable to set in the remote invocation; for example -env SQL_ENGINE=remote-engine", func(val string) error {
if !strings.Contains(val, "=") {
return fmt.Errorf("error, expected -env value to contain a variable setting such as ENVVAR=VALUE, got: %v", val)
}
EnvVars = append(EnvVars, val)
return nil
})
flag.Parse()
if *OutputFormat != "pretty" && *OutputFormat != "tap" {
fmt.Println("invalid output format")
PrintUsage()
} else if *OutputFormat == "tap" {
OutputResults = OutputTAPResults
}
if *ExecutionStrategy != "lambda" && *ExecutionStrategy != "lambda_skip" && *ExecutionStrategy != "lambda_emulator" {
fmt.Println("invalid execution strategy")
PrintUsage()
}
fileArgs := flag.Args()
if len(fileArgs) == 0 {
fmt.Println("must supply tests to run")
PrintUsage()
}
doltSrcDir, err := GetDoltSrcDir(fileArgs)
if err != nil {
fmt.Printf("could not find dolt source directory: %v\n", err)
PrintUsage()
}
ctx := context.Background()
var config RunConfig
var fallbackRunner Runner
switch *ExecutionStrategy {
case "lambda":
config, err = NewAWSRunConfig(ctx, *EnvCreds)
if err != nil {
panic(err)
}
fallbackRunner = NewLocalRunner(filepath.Join(doltSrcDir, "integration-tests/bats"))
case "lambda_skip":
config, err = NewAWSRunConfig(ctx, *EnvCreds)
if err != nil {
panic(err)
}
fallbackRunner = NewSkipRunner("lambda runner does not support virtual ttys")
case "lambda_emulator":
config = NewTestRunConfig()
}
testArtifacts, err := UploadTests(ctx, config.Uploader, doltSrcDir)
if err != nil {
panic(err)
}
files, total, err := LoadTestFiles(fileArgs)
if err != nil {
panic(err)
}
eg, egCtx := errgroup.WithContext(ctx)
eg.SetLimit(config.Concurrency)
bar := progressbar.Default(int64(total), "running tests")
RunTest := func(fi, ti int) {
eg.Go(func() error {
filter := EscapeNameForFilter(files[fi].Tests[ti].Name)
req := wire.RunTestRequest{
DoltLocation: testArtifacts.DoltPath,
BinLocation: testArtifacts.BinPath,
BatsLocation: testArtifacts.TestsPath,
FileName: files[fi].Name,
TestName: files[fi].Tests[ti].Name,
TestFilter: filter,
EnvVars: EnvVars,
}
runner := config.Runner
if files[fi].Tests[ti].HasTag("no_lambda") {
runner = fallbackRunner
}
resp, err := runner.Run(egCtx, req)
if err != nil {
return err
}
bar.Add(1)
files[fi].Tests[ti].Runs = append(files[fi].Tests[ti].Runs, TestRun{
Response: resp,
})
return nil
})
}
// Run all the tests...
for fi := range files {
for ti := range files[fi].Tests {
RunTest(fi, ti)
}
}
err = eg.Wait()
if err != nil {
panic(err)
}
bar.Finish()
bar.Close()
// Print the results...
res := OutputResults(files)
os.Exit(res)
}
func DoLogin() int {
err := WithAWSConfig(func(path string) error {
cmd := exec.Command("aws")
cmd.Args = []string{
"aws", "sso", "login", "--sso-session", "dolthub_sso_session",
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "AWS_CONFIG_FILE="+path)
err := cmd.Run()
return err
})
if err != nil {
fmt.Printf("error running `aws sso login`: %v", err)
return 1
}
return 0
}