/
utils.py
72 lines (49 loc) · 1.63 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
import os
import errno
import requests
from .logs import get_logger
from urllib.parse import urljoin
import socket
logger = get_logger()
class ClientSession(requests.Session):
def __init__(self, base_url):
self.base_url = base_url
super().__init__()
def request(self, method, url, *args, **kwargs):
return super().request(method, urljoin(self.base_url, url), *args, **kwargs)
class CrashTest(Exception):
def __init__(self):
super().__init__("Crash Test!")
def ensure_success(response):
if response.status_code != 200:
text = response.text
logger.error(text)
assert response.status_code == 200
def get_connection(host: str, port: int):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
return s
def ensure_folder(path):
try:
os.makedirs(path)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
def assert_files_equals(path_one, path_two):
with open(path_one, mode="rb") as one, open(path_two, mode="rb") as two:
chunk_one = one.read(1024)
chunk_two = two.read(1024)
assert chunk_one == chunk_two
def assert_file_content_equals(file_path, content):
with open(file_path, mode="rt", encoding="utf8") as file:
file_contents = file.read()
assert file_contents == content
def get_file_bytes(file_path):
with open(file_path, mode="rb") as file:
return file.read()
def get_sleep_time():
# when starting a server process,
# a longer sleep time is necessary on Windows
if os.name == "nt":
return 1.5
return 0.5