123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659 |
- # encoding: utf-8
- from __future__ import print_function
- import os
- import shutil
- import tempfile
- from operator import itemgetter
- import py
- import pytest
- from compose.config import config
- from compose.config.config import resolve_environment
- from compose.config.errors import ConfigurationError
- from compose.config.types import VolumeSpec
- from compose.const import IS_WINDOWS_PLATFORM
- from tests import mock
- from tests import unittest
- def make_service_dict(name, service_dict, working_dir, filename=None):
- """
- Test helper function to construct a ServiceExtendsResolver
- """
- resolver = config.ServiceExtendsResolver(config.ServiceConfig(
- working_dir=working_dir,
- filename=filename,
- name=name,
- config=service_dict))
- return config.process_service(resolver.run())
- def service_sort(services):
- return sorted(services, key=itemgetter('name'))
- def build_config_details(contents, working_dir='working_dir', filename='filename.yml'):
- return config.ConfigDetails(
- working_dir,
- [config.ConfigFile(filename, contents)])
- class ConfigTest(unittest.TestCase):
- def test_load(self):
- service_dicts = config.load(
- build_config_details(
- {
- 'foo': {'image': 'busybox'},
- 'bar': {'image': 'busybox', 'environment': ['FOO=1']},
- },
- 'tests/fixtures/extends',
- 'common.yml'
- )
- )
- self.assertEqual(
- service_sort(service_dicts),
- service_sort([
- {
- 'name': 'bar',
- 'image': 'busybox',
- 'environment': {'FOO': '1'},
- },
- {
- 'name': 'foo',
- 'image': 'busybox',
- }
- ])
- )
- def test_load_throws_error_when_not_dict(self):
- with self.assertRaises(ConfigurationError):
- config.load(
- build_config_details(
- {'web': 'busybox:latest'},
- 'working_dir',
- 'filename.yml'
- )
- )
- def test_load_config_invalid_service_names(self):
- for invalid_name in ['?not?allowed', ' ', '', '!', '/', '\xe2']:
- with pytest.raises(ConfigurationError) as exc:
- config.load(build_config_details(
- {invalid_name: {'image': 'busybox'}},
- 'working_dir',
- 'filename.yml'))
- assert 'Invalid service name \'%s\'' % invalid_name in exc.exconly()
- def test_load_with_invalid_field_name(self):
- config_details = build_config_details(
- {'web': {'image': 'busybox', 'name': 'bogus'}},
- 'working_dir',
- 'filename.yml')
- with pytest.raises(ConfigurationError) as exc:
- config.load(config_details)
- error_msg = "Unsupported config option for 'web' service: 'name'"
- assert error_msg in exc.exconly()
- assert "Validation failed in file 'filename.yml'" in exc.exconly()
- def test_load_invalid_service_definition(self):
- config_details = build_config_details(
- {'web': 'wrong'},
- 'working_dir',
- 'filename.yml')
- with pytest.raises(ConfigurationError) as exc:
- config.load(config_details)
- error_msg = "service 'web' doesn't have any configuration options"
- assert error_msg in exc.exconly()
- def test_config_integer_service_name_raise_validation_error(self):
- expected_error_msg = ("In file 'filename.yml' service name: 1 needs to "
- "be a string, eg '1'")
- with self.assertRaisesRegexp(ConfigurationError, expected_error_msg):
- config.load(
- build_config_details(
- {1: {'image': 'busybox'}},
- 'working_dir',
- 'filename.yml'
- )
- )
- @pytest.mark.xfail(IS_WINDOWS_PLATFORM, reason='paths use slash')
- def test_load_with_multiple_files(self):
- base_file = config.ConfigFile(
- 'base.yaml',
- {
- 'web': {
- 'image': 'example/web',
- 'links': ['db'],
- },
- 'db': {
- 'image': 'example/db',
- },
- })
- override_file = config.ConfigFile(
- 'override.yaml',
- {
- 'web': {
- 'build': '/',
- 'volumes': ['/home/user/project:/code'],
- },
- })
- details = config.ConfigDetails('.', [base_file, override_file])
- service_dicts = config.load(details)
- expected = [
- {
- 'name': 'web',
- 'build': '/',
- 'links': ['db'],
- 'volumes': [VolumeSpec.parse('/home/user/project:/code')],
- },
- {
- 'name': 'db',
- 'image': 'example/db',
- },
- ]
- self.assertEqual(service_sort(service_dicts), service_sort(expected))
- def test_load_with_multiple_files_and_empty_override(self):
- base_file = config.ConfigFile(
- 'base.yml',
- {'web': {'image': 'example/web'}})
- override_file = config.ConfigFile('override.yml', None)
- details = config.ConfigDetails('.', [base_file, override_file])
- with pytest.raises(ConfigurationError) as exc:
- config.load(details)
- error_msg = "Top level object in 'override.yml' needs to be an object"
- assert error_msg in exc.exconly()
- def test_load_with_multiple_files_and_empty_base(self):
- base_file = config.ConfigFile('base.yml', None)
- override_file = config.ConfigFile(
- 'override.yml',
- {'web': {'image': 'example/web'}})
- details = config.ConfigDetails('.', [base_file, override_file])
- with pytest.raises(ConfigurationError) as exc:
- config.load(details)
- assert "Top level object in 'base.yml' needs to be an object" in exc.exconly()
- def test_load_with_multiple_files_and_extends_in_override_file(self):
- base_file = config.ConfigFile(
- 'base.yaml',
- {
- 'web': {'image': 'example/web'},
- })
- override_file = config.ConfigFile(
- 'override.yaml',
- {
- 'web': {
- 'extends': {
- 'file': 'common.yml',
- 'service': 'base',
- },
- 'volumes': ['/home/user/project:/code'],
- },
- })
- details = config.ConfigDetails('.', [base_file, override_file])
- tmpdir = py.test.ensuretemp('config_test')
- self.addCleanup(tmpdir.remove)
- tmpdir.join('common.yml').write("""
- base:
- labels: ['label=one']
- """)
- with tmpdir.as_cwd():
- service_dicts = config.load(details)
- expected = [
- {
- 'name': 'web',
- 'image': 'example/web',
- 'volumes': [VolumeSpec.parse('/home/user/project:/code')],
- 'labels': {'label': 'one'},
- },
- ]
- self.assertEqual(service_sort(service_dicts), service_sort(expected))
- def test_load_with_multiple_files_and_invalid_override(self):
- base_file = config.ConfigFile(
- 'base.yaml',
- {'web': {'image': 'example/web'}})
- override_file = config.ConfigFile(
- 'override.yaml',
- {'bogus': 'thing'})
- details = config.ConfigDetails('.', [base_file, override_file])
- with pytest.raises(ConfigurationError) as exc:
- config.load(details)
- assert "service 'bogus' doesn't have any configuration" in exc.exconly()
- assert "In file 'override.yaml'" in exc.exconly()
- def test_load_sorts_in_dependency_order(self):
- config_details = build_config_details({
- 'web': {
- 'image': 'busybox:latest',
- 'links': ['db'],
- },
- 'db': {
- 'image': 'busybox:latest',
- 'volumes_from': ['volume:ro']
- },
- 'volume': {
- 'image': 'busybox:latest',
- 'volumes': ['/tmp'],
- }
- })
- services = config.load(config_details)
- assert services[0]['name'] == 'volume'
- assert services[1]['name'] == 'db'
- assert services[2]['name'] == 'web'
- def test_config_valid_service_names(self):
- for valid_name in ['_', '-', '.__.', '_what-up.', 'what_.up----', 'whatup']:
- services = config.load(
- build_config_details(
- {valid_name: {'image': 'busybox'}},
- 'tests/fixtures/extends',
- 'common.yml'))
- assert services[0]['name'] == valid_name
- def test_config_hint(self):
- expected_error_msg = "(did you mean 'privileged'?)"
- with self.assertRaisesRegexp(ConfigurationError, expected_error_msg):
- config.load(
- build_config_details(
- {
- 'foo': {'image': 'busybox', 'privilige': 'something'},
- },
- 'tests/fixtures/extends',
- 'filename.yml'
- )
- )
- def test_invalid_config_build_and_image_specified(self):
- expected_error_msg = "Service 'foo' has both an image and build path specified."
- with self.assertRaisesRegexp(ConfigurationError, expected_error_msg):
- config.load(
- build_config_details(
- {
- 'foo': {'image': 'busybox', 'build': '.'},
- },
- 'tests/fixtures/extends',
- 'filename.yml'
- )
- )
- def test_invalid_config_type_should_be_an_array(self):
- expected_error_msg = "Service 'foo' configuration key 'links' contains an invalid type, it should be an array"
- with self.assertRaisesRegexp(ConfigurationError, expected_error_msg):
- config.load(
- build_config_details(
- {
- 'foo': {'image': 'busybox', 'links': 'an_link'},
- },
- 'tests/fixtures/extends',
- 'filename.yml'
- )
- )
- def test_invalid_config_not_a_dictionary(self):
- expected_error_msg = ("Top level object in 'filename.yml' needs to be "
- "an object.")
- with self.assertRaisesRegexp(ConfigurationError, expected_error_msg):
- config.load(
- build_config_details(
- ['foo', 'lol'],
- 'tests/fixtures/extends',
- 'filename.yml'
- )
- )
- def test_invalid_config_not_unique_items(self):
- expected_error_msg = "has non-unique elements"
- with self.assertRaisesRegexp(ConfigurationError, expected_error_msg):
- config.load(
- build_config_details(
- {
- 'web': {'build': '.', 'devices': ['/dev/foo:/dev/foo', '/dev/foo:/dev/foo']}
- },
- 'tests/fixtures/extends',
- 'filename.yml'
- )
- )
- def test_invalid_list_of_strings_format(self):
- expected_error_msg = "Service 'web' configuration key 'command' contains 1"
- expected_error_msg += ", which is an invalid type, it should be a string"
- with self.assertRaisesRegexp(ConfigurationError, expected_error_msg):
- config.load(
- build_config_details(
- {
- 'web': {'build': '.', 'command': [1]}
- },
- 'tests/fixtures/extends',
- 'filename.yml'
- )
- )
- def test_config_image_and_dockerfile_raise_validation_error(self):
- expected_error_msg = "Service 'web' has both an image and alternate Dockerfile."
- with self.assertRaisesRegexp(ConfigurationError, expected_error_msg):
- config.load(
- build_config_details(
- {'web': {'image': 'busybox', 'dockerfile': 'Dockerfile.alt'}},
- 'working_dir',
- 'filename.yml'
- )
- )
- def test_config_extra_hosts_string_raises_validation_error(self):
- expected_error_msg = "Service 'web' configuration key 'extra_hosts' contains an invalid type"
- with self.assertRaisesRegexp(ConfigurationError, expected_error_msg):
- config.load(
- build_config_details(
- {'web': {
- 'image': 'busybox',
- 'extra_hosts': 'somehost:162.242.195.82'
- }},
- 'working_dir',
- 'filename.yml'
- )
- )
- def test_config_extra_hosts_list_of_dicts_validation_error(self):
- expected_error_msg = "key 'extra_hosts' contains {'somehost': '162.242.195.82'}, which is an invalid type, it should be a string"
- with self.assertRaisesRegexp(ConfigurationError, expected_error_msg):
- config.load(
- build_config_details(
- {'web': {
- 'image': 'busybox',
- 'extra_hosts': [
- {'somehost': '162.242.195.82'},
- {'otherhost': '50.31.209.229'}
- ]
- }},
- 'working_dir',
- 'filename.yml'
- )
- )
- def test_config_ulimits_invalid_keys_validation_error(self):
- expected = ("Service 'web' configuration key 'ulimits' 'nofile' contains "
- "unsupported option: 'not_soft_or_hard'")
- with pytest.raises(ConfigurationError) as exc:
- config.load(build_config_details(
- {
- 'web': {
- 'image': 'busybox',
- 'ulimits': {
- 'nofile': {
- "not_soft_or_hard": 100,
- "soft": 10000,
- "hard": 20000,
- }
- }
- }
- },
- 'working_dir',
- 'filename.yml'))
- assert expected in exc.exconly()
- def test_config_ulimits_required_keys_validation_error(self):
- with pytest.raises(ConfigurationError) as exc:
- config.load(build_config_details(
- {
- 'web': {
- 'image': 'busybox',
- 'ulimits': {'nofile': {"soft": 10000}}
- }
- },
- 'working_dir',
- 'filename.yml'))
- assert "Service 'web' configuration key 'ulimits' 'nofile'" in exc.exconly()
- assert "'hard' is a required property" in exc.exconly()
- def test_config_ulimits_soft_greater_than_hard_error(self):
- expected = "cannot contain a 'soft' value higher than 'hard' value"
- with pytest.raises(ConfigurationError) as exc:
- config.load(build_config_details(
- {
- 'web': {
- 'image': 'busybox',
- 'ulimits': {
- 'nofile': {"soft": 10000, "hard": 1000}
- }
- }
- },
- 'working_dir',
- 'filename.yml'))
- assert expected in exc.exconly()
- def test_valid_config_which_allows_two_type_definitions(self):
- expose_values = [["8000"], [8000]]
- for expose in expose_values:
- service = config.load(
- build_config_details(
- {'web': {
- 'image': 'busybox',
- 'expose': expose
- }},
- 'working_dir',
- 'filename.yml'
- )
- )
- self.assertEqual(service[0]['expose'], expose)
- def test_valid_config_oneof_string_or_list(self):
- entrypoint_values = [["sh"], "sh"]
- for entrypoint in entrypoint_values:
- service = config.load(
- build_config_details(
- {'web': {
- 'image': 'busybox',
- 'entrypoint': entrypoint
- }},
- 'working_dir',
- 'filename.yml'
- )
- )
- self.assertEqual(service[0]['entrypoint'], entrypoint)
- @mock.patch('compose.config.validation.log')
- def test_logs_warning_for_boolean_in_environment(self, mock_logging):
- expected_warning_msg = "There is a boolean value in the 'environment' key."
- config.load(
- build_config_details(
- {'web': {
- 'image': 'busybox',
- 'environment': {'SHOW_STUFF': True}
- }},
- 'working_dir',
- 'filename.yml'
- )
- )
- self.assertTrue(mock_logging.warn.called)
- self.assertTrue(expected_warning_msg in mock_logging.warn.call_args[0][0])
- def test_config_valid_environment_dict_key_contains_dashes(self):
- services = config.load(
- build_config_details(
- {'web': {
- 'image': 'busybox',
- 'environment': {'SPRING_JPA_HIBERNATE_DDL-AUTO': 'none'}
- }},
- 'working_dir',
- 'filename.yml'
- )
- )
- self.assertEqual(services[0]['environment']['SPRING_JPA_HIBERNATE_DDL-AUTO'], 'none')
- def test_load_yaml_with_yaml_error(self):
- tmpdir = py.test.ensuretemp('invalid_yaml_test')
- self.addCleanup(tmpdir.remove)
- invalid_yaml_file = tmpdir.join('docker-compose.yml')
- invalid_yaml_file.write("""
- web:
- this is bogus: ok: what
- """)
- with pytest.raises(ConfigurationError) as exc:
- config.load_yaml(str(invalid_yaml_file))
- assert 'line 3, column 32' in exc.exconly()
- def test_validate_extra_hosts_invalid(self):
- with pytest.raises(ConfigurationError) as exc:
- config.load(build_config_details({
- 'web': {
- 'image': 'alpine',
- 'extra_hosts': "www.example.com: 192.168.0.17",
- }
- }))
- assert "'extra_hosts' contains an invalid type" in exc.exconly()
- def test_validate_extra_hosts_invalid_list(self):
- with pytest.raises(ConfigurationError) as exc:
- config.load(build_config_details({
- 'web': {
- 'image': 'alpine',
- 'extra_hosts': [
- {'www.example.com': '192.168.0.17'},
- {'api.example.com': '192.168.0.18'}
- ],
- }
- }))
- assert "which is an invalid type" in exc.exconly()
- def test_normalize_dns_options(self):
- actual = config.load(build_config_details({
- 'web': {
- 'image': 'alpine',
- 'dns': '8.8.8.8',
- 'dns_search': 'domain.local',
- }
- }))
- assert actual == [
- {
- 'name': 'web',
- 'image': 'alpine',
- 'dns': ['8.8.8.8'],
- 'dns_search': ['domain.local'],
- }
- ]
- class PortsTest(unittest.TestCase):
- INVALID_PORTS_TYPES = [
- {"1": "8000"},
- False,
- "8000",
- 8000,
- ]
- NON_UNIQUE_SINGLE_PORTS = [
- ["8000", "8000"],
- ]
- INVALID_PORT_MAPPINGS = [
- ["8000-8001:8000"],
- ]
- VALID_SINGLE_PORTS = [
- ["8000"],
- ["8000/tcp"],
- ["8000", "9000"],
- [8000],
- [8000, 9000],
- ]
- VALID_PORT_MAPPINGS = [
- ["8000:8050"],
- ["49153-49154:3002-3003"],
- ]
- def test_config_invalid_ports_type_validation(self):
- for invalid_ports in self.INVALID_PORTS_TYPES:
- with pytest.raises(ConfigurationError) as exc:
- self.check_config({'ports': invalid_ports})
- assert "contains an invalid type" in exc.value.msg
- def test_config_non_unique_ports_validation(self):
- for invalid_ports in self.NON_UNIQUE_SINGLE_PORTS:
- with pytest.raises(ConfigurationError) as exc:
- self.check_config({'ports': invalid_ports})
- assert "non-unique" in exc.value.msg
- def test_config_invalid_ports_format_validation(self):
- for invalid_ports in self.INVALID_PORT_MAPPINGS:
- with pytest.raises(ConfigurationError) as exc:
- self.check_config({'ports': invalid_ports})
- assert "Port ranges don't match in length" in exc.value.msg
- def test_config_valid_ports_format_validation(self):
- for valid_ports in self.VALID_SINGLE_PORTS + self.VALID_PORT_MAPPINGS:
- self.check_config({'ports': valid_ports})
- def test_config_invalid_expose_type_validation(self):
- for invalid_expose in self.INVALID_PORTS_TYPES:
- with pytest.raises(ConfigurationError) as exc:
- self.check_config({'expose': invalid_expose})
- assert "contains an invalid type" in exc.value.msg
- def test_config_non_unique_expose_validation(self):
- for invalid_expose in self.NON_UNIQUE_SINGLE_PORTS:
- with pytest.raises(ConfigurationError) as exc:
- self.check_config({'expose': invalid_expose})
- assert "non-unique" in exc.value.msg
- def test_config_invalid_expose_format_validation(self):
- # Valid port mappings ARE NOT valid 'expose' entries
- for invalid_expose in self.INVALID_PORT_MAPPINGS + self.VALID_PORT_MAPPINGS:
- with pytest.raises(ConfigurationError) as exc:
- self.check_config({'expose': invalid_expose})
- assert "should be of the format" in exc.value.msg
- def test_config_valid_expose_format_validation(self):
- # Valid single ports ARE valid 'expose' entries
- for valid_expose in self.VALID_SINGLE_PORTS:
- self.check_config({'expose': valid_expose})
- def check_config(self, cfg):
- config.load(
- build_config_details(
- {'web': dict(image='busybox', **cfg)},
- 'working_dir',
- 'filename.yml'
- )
- )
- class InterpolationTest(unittest.TestCase):
- @mock.patch.dict(os.environ)
- def test_config_file_with_environment_variable(self):
- os.environ.update(
- IMAGE="busybox",
- HOST_PORT="80",
- LABEL_VALUE="myvalue",
- )
- service_dicts = config.load(
- config.find('tests/fixtures/environment-interpolation', None),
- )
- self.assertEqual(service_dicts, [
- {
- 'name': 'web',
- 'image': 'busybox',
- 'ports': ['80:8000'],
- 'labels': {'mylabel': 'myvalue'},
- 'hostname': 'host-',
- 'command': '${ESCAPED}',
- }
- ])
- @mock.patch.dict(os.environ)
- def test_unset_variable_produces_warning(self):
- os.environ.pop('FOO', None)
- os.environ.pop('BAR', None)
- config_details = build_config_details(
- {
- 'web': {
- 'image': '${FOO}',
- 'command': '${BAR}',
- 'container_name': '${BAR}',
- },
- },
- '.',
- None,
- )
- with mock.patch('compose.config.interpolation.log') as log:
- config.load(config_details)
- self.assertEqual(2, log.warn.call_count)
- warnings = sorted(args[0][0] for args in log.warn.call_args_list)
- self.assertIn('BAR', warnings[0])
- self.assertIn('FOO', warnings[1])
- @mock.patch.dict(os.environ)
- def test_invalid_interpolation(self):
- with self.assertRaises(config.ConfigurationError) as cm:
- config.load(
- build_config_details(
- {'web': {'image': '${'}},
- 'working_dir',
- 'filename.yml'
- )
- )
- self.assertIn('Invalid', cm.exception.msg)
- self.assertIn('for "image" option', cm.exception.msg)
- self.assertIn('in service "web"', cm.exception.msg)
- self.assertIn('"${"', cm.exception.msg)
- def test_empty_environment_key_allowed(self):
- service_dict = config.load(
- build_config_details(
- {
- 'web': {
- 'build': '.',
- 'environment': {
- 'POSTGRES_PASSWORD': ''
- },
- },
- },
- '.',
- None,
- )
- )[0]
- self.assertEquals(service_dict['environment']['POSTGRES_PASSWORD'], '')
- class VolumeConfigTest(unittest.TestCase):
- def test_no_binding(self):
- d = make_service_dict('foo', {'build': '.', 'volumes': ['/data']}, working_dir='.')
- self.assertEqual(d['volumes'], ['/data'])
- @mock.patch.dict(os.environ)
- def test_volume_binding_with_environment_variable(self):
- os.environ['VOLUME_PATH'] = '/host/path'
- d = config.load(build_config_details(
- {'foo': {'build': '.', 'volumes': ['${VOLUME_PATH}:/container/path']}},
- '.',
- ))[0]
- self.assertEqual(d['volumes'], [VolumeSpec.parse('/host/path:/container/path')])
- @pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason='posix paths')
- @mock.patch.dict(os.environ)
- def test_volume_binding_with_home(self):
- os.environ['HOME'] = '/home/user'
- d = make_service_dict('foo', {'build': '.', 'volumes': ['~:/container/path']}, working_dir='.')
- self.assertEqual(d['volumes'], ['/home/user:/container/path'])
- def test_name_does_not_expand(self):
- d = make_service_dict('foo', {'build': '.', 'volumes': ['mydatavolume:/data']}, working_dir='.')
- self.assertEqual(d['volumes'], ['mydatavolume:/data'])
- def test_absolute_posix_path_does_not_expand(self):
- d = make_service_dict('foo', {'build': '.', 'volumes': ['/var/lib/data:/data']}, working_dir='.')
- self.assertEqual(d['volumes'], ['/var/lib/data:/data'])
- def test_absolute_windows_path_does_not_expand(self):
- d = make_service_dict('foo', {'build': '.', 'volumes': ['c:\\data:/data']}, working_dir='.')
- self.assertEqual(d['volumes'], ['c:\\data:/data'])
- @pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason='posix paths')
- def test_relative_path_does_expand_posix(self):
- d = make_service_dict('foo', {'build': '.', 'volumes': ['./data:/data']}, working_dir='/home/me/myproject')
- self.assertEqual(d['volumes'], ['/home/me/myproject/data:/data'])
- d = make_service_dict('foo', {'build': '.', 'volumes': ['.:/data']}, working_dir='/home/me/myproject')
- self.assertEqual(d['volumes'], ['/home/me/myproject:/data'])
- d = make_service_dict('foo', {'build': '.', 'volumes': ['../otherproject:/data']}, working_dir='/home/me/myproject')
- self.assertEqual(d['volumes'], ['/home/me/otherproject:/data'])
- @pytest.mark.skipif(not IS_WINDOWS_PLATFORM, reason='windows paths')
- def test_relative_path_does_expand_windows(self):
- d = make_service_dict('foo', {'build': '.', 'volumes': ['./data:/data']}, working_dir='c:\\Users\\me\\myproject')
- self.assertEqual(d['volumes'], ['c:\\Users\\me\\myproject\\data:/data'])
- d = make_service_dict('foo', {'build': '.', 'volumes': ['.:/data']}, working_dir='c:\\Users\\me\\myproject')
- self.assertEqual(d['volumes'], ['c:\\Users\\me\\myproject:/data'])
- d = make_service_dict('foo', {'build': '.', 'volumes': ['../otherproject:/data']}, working_dir='c:\\Users\\me\\myproject')
- self.assertEqual(d['volumes'], ['c:\\Users\\me\\otherproject:/data'])
- @mock.patch.dict(os.environ)
- def test_home_directory_with_driver_does_not_expand(self):
- os.environ['NAME'] = 'surprise!'
- d = make_service_dict('foo', {
- 'build': '.',
- 'volumes': ['~:/data'],
- 'volume_driver': 'foodriver',
- }, working_dir='.')
- self.assertEqual(d['volumes'], ['~:/data'])
- def test_volume_path_with_non_ascii_directory(self):
- volume = u'/Füü/data:/data'
- container_path = config.resolve_volume_path(".", volume)
- self.assertEqual(container_path, volume)
- class MergePathMappingTest(object):
- def config_name(self):
- return ""
- def test_empty(self):
- service_dict = config.merge_service_dicts({}, {})
- self.assertNotIn(self.config_name(), service_dict)
- def test_no_override(self):
- service_dict = config.merge_service_dicts(
- {self.config_name(): ['/foo:/code', '/data']},
- {},
- )
- self.assertEqual(set(service_dict[self.config_name()]), set(['/foo:/code', '/data']))
- def test_no_base(self):
- service_dict = config.merge_service_dicts(
- {},
- {self.config_name(): ['/bar:/code']},
- )
- self.assertEqual(set(service_dict[self.config_name()]), set(['/bar:/code']))
- def test_override_explicit_path(self):
- service_dict = config.merge_service_dicts(
- {self.config_name(): ['/foo:/code', '/data']},
- {self.config_name(): ['/bar:/code']},
- )
- self.assertEqual(set(service_dict[self.config_name()]), set(['/bar:/code', '/data']))
- def test_add_explicit_path(self):
- service_dict = config.merge_service_dicts(
- {self.config_name(): ['/foo:/code', '/data']},
- {self.config_name(): ['/bar:/code', '/quux:/data']},
- )
- self.assertEqual(set(service_dict[self.config_name()]), set(['/bar:/code', '/quux:/data']))
- def test_remove_explicit_path(self):
- service_dict = config.merge_service_dicts(
- {self.config_name(): ['/foo:/code', '/quux:/data']},
- {self.config_name(): ['/bar:/code', '/data']},
- )
- self.assertEqual(set(service_dict[self.config_name()]), set(['/bar:/code', '/data']))
- class MergeVolumesTest(unittest.TestCase, MergePathMappingTest):
- def config_name(self):
- return 'volumes'
- class MergeDevicesTest(unittest.TestCase, MergePathMappingTest):
- def config_name(self):
- return 'devices'
- class BuildOrImageMergeTest(unittest.TestCase):
- def test_merge_build_or_image_no_override(self):
- self.assertEqual(
- config.merge_service_dicts({'build': '.'}, {}),
- {'build': '.'},
- )
- self.assertEqual(
- config.merge_service_dicts({'image': 'redis'}, {}),
- {'image': 'redis'},
- )
- def test_merge_build_or_image_override_with_same(self):
- self.assertEqual(
- config.merge_service_dicts({'build': '.'}, {'build': './web'}),
- {'build': './web'},
- )
- self.assertEqual(
- config.merge_service_dicts({'image': 'redis'}, {'image': 'postgres'}),
- {'image': 'postgres'},
- )
- def test_merge_build_or_image_override_with_other(self):
- self.assertEqual(
- config.merge_service_dicts({'build': '.'}, {'image': 'redis'}),
- {'image': 'redis'}
- )
- self.assertEqual(
- config.merge_service_dicts({'image': 'redis'}, {'build': '.'}),
- {'build': '.'}
- )
- class MergeListsTest(unittest.TestCase):
- def test_empty(self):
- service_dict = config.merge_service_dicts({}, {})
- self.assertNotIn('ports', service_dict)
- def test_no_override(self):
- service_dict = config.merge_service_dicts(
- {'ports': ['10:8000', '9000']},
- {},
- )
- self.assertEqual(set(service_dict['ports']), set(['10:8000', '9000']))
- def test_no_base(self):
- service_dict = config.merge_service_dicts(
- {},
- {'ports': ['10:8000', '9000']},
- )
- self.assertEqual(set(service_dict['ports']), set(['10:8000', '9000']))
- def test_add_item(self):
- service_dict = config.merge_service_dicts(
- {'ports': ['10:8000', '9000']},
- {'ports': ['20:8000']},
- )
- self.assertEqual(set(service_dict['ports']), set(['10:8000', '9000', '20:8000']))
- class MergeStringsOrListsTest(unittest.TestCase):
- def test_no_override(self):
- service_dict = config.merge_service_dicts(
- {'dns': '8.8.8.8'},
- {},
- )
- self.assertEqual(set(service_dict['dns']), set(['8.8.8.8']))
- def test_no_base(self):
- service_dict = config.merge_service_dicts(
- {},
- {'dns': '8.8.8.8'},
- )
- self.assertEqual(set(service_dict['dns']), set(['8.8.8.8']))
- def test_add_string(self):
- service_dict = config.merge_service_dicts(
- {'dns': ['8.8.8.8']},
- {'dns': '9.9.9.9'},
- )
- self.assertEqual(set(service_dict['dns']), set(['8.8.8.8', '9.9.9.9']))
- def test_add_list(self):
- service_dict = config.merge_service_dicts(
- {'dns': '8.8.8.8'},
- {'dns': ['9.9.9.9']},
- )
- self.assertEqual(set(service_dict['dns']), set(['8.8.8.8', '9.9.9.9']))
- class MergeLabelsTest(unittest.TestCase):
- def test_empty(self):
- service_dict = config.merge_service_dicts({}, {})
- self.assertNotIn('labels', service_dict)
- def test_no_override(self):
- service_dict = config.merge_service_dicts(
- make_service_dict('foo', {'build': '.', 'labels': ['foo=1', 'bar']}, 'tests/'),
- make_service_dict('foo', {'build': '.'}, 'tests/'),
- )
- self.assertEqual(service_dict['labels'], {'foo': '1', 'bar': ''})
- def test_no_base(self):
- service_dict = config.merge_service_dicts(
- make_service_dict('foo', {'build': '.'}, 'tests/'),
- make_service_dict('foo', {'build': '.', 'labels': ['foo=2']}, 'tests/'),
- )
- self.assertEqual(service_dict['labels'], {'foo': '2'})
- def test_override_explicit_value(self):
- service_dict = config.merge_service_dicts(
- make_service_dict('foo', {'build': '.', 'labels': ['foo=1', 'bar']}, 'tests/'),
- make_service_dict('foo', {'build': '.', 'labels': ['foo=2']}, 'tests/'),
- )
- self.assertEqual(service_dict['labels'], {'foo': '2', 'bar': ''})
- def test_add_explicit_value(self):
- service_dict = config.merge_service_dicts(
- make_service_dict('foo', {'build': '.', 'labels': ['foo=1', 'bar']}, 'tests/'),
- make_service_dict('foo', {'build': '.', 'labels': ['bar=2']}, 'tests/'),
- )
- self.assertEqual(service_dict['labels'], {'foo': '1', 'bar': '2'})
- def test_remove_explicit_value(self):
- service_dict = config.merge_service_dicts(
- make_service_dict('foo', {'build': '.', 'labels': ['foo=1', 'bar=2']}, 'tests/'),
- make_service_dict('foo', {'build': '.', 'labels': ['bar']}, 'tests/'),
- )
- self.assertEqual(service_dict['labels'], {'foo': '1', 'bar': ''})
- class MemoryOptionsTest(unittest.TestCase):
- def test_validation_fails_with_just_memswap_limit(self):
- """
- When you set a 'memswap_limit' it is invalid config unless you also set
- a mem_limit
- """
- expected_error_msg = (
- "Service 'foo' configuration key 'memswap_limit' is invalid: when "
- "defining 'memswap_limit' you must set 'mem_limit' as well"
- )
- with self.assertRaisesRegexp(ConfigurationError, expected_error_msg):
- config.load(
- build_config_details(
- {
- 'foo': {'image': 'busybox', 'memswap_limit': 2000000},
- },
- 'tests/fixtures/extends',
- 'filename.yml'
- )
- )
- def test_validation_with_correct_memswap_values(self):
- service_dict = config.load(
- build_config_details(
- {'foo': {'image': 'busybox', 'mem_limit': 1000000, 'memswap_limit': 2000000}},
- 'tests/fixtures/extends',
- 'common.yml'
- )
- )
- self.assertEqual(service_dict[0]['memswap_limit'], 2000000)
- def test_memswap_can_be_a_string(self):
- service_dict = config.load(
- build_config_details(
- {'foo': {'image': 'busybox', 'mem_limit': "1G", 'memswap_limit': "512M"}},
- 'tests/fixtures/extends',
- 'common.yml'
- )
- )
- self.assertEqual(service_dict[0]['memswap_limit'], "512M")
- class EnvTest(unittest.TestCase):
- def test_parse_environment_as_list(self):
- environment = [
- 'NORMAL=F1',
- 'CONTAINS_EQUALS=F=2',
- 'TRAILING_EQUALS=',
- ]
- self.assertEqual(
- config.parse_environment(environment),
- {'NORMAL': 'F1', 'CONTAINS_EQUALS': 'F=2', 'TRAILING_EQUALS': ''},
- )
- def test_parse_environment_as_dict(self):
- environment = {
- 'NORMAL': 'F1',
- 'CONTAINS_EQUALS': 'F=2',
- 'TRAILING_EQUALS': None,
- }
- self.assertEqual(config.parse_environment(environment), environment)
- def test_parse_environment_invalid(self):
- with self.assertRaises(ConfigurationError):
- config.parse_environment('a=b')
- def test_parse_environment_empty(self):
- self.assertEqual(config.parse_environment(None), {})
- @mock.patch.dict(os.environ)
- def test_resolve_environment(self):
- os.environ['FILE_DEF'] = 'E1'
- os.environ['FILE_DEF_EMPTY'] = 'E2'
- os.environ['ENV_DEF'] = 'E3'
- service_dict = {
- 'build': '.',
- 'environment': {
- 'FILE_DEF': 'F1',
- 'FILE_DEF_EMPTY': '',
- 'ENV_DEF': None,
- 'NO_DEF': None
- },
- }
- self.assertEqual(
- resolve_environment(service_dict),
- {'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''},
- )
- def test_resolve_environment_from_env_file(self):
- self.assertEqual(
- resolve_environment({'env_file': ['tests/fixtures/env/one.env']}),
- {'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'bar'},
- )
- def test_resolve_environment_with_multiple_env_files(self):
- service_dict = {
- 'env_file': [
- 'tests/fixtures/env/one.env',
- 'tests/fixtures/env/two.env'
- ]
- }
- self.assertEqual(
- resolve_environment(service_dict),
- {'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'baz', 'DOO': 'dah'},
- )
- def test_resolve_environment_nonexistent_file(self):
- with pytest.raises(ConfigurationError) as exc:
- config.load(build_config_details(
- {'foo': {'image': 'example', 'env_file': 'nonexistent.env'}},
- working_dir='tests/fixtures/env'))
- assert 'Couldn\'t find env file' in exc.exconly()
- assert 'nonexistent.env' in exc.exconly()
- @mock.patch.dict(os.environ)
- def test_resolve_environment_from_env_file_with_empty_values(self):
- os.environ['FILE_DEF'] = 'E1'
- os.environ['FILE_DEF_EMPTY'] = 'E2'
- os.environ['ENV_DEF'] = 'E3'
- self.assertEqual(
- resolve_environment({'env_file': ['tests/fixtures/env/resolve.env']}),
- {
- 'FILE_DEF': u'bär',
- 'FILE_DEF_EMPTY': '',
- 'ENV_DEF': 'E3',
- 'NO_DEF': ''
- },
- )
- @pytest.mark.xfail(IS_WINDOWS_PLATFORM, reason='paths use slash')
- @mock.patch.dict(os.environ)
- def test_resolve_path(self):
- os.environ['HOSTENV'] = '/tmp'
- os.environ['CONTAINERENV'] = '/host/tmp'
- service_dict = config.load(
- build_config_details(
- {'foo': {'build': '.', 'volumes': ['$HOSTENV:$CONTAINERENV']}},
- "tests/fixtures/env",
- )
- )[0]
- self.assertEqual(
- set(service_dict['volumes']),
- set([VolumeSpec.parse('/tmp:/host/tmp')]))
- service_dict = config.load(
- build_config_details(
- {'foo': {'build': '.', 'volumes': ['/opt${HOSTENV}:/opt${CONTAINERENV}']}},
- "tests/fixtures/env",
- )
- )[0]
- self.assertEqual(
- set(service_dict['volumes']),
- set([VolumeSpec.parse('/opt/tmp:/opt/host/tmp')]))
- def load_from_filename(filename):
- return config.load(config.find('.', [filename]))
- class ExtendsTest(unittest.TestCase):
- def test_extends(self):
- service_dicts = load_from_filename('tests/fixtures/extends/docker-compose.yml')
- self.assertEqual(service_sort(service_dicts), service_sort([
- {
- 'name': 'mydb',
- 'image': 'busybox',
- 'command': 'top',
- },
- {
- 'name': 'myweb',
- 'image': 'busybox',
- 'command': 'top',
- 'links': ['mydb:db'],
- 'environment': {
- "FOO": "1",
- "BAR": "2",
- "BAZ": "2",
- },
- }
- ]))
- def test_nested(self):
- service_dicts = load_from_filename('tests/fixtures/extends/nested.yml')
- self.assertEqual(service_dicts, [
- {
- 'name': 'myweb',
- 'image': 'busybox',
- 'command': '/bin/true',
- 'environment': {
- "FOO": "2",
- "BAR": "2",
- },
- },
- ])
- def test_self_referencing_file(self):
- """
- We specify a 'file' key that is the filename we're already in.
- """
- service_dicts = load_from_filename('tests/fixtures/extends/specify-file-as-self.yml')
- self.assertEqual(service_sort(service_dicts), service_sort([
- {
- 'environment':
- {
- 'YEP': '1', 'BAR': '1', 'BAZ': '3'
- },
- 'image': 'busybox',
- 'name': 'myweb'
- },
- {
- 'environment':
- {'YEP': '1'},
- 'image': 'busybox',
- 'name': 'otherweb'
- },
- {
- 'environment':
- {'YEP': '1', 'BAZ': '3'},
- 'image': 'busybox',
- 'name': 'web'
- }
- ]))
- def test_circular(self):
- with pytest.raises(config.CircularReference) as exc:
- load_from_filename('tests/fixtures/extends/circle-1.yml')
- path = [
- (os.path.basename(filename), service_name)
- for (filename, service_name) in exc.value.trail
- ]
- expected = [
- ('circle-1.yml', 'web'),
- ('circle-2.yml', 'other'),
- ('circle-1.yml', 'web'),
- ]
- self.assertEqual(path, expected)
- def test_extends_validation_empty_dictionary(self):
- with self.assertRaisesRegexp(ConfigurationError, 'service'):
- config.load(
- build_config_details(
- {
- 'web': {'image': 'busybox', 'extends': {}},
- },
- 'tests/fixtures/extends',
- 'filename.yml'
- )
- )
- def test_extends_validation_missing_service_key(self):
- with self.assertRaisesRegexp(ConfigurationError, "'service' is a required property"):
- config.load(
- build_config_details(
- {
- 'web': {'image': 'busybox', 'extends': {'file': 'common.yml'}},
- },
- 'tests/fixtures/extends',
- 'filename.yml'
- )
- )
- def test_extends_validation_invalid_key(self):
- expected_error_msg = (
- "Service 'web' configuration key 'extends' "
- "contains unsupported option: 'rogue_key'"
- )
- with self.assertRaisesRegexp(ConfigurationError, expected_error_msg):
- config.load(
- build_config_details(
- {
- 'web': {
- 'image': 'busybox',
- 'extends': {
- 'file': 'common.yml',
- 'service': 'web',
- 'rogue_key': 'is not allowed'
- }
- },
- },
- 'tests/fixtures/extends',
- 'filename.yml'
- )
- )
- def test_extends_validation_sub_property_key(self):
- expected_error_msg = (
- "Service 'web' configuration key 'extends' 'file' contains 1, "
- "which is an invalid type, it should be a string"
- )
- with self.assertRaisesRegexp(ConfigurationError, expected_error_msg):
- config.load(
- build_config_details(
- {
- 'web': {
- 'image': 'busybox',
- 'extends': {
- 'file': 1,
- 'service': 'web',
- }
- },
- },
- 'tests/fixtures/extends',
- 'filename.yml'
- )
- )
- def test_extends_validation_no_file_key_no_filename_set(self):
- dictionary = {'extends': {'service': 'web'}}
- def load_config():
- return make_service_dict('myweb', dictionary, working_dir='tests/fixtures/extends')
- self.assertRaisesRegexp(ConfigurationError, 'file', load_config)
- def test_extends_validation_valid_config(self):
- service = config.load(
- build_config_details(
- {
- 'web': {'image': 'busybox', 'extends': {'service': 'web', 'file': 'common.yml'}},
- },
- 'tests/fixtures/extends',
- 'common.yml'
- )
- )
- self.assertEquals(len(service), 1)
- self.assertIsInstance(service[0], dict)
- self.assertEquals(service[0]['command'], "/bin/true")
- def test_extended_service_with_invalid_config(self):
- expected_error_msg = "Service 'myweb' has neither an image nor a build path specified"
- with self.assertRaisesRegexp(ConfigurationError, expected_error_msg):
- load_from_filename('tests/fixtures/extends/service-with-invalid-schema.yml')
- def test_extended_service_with_valid_config(self):
- service = load_from_filename('tests/fixtures/extends/service-with-valid-composite-extends.yml')
- self.assertEquals(service[0]['command'], "top")
- def test_extends_file_defaults_to_self(self):
- """
- Test not specifying a file in our extends options that the
- config is valid and correctly extends from itself.
- """
- service_dicts = load_from_filename('tests/fixtures/extends/no-file-specified.yml')
- self.assertEqual(service_sort(service_dicts), service_sort([
- {
- 'name': 'myweb',
- 'image': 'busybox',
- 'environment': {
- "BAR": "1",
- "BAZ": "3",
- }
- },
- {
- 'name': 'web',
- 'image': 'busybox',
- 'environment': {
- "BAZ": "3",
- }
- }
- ]))
- def test_invalid_links_in_extended_service(self):
- expected_error_msg = "services with 'links' cannot be extended"
- with self.assertRaisesRegexp(ConfigurationError, expected_error_msg):
- load_from_filename('tests/fixtures/extends/invalid-links.yml')
- def test_invalid_volumes_from_in_extended_service(self):
- expected_error_msg = "services with 'volumes_from' cannot be extended"
- with self.assertRaisesRegexp(ConfigurationError, expected_error_msg):
- load_from_filename('tests/fixtures/extends/invalid-volumes.yml')
- def test_invalid_net_in_extended_service(self):
- expected_error_msg = "services with 'net: container' cannot be extended"
- with self.assertRaisesRegexp(ConfigurationError, expected_error_msg):
- load_from_filename('tests/fixtures/extends/invalid-net.yml')
- @mock.patch.dict(os.environ)
- def test_valid_interpolation_in_extended_service(self):
- os.environ.update(
- HOSTNAME_VALUE="penguin",
- )
- expected_interpolated_value = "host-penguin"
- service_dicts = load_from_filename('tests/fixtures/extends/valid-interpolation.yml')
- for service in service_dicts:
- self.assertTrue(service['hostname'], expected_interpolated_value)
- @pytest.mark.xfail(IS_WINDOWS_PLATFORM, reason='paths use slash')
- def test_volume_path(self):
- dicts = load_from_filename('tests/fixtures/volume-path/docker-compose.yml')
- paths = [
- VolumeSpec(
- os.path.abspath('tests/fixtures/volume-path/common/foo'),
- '/foo',
- 'rw'),
- VolumeSpec(
- os.path.abspath('tests/fixtures/volume-path/bar'),
- '/bar',
- 'rw')
- ]
- self.assertEqual(set(dicts[0]['volumes']), set(paths))
- def test_parent_build_path_dne(self):
- child = load_from_filename('tests/fixtures/extends/nonexistent-path-child.yml')
- self.assertEqual(child, [
- {
- 'name': 'dnechild',
- 'image': 'busybox',
- 'command': '/bin/true',
- 'environment': {
- "FOO": "1",
- "BAR": "2",
- },
- },
- ])
- def test_load_throws_error_when_base_service_does_not_exist(self):
- err_msg = r'''Cannot extend service 'foo' in .*: Service not found'''
- with self.assertRaisesRegexp(ConfigurationError, err_msg):
- load_from_filename('tests/fixtures/extends/nonexistent-service.yml')
- def test_partial_service_config_in_extends_is_still_valid(self):
- dicts = load_from_filename('tests/fixtures/extends/valid-common-config.yml')
- self.assertEqual(dicts[0]['environment'], {'FOO': '1'})
- def test_extended_service_with_verbose_and_shorthand_way(self):
- services = load_from_filename('tests/fixtures/extends/verbose-and-shorthand.yml')
- self.assertEqual(service_sort(services), service_sort([
- {
- 'name': 'base',
- 'image': 'busybox',
- 'environment': {'BAR': '1'},
- },
- {
- 'name': 'verbose',
- 'image': 'busybox',
- 'environment': {'BAR': '1', 'FOO': '1'},
- },
- {
- 'name': 'shorthand',
- 'image': 'busybox',
- 'environment': {'BAR': '1', 'FOO': '2'},
- },
- ]))
- def test_extends_with_environment_and_env_files(self):
- tmpdir = py.test.ensuretemp('test_extends_with_environment')
- self.addCleanup(tmpdir.remove)
- commondir = tmpdir.mkdir('common')
- commondir.join('base.yml').write("""
- app:
- image: 'example/app'
- env_file:
- - 'envs'
- environment:
- - SECRET
- - TEST_ONE=common
- - TEST_TWO=common
- """)
- tmpdir.join('docker-compose.yml').write("""
- ext:
- extends:
- file: common/base.yml
- service: app
- env_file:
- - 'envs'
- environment:
- - THING
- - TEST_ONE=top
- """)
- commondir.join('envs').write("""
- COMMON_ENV_FILE
- TEST_ONE=common-env-file
- TEST_TWO=common-env-file
- TEST_THREE=common-env-file
- TEST_FOUR=common-env-file
- """)
- tmpdir.join('envs').write("""
- TOP_ENV_FILE
- TEST_ONE=top-env-file
- TEST_TWO=top-env-file
- TEST_THREE=top-env-file
- """)
- expected = [
- {
- 'name': 'ext',
- 'image': 'example/app',
- 'environment': {
- 'SECRET': 'secret',
- 'TOP_ENV_FILE': 'secret',
- 'COMMON_ENV_FILE': 'secret',
- 'THING': 'thing',
- 'TEST_ONE': 'top',
- 'TEST_TWO': 'common',
- 'TEST_THREE': 'top-env-file',
- 'TEST_FOUR': 'common-env-file',
- },
- },
- ]
- with mock.patch.dict(os.environ):
- os.environ['SECRET'] = 'secret'
- os.environ['THING'] = 'thing'
- os.environ['COMMON_ENV_FILE'] = 'secret'
- os.environ['TOP_ENV_FILE'] = 'secret'
- config = load_from_filename(str(tmpdir.join('docker-compose.yml')))
- assert config == expected
- @pytest.mark.xfail(IS_WINDOWS_PLATFORM, reason='paths use slash')
- class ExpandPathTest(unittest.TestCase):
- working_dir = '/home/user/somedir'
- def test_expand_path_normal(self):
- result = config.expand_path(self.working_dir, 'myfile')
- self.assertEqual(result, self.working_dir + '/' + 'myfile')
- def test_expand_path_absolute(self):
- abs_path = '/home/user/otherdir/somefile'
- result = config.expand_path(self.working_dir, abs_path)
- self.assertEqual(result, abs_path)
- def test_expand_path_with_tilde(self):
- test_path = '~/otherdir/somefile'
- with mock.patch.dict(os.environ):
- os.environ['HOME'] = user_path = '/home/user/'
- result = config.expand_path(self.working_dir, test_path)
- self.assertEqual(result, user_path + 'otherdir/somefile')
- class VolumePathTest(unittest.TestCase):
- @pytest.mark.xfail((not IS_WINDOWS_PLATFORM), reason='does not have a drive')
- def test_split_path_mapping_with_windows_path(self):
- windows_volume_path = "c:\\Users\\msamblanet\\Documents\\anvil\\connect\\config:/opt/connect/config:ro"
- expected_mapping = (
- "/opt/connect/config:ro",
- "c:\\Users\\msamblanet\\Documents\\anvil\\connect\\config"
- )
- mapping = config.split_path_mapping(windows_volume_path)
- self.assertEqual(mapping, expected_mapping)
- @pytest.mark.xfail(IS_WINDOWS_PLATFORM, reason='paths use slash')
- class BuildPathTest(unittest.TestCase):
- def setUp(self):
- self.abs_context_path = os.path.join(os.getcwd(), 'tests/fixtures/build-ctx')
- def test_nonexistent_path(self):
- with self.assertRaises(ConfigurationError):
- config.load(
- build_config_details(
- {
- 'foo': {'build': 'nonexistent.path'},
- },
- 'working_dir',
- 'filename.yml'
- )
- )
- def test_relative_path(self):
- relative_build_path = '../build-ctx/'
- service_dict = make_service_dict(
- 'relpath',
- {'build': relative_build_path},
- working_dir='tests/fixtures/build-path'
- )
- self.assertEquals(service_dict['build'], self.abs_context_path)
- def test_absolute_path(self):
- service_dict = make_service_dict(
- 'abspath',
- {'build': self.abs_context_path},
- working_dir='tests/fixtures/build-path'
- )
- self.assertEquals(service_dict['build'], self.abs_context_path)
- def test_from_file(self):
- service_dict = load_from_filename('tests/fixtures/build-path/docker-compose.yml')
- self.assertEquals(service_dict, [{'name': 'foo', 'build': self.abs_context_path}])
- def test_valid_url_in_build_path(self):
- valid_urls = [
- 'git://github.com/docker/docker',
- '[email protected]:docker/docker.git',
- '[email protected]:atlassianlabs/atlassian-docker.git',
- 'https://github.com/docker/docker.git',
- 'http://github.com/docker/docker.git',
- 'github.com/docker/docker.git',
- ]
- for valid_url in valid_urls:
- service_dict = config.load(build_config_details({
- 'validurl': {'build': valid_url},
- }, '.', None))
- assert service_dict[0]['build'] == valid_url
- def test_invalid_url_in_build_path(self):
- invalid_urls = [
- 'example.com/bogus',
- 'ftp://example.com/',
- '/path/does/not/exist',
- ]
- for invalid_url in invalid_urls:
- with pytest.raises(ConfigurationError) as exc:
- config.load(build_config_details({
- 'invalidurl': {'build': invalid_url},
- }, '.', None))
- assert 'build path' in exc.exconly()
- class GetDefaultConfigFilesTestCase(unittest.TestCase):
- files = [
- 'docker-compose.yml',
- 'docker-compose.yaml',
- 'fig.yml',
- 'fig.yaml',
- ]
- def test_get_config_path_default_file_in_basedir(self):
- for index, filename in enumerate(self.files):
- self.assertEqual(
- filename,
- get_config_filename_for_files(self.files[index:]))
- with self.assertRaises(config.ComposeFileNotFound):
- get_config_filename_for_files([])
- def test_get_config_path_default_file_in_parent_dir(self):
- """Test with files placed in the subdir"""
- def get_config_in_subdir(files):
- return get_config_filename_for_files(files, subdir=True)
- for index, filename in enumerate(self.files):
- self.assertEqual(filename, get_config_in_subdir(self.files[index:]))
- with self.assertRaises(config.ComposeFileNotFound):
- get_config_in_subdir([])
- def get_config_filename_for_files(filenames, subdir=None):
- def make_files(dirname, filenames):
- for fname in filenames:
- with open(os.path.join(dirname, fname), 'w') as f:
- f.write('')
- project_dir = tempfile.mkdtemp()
- try:
- make_files(project_dir, filenames)
- if subdir:
- base_dir = tempfile.mkdtemp(dir=project_dir)
- else:
- base_dir = project_dir
- filename, = config.get_default_config_files(base_dir)
- return os.path.basename(filename)
- finally:
- shutil.rmtree(project_dir)
|