-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
369 lines (302 loc) · 11.8 KB
/
utils.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
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
import os.path
import time
import pathlib
import json
import unittest
import requests
import codecs
import re
import random
import string
import sys
from websocket._core import create_connection
from datetime import datetime
from errors import ERRORS
from enum import Enum
CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))
PROJECT_ROOT_DIR = pathlib.Path(CURRENT_DIR)
class Utils(unittest.TestCase):
class TestingStatus(Enum):
START = 1
FINISHED = 2
NONE = 3
@staticmethod
def get_date_strings():
"""Return date information with various formats to check date."""
local_datetime = datetime.now()
dt = datetime(
local_datetime.year,
local_datetime.month,
local_datetime.day,
local_datetime.hour,
local_datetime.minute,
local_datetime.second,
)
timestamp = time.mktime(dt.timetuple())
return (local_datetime, dt, timestamp)
@staticmethod
def get_log_filename_with_path():
"""Create log file whenever test is executed and returns path, directory name, and log file name."""
_, _, timestamp = Utils.get_date_strings()
log_directory_name = datetime.fromtimestamp(int(timestamp)).strftime("%Y%m%d%H%M%S")
log_path = f"./reports/{log_directory_name}"
pathlib.Path(log_path).mkdir(parents=True, exist_ok=True)
log_file_name = f"{log_path}/kaia.log"
return log_path, log_directory_name, log_file_name
@staticmethod
def get_request_id():
"""Returns a random integer (1 ~ 99) to be used as request id when call API."""
return random.randint(1, 100)
@staticmethod
def save_result_data(log_path, file_name, data):
"""If result value is big like blockdump, store it as a file."""
# Check path of log and if there is no directory, create a directory.
pathlib.Path(log_path).mkdir(parents=True, exist_ok=True)
# Appoint path and file name to store.
path = f"{log_path}/{file_name}"
with open(path, "wt") as f:
f.write(f"{data}")
@staticmethod
def convert_to_hex(message):
"""Change string, int data to hex data."""
hex_data = ""
if isinstance(message, str):
hex_data = "0x" + "".join("{:02x}".format(ord(c)) for c in str(message))
if isinstance(message, int):
hex_data = hex(message)
if isinstance(message, dict):
message = json.dumps(message)
hex_data = "0x" + "".join("{:02x}".format(ord(c)) for c in str(message))
return hex_data
@staticmethod
def convert_hex_to_string(hex_data):
"""Convert hex data to string."""
hex_data = Utils.strip_hex_prefix(hex_data)
string_data = codecs.decode(hex_data, "hex").decode("utf-8", "ignore")
# replace null character
string_data = string_data.replace("\x00", "")
string_data = string_data.replace("\x16", "")
return string_data
@staticmethod
def is_hex(s):
"""Returns true if given string is a hexadecimal."""
s = Utils.strip_hex_prefix(s)
return all(c in string.hexdigits for c in s)
@staticmethod
def strip_hex_prefix(s):
"""Remove hex prefix and returns hexadecimal without prefix."""
if len(s) >= 2:
if s[:2] == "0x" or s[:2] == "0X":
return s[2:]
else:
return s
return s
@staticmethod
def left_pad64(hex_data):
"""Pad zeros and returns 64-length hexadecimal."""
hex_data = Utils.strip_hex_prefix(hex_data)
return "{:0>64}".format(hex_data)
@staticmethod
def get_config():
"""Read config.json and return parsed json data."""
with open(f"{PROJECT_ROOT_DIR}/config.json") as f:
data = json.load(f)
return data
@staticmethod
def call_rpc(endpoint, method, params, log_file, save_result=False, port=8551):
if endpoint is None or endpoint == "":
endpoint = Utils.get_config().get("endpoint")
host = f"http://{endpoint}:{port}"
headers = {"Content-Type": "application/json"}
payload = {
"jsonrpc": "2.0",
"method": method,
"params": [],
"id": Utils.get_request_id(),
}
if params is not None:
payload["params"] = params
response = requests.post(host, data=json.dumps(payload), headers=headers, timeout=300)
if response.status_code != 200:
return f"Error: Unexpected response {response}"
Utils.write_log(
log_file,
method,
host,
payload,
response.text,
save_file=save_result,
)
response_json = json.loads(response.text)
result = response_json.get("result")
error = response_json.get("error")
return result, error
@staticmethod
def call_ws(endpoint, method, params, log_file, save_result=False, port=8552):
host = f"ws://{endpoint}:{port}"
if params == None:
params = []
payload = {
"jsonrpc": "2.0",
"method": method,
"params": params,
"id": Utils.get_request_id(),
}
ws = create_connection(host)
ws.send(json.dumps(payload))
response = ws.recv()
ws.close()
response_json = json.loads(response)
result = response_json.get("result")
error = response_json.get("error")
Utils.write_log(
log_file,
method,
host,
payload,
response,
save_file=save_result,
service="ws",
)
return result, error
@staticmethod
def get_console_result_with_index(screenshot, command, log_file, save_result=False, columns=0):
kaia_list, command_index = Utils.parse_command(screenshot, command, columns)
# Make a string from command_index to last line and remove white spaces.
result_string = " ".join(kaia_list[command_index:]).replace(" ", "")
# There is no quotation at key of returned json result from kaia console.
# Use regular expressions to change it to valid json string.
valid_json = re.sub(r"(?<={|,)([a-zA-Z][a-zA-Z0-9]*)(?=:)", r'"\1"', result_string)
try:
result = json.loads(valid_json)
except:
result = valid_json
if command == "> exit":
result = None
Utils.write_console_log(log_file, command_index, command, result, save_file=save_result)
return result
@staticmethod
def parse_command(screenshot, command, columns):
command = f"> {command}"
# Split screenshot by lines and convert it to the array and remove ''.
# Maximum lines of screenshot is 9999.
kaia_list = list(filter(None, screenshot.splitlines()))
# Check the last line of an array and remove when it is '>' (waiting command status).
last_index = len(kaia_list) - 1
if kaia_list[last_index] == ">":
del kaia_list[-1]
# Checks the index of kaia command at the last of array.
# If command is longer than terminal's width(columns), find after cutting it out.
if columns == 0:
command_index = last_index - kaia_list[::-1].index(command)
else:
command_index = last_index - kaia_list[::-1].index(command[:columns])
return kaia_list, command_index
@staticmethod
def get_console_sub_command(screenshot, command, logfile, save_result=False, columns=0):
kaia_list, command_index = Utils.parse_command(screenshot, command, columns)
Utils.write_console_log(logfile, command_index, command, None, save_file=save_result)
return command
@staticmethod
def write_head_or_tail_log(
log_file_name,
testing_status,
testing_unit="",
):
with open(log_file_name, "at") as f:
if testing_status == Utils.TestingStatus.START:
log_string = f"------------ Testing Start ({testing_unit}) ------------- \n\n"
elif testing_status == Utils.TestingStatus.FINISHED:
log_string = f"------------ Testing Finished ({testing_unit}) ------------ \n\n"
print(log_string)
f.write(f"\n{log_string}")
@staticmethod
def write_log(
log_file_name,
method,
host,
data,
result_text,
file_name="",
save_file=False,
service="rpc",
):
"""
Write start of testing at log file.
If result data size is big like debug_dumpBlock, that will be stored as file.
"""
if save_file and (file_name != "" and file_name is not None):
with open(file_name, "w") as f:
f.write(result_text)
with open(log_file_name, "at") as f:
if service == "rpc":
curl_string = f'curl -H "Content-Type: application/json" --data "{str(json.dumps(data))}" {host}'
else:
curl_string = f'["{host}"]\n{str(json.dumps(data))}'
log_string = f"[{str(datetime.now())}] [{method}]\n" f"{curl_string}\n"
try:
result_json = json.loads(result_text)
log_string += f"{json.dumps(result_json, indent=4, sort_keys=True)} \n\n"
except ValueError as e:
log_string += f"{result_text} \n\n"
print(log_string)
f.write(f"\n{log_string}")
@staticmethod
def write_console_log(
log_file_name,
search_index,
command,
result,
testing_status,
testing_unit="",
save_file=False,
):
"""
Write start of testing at log file.
If result data size is big like debug_dumpBlock, that will be stored as file.
"""
if save_file:
result = {"Utils_Message": "Output data was saved at a specific file."}
with open(log_file_name, "at") as f:
if testing_status == Utils.TestingStatus.START:
log_string = f"------------ Testing Start ({testing_unit}) ------------- \n\n"
elif testing_status == Utils.TestingStatus.FINISHED:
log_string = f"------------ Testing Finished ({testing_unit}) ------------ \n\n"
else:
log_string = (
f"[IDX:{str(search_index)}] [{str(datetime.now())}] \n"
f"{command} \n"
f"{json.dumps(result, indent=4, sort_keys=True)} \n\n"
)
f.write(log_string)
print(log_string)
time.sleep(2)
@staticmethod
def waiting_count(frontString, second, rearString):
"""
Lets count number while waiting.
ex) Utils.waitingCount('Waiting for', 5, 'seconds until writing in block a transaction.')
"""
for i in range(second, -1, -1):
sys.stdout.write(f"\r{frontString} {i} {rearString}")
sys.stdout.flush()
time.sleep(1)
sys.stdout.write("\n\n")
@staticmethod
def to_kei(kaia):
"""Convert unit KAIA to Kei."""
kei = 0
if isinstance(kaia, float):
kei = int(kaia * 1000000000000000000.0)
if isinstance(kaia, int):
kei = kaia * 1000000000000000000
return kei
@staticmethod
def check_error(target_instance, expected_error_key, error):
"""
Check whether given error is expected error or not by using expected_error_key.
target_instance must inherit unittest.TestCase class.
"""
expected_error_code, expected_error_message = ERRORS[expected_error_key]
target_instance.assertEqual(expected_error_code, error.get("code"), error)
target_instance.assertIn(expected_error_message, error.get("message"), error)