From 2f3929eb62ad19899df0eb3aebf9e15c22d059e3 Mon Sep 17 00:00:00 2001 From: Gloria Ciavarrini Date: Wed, 28 Dec 2022 12:30:33 +0100 Subject: [PATCH 1/3] Add support for custom driver name This commit adds support for custom driver names starting with `molecule_`, `molecule-`, `custom_`, or `custom-` Signed-off-by: Gloria Ciavarrini --- src/molecule/data/molecule.json | 43 +++++++++++++------ src/molecule/model/schema_v3.py | 10 ++++- .../test/unit/model/v2/test_driver_section.py | 25 ++++++++--- 3 files changed, 58 insertions(+), 20 deletions(-) diff --git a/src/molecule/data/molecule.json b/src/molecule/data/molecule.json index 5dbe465ade..5dcdf2bc47 100644 --- a/src/molecule/data/molecule.json +++ b/src/molecule/data/molecule.json @@ -79,22 +79,37 @@ "type": "string" }, "name": { - "enum": [ - "azure", - "ec2", - "delegated", - "docker", - "containers", - "openstack", - "podman", - "vagrant", - "digitalocean", - "gce", - "libvirt", - "lxd" + "anyOf": [ + { + "enum": [ + "azure", + "ec2", + "delegated", + "docker", + "containers", + "openstack", + "podman", + "vagrant", + "digitalocean", + "gce", + "libvirt", + "lxd" + ] + }, + { + "pattern": "^molecule[_\\-][a-zA-Z0-9:_.\\-]+$", + "type": "string" + }, + { + "pattern": "^custom[_\\-][a-zA-Z0-9:_.\\-]+$", + "type": "string" + } ], "title": "Name", - "type": "string" + "type": "string", + "messages": { + "anyOf": "is not one of ['azure', 'ec2', 'delegated', 'docker', 'containers', 'openstack', 'podman', 'vagrant', 'digitalocean', 'gce', 'libvirt', 'lxd', 'molecule-*', 'molecule_*', 'custom-*', 'custom_*']" + } }, "options": { "$ref": "#/$defs/MoleculeDriverOptionsModel" diff --git a/src/molecule/model/schema_v3.py b/src/molecule/model/schema_v3.py index 6f7944c8e9..20e45fe138 100644 --- a/src/molecule/model/schema_v3.py +++ b/src/molecule/model/schema_v3.py @@ -38,6 +38,14 @@ def validate(c): try: jsonschema_validate(c, schema) except ValidationError as exc: - result.append(exc.message) + # handle validation error for driver name + if exc.json_path == "$.driver.name" and exc.message.endswith( + ("is not of type 'string'", "is not valid under any of the given schemas") + ): + wrong_driver_name = str(exc.message.split()[0]) + driver_name_err_msg = exc.schema["messages"]["anyOf"] + result.append(" ".join((wrong_driver_name, driver_name_err_msg))) + else: + result.append(exc.message) return result diff --git a/src/molecule/test/unit/model/v2/test_driver_section.py b/src/molecule/test/unit/model/v2/test_driver_section.py index b653fe3b56..34ed013517 100644 --- a/src/molecule/test/unit/model/v2/test_driver_section.py +++ b/src/molecule/test/unit/model/v2/test_driver_section.py @@ -49,17 +49,32 @@ def _model_driver_errors_section_data(): } } +@pytest.fixture +def _model_driver_errors_section_data_no_prefix(): + return { + "driver": { + "name": "random_name", + } + } @pytest.mark.parametrize( - "_config", ["_model_driver_errors_section_data"], indirect=True + "_config", + [ + "_model_driver_errors_section_data", + "_model_driver_errors_section_data_no_prefix" + ], + indirect=True ) def test_driver_has_errors(_config): - x = [ - "0 is not one of ['azure', 'ec2', 'delegated', 'docker', 'containers', 'openstack', 'podman', 'vagrant', 'digitalocean', 'gce', 'libvirt', 'lxd']" - ] + base_error_msg = "is not one of ['azure', 'ec2', 'delegated', 'docker', 'containers', 'openstack', 'podman', 'vagrant', 'digitalocean', 'gce', 'libvirt', 'lxd', 'molecule_*', 'custom_*']" - assert x == schema_v3.validate(_config) + driver_name = str(_config["driver"]["name"]) + if(type(_config["driver"]["name"]) is str): + # add single quotes for string + driver_name = f"'{driver_name}'" + error_msg = [' '.join((driver_name, base_error_msg))] + assert error_msg == schema_v3.validate(_config) @pytest.fixture def _model_driver_provider_name_nullable_section_data(): From 83fb15e57985c6599905ffde03964f6294e7223f Mon Sep 17 00:00:00 2001 From: Gloria Ciavarrini Date: Wed, 28 Dec 2022 12:32:56 +0100 Subject: [PATCH 2/3] Test custom driver name feature Test if `molecule-`, `molecule_`, `custom-`, `custom_` are valid prefixes as driver name. Test that any other name fails the validation. Signed-off-by: Gloria Ciavarrini --- .../test/unit/model/v2/test_driver_section.py | 37 ++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/src/molecule/test/unit/model/v2/test_driver_section.py b/src/molecule/test/unit/model/v2/test_driver_section.py index 34ed013517..5229b8b21b 100644 --- a/src/molecule/test/unit/model/v2/test_driver_section.py +++ b/src/molecule/test/unit/model/v2/test_driver_section.py @@ -49,6 +49,7 @@ def _model_driver_errors_section_data(): } } + @pytest.fixture def _model_driver_errors_section_data_no_prefix(): return { @@ -57,25 +58,27 @@ def _model_driver_errors_section_data_no_prefix(): } } + @pytest.mark.parametrize( "_config", [ "_model_driver_errors_section_data", - "_model_driver_errors_section_data_no_prefix" + "_model_driver_errors_section_data_no_prefix", ], - indirect=True + indirect=True, ) def test_driver_has_errors(_config): - base_error_msg = "is not one of ['azure', 'ec2', 'delegated', 'docker', 'containers', 'openstack', 'podman', 'vagrant', 'digitalocean', 'gce', 'libvirt', 'lxd', 'molecule_*', 'custom_*']" + base_error_msg = "is not one of ['azure', 'ec2', 'delegated', 'docker', 'containers', 'openstack', 'podman', 'vagrant', 'digitalocean', 'gce', 'libvirt', 'lxd', 'molecule-*', 'molecule_*', 'custom-*', 'custom_*']" driver_name = str(_config["driver"]["name"]) - if(type(_config["driver"]["name"]) is str): + if type(_config["driver"]["name"]) is str: # add single quotes for string driver_name = f"'{driver_name}'" - error_msg = [' '.join((driver_name, base_error_msg))] + error_msg = [" ".join((driver_name, base_error_msg))] assert error_msg == schema_v3.validate(_config) + @pytest.fixture def _model_driver_provider_name_nullable_section_data(): return {"driver": {"provider": {"name": None}}} @@ -93,11 +96,35 @@ def _model_driver_allows_delegated_section_data(): return {"driver": {"name": "delegated"}} +@pytest.fixture +def _model_driver_allows_molecule_section_data1(): + return {"driver": {"name": "molecule-test_driver.name"}} + + +@pytest.fixture +def _model_driver_allows_molecule_section_data2(): + return {"driver": {"name": "molecule_test_driver.name"}} + + +@pytest.fixture +def _model_driver_allows_custom_section_data1(): + return {"driver": {"name": "custom-test_driver.name"}} + + +@pytest.fixture +def _model_driver_allows_custom_section_data2(): + return {"driver": {"name": "custom_test_driver.name"}} + + ### @pytest.mark.parametrize( "_config", [ ("_model_driver_allows_delegated_section_data"), + ("_model_driver_allows_molecule_section_data1"), + ("_model_driver_allows_molecule_section_data2"), + ("_model_driver_allows_custom_section_data2"), + ("_model_driver_allows_custom_section_data1"), ], indirect=True, ) From 567215263ce75ea193e9f4bb08bd4c0bf15869e7 Mon Sep 17 00:00:00 2001 From: Gloria Ciavarrini Date: Tue, 10 Jan 2023 15:14:11 +0100 Subject: [PATCH 3/3] Update `PYTEST_REQPASS` value Signed-off-by: Gloria Ciavarrini --- .github/workflows/tox.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index abafd94976..b268ace470 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -39,7 +39,7 @@ jobs: runs-on: ubuntu-latest needs: pre env: - PYTEST_REQPASS: 448 + PYTEST_REQPASS: 453 # limit potential endless looks like we had with build-containers timeout-minutes: 20 strategy: