From 60cde73cee8c08bd5c6efb88eb0d511c6c025620 Mon Sep 17 00:00:00 2001 From: Maxence Boutet Date: Wed, 9 Jun 2021 18:05:04 -0400 Subject: [PATCH 1/2] Ensure that the exception objects are not mutated when generated report --- locust/html.py | 5 ++++- locust/test/test_web.py | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/locust/html.py b/locust/html.py index 34ecfcd3bf..0ec71c5d22 100644 --- a/locust/html.py +++ b/locust/html.py @@ -1,3 +1,5 @@ +from copy import deepcopy + from jinja2 import Environment, FileSystemLoader import os import pathlib @@ -36,7 +38,8 @@ def get_html_report(environment, show_download_link=True): requests_statistics = list(chain(sort_stats(stats.entries), [stats.total])) failures_statistics = sort_stats(stats.errors) exceptions_statistics = [] - for exc in environment.runner.exceptions.values(): + exceptions = deepcopy(environment.runner.exceptions) + for exc in exceptions.values(): exc["nodes"] = ", ".join(exc["nodes"]) exceptions_statistics.append(exc) diff --git a/locust/test/test_web.py b/locust/test/test_web.py index b6c522c8bc..f7d7cf20d2 100644 --- a/locust/test/test_web.py +++ b/locust/test/test_web.py @@ -356,6 +356,14 @@ def test_report_exceptions(self): # self.assertEqual(200, r.status_code) self.assertIn("

Exceptions Statistics

", r.text) + # Prior to 088a98bf8ff4035a0de3becc8cd4e887d618af53, the "nodes" field for each exception in + # "self.runner.exceptions" was accidentally mutated in "get_html_report" to a string. + # This assertion reproduces the issue and it is left there to make sure there's no + # regression in the future. + self.assertTrue( + isinstance(next(iter(self.runner.exceptions.values()))["nodes"], set), "exception object has been mutated" + ) + class TestWebUIAuth(LocustTestCase): def setUp(self): From 08716b2e11490de0594ef2ed74f1891d15cfcf0f Mon Sep 17 00:00:00 2001 From: Maxence Boutet Date: Fri, 11 Jun 2021 15:33:19 -0400 Subject: [PATCH 2/2] Use a list comprehension with dict-unpacking instead of deepcopy --- locust/html.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/locust/html.py b/locust/html.py index 0ec71c5d22..49f4ebb97e 100644 --- a/locust/html.py +++ b/locust/html.py @@ -1,5 +1,3 @@ -from copy import deepcopy - from jinja2 import Environment, FileSystemLoader import os import pathlib @@ -37,11 +35,9 @@ def get_html_report(environment, show_download_link=True): requests_statistics = list(chain(sort_stats(stats.entries), [stats.total])) failures_statistics = sort_stats(stats.errors) - exceptions_statistics = [] - exceptions = deepcopy(environment.runner.exceptions) - for exc in exceptions.values(): - exc["nodes"] = ", ".join(exc["nodes"]) - exceptions_statistics.append(exc) + exceptions_statistics = [ + {**exc, "nodes": ", ".join(exc["nodes"])} for exc in environment.runner.exceptions.values() + ] history = stats.history