Kaydet (Commit) e4b6d0dc authored tarafından Aanand Prasad's avatar Aanand Prasad

Convert dicts to Healthcheck objects, string commands to CMD-SHELL lists

Signed-off-by: 's avatarAanand Prasad <aanand.prasad@gmail.com>
üst 6bb7844a
from .base import DictType
import six
class Healthcheck(DictType):
def __init__(self, **kwargs):
test = kwargs.get('test', kwargs.get('Test'))
if isinstance(test, six.string_types):
test = ["CMD-SHELL", test]
interval = kwargs.get('interval', kwargs.get('Interval'))
timeout = kwargs.get('timeout', kwargs.get('Timeout'))
retries = kwargs.get('retries', kwargs.get('Retries'))
super(Healthcheck, self).__init__({
'Test': test,
'Interval': interval,
......
......@@ -18,7 +18,7 @@ import six
from .. import constants
from .. import errors
from .. import tls
from ..types import Ulimit, LogConfig
from ..types import Ulimit, LogConfig, Healthcheck
if six.PY2:
from urllib import splitnport
......@@ -1119,6 +1119,9 @@ def create_container_config(
# Force None, an empty list or dict causes client.start to fail
volumes_from = None
if healthcheck and isinstance(healthcheck, dict):
healthcheck = Healthcheck(**healthcheck)
attach_stdin = False
attach_stdout = False
attach_stderr = False
......
import docker
from .base import BaseIntegrationTest
from .base import BUSYBOX
from .. import helpers
......@@ -7,50 +5,47 @@ from .. import helpers
SECOND = 1000000000
def wait_on_health_status(client, container, status):
def condition():
res = client.inspect_container(container)
return res['State']['Health']['Status'] == status
return helpers.wait_on_condition(condition)
class HealthcheckTest(BaseIntegrationTest):
@helpers.requires_api_version('1.24')
def test_healthcheck_passes(self):
healthcheck = docker.types.Healthcheck(
test=["CMD-SHELL", "true"],
interval=1*SECOND,
timeout=1*SECOND,
retries=1,
)
def test_healthcheck_shell_command(self):
container = self.client.create_container(
BUSYBOX, 'top', healthcheck=healthcheck)
BUSYBOX, 'top', healthcheck=dict(test='echo "hello world"'))
self.tmp_containers.append(container)
res = self.client.inspect_container(container)
assert res['Config']['Healthcheck'] == {
"Test": ["CMD-SHELL", "true"],
"Interval": 1*SECOND,
"Timeout": 1*SECOND,
"Retries": 1,
}
def condition():
res = self.client.inspect_container(container)
return res['State']['Health']['Status'] == "healthy"
assert res['Config']['Healthcheck']['Test'] == \
['CMD-SHELL', 'echo "hello world"']
@helpers.requires_api_version('1.24')
def test_healthcheck_passes(self):
container = self.client.create_container(
BUSYBOX, 'top', healthcheck=dict(
test="true",
interval=1*SECOND,
timeout=1*SECOND,
retries=1,
))
self.tmp_containers.append(container)
self.client.start(container)
helpers.wait_on_condition(condition)
wait_on_health_status(self.client, container, "healthy")
@helpers.requires_api_version('1.24')
def test_healthcheck_fails(self):
healthcheck = docker.types.Healthcheck(
test=["CMD-SHELL", "false"],
interval=1*SECOND,
timeout=1*SECOND,
retries=1,
)
container = self.client.create_container(
BUSYBOX, 'top', healthcheck=healthcheck)
BUSYBOX, 'top', healthcheck=dict(
test="false",
interval=1*SECOND,
timeout=1*SECOND,
retries=1,
))
self.tmp_containers.append(container)
def condition():
res = self.client.inspect_container(container)
return res['State']['Health']['Status'] == "unhealthy"
self.client.start(container)
helpers.wait_on_condition(condition)
wait_on_health_status(self.client, container, "unhealthy")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment