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 os.path
|
||||||
import tarfile
|
import tarfile
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import time
|
||||||
|
|
||||||
import docker
|
import docker
|
||||||
import pytest
|
import pytest
|
||||||
|
@ -47,3 +48,11 @@ def requires_api_version(version):
|
||||||
),
|
),
|
||||||
reason="API version is too low (< {0})".format(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
|
import docker
|
||||||
|
|
||||||
from ..base import requires_api_version
|
from .base import BaseIntegrationTest
|
||||||
|
from .base import BUSYBOX
|
||||||
from .. import helpers
|
from .. import helpers
|
||||||
|
|
||||||
|
SECOND = 1000000000
|
||||||
|
|
||||||
class HealthcheckTest(helpers.BaseTestCase):
|
|
||||||
|
|
||||||
@requires_api_version('1.21')
|
class HealthcheckTest(BaseIntegrationTest):
|
||||||
def test_healthcheck(self):
|
|
||||||
|
@helpers.requires_api_version('1.24')
|
||||||
|
def test_healthcheck_passes(self):
|
||||||
healthcheck = docker.types.Healthcheck(
|
healthcheck = docker.types.Healthcheck(
|
||||||
test=["CMD-SHELL",
|
test=["CMD-SHELL", "true"],
|
||||||
"foo.txt || (/bin/usleep 10000 && touch foo.txt)"],
|
interval=1*SECOND,
|
||||||
interval=500000,
|
timeout=1*SECOND,
|
||||||
timeout=1000000000,
|
retries=1,
|
||||||
retries=1
|
|
||||||
)
|
)
|
||||||
container = self.client.create_container(helpers.BUSYBOX, 'cat',
|
container = self.client.create_container(
|
||||||
detach=True, stdin_open=True,
|
BUSYBOX, 'top', healthcheck=healthcheck)
|
||||||
healthcheck=healthcheck)
|
self.tmp_containers.append(container)
|
||||||
id = container['Id']
|
|
||||||
self.client.start(id)
|
res = self.client.inspect_container(container)
|
||||||
self.tmp_containers.append(id)
|
assert res['Config']['Healthcheck'] == {
|
||||||
res1 = self.client.inspect_container(id)
|
"Test": ["CMD-SHELL", "true"],
|
||||||
self.assertIn('State', res1)
|
"Interval": 1*SECOND,
|
||||||
self.assertIn('Health', res1['State'])
|
"Timeout": 1*SECOND,
|
||||||
self.assertIn('Status', res1['State']['Health'])
|
"Retries": 1,
|
||||||
self.assertEqual(res1['State']['Health']['Status'], "starting")
|
}
|
||||||
time.sleep(0.5)
|
|
||||||
res2 = self.client.inspect_container(id)
|
def condition():
|
||||||
self.assertIn('State', res2)
|
res = self.client.inspect_container(container)
|
||||||
self.assertIn('Health', res2['State'])
|
return res['State']['Health']['Status'] == "healthy"
|
||||||
self.assertIn('Status', res2['State']['Health'])
|
|
||||||
self.assertEqual(res2['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