Skip to content

Commit

Permalink
Refactor health check methods to use requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Shay Arbov committed Jun 22, 2017
1 parent 1150ced commit 4c29843
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
28 changes: 18 additions & 10 deletions docker_test_tools/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import logging
import subprocess

import httplib
import waiting

import requests

log = logging.getLogger(__name__)

Expand All @@ -28,29 +27,38 @@ def run_health_checks(checks, interval=1, timeout=60):
return False


def is_curl_responsive(address):
"""Return True if the address is responsive using curl.
def is_responsive(address, expected_status=httplib.OK):
"""Return True if the address is responsive.
:param string address: url address 'hostname:port'.
:param int expected_status: expected response status code.
:return bool: True is the address is responsive, False otherwise.
"""
return subprocess.call(['curl', '-s', address], stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0
try:
return requests.get(address, timeout=5).status_code == expected_status
except:
return False


def get_curl_health_check(service_name, url):
def get_health_check(service_name, url, expected_status=httplib.OK):
"""Return a function used to determine if the given service is responsive.
:param string service_name: service name.
:param string url: service url 'hostname:port'.
:param int expected_status: expected response status code.
:return function: function used to determine if the given service is responsive.
"""
log.debug('Defining a CURL based health check for service: %s at: %s', service_name, url)

def curl_health_check():
def url_health_check():
"""Return True if the service is responsive."""
is_ready = is_curl_responsive(url)
is_ready = is_responsive(url, expected_status)
log.debug('Service %s ready: %s', service_name, is_ready)
return is_ready

return curl_health_check
return url_health_check


# For backward compatability
get_curl_health_check = get_health_check
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pbr==1.8
waiting==1.3.0
requests==2.13.0
docker-compose==1.13.0
requests-unixsocket==0.1.5
1 change: 0 additions & 1 deletion test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ nose2==0.6.5
flake8==2.6.2
pylint==1.7.1
pytest==3.0.6
requests==2.13.0
cov-core==1.15.0
docker-compose==1.11.2
14 changes: 7 additions & 7 deletions tests/ut/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import mock
import unittest
import subprocess

from waiting import TimeoutExpired
from docker_test_tools import utils


class TestUtils(unittest.TestCase):
@mock.patch('subprocess.call')
def test_run_health_checks(self, call_mock):
@mock.patch('requests.get')
def test_run_health_checks(self, get_mock):
"""Validate the run_health_checks function."""
call_mock.return_value = 0
get_mock.return_value = mock.MagicMock(status_code=200)
utils.run_health_checks([utils.get_curl_health_check('service1', 'first_url'),
utils.get_curl_health_check('service2', 'second_url')])
utils.get_curl_health_check('service2', 'second_url')],
timeout=5)

call_mock.assert_any_call(['curl', '-s', 'first_url'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
call_mock.assert_any_call(['curl', '-s', 'second_url'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
get_mock.assert_any_call('first_url', timeout=5)
get_mock.assert_any_call('second_url', timeout=5)

with mock.patch("waiting.wait", return_value=True):
self.assertTrue(utils.run_health_checks([]))
Expand Down

0 comments on commit 4c29843

Please sign in to comment.