20
20
from datadog_checks .sqlserver .utils import (
21
21
Database ,
22
22
extract_sql_comments_and_procedure_name ,
23
+ get_unixodbc_sysconfig ,
24
+ is_non_empty_file ,
23
25
parse_sqlserver_major_version ,
24
26
set_default_driver_conf ,
25
27
)
26
28
27
29
from .common import CHECK_NAME , DOCKER_SERVER , assert_metrics
28
- from .utils import deep_compare , windows_ci
30
+ from .utils import deep_compare , not_windows_ci , windows_ci
29
31
30
32
try :
31
33
import pyodbc
@@ -435,6 +437,12 @@ def test_set_default_driver_conf():
435
437
set_default_driver_conf ()
436
438
assert os .environ ['ODBCSYSINI' ].endswith (os .path .join ('data' , 'driver_config' ))
437
439
440
+ with mock .patch ("datadog_checks.base.utils.platform.Platform.is_linux" , return_value = True ):
441
+ with EnvVars ({}, ignore = ['ODBCSYSINI' ]):
442
+ set_default_driver_conf ()
443
+ assert 'ODBCSYSINI' in os .environ , "ODBCSYSINI should be set"
444
+ assert os .environ ['ODBCSYSINI' ].endswith (os .path .join ('data' , 'driver_config' ))
445
+
438
446
# `set_default_driver_conf` have no effect on the cases below
439
447
with EnvVars ({'ODBCSYSINI' : 'ABC' , 'DOCKER_DD_AGENT' : 'true' }):
440
448
set_default_driver_conf ()
@@ -446,23 +454,27 @@ def test_set_default_driver_conf():
446
454
assert 'ODBCSYSINI' in os .environ
447
455
assert os .environ ['ODBCSYSINI' ].endswith (os .path .join ('tests' , 'odbc' ))
448
456
449
- with EnvVars ({}, ignore = ['ODBCSYSINI' ]):
450
- with mock .patch ("os.path.exists" , return_value = True ):
451
- # odbcinst.ini or odbc.ini exists in agent embedded directory
452
- set_default_driver_conf ()
453
- assert 'ODBCSYSINI' not in os .environ
454
-
455
- with EnvVars ({}, ignore = ['ODBCSYSINI' ]):
456
- set_default_driver_conf ()
457
- assert 'ODBCSYSINI' in os .environ # ODBCSYSINI is set by the integration
458
- if pyodbc is not None :
459
- assert pyodbc .drivers () is not None
460
-
461
457
with EnvVars ({'ODBCSYSINI' : 'ABC' }):
462
458
set_default_driver_conf ()
463
459
assert os .environ ['ODBCSYSINI' ] == 'ABC'
464
460
465
461
462
+ @not_windows_ci
463
+ def test_set_default_driver_conf_linux ():
464
+ odbc_config_dir = os .path .expanduser ('~' )
465
+ with mock .patch ("datadog_checks.sqlserver.utils.get_unixodbc_sysconfig" , return_value = odbc_config_dir ):
466
+ with EnvVars ({}, ignore = ['ODBCSYSINI' ]):
467
+ odbc_inst = os .path .join (odbc_config_dir , "odbcinst.ini" )
468
+ odbc_ini = os .path .join (odbc_config_dir , "odbc.ini" )
469
+ for file in [odbc_inst , odbc_ini ]:
470
+ if os .path .exists (file ):
471
+ os .remove (file )
472
+ with open (odbc_ini , "x" ) as file :
473
+ file .write ("dummy-content" )
474
+ set_default_driver_conf ()
475
+ assert is_non_empty_file (odbc_inst ), "odbc_inst should have been created when a non empty odbc.ini exists"
476
+
477
+
466
478
@windows_ci
467
479
def test_check_local (aggregator , dd_run_check , init_config , instance_docker ):
468
480
sqlserver_check = SQLServer (CHECK_NAME , init_config , [instance_docker ])
@@ -866,3 +878,16 @@ def test_exception_handling_by_do_for_dbs(instance_docker):
866
878
'datadog_checks.sqlserver.utils.is_azure_sql_database' , return_value = {}
867
879
):
868
880
schemas ._fetch_for_databases ()
881
+
882
+
883
+ def test_get_unixodbc_sysconfig ():
884
+ etc_dir = os .path .sep
885
+ for dir in ["opt" , "datadog-agent" , "embedded" , "bin" , "python" ]:
886
+ etc_dir = os .path .join (etc_dir , dir )
887
+ assert get_unixodbc_sysconfig (etc_dir ).split (os .path .sep ) == [
888
+ "" ,
889
+ "opt" ,
890
+ "datadog-agent" ,
891
+ "embedded" ,
892
+ "etc" ,
893
+ ], "incorrect unix odbc config dir"
0 commit comments