-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathtest_unit.py
More file actions
457 lines (373 loc) · 18.3 KB
/
Copy pathtest_unit.py
File metadata and controls
457 lines (373 loc) · 18.3 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# (C) Datadog, Inc. 2019-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
import mock
import pytest
from clickhouse_connect.driver.exceptions import Error, OperationalError
from datadog_checks.base import ConfigurationError
from datadog_checks.clickhouse import ClickhouseCheck, advanced_queries, queries
from datadog_checks.clickhouse.utils import cluster_aware_query
from .utils import ensure_csv_safe, parse_described_metrics, raise_error
pytestmark = pytest.mark.unit
def test_config(instance):
check = ClickhouseCheck('clickhouse', {}, [instance])
check.check_id = 'test-clickhouse'
with mock.patch('clickhouse_connect.get_client') as m:
mock_client = mock.MagicMock()
m.return_value = mock_client
check.connect()
m.assert_called_once_with(
host=instance['server'],
port=instance['port'],
username=instance['username'],
password=instance['password'],
database='default',
connect_timeout=10,
send_receive_timeout=10,
secure=False,
ca_cert=None,
verify=True,
client_name='datadog-test-clickhouse',
compress=False,
autogenerate_session_id=False,
settings={},
pool_mgr=mock.ANY,
)
def test_config_verify_false(instance):
"""Regression: verify: false must be forwarded to the shared pool manager.
When pool_mgr is provided to clickhouse-connect, it skips creating its own
TLS-aware pool, so TLS settings must be baked into the pool at creation time.
"""
instance = {**instance, 'verify': False}
with mock.patch('clickhouse_connect.driver.httputil.get_pool_manager') as mock_pool:
mock_pool.return_value = mock.MagicMock()
ClickhouseCheck('clickhouse', {}, [instance])
mock_pool.assert_called_once_with(maxsize=8, num_pools=4, verify=False, ca_cert=None)
def test_config_tls_ca_cert_forwarded_to_pool_manager(instance):
"""Regression: tls_ca_cert must be forwarded to the shared pool manager.
Same failure mode as verify=False: if ca_cert isn't baked into the pre-supplied
pool manager, clickhouse-connect's get_client can't apply it later.
"""
instance = {**instance, 'tls_ca_cert': '/path/to/ca.pem'}
with mock.patch('clickhouse_connect.driver.httputil.get_pool_manager') as mock_pool:
mock_pool.return_value = mock.MagicMock()
ClickhouseCheck('clickhouse', {}, [instance])
mock_pool.assert_called_once_with(maxsize=8, num_pools=4, verify=True, ca_cert='/path/to/ca.pem')
def test_error_query(instance, dd_run_check):
check = ClickhouseCheck('clickhouse', {}, [instance])
check.log = mock.MagicMock()
check.get_queries = lambda _: []
client = mock.MagicMock()
client.execute_iter = raise_error
check._client = client
with pytest.raises(Exception):
dd_run_check(check)
@pytest.mark.latest_metrics
@pytest.mark.parametrize(
'metrics, ignored_columns, metric_source_url',
[
(
advanced_queries.SystemMetrics['columns'][1]['items'],
{'Revision', 'VersionInteger'},
'https://raw.githubusercontent.com/ClickHouse/ClickHouse/master/src/Common/CurrentMetrics.cpp',
),
(
advanced_queries.SystemEvents['columns'][1]['items'],
set(),
'https://raw.githubusercontent.com/ClickHouse/ClickHouse/master/src/Common/ProfileEvents.cpp',
),
],
ids=['SystemMetrics', 'SystemEvents'],
)
def test_latest_metrics_supported(metrics, ignored_columns, metric_source_url):
assert list(metrics) == sorted(metrics)
described_metrics = parse_described_metrics(metric_source_url)
difference = set(described_metrics).difference(metrics).difference(ignored_columns)
if difference: # no cov
num_metrics = len(difference)
raise AssertionError(
'{} newly documented metric{}!\n{}'.format(
num_metrics,
's' if num_metrics > 1 else '',
'\n'.join(
'---> {} | {}'.format(metric, ensure_csv_safe(described_metrics[metric]))
for metric in sorted(difference)
),
)
)
@mock.patch('datadog_checks.base.AgentCheck.is_metadata_collection_enabled', return_value=False)
def test_can_connect_submits_on_every_check_run(is_metadata_collection_enabled, aggregator, instance):
"""
Regression test: a copy of the `can_connect` service check must be submitted for each check run.
(It used to be submitted only once on check init, which led to customer seeing "no data" in the UI.)
"""
check = ClickhouseCheck('clickhouse', {}, [instance])
with mock.patch("datadog_checks.clickhouse.clickhouse.clickhouse_connect"):
# Test for consecutive healthy clickhouse.can_connect statuses
num_runs = 3
for _ in range(num_runs):
check.check({})
aggregator.assert_service_check("clickhouse.can_connect", count=num_runs, status=check.OK)
@mock.patch('datadog_checks.base.AgentCheck.is_metadata_collection_enabled', return_value=False)
def test_can_connect_recovers_after_failed_connection(is_metadata_collection_enabled, aggregator, instance):
check = ClickhouseCheck('clickhouse', {}, [instance])
# Test 1 healthy connection --> 2 Unhealthy service checks --> 1 healthy connection. Recovered
with mock.patch("datadog_checks.clickhouse.clickhouse.clickhouse_connect"):
check.check({})
with mock.patch('clickhouse_connect.get_client', side_effect=OperationalError('Connection refused')):
with mock.patch('datadog_checks.clickhouse.ClickhouseCheck.ping_clickhouse', return_value=False):
with pytest.raises(Exception):
check.check({})
with pytest.raises(Exception):
check.check({})
with mock.patch("datadog_checks.clickhouse.clickhouse.clickhouse_connect"):
check.check({})
aggregator.assert_service_check("clickhouse.can_connect", count=2, status=check.CRITICAL)
aggregator.assert_service_check("clickhouse.can_connect", count=2, status=check.OK)
@mock.patch('datadog_checks.base.AgentCheck.is_metadata_collection_enabled', return_value=False)
def test_can_connect_recovers_after_failed_ping(is_metadata_collection_enabled, aggregator, instance):
check = ClickhouseCheck('clickhouse', {}, [instance])
# Test Exception in ping_clickhouse(), but reestablishes connection.
with mock.patch("datadog_checks.clickhouse.clickhouse.clickhouse_connect"):
check.check({})
with mock.patch('datadog_checks.clickhouse.ClickhouseCheck.ping_clickhouse', side_effect=Error()):
# connect() should be able to handle an exception in ping_clickhouse() and attempt reconnection
check.check({})
check.check({})
aggregator.assert_service_check("clickhouse.can_connect", count=3, status=check.OK)
def test_validate_config(instance):
instance['compression'] = 'invalid-compression-type'
check = ClickhouseCheck('clickhouse', {}, [instance])
with pytest.raises(ConfigurationError):
check.validate_config()
def test_deprecated_user_option():
"""Test that the deprecated 'user' option is migrated to 'username' with a warning."""
instance = {
'server': 'localhost',
'port': 8128,
'user': 'datadog', # Using deprecated option
'password': 'test123',
}
check = ClickhouseCheck('clickhouse', {}, [instance])
# Check that username was set from user
assert check._config.username == 'datadog'
# Check that deprecation warning was added
assert any('user' in warning and 'deprecated' in warning.lower() for warning in check._validation_result.warnings)
def test_deprecated_user_option_with_username():
"""Test that username takes precedence over user when both are provided."""
instance = {
'server': 'localhost',
'port': 8128,
'user': 'old_user', # Using deprecated option
'username': 'new_user', # New option takes precedence
'password': 'test123',
}
check = ClickhouseCheck('clickhouse', {}, [instance])
# Check that username was preferred
assert check._config.username == 'new_user'
# Check that deprecation warning was still added
assert any('user' in warning and 'deprecated' in warning.lower() for warning in check._validation_result.warnings)
def test_deprecated_host_option():
"""Test that the deprecated 'host' option is migrated to 'server' with a warning."""
instance = {
'host': 'localhost', # Using deprecated option
'port': 8128,
'username': 'datadog',
'password': 'test123',
}
check = ClickhouseCheck('clickhouse', {}, [instance])
# Check that server was set from host
assert check._config.server == 'localhost'
# Check that deprecation warning was added
assert any('host' in warning and 'deprecated' in warning.lower() for warning in check._validation_result.warnings)
def test_missing_server_config():
"""Test that missing server/host configuration triggers an error."""
instance = {
# Missing both 'server' and 'host'
'port': 8128,
'username': 'datadog',
'password': 'test123',
}
check = ClickhouseCheck('clickhouse', {}, [instance])
# The error should be in the validation result
assert not check._validation_result.valid
assert any('server' in str(error).lower() for error in check._validation_result.errors)
def test_connect_no_password_uses_empty_string():
"""
Regression test: when no password is configured, connect() must pass password=''
not password=None. clickhouse_connect encodes None as the literal string 'None'
in the Authorization header, causing ClickHouse error code 194 (auth failure).
"""
instance = {
'server': 'localhost',
'port': 8123,
'username': 'default',
# 'password' intentionally omitted
}
check = ClickhouseCheck('clickhouse', {}, [instance])
check.check_id = 'test-no-password'
assert check._config.password == '', (
"password must default to '' — None causes auth error 194 in clickhouse_connect"
)
with mock.patch('clickhouse_connect.get_client') as m:
mock_client = mock.MagicMock()
m.return_value = mock_client
check.connect()
_, kwargs = m.call_args
assert kwargs['password'] == '', "connect() must pass password='' not password=None to clickhouse_connect"
@pytest.mark.parametrize(
['ch_version', 'comparable', 'expected'],
[
('25', 'latest', True),
('25', '25', False),
('25.1', '25.2', True),
('25.1.2.3', '25.1.2.10', True),
('25.1', '25.3', True),
('23.1', '25.1', True),
],
)
def test_version_lt(instance, ch_version, comparable, expected):
check = ClickhouseCheck('clickhouse', {}, [instance])
check._server_version = ch_version
assert check.version_lt(comparable) == expected
@pytest.mark.parametrize(
['ch_version', 'comparable', 'expected'],
[
('25', 'latest', False),
('25', '25', True),
('25.1.2.3', '25.1.2', True),
('25.1.2.3', '25.1.2.3', True),
('25.1', '25.3', False),
('23.1', '25.1', False),
],
)
def test_version_ge(instance, ch_version, comparable, expected):
check = ClickhouseCheck('clickhouse', {}, [instance])
check._server_version = ch_version
assert check.version_ge(comparable) == expected
@pytest.mark.parametrize("bad_value", [0, -1, -100])
def test_query_errors_zero_samples_per_hour_defaults(bad_value):
"""Zero or negative samples_per_hour_per_query must not crash the constructor via ZeroDivisionError."""
instance = {
'server': 'localhost',
'port': 9000,
'username': 'default',
'dbm': True,
'query_errors': {'enabled': True, 'samples_per_hour_per_query': bad_value},
}
check = ClickhouseCheck('clickhouse', {}, [instance])
assert check._config.query_errors.samples_per_hour_per_query > 0
assert any('query_errors.samples_per_hour_per_query' in w for w in check._validation_result.warnings)
@pytest.mark.parametrize("bad_value", [0, -1, -100])
def test_query_completions_zero_samples_per_hour_defaults(bad_value):
"""Zero or negative samples_per_hour_per_query must not crash the constructor via ZeroDivisionError."""
instance = {
'server': 'localhost',
'port': 9000,
'username': 'default',
'dbm': True,
'query_completions': {'enabled': True, 'samples_per_hour_per_query': bad_value},
}
check = ClickhouseCheck('clickhouse', {}, [instance])
assert check._config.query_completions.samples_per_hour_per_query > 0
assert any('query_completions.samples_per_hour_per_query' in w for w in check._validation_result.warnings)
@pytest.mark.parametrize("bad_value", [0, -1, -100])
def test_collect_schemas_zero_collection_interval_defaults(bad_value):
"""Zero or negative collection_interval must not crash the constructor via ZeroDivisionError."""
instance = {
'server': 'localhost',
'port': 9000,
'username': 'default',
'dbm': True,
'collect_schemas': {'enabled': True, 'collection_interval': bad_value},
}
check = ClickhouseCheck('clickhouse', {}, [instance])
assert check._config.collect_schemas.collection_interval > 0
assert any('collect_schemas.collection_interval' in w for w in check._validation_result.warnings)
BASE_INSTANCE = {'server': 'myhost.example.com', 'port': 8123, 'username': 'default'}
def test_reported_hostname_explicit_config():
instance = {**BASE_INSTANCE, 'reported_hostname': 'custom-host'}
check = ClickhouseCheck('clickhouse', {}, [instance])
assert check.reported_hostname == 'custom-host'
@pytest.mark.parametrize('loopback', ['localhost', '127.0.0.1'])
def test_reported_hostname_loopback_substitutes_agent_hostname(loopback):
instance = {**BASE_INSTANCE, 'server': loopback}
with mock.patch('datadog_checks.clickhouse.clickhouse.resolve_db_host', return_value='my-agent-host'):
check = ClickhouseCheck('clickhouse', {}, [instance])
assert check.reported_hostname == 'my-agent-host'
@pytest.mark.parametrize(
'reported_hostname, expected_reported_hostname',
[
pytest.param(None, 'resolved-host', id='no-override'),
pytest.param('custom-host', 'custom-host', id='with-override'),
],
)
def test_database_hostname_ignores_reported_hostname_override(reported_hostname, expected_reported_hostname):
instance = {**BASE_INSTANCE}
if reported_hostname:
instance['reported_hostname'] = reported_hostname
with mock.patch(
'datadog_checks.clickhouse.clickhouse.resolve_db_host', return_value='resolved-host'
) as mock_resolve:
check = ClickhouseCheck('clickhouse', {}, [instance])
# database_hostname always resolves the real host, regardless of the override
assert check.database_hostname == 'resolved-host'
# reported_hostname honors the override when configured, otherwise the resolved host
assert check.reported_hostname == expected_reported_hostname
mock_resolve.assert_called_with(BASE_INSTANCE['server'])
def test_cluster_aware_query_bulk_match_query():
"""The cluster-aware variant reads all replicas and tags system.events per node."""
variant = cluster_aware_query(advanced_queries.SystemEvents)
assert variant['query'] == (
"SELECT value, event, hostName() AS clickhouse_node FROM clusterAllReplicas('default', system.events)"
)
assert variant['columns'][-1] == {'name': 'clickhouse_node', 'type': 'tag'}
# The base query dict must not be mutated by building the variant.
assert advanced_queries.SystemEvents['query'] == 'SELECT value, event FROM system.events'
assert all(column['name'] != 'clickhouse_node' for column in advanced_queries.SystemEvents['columns'])
def test_cluster_aware_query_preserves_where_clause():
"""system.errors carries a WHERE clause that must survive in the cluster-aware variant."""
variant = cluster_aware_query(advanced_queries.SystemErrors)
assert variant['query'] == (
"SELECT value, name, code, remote, hostName() AS clickhouse_node "
"FROM clusterAllReplicas('default', system.errors) WHERE value > 0"
)
assert variant['columns'][-1] == {'name': 'clickhouse_node', 'type': 'tag'}
def test_cluster_aware_query_legacy_query():
"""The helper builds a cluster-aware variant for a legacy query too."""
variant = cluster_aware_query(queries.SystemMetrics)
assert variant['query'] == (
"SELECT value, metric, hostName() AS clickhouse_node FROM clusterAllReplicas('default', system.metrics)"
)
assert variant['columns'][-1] == {'name': 'clickhouse_node', 'type': 'tag'}
assert queries.SystemMetrics['query'] == 'SELECT value, metric FROM system.metrics'
@pytest.mark.parametrize('use_advanced_queries', [True, False])
def test_get_queries_tags_system_tables_per_node_in_single_endpoint_mode(instance, use_advanced_queries):
instance = {
**instance,
'single_endpoint_mode': True,
'use_advanced_queries': use_advanced_queries,
'use_legacy_queries': not use_advanced_queries,
}
check = ClickhouseCheck('clickhouse', {}, [instance])
check._server_version = '24.8'
cluster_aware = [q for q in check.get_queries() if 'clusterAllReplicas' in q['query']]
# system.events, system.metrics, system.asynchronous_metrics (+ system.errors / events-to-deprecate)
assert cluster_aware
for query in cluster_aware:
assert 'hostName() AS clickhouse_node' in query['query']
assert query['columns'][-1] == {'name': 'clickhouse_node', 'type': 'tag'}
# system.parts/replicas/dictionaries use GROUP BY and are intentionally left untouched here.
if not use_advanced_queries:
assert any(q is queries.SystemParts for q in check.get_queries())
@pytest.mark.parametrize('use_advanced_queries', [True, False])
def test_get_queries_uses_base_queries_for_direct_connection(instance, use_advanced_queries):
instance = {
**instance,
'single_endpoint_mode': False,
'use_advanced_queries': use_advanced_queries,
'use_legacy_queries': not use_advanced_queries,
}
check = ClickhouseCheck('clickhouse', {}, [instance])
check._server_version = '24.8'
assert all('clusterAllReplicas' not in q['query'] for q in check.get_queries())