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 d416966 commit 2294b2f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 116 deletions.
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))
18 changes: 5 additions & 13 deletions locust/test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,8 @@ def test_percentile_parameter(self):
content=textwrap.dedent(
"""
from locust import User, task, constant, events
from locust.stats import PERCENTILES_TO_CHART
PERCENTILES_TO_CHART[0] = 0.9
PERCENTILES_TO_CHART[1] = 0.4
from locust import stats
stats.PERCENTILES_TO_CHART = [0.9, 0.4]
class TestUser(User):
wait_time = constant(3)
@task
Expand Down Expand Up @@ -274,8 +273,8 @@ def test_invalid_percentile_parameter(self):
content=textwrap.dedent(
"""
from locust import User, task, constant, events
from locust.stats import PERCENTILES_TO_CHART
PERCENTILES_TO_CHART[0] = 1.2
from locust import stats
stats.PERCENTILES_TO_CHART = [1.2]
class TestUser(User):
wait_time = constant(3)
@task
Expand Down Expand Up @@ -728,7 +727,6 @@ def tick(self):
"locust",
"-f",
mocked.file_path,
"--legacy-ui",
"--web-port",
str(port),
"--autostart",
Expand Down Expand Up @@ -795,7 +793,6 @@ def my_task(self):
"locust",
"-f",
f"{mocked1.file_path},{mocked2}",
"--legacy-ui",
"--web-port",
str(port),
"--autostart",
Expand Down Expand Up @@ -1115,7 +1112,6 @@ def test_html_report_option(self):
"locust",
"-f",
mocked.file_path,
"--legacy-ui",
"--host",
"https://test.com/",
"--run-time",
Expand Down Expand Up @@ -1143,11 +1139,7 @@ def test_html_report_option(self):

# 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('"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
125 changes: 26 additions & 99 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 @@ -793,30 +785,6 @@ def _(parser, **kw):
self.assertEqual(200, response.status_code)
self.assertEqual(my_dict["val"], 42)

def test_custom_argument_dropdown(self):
class MyUser(User):
host = "http://example.com"

@locust.events.init_command_line_parser.add_listener
def _(parser, **kw):
parser.add_argument("--my-argument", default="a", choices=["a", "b"], help="Pick one")

parsed_options = parse_options(args=["--my-argument", "b"])
self.environment.user_classes = [MyUser]
self.environment.parsed_options = parsed_options
self.environment.web_ui.parsed_options = parsed_options

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
dropdown_pattern = re.compile(r"<select[^>]*id=\"my_argument\"[^>]*>", flags=re.I)
self.assertRegex(response.text, dropdown_pattern)

# regex to match the input that generates if it fails to find the choices
textfield_pattern = re.compile(r"<input[^>]*id=\"my_argument\"[^>]*>", flags=re.I)
self.assertNotRegex(response.text, textfield_pattern)

def test_swarm_host_value_not_specified(self):
class MyUser(User):
wait_time = constant(1)
Expand Down Expand Up @@ -944,40 +912,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 +961,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 +976,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 +989,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 2294b2f

Please sign in to comment.