-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
tester.py
184 lines (162 loc) · 4.61 KB
/
tester.py
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
import os
import sys
from typing import List, Optional
import yaml
import click
from ci.ray_ci.container import _DOCKER_ECR_REPO
from ci.ray_ci.tester_container import TesterContainer
from ci.ray_ci.utils import docker_login
# Gets the path of product/tools/docker (i.e. the parent of 'common')
bazel_workspace_dir = os.environ.get("BUILD_WORKSPACE_DIRECTORY", "")
@click.command()
@click.argument("targets", required=True, type=str, nargs=-1)
@click.argument("team", required=True, type=str, nargs=1)
@click.option(
"--workers",
default=1,
type=int,
help=("Number of concurrent test jobs to run."),
)
@click.option(
"--worker-id",
default=0,
type=int,
help=("Index of the concurrent shard to run."),
)
@click.option(
"--parallelism-per-worker",
default=1,
type=int,
help=("Number of concurrent test jobs to run per worker."),
)
@click.option(
"--except-tags",
default="",
type=str,
help=("Except tests with the given tags."),
)
@click.option(
"--only-tags",
default="",
type=str,
help=("Only include tests with the given tags."),
)
@click.option(
"--run-flaky-tests",
is_flag=True,
show_default=True,
default=False,
help=("Run flaky tests."),
)
@click.option(
"--test-env",
multiple=True,
type=str,
help="Environment variables to set for the test.",
)
@click.option(
"--build-name",
type=str,
help="Name of the build used to run tests",
)
def main(
targets: List[str],
team: str,
workers: int,
worker_id: int,
parallelism_per_worker: int,
except_tags: str,
only_tags: str,
run_flaky_tests: bool,
test_env: List[str],
build_name: Optional[str],
) -> None:
if not bazel_workspace_dir:
raise Exception("Please use `bazelisk run //ci/ray_ci`")
os.chdir(bazel_workspace_dir)
docker_login(_DOCKER_ECR_REPO.split("/")[0])
container = _get_container(
team, workers, worker_id, parallelism_per_worker, build_name
)
if run_flaky_tests:
test_targets = _get_flaky_test_targets(team)
else:
test_targets = _get_test_targets(
container,
targets,
team,
except_tags,
only_tags,
)
success = container.run_tests(test_targets, test_env)
sys.exit(0 if success else 1)
def _get_container(
team: str,
workers: int,
worker_id: int,
parallelism_per_worker: int,
build_name: Optional[str] = None,
) -> TesterContainer:
shard_count = workers * parallelism_per_worker
shard_start = worker_id * parallelism_per_worker
shard_end = (worker_id + 1) * parallelism_per_worker
return TesterContainer(
build_name or f"{team}build",
shard_count=shard_count,
shard_ids=list(range(shard_start, shard_end)),
)
def _get_all_test_query(
targets: List[str],
team: str,
except_tags: Optional[str] = None,
only_tags: Optional[str] = None,
) -> str:
"""
Get all test targets that are owned by a particular team, except those that
have the given tags
"""
test_query = " union ".join([f"tests({target})" for target in targets])
query = f"attr(tags, 'team:{team}\\\\b', {test_query})"
if except_tags:
except_query = " union ".join(
[f"attr(tags, {t}, {test_query})" for t in except_tags.split(",")]
)
query = f"{query} except ({except_query})"
if only_tags:
only_query = " union ".join(
[f"attr(tags, {t}, {test_query})" for t in only_tags.split(",")]
)
query = f"{query} intersect ({only_query})"
return query
def _get_test_targets(
container: TesterContainer,
targets: str,
team: str,
except_tags: Optional[str] = "",
only_tags: Optional[str] = "",
yaml_dir: Optional[str] = None,
) -> List[str]:
"""
Get all test targets that are not flaky
"""
query = _get_all_test_query(targets, team, except_tags, only_tags)
test_targets = (
container.run_script_with_output(
[
f'bazel query "{query}"',
]
)
.decode("utf-8")
.split("\n")
)
flaky_tests = _get_flaky_test_targets(team, yaml_dir)
return [test for test in test_targets if test and test not in flaky_tests]
def _get_flaky_test_targets(team: str, yaml_dir: Optional[str] = None) -> List[str]:
"""
Get all test targets that are flaky
"""
if not yaml_dir:
yaml_dir = os.path.join(bazel_workspace_dir, "ci/ray_ci")
with open(f"{yaml_dir}/{team}.tests.yml", "rb") as f:
flaky_tests = yaml.safe_load(f)["flaky_tests"]
return flaky_tests