|
7 | 7 | import socket
|
8 | 8 | import subprocess
|
9 | 9 | import sys
|
| 10 | +import tempfile |
10 | 11 | import textwrap
|
11 | 12 | import unittest
|
12 | 13 | from subprocess import PIPE, STDOUT
|
@@ -110,7 +111,7 @@ def my_task(self):
|
110 | 111 | gevent.sleep(1)
|
111 | 112 |
|
112 | 113 | requests.post(
|
113 |
| - "http://127.0.0.1:%i/swarm" % port, |
| 114 | + f"http://127.0.0.1:{port}/swarm", |
114 | 115 | data={"user_count": 1, "spawn_rate": 1, "host": "https://localhost", "custom_string_arg": "web_form_value"},
|
115 | 116 | )
|
116 | 117 | gevent.sleep(1)
|
@@ -859,7 +860,7 @@ def test_web_options(self):
|
859 | 860 | stderr=PIPE,
|
860 | 861 | )
|
861 | 862 | gevent.sleep(1)
|
862 |
| - self.assertEqual(200, requests.get("http://127.0.0.1:%i/" % port, timeout=3).status_code) |
| 863 | + self.assertEqual(200, requests.get(f"http://127.0.0.1:{port}/", timeout=3).status_code) |
863 | 864 | proc.terminate()
|
864 | 865 |
|
865 | 866 | @unittest.skipIf(os.name == "nt", reason="termios doesnt exist on windows, and thus we cannot import pty")
|
@@ -1150,44 +1151,59 @@ def t(self):
|
1150 | 1151 | self.assertEqual(0, proc.returncode)
|
1151 | 1152 |
|
1152 | 1153 | def test_html_report_option(self):
|
| 1154 | + html_template = "some_name_{u}_{r}_{t}.html" |
| 1155 | + expected_filename = "some_name_11_5_2.html" |
| 1156 | + |
1153 | 1157 | with mock_locustfile() as mocked:
|
1154 |
| - with temporary_file("", suffix=".html") as html_report_file_path: |
1155 |
| - try: |
1156 |
| - subprocess.check_output( |
1157 |
| - [ |
1158 |
| - "locust", |
1159 |
| - "-f", |
1160 |
| - mocked.file_path, |
1161 |
| - "--host", |
1162 |
| - "https://test.com/", |
1163 |
| - "--run-time", |
1164 |
| - "2s", |
1165 |
| - "--headless", |
1166 |
| - "--exit-code-on-error", |
1167 |
| - "0", |
1168 |
| - "--html", |
1169 |
| - html_report_file_path, |
1170 |
| - ], |
1171 |
| - stderr=subprocess.STDOUT, |
1172 |
| - timeout=10, |
1173 |
| - text=True, |
1174 |
| - ).strip() |
1175 |
| - except subprocess.CalledProcessError as e: |
1176 |
| - raise AssertionError(f"Running locust command failed. Output was:\n\n{e.stdout}") from e |
1177 |
| - |
1178 |
| - with open(html_report_file_path, encoding="utf-8") as f: |
1179 |
| - html_report_content = f.read() |
1180 |
| - |
1181 |
| - # make sure title appears in the report |
1182 |
| - _, locustfile = os.path.split(mocked.file_path) |
1183 |
| - self.assertIn(locustfile, html_report_content) |
1184 |
| - |
1185 |
| - # make sure host appears in the report |
1186 |
| - self.assertIn("https://test.com/", html_report_content) |
1187 |
| - self.assertIn('"show_download_link": false', html_report_content) |
1188 |
| - self.assertRegex(html_report_content, r'"start_time": "\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z"') |
1189 |
| - self.assertRegex(html_report_content, r'"end_time": "\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z"') |
1190 |
| - self.assertRegex(html_report_content, r'"duration": "\d* seconds?"') |
| 1158 | + # Get system temp directory |
| 1159 | + temp_dir = tempfile.gettempdir() |
| 1160 | + |
| 1161 | + # Define the input filename as well as the resulting filename within the temp directory |
| 1162 | + html_report_file_path = os.path.join(temp_dir, html_template) |
| 1163 | + output_html_report_file_path = os.path.join(temp_dir, expected_filename) |
| 1164 | + |
| 1165 | + try: |
| 1166 | + output = subprocess.check_output( |
| 1167 | + [ |
| 1168 | + "locust", |
| 1169 | + "-f", |
| 1170 | + mocked.file_path, |
| 1171 | + "--host", |
| 1172 | + "https://test.com/", |
| 1173 | + "--run-time", |
| 1174 | + "2s", |
| 1175 | + "--headless", |
| 1176 | + "--exit-code-on-error", |
| 1177 | + "0", |
| 1178 | + "-u", |
| 1179 | + "11", |
| 1180 | + "-r", |
| 1181 | + "5", |
| 1182 | + "--html", |
| 1183 | + html_report_file_path, |
| 1184 | + ], |
| 1185 | + stderr=subprocess.STDOUT, |
| 1186 | + timeout=10, |
| 1187 | + text=True, |
| 1188 | + ).strip() |
| 1189 | + |
| 1190 | + except subprocess.CalledProcessError as e: |
| 1191 | + raise AssertionError(f"Running locust command failed. Output was:\n\n{e.stdout}") from e |
| 1192 | + with open(output_html_report_file_path, encoding="utf-8") as f: |
| 1193 | + html_report_content = f.read() |
| 1194 | + |
| 1195 | + # make sure correct name is generated based on filename arguments |
| 1196 | + self.assertIn(expected_filename, output) |
| 1197 | + |
| 1198 | + _, locustfile = os.path.split(mocked.file_path) |
| 1199 | + self.assertIn(locustfile, html_report_content) |
| 1200 | + |
| 1201 | + # make sure host appears in the report |
| 1202 | + self.assertIn("https://test.com/", html_report_content) |
| 1203 | + self.assertIn('"show_download_link": false', html_report_content) |
| 1204 | + self.assertRegex(html_report_content, r'"start_time": "\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z"') |
| 1205 | + self.assertRegex(html_report_content, r'"end_time": "\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z"') |
| 1206 | + self.assertRegex(html_report_content, r'"duration": "\d* seconds?"') |
1191 | 1207 |
|
1192 | 1208 | def test_run_with_userclass_picker(self):
|
1193 | 1209 | with temporary_file(content=MOCK_LOCUSTFILE_CONTENT_A) as file1:
|
|
0 commit comments