|
27 | 27 | STATIC_INFO_VERSION, |
28 | 28 | ) |
29 | 29 | from datadog_checks.sqlserver.metrics import SqlFractionMetric |
30 | | -from datadog_checks.sqlserver.schemas import SQLServerSchemaCollector |
| 30 | +from datadog_checks.sqlserver.schemas import KEY_PREFIX, KEY_PREFIX_PRE_2017, SQLServerSchemaCollector |
31 | 31 | from datadog_checks.sqlserver.sqlserver import SQLConnectionError |
32 | 32 | from datadog_checks.sqlserver.utils import ( |
33 | 33 | Database, |
@@ -219,16 +219,165 @@ def test_schema_collector_uses_database_compatibility_level_for_schema_query( |
219 | 219 | } |
220 | 220 | ) |
221 | 221 | collector._database_compatibility_levels = {"datadog_test": compatibility_level} |
222 | | - cursor = mock.Mock() |
223 | | - collector._check.connection.open_managed_default_connection.return_value = contextlib.nullcontext() |
224 | | - collector._check.connection.get_managed_cursor.return_value = contextlib.nullcontext(cursor) |
| 222 | + main_cursor = mock.Mock() |
| 223 | + detail_cursor = mock.Mock() |
| 224 | + collector._check.connection.open_managed_default_connection.side_effect = [ |
| 225 | + contextlib.nullcontext(), |
| 226 | + contextlib.nullcontext(), |
| 227 | + ] |
| 228 | + collector._check.connection.get_managed_cursor.side_effect = [ |
| 229 | + contextlib.nullcontext(main_cursor), |
| 230 | + contextlib.nullcontext(detail_cursor), |
| 231 | + ] |
225 | 232 |
|
226 | 233 | with collector._get_cursor("datadog_test"): |
227 | 234 | pass |
228 | 235 |
|
229 | | - tables_query = cursor.execute.call_args_list[1][0][0] |
| 236 | + tables_query = main_cursor.execute.call_args_list[1][0][0] |
230 | 237 | assert collector._is_2016_or_earlier is expected_legacy |
231 | 238 | assert ("STRING_AGG" in tables_query) is not expected_legacy |
| 239 | + assert (detail_cursor.execute.call_count == 1) is expected_legacy |
| 240 | + |
| 241 | + |
| 242 | +def test_schema_collector_reuses_pre_2017_connection_for_table_detail_queries() -> None: |
| 243 | + collector = create_schema_collector( |
| 244 | + { |
| 245 | + STATIC_INFO_ENGINE_EDITION: ENGINE_EDITION_STANDARD, |
| 246 | + STATIC_INFO_MAJOR_VERSION: 13, |
| 247 | + } |
| 248 | + ) |
| 249 | + main_cursor = mock.Mock() |
| 250 | + detail_cursor = mock.Mock() |
| 251 | + detail_cursor.fetchall_dict.side_effect = [ |
| 252 | + [{"name": "id"}], |
| 253 | + [{"name": "pk_table_1"}], |
| 254 | + [{"foreign_key_name": "fk_table_1"}], |
| 255 | + [{"name": "id"}], |
| 256 | + [{"name": "pk_table_2"}], |
| 257 | + [{"foreign_key_name": "fk_table_2"}], |
| 258 | + ] |
| 259 | + detail_cursor.fetchone_dict.side_effect = [{"partition_count": 1}, {"partition_count": 2}] |
| 260 | + collector._check.connection.open_managed_default_connection.side_effect = [ |
| 261 | + contextlib.nullcontext(), |
| 262 | + contextlib.nullcontext(), |
| 263 | + ] |
| 264 | + collector._check.connection.get_managed_cursor.side_effect = [ |
| 265 | + contextlib.nullcontext(main_cursor), |
| 266 | + contextlib.nullcontext(detail_cursor), |
| 267 | + ] |
| 268 | + database = {"name": "datadog_test", "id": "1", "collation": "SQL_Latin1_General_CP1_CI_AS", "owner": "dbo"} |
| 269 | + rows = [ |
| 270 | + {"schema_name": "test_schema", "schema_id": 1, "owner_name": "dbo", "table_name": "table_1", "table_id": 101}, |
| 271 | + {"schema_name": "test_schema", "schema_id": 1, "owner_name": "dbo", "table_name": "table_2", "table_id": 102}, |
| 272 | + ] |
| 273 | + |
| 274 | + with collector._get_cursor("datadog_test"): |
| 275 | + mapped_rows = [collector._map_row(database, row) for row in rows] |
| 276 | + |
| 277 | + assert collector._check.connection.open_managed_default_connection.call_args_list == [ |
| 278 | + mock.call(KEY_PREFIX), |
| 279 | + mock.call(KEY_PREFIX_PRE_2017), |
| 280 | + ] |
| 281 | + assert collector._check.connection.get_managed_cursor.call_args_list == [ |
| 282 | + mock.call(KEY_PREFIX), |
| 283 | + mock.call(KEY_PREFIX_PRE_2017), |
| 284 | + ] |
| 285 | + assert detail_cursor.execute.call_args_list[0] == mock.call("USE [datadog_test];") |
| 286 | + assert detail_cursor.execute.call_args_list.count(mock.call("USE [datadog_test];")) == 1 |
| 287 | + assert detail_cursor.execute.call_count == 9 |
| 288 | + assert collector._pre_2017_cursor is None |
| 289 | + assert [row["schemas"][0]["tables"][0]["partitions"]["partition_count"] for row in mapped_rows] == [1, 2] |
| 290 | + |
| 291 | + |
| 292 | +def configure_pre_2017_detail_cursor(cursor: mock.Mock, partition_count: int = 1) -> None: |
| 293 | + cursor.fetchall_dict.side_effect = [ |
| 294 | + [{"name": "id"}], |
| 295 | + [{"name": "pk_table"}], |
| 296 | + [{"foreign_key_name": "fk_table"}], |
| 297 | + ] |
| 298 | + cursor.fetchone_dict.return_value = {"partition_count": partition_count} |
| 299 | + |
| 300 | + |
| 301 | +def test_schema_collector_uses_new_pre_2017_connection_for_each_database() -> None: |
| 302 | + collector = create_schema_collector( |
| 303 | + { |
| 304 | + STATIC_INFO_ENGINE_EDITION: ENGINE_EDITION_STANDARD, |
| 305 | + STATIC_INFO_MAJOR_VERSION: 13, |
| 306 | + } |
| 307 | + ) |
| 308 | + main_cursor_1 = mock.Mock() |
| 309 | + detail_cursor_1 = mock.Mock() |
| 310 | + configure_pre_2017_detail_cursor(detail_cursor_1, partition_count=1) |
| 311 | + main_cursor_2 = mock.Mock() |
| 312 | + detail_cursor_2 = mock.Mock() |
| 313 | + configure_pre_2017_detail_cursor(detail_cursor_2, partition_count=2) |
| 314 | + collector._check.connection.open_managed_default_connection.side_effect = [ |
| 315 | + contextlib.nullcontext(), |
| 316 | + contextlib.nullcontext(), |
| 317 | + contextlib.nullcontext(), |
| 318 | + contextlib.nullcontext(), |
| 319 | + ] |
| 320 | + collector._check.connection.get_managed_cursor.side_effect = [ |
| 321 | + contextlib.nullcontext(main_cursor_1), |
| 322 | + contextlib.nullcontext(detail_cursor_1), |
| 323 | + contextlib.nullcontext(main_cursor_2), |
| 324 | + contextlib.nullcontext(detail_cursor_2), |
| 325 | + ] |
| 326 | + row = {"schema_name": "test_schema", "schema_id": 1, "owner_name": "dbo", "table_name": "table", "table_id": 101} |
| 327 | + |
| 328 | + with collector._get_cursor("datadog_test_1"): |
| 329 | + collector._map_row( |
| 330 | + {"name": "datadog_test_1", "id": "1", "collation": "SQL_Latin1_General_CP1_CI_AS", "owner": "dbo"}, |
| 331 | + row, |
| 332 | + ) |
| 333 | + with collector._get_cursor("datadog_test_2"): |
| 334 | + collector._map_row( |
| 335 | + {"name": "datadog_test_2", "id": "2", "collation": "SQL_Latin1_General_CP1_CI_AS", "owner": "dbo"}, |
| 336 | + row, |
| 337 | + ) |
| 338 | + |
| 339 | + assert collector._check.connection.get_managed_cursor.call_args_list == [ |
| 340 | + mock.call(KEY_PREFIX), |
| 341 | + mock.call(KEY_PREFIX_PRE_2017), |
| 342 | + mock.call(KEY_PREFIX), |
| 343 | + mock.call(KEY_PREFIX_PRE_2017), |
| 344 | + ] |
| 345 | + assert detail_cursor_1.execute.call_args_list[0] == mock.call("USE [datadog_test_1];") |
| 346 | + assert detail_cursor_2.execute.call_args_list[0] == mock.call("USE [datadog_test_2];") |
| 347 | + assert collector._pre_2017_cursor is None |
| 348 | + |
| 349 | + |
| 350 | +def test_schema_collector_does_not_open_pre_2017_connection_for_modern_query() -> None: |
| 351 | + collector = create_schema_collector( |
| 352 | + { |
| 353 | + STATIC_INFO_ENGINE_EDITION: ENGINE_EDITION_STANDARD, |
| 354 | + STATIC_INFO_MAJOR_VERSION: 14, |
| 355 | + } |
| 356 | + ) |
| 357 | + collector._database_compatibility_levels = {"datadog_test": 130} |
| 358 | + cursor = mock.Mock() |
| 359 | + collector._check.connection.open_managed_default_connection.return_value = contextlib.nullcontext() |
| 360 | + collector._check.connection.get_managed_cursor.return_value = contextlib.nullcontext(cursor) |
| 361 | + database = {"name": "datadog_test", "id": "1", "collation": "SQL_Latin1_General_CP1_CI_AS", "owner": "dbo"} |
| 362 | + row = { |
| 363 | + "schema_name": "test_schema", |
| 364 | + "schema_id": 1, |
| 365 | + "owner_name": "dbo", |
| 366 | + "table_name": "table", |
| 367 | + "table_id": 101, |
| 368 | + "columns": '[{"name": "id"}]', |
| 369 | + "indexes": '[{"name": "pk_table"}]', |
| 370 | + "foreign_keys": '[{"foreign_key_name": "fk_table"}]', |
| 371 | + "partition_count": 1, |
| 372 | + } |
| 373 | + |
| 374 | + with collector._get_cursor("datadog_test"): |
| 375 | + mapped_row = collector._map_row(database, row) |
| 376 | + |
| 377 | + assert collector._check.connection.open_managed_default_connection.call_args_list == [mock.call(KEY_PREFIX)] |
| 378 | + assert collector._check.connection.get_managed_cursor.call_args_list == [mock.call(KEY_PREFIX)] |
| 379 | + assert collector._pre_2017_cursor is None |
| 380 | + assert mapped_row["schemas"][0]["tables"][0]["columns"] == [{"name": "id"}] |
232 | 381 |
|
233 | 382 |
|
234 | 383 | def test_get_cursor(instance_docker): |
|
0 commit comments