-
Notifications
You must be signed in to change notification settings - Fork 9
/
startmonster.py
executable file
·131 lines (106 loc) · 4.29 KB
/
startmonster.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
from __future__ import unicode_literals
import argparse
import json
import os
import re
import shutil
import subprocess
import sys
import time
import traceback
from logs.log import logger
from eosapi.httpapi.client import Client
from eosapi.httpapi.exceptions import HttpAPIError
TMP_PATH = './tmp'
############################################
# Start run testcase #
############################################
def check_exist_acct(conf_dict):
'''check the common_params.exist_account availability'''
# NOTE: get account that you need open http plugin.
assert conf_dict['common_params']['exist_account']
client = Client([conf_dict['common_params']['nodeos_url']])
try:
client.get_account(conf_dict['common_params']['exist_account'])
except HttpAPIError as err:
return False
return True
def setup_env():
if os.path.exists(TMP_PATH):
shutil.rmtree(TMP_PATH)
os.mkdir(TMP_PATH)
def run_testcase(case_dict, common_params):
cmdline = ''
try:
if 'pre_call' in case_dict and case_dict['pre_call']:
cmdline = case_dict['pre_call']
logger.info('Going to execute PRE call: {} {}'.format(case_dict['casename'], cmdline))
pmsg = subprocess.check_output(cmdline, stderr=subprocess.STDOUT, shell=True)
logger.info(pmsg)
if 'cmdline' not in case_dict or not case_dict['cmdline']:
logger.error('Error: cmdline can NOT be empty {}'.format(case_dict['casename']))
return False
params = {}
params.update(common_params)
params.update(case_dict['params'])
params_file = os.path.join(TMP_PATH, str(re.sub(r"\s+", "", case_dict['casename'])).lower() + ".json")
with open(params_file, "w") as fp:
fp.write(json.dumps(params, indent=True, sort_keys=True, ensure_ascii=False))
cmdline = case_dict['cmdline'] + " " + params_file
logger.info('Going to execute cmdline: {} {}'.format(case_dict['casename'], cmdline))
pmsg = subprocess.check_output(cmdline, stderr=subprocess.STDOUT, shell=True)
logger.info(pmsg)
if 'post_call' in case_dict and case_dict['post_call']:
cmdline = case_dict['post_call']
logger.info('Going to execute POST call: {} {}'.format(case_dict['casename'], cmdline))
pmsg = subprocess.check_output(cmdline, stderr=subprocess.STDOUT, shell=True)
logger.info(pmsg)
return True
except Exception as e:
logger.error('get exception: {} {}'.format(case_dict['casename'], cmdline))
logger.error(traceback.print_exc())
return False
def run_monster(conf_dict):
for case_dict in conf_dict['testcases']:
result = run_testcase(case_dict, conf_dict['common_params'])
if case_dict['stoponfail'] and not result:
return False
return True
####################################################################
def main():
parser = argparse.ArgumentParser(description='EOSIO testcase collections running tool.')
parser.add_argument('--config', default="./config.json", type=str, help='config.json config file path')
args = parser.parse_args()
conf_file = os.path.abspath(os.path.expanduser(args.config))
# Check the parameters
if not os.path.exists('testcases'):
logger.error('call startmonster.py in the eostestmonster directory')
sys.exit(1)
conf_dict = None
with open(conf_file, 'r') as fp:
conf_dict = json.loads(fp.read())
if not conf_dict:
logger.error('validator config can not be empty: {}'.format(conf_file))
sys.exit(1)
if not check_exist_acct(conf_dict):
logger.error('ERROR: the exist_account is NOT available')
sys.exit(1)
setup_env()
# Start the testcase
try:
time_start = time.time()
result = run_monster(conf_dict)
if not result:
logger.error('!!! Call testcases FAILED !!!')
else:
logger.info('Call testcases SUCCESS !!!')
time_usage = time.time() - time_start
logger.info('TIME USAGE:%ss' % (time_usage,))
return result
except Exception as e:
logger.error(traceback.print_exc())
if __name__ == '__main__':
sys.exit(0 if main() else 1)