-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathtest_metadata.py
More file actions
388 lines (359 loc) · 15.4 KB
/
Copy pathtest_metadata.py
File metadata and controls
388 lines (359 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# (C) Datadog, Inc. 2023-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
from __future__ import unicode_literals
import logging
from copy import copy, deepcopy
import pytest
from datadog_checks.dev.utils import running_on_windows_ci
from datadog_checks.sqlserver import SQLServer
from .common import CHECK_NAME
from .utils import deep_compare, normalize_ids, normalize_indexes_columns
try:
import pyodbc
except ImportError:
pyodbc = None
def normalize_compatibility_level(actual_payload):
assert actual_payload['compatibility_level'].isdigit()
actual_payload['compatibility_level'] = 'normalized_value'
@pytest.fixture
def dbm_instance(instance_docker):
instance_docker['dbm'] = True
instance_docker['min_collection_interval'] = 1
instance_docker['query_metrics'] = {'enabled': False}
instance_docker['query_activity'] = {'enabled': False}
instance_docker['procedure_metrics'] = {'enabled': False}
# set a very small collection interval so the tests go fast
instance_docker['collect_settings'] = {
'enabled': True,
'run_sync': True,
'collection_interval': 0.1,
}
return copy(instance_docker)
@pytest.mark.integration
@pytest.mark.usefixtures('dd_environment')
@pytest.mark.parametrize(
"expected_columns,available_columns",
[
[
["name", "value"],
["name", "value"],
],
[
["name", "value", "some_missing_column"],
["name", "value"],
],
],
)
def test_get_available_settings_columns(dbm_instance, expected_columns, available_columns):
check = SQLServer(CHECK_NAME, {}, [dbm_instance])
check.initialize_connection()
_conn_key_prefix = "dbm-metadata-"
with check.connection.open_managed_default_connection(_conn_key_prefix):
with check.connection.get_managed_cursor(_conn_key_prefix) as cursor:
result_available_columns = check.sql_metadata._get_available_settings_columns(cursor, expected_columns)
assert result_available_columns == available_columns
@pytest.mark.integration
@pytest.mark.usefixtures('dd_environment')
def test_get_settings_query_cached(dbm_instance, caplog):
caplog.set_level(logging.DEBUG)
check = SQLServer(CHECK_NAME, {}, [dbm_instance])
check.initialize_connection()
_conn_key_prefix = "dbm-metadata"
with check.connection.open_managed_default_connection(_conn_key_prefix):
with check.connection.get_managed_cursor(_conn_key_prefix) as cursor:
for _ in range(3):
query = check.sql_metadata._get_settings_query_cached(cursor)
assert query, "query should be non-empty"
times_columns_loaded = 0
for r in caplog.records:
if r.message.startswith("found available sys.configurations columns"):
times_columns_loaded += 1
assert times_columns_loaded == 1, "columns should have been loaded only once"
def test_sqlserver_collect_settings(aggregator, dd_run_check, dbm_instance):
check = SQLServer(CHECK_NAME, {}, [dbm_instance])
# dd_run_check(check)
check.initialize_connection()
check.check(dbm_instance)
dbm_metadata = aggregator.get_event_platform_events("dbm-metadata")
event = next((e for e in dbm_metadata if e['kind'] == 'sqlserver_configs'), None)
assert event is not None
assert event['dbms'] == "sqlserver"
assert event['kind'] == "sqlserver_configs"
assert len(event["metadata"]) > 0
def test_collect_schemas(aggregator, dd_run_check, dbm_instance):
databases_to_find = ['datadog_test_schemas', 'datadog_test_schemas_second', 'datadog_test_collation']
exp_datadog_test = {
'id': 'normalized_value',
'name': 'datadog_test_schemas_second',
"collation": "SQL_Latin1_General_CP1_CI_AS",
'owner': 'dbo',
'compatibility_level': 'normalized_value',
'schemas': [
{
'name': 'dbo',
'id': 'normalized_value',
'owner_name': 'dbo',
'tables': [
{
'id': 'normalized_value',
'name': 'ϑings',
'columns': [
{
'name': 'id',
'data_type': 'int',
'default': '((0))',
'nullable': True,
},
{
'name': 'name',
'data_type': 'varchar',
'default': 'None',
'nullable': True,
},
],
'foreign_keys': [],
'partitions': {'partition_count': 1},
'indexes': [
{
'name': 'thingsindex',
'type': 1,
'is_unique': False,
'is_primary_key': False,
'is_unique_constraint': False,
'is_disabled': False,
'column_names': 'name',
}
],
}
],
}
],
}
exp_datadog_test_schemas = {
'id': 'normalized_value',
'name': 'datadog_test_schemas',
"collation": "SQL_Latin1_General_CP1_CI_AS",
'owner': 'dbo',
'compatibility_level': 'normalized_value',
'schemas': [
{
'name': 'test_schema',
'id': 'normalized_value',
'owner_name': 'dbo',
'tables': [
{
'id': 'normalized_value',
'name': 'cities',
'columns': [
{
'name': 'id',
'data_type': 'int',
'default': '((0))',
'nullable': False,
},
{
'name': 'name',
'data_type': 'varchar',
'default': 'None',
'nullable': True,
},
{
'name': 'population',
'data_type': 'int',
'default': '((0))',
'nullable': False,
},
],
'foreign_keys': [],
'partitions': {'partition_count': 12},
'indexes': [
{
'name': 'PK_Cities',
'type': 1,
'is_unique': True,
'is_primary_key': True,
'is_unique_constraint': False,
'is_disabled': False,
'column_names': 'id',
},
{
'name': 'single_column_index',
'type': 2,
'is_unique': False,
'is_primary_key': False,
'is_unique_constraint': False,
'is_disabled': False,
'column_names': 'id,population',
},
{
'name': 'two_columns_index',
'type': 2,
'is_unique': False,
'is_primary_key': False,
'is_unique_constraint': False,
'is_disabled': False,
'column_names': 'id,name',
},
],
},
{
'id': 'normalized_value',
'name': 'landmarks',
'columns': [
{
'name': 'name',
'data_type': 'varchar',
'default': 'None',
'nullable': True,
},
{
'name': 'city_id',
'data_type': 'int',
'default': '((0))',
'nullable': True,
},
],
'indexes': [],
'partitions': {'partition_count': 1},
'foreign_keys': [
{
'foreign_key_name': 'FK_CityId',
'referencing_table': 'landmarks',
'referencing_column': 'city_id',
'referenced_table': 'cities',
'referenced_column': 'id',
"delete_action": "SET_NULL",
"update_action": "NO_ACTION",
}
],
},
{
'id': 'normalized_value',
'name': 'RestaurantReviews',
'columns': [
{
'name': 'RestaurantName',
'data_type': 'varchar',
'default': 'None',
'nullable': True,
},
{
'name': 'District',
'data_type': 'varchar',
'default': 'None',
'nullable': True,
},
{
'name': 'Review',
'data_type': 'varchar',
'default': 'None',
'nullable': True,
},
],
'indexes': [],
'partitions': {'partition_count': 1},
'foreign_keys': [
{
'foreign_key_name': 'FK_RestaurantNameDistrict',
'referencing_table': 'RestaurantReviews',
'referencing_column': 'RestaurantName,District',
'referenced_table': 'Restaurants',
'referenced_column': 'RestaurantName,District',
"delete_action": "CASCADE",
"update_action": "SET_NULL",
}
],
},
{
'id': 'normalized_value',
'name': 'Restaurants',
'columns': [
{
'name': 'RestaurantName',
'data_type': 'varchar',
'default': 'None',
'nullable': True,
},
{
'name': 'District',
'data_type': 'varchar',
'default': 'None',
'nullable': True,
},
{
'name': 'Cuisine',
'data_type': 'varchar',
'default': 'None',
'nullable': True,
},
],
'foreign_keys': [],
'partitions': {'partition_count': 2},
'indexes': [
{
'name': 'UC_RestaurantNameDistrict',
'type': 2,
'is_unique': True,
'is_primary_key': False,
'is_unique_constraint': True,
'is_disabled': False,
'column_names': 'District,RestaurantName',
}
],
},
],
}
],
}
exp_datadog_test_collation = deepcopy(exp_datadog_test_schemas)
exp_datadog_test_collation['name'] = 'datadog_test_collation'
exp_datadog_test_collation['collation'] = 'Latin1_General_100_BIN2'
if running_on_windows_ci():
exp_datadog_test['owner'] = 'None'
exp_datadog_test_schemas['owner'] = 'None'
exp_datadog_test_collation['owner'] = 'None'
expected_data_for_db = {
'datadog_test_schemas_second': exp_datadog_test,
'datadog_test_schemas': exp_datadog_test_schemas,
'datadog_test_collation': exp_datadog_test_collation,
}
dbm_instance['database_autodiscovery'] = True
dbm_instance['autodiscovery_include'] = [
'datadog_test_schemas',
'datadog_test_schemas_second',
'datadog_test_collation',
]
dbm_instance['dbm'] = True
dbm_instance['collect_schemas'] = {"enabled": True}
check = SQLServer(CHECK_NAME, {}, [dbm_instance])
dd_run_check(check)
dbm_metadata = aggregator.get_event_platform_events("dbm-metadata")
actual_payloads = {}
for schema_event in (e for e in dbm_metadata if e['kind'] == 'sqlserver_databases'):
assert schema_event.get("timestamp") is not None
assert schema_event["host"] == "stubbed.hostname"
assert schema_event["agent_version"] == "0.0.0"
assert schema_event["dbms"] == "sqlserver"
assert schema_event.get("collection_interval") is not None
assert schema_event.get("dbms_version") is not None
database_metadata = schema_event['metadata']
for db_metadata in database_metadata:
db_name = db_metadata['name']
if db_name in actual_payloads:
actual_payloads[db_name]['schemas'][0]['tables'] = (
actual_payloads[db_name]['schemas'][0]['tables'] + db_metadata['schemas'][0]['tables']
)
else:
actual_payloads[db_name] = db_metadata
assert len(actual_payloads) == len(expected_data_for_db)
for db_name, actual_payload in actual_payloads.items():
assert db_name in databases_to_find
# id's are env dependant
normalize_ids(actual_payload)
# compatibility_level varies by SQL Server version
normalize_compatibility_level(actual_payload)
# index columns may be in any order
normalize_indexes_columns(actual_payload)
matches = deep_compare(actual_payload, expected_data_for_db[db_name])
assert matches