mirror of
https://github.com/docker/compose.git
synced 2025-07-23 21:54:40 +02:00
Merge pull request #4389 from shin-/4372-normalize-time-values
Convert time data back to string values when serializing config
This commit is contained in:
commit
e05a9f4e62
@ -57,6 +57,25 @@ def serialize_config(config):
|
|||||||
width=80)
|
width=80)
|
||||||
|
|
||||||
|
|
||||||
|
def serialize_ns_time_value(value):
|
||||||
|
result = (value, 'ns')
|
||||||
|
table = [
|
||||||
|
(1000., 'us'),
|
||||||
|
(1000., 'ms'),
|
||||||
|
(1000., 's'),
|
||||||
|
(60., 'm'),
|
||||||
|
(60., 'h')
|
||||||
|
]
|
||||||
|
for stage in table:
|
||||||
|
tmp = value / stage[0]
|
||||||
|
if tmp == int(value / stage[0]):
|
||||||
|
value = tmp
|
||||||
|
result = (int(value), stage[1])
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
return '{0}{1}'.format(*result)
|
||||||
|
|
||||||
|
|
||||||
def denormalize_service_dict(service_dict, version):
|
def denormalize_service_dict(service_dict, version):
|
||||||
service_dict = service_dict.copy()
|
service_dict = service_dict.copy()
|
||||||
|
|
||||||
@ -73,4 +92,14 @@ def denormalize_service_dict(service_dict, version):
|
|||||||
svc for svc in service_dict['depends_on'].keys()
|
svc for svc in service_dict['depends_on'].keys()
|
||||||
])
|
])
|
||||||
|
|
||||||
|
if 'healthcheck' in service_dict:
|
||||||
|
if 'interval' in service_dict['healthcheck']:
|
||||||
|
service_dict['healthcheck']['interval'] = serialize_ns_time_value(
|
||||||
|
service_dict['healthcheck']['interval']
|
||||||
|
)
|
||||||
|
if 'timeout' in service_dict['healthcheck']:
|
||||||
|
service_dict['healthcheck']['timeout'] = serialize_ns_time_value(
|
||||||
|
service_dict['healthcheck']['timeout']
|
||||||
|
)
|
||||||
|
|
||||||
return service_dict
|
return service_dict
|
||||||
|
@ -353,8 +353,8 @@ class CLITestCase(DockerClientTestCase):
|
|||||||
|
|
||||||
'healthcheck': {
|
'healthcheck': {
|
||||||
'test': 'cat /etc/passwd',
|
'test': 'cat /etc/passwd',
|
||||||
'interval': 10000000000,
|
'interval': '10s',
|
||||||
'timeout': 1000000000,
|
'timeout': '1s',
|
||||||
'retries': 5,
|
'retries': 5,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -23,6 +23,7 @@ from compose.config.environment import Environment
|
|||||||
from compose.config.errors import ConfigurationError
|
from compose.config.errors import ConfigurationError
|
||||||
from compose.config.errors import VERSION_EXPLANATION
|
from compose.config.errors import VERSION_EXPLANATION
|
||||||
from compose.config.serialize import denormalize_service_dict
|
from compose.config.serialize import denormalize_service_dict
|
||||||
|
from compose.config.serialize import serialize_ns_time_value
|
||||||
from compose.config.types import VolumeSpec
|
from compose.config.types import VolumeSpec
|
||||||
from compose.const import IS_WINDOWS_PLATFORM
|
from compose.const import IS_WINDOWS_PLATFORM
|
||||||
from compose.utils import nanoseconds_from_time_seconds
|
from compose.utils import nanoseconds_from_time_seconds
|
||||||
@ -3334,3 +3335,38 @@ class SerializeTest(unittest.TestCase):
|
|||||||
}
|
}
|
||||||
|
|
||||||
assert denormalize_service_dict(service_dict, V2_1) == service_dict
|
assert denormalize_service_dict(service_dict, V2_1) == service_dict
|
||||||
|
|
||||||
|
def test_serialize_time(self):
|
||||||
|
data = {
|
||||||
|
9: '9ns',
|
||||||
|
9000: '9us',
|
||||||
|
9000000: '9ms',
|
||||||
|
90000000: '90ms',
|
||||||
|
900000000: '900ms',
|
||||||
|
999999999: '999999999ns',
|
||||||
|
1000000000: '1s',
|
||||||
|
60000000000: '1m',
|
||||||
|
60000000001: '60000000001ns',
|
||||||
|
9000000000000: '150m',
|
||||||
|
90000000000000: '25h',
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v in data.items():
|
||||||
|
assert serialize_ns_time_value(k) == v
|
||||||
|
|
||||||
|
def test_denormalize_healthcheck(self):
|
||||||
|
service_dict = {
|
||||||
|
'image': 'test',
|
||||||
|
'healthcheck': {
|
||||||
|
'test': 'exit 1',
|
||||||
|
'interval': '1m40s',
|
||||||
|
'timeout': '30s',
|
||||||
|
'retries': 5
|
||||||
|
}
|
||||||
|
}
|
||||||
|
processed_service = config.process_service(config.ServiceConfig(
|
||||||
|
'.', 'test', 'test', service_dict
|
||||||
|
))
|
||||||
|
denormalized_service = denormalize_service_dict(processed_service, V2_1)
|
||||||
|
assert denormalized_service['healthcheck']['interval'] == '100s'
|
||||||
|
assert denormalized_service['healthcheck']['timeout'] == '30s'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user