Skip to content

Commit

Permalink
[network] move 'core/test_service_checks' tests
Browse files Browse the repository at this point in the history
Add tests for `instance_name` tag normalization.
Move 'core/test_service_checks.py' tests to
'checks/integration/test_http_check.py' and
'checks/integration/test_tcp_check.py'.
  • Loading branch information
yannmh committed Aug 4, 2015
1 parent 63ef7d3 commit 73aeaf9
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 193 deletions.
112 changes: 104 additions & 8 deletions tests/checks/integration/test_http_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@
import mock

# project
from config import AGENT_VERSION
from tests.checks.common import AgentCheckTest
from util import headers as agent_headers

RESULTS_TIMEOUT = 5

AGENT_CONFIG = {
'version': AGENT_VERSION,
'api_key': 'toto'
}

CONFIG = {
'instances': [{
'name': 'conn_error',
Expand Down Expand Up @@ -77,6 +84,26 @@
]
}

CONFIG_UNORMALIZED_INSTANCE_NAME = {
'instances': [{
'name': '_need-to__be_normalized-',
'url': 'https://github.com',
'timeout': 1,
'check_certificate_expiration': True,
'days_warning': 14
},
]
}

CONFIG_HTTP_HEADERS = {
'instances': [{
'url': 'https://google.com',
'name': 'UpService',
'timeout': 1,
'headers': {"X-Auth-Token": "SOME-AUTH-TOKEN"}
}]
}


FAKE_CERT = {'notAfter': 'Apr 12 12:00:00 2006 GMT'}

Expand All @@ -85,23 +112,48 @@ class HTTPCheckTest(AgentCheckTest):
CHECK_NAME = 'http_check'

def tearDown(self):
self.check.stop()
if self.check:
self.check.stop()

def wait_for_async_service_checks(self, count):
def wait_for_async(self, method, attribute, count):
"""
Loop on `self.check.method` until `self.check.attribute >= count`.
Raise after
"""
i = 0
while i < RESULTS_TIMEOUT:
self.check._process_results()
if len(self.check.service_checks) >= count:
return self.check.get_service_checks()
if len(getattr(self.check, attribute)) >= count:
return getattr(self.check, method)()
time.sleep(1)
i += 1
raise Exception("Didn't get the right count of service checks in time {0}"
.format(self.check.service_checks))
.format(getattr(self.check, attribute)))

def test_http_headers(self):
"""
Headers format.
"""
# Run the check
self.load_check(CONFIG_HTTP_HEADERS, AGENT_CONFIG)

url, username, password, http_response_status_code, timeout,\
include_content, headers, response_time, content_match,\
tags, ssl, ssl_expiration,\
instance_ca_certs = self.check._load_conf(CONFIG_HTTP_HEADERS['instances'][0])

self.assertEqual(headers["X-Auth-Token"], "SOME-AUTH-TOKEN", headers)
expected_headers = agent_headers(AGENT_CONFIG).get('User-Agent')
self.assertEqual(expected_headers, headers.get('User-Agent'), headers)

def test_check(self):
"""
Check coverage.
"""
self.run_check(CONFIG)
# Overrides self.service_checks attribute when values are available\
self.service_checks = self.wait_for_async_service_checks(5)
self.service_checks = self.wait_for_async('get_service_checks', 'service_checks', 5)

# HTTP connection error
tags = ['url:https://thereisnosuchlink.com', 'instance:conn_error']
Expand Down Expand Up @@ -130,7 +182,7 @@ def test_check(self):
def test_check_ssl(self):
self.run_check(CONFIG_SSL_ONLY)
# Overrides self.service_checks attribute when values are available
self.service_checks = self.wait_for_async_service_checks(6)
self.service_checks = self.wait_for_async('get_service_checks', 'service_checks', 6)
tags = ['url:https://github.com', 'instance:good_cert']
self.assertServiceCheckOK("http.can_connect", tags=tags)
self.assertServiceCheckOK("http.ssl_cert", tags=tags)
Expand All @@ -150,8 +202,52 @@ def test_mock_case(self, getpeercert_func):
self.run_check(CONFIG_EXPIRED_SSL)
# Overrides self.service_checks attribute when values are av
# Needed for the HTTP headers
self.service_checks = self.wait_for_async_service_checks(2)
self.service_checks = self.wait_for_async('get_service_checks', 'service_checks', 2)
tags = ['url:https://github.com', 'instance:expired_cert']
self.assertServiceCheckOK("http.can_connect", tags=tags)
self.assertServiceCheckCritical("http.ssl_cert", tags=tags)
self.coverage_report()

def test_service_check_instance_name_normalization(self):
"""
Service check `instance` tag value is normalized.
Note: necessary to avoid mismatch and backward incompatiblity.
"""
# Run the check
self.run_check(CONFIG_UNORMALIZED_INSTANCE_NAME)

# Overrides self.service_checks attribute when values are available
self.service_checks = self.wait_for_async('get_service_checks', 'service_checks', 2)

# Assess instance name normalization
tags = ['url:https://github.com', 'instance:need_to_be_normalized']
self.assertServiceCheckOK("http.can_connect", tags=tags)
self.assertServiceCheckOK("http.ssl_cert", tags=tags)

def test_warnings(self):
"""
Deprecate events usage for service checks.
"""
self.run_check(CONFIG)

# Overrides self.service_checks attribute when values are available\
self.warnings = self.wait_for_async('get_warnings', 'warnings', 8)

# Assess warnings
self.assertWarning(
"Skipping SSL certificate validation for "
"https://github.com based on configuration",
count=2
)
self.assertWarning(
"Skipping SSL certificate validation for "
"https://thereisnosuchlink.com based on configuration",
count=1
)
self.assertWarning(
"Using events for service checks is deprecated in "
"favor of monitors and will be removed in future versions of the "
"Datadog Agent.",
count=5
)
89 changes: 89 additions & 0 deletions tests/checks/integration/test_tcp_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# stdlibb
import time

# project
from tests.checks.common import AgentCheckTest

RESULTS_TIMEOUT = 5

CONFIG = {
'init_config': {},
'instances': [{
'host': '127.0.0.1',
'port': 65530,
'timeout': 1,
'name': 'DownService'
}, {
'host': '126.0.0.1',
'port': 65530,
'timeout': 1,
'name': 'DownService2'
}, {
'host': 'datadoghq.com',
'port': 80,
'timeout': 1,
'name': 'UpService'
}]
}


class TCPCheckTest(AgentCheckTest):
CHECK_NAME = 'tcp_check'

def tearDown(self):
self.check.stop()

def wait_for_async(self, method, attribute, count):
"""
Loop on `self.check.method` until `self.check.attribute >= count`.
Raise after
"""
i = 0
while i < RESULTS_TIMEOUT:
self.check._process_results()
if len(getattr(self.check, attribute)) >= count:
return getattr(self.check, method)()
time.sleep(1)
i += 1
raise Exception("Didn't get the right count of service checks in time {0}"
.format(getattr(self.check, attribute)))

def test_event_deprecation(self):
"""
Deprecate events usage for service checks.
"""
# Run the check
self.run_check(CONFIG)

# Overrides self.service_checks attribute when values are available
self.warnings = self.wait_for_async('get_warnings', 'warnings', 3)

# Assess warnings
self.assertWarning(
"Using events for service checks is deprecated in "
"favor of monitors and will be removed in future versions of the "
"Datadog Agent.",
count=3
)

def test_check(self):
"""
Check coverage.
"""
# Run the check
self.run_check(CONFIG)

# Overrides self.service_checks attribute when values are available
self.service_checks = self.wait_for_async('get_service_checks', 'service_checks', 3)

expected_tags = ["instance:DownService", "target_host:127.0.0.1", "port:65530"]
self.assertServiceCheckCritical("tcp.can_connect", tags=expected_tags)

expected_tags = ["instance:DownService2", "target_host:126.0.0.1", "port:65530"]
self.assertServiceCheckCritical("tcp.can_connect", tags=expected_tags)

expected_tags = ["instance:UpService", "target_host:datadoghq.com", "port:80"]
self.assertServiceCheckOK("tcp.can_connect", tags=expected_tags)

self.coverage_report()
Loading

0 comments on commit 73aeaf9

Please sign in to comment.