Skip to content
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

Check that locustfile downloaded from URL is valid Python code #2604

Merged
merged 3 commits into from
Feb 15, 2024
Merged
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
9 changes: 8 additions & 1 deletion locust/argument_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from locust import runners
from locust.rpc import Message, zmqrpc

import ast
import atexit
import os
import platform
Expand Down Expand Up @@ -159,13 +160,19 @@ def is_url(url: str) -> bool:
def download_locustfile_from_url(url: str) -> str:
try:
response = requests.get(url)
# Check if response is valid python code
ast.parse(response.text)
except requests.exceptions.RequestException as e:
sys.stderr.write(f"Failed to get locustfile from: {url}. Exception: {e}")
sys.exit(1)
except SyntaxError:
sys.stderr.write(f"Failed to get locustfile from: {url}. Response is not valid python code.")
sys.exit(1)

with open(os.path.join(tempfile.gettempdir(), url.rsplit("/", 1)[-1]), "w") as locustfile:
locustfile.write(response.text)

# Clean up downloaded files on exit
def exit_handler():
try:
os.remove(locustfile.name)
Expand Down Expand Up @@ -202,7 +209,7 @@ def get_empty_argument_parser(add_help=True, default_config_files=DEFAULT_CONFIG
"--locustfile",
metavar="<filename>",
default="locustfile",
help="The Python file or module that contains your test, e.g. 'my_test.py'. Also accepts multiple comma-separated .py files or a package name/directory. Defaults to 'locustfile'.",
help="The Python file or module that contains your test, e.g. 'my_test.py'. Accepts multiple comma-separated .py files, a package name/directory or a url to a remote locustfile. Defaults to 'locustfile'.",
env_var="LOCUST_LOCUSTFILE",
)

Expand Down
Loading