Skip to content

Commit

Permalink
fixed test_deadlock_xml_bad_format
Browse files Browse the repository at this point in the history
  • Loading branch information
nenadnoveljic committed Sep 23, 2024
1 parent 8ba285b commit a36be88
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
1 change: 0 additions & 1 deletion sqlserver/datadog_checks/sqlserver/deadlocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ def collect_deadlocks(self):
if rows:
deadlocks_event = self._create_deadlock_event(rows)
payload = json.dumps(deadlocks_event, default=default_json_event_encoding)
breakpoint()
self._log.debug("Deadlocks payload: %s", str(payload))
self._check.database_monitoring_query_activity(payload)

Expand Down
43 changes: 42 additions & 1 deletion sqlserver/tests/test_deadlocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,21 @@ def test_deadlocks(aggregator, dd_run_check, init_config, dbm_instance):
def _load_test_deadlocks_xml(filename):
with open(os.path.join(DEADLOCKS_PLAN_DIR, filename), 'r') as f:
return f.read()

@pytest.fixture
def deadlocks_collection_instance(instance_docker):
instance_docker['dbm'] = True
instance_docker['deadlocks_collection'] = {
'enabled': True,
'collection_interval': 1.0,
}
instance_docker['min_collection_interval'] = 1
# do not need other dbm metrics
instance_docker['query_activity'] = {'enabled': False}
instance_docker['query_metrics'] = {'enabled': False}
instance_docker['procedure_metrics'] = {'enabled': False}
instance_docker['collect_settings'] = {'enabled': False}
return copy(instance_docker)

def test__create_deadlock_rows():
deadlocks_obj = None
Expand All @@ -187,4 +202,30 @@ def test__create_deadlock_rows():
assert len(query_signatures) == 2, "Should have two query signatures"
first_mapping = query_signatures[0]
assert "spid" in first_mapping, "Should have spid in query signatures"
assert isinstance(first_mapping["spid"], int), "spid should be an int"
assert isinstance(first_mapping["spid"], int), "spid should be an int"

def test_deadlock_xml_bad_format(deadlocks_collection_instance):
test_xml = """
<event name="xml_deadlock_report" package="sqlserver" timestamp="2024-08-20T08:30:35.762Z">
<data name="xml_report">
<type name="xml" package="package0"/>
<value>
<deadlock>
<victim-list>
<victimProcess id="process12108eb088"/>
</victim-list>
</deadlock>
</value>
</data>
</event>
"""
check = SQLServer(CHECK_NAME, {}, [deadlocks_collection_instance])
deadlocks = check.deadlocks
root = ET.fromstring(test_xml)
try:
deadlocks._obfuscate_xml(root)
except Exception as e:
result = str(e)
assert result == "process-list element not found. The deadlock XML is in an unexpected format."
else:
assert False, "Should have raised an exception for bad XML format"

0 comments on commit a36be88

Please sign in to comment.