diff --git a/compose/config/config.py b/compose/config/config.py index 4fddac822..2a81b93da 100644 --- a/compose/config/config.py +++ b/compose/config/config.py @@ -24,6 +24,7 @@ from .environment import split_env from .errors import CircularReference from .errors import ComposeFileNotFound from .errors import ConfigurationError +from .errors import DuplicateOverrideFileFound from .errors import VERSION_EXPLANATION from .interpolation import interpolate_environment_variables from .sort_services import get_container_name_from_network_mode @@ -128,7 +129,7 @@ SUPPORTED_FILENAMES = [ 'docker-compose.yaml', ] -DEFAULT_OVERRIDE_FILENAME = 'docker-compose.override.yml' +DEFAULT_OVERRIDE_FILENAMES = ('docker-compose.override.yml', 'docker-compose.override.yaml') log = logging.getLogger(__name__) @@ -292,8 +293,12 @@ def get_default_config_files(base_dir): def get_default_override_file(path): - override_filename = os.path.join(path, DEFAULT_OVERRIDE_FILENAME) - return [override_filename] if os.path.exists(override_filename) else [] + override_files_in_path = [os.path.join(path, override_filename) for override_filename + in DEFAULT_OVERRIDE_FILENAMES + if os.path.exists(os.path.join(path, override_filename))] + if len(override_files_in_path) > 1: + raise DuplicateOverrideFileFound(override_files_in_path) + return override_files_in_path def find_candidates_in_parent_dirs(filenames, path): diff --git a/compose/config/errors.py b/compose/config/errors.py index 9b82df0ab..ac1d3ac19 100644 --- a/compose/config/errors.py +++ b/compose/config/errors.py @@ -44,3 +44,12 @@ class ComposeFileNotFound(ConfigurationError): Supported filenames: %s """ % ", ".join(supported_filenames)) + + +class DuplicateOverrideFileFound(ConfigurationError): + def __init__(self, override_filenames): + self.override_filenames = override_filenames + super(DuplicateOverrideFileFound, self).__init__( + "Multiple override files found: {}. You may only use a single " + "override file.".format(", ".join(override_filenames)) + ) diff --git a/tests/acceptance/cli_test.py b/tests/acceptance/cli_test.py index 30eff1b6a..1ba64201f 100644 --- a/tests/acceptance/cli_test.py +++ b/tests/acceptance/cli_test.py @@ -21,6 +21,7 @@ from docker import errors from .. import mock from ..helpers import create_host_file from compose.cli.command import get_project +from compose.config.errors import DuplicateOverrideFileFound from compose.container import Container from compose.project import OneOffFilter from compose.utils import nanoseconds_from_time_seconds @@ -31,7 +32,6 @@ from tests.integration.testcases import v2_1_only from tests.integration.testcases import v2_only from tests.integration.testcases import v3_only - ProcessResult = namedtuple('ProcessResult', 'stdout stderr') @@ -2149,3 +2149,25 @@ class CLITestCase(DockerClientTestCase): assert 'busybox' in result.stdout assert 'multiplecomposefiles_another_1' in result.stdout assert 'multiplecomposefiles_simple_1' in result.stdout + + def test_up_with_override_yaml(self): + self.base_dir = 'tests/fixtures/override-yaml-files' + self._project = get_project(self.base_dir, []) + self.dispatch( + [ + 'up', '-d', + ], + None) + + containers = self.project.containers() + self.assertEqual(len(containers), 2) + + web, db = containers + self.assertEqual(web.human_readable_command, 'sleep 100') + self.assertEqual(db.human_readable_command, 'top') + + def test_up_with_duplicate_override_yaml_files(self): + self.base_dir = 'tests/fixtures/duplicate-override-yaml-files' + with self.assertRaises(DuplicateOverrideFileFound): + get_project(self.base_dir, []) + self.base_dir = None diff --git a/tests/fixtures/duplicate-override-yaml-files/docker-compose.override.yaml b/tests/fixtures/duplicate-override-yaml-files/docker-compose.override.yaml new file mode 100644 index 000000000..58c673482 --- /dev/null +++ b/tests/fixtures/duplicate-override-yaml-files/docker-compose.override.yaml @@ -0,0 +1,3 @@ + +db: + command: "top" diff --git a/tests/fixtures/duplicate-override-yaml-files/docker-compose.override.yml b/tests/fixtures/duplicate-override-yaml-files/docker-compose.override.yml new file mode 100644 index 000000000..f1b8ef181 --- /dev/null +++ b/tests/fixtures/duplicate-override-yaml-files/docker-compose.override.yml @@ -0,0 +1,3 @@ + +db: + command: "sleep 300" diff --git a/tests/fixtures/duplicate-override-yaml-files/docker-compose.yml b/tests/fixtures/duplicate-override-yaml-files/docker-compose.yml new file mode 100644 index 000000000..5f2909d69 --- /dev/null +++ b/tests/fixtures/duplicate-override-yaml-files/docker-compose.yml @@ -0,0 +1,10 @@ + +web: + image: busybox:latest + command: "sleep 100" + links: + - db + +db: + image: busybox:latest + command: "sleep 200" diff --git a/tests/fixtures/override-yaml-files/docker-compose.override.yaml b/tests/fixtures/override-yaml-files/docker-compose.override.yaml new file mode 100644 index 000000000..58c673482 --- /dev/null +++ b/tests/fixtures/override-yaml-files/docker-compose.override.yaml @@ -0,0 +1,3 @@ + +db: + command: "top" diff --git a/tests/fixtures/override-yaml-files/docker-compose.yml b/tests/fixtures/override-yaml-files/docker-compose.yml new file mode 100644 index 000000000..5f2909d69 --- /dev/null +++ b/tests/fixtures/override-yaml-files/docker-compose.yml @@ -0,0 +1,10 @@ + +web: + image: busybox:latest + command: "sleep 100" + links: + - db + +db: + image: busybox:latest + command: "sleep 200"