'Add URL prefix for Locust test
I've developed a self-service tool in AWS that allows the user to spin up a Locust instance with Fargate containers as workers. This requires that every test have a name, e.g. "my-test-1", and that every test be accessed via a unique URL. Previously, that URL was (e.g.) https://locust-test-my-test-1.mycompany.tld/. I want to change it to (e.g.) https://locust-test-runner.mycompany.tld/my-test-1. I can get the ELB to route traffic to the right place, but I get a 404 from the running instance of Locust. How do I add a URL prefix to Locust such that (e.g.) /my-test-1 is prepended to all Locust paths?
Solution 1:[1]
I ended up using DispatcherMiddleware from werkzeug like so:
web_ui = env.create_web_ui(
port=LOCUST_HTTP_PORT,
stats_csv_writer=stats_csv_writer,
delayed_start=True
)
if TEST_PATH: # defined in process.env
path = TEST_PATH
if path[0] != '/':
path = '/' + path
config = copy.deepcopy(web_ui.app.config)
logger.debug(f"Set prefix to {path}")
# h/t: https://dlukes.github.io/flask-wsgi-url-prefix.html
web_ui.app = DispatcherMiddleware(Response('Not Found', status=404), {path: web_ui.app})
web_ui.app.config = config # Necessary to keep the Locust web UI working
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kit Peters |
