Skip to content

Commit

Permalink
Update tests for modern ui
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbaldwin44 committed May 6, 2024
1 parent c1682b5 commit 1778381
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 93 deletions.
3 changes: 0 additions & 3 deletions locust/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,6 @@ def is_valid_percentile(parameter):
sys.stderr.write("[DEPRECATED] The --hatch-rate parameter has been renamed --spawn-rate\n")
options.spawn_rate = options.hatch_rate

if options.legacy_ui:
sys.stderr.write("[DEPRECATED] The legacy UI is deprecated and will be removed soon\n")

# setup logging
if not options.skip_log_setup:
if options.loglevel.upper() in ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]:
Expand Down
10 changes: 6 additions & 4 deletions locust/test/test_fasthttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import gevent
from geventhttpclient.client import HTTPClientPool
from pyquery import PyQuery as pq

from .testcases import LocustTestCase, WebserverTestCase
from .util import create_tls_cert
Expand Down Expand Up @@ -777,7 +778,8 @@ def tearDown(self):

def test_ssl_request_insecure(self):
s = FastHttpSession(self.environment, "https://127.0.0.1:%i" % self.web_port, insecure=True, user=None)
r = s.get("/")
self.assertEqual(200, r.status_code)
self.assertIn("<title>Locust for None</title>", r.content.decode("utf-8"))
self.assertIn("<p>Script: <span>None</span></p>", r.text)
response = s.get("/")
d = pq(response.content.decode("utf-8"))

self.assertEqual(200, response.status_code)
self.assertIn('"users": null', str(d))
13 changes: 2 additions & 11 deletions locust/test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1136,18 +1136,9 @@ def test_html_report_option(self):
with open(html_report_file_path, encoding="utf-8") as f:
html_report_content = f.read()

# make sure title appears in the report
_, locustfile = os.path.split(mocked.file_path)
self.assertIn(f"<title>Test Report for {locustfile}</title>", html_report_content)
self.assertIn(f"<p>Script: <span>{locustfile}</span></p>", html_report_content)

# make sure host appears in the report
self.assertIn("https://test.com/", html_report_content)

# make sure the charts container appears in the report
self.assertIn("charts-container", html_report_content)

self.assertNotIn("Download the Report", html_report_content, "Download report link found in HTML content")
self.assertIn('"host": "https://test.com/"', html_report_content)
self.assertIn('"show_download_link": false', html_report_content)

def test_run_with_userclass_picker(self):
with temporary_file(content=MOCK_LOCUSTFILE_CONTENT_A) as file1:
Expand Down
101 changes: 26 additions & 75 deletions locust/test/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,27 +105,19 @@ def test_index(self):

def test_index_with_spawn_options(self):
html_to_option = {
"user_count": ["-u", "100"],
"num_users": ["-u", "100"],
"spawn_rate": ["-r", "10.0"],
}

for html_name_to_test in html_to_option.keys():
# Test that setting each spawn option individually populates the corresponding field in the html, and none of the others
self.environment.parsed_options = parse_options(html_to_option[html_name_to_test])

response = requests.get("http://127.0.0.1:%i/" % self.web_port)
self.assertEqual(200, response.status_code)

d = pq(response.content.decode("utf-8"))

for html_name in html_to_option.keys():
start_value = d(f".start [name={html_name}]").attr("value")
edit_value = d(f".edit [name={html_name}]").attr("value")
if html_name_to_test == html_name:
self.assertEqual(html_to_option[html_name][1], start_value)
self.assertEqual(html_to_option[html_name][1], edit_value)
else:
self.assertEqual("1", start_value, msg=f"start value was {start_value} for {html_name}")
self.assertEqual("1", edit_value, msg=f"edit value was {edit_value} for {html_name}")
self.assertIn(f'"{html_name_to_test}": {html_to_option[html_name_to_test][1]}', str(d))

def test_stats_no_data(self):
self.assertEqual(200, requests.get("http://127.0.0.1:%i/stats/requests" % self.web_port).status_code)
Expand Down Expand Up @@ -944,40 +936,42 @@ class MyUser2(User):
response = requests.get("http://127.0.0.1:%i/" % self.web_port)
self.assertEqual(200, response.status_code)
self.assertNotIn("http://example.com", response.content.decode("utf-8"))
self.assertIn("setting this will override the host on all User classes", response.content.decode("utf-8"))

def test_report_page(self):
self.stats.log_request("GET", "/test", 120, 5612)
r = requests.get("http://127.0.0.1:%i/stats/report" % self.web_port)

d = pq(r.content.decode("utf-8"))

self.assertEqual(200, r.status_code)
self.assertIn("<title>Test Report for None</title>", r.text)
self.assertIn("<p>Script: <span>None</span></p>", r.text)
self.assertIn("charts-container", r.text)
self.assertIn(
'<a href="?download=1">Download the Report</a>',
r.text,
"Download report link not found in HTML content",
)
self.assertIn('"host": "None"', str(d))
self.assertIn('"num_requests": 1', str(d))
self.assertIn('"is_report": true', str(d))
self.assertIn('"show_download_link": true', str(d))

def test_report_page_empty_stats(self):
r = requests.get("http://127.0.0.1:%i/stats/report" % self.web_port)
self.assertEqual(200, r.status_code)
self.assertIn("<title>Test Report for None</title>", r.text)
self.assertIn("charts-container", r.text)

def test_report_download(self):
self.stats.log_request("GET", "/test", 120, 5612)
r = requests.get("http://127.0.0.1:%i/stats/report?download=1" % self.web_port)

d = pq(r.content.decode("utf-8"))

self.assertEqual(200, r.status_code)
self.assertIn("attachment", r.headers.get("Content-Disposition", ""))
self.assertNotIn("Download the Report", r.text, "Download report link found in HTML content")
self.assertIn('"show_download_link": false', str(d))

def test_report_host(self):
self.environment.host = "http://test.com"
self.stats.log_request("GET", "/test", 120, 5612)
r = requests.get("http://127.0.0.1:%i/stats/report" % self.web_port)

d = pq(r.content.decode("utf-8"))

self.assertEqual(200, r.status_code)
self.assertIn("http://test.com", r.text)
self.assertIn('"host": "http://test.com"', str(d))

def test_report_host2(self):
class MyUser(User):
Expand All @@ -991,8 +985,11 @@ def my_task(self):
self.environment.user_classes = [MyUser]
self.stats.log_request("GET", "/test", 120, 5612)
r = requests.get("http://127.0.0.1:%i/stats/report" % self.web_port)

d = pq(r.content.decode("utf-8"))

self.assertEqual(200, r.status_code)
self.assertIn("http://test2.com", r.text)
self.assertIn('"host": "http://test2.com"', str(d))

def test_report_exceptions(self):
try:
Expand All @@ -1003,8 +1000,10 @@ def test_report_exceptions(self):
self.runner.log_exception("local", str(e), "".join(traceback.format_tb(tb)))
self.stats.log_request("GET", "/test", 120, 5612)
r = requests.get("http://127.0.0.1:%i/stats/report" % self.web_port)
# self.assertEqual(200, r.status_code)
self.assertIn("<h2>Exceptions Statistics</h2>", r.text)

d = pq(r.content.decode("utf-8"))

self.assertIn('exceptions_statistics": [{"count": 2', str(d))

# Prior to 088a98bf8ff4035a0de3becc8cd4e887d618af53, the "nodes" field for each exception in
# "self.runner.exceptions" was accidentally mutated in "get_html_report" to a string.
Expand All @@ -1014,54 +1013,6 @@ def test_report_exceptions(self):
isinstance(next(iter(self.runner.exceptions.values()))["nodes"], set), "exception object has been mutated"
)

def test_custom_shape_deactivate_num_users_and_spawn_rate(self):
class TestShape(LoadTestShape):
def tick(self):
return None

self.environment.shape_class = TestShape

response = requests.get("http://127.0.0.1:%i/" % self.web_port)
self.assertEqual(200, response.status_code)

# regex to match the intended select tag with id from the custom argument
re_disabled_user_count = re.compile(
r"<input[^>]*id=\"(new_)?user_count\"[^>]*disabled=\"disabled\"[^>]*>", flags=re.I
)
self.assertRegex(response.text, re_disabled_user_count)

re_disabled_spawn_rate = re.compile(
r"<input[^>]*id=\"(new_)?spawn_rate\"[^>]*disabled=\"disabled\"[^>]*>", flags=re.I
)
self.assertRegex(response.text, re_disabled_spawn_rate)

def test_custom_shape_with_use_common_options_keep_num_users_and_spawn_rate(self):
class TestShape(LoadTestShape):
use_common_options = True

def tick(self):
return None

self.environment.shape_class = TestShape

response = requests.get("http://127.0.0.1:%i/" % self.web_port)
self.assertEqual(200, response.status_code)

# regex to match the intended select tag with id from the custom argument
re_user_count = re.compile(r"<input[^>]*id=\"(new_)?user_count\"[^>]*>", flags=re.I)
re_disabled_user_count = re.compile(
r"<input[^>]*id=\"(new_)?user_count\"[^>]*disabled=\"disabled\"[^>]*>", flags=re.I
)
self.assertRegex(response.text, re_user_count)
self.assertNotRegex(response.text, re_disabled_user_count)

re_spawn_rate = re.compile(r"<input[^>]*id=\"(new_)?spawn_rate\"[^>]*>", flags=re.I)
re_disabled_spawn_rate = re.compile(
r"<input[^>]*id=\"(new_)?spawn_rate\"[^>]*disabled=\"disabled\"[^>]*>", flags=re.I
)
self.assertRegex(response.text, re_spawn_rate)
self.assertNotRegex(response.text, re_disabled_spawn_rate)

def test_html_stats_report(self):
self.environment.locustfile = "locust.py"
self.environment.host = "http://localhost"
Expand Down

0 comments on commit 1778381

Please sign in to comment.