-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathmain.go
354 lines (329 loc) · 10.9 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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package main
import (
"context"
"encoding/xml"
"flag"
"fmt"
"log"
"os"
"regexp"
"strings"
"cloud.google.com/go/storage"
"github.com/GoogleCloudPlatform/compute-daisy/compute"
"github.com/GoogleCloudPlatform/guest-test-infra/imagetest"
"github.com/GoogleCloudPlatform/guest-test-infra/imagetest/test_suites/cvm"
"github.com/GoogleCloudPlatform/guest-test-infra/imagetest/test_suites/disk"
"github.com/GoogleCloudPlatform/guest-test-infra/imagetest/test_suites/guestagent"
"github.com/GoogleCloudPlatform/guest-test-infra/imagetest/test_suites/hostnamevalidation"
"github.com/GoogleCloudPlatform/guest-test-infra/imagetest/test_suites/hotattach"
"github.com/GoogleCloudPlatform/guest-test-infra/imagetest/test_suites/imageboot"
"github.com/GoogleCloudPlatform/guest-test-infra/imagetest/test_suites/licensevalidation"
"github.com/GoogleCloudPlatform/guest-test-infra/imagetest/test_suites/livemigrate"
"github.com/GoogleCloudPlatform/guest-test-infra/imagetest/test_suites/loadbalancer"
"github.com/GoogleCloudPlatform/guest-test-infra/imagetest/test_suites/mdsmtls"
"github.com/GoogleCloudPlatform/guest-test-infra/imagetest/test_suites/metadata"
"github.com/GoogleCloudPlatform/guest-test-infra/imagetest/test_suites/network"
"github.com/GoogleCloudPlatform/guest-test-infra/imagetest/test_suites/networkperf"
"github.com/GoogleCloudPlatform/guest-test-infra/imagetest/test_suites/oslogin"
"github.com/GoogleCloudPlatform/guest-test-infra/imagetest/test_suites/packagevalidation"
"github.com/GoogleCloudPlatform/guest-test-infra/imagetest/test_suites/security"
"github.com/GoogleCloudPlatform/guest-test-infra/imagetest/test_suites/shapevalidation"
"github.com/GoogleCloudPlatform/guest-test-infra/imagetest/test_suites/sql"
"github.com/GoogleCloudPlatform/guest-test-infra/imagetest/test_suites/ssh"
"github.com/GoogleCloudPlatform/guest-test-infra/imagetest/test_suites/storageperf"
"github.com/GoogleCloudPlatform/guest-test-infra/imagetest/test_suites/suspendresume"
"github.com/GoogleCloudPlatform/guest-test-infra/imagetest/test_suites/windowscontainers"
"github.com/GoogleCloudPlatform/guest-test-infra/imagetest/test_suites/winrm"
"google.golang.org/api/option"
)
var (
project = flag.String("project", "", "project to use for test runner")
testProjects = flag.String("test_projects", "", "comma separated list of projects to be used for tests. defaults to the test runner project")
zone = flag.String("zone", "us-central1-a", "zone to be used for tests")
printwf = flag.Bool("print", false, "print out the parsed test workflows and exit")
validate = flag.Bool("validate", false, "validate all the test workflows and exit")
outPath = flag.String("out_path", "junit.xml", "junit xml path")
gcsPath = flag.String("gcs_path", "", "GCS Path for Daisy working directory")
localPath = flag.String("local_path", "", "path where test output files are stored, can be modified for local testing")
images = flag.String("images", "", "comma separated list of images to test")
timeout = flag.String("timeout", "45m", "timeout for the test suite")
computeEndpointOverride = flag.String("compute_endpoint_override", "", "compute client endpoint override")
parallelCount = flag.Int("parallel_count", 5, "TestParallelCount")
parallelStagger = flag.String("parallel_stagger", "60s", "parseable time.Duration to stagger each parallel test")
filter = flag.String("filter", "", "only run tests matching filter")
exclude = flag.String("exclude", "", "skip tests matching filter")
machineType = flag.String("machine_type", "", "deprecated, use -x86_shape and/or -arm64_shape instead")
x86Shape = flag.String("x86_shape", "n1-standard-1", "default x86(-32 and -64) vm shape for tests not requiring a specific shape")
arm64Shape = flag.String("arm64_shape", "t2a-standard-1", "default arm64 vm shape for tests not requiring a specific shape")
setExitStatus = flag.Bool("set_exit_status", true, "Exit with non-zero exit code if test suites are failing")
)
var (
projectMap = map[string]string{
"almalinux": "almalinux-cloud",
"centos": "centos-cloud",
"cos": "cos-cloud",
"debian": "debian-cloud",
"fedora-cloud": "fedora-cloud",
"fedora-coreos": "fedora-coreos-cloud",
"opensuse": "opensuse-cloud",
"rhel": "rhel-cloud",
"rhel-sap": "rhel-sap-cloud",
"rocky-linux": "rocky-linux-cloud",
"sles": "suse-cloud",
"sles-sap": "suse-sap-cloud",
"sql-": "windows-sql-cloud",
"ubuntu": "ubuntu-os-cloud",
"ubuntu-pro": "ubuntu-os-pro-cloud",
"windows": "windows-cloud",
}
)
type logWriter struct {
log *log.Logger
}
func (l *logWriter) Write(b []byte) (int, error) {
l.log.Print(string(b))
return len(b), nil
}
func main() {
flag.Parse()
if *project == "" || *zone == "" || *images == "" {
log.Fatal("Must provide project, zone and images arguments")
return
}
var testProjectsReal []string
if *testProjects == "" {
testProjectsReal = append(testProjectsReal, *project)
} else {
testProjectsReal = strings.Split(*testProjects, ",")
}
log.Printf("Running in project %s zone %s. Tests will run in projects: %s", *project, *zone, testProjectsReal)
if *gcsPath != "" {
log.Printf("gcs_path set to %s", *gcsPath)
}
var filterRegex *regexp.Regexp
if *filter != "" {
var err error
filterRegex, err = regexp.Compile(*filter)
if err != nil {
log.Fatal("-filter flag not valid:", err)
}
log.Printf("using -filter %s", *filter)
}
var excludeRegex *regexp.Regexp
if *exclude != "" {
var err error
excludeRegex, err = regexp.Compile(*exclude)
if err != nil {
log.Fatal("-exclude flag not valid:", err)
}
log.Printf("using -exclude %s", *exclude)
}
if *machineType != "" {
log.Printf("The -machine_type flag is deprecated, please use -x86_shape and -arm64_shape instead. Retaining legacy behavior while this is set.")
*x86Shape = *machineType
*arm64Shape = *machineType
}
// Setup tests.
testPackages := []struct {
name string
setupFunc func(*imagetest.TestWorkflow) error
}{
{
cvm.Name,
cvm.TestSetup,
},
{
livemigrate.Name,
livemigrate.TestSetup,
},
{
suspendresume.Name,
suspendresume.TestSetup,
},
{
networkperf.Name,
networkperf.TestSetup,
},
{
loadbalancer.Name,
loadbalancer.TestSetup,
},
{
guestagent.Name,
guestagent.TestSetup,
},
{
hostnamevalidation.Name,
hostnamevalidation.TestSetup,
},
{
imageboot.Name,
imageboot.TestSetup,
},
{
licensevalidation.Name,
licensevalidation.TestSetup,
},
{
network.Name,
network.TestSetup,
},
{
security.Name,
security.TestSetup,
},
{
hotattach.Name,
hotattach.TestSetup,
},
{
disk.Name,
disk.TestSetup,
},
{
shapevalidation.Name,
shapevalidation.TestSetup,
},
{
packagevalidation.Name,
packagevalidation.TestSetup,
},
{
storageperf.Name,
storageperf.TestSetup,
},
{
ssh.Name,
ssh.TestSetup,
},
{
winrm.Name,
winrm.TestSetup,
},
{
sql.Name,
sql.TestSetup,
},
{
metadata.Name,
metadata.TestSetup,
},
{
oslogin.Name,
oslogin.TestSetup,
},
{
mdsmtls.Name,
mdsmtls.TestSetup,
},
{
windowscontainers.Name,
windowscontainers.TestSetup,
},
}
ctx := context.Background()
var computeclient compute.Client
var err error
if *computeEndpointOverride != "" {
log.Printf("Using compute endpoint %q", *computeEndpointOverride)
computeclient, err = compute.NewClient(ctx, option.WithEndpoint(*computeEndpointOverride))
} else {
computeclient, err = compute.NewClient(ctx)
}
if err != nil {
log.Fatalf("Could not create compute client:%v", err)
}
var testWorkflows []*imagetest.TestWorkflow
for _, testPackage := range testPackages {
if filterRegex != nil && !filterRegex.MatchString(testPackage.name) {
continue
}
if excludeRegex != nil && excludeRegex.MatchString(testPackage.name) {
continue
}
for _, image := range strings.Split(*images, ",") {
if !strings.Contains(image, "/") {
// Find the project of the image.
project := ""
for k := range projectMap {
if strings.Contains(k, "sap") {
// sap follows a slightly different naming convention.
imageName := strings.Split(k, "-")[0]
if strings.HasPrefix(image, imageName) && strings.Contains(image, "sap") {
project = projectMap[k]
break
}
}
if strings.HasPrefix(image, k) {
project = projectMap[k]
break
}
}
if project == "" {
log.Fatalf("unknown image %s", image)
}
// Check whether the image is an image family or a specific image version.
isMatch, err := regexp.MatchString(".*v([0-9]+)", image)
if err != nil {
log.Fatalf("failed regex: %v", err)
}
if isMatch {
image = fmt.Sprintf("projects/%s/global/images/%s", project, image)
} else {
image = fmt.Sprintf("projects/%s/global/images/family/%s", project, image)
}
}
log.Printf("Add test workflow for test %s on image %s", testPackage.name, image)
test, err := imagetest.NewTestWorkflow(computeclient, *computeEndpointOverride, testPackage.name, image, *timeout, *project, *zone, *x86Shape, *arm64Shape)
if err != nil {
log.Fatalf("Failed to create test workflow: %v", err)
}
testWorkflows = append(testWorkflows, test)
if err := testPackage.setupFunc(test); err != nil {
log.Fatalf("%s.TestSetup for %s failed: %v", testPackage.name, image, err)
}
}
}
if len(testWorkflows) == 0 {
log.Fatalf("No workflows to run!")
}
log.Println("Done with setup")
storageclient, err := storage.NewClient(ctx)
if err != nil {
log.Fatalf("failed to set up storage client: %v", err)
}
if *printwf {
imagetest.PrintTests(ctx, storageclient, testWorkflows, *project, *zone, *gcsPath, *localPath)
return
}
if *validate {
if err := imagetest.ValidateTests(ctx, storageclient, testWorkflows, *project, *zone, *gcsPath, *localPath); err != nil {
log.Printf("Validate failed: %v\n", err)
}
return
}
suites, err := imagetest.RunTests(ctx, storageclient, testWorkflows, *project, *zone, *gcsPath, *localPath, *parallelCount, *parallelStagger, testProjectsReal)
if err != nil {
log.Fatalf("Failed to run tests: %v", err)
}
bytes, err := xml.MarshalIndent(suites, "", "\t")
if err != nil {
log.Fatalf("failed to marshall result: %v", err)
}
var outFile *os.File
if artifacts := os.Getenv("ARTIFACTS"); artifacts != "" {
outFile, err = os.Create(artifacts + "/junit.xml")
} else {
outFile, err = os.Create(*outPath)
}
if err != nil {
log.Fatalf("failed to create output file: %v", err)
}
defer outFile.Close()
outFile.Write(bytes)
outFile.Write([]byte{'\n'})
fmt.Printf("%s\n", bytes)
if *setExitStatus && (suites.Errors != 0 || suites.Failures != 0) {
log.Fatalf("test suite has error or failure")
}
}