diff --git a/tests/testing_support/validators/validate_log_events.py b/tests/testing_support/validators/validate_log_events.py index c8618b3904..b343bf228e 100644 --- a/tests/testing_support/validators/validate_log_events.py +++ b/tests/testing_support/validators/validate_log_events.py @@ -20,8 +20,9 @@ function_wrapper) from testing_support.fixtures import catch_background_exceptions -def validate_log_events(events=None, forgone_attrs=None): - events = events or [{}] # Empty event allows assertions based on only forgone attrs to still run and validate +def validate_log_events(events=None, required_attrs=None, forgone_attrs=None): + events = events or [{}] # Empty event allows assertions based on only required or forgone attrs to still run and validate + required_attrs = required_attrs or [] forgone_attrs = forgone_attrs or [] @function_wrapper @@ -57,14 +58,14 @@ def _validate_log_events(wrapped, instance, args, kwargs): matching_log_events = 0 mismatches = [] for captured in logs: - if _check_log_attributes(expected, forgone_attrs, captured, mismatches): + if _check_log_attributes(expected, required_attrs, forgone_attrs, captured, mismatches): matching_log_events += 1 assert matching_log_events == 1, _log_details(matching_log_events, logs, mismatches) return val - def _check_log_attributes(expected, forgone_attrs, captured, mismatches): + def _check_log_attributes(expected, required_attrs, forgone_attrs, captured, mismatches): for key, value in six.iteritems(expected): if hasattr(captured, key): captured_value = getattr(captured, key, None) @@ -79,6 +80,11 @@ def _check_log_attributes(expected, forgone_attrs, captured, mismatches): mismatches.append("key: %s, value:<%s><%s>" % (key, value, captured_value)) return False + for key in required_attrs: + if not hasattr(captured, key) and key not in captured.attributes: + mismatches.append("required_key: %s" % key) + return False + for key in forgone_attrs: if hasattr(captured, key): mismatches.append("forgone_key: %s, value:<%s>" % (key, getattr(captured, key, None)))