Merge pull request #50382 from garethgreenaway/50062_max_event_size_does_not_always_trim_large_events

[2018.3] Fix to utils/dicttrim.py to honor max_event_size with nested dictionaries
This commit is contained in:
Nicole Thomas 2018-11-05 15:04:05 -05:00 committed by GitHub
commit ed4fa8710b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 2 deletions

View File

@ -6,6 +6,22 @@ import sys
import salt.payload
def _trim_dict_in_dict(data, max_val_size, replace_with):
'''
Takes a dictionary, max_val_size and replace_with
and recursively loops through and replaces any values
that are greater than max_val_size.
'''
for key in data:
if isinstance(data[key], dict):
_trim_dict_in_dict(data[key],
max_val_size,
replace_with)
else:
if sys.getsizeof(data[key]) > max_val_size:
data[key] = replace_with
def trim_dict(
data,
max_dict_bytes,
@ -63,6 +79,11 @@ def trim_dict(
max_val_size = float(max_dict_bytes * (percent / 100))
try:
for key in data:
if isinstance(data[key], dict):
_trim_dict_in_dict(data[key],
max_val_size,
replace_with)
else:
if sys.getsizeof(data[key]) > max_val_size:
data[key] = replace_with
percent = percent - stepper_size

View File

@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
# Import python libs
from __future__ import absolute_import, print_function, unicode_literals
# Import Salt Testing libs
from tests.support.unit import TestCase
# Import Salt libs
import salt.utils.dicttrim as dicttrimmer
import logging
log = logging.getLogger(__name__)
class DictTrimTestCase(TestCase):
def setUp(self):
self.old_dict = {'a': 'b', 'c': 'x' * 10000}
self.new_dict = {'a': 'b', 'c': 'VALUE_TRIMMED'}
def test_trim_dict(self):
ret = dicttrimmer.trim_dict(self.old_dict, 1000)
self.assertEqual(ret, self.new_dict)
class RecursiveDictTrimTestCase(TestCase):
def setUp(self):
self.old_dict = {'a': {'b': 1, 'c': 2, 'e': 'x' * 10000, 'f': '3'}}
self.new_dict = {'a': {'b': 1, 'c': 2, 'e': 'VALUE_TRIMMED', 'f': '3'}}
def test_trim_dict(self):
ret = dicttrimmer.trim_dict(self.old_dict, 1000)
self.assertEqual(ret, self.new_dict)