diff --git a/molecule/model/schema_v2.py b/molecule/model/schema_v2.py index c9c851300..4bd0fc82b 100644 --- a/molecule/model/schema_v2.py +++ b/molecule/model/schema_v2.py @@ -683,6 +683,7 @@ def pre_validate_base_schema(env, keep_string): 'type': 'list', 'schema': { 'type': 'string', + 'coerce': 'exposed_ports' } }, 'published_ports': { @@ -970,6 +971,17 @@ def _validate_disallowed(self, disallowed, field, value): msg = 'disallowed user provided config option' self._error(field, msg) + def _normalize_coerce_exposed_ports(self, value): + """Coerce ``exposed_ports`` values to string. + + Not all types that can be specified by the user are acceptable and + therefore we cannot simply pass a ``'coerce': 'string'`` to the schema + definition. + """ + if type(value) == int: + return str(value) + return value + def _validate_molecule_env_var(self, molecule_env_var, field, value): """ Readonly but with a custom error. diff --git a/test/unit/model/v2/test_platforms_section.py b/test/unit/model/v2/test_platforms_section.py index a5971f640..4232b6cb5 100644 --- a/test/unit/model/v2/test_platforms_section.py +++ b/test/unit/model/v2/test_platforms_section.py @@ -137,6 +137,13 @@ def test_platforms_unique_names(_config): assert expected_validation_errors == schema_v2.validate(_config) +@pytest.mark.parametrize( + '_config', ['_model_platforms_docker_section_data'], indirect=True) +def test_platforms_docker_exposed_ports_coerced(_config): + _config['platforms'][0]['exposed_ports'] = [9904] + assert {} == schema_v2.validate(_config) + + @pytest.fixture def _model_platforms_docker_errors_section_data(): return { @@ -171,7 +178,7 @@ def _model_platforms_docker_errors_section_data(): int(), ], 'exposed_ports': [ - int(), + bool(), ], 'published_ports': [ int(),