瀏覽代碼

Fix format of 'restart' option in 'config' output

Signed-off-by: Aanand Prasad <[email protected]>
Aanand Prasad 9 年之前
父節點
當前提交
756ef14edc
共有 4 個文件被更改,包括 70 次插入6 次删除
  1. 20 6
      compose/config/serialize.py
  2. 9 0
      compose/config/types.py
  3. 27 0
      tests/acceptance/cli_test.py
  4. 14 0
      tests/fixtures/restart/docker-compose.yml

+ 20 - 6
compose/config/serialize.py

@@ -19,12 +19,14 @@ yaml.SafeDumper.add_representer(types.VolumeSpec, serialize_config_type)
 
 
 def serialize_config(config):
-    services = {service.pop('name'): service for service in config.services}
-
-    if config.version == V1:
-        for service_dict in services.values():
-            if 'network_mode' not in service_dict:
-                service_dict['network_mode'] = 'bridge'
+    denormalized_services = [
+        denormalize_service_dict(service_dict, config.version)
+        for service_dict in config.services
+    ]
+    services = {
+        service_dict.pop('name'): service_dict
+        for service_dict in denormalized_services
+    }
 
     output = {
         'version': V2_0,
@@ -38,3 +40,15 @@ def serialize_config(config):
         default_flow_style=False,
         indent=2,
         width=80)
+
+
+def denormalize_service_dict(service_dict, version):
+    service_dict = service_dict.copy()
+
+    if 'restart' in service_dict:
+        service_dict['restart'] = types.serialize_restart_spec(service_dict['restart'])
+
+    if version == V1 and 'network_mode' not in service_dict:
+        service_dict['network_mode'] = 'bridge'
+
+    return service_dict

+ 9 - 0
compose/config/types.py

@@ -7,6 +7,8 @@ from __future__ import unicode_literals
 import os
 from collections import namedtuple
 
+import six
+
 from compose.config.config import V1
 from compose.config.errors import ConfigurationError
 from compose.const import IS_WINDOWS_PLATFORM
@@ -89,6 +91,13 @@ def parse_restart_spec(restart_config):
     return {'Name': name, 'MaximumRetryCount': int(max_retry_count)}
 
 
+def serialize_restart_spec(restart_spec):
+    parts = [restart_spec['Name']]
+    if restart_spec['MaximumRetryCount']:
+        parts.append(six.text_type(restart_spec['MaximumRetryCount']))
+    return ':'.join(parts)
+
+
 def parse_extra_hosts(extra_hosts_config):
     if not extra_hosts_config:
         return {}

+ 27 - 0
tests/acceptance/cli_test.py

@@ -190,6 +190,33 @@ class CLITestCase(DockerClientTestCase):
         }
         assert output == expected
 
+    def test_config_restart(self):
+        self.base_dir = 'tests/fixtures/restart'
+        result = self.dispatch(['config'])
+        assert yaml.load(result.stdout) == {
+            'version': '2.0',
+            'services': {
+                'never': {
+                    'image': 'busybox',
+                    'restart': 'no',
+                },
+                'always': {
+                    'image': 'busybox',
+                    'restart': 'always',
+                },
+                'on-failure': {
+                    'image': 'busybox',
+                    'restart': 'on-failure',
+                },
+                'on-failure-5': {
+                    'image': 'busybox',
+                    'restart': 'on-failure:5',
+                },
+            },
+            'networks': {},
+            'volumes': {},
+        }
+
     def test_config_v1(self):
         self.base_dir = 'tests/fixtures/v1-config'
         result = self.dispatch(['config'])

+ 14 - 0
tests/fixtures/restart/docker-compose.yml

@@ -0,0 +1,14 @@
+version: "2"
+services:
+  never:
+    image: busybox
+    restart: "no"
+  always:
+    image: busybox
+    restart: always
+  on-failure:
+    image: busybox
+    restart: on-failure
+  on-failure-5:
+    image: busybox
+    restart: "on-failure:5"