-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_runner.py
More file actions
282 lines (211 loc) · 8.98 KB
/
test_runner.py
File metadata and controls
282 lines (211 loc) · 8.98 KB
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
import os
import platform
import shutil
import sys
import time
import unittest
from datetime import datetime
from subprocess import check_output, CalledProcessError, STDOUT
from typing import Iterable, Optional, List
from unittest import TestCase
from teamcity import is_running_under_teamcity
from teamcity.unittestpy import TeamcityTestRunner
JAVA_HOME = os.getenv(sys.argv[1]) if len(sys.argv) > 1 else os.getenv("JAVA_HOME")
AGENT_NAME = "memory_agent"
IS_UNDER_TEAMCITY = is_running_under_teamcity()
PROXY_COMPILED_PATH = os.path.join('test_data', 'proxy', 'build') if IS_UNDER_TEAMCITY else None
if JAVA_HOME is None:
print("Java not found. Please specify JAVA_HOME and try again.")
exit(1)
def get_java_executable() -> str:
return os.path.join(JAVA_HOME, 'bin', 'java')
def get_java_compiler() -> str:
return os.path.join(JAVA_HOME, 'bin', 'javac')
def get_java_bitness() -> int:
out = check_output([get_java_executable(), '-version'], stderr=STDOUT).decode('utf-8')
if out.find('64-Bit') != -1:
return 64
return 32
OUTS_DIR = 'outs' if get_java_bitness() == 64 else 'outs32'
def output_file(name: str, directory: Optional[str] = None) -> str:
result = '{}.out'.format(name)
if directory is not None:
result = os.path.join(directory, result)
return result
def dynamic_library_name(lib_name) -> str:
def dynamic_lib_format() -> str:
os_type = platform.system()
os_arch = platform.processor()
if os_type == "Windows":
if get_java_bitness() == 32:
return '{}32.dll'
else:
return '{}.dll'
if os_type == "Darwin":
return 'lib{}.dylib'
if os_type == "Linux" and os_arch == 'aarch64':
return 'lib{}_aarch64.so'
if os_type == "Linux":
return 'lib{}.so'
raise Exception("Unknown OS type")
return dynamic_lib_format().format(lib_name)
def agent_lib_name() -> str:
return dynamic_library_name(AGENT_NAME)
def find_agent_in_project_directory(file_name: str):
result = list()
for root, dirs, files in os.walk('.'):
result.extend((os.path.join(root, file) for file in files if file_name == file))
if len(result) == 0:
raise AssertionError("Agent not found")
if len(result) > 1:
raise AssertionError("Too many agents found: " + str(result))
return result[0]
def find_agent_file() -> str:
library_file_name = agent_lib_name()
if IS_UNDER_TEAMCITY:
return os.path.join('bin', library_file_name)
else:
return find_agent_in_project_directory(library_file_name)
class JavaCompiler:
def __init__(self, javac: str, output_path: str) -> None:
self.__output = output_path
self.__javac = javac
def compile_java(self, source_files: List[str], classpath: str):
args = list()
args.append(self.__javac)
args.extend(['-d', self.__output])
if classpath is not None:
args.extend(['-classpath', classpath])
args.extend(source_files)
check_output(args)
class Test:
def __init__(self, name: str, output: str, src_dir: str) -> None:
self.__name = name
self.__output = output
self.__path = os.path.join(src_dir, name)
def name(self) -> str:
return self.__name
def expected_output(self) -> Optional[str]:
return self.__output
def src_path(self):
return self.__path
class TestResult:
def __init__(self, test: Test, output: str) -> None:
self.__output = output
self.__test = test
def get_output(self) -> str:
return self.__output
def get_test(self) -> Test:
return self.__test
class TestRunner:
def __init__(self, java, build_dir: str, output_dir: str, agent_path: str) -> None:
self.__java = java
self.__build_dir = build_dir
self.__output_directory = output_dir
self.__agent_path = os.path.abspath(agent_path)
def run(self, test: Test) -> TestResult:
if not os.path.exists(self.__output_directory):
os.makedirs(self.__output_directory)
args = list()
args.append(self.__java)
args.append('-agentpath:{}=3'.format(self.__agent_path))
args.extend(['-classpath', self.__build_dir])
args.append(test.name())
out = check_output(args).decode("utf-8").replace('\r\n', '\n')
with open(output_file(test.name(), self.__output_directory), mode='w') as out_file:
out_file.write(out)
return TestResult(test, out)
class TestRepository:
def __init__(self, path: str) -> None:
assert os.path.exists(path), "Test repository is not found"
assert os.path.isdir(path), "Test repository must be a directory"
self.__path = path
self.__ignored_dirs = {'common', 'memory'}
def test_count(self) -> int:
return len(list(self.__iterate_tests_files(self.test_src_dir())))
def read_output(self, name) -> Optional[str]:
try:
with open(output_file(name, self.__test_out_dir()), mode='r') as file:
return file.read()
except IOError:
return None
def write_output(self, name, output):
with open(output_file(name, self.__test_out_dir()), mode='w') as file:
file.write(output)
def iterate_tests(self) -> Iterable[Test]:
src_dir = self.test_src_dir()
for test_name in self.__iterate_tests_files(src_dir):
yield Test(test_name, self.read_output(test_name), src_dir)
def __iterate_tests_files(self, src_dir, package: str = '') -> Iterable[str]:
for file_name in os.listdir(src_dir):
path = os.path.join(src_dir, file_name)
if os.path.isdir(path):
if not self.__is_ignored_dir(file_name):
yield from self.__iterate_tests_files(path, self.__join_with_package(package, file_name))
else:
yield self.__join_with_package(package, str(file_name).split('.java')[0])
def test_src_dir(self) -> str:
return os.path.join(self.__path, 'src')
def get_all_files_for_compilation(self) -> List[str]:
result = self.__list_files_from_src_root(os.path.join(self.__path, 'src'))
if PROXY_COMPILED_PATH is None:
result.extend(self.__list_files_from_src_root(os.path.join(self.__path, 'proxy/src')))
return result
@staticmethod
def __list_files_from_src_root(src_root) -> List[str]:
result = list()
for root, dirs, files in os.walk(src_root):
for file in files:
if file != ".DS_Store":
result.append(os.path.join(root, file))
return result
@staticmethod
def __join_with_package(parent: str, child: str) -> str:
return child if parent == '' else '{}.{}'.format(parent, child)
def __test_out_dir(self) -> str:
return os.path.join(self.__path, OUTS_DIR)
def __is_ignored_dir(self, dir_name: str) -> bool:
return dir_name in self.__ignored_dirs
test_repo = TestRepository('test_data')
timestamp = int(time.time())
timestamp = datetime.fromtimestamp(timestamp).strftime('%Y.%m.%d_%H.%M.%S')
build_directory = 'test_outs/{}/build'.format(timestamp)
output_directory = 'test_outs/{}/outs'.format(timestamp)
class NativeAgentTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
os.makedirs(build_directory)
os.makedirs(output_directory)
JavaCompiler(get_java_compiler(), build_directory) \
.compile_java(test_repo.get_all_files_for_compilation(), PROXY_COMPILED_PATH)
if PROXY_COMPILED_PATH is not None:
shutil.copytree(PROXY_COMPILED_PATH, build_directory, dirs_exist_ok=True)
def to_test_name(value: str) -> str:
return 'test_{}'.format(value.replace('.', '_').replace(' ', '_').lower())
def create_test(test: Test, runner: TestRunner, repo: TestRepository):
def do_test(self: TestCase):
try:
result = runner.run(test)
except CalledProcessError as ex:
self.fail(ex.output)
actual = result.get_output()
expected = test.expected_output()
if expected is not None:
self.assertEqual(expected.strip(), actual.strip(), "outputs are mismatched")
else:
repo.write_output(test.name(), actual)
error_text = "********* EXPECTED OUTPUT NOT FOUND. DO NOT FORGET PUT IT INTO VCS *********"
self.fail(error_text)
return do_test
def create_tests():
runner = TestRunner(get_java_executable(), build_directory, output_directory, find_agent_file())
for test in test_repo.iterate_tests():
setattr(NativeAgentTests, to_test_name(test.name()), create_test(test, runner, test_repo))
if __name__ == '__main__':
create_tests()
if IS_UNDER_TEAMCITY:
runner = TeamcityTestRunner()
else:
runner = unittest.TextTestRunner()
unittest.main(testRunner=runner, argv=None if len(sys.argv) == 1 else [sys.argv[0]] + sys.argv[2:])