Skip to content

Fix: Validate headless and host options #2627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions locust/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import sys
import time
import traceback
from urllib.parse import urlparse

import gevent

Expand Down Expand Up @@ -512,6 +513,10 @@ def assign_equal_weights(environment, **kwargs):
if options.equal_weights:
environment.events.init.add_listener(assign_equal_weights)

if options.host and not (urlparse(options.host).scheme):
logger.error(f"Invalid --host option: {options.host}. Did you mean https://{options.host}?")
sys.exit(1)

# Fire locust init event which can be used by end-users' code to run setup code that
# need access to the Environment, Runner or WebUI.
environment.events.init.fire(environment=environment, runner=runner, web_ui=web_ui)
Expand Down
48 changes: 48 additions & 0 deletions locust/test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,54 @@ def my_task(self):
# ensure stats printer printed at least one report before shutting down and that there was a final report printed as well
self.assertRegex(stderr, r".*Aggregated[\S\s]*Shutting down[\S\s]*Aggregated.*")

def test_host_wo_protocol(self):
with mock_locustfile() as mocked:
proc = subprocess.Popen(
[
"locust",
"-f",
mocked.file_path,
"--run-time",
"1s",
"--headless",
"--host",
"test.com",
"--loglevel",
"DEBUG",
],
stdout=PIPE,
stderr=PIPE,
text=True,
)
stdout, stderr = proc.communicate(timeout=4)
self.assertIn("Invalid --host option: test.com. Did you mean https://test.com?", stderr)
self.assertEqual(1, proc.returncode)

def test_headless_w_host(self):
with mock_locustfile() as mocked:
proc = subprocess.Popen(
[
"locust",
"-f",
mocked.file_path,
"--run-time",
"1s",
"--headless",
"--host",
"https://test.com",
"--loglevel",
"DEBUG",
],
stdout=PIPE,
stderr=PIPE,
text=True,
)
stdout, stderr = proc.communicate(timeout=4)
self.assertIn("Ramping to 1 users", stderr)
self.assertIn("All users of class UserSubclass spawned", stderr)
self.assertIn("Cleaning up runner", stderr)
self.assertEqual(0, proc.returncode)


class DistributedIntegrationTests(ProcessIntegrationTest):
failed_port_check = False
Expand Down
Loading