forked from vanadium-archive/go.devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
presubmit.go
213 lines (189 loc) · 5.93 KB
/
presubmit.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
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package test
import (
"fmt"
"os"
"path/filepath"
"v.io/jiri"
"v.io/jiri/collect"
"v.io/x/devtools/internal/test"
"v.io/x/devtools/internal/xunit"
)
var (
jenkinsHost = "http://localhost:8001/jenkins"
)
// requireEnv makes sure that the given environment variables are set.
func requireEnv(names []string) error {
for _, name := range names {
if os.Getenv(name) == "" {
return fmt.Errorf("environment variable %q is not set", name)
}
}
return nil
}
// vanadiumPresubmitPoll polls vanadium projects for new patchsets for
// which to run presubmit tests.
func vanadiumPresubmitPoll(jirix *jiri.X, testName string, _ ...Opt) (_ *test.Result, e error) {
// Initialize the test.
cleanup, err := initTestImpl(jirix, false, false, false, testName, nil, "")
if err != nil {
return nil, newInternalError(err, "Init")
}
defer collect.Error(func() error { return cleanup() }, &e)
// Use the "presubmit query" command to poll for new changes.
logfile := filepath.Join(jirix.Root, ".presubmit_log")
args := []string{}
if jirix.Verbose() {
args = append(args, "-v")
} else {
// append this for testing this CL only - remove on checkin.
args = append(args, "-v")
}
args = append(args,
"-host", jenkinsHost,
"query",
"-log-file", logfile,
"-manifest", "tools",
)
if err := jirix.NewSeq().Capture(jirix.Stdout(), jirix.Stderr()).Last("presubmit", args...); err != nil {
return nil, err
}
return &test.Result{Status: test.Passed}, nil
}
// vanadiumPresubmitTest runs presubmit tests for a given project specified
// in TEST environment variable.
func vanadiumPresubmitTest(jirix *jiri.X, testName string, opts ...Opt) (_ *test.Result, e error) {
if err := requireEnv([]string{"BUILD_NUMBER", "REFS", "PROJECTS", "TEST", "WORKSPACE"}); err != nil {
return nil, err
}
// Initialize the test.
cleanup, err := initTestImpl(jirix, false, false, false, testName, nil, "")
if err != nil {
return nil, newInternalError(err, "Init")
}
defer collect.Error(func() error { return cleanup() }, &e)
displayProfiles(jirix, "presubmit")
s := jirix.NewSeq()
// Get number of test workers from opts.
numWorkers := 1
for _, opt := range opts {
switch typedOpt := opt.(type) {
case NumWorkersOpt:
numWorkers = int(typedOpt)
break
}
}
// Use the "presubmit test" command to run the presubmit test.
args := []string{}
if jirix.Verbose() {
args = append(args, "-v")
}
name := os.Getenv("TEST")
args = append(args,
"-host", jenkinsHost,
"test",
"-build-number", os.Getenv("BUILD_NUMBER"),
"-manifest", "tools",
"-num-test-workers", fmt.Sprintf("%d", numWorkers),
"-projects", os.Getenv("PROJECTS"),
"-refs", os.Getenv("REFS"),
"-test", name,
)
if err := s.Capture(jirix.Stdout(), jirix.Stderr()).Last("presubmit", args...); err != nil {
return nil, newInternalError(err, "Presubmit")
}
// Remove any test result files that are empty.
testResultFiles, err := findTestResultFiles(jirix, name)
if err != nil {
return nil, err
}
for _, file := range testResultFiles {
fileInfo, err := s.Stat(file)
if err != nil {
return nil, err
}
if fileInfo.Size() == 0 {
if err := s.RemoveAll(file).Done(); err != nil {
return nil, err
}
}
}
return &test.Result{Status: test.Passed}, nil
}
// vanadiumPresubmitResult runs "presubmit result" command to process and post test results.
func vanadiumPresubmitResult(jirix *jiri.X, testName string, _ ...Opt) (_ *test.Result, e error) {
if err := requireEnv([]string{"BUILD_NUMBER", "REFS", "PROJECTS", "WORKSPACE"}); err != nil {
return nil, err
}
// Initialize the test.
cleanup, err := initTestImpl(jirix, false, false, false, testName, nil, "")
if err != nil {
return nil, newInternalError(err, "Init")
}
defer collect.Error(func() error { return cleanup() }, &e)
// Run "presubmit result".
args := []string{}
if jirix.Verbose() {
args = append(args, "-v")
}
args = append(args,
"-host", jenkinsHost,
"result",
"-build-number", os.Getenv("BUILD_NUMBER"),
"-manifest", "tools",
"-refs", os.Getenv("REFS"),
"-projects", os.Getenv("PROJECTS"),
)
if err := jirix.NewSeq().Capture(jirix.Stdout(), jirix.Stderr()).Last("presubmit", args...); err != nil {
return nil, err
}
return &test.Result{Status: test.Passed}, nil
}
// TestPresubmitTestOpt is an option used in testPresubmitTest for checking
// the content of a file.
type TestPresubmitTestOpt struct {
FilePaths []string
ExpectedFileContents []string
}
func (TestPresubmitTestOpt) Opt() {}
// testPresubmitTest is used to test the "test" phase of presubmit.
// It reads a set of given files and compares their contents with the given
// expected contents. The test will pass when the file contents can be read and
// correctly matched.
func testPresubmitTest(jirix *jiri.X, testName string, opts ...Opt) (*test.Result, error) {
testData := TestPresubmitTestOpt{}
gotTestData := false
for _, opt := range opts {
switch typedOpt := opt.(type) {
case TestPresubmitTestOpt:
testData = TestPresubmitTestOpt(typedOpt)
gotTestData = true
}
}
if !gotTestData {
return nil, fmt.Errorf("TestPresubmitTestOpt not found")
}
status := test.Passed
errMsg := "Failed to match file content"
if len(testData.FilePaths) != len(testData.ExpectedFileContents) {
status = test.Failed
if err := xunit.CreateFailureReport(jirix, testName, "MockTest", "MockTest", errMsg, ""); err != nil {
return nil, err
}
} else {
for i, path := range testData.FilePaths {
expectedContent := testData.ExpectedFileContents[i]
content, err := jirix.NewSeq().ReadFile(path)
if err != nil || expectedContent != string(content) {
status = test.Failed
if err := xunit.CreateFailureReport(jirix, testName, "MockTest", "MockTest", errMsg, ""); err != nil {
return nil, err
}
break
}
}
}
return &test.Result{Status: status}, nil
}