Module: check_mk
Branch: master
Commit: 89e2408a1d54fe28bd5c2f66bd0a7d0068e181e6
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=89e2408a1d54fe…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu Sep 15 13:22:51 2016 +0200
Fixed crash reporting modifying data strcutures it reported a crash for
---
lib/crash_reporting.py | 2 ++
tests/lib/test_crash_reporting.py | 62 +++++++++++++++++++++++++++++++++++++++
2 files changed, 64 insertions(+)
diff --git a/lib/crash_reporting.py b/lib/crash_reporting.py
index 4a1a5af..d709a3c 100644
--- a/lib/crash_reporting.py
+++ b/lib/crash_reporting.py
@@ -126,10 +126,12 @@ def format_var_for_export(val, maxdepth=4, maxsize=1024*1024):
return "Max recursion depth reached"
if isinstance(val, dict):
+ val = val.copy()
for item_key, item_val in val.items():
val[item_key] = format_var_for_export(item_val, maxdepth-1)
elif isinstance(val, list):
+ val = val[:]
for index, item in enumerate(val):
val[index] = format_var_for_export(item, maxdepth-1)
diff --git a/tests/lib/test_crash_reporting.py b/tests/lib/test_crash_reporting.py
new file mode 100644
index 0000000..853feac
--- /dev/null
+++ b/tests/lib/test_crash_reporting.py
@@ -0,0 +1,62 @@
+import copy
+import cmk.crash_reporting as crash_reporting
+
+def test_format_var_for_export_strip_nested_dict():
+ orig_var = {
+ "a": {
+ "b": {
+ "c": {
+ "d": {
+
+ },
+ },
+ },
+ },
+ }
+
+ var = copy.deepcopy(orig_var)
+ formated = crash_reporting.format_var_for_export(var)
+
+ # Stripped?
+ assert formated["a"]["b"]["c"]["d"] ==
"Max recursion depth reached"
+
+ # Not modified original data
+ assert orig_var == var
+
+
+def test_format_var_for_export_strip_large_data():
+ orig_var = {
+ "a": {
+ "y": ("a" * 1024 * 1024) + "a"
+ },
+ }
+
+ var = copy.deepcopy(orig_var)
+ formated = crash_reporting.format_var_for_export(var)
+
+ # Stripped?
+ assert formated["a"]["y"].endswith("(1 bytes
stripped)")
+
+ # Not modified original data
+ assert orig_var == var
+
+
+def test_format_var_for_export_strip_nested_dict_with_list():
+ orig_var = {
+ "a": {
+ "b": {
+ "c": [
+ {}
+ ],
+ },
+ },
+ }
+
+ var = copy.deepcopy(orig_var)
+ formated = crash_reporting.format_var_for_export(var)
+
+ # Stripped?
+ assert formated["a"]["b"]["c"][0] == "Max
recursion depth reached"
+
+ # Not modified original data
+ assert orig_var == var