Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shmsr committed Aug 2, 2023
1 parent d72ca05 commit 7ae6948
Showing 1 changed file with 11 additions and 27 deletions.
38 changes: 11 additions & 27 deletions x-pack/metricbeat/module/sql/query/test_sql_mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,29 @@ class Test(XPackTest):
def test_query_without_fetch_from_all_databases(self):
"""
SQL MSSQL custom query with fetch_from_all_databases=false
"""
self.render_config_template(modules=[{
"name": "sql",
"metricsets": ["query"],
"hosts": ['sqlserver://{}:{}@{}'.format(self.get_username(), self.get_password(), self.compose_host())],
"period": "5s",
"additional_content": """
driver: mssql
fetch_from_all_databases: false
sql_query: SELECT DB_NAME() AS 'database_name';
sql_response_format: table"""
}])

proc = self.start_beat()
self.wait_until(lambda: self.output_lines() > 0)
proc.check_kill_and_wait()
self.assert_no_logged_warnings()
output = self.read_output_json()
self.assertEqual(len(output), 1)

for evt in output:
self.assert_fields_are_documented(evt)
self.assertIn("sql", evt.keys(), evt)
self.assertIn("query", evt["sql"].keys(), evt)
1 document will be received from the default selected database: 'master'.
"""
self.run_query_test(fetch_from_all_databases=False, expected_output_count=1)

@unittest.skipUnless(metricbeat.INTEGRATION_TESTS, "integration test")
def test_query_with_fetch_from_all_databases(self):
"""
SQL MSSQL custom query with fetch_from_all_databases=true
4 documents will be received because 4 databases are present by default in MSSQL and 1 document per database will received.
"""
self.run_query_test(fetch_from_all_databases=True, expected_output_count=4)

def run_query_test(self, fetch_from_all_databases: bool, expected_output_count: int) -> list:
self.render_config_template(modules=[{
"name": "sql",
"metricsets": ["query"],
"hosts": ['sqlserver://{}:{}@{}'.format(self.get_username(), self.get_password(), self.compose_host())],
"period": "5s",
"additional_content": """
"additional_content": f"""
driver: mssql
fetch_from_all_databases: true
fetch_from_all_databases: {str(fetch_from_all_databases).lower()}
sql_query: SELECT DB_NAME() AS 'database_name';
sql_response_format: table"""
}])
Expand All @@ -61,7 +45,7 @@ def test_query_with_fetch_from_all_databases(self):
self.assert_no_logged_warnings()

output = self.read_output_json()
self.assertEqual(len(output), 4)
self.assertEqual(len(output), expected_output_count)

for evt in output:
self.assert_fields_are_documented(evt)
Expand Down

0 comments on commit 7ae6948

Please sign in to comment.