File size: 2,387 Bytes
065fee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from .. import helpers
from .base import TEST_IMG, BaseAPIIntegrationTest

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(BaseAPIIntegrationTest):

    @helpers.requires_api_version('1.24')
    def test_healthcheck_shell_command(self):
        container = self.client.create_container(
            TEST_IMG, 'top', healthcheck={'test': 'echo "hello world"'})
        self.tmp_containers.append(container)

        res = self.client.inspect_container(container)
        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(
            TEST_IMG, 'top', healthcheck={
                'test': "true",
                'interval': 1 * SECOND,
                'timeout': 1 * SECOND,
                'retries': 1,
            })
        self.tmp_containers.append(container)
        self.client.start(container)
        wait_on_health_status(self.client, container, "healthy")

    @helpers.requires_api_version('1.24')
    def test_healthcheck_fails(self):
        container = self.client.create_container(
            TEST_IMG, 'top', healthcheck={
                'test': "false",
                'interval': 1 * SECOND,
                'timeout': 1 * SECOND,
                'retries': 1,
            })
        self.tmp_containers.append(container)
        self.client.start(container)
        wait_on_health_status(self.client, container, "unhealthy")

    @helpers.requires_api_version('1.29')
    def test_healthcheck_start_period(self):
        container = self.client.create_container(
            TEST_IMG, 'top', healthcheck={
                'test': "echo 'x' >> /counter.txt && "
                     "test `cat /counter.txt | wc -l` -ge 3",
                'interval': 1 * SECOND,
                'timeout': 1 * SECOND,
                'retries': 1,
                'start_period': 3 * SECOND
            }
        )

        self.tmp_containers.append(container)
        self.client.start(container)
        wait_on_health_status(self.client, container, "healthy")