def test_tls(self, instance): instance = instance.copy() instance['use_tls'] = True instance['connection_properties'] = { 'key': 'hdbuserid', 'sslUseDefaultTrustStore': False, 'foo': ['bar', 'baz'], } check = SapHanaCheck('sap_hana', {}, [instance]) with mock.patch('datadog_checks.sap_hana.sap_hana.HanaConnection') as m: check.get_connection() m.assert_called_once_with( address=instance['server'], port=instance['port'], user=instance['username'], password=instance['password'], communicationTimeout=10000, nodeConnectTimeout=10000, encrypt=True, sslHostNameInCertificate=instance['server'], sslSNIHostname=instance['server'], sslTrustStore=certifi.where(), key='hdbuserid', sslUseDefaultTrustStore=False, foo=['bar', 'baz'], )
def test_custom_queries(aggregator, instance): instance['custom_queries'] = [{ 'tags': ['test:sap_hana'], 'query': 'SELECT DATABASE_NAME, COUNT(*) FROM SYS_DATABASES.M_DATA_VOLUMES GROUP BY DATABASE_NAME', 'columns': [{ 'name': 'db', 'type': 'tag' }, { 'name': 'data_volume.total', 'type': 'gauge' }], }] check = SapHanaCheck('sap_hana', {}, [instance]) check.check(instance) for db in ('SYSTEMDB', 'HXE'): aggregator.assert_metric( 'sap_hana.data_volume.total', metric_type=0, tags=[ 'server:{}'.format(instance['server']), 'port:{}'.format(instance['port']), 'db:{}'.format(db), 'test:sap_hana', ], )
def test_defined(self, instance): instance = instance.copy() instance['timeout'] = 5 instance['connection_properties'] = { 'address': 'foobar', 'nodeConnectTimeout': 1234, 'key': 'hdbuserid', 'sslUseDefaultTrustStore': False, 'foo': ['bar', 'baz'], } check = SapHanaCheck('sap_hana', {}, [instance]) with mock.patch('datadog_checks.sap_hana.sap_hana.HanaConnection') as m: check.get_connection() m.assert_called_once_with( address='foobar', port=instance['port'], user=instance['username'], password=instance['password'], communicationTimeout=5000, nodeConnectTimeout=1234, key='hdbuserid', sslUseDefaultTrustStore=False, foo=['bar', 'baz'], )
def test_check(aggregator, instance): check = SapHanaCheck('sap_hana', {}, [instance]) check.check(instance) for metric in metrics.STANDARD: aggregator.assert_metric_has_tag( metric, 'server:{}'.format(instance['server'])) aggregator.assert_metric_has_tag(metric, 'port:{}'.format(instance['port'])) aggregator.assert_all_metrics_covered()
def test_error_query(instance, dd_run_check): check = SapHanaCheck('sap_hana', {}, [instance]) check.log = mock.MagicMock() conn = mock.MagicMock() cursor = mock.MagicMock() cursor.execute = error conn.cursor = lambda: cursor check._conn = conn dd_run_check(check) check.log.error.assert_any_call('Error querying %s: %s', 'SYS.M_DATABASE', 'test')
def test_default(self, instance): check = SapHanaCheck('sap_hana', {}, [instance]) with mock.patch('datadog_checks.sap_hana.sap_hana.HanaConnection') as m: check.get_connection() m.assert_called_once_with( address=instance['server'], port=instance['port'], user=instance['username'], password=instance['password'], communicationTimeout=10000, nodeConnectTimeout=10000, )
def test_error_unknown(instance, dd_run_check): def query_master_database(): error() check = SapHanaCheck('sap_hana', {}, [instance]) check.log = mock.MagicMock() conn = mock.MagicMock() cursor = mock.MagicMock() cursor.execute = error conn.cursor = lambda: cursor check._conn = conn check._default_methods.append(query_master_database) dd_run_check(check) check.log.exception.assert_any_call('Unexpected error running `%s`: %s', 'query_master_database', 'test')
def test_custom_queries(aggregator, instance_custom_queries, dd_run_check): check = SapHanaCheck('sap_hana', {}, [instance_custom_queries]) dd_run_check(check) for db in ('SYSTEMDB', 'HXE'): aggregator.assert_metric( 'sap_hana.data_volume.total', metric_type=0, tags=[ 'server:{}'.format(instance_custom_queries['server']), 'port:{}'.format(instance_custom_queries['port']), 'db:{}'.format(db), 'test:sap_hana', ], )
def test_only_custom_queries(aggregator, dd_run_check, instance_custom_queries, custom_only): instance_custom_queries['only_custom_queries'] = custom_only check = SapHanaCheck('sap_hana', {}, [instance_custom_queries]) dd_run_check(check) for metric in metrics.STANDARD: if custom_only: aggregator.assert_metric(metric, count=0) else: aggregator.assert_metric(metric, at_least=1) for _db in ('SYSTEMDB', 'HXE'): aggregator.assert_metric('sap_hana.data_volume.total', count=2) aggregator.assert_all_metrics_covered()
def test_error_unknown(instance): def query_master_database(): error() check = SapHanaCheck('sap_hana', {}, [instance]) check.log = mock.MagicMock() conn = mock.MagicMock() cursor = mock.MagicMock() cursor.execute = error conn.cursor = lambda: cursor check._conn = conn check.query_master_database = query_master_database check.check(None) check.log.error.assert_any_call('Unexpected error running `%s`: %s', 'query_master_database', 'test')
def test_custom_query_configuration(instance): def rows_generator(): all_rows = [[['foo', 'bar'], ['foo', 'bar']]] def _get_rows(_): if not all_rows: return return all_rows.pop() return _get_rows check = SapHanaCheck('sap_hana', {}, [instance]) log = mock.MagicMock() gauge = mock.MagicMock() conn = mock.MagicMock() cursor = mock.MagicMock() cursor.fetchmany = rows_generator() conn.cursor.return_value = cursor check._conn = conn check.log = log check.gauge = gauge query = {} check._custom_queries = [query] # No query check.query_custom() log.error.assert_called_once_with('Custom query field `query` is required') log.reset_mock() cursor.fetchmany = rows_generator() # No columns query['query'] = 'foo' check.query_custom() log.error.assert_called_once_with('Custom query field `columns` is required') log.reset_mock() cursor.fetchmany = rows_generator() # Columns count incorrect query['columns'] = [{}] check.query_custom() log.error.assert_any_call('Custom query result expected %s column(s), got %s', 1, 2) assert log.error.call_count == 2 log.reset_mock() cursor.fetchmany = rows_generator() # No name column_1 = {'name': 'baz', 'type': 'tag'} column_2 = {'foo': 'bar'} columns = [column_1, column_2] query['columns'] = columns check.query_custom() log.error.assert_any_call('Column field `name` is required') log.reset_mock() cursor.fetchmany = rows_generator() # No type del column_2['foo'] column_2['name'] = 'foo' check.query_custom() log.error.assert_any_call('Column field `type` is required for column `%s`', 'foo') log.reset_mock() cursor.fetchmany = rows_generator() # Unknown type column_2['type'] = 'unknown' check.query_custom() log.error.assert_any_call('Invalid submission method `%s` for metric column `%s`', 'unknown', 'foo') log.reset_mock() cursor.fetchmany = rows_generator() # Non-numeric value column_2['type'] = 'gauge' check.query_custom() log.error.assert_any_call('Non-numeric value `%s` for metric column `%s`', 'bar', 'foo') log.reset_mock() cursor.fetchmany = rows_generator() gauge.assert_not_called()