|
5 | 5 | import logging |
6 | 6 | import time |
7 | 7 | from copy import copy |
| 8 | +from unittest.mock import Mock |
8 | 9 |
|
9 | 10 | import pytest |
10 | 11 |
|
11 | 12 | from datadog_checks.sqlserver import SQLServer |
| 13 | +from datadog_checks.sqlserver.agent_history import SqlserverAgentHistory |
12 | 14 |
|
13 | 15 | from .common import ( |
14 | 16 | CHECK_NAME, |
|
96 | 98 | message |
97 | 99 | FROM HISTORY_ENTRIES |
98 | 100 | WHERE |
99 | | - completion_epoch_time > {last_collection_time_filter}; |
| 101 | + completion_epoch_time > ?; |
100 | 102 | """ |
101 | 103 |
|
102 | 104 |
|
|
176 | 178 | message |
177 | 179 | FROM HISTORY_ENTRIES |
178 | 180 | WHERE |
179 | | - completion_epoch_time > 10000; |
| 181 | + completion_epoch_time > ?; |
180 | 182 | """ |
181 | 183 |
|
182 | 184 | AGENT_ACTIVITY_DURATION_QUERY = """\ |
@@ -404,16 +406,52 @@ def test_connection_with_agent_history(instance_docker): |
404 | 406 |
|
405 | 407 | with check.connection.open_managed_default_connection(KEY_PREFIX): |
406 | 408 | with check.connection.get_managed_cursor(KEY_PREFIX) as cursor: |
407 | | - last_collection_time_filter = "{last_collection_time}".format(last_collection_time=10000) |
408 | 409 | history_row_limit_filter = "TOP {history_row_limit}".format(history_row_limit=10000) |
409 | | - query = AGENT_HISTORY_QUERY.format( |
410 | | - history_row_limit_filter=history_row_limit_filter, |
411 | | - last_collection_time_filter=last_collection_time_filter, |
412 | | - ) |
413 | | - cursor.execute(query) |
| 410 | + query = AGENT_HISTORY_QUERY.format(history_row_limit_filter=history_row_limit_filter) |
| 411 | + cursor.execute(query, (10000,)) |
414 | 412 | assert query == FORMATTED_HISTORY_QUERY |
415 | 413 |
|
416 | 414 |
|
| 415 | +class AgentHistoryCursor: |
| 416 | + description = [('run_epoch_time',), ('run_duration_seconds',)] |
| 417 | + |
| 418 | + def __init__(self) -> None: |
| 419 | + self.executions: list[tuple[str, tuple[int, ...]]] = [] |
| 420 | + |
| 421 | + def execute(self, query: str, params: tuple[int, ...]) -> None: |
| 422 | + self.executions.append((query, params)) |
| 423 | + |
| 424 | + def fetchall(self) -> list[tuple[int, int]]: |
| 425 | + return [] |
| 426 | + |
| 427 | + |
| 428 | +class AgentHistoryCheck: |
| 429 | + name = CHECK_NAME |
| 430 | + |
| 431 | + def __init__(self) -> None: |
| 432 | + self.log = Mock() |
| 433 | + self.count = Mock() |
| 434 | + self.gauge = Mock() |
| 435 | + self.histogram = Mock() |
| 436 | + |
| 437 | + |
| 438 | +def test_agent_history_query_parameterizes_last_collection_time() -> None: |
| 439 | + check = AgentHistoryCheck() |
| 440 | + agent_history = object.__new__(SqlserverAgentHistory) |
| 441 | + agent_history._check = check |
| 442 | + agent_history.log = check.log |
| 443 | + agent_history.history_row_limit = 10000 |
| 444 | + agent_history._last_collection_time = 10000 |
| 445 | + cursor = AgentHistoryCursor() |
| 446 | + |
| 447 | + agent_history._get_new_agent_job_history(cursor) |
| 448 | + |
| 449 | + query, params = cursor.executions[0] |
| 450 | + assert "completion_epoch_time > ?;" in query |
| 451 | + assert "completion_epoch_time > 10000;" not in query |
| 452 | + assert params == (10000,) |
| 453 | + |
| 454 | + |
417 | 455 | @pytest.mark.usefixtures('dd_environment') |
418 | 456 | def test_connection_with_agent_activity_duration(instance_docker): |
419 | 457 | check = SQLServer(CHECK_NAME, {}, [instance_docker]) |
@@ -466,23 +504,15 @@ def test_history_output(instance_docker, sa_conn): |
466 | 504 | check.initialize_connection() |
467 | 505 | with check.connection.open_managed_default_connection(KEY_PREFIX): |
468 | 506 | with check.connection.get_managed_cursor(KEY_PREFIX) as cursor: |
469 | | - last_collection_time_filter = "{last_collection_time}".format(last_collection_time=now - 1) |
470 | 507 | history_row_limit_filter = "TOP {history_row_limit}".format(history_row_limit=10000) |
471 | | - query = AGENT_HISTORY_QUERY.format( |
472 | | - history_row_limit_filter=history_row_limit_filter, |
473 | | - last_collection_time_filter=last_collection_time_filter, |
474 | | - ) |
475 | | - cursor.execute(query) |
| 508 | + query = AGENT_HISTORY_QUERY.format(history_row_limit_filter=history_row_limit_filter) |
| 509 | + cursor.execute(query, (now - 1,)) |
476 | 510 | results = cursor.fetchall() |
477 | 511 | assert len(results) == 7, "should have 7 steps associated with completed jobs" |
478 | 512 | assert len(results[0]) == 10, "should have 10 columns per step" |
479 | | - last_collection_time_filter = "{last_collection_time}".format(last_collection_time=now + 1) |
480 | 513 | history_row_limit_filter = "TOP {history_row_limit}".format(history_row_limit=10000) |
481 | | - query = AGENT_HISTORY_QUERY.format( |
482 | | - history_row_limit_filter=history_row_limit_filter, |
483 | | - last_collection_time_filter=last_collection_time_filter, |
484 | | - ) |
485 | | - cursor.execute(query) |
| 514 | + query = AGENT_HISTORY_QUERY.format(history_row_limit_filter=history_row_limit_filter) |
| 515 | + cursor.execute(query, (now + 1,)) |
486 | 516 | results = cursor.fetchall() |
487 | 517 | assert len(results) == 4, ( |
488 | 518 | "should only have 4 steps associated with completed jobs when filtering with last collection time" |
|
0 commit comments