-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
pytest.go
339 lines (292 loc) · 10.3 KB
/
pytest.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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you 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 mage
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
// WINDOWS USERS:
// The python installer does not create a python3 alias like it does on other
// platforms. So do verify the version with python.exe --version.
//
// Setting up a python virtual environment on a network drive does not work
// well. So if this applies to your development environment set PYTHON_ENV
// to point to somewhere on C:\.
const (
libbeatRequirements = "{{ elastic_beats_dir}}/libbeat/tests/system/requirements.txt"
aixLibbeatRequirements = "{{ elastic_beats_dir}}/libbeat/tests/system/requirements_aix.txt"
)
var (
// VirtualenvReqs specifies a list of virtualenv requirements files to be
// used when calling PythonVirtualenv(). It defaults to the libbeat
// requirements.txt file.
VirtualenvReqs = []string{
libbeatRequirements,
}
pythonVirtualenvDir string // Location of python virtualenv (lazily set).
pythonVirtualenvLock sync.Mutex
// More globs may be needed in the future if tests are added in more places.
pythonTestFiles = []string{
"tests/system/test_*.py",
"module/*/test_*.py",
"module/*/*/test_*.py",
}
// pythonExe points to the python executable to use. The PYTHON_EXE
// environment can be used to modify the executable used.
// On Windows this defaults to python and on all other platforms this
// defaults to python3.
pythonExe = EnvOr("PYTHON_EXE", "python3")
)
func init() {
// The python installer for Windows does not setup a python3 alias.
if runtime.GOOS == "windows" {
pythonExe = EnvOr("PYTHON_EXE", "python")
}
}
// PythonTestArgs are the arguments used for the "python*Test" targets and they
// define how python tests are invoked.
type PythonTestArgs struct {
TestName string // Test name used in logging.
Env map[string]string // Env vars to add to the current env.
Files []string // Globs used to find tests.
XUnitReportFile string // File to write the XUnit XML test report to.
CoverageProfileFile string // Test coverage profile file.
}
func makePythonTestArgs(name string) PythonTestArgs {
fileName := fmt.Sprintf("build/TEST-python-%s", strings.Replace(strings.ToLower(name), " ", "_", -1))
params := PythonTestArgs{
TestName: name,
Env: map[string]string{},
XUnitReportFile: fileName + ".xml",
}
if TestCoverage {
params.CoverageProfileFile = fileName + ".cov"
}
return params
}
// DefaultPythonTestUnitArgs returns a default set of arguments for running
// all unit tests.
func DefaultPythonTestUnitArgs() PythonTestArgs { return makePythonTestArgs("Unit") }
// DefaultPythonTestIntegrationArgs returns a default set of arguments for
// running all integration tests. Integration tests are made conditional by
// checking for INTEGRATION_TEST=1 in the test code.
func DefaultPythonTestIntegrationArgs() PythonTestArgs { return makePythonTestArgs("Integration") }
// PythonTest executes python tests via a Python virtualenv.
func PythonTest(params PythonTestArgs) error {
fmt.Println(">> python test:", params.TestName, "Testing")
ve, err := PythonVirtualenv()
if err != nil {
return err
}
pytestEnv := map[string]string{
// activate sets this. Not sure if it's ever needed.
"VIRTUAL_ENV": ve,
}
if IsInIntegTestEnv() {
pytestEnv["INTEGRATION_TESTS"] = "1"
}
for k, v := range params.Env {
pytestEnv[k] = v
}
pytestOptions := []string{
"--timeout=90",
"--durations=20",
// Enable -x to stop at the first failing test
// "-x",
// Enable --tb=long to produce long tracebacks
//"--tb=long",
// Enable -v to produce verbose output
//"-v",
// Don't capture test output
//"-s",
}
if mg.Verbose() {
pytestOptions = append(pytestOptions, "-v")
}
if params.XUnitReportFile != "" {
pytestOptions = append(pytestOptions,
"--junit-xml="+createDir(params.XUnitReportFile),
)
}
files := pythonTestFiles
if len(params.Files) > 0 {
files = params.Files
}
testFiles, err := FindFiles(files...)
if err != nil {
return err
}
if len(testFiles) == 0 {
fmt.Println(">> python test:", params.TestName, "Testing - No tests found.")
return nil
}
// We check both the VE and the normal PATH because on Windows if the
// requirements are met by the globally installed package they are not
// installed to the VE.
pytestPath, err := LookVirtualenvPath(ve, "pytest")
if err != nil {
return err
}
defer fmt.Println(">> python test:", params.TestName, "Testing Complete")
_, err = sh.Exec(pytestEnv, os.Stdout, os.Stderr, pytestPath, append(pytestOptions, testFiles...)...)
return err
// TODO: Aggregate all the individual code coverage reports and generate
// and HTML report.
}
// PythonTestForModule executes python system tests for modules.
//
// Use `MODULE=module` to run only tests for `module`.
func PythonTestForModule(params PythonTestArgs) error {
if module := EnvOr("MODULE", ""); module != "" {
fmt.Println(">> Single module selected for testing: ", module)
params.Files = []string{
fmt.Sprintf("module/%s/test_*.py", module),
fmt.Sprintf("module/%s/*/test_*.py", module),
// Run always the base tests, that include tests for module dashboards.
"tests/system/test*_base.py",
}
fmt.Println("Test files: ", params.Files)
params.TestName += "-" + module
} else {
fmt.Println(">> Running tests for all modules, you can use MODULE=foo to scope it down to a single module...")
}
return PythonTest(params)
}
// PythonVirtualenv constructs a virtualenv that contains the given modules as
// defined in the requirements file pointed to by requirementsTxt. It returns
// the path to the virtualenv.
func PythonVirtualenv() (string, error) {
pythonVirtualenvLock.Lock()
defer pythonVirtualenvLock.Unlock()
// Certain docker requirements simply won't build on AIX
// Skipping them here will obviously break the components that require docker-compose,
// But at least the components that don't require it will still run
if runtime.GOOS == "aix" {
VirtualenvReqs[0] = aixLibbeatRequirements
}
// Determine the location of the virtualenv.
ve, err := pythonVirtualenvPath()
if err != nil {
return "", err
}
reqs := expandVirtualenvReqs()
// Only execute if requirements.txt is newer than the virtualenv activate
// script.
activate := virtualenvPath(ve, "activate")
if IsUpToDate(activate, reqs...) {
return pythonVirtualenvDir, nil
}
// Create a virtual environment only if the dir does not exist.
if _, err := os.Stat(ve); err != nil {
if err := sh.Run(pythonExe, "-m", "venv", ve); err != nil {
return "", err
}
}
// activate sets this. Not sure if it's ever needed.
env := map[string]string{
"VIRTUAL_ENV": ve,
}
pip := virtualenvPath(ve, "pip")
pipUpgrade := func(pkg string) error {
return sh.RunWith(env, pip, "install", "-U", pkg)
}
// Ensure we are using the latest pip version.
if err = pipUpgrade("pip"); err != nil {
fmt.Printf("warn: failed to upgrade pip (ignoring): %v", err)
}
// First ensure that wheel is installed so that bdists build cleanly.
if err = pipUpgrade("wheel"); err != nil {
return "", err
}
// Execute pip to install the dependencies.
args := []string{"install"}
if !mg.Verbose() {
args = append(args, "--quiet")
}
for _, req := range reqs {
args = append(args, "-Ur", req)
}
if err := sh.RunWith(env, pip, args...); err != nil {
return "", err
}
// Touch activate script.
mtime := time.Now()
if err := os.Chtimes(activate, mtime, mtime); err != nil {
log.Fatal(err)
}
return ve, nil
}
// pythonVirtualenvPath determines the location of the Python virtualenv.
func pythonVirtualenvPath() (string, error) {
if pythonVirtualenvDir != "" {
return pythonVirtualenvDir, nil
}
// PYTHON_ENV can override the default location. This is used by CI to
// shorten the overall shebang interpreter path below the path length limits.
pythonVirtualenvDir = os.Getenv("PYTHON_ENV")
if pythonVirtualenvDir == "" {
info, err := GetProjectRepoInfo()
if err != nil {
return "", err
}
pythonVirtualenvDir = info.RootDir
}
pythonVirtualenvDir = filepath.Join(pythonVirtualenvDir, "build/ve")
// Use OS and docker specific virtualenv's because the interpreter in
// scripts is different.
if IsInIntegTestEnv() {
pythonVirtualenvDir = filepath.Join(pythonVirtualenvDir, "docker")
} else {
pythonVirtualenvDir = filepath.Join(pythonVirtualenvDir, runtime.GOOS)
}
return pythonVirtualenvDir, nil
}
// virtualenvPath builds the path to a binary (in the OS specific binary path).
func virtualenvPath(ve string, parts ...string) string {
if runtime.GOOS == "windows" {
return filepath.Join(append([]string{ve, "Scripts"}, parts...)...)
}
return filepath.Join(append([]string{ve, "bin"}, parts...)...)
}
// LookVirtualenvPath looks for an executable in the path and it includes the
// virtualenv in the search.
func LookVirtualenvPath(ve, file string) (string, error) {
// This is kind of unsafe w.r.t. concurrent execs because they could end
// up with different PATHs. But it allows us to search the VE path without
// having to re-implement the exec.LookPath logic. And does not require us
// to "deactivate" the virtualenv because we never activated it.
path := os.Getenv("PATH")
os.Setenv("PATH", virtualenvPath(ve)+string(filepath.ListSeparator)+path)
defer os.Setenv("PATH", path)
return exec.LookPath(file)
}
func expandVirtualenvReqs() []string {
out := make([]string, 0, len(VirtualenvReqs))
for _, path := range VirtualenvReqs {
out = append(out, MustExpand(path))
}
return out
}