Add health string generator to match `docker ps` output

This commit is contained in:
Sorawis Nilparuk 2018-02-03 13:14:27 -08:00 committed by Joffrey F
parent 70b2d3b7cb
commit bb16d9e951
2 changed files with 80 additions and 1 deletions

View File

@ -129,7 +129,7 @@ class Container(object):
if self.is_restarting:
return 'Restarting'
if self.is_running:
return 'Ghost' if self.get('State.Ghost') else 'Up'
return 'Ghost' if self.get('State.Ghost') else self.human_readable_health_status
else:
return 'Exit %s' % self.get('State.ExitCode')
@ -172,6 +172,18 @@ class Container(object):
log_type = self.log_driver
return not log_type or log_type in ('json-file', 'journald')
@property
def human_readable_health_status(self):
""" Generate UP status string with up time and health
"""
status_string = 'Up'
container_status = self.get('State.Health.Status')
if container_status == 'starting':
status_string += ' (health: starting)'
elif container_status is not None:
status_string += ' (%s)' % container_status
return status_string
def attach_log_stream(self):
"""A log stream can only be attached if the container uses a json-file
log driver.

View File

@ -129,6 +129,73 @@ class ContainerTest(unittest.TestCase):
assert container.get_local_port(45454, protocol='tcp') == '0.0.0.0:49197'
def test_human_readable_states_no_health(self):
container = Container(None, {
"State": {
"Status": "running",
"Running": True,
"Paused": False,
"Restarting": False,
"OOMKilled": False,
"Dead": False,
"Pid": 7623,
"ExitCode": 0,
"Error": "",
"StartedAt": "2018-01-29T00:34:25.2052414Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
}, has_been_inspected=True)
expected = "Up"
assert container.human_readable_state == expected
def test_human_readable_states_starting(self):
container = Container(None, {
"State": {
"Status": "running",
"Running": True,
"Paused": False,
"Restarting": False,
"OOMKilled": False,
"Dead": False,
"Pid": 11744,
"ExitCode": 0,
"Error": "",
"StartedAt": "2018-02-03T07:56:20.3591233Z",
"FinishedAt": "2018-01-31T08:56:11.0505228Z",
"Health": {
"Status": "starting",
"FailingStreak": 0,
"Log": []
}
}
}, has_been_inspected=True)
expected = "Up (health: starting)"
assert container.human_readable_state == expected
def test_human_readable_states_healthy(self):
container = Container(None, {
"State": {
"Status": "running",
"Running": True,
"Paused": False,
"Restarting": False,
"OOMKilled": False,
"Dead": False,
"Pid": 5674,
"ExitCode": 0,
"Error": "",
"StartedAt": "2018-02-03T08:32:05.3281831Z",
"FinishedAt": "2018-02-03T08:11:35.7872706Z",
"Health": {
"Status": "healthy",
"FailingStreak": 0,
"Log": []
}
}
}, has_been_inspected=True)
expected = "Up (healthy)"
assert container.human_readable_state == expected
def test_get(self):
container = Container(None, {
"Status": "Up 8 seconds",