From df1f67eb1e0c8683d9110893f4baab5ee32698d5 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Wed, 12 Jul 2023 15:50:20 +0100 Subject: [PATCH 1/6] Change current directory to scenario before running playbooks From now on, instead of using the current working directory when running as scenario, molecule will chdir to scenario folder. This change will enable people to use scenario specific ansible.cfg files. Follow-up change will enable running molecule scenarios directly from within them. When user is inside a scenario, molecule will recognize it and no longer require the --scenario argument. --- .github/workflows/tox.yml | 2 +- molecule/smoke/converge.yml | 10 ++++++++++ molecule/smoke/molecule.yml | 2 ++ src/molecule/config.py | 2 ++ src/molecule/provisioner/ansible_playbook.py | 2 ++ src/molecule/test/b_functional/test_command.py | 10 ++++++++++ 6 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 molecule/smoke/converge.yml create mode 100644 molecule/smoke/molecule.yml diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index 9367ea7de..1b999ab3b 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -51,7 +51,7 @@ jobs: matrix: ${{ fromJson(needs.pre.outputs.matrix) }} env: - PYTEST_REQPASS: 445 + PYTEST_REQPASS: 446 steps: - uses: actions/checkout@v3 with: diff --git a/molecule/smoke/converge.yml b/molecule/smoke/converge.yml new file mode 100644 index 000000000..6f891942f --- /dev/null +++ b/molecule/smoke/converge.yml @@ -0,0 +1,10 @@ +- name: Converge + hosts: localhost + tasks: + - name: Assert CWD is the same as playbook_dir + ansible.builtin.assert: + that: + - playbook_dir == lookup('env', 'PWD') + msg: > + Molecule should automaticaly chdir to scenario directory {{ playbook_dir }} + but we found that it runs from {{ lookup('env', 'PWD') }} instead." diff --git a/molecule/smoke/molecule.yml b/molecule/smoke/molecule.yml new file mode 100644 index 000000000..0e6418d79 --- /dev/null +++ b/molecule/smoke/molecule.yml @@ -0,0 +1,2 @@ +platforms: + - name: localhost diff --git a/src/molecule/config.py b/src/molecule/config.py index 2453bd695..ba60d939b 100644 --- a/src/molecule/config.py +++ b/src/molecule/config.py @@ -24,6 +24,7 @@ import os import warnings from collections.abc import MutableMapping +from pathlib import Path from uuid import uuid4 from ansible_compat.ports import cache, cached_property @@ -114,6 +115,7 @@ def __init__( self._run_uuid = str(uuid4()) self.project_directory = os.getenv("MOLECULE_PROJECT_DIRECTORY", os.getcwd()) self.runtime = app.runtime + self.scenario_path = Path(molecule_file).parent def after_init(self): self.config = self._reget_config() diff --git a/src/molecule/provisioner/ansible_playbook.py b/src/molecule/provisioner/ansible_playbook.py index 56134abf8..87028627b 100644 --- a/src/molecule/provisioner/ansible_playbook.py +++ b/src/molecule/provisioner/ansible_playbook.py @@ -110,10 +110,12 @@ def execute(self, action_args=None): with warnings.catch_warnings(record=True) as warns: warnings.filterwarnings("default", category=MoleculeRuntimeWarning) self._config.driver.sanity_checks() + cwd = self._config.scenario_path result = util.run_command( cmd=self._ansible_command, env=self._env, debug=self._config.debug, + cwd=cwd, ) if result.returncode != 0: diff --git a/src/molecule/test/b_functional/test_command.py b/src/molecule/test/b_functional/test_command.py index 28b23e4a4..1cbb0b654 100644 --- a/src/molecule/test/b_functional/test_command.py +++ b/src/molecule/test/b_functional/test_command.py @@ -343,3 +343,13 @@ def test_podman() -> None: ).returncode == 0 ) + + +def test_smoke() -> None: + """Execute smoke-test scenario that should spot potentially breaking changes.""" + assert ( + run_command( + ["molecule", "test", "--scenario-name", "smoke"], + ).returncode + == 0 + ) From 309884e79ce1726f1ad665d6f05b1e3df4c11a1e Mon Sep 17 00:00:00 2001 From: "Bradley A. Thornton" Date: Mon, 24 Jul 2023 06:48:41 -0700 Subject: [PATCH 2/6] Update converge.yml --- molecule/smoke/converge.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/molecule/smoke/converge.yml b/molecule/smoke/converge.yml index 6f891942f..03c01c0a2 100644 --- a/molecule/smoke/converge.yml +++ b/molecule/smoke/converge.yml @@ -6,5 +6,5 @@ that: - playbook_dir == lookup('env', 'PWD') msg: > - Molecule should automaticaly chdir to scenario directory {{ playbook_dir }} + Molecule should automatically chdir to scenario directory {{ playbook_dir }} but we found that it runs from {{ lookup('env', 'PWD') }} instead." From 59b2d70d399a6ba4cb5a8f24feea0aa776d7b9c3 Mon Sep 17 00:00:00 2001 From: Ajinkya Udgirkar Date: Thu, 3 Aug 2023 14:07:19 +0530 Subject: [PATCH 3/6] fixed missing closing brackets --- src/molecule/test/b_functional/test_command.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/molecule/test/b_functional/test_command.py b/src/molecule/test/b_functional/test_command.py index 1a2995a4e..8e86d87c8 100644 --- a/src/molecule/test/b_functional/test_command.py +++ b/src/molecule/test/b_functional/test_command.py @@ -356,6 +356,6 @@ def test_docker() -> None: def test_smoke() -> None: """Execute smoke-test scenario that should spot potentially breaking changes.""" - assert ( - run_command( - ["molecule", "test", "--scenario-name", "smoke"], + assert run_command( + ["molecule", "test", "--scenario-name", "smoke"], + ) From 29fb31729d990da90279022d151da7c150c83419 Mon Sep 17 00:00:00 2001 From: Ajinkya Udgirkar Date: Thu, 3 Aug 2023 14:13:28 +0530 Subject: [PATCH 4/6] fixed missing closing brackets --- .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 1b999ab3b..c2cd6a432 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -51,7 +51,7 @@ jobs: matrix: ${{ fromJson(needs.pre.outputs.matrix) }} env: - PYTEST_REQPASS: 446 + PYTEST_REQPASS: 447 steps: - uses: actions/checkout@v3 with: From 05d10bfab6b923a0bec745a3adcbe9c0516e067e Mon Sep 17 00:00:00 2001 From: Ajinkya Udgirkar Date: Thu, 3 Aug 2023 14:13:54 +0530 Subject: [PATCH 5/6] fixed missing closing brackets --- .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 c2cd6a432..1b999ab3b 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -51,7 +51,7 @@ jobs: matrix: ${{ fromJson(needs.pre.outputs.matrix) }} env: - PYTEST_REQPASS: 447 + PYTEST_REQPASS: 446 steps: - uses: actions/checkout@v3 with: From 3fa6b9eaf7259695cf4825d3643bee0fc119849a Mon Sep 17 00:00:00 2001 From: Ajinkya Udgirkar Date: Thu, 3 Aug 2023 14:21:27 +0530 Subject: [PATCH 6/6] Update total paasing tests --- .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 1b999ab3b..c2cd6a432 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -51,7 +51,7 @@ jobs: matrix: ${{ fromJson(needs.pre.outputs.matrix) }} env: - PYTEST_REQPASS: 446 + PYTEST_REQPASS: 447 steps: - uses: actions/checkout@v3 with: