|
11 | 11 | import mock |
12 | 12 | import pytest |
13 | 13 |
|
| 14 | +from datadog_checks.base.stubs.datadog_agent import datadog_agent |
14 | 15 | from datadog_checks.dev import EnvVars |
15 | 16 | from datadog_checks.sqlserver import SQLServer |
16 | 17 | from datadog_checks.sqlserver.connection import split_sqlserver_host_port |
@@ -908,6 +909,71 @@ def test_split_sqlserver_host(instance_host, split_host, split_port): |
908 | 909 | assert (s_host, s_port) == (split_host, split_port) |
909 | 910 |
|
910 | 911 |
|
| 912 | +AGENT_HOSTNAME = 'sql-agent-host.example.com' |
| 913 | + |
| 914 | + |
| 915 | +@pytest.fixture |
| 916 | +def agent_hostname_for_resolve_db_host(): |
| 917 | + datadog_agent.set_hostname(AGENT_HOSTNAME) |
| 918 | + yield |
| 919 | + datadog_agent.reset_hostname() |
| 920 | + |
| 921 | + |
| 922 | +@pytest.mark.parametrize( |
| 923 | + 'instance_host,host_part', |
| 924 | + [ |
| 925 | + (r'SQL-HOST01\INSTANCE01,1601', r'SQL-HOST01\INSTANCE01'), |
| 926 | + (r'MY-SERVER\SQLEXPRESS,1433', r'MY-SERVER\SQLEXPRESS'), |
| 927 | + (r'MY-SERVER\SQLEXPRESS', r'MY-SERVER\SQLEXPRESS'), |
| 928 | + ], |
| 929 | +) |
| 930 | +def test_resolve_db_host_named_instance_returns_agent_hostname( |
| 931 | + agent_hostname_for_resolve_db_host, instance_host, host_part |
| 932 | +): |
| 933 | + instance = { |
| 934 | + 'host': instance_host, |
| 935 | + 'username': 'datadog', |
| 936 | + 'password': 'secret', |
| 937 | + } |
| 938 | + check = SQLServer(CHECK_NAME, {}, [instance]) |
| 939 | + assert check.host == host_part |
| 940 | + |
| 941 | + # Agent 7.79+ base resolver returns the literal host string for unresolvable names. |
| 942 | + with mock.patch( |
| 943 | + 'datadog_checks.sqlserver.sqlserver.agent_host_resolver', |
| 944 | + return_value=host_part, |
| 945 | + ): |
| 946 | + assert check.resolve_db_host() == AGENT_HOSTNAME |
| 947 | + assert check.resolved_hostname == AGENT_HOSTNAME |
| 948 | + assert check.database_hostname == AGENT_HOSTNAME |
| 949 | + |
| 950 | + |
| 951 | +@pytest.mark.parametrize( |
| 952 | + 'instance_host,host_part,base_resolver_return', |
| 953 | + [ |
| 954 | + ('db.example.com,1433', 'db.example.com', 'resolved-db.example.com'), |
| 955 | + ('192.0.2.10,1433', '192.0.2.10', '192.0.2.10'), |
| 956 | + ], |
| 957 | +) |
| 958 | +def test_resolve_db_host_plain_host_delegates_to_base_resolver( |
| 959 | + agent_hostname_for_resolve_db_host, instance_host, host_part, base_resolver_return |
| 960 | +): |
| 961 | + instance = { |
| 962 | + 'host': instance_host, |
| 963 | + 'username': 'datadog', |
| 964 | + 'password': 'secret', |
| 965 | + } |
| 966 | + check = SQLServer(CHECK_NAME, {}, [instance]) |
| 967 | + assert check.host == host_part |
| 968 | + |
| 969 | + with mock.patch( |
| 970 | + 'datadog_checks.sqlserver.sqlserver.agent_host_resolver', |
| 971 | + return_value=base_resolver_return, |
| 972 | + ) as mock_resolver: |
| 973 | + assert check.resolve_db_host() == base_resolver_return |
| 974 | + mock_resolver.assert_called_once_with(host_part) |
| 975 | + |
| 976 | + |
911 | 977 | @pytest.mark.parametrize( |
912 | 978 | "query,expected_comments,is_proc,expected_name", |
913 | 979 | [ |
|
0 commit comments