Skip to content

Commit 811f892

Browse files
committed
Re-format code
1 parent e868840 commit 811f892

File tree

12 files changed

+377
-238
lines changed

12 files changed

+377
-238
lines changed

tests/AutoscalingTests/common.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,32 @@
2828
timeout=3600,
2929
)
3030

31-
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+
3244

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

49+
3750
def create_session(browser_name):
3851
options = BROWSER[browser_name]
3952
options.set_capability("platformName", "Linux")
40-
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+
4157

4258
def wait_for_count_matches(sessions, timeout=10, interval=5):
4359
elapsed = 0
@@ -49,20 +65,26 @@ def wait_for_count_matches(sessions, timeout=10, interval=5):
4965
time.sleep(interval)
5066
elapsed += interval
5167
if pod_count != len(sessions):
52-
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+
)
5371
else:
5472
print(f"PASS: Pod count matches session count after {elapsed} seconds.")
5573

74+
5675
def close_all_sessions(sessions):
5776
for session in sessions:
5877
session.quit()
5978
sessions.clear()
6079
return sessions
6180

81+
6282
def create_sessions_in_parallel(new_request_sessions):
6383
failed_jobs = 0
6484
with concurrent.futures.ThreadPoolExecutor() as executor:
65-
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+
]
6688
sessions = []
6789
for future in concurrent.futures.as_completed(futures):
6890
try:
@@ -73,6 +95,7 @@ def create_sessions_in_parallel(new_request_sessions):
7395
print(f"Total failed jobs: {failed_jobs}")
7496
return sessions
7597

98+
7699
def randomly_quit_sessions(sessions, sublist_size):
77100
if sessions:
78101
sessions_to_quit = random.sample(sessions, min(sublist_size, len(sessions)))
@@ -83,15 +106,18 @@ def randomly_quit_sessions(sessions, sublist_size):
83106
return len(sessions_to_quit)
84107
return 0
85108

109+
86110
def get_result_file_name():
87111
return f"tests/autoscaling_results"
88112

113+
89114
def export_results_to_csv(output_file, field_names, results):
90115
with open(output_file, mode="w") as csvfile:
91116
writer = csv.DictWriter(csvfile, fieldnames=field_names)
92117
writer.writeheader()
93118
writer.writerows(results)
94119

120+
95121
def export_results_csv_to_md(csv_file, md_file):
96122
with open(csv_file) as f:
97123
table = Table.parse_csv(f)

tests/AutoscalingTests/test_scale_chaos.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
TEST_NODE_MAX_SESSIONS = int(os.getenv("TEST_NODE_MAX_SESSIONS", 1))
1414
TEST_AUTOSCALING_ITERATIONS = int(os.getenv("TEST_AUTOSCALING_ITERATIONS", 20))
1515

16+
1617
def signal_handler(signum, frame):
1718
print("Signal received, quitting all sessions...")
1819
close_all_sessions(SESSIONS)
1920

21+
2022
signal.signal(signal.SIGTERM, signal_handler)
2123
signal.signal(signal.SIGINT, signal_handler)
2224

25+
2326
class SeleniumAutoscalingTests(unittest.TestCase):
2427
def test_run_tests(self):
2528
try:
@@ -40,18 +43,20 @@ def test_run_tests(self):
4043
print(f"INFO: Total sessions: {total_sessions}")
4144
print(f"INFO: Total pods: {total_pods}")
4245
closed_session = randomly_quit_sessions(SESSIONS, random.randint(3, 12))
43-
RESULTS.append({
44-
FIELD_NAMES[0]: iteration + 1,
45-
FIELD_NAMES[1]: new_request_sessions,
46-
FIELD_NAMES[2]: f"{elapsed_time:.2f} s",
47-
FIELD_NAMES[3]: failed_sessions,
48-
FIELD_NAMES[4]: new_scaled_pods,
49-
FIELD_NAMES[5]: total_sessions,
50-
FIELD_NAMES[6]: total_pods,
51-
FIELD_NAMES[7]: TEST_NODE_MAX_SESSIONS,
52-
FIELD_NAMES[8]: (total_pods * TEST_NODE_MAX_SESSIONS) - total_sessions,
53-
FIELD_NAMES[9]: closed_session,
54-
})
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+
)
5560
time.sleep(15)
5661
finally:
5762
print(f"FINISH: Closing {len(SESSIONS)} sessions.")
@@ -60,5 +65,6 @@ def test_run_tests(self):
6065
export_results_to_csv(f"{output_file}.csv", FIELD_NAMES, RESULTS)
6166
export_results_csv_to_md(f"{output_file}.csv", f"{output_file}.md")
6267

68+
6369
if __name__ == "__main__":
6470
unittest.main()

tests/AutoscalingTests/test_scale_up.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
TEST_NODE_MAX_SESSIONS = int(os.getenv("TEST_NODE_MAX_SESSIONS", 1))
1414
TEST_AUTOSCALING_ITERATIONS = int(os.getenv("TEST_AUTOSCALING_ITERATIONS", 20))
1515

16+
1617
def signal_handler(signum, frame):
1718
print("Signal received, quitting all sessions...")
1819
close_all_sessions(SESSIONS)
1920

21+
2022
signal.signal(signal.SIGTERM, signal_handler)
2123
signal.signal(signal.SIGINT, signal_handler)
2224

25+
2326
class SeleniumAutoscalingTests(unittest.TestCase):
2427
def test_run_tests(self):
2528
try:
@@ -43,18 +46,20 @@ def test_run_tests(self):
4346
closed_session = randomly_quit_sessions(SESSIONS, 20)
4447
else:
4548
closed_session = 0
46-
RESULTS.append({
47-
FIELD_NAMES[0]: iteration + 1,
48-
FIELD_NAMES[1]: new_request_sessions,
49-
FIELD_NAMES[2]: f"{elapsed_time:.2f} s",
50-
FIELD_NAMES[3]: failed_sessions,
51-
FIELD_NAMES[4]: new_scaled_pods,
52-
FIELD_NAMES[5]: total_sessions,
53-
FIELD_NAMES[6]: total_pods,
54-
FIELD_NAMES[7]: TEST_NODE_MAX_SESSIONS,
55-
FIELD_NAMES[8]: (total_pods * TEST_NODE_MAX_SESSIONS) - total_sessions,
56-
FIELD_NAMES[9]: closed_session,
57-
})
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+
)
5863
time.sleep(15)
5964
finally:
6065
print(f"FINISH: Closing {len(SESSIONS)} sessions.")
@@ -63,5 +68,6 @@ def test_run_tests(self):
6368
export_results_to_csv(f"{output_file}.csv", FIELD_NAMES, RESULTS)
6469
export_results_csv_to_md(f"{output_file}.csv", f"{output_file}.md")
6570

71+
6672
if __name__ == "__main__":
6773
unittest.main()

0 commit comments

Comments
 (0)