Skip to content

Commit

Permalink
[Molecule v25.1.0] Replace util.run_command() with get_app(Path()).ru…
Browse files Browse the repository at this point in the history
…n_command()

From ansible/molecule#4359 `util.run_command()`
should now replaced with `get_app(Path()).run_command()`.

Fixes ansible/molecule#4372

Signed-off-by: Wong Hoi Sing Edison <[email protected]>
  • Loading branch information
hswong3i authored and apatard committed Jan 22, 2025
1 parent 3252e87 commit d511fe6
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 71 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ jobs:
TOX_PARALLEL_NO_SPINNER: 1

steps:
- name: Switch to using Python 3.9 by default
- name: Switch to using Python 3.10 by default
uses: actions/setup-python@v5
with:
python-version: 3.9
python-version: 3.10

- name: Install tox
run: >-
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ jobs:
id: generate_matrix
uses: coactions/dynamic-matrix@v1
with:
min_python: "3.9"
min_python: "3.10"
max_python: "3.12"
default_python: "3.9" # used by jobs in other_names
default_python: "3.10" # used by jobs in other_names
other_names: |
lint
pkg
Expand Down
6 changes: 4 additions & 2 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import random
import shutil
import string
from pathlib import Path

import pytest

from molecule import config, logger, util
from molecule import config, logger
from molecule.app import get_app
from molecule.scenario import ephemeral_directory

LOG = logger.get_logger(__name__)
Expand All @@ -18,7 +20,7 @@ def run_command(cmd, env=os.environ, log=True):
if log:
cmd = _rebake_command(cmd, env)
cmd = cmd.bake(_truncate_exc=False)
return util.run_command(cmd, env=env)
return get_app(Path()).run_command(cmd, env=env)


def _rebake_command(cmd, env, out=LOG.info, err=LOG.error):
Expand Down
16 changes: 8 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build-backend = "setuptools.build_meta"

[project]
# https://peps.python.org/pep-0621/#readme
requires-python = ">=3.9"
requires-python = ">=3.10"
dynamic = ["version"]
name = "molecule-plugins"
description = "Molecule Plugins"
Expand All @@ -26,7 +26,6 @@ classifiers = [
"Operating System :: MacOS",
"Operating System :: POSIX",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
Expand All @@ -40,7 +39,7 @@ classifiers = [
keywords = ["ansible", "testing", "molecule", "plugin"]
dependencies = [
# molecule plugins are not allowed to mention Ansible as a direct dependency
"molecule >= 6.0.0a1",
"molecule >= 25.1.0",
]

[project.urls]
Expand All @@ -52,7 +51,7 @@ changelog = "https://github.com/ansible-community/molecule-plugins/releases"
[project.optional-dependencies]
test = [
"pytest-helpers-namespace >= 2019.1.8",
"molecule[test] >= 6.0.0a1"
"molecule[test] >= 25.1.0"
]
azure = []
docker = [
Expand Down Expand Up @@ -89,7 +88,7 @@ openstack = [
]

[tool.ruff]
ignore = [
lint.ignore = [
"E501", # we use black
# we deliberately ignore these:
"EM102",
Expand All @@ -101,6 +100,7 @@ ignore = [
"B028",
"BLE",
"C901",
"COM812",
"D",
"DTZ",
"FBT",
Expand All @@ -119,15 +119,15 @@ ignore = [
"FIX002",
"TRY",
]
select = ["ALL"]
lint.select = ["ALL"]
target-version = "py39"
# Same as Black.
line-length = 88

[tool.ruff.flake8-pytest-style]
[tool.ruff.lint.flake8-pytest-style]
parametrize-values-type = "tuple"

[tool.ruff.isort]
[tool.ruff.lint.isort]
known-first-party = ["molecule_plugins"]

[project.entry-points."molecule.driver"]
Expand Down
1 change: 0 additions & 1 deletion src/molecule_plugins/docker/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
# DEALINGS IN THE SOFTWARE.
"""Docker Driver Module."""


import os

from molecule import logger
Expand Down
9 changes: 6 additions & 3 deletions src/molecule_plugins/podman/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@
# DEALINGS IN THE SOFTWARE.
"""Podman Driver Module."""


import os
import warnings
from pathlib import Path
from shutil import which

from ansible_compat.runtime import Runtime
from packaging.version import Version

from molecule import logger, util
from molecule.api import Driver, MoleculeRuntimeWarning
from molecule.app import get_app
from molecule.constants import RC_SETUP_ERROR
from molecule.util import run_command, sysexit_with_message
from molecule.util import sysexit_with_message

log = logger.get_logger(__name__)

Expand Down Expand Up @@ -246,4 +247,6 @@ def required_collections(self) -> dict[str, str]:

def reset(self):
# keep `--filter` in sync with playbooks/create.yml
run_command(["podman", "rm", "--force", "--filter=label=owner=molecule"])
get_app(Path()).run_command(
["podman", "rm", "--force", "--filter=label=owner=molecule"]
)
12 changes: 3 additions & 9 deletions src/molecule_plugins/vagrant/modules/vagrant.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,7 @@ def _conf_instance(self, instance_name):
try:
return self._vagrant.conf(vm_name=instance_name)
except Exception:
msg = "Failed to get vagrant config for {}: See log file '{}'".format(
instance_name,
self._get_stderr_log(),
)
msg = f"Failed to get vagrant config for {instance_name}: See log file '{self._get_stderr_log()}'"
with open(self._get_stderr_log(), encoding="utf-8") as f:
self.result["stderr"] = f.read()
self._module.fail_json(msg=msg, **self.result)
Expand All @@ -517,10 +514,7 @@ def _status_instance(self, instance_name):

return {"name": s.name, "state": s.state, "provider": s.provider}
except Exception:
msg = "Failed to get status for {}: See log file '{}'".format(
instance_name,
self._get_stderr_log(),
)
msg = f"Failed to get status for {instance_name}: See log file '{self._get_stderr_log()}'"
with open(self._get_stderr_log(), encoding="utf-8") as f:
self.result["stderr"] = f.read()
self._module.fail_json(msg=msg, **self.result)
Expand Down Expand Up @@ -697,7 +691,7 @@ def _get_stdout_log(self):
def _get_stderr_log(self):
return self._get_vagrant_log("err")

def _get_vagrant_log(self, __type):
def _get_vagrant_log(self, __type, /):
return os.path.join(self._config["workdir"], f"vagrant.{__type}")


Expand Down
13 changes: 7 additions & 6 deletions test/azure/functional/test_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,21 @@
# DEALINGS IN THE SOFTWARE.

import os
from pathlib import Path

import pytest

from conftest import change_dir_to
from molecule import logger
from molecule.util import run_command
from molecule.app import get_app

LOG = logger.get_logger(__name__)


def test_azure_command_init_scenario(temp_dir):
role_directory = os.path.join(temp_dir.strpath, "test_init")
cmd = ["ansible-galaxy", "role", "init", "test_init"]
result = run_command(cmd)
result = get_app(Path()).run_command(cmd)
assert result.returncode == 0

with change_dir_to(role_directory):
Expand All @@ -47,7 +48,7 @@ def test_azure_command_init_scenario(temp_dir):
"-a",
'path=meta/main.yml line=" namespace: foo" insertafter=" author: your name"',
]
run_command(cmd_meta, check=True)
get_app(Path()).run_command(cmd_meta, check=True)

# we need to inject namespace info into tests/test.yml
cmd_tests = [
Expand All @@ -59,7 +60,7 @@ def test_azure_command_init_scenario(temp_dir):
"-a",
'path=tests/test.yml line=" - foo.test_init" regex="^(.*) - test_init"',
]
run_command(cmd_tests, check=True)
get_app(Path()).run_command(cmd_tests, check=True)

molecule_directory = pytest.helpers.molecule_directory()
scenario_directory = os.path.join(molecule_directory, "test_scenario")
Expand All @@ -71,7 +72,7 @@ def test_azure_command_init_scenario(temp_dir):
"--driver-name",
"azure",
]
result = run_command(cmd)
result = get_app(Path()).run_command(cmd)
assert result.returncode == 0

assert os.path.isdir(scenario_directory)
Expand All @@ -82,5 +83,5 @@ def test_azure_command_init_scenario(temp_dir):
# temporary trick to pass on CI/CD
if "AZURE_SECRET" in os.environ:
cmd = ["molecule", "test", "-s", "test-scenario"]
result = run_command(cmd)
result = get_app(Path()).run_command(cmd)
assert result.returncode == 0
8 changes: 5 additions & 3 deletions test/containers/functional/test_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
"""Functional Tests."""

import os
from pathlib import Path

from conftest import change_dir_to, molecule_directory
from molecule import logger
from molecule.util import run_command
from molecule.app import get_app

LOG = logger.get_logger(__name__)

Expand All @@ -40,7 +42,7 @@ def test_containers_command_init_scenario(temp_dir):
"--driver-name",
"containers",
]
result = run_command(cmd)
result = get_app(Path()).run_command(cmd)
assert result.returncode == 0

assert os.path.isdir(scenario_directory)
Expand All @@ -49,5 +51,5 @@ def test_containers_command_init_scenario(temp_dir):
# is shorter but comprehensive enough to test the most important
# functionality: destroy, dependency, create, prepare, converge
cmd = ["molecule", "check", "-s", "default"]
result = run_command(cmd)
result = get_app(Path()).run_command(cmd)
assert result.returncode == 0
21 changes: 13 additions & 8 deletions test/docker/test_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import pathlib
import shutil
import subprocess
from pathlib import Path

import pytest

from conftest import change_dir_to
from molecule import logger
from molecule.util import run_command
from molecule.app import get_app

LOG = logger.get_logger(__name__)

Expand Down Expand Up @@ -40,21 +41,25 @@ def test_command_init_and_test_scenario(tmp_path: pathlib.Path, DRIVER: str) ->
"--driver-name",
DRIVER,
]
result = run_command(cmd)
result = get_app(tmp_path).run_command(cmd)
assert result.returncode == 0

assert scenario_directory.exists()

# run molecule reset as this may clean some leftovers from other
# test runs and also ensure that reset works.
result = run_command(["molecule", "reset"]) # default scenario
result = get_app(tmp_path).run_command(
["molecule", "reset"]
) # default scenario
assert result.returncode == 0

result = run_command(["molecule", "reset", "-s", scenario_name])
result = get_app(tmp_path).run_command(
["molecule", "reset", "-s", scenario_name]
)
assert result.returncode == 0

cmd = ["molecule", "--debug", "test", "-s", scenario_name]
result = run_command(cmd)
result = get_app(tmp_path).run_command(cmd)
assert result.returncode == 0


Expand All @@ -63,7 +68,7 @@ def test_command_static_scenario() -> None:
"""Validate that the scenario we included with code still works."""
cmd = ["molecule", "test"]

result = run_command(cmd)
result = get_app(Path()).run_command(cmd)
assert result.returncode == 0


Expand All @@ -72,7 +77,7 @@ def test_dockerfile_with_context() -> None:
"""Verify that Dockerfile.j2 with context works."""
with change_dir_to("test/docker/scenarios/with-context"):
cmd = ["molecule", "--debug", "test"]
result = run_command(cmd)
result = get_app(Path()).run_command(cmd)
assert result.returncode == 0


Expand All @@ -82,5 +87,5 @@ def test_env_substitution() -> None:
os.environ["MOLECULE_ROLE_IMAGE"] = "debian:bullseye"
with change_dir_to("test/docker/scenarios/env-substitution"):
cmd = ["molecule", "--debug", "test"]
result = run_command(cmd)
result = get_app(Path()).run_command(cmd)
assert result.returncode == 0
9 changes: 5 additions & 4 deletions test/ec2/functional/test_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
# DEALINGS IN THE SOFTWARE.

import os
from pathlib import Path

import pytest

from conftest import change_dir_to, metadata_lint_update
from molecule import logger
from molecule.util import run_command
from molecule.app import get_app

LOG = logger.get_logger(__name__)

Expand All @@ -34,7 +35,7 @@
def test_ec2_command_init_scenario(temp_dir):
role_directory = os.path.join(temp_dir.strpath, "test-init")
cmd = ["molecule", "init", "role", "test-init"]
assert run_command(cmd).returncode == 0
assert get_app(Path()).run_command(cmd).returncode == 0
metadata_lint_update(role_directory)

with change_dir_to(role_directory):
Expand All @@ -48,11 +49,11 @@ def test_ec2_command_init_scenario(temp_dir):
"--role_name=test-init",
"--driver-name=ec2",
]
assert run_command(cmd).returncode == 0
assert get_app(Path()).run_command(cmd).returncode == 0

assert os.path.isdir(scenario_directory)
os.unlink(os.path.join(scenario_directory, "create.yml"))
os.unlink(os.path.join(scenario_directory, "destroy.yml"))

cmd = ["molecule", "test", "-s", "test-scenario"]
assert run_command(cmd).returncode == 0
assert get_app(Path()).run_command(cmd).returncode == 0
Loading

0 comments on commit d511fe6

Please sign in to comment.