mirror of https://github.com/docker/docker-py.git
Rework healthcheck integration test
Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com>
This commit is contained in:
parent
b4f2b5fa70
commit
6bb7844ab3
|
@ -2,6 +2,7 @@ import os
|
|||
import os.path
|
||||
import tarfile
|
||||
import tempfile
|
||||
import time
|
||||
|
||||
import docker
|
||||
import pytest
|
||||
|
@ -47,3 +48,11 @@ def requires_api_version(version):
|
|||
),
|
||||
reason="API version is too low (< {0})".format(version)
|
||||
)
|
||||
|
||||
|
||||
def wait_on_condition(condition, delay=0.1, timeout=40):
|
||||
start_time = time.time()
|
||||
while not condition():
|
||||
if time.time() - start_time > timeout:
|
||||
raise AssertionError("Timeout: %s" % condition)
|
||||
time.sleep(delay)
|
||||
|
|
|
@ -1,35 +1,56 @@
|
|||
import time
|
||||
import docker
|
||||
|
||||
from ..base import requires_api_version
|
||||
from .base import BaseIntegrationTest
|
||||
from .base import BUSYBOX
|
||||
from .. import helpers
|
||||
|
||||
SECOND = 1000000000
|
||||
|
||||
class HealthcheckTest(helpers.BaseTestCase):
|
||||
|
||||
@requires_api_version('1.21')
|
||||
def test_healthcheck(self):
|
||||
class HealthcheckTest(BaseIntegrationTest):
|
||||
|
||||
@helpers.requires_api_version('1.24')
|
||||
def test_healthcheck_passes(self):
|
||||
healthcheck = docker.types.Healthcheck(
|
||||
test=["CMD-SHELL",
|
||||
"foo.txt || (/bin/usleep 10000 && touch foo.txt)"],
|
||||
interval=500000,
|
||||
timeout=1000000000,
|
||||
retries=1
|
||||
test=["CMD-SHELL", "true"],
|
||||
interval=1*SECOND,
|
||||
timeout=1*SECOND,
|
||||
retries=1,
|
||||
)
|
||||
container = self.client.create_container(helpers.BUSYBOX, 'cat',
|
||||
detach=True, stdin_open=True,
|
||||
healthcheck=healthcheck)
|
||||
id = container['Id']
|
||||
self.client.start(id)
|
||||
self.tmp_containers.append(id)
|
||||
res1 = self.client.inspect_container(id)
|
||||
self.assertIn('State', res1)
|
||||
self.assertIn('Health', res1['State'])
|
||||
self.assertIn('Status', res1['State']['Health'])
|
||||
self.assertEqual(res1['State']['Health']['Status'], "starting")
|
||||
time.sleep(0.5)
|
||||
res2 = self.client.inspect_container(id)
|
||||
self.assertIn('State', res2)
|
||||
self.assertIn('Health', res2['State'])
|
||||
self.assertIn('Status', res2['State']['Health'])
|
||||
self.assertEqual(res2['State']['Health']['Status'], "healthy")
|
||||
container = self.client.create_container(
|
||||
BUSYBOX, 'top', healthcheck=healthcheck)
|
||||
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"
|
||||
|
||||
self.client.start(container)
|
||||
helpers.wait_on_condition(condition)
|
||||
|
||||
@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)
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue