Skip to content

Commit c672b0d

Browse files
authored
Fix Python formatting in tests (#2866)
* Fix import sort order in tests * Re-format code
1 parent ee44ced commit c672b0d

File tree

12 files changed

+433
-280
lines changed

12 files changed

+433
-280
lines changed

tests/AutoscalingTests/common.py

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
import unittest
2-
import random
3-
import time
4-
import subprocess
5-
import signal
61
import concurrent.futures
72
import csv
83
import os
4+
import random
5+
import signal
6+
import subprocess
7+
import time
8+
import unittest
9+
10+
from csv2md.table import Table
911
from selenium import webdriver
10-
from selenium.webdriver.firefox.options import Options as FirefoxOptions
11-
from selenium.webdriver.edge.options import Options as EdgeOptions
1212
from selenium.webdriver.chrome.options import Options as ChromeOptions
13+
from selenium.webdriver.edge.options import Options as EdgeOptions
14+
from selenium.webdriver.firefox.options import Options as FirefoxOptions
1315
from selenium.webdriver.remote.client_config import ClientConfig
14-
from csv2md.table import Table
1516

1617
BROWSER = {
1718
"chrome": ChromeOptions(),
@@ -27,16 +28,32 @@
2728
timeout=3600,
2829
)
2930

30-
FIELD_NAMES = ["Iteration", "New request sessions", "Sessions created time", "Sessions failed to create", "New pods scaled up", "Total running sessions", "Total running pods", "Max sessions per pod", "Gaps", "Sessions closed"]
31+
FIELD_NAMES = [
32+
"Iteration",
33+
"New request sessions",
34+
"Sessions created time",
35+
"Sessions failed to create",
36+
"New pods scaled up",
37+
"Total running sessions",
38+
"Total running pods",
39+
"Max sessions per pod",
40+
"Gaps",
41+
"Sessions closed",
42+
]
43+
3144

3245
def get_pod_count():
3346
result = subprocess.run(["kubectl", "get", "pods", "-A", "--no-headers"], capture_output=True, text=True)
3447
return len([line for line in result.stdout.splitlines() if "selenium-node-" in line and "Running" in line])
3548

49+
3650
def create_session(browser_name):
3751
options = BROWSER[browser_name]
3852
options.set_capability("platformName", "Linux")
39-
return webdriver.Remote(command_executor=CLIENT_CONFIG.remote_server_addr, options=options, client_config=CLIENT_CONFIG)
53+
return webdriver.Remote(
54+
command_executor=CLIENT_CONFIG.remote_server_addr, options=options, client_config=CLIENT_CONFIG
55+
)
56+
4057

4158
def wait_for_count_matches(sessions, timeout=10, interval=5):
4259
elapsed = 0
@@ -48,20 +65,26 @@ def wait_for_count_matches(sessions, timeout=10, interval=5):
4865
time.sleep(interval)
4966
elapsed += interval
5067
if pod_count != len(sessions):
51-
print(f"WARN: Mismatch between pod count and session count after {timeout} seconds. Gaps: {pod_count - len(sessions)}")
68+
print(
69+
f"WARN: Mismatch between pod count and session count after {timeout} seconds. Gaps: {pod_count - len(sessions)}"
70+
)
5271
else:
5372
print(f"PASS: Pod count matches session count after {elapsed} seconds.")
5473

74+
5575
def close_all_sessions(sessions):
5676
for session in sessions:
5777
session.quit()
5878
sessions.clear()
5979
return sessions
6080

81+
6182
def create_sessions_in_parallel(new_request_sessions):
6283
failed_jobs = 0
6384
with concurrent.futures.ThreadPoolExecutor() as executor:
64-
futures = [executor.submit(create_session, random.choice(list(BROWSER.keys()))) for _ in range(new_request_sessions)]
85+
futures = [
86+
executor.submit(create_session, random.choice(list(BROWSER.keys()))) for _ in range(new_request_sessions)
87+
]
6588
sessions = []
6689
for future in concurrent.futures.as_completed(futures):
6790
try:
@@ -72,6 +95,7 @@ def create_sessions_in_parallel(new_request_sessions):
7295
print(f"Total failed jobs: {failed_jobs}")
7396
return sessions
7497

98+
7599
def randomly_quit_sessions(sessions, sublist_size):
76100
if sessions:
77101
sessions_to_quit = random.sample(sessions, min(sublist_size, len(sessions)))
@@ -82,15 +106,18 @@ def randomly_quit_sessions(sessions, sublist_size):
82106
return len(sessions_to_quit)
83107
return 0
84108

109+
85110
def get_result_file_name():
86111
return f"tests/autoscaling_results"
87112

113+
88114
def export_results_to_csv(output_file, field_names, results):
89115
with open(output_file, mode="w") as csvfile:
90116
writer = csv.DictWriter(csvfile, fieldnames=field_names)
91117
writer.writeheader()
92118
writer.writerows(results)
93119

120+
94121
def export_results_csv_to_md(csv_file, md_file):
95122
with open(csv_file) as f:
96123
table = Table.parse_csv(f)
Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
1-
import unittest
1+
import csv
22
import random
3-
import time
43
import signal
5-
import csv
4+
import time
5+
import unittest
6+
67
from csv2md.table import Table
8+
79
from .common import *
810

911
SESSIONS = []
1012
RESULTS = []
1113
TEST_NODE_MAX_SESSIONS = int(os.getenv("TEST_NODE_MAX_SESSIONS", 1))
1214
TEST_AUTOSCALING_ITERATIONS = int(os.getenv("TEST_AUTOSCALING_ITERATIONS", 20))
1315

16+
1417
def signal_handler(signum, frame):
1518
print("Signal received, quitting all sessions...")
1619
close_all_sessions(SESSIONS)
1720

21+
1822
signal.signal(signal.SIGTERM, signal_handler)
1923
signal.signal(signal.SIGINT, signal_handler)
2024

25+
2126
class SeleniumAutoscalingTests(unittest.TestCase):
2227
def test_run_tests(self):
2328
try:
@@ -38,18 +43,20 @@ def test_run_tests(self):
3843
print(f"INFO: Total sessions: {total_sessions}")
3944
print(f"INFO: Total pods: {total_pods}")
4045
closed_session = randomly_quit_sessions(SESSIONS, random.randint(3, 12))
41-
RESULTS.append({
42-
FIELD_NAMES[0]: iteration + 1,
43-
FIELD_NAMES[1]: new_request_sessions,
44-
FIELD_NAMES[2]: f"{elapsed_time:.2f} s",
45-
FIELD_NAMES[3]: failed_sessions,
46-
FIELD_NAMES[4]: new_scaled_pods,
47-
FIELD_NAMES[5]: total_sessions,
48-
FIELD_NAMES[6]: total_pods,
49-
FIELD_NAMES[7]: TEST_NODE_MAX_SESSIONS,
50-
FIELD_NAMES[8]: (total_pods * TEST_NODE_MAX_SESSIONS) - total_sessions,
51-
FIELD_NAMES[9]: closed_session,
52-
})
46+
RESULTS.append(
47+
{
48+
FIELD_NAMES[0]: iteration + 1,
49+
FIELD_NAMES[1]: new_request_sessions,
50+
FIELD_NAMES[2]: f"{elapsed_time:.2f} s",
51+
FIELD_NAMES[3]: failed_sessions,
52+
FIELD_NAMES[4]: new_scaled_pods,
53+
FIELD_NAMES[5]: total_sessions,
54+
FIELD_NAMES[6]: total_pods,
55+
FIELD_NAMES[7]: TEST_NODE_MAX_SESSIONS,
56+
FIELD_NAMES[8]: (total_pods * TEST_NODE_MAX_SESSIONS) - total_sessions,
57+
FIELD_NAMES[9]: closed_session,
58+
}
59+
)
5360
time.sleep(15)
5461
finally:
5562
print(f"FINISH: Closing {len(SESSIONS)} sessions.")
@@ -58,5 +65,6 @@ def test_run_tests(self):
5865
export_results_to_csv(f"{output_file}.csv", FIELD_NAMES, RESULTS)
5966
export_results_csv_to_md(f"{output_file}.csv", f"{output_file}.md")
6067

68+
6169
if __name__ == "__main__":
6270
unittest.main()

tests/AutoscalingTests/test_scale_up.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
1-
import unittest
1+
import csv
22
import random
3-
import time
43
import signal
5-
import csv
4+
import time
5+
import unittest
6+
67
from csv2md.table import Table
8+
79
from .common import *
810

911
SESSIONS = []
1012
RESULTS = []
1113
TEST_NODE_MAX_SESSIONS = int(os.getenv("TEST_NODE_MAX_SESSIONS", 1))
1214
TEST_AUTOSCALING_ITERATIONS = int(os.getenv("TEST_AUTOSCALING_ITERATIONS", 20))
1315

16+
1417
def signal_handler(signum, frame):
1518
print("Signal received, quitting all sessions...")
1619
close_all_sessions(SESSIONS)
1720

21+
1822
signal.signal(signal.SIGTERM, signal_handler)
1923
signal.signal(signal.SIGINT, signal_handler)
2024

25+
2126
class SeleniumAutoscalingTests(unittest.TestCase):
2227
def test_run_tests(self):
2328
try:
@@ -41,18 +46,20 @@ def test_run_tests(self):
4146
closed_session = randomly_quit_sessions(SESSIONS, 20)
4247
else:
4348
closed_session = 0
44-
RESULTS.append({
45-
FIELD_NAMES[0]: iteration + 1,
46-
FIELD_NAMES[1]: new_request_sessions,
47-
FIELD_NAMES[2]: f"{elapsed_time:.2f} s",
48-
FIELD_NAMES[3]: failed_sessions,
49-
FIELD_NAMES[4]: new_scaled_pods,
50-
FIELD_NAMES[5]: total_sessions,
51-
FIELD_NAMES[6]: total_pods,
52-
FIELD_NAMES[7]: TEST_NODE_MAX_SESSIONS,
53-
FIELD_NAMES[8]: (total_pods * TEST_NODE_MAX_SESSIONS) - total_sessions,
54-
FIELD_NAMES[9]: closed_session,
55-
})
49+
RESULTS.append(
50+
{
51+
FIELD_NAMES[0]: iteration + 1,
52+
FIELD_NAMES[1]: new_request_sessions,
53+
FIELD_NAMES[2]: f"{elapsed_time:.2f} s",
54+
FIELD_NAMES[3]: failed_sessions,
55+
FIELD_NAMES[4]: new_scaled_pods,
56+
FIELD_NAMES[5]: total_sessions,
57+
FIELD_NAMES[6]: total_pods,
58+
FIELD_NAMES[7]: TEST_NODE_MAX_SESSIONS,
59+
FIELD_NAMES[8]: (total_pods * TEST_NODE_MAX_SESSIONS) - total_sessions,
60+
FIELD_NAMES[9]: closed_session,
61+
}
62+
)
5663
time.sleep(15)
5764
finally:
5865
print(f"FINISH: Closing {len(SESSIONS)} sessions.")
@@ -61,5 +68,6 @@ def test_run_tests(self):
6168
export_results_to_csv(f"{output_file}.csv", FIELD_NAMES, RESULTS)
6269
export_results_csv_to_md(f"{output_file}.csv", f"{output_file}.md")
6370

71+
6472
if __name__ == "__main__":
6573
unittest.main()

0 commit comments

Comments
 (0)