forked from neonevm/neon-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclickfile.py
More file actions
executable file
·398 lines (333 loc) · 13.6 KB
/
Copy pathclickfile.py
File metadata and controls
executable file
·398 lines (333 loc) · 13.6 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/usr/bin/env python3
import functools
import glob
import json
import os
import pathlib
import re
import shutil
import subprocess
import sys
import typing as tp
from multiprocessing.dummy import Pool
from urllib.parse import urlparse
import requests
try:
import click
except ImportError:
print("Please install click library: pip install click==8.0.3")
sys.exit(1)
try:
from utils import web3client
from utils import faucet
from utils import cloud
except ImportError:
pass
CMD_ERROR_LOG = "click_cmd_err.log"
ERR_MSG_TPL = {
"blocks": [
{
"type": "section",
"text": {"type": "mrkdwn", "text": ""},
},
{"type": "divider"},
]
}
ERR_MESSAGES = {"run": "Unsuccessful tests executing.", "requirements": "Unsuccessful requirements installation."}
def catch_traceback(func: tp.Callable) -> tp.Callable:
"""Catch traceback to file"""
def create_report(func_name, exc=None):
data = ""
exc = f"\n*Error:* {exc}" if exc else ""
path = pathlib.Path(CMD_ERROR_LOG)
if path.exists() and path.stat().st_size != 0:
with path.open("r") as fd:
data = f"{fd.read()}\n"
path.unlink()
err_msg = f"*{ERR_MESSAGES.get(func_name)}*{exc}\n{data}"
with open(CMD_ERROR_LOG, "w") as fd:
fd.write(err_msg)
@functools.wraps(func)
def wrap(*args, **kwargs) -> tp.Any:
try:
result = func(*args, **kwargs)
except Exception as e:
create_report(func.__name__, e)
raise
finally:
e = sys.exc_info()
if e[0] and e[0].__name__ == "SystemExit" and e[1] != 0:
create_report(func.__name__)
return result
return wrap
networks = []
with open("./envs.json", "r") as f:
networks = json.load(f)
def prepare_wallets_with_balance(network, count=8, airdrop_amount=20000):
print(f"Preparing {count} wallets with balances")
settings = networks[network]
web3_client = web3client.NeonWeb3Client(settings["proxy_url"], settings["network_id"])
faucet_client = faucet.Faucet(settings["faucet_url"])
private_keys = []
for i in range(count):
acc = web3_client.eth.account.create()
faucet_client.request_neon(acc.address, airdrop_amount)
if i == 0:
for _ in range(2):
faucet_client.request_neon(acc.address, airdrop_amount)
private_keys.append(acc.privateKey.hex())
print("All private keys: ", ",".join(private_keys))
return private_keys
def run_openzeppelin_tests(network, jobs=8):
print(f"Running OpenZeppelin tests in {jobs} jobs on {network}")
cwd = (pathlib.Path().parent / "compatibility/openzeppelin-contracts").absolute()
subprocess.check_call("npx hardhat compile", shell=True, cwd=cwd)
(cwd.parent / "results").mkdir(parents=True, exist_ok=True)
keys_env = [prepare_wallets_with_balance(network) for i in range(jobs)]
tests = subprocess.check_output("find \"test\" -name '*.test.js'", shell=True, cwd=cwd).decode().splitlines()
def run_oz_file(file_name):
print(f"Run {file_name}")
keys = keys_env.pop(0)
env = os.environ.copy()
env["PRIVATE_KEYS"] = ",".join(keys)
env["NETWORK_ID"] = str(networks[network]["network_id"])
env["PROXY_URL"] = networks[network]["proxy_url"]
out = subprocess.run(f"npx hardhat test {file_name}", shell=True, cwd=cwd, capture_output=True, env=env)
stdout = out.stdout.decode()
stderr = out.stderr.decode()
print(f"Test {file_name} finished with code {out.returncode}")
print(stdout)
print(stderr)
keys_env.append(keys)
log_dirs = cwd.parent / "results" / file_name.replace(".", "_").replace("/", "_")
log_dirs.mkdir(parents=True, exist_ok=True)
with open(log_dirs / "stdout.log", "w") as f:
f.write(stdout)
with open(log_dirs / "stderr.log", "w") as f:
f.write(stderr)
pool = Pool(jobs)
pool.map(run_oz_file, tests)
pool.close()
pool.join()
# Add allure environment
settings = networks[network]
web3_client = web3client.NeonWeb3Client(settings["proxy_url"], settings["network_id"])
opts = {
"Proxy.Version": web3_client.get_proxy_version()["result"],
"EVM.Version": web3_client.get_evm_version()["result"],
"CLI.Version": web3_client.get_cli_version()["result"],
}
with open("./allure-results/environment.properties", "w+") as f:
f.write("\n".join(map(lambda x: f"{x[0]}={x[1]}", opts.items())))
f.write("\n")
# Add epic name for allure result files
openzeppelin_reports = pathlib.Path("./allure-results")
res_file_list = [str(res_file) for res_file in openzeppelin_reports.glob("*-result.json")]
print("Fix allure results: {}".format(len(res_file_list)))
for res_file in res_file_list:
with open(res_file, "r+") as f:
report = json.load(f)
report["labels"].append({"name": "epic", "value": "OpenZeppelin contracts"})
with open(res_file, "w+") as f:
json.dump(report, f)
def parse_openzeppelin_results():
test_report = {"passing": 0, "pending": 0, "failing": 0}
skipped_files = []
stdout_files = glob.glob("./compatibility/results/**/stdout.log", recursive=True)
print("`stdout` files found: {}. Processing ...\n".format(len(stdout_files)))
for stdout in stdout_files:
with open(stdout, "r+", encoding="utf8") as f:
rep = f.read()
result = re.findall(r"(\d+) (passing|pending|failing)", rep)
if not result:
skipped_files.append(stdout)
for count in result:
test_report[count[1]] += int(count[0])
return test_report, skipped_files
def print_test_suite_results(test_report: tp.Dict[str, int], skipped_files: tp.List[str]):
print("Summarize result:\n")
for state in test_report:
print(" {} - {}".format(state.capitalize(), test_report[state]))
print("\nTotal tests - {:d}\n".format(sum(test_report.values())))
print("Test files without test result - {}:\n".format(len(skipped_files)))
for f in skipped_files:
test_file_name = f.split("/", 3)[3].rsplit("/", 1)[0].replace("_", "")
print(" {}".format(test_file_name))
def generate_allure_environment(network_name: str):
network = networks[network_name]
env = os.environ.copy()
env["NETWORK_ID"] = str(network["network_id"])
env["PROXY_URL"] = network["proxy_url"]
return env
def install_python_requirements():
command = "pip3 install --upgrade -r deploy/requirements/prod.txt -r deploy/requirements/devel.txt"
subprocess.check_call(command, shell=True)
subprocess.check_call("pip3 install --no-deps -r deploy/requirements/nodeps.txt", shell=True)
def install_oz_requirements():
cwd = pathlib.Path().parent / "compatibility/openzeppelin-contracts"
if list(cwd.glob("*lock*")):
cmd = "npm ci"
else:
cmd = "npm install npm@latest -g"
subprocess.check_call(cmd, shell=True, cwd=cwd.absolute())
@click.group()
def cli():
pass
@cli.command(help="Update base python requirements")
@catch_traceback
def requirements():
install_python_requirements()
install_oz_requirements()
@cli.command(help="Run any type of tests")
@click.option(
"-n", "--network", default="night-stand", type=click.Choice(networks.keys()), help="In which stand run tests"
)
@click.option("-j", "--jobs", default=8, help="Number of parallel jobs (for openzeppelin)")
@click.argument("name", required=True, type=click.Choice(["economy", "basic", "oz"]))
@catch_traceback
def run(name, network, jobs):
if pathlib.Path("./allure-results").exists():
shutil.rmtree("./allure-results", ignore_errors=True)
if name == "economy":
command = "py.test integration/tests/economy/test_economics.py"
elif name == "basic":
command = "py.test integration/tests/basic"
elif name == "oz":
run_openzeppelin_tests(network, jobs=int(jobs))
shutil.copyfile("./allure/categories.json", "./allure-results/categories.json")
return
else:
raise click.ClickException("Unknown test name")
command += f" --network={network} --make-report"
cmd = subprocess.run(command, shell=True)
shutil.copyfile("./allure/categories.json", "./allure-results/categories.json")
if cmd.returncode != 0:
sys.exit(cmd.returncode)
@cli.command(help="Summarize openzeppelin tests results")
def ozreport():
test_report, skipped_files = parse_openzeppelin_results()
print_test_suite_results(test_report, skipped_files)
@cli.command(help="Run `neon` pipeline performance test")
@click.option(
"-f",
"--locustfile",
type=str,
default="loadtesting/locustfile.py",
help="Python module to import. It's sub-folder and file name.",
show_default=True,
)
@click.option(
"-c",
"--credentials",
type=str,
help="Relative path to credentials module.",
show_default=True,
)
@click.option(
"-h",
"--host",
default="night-stand",
type=click.Choice(networks),
help="In which stand run tests.",
show_default=True,
)
@click.option("-u", "--users", default=10, type=int, help="Peak number of concurrent Locust users.", show_default=True)
@click.option(
"-r", "--spawn-rate", default=1, type=int, help="Rate to spawn users at (users per second)", show_default=True
)
@click.option(
"-t",
"--run-time",
type=int,
help="Stop after the specified amount of time, e.g. (300s, 20m, 3h, 1h30m, etc.). "
"Only used together without Locust Web UI. [default: always run]",
)
@click.option(
"-T",
"--tag",
type=str,
multiple=True,
help="tag to include in the test, so only tasks " "with any matching tags will be executed",
)
@click.option(
"--web-ui/--headless",
" /-w",
default=True,
help="Enable the web interface. " "If UI is enabled, go to http://0.0.0.0:8089/ [default: `Web UI is enabled`]",
)
def locust(locustfile, credentials, host, users, spawn_rate, run_time, tag, web_ui):
"""Run `Neon` pipeline performance test
path it's sub-folder and file name `loadtesting/locustfile.py`.
"""
path = pathlib.Path(__file__).parent / locustfile
if not (path.exists() and path.is_file()):
raise FileNotFoundError(f"path doe's not exists. {path.resolve()}")
command = f"locust -f {path.as_posix()} --host={host} --users={users} --spawn-rate={spawn_rate}"
if credentials:
command += f" --credentials={credentials}"
if run_time:
command += f" --run-time={run_time}"
if tag:
command += f" --tags {' '.join(tag)}"
if not web_ui:
command += f" --headless"
cmd = subprocess.run(command, shell=True)
if cmd.returncode != 0:
sys.exit(cmd.returncode)
@cli.command(help="Download allure history")
@click.argument("name", type=click.STRING)
@click.option(
"-n", "--network", default="night-stand", type=click.Choice(networks.keys()), help="In which stand run tests"
)
@click.option("-d", "--destination", default="./allure-results", type=click.Path(file_okay=False, dir_okay=True))
def get_allure_history(name: str, network: str, destination: str = "./allure-results"):
branch = os.environ.get("GITHUB_REF_NAME")
path = pathlib.Path(name) / network / branch
runs = []
previous_runs = cloud.client.list_objects_v2(
Bucket=cloud.NEON_TESTS_BUCKET_NAME, Prefix=f"{path}/", Delimiter="/"
).get("CommonPrefixes", [])
for run in previous_runs:
run_id = re.findall(r"(\d+)", run["Prefix"])
if len(run_id) > 0:
runs.append(int(run_id[0]))
if len(runs) > 0:
print(f"Downloading allure history from build: {max(runs)}")
cloud.download(path / str(max(runs)) / "history", pathlib.Path(destination) / "history")
@cli.command(help="Upload allure report")
@click.argument("name", type=click.STRING)
@click.option(
"-n", "--network", default="night-stand", type=click.Choice(networks.keys()), help="In which stand run tests"
)
@click.option("-s", "--source", default="./allure-report", type=click.Path(file_okay=False, dir_okay=True))
def upload_allure_report(name: str, network: str, source: str = "./allure-report"):
branch = os.environ.get("GITHUB_REF_NAME")
build_id = os.environ.get("GITHUB_RUN_NUMBER")
path = pathlib.Path(name) / network / branch
cloud.upload(source, path / build_id)
report_url = f"http://neon-test-allure.s3-website.eu-central-1.amazonaws.com/{path / build_id}"
with open("/tmp/index.html", "w") as f:
f.write(
f"""<!DOCTYPE html><meta charset="utf-8"><meta http-equiv="refresh" content="0; URL={report_url}">
<meta http-equiv="Pragma" content="no-cache"><meta http-equiv="Expires" content="0">
"""
)
cloud.upload("/tmp/index.html", path)
print(f"Allure report link: {report_url}")
@cli.command(help="Send notification to slack")
@click.option("-u", "--url", help="slack app endpoint url.")
@click.option("-b", "--build_url", help="github action test build url.")
def send_notification(url, build_url):
p = pathlib.Path(f"./{CMD_ERROR_LOG}")
trace_back = p.read_text() if p.exists() else ""
tpl = ERR_MSG_TPL.copy()
parsed_build_url = urlparse(build_url).path.split("/")
build_id = parsed_build_url[-1]
repo_name = f"{parsed_build_url[1]}/{parsed_build_url[2]}"
tpl["blocks"][0]["text"]["text"] = (
f"*Build <{build_url}|`{build_id}`> of repository `{repo_name}` is failed.* \n{trace_back}"
f"\n<{build_url}|View build details>"
)
requests.post(url=url, data=json.dumps(tpl))
if __name__ == "__main__":
cli()