Skip to content

Commit 6a828ab

Browse files
lennyshpre-commit-ci[bot]
authored andcommitted
fix(devel): drop --driver-name from init scenario, patch molecule.yml… (#358)
* fix(devel): drop --driver-name from init scenario, patch molecule.yml for Molecule 4.x Made-with: Cursor * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * linting fixes * force pipeline to re-run due to timeout on last job * revert dict sort back * XFAIL fixes? * fix role init * remove linting not needed * remove --role-name * fix docker test? * docker fixes --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 269bbd1 commit 6a828ab

11 files changed

Lines changed: 203 additions & 83 deletions

File tree

.config/dictionary.txt

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,78 @@ Libera
22
OAEP
33
Sbarnea
44
Sorin
5+
addopts
6+
allitems
57
assumeyes
68
azcollection
9+
bindep
10+
buildargs
711
cachier
812
calver
13+
certdir
914
cgroupfs
1015
cgroupns
1116
codespell
17+
confpath
18+
contentdir
19+
cpuset
1220
createdbymolecule
1321
devel
1422
dockerfiles
23+
endraw
1524
endmacro
25+
equalto
1626
esxi
27+
Freenode
28+
FIPS
1729
fqcn
30+
fstack
1831
googleapiclient
32+
insertafter
33+
iitem
34+
keypair
1935
keypairs
2036
libvirtd
2137
libvirtdoverlayfs
22-
libvirtdoverlayfs
2338
lineinfile
2439
linuxgce
40+
machineaccount
41+
makecache
2542
netns
2643
noconfirm
2744
norecursedirs
2845
numvcpus
2946
openstacksdk
3047
overlayfs
3148
pacman
49+
passwordless
3250
polkit
3351
pypa
3452
rebake
53+
resourcegroup
54+
rpmbuild
3555
runas
3656
sameas
57+
seccomp
3758
slirp
59+
snet
60+
snat
61+
subnetworks
3862
tcp_syncookies
63+
testbox
64+
testconf
3965
testinfra
66+
testrole
67+
TESTBOX
4068
tlsverify
4169
tmpfs
70+
ulimits
4271
vagrantdir
4372
virbr
73+
virtualnetwork
4474
virsh
4575
virtualmachine
76+
vnet
4677
westus
4778
xbps
4879
xfreerdp

conftest.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import contextlib
22
import os
33
import random
4+
import re
45
import shutil
56
import string
67
from pathlib import Path
@@ -78,6 +79,30 @@ def get_molecule_file(path):
7879
return config.molecule_file(path)
7980

8081

82+
def set_driver_in_scenario_molecule_yml(
83+
scenario_directory: str, driver_name: str
84+
) -> None:
85+
"""Set driver name in molecule.yml after 'molecule init scenario' (no --driver-name).
86+
87+
Molecule 4.x+ removed --driver-name from 'init scenario'. Init then patch the file.
88+
"""
89+
molecule_yml = os.path.join(scenario_directory, "molecule.yml")
90+
with open(molecule_yml) as f:
91+
content = f.read()
92+
driver_block = f"\ndriver:\n name: {driver_name}\n"
93+
if "driver:" not in content:
94+
content = content.replace("---\n", "---" + driver_block, 1)
95+
else:
96+
content = re.sub(
97+
r"(driver:\s*\n\s*name:)\s*\w+",
98+
f"\\1 {driver_name}",
99+
content,
100+
count=1,
101+
)
102+
with open(molecule_yml, "w") as f:
103+
f.write(content)
104+
105+
81106
def metadata_lint_update(role_directory: str) -> None:
82107
# By default, ansible-lint will fail on newly-created roles because the
83108
# fields in this file have not been changed from their defaults. This is

test/azure/functional/test_azure.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import pytest
2626

27-
from conftest import change_dir_to
27+
from conftest import change_dir_to, set_driver_in_scenario_molecule_yml
2828
from molecule import logger
2929
from molecule.app import get_app
3030

@@ -69,11 +69,10 @@ def test_azure_command_init_scenario(temp_dir):
6969
"init",
7070
"scenario",
7171
"test_scenario",
72-
"--driver-name",
73-
"azure",
7472
]
7573
result = get_app(Path()).run_command(cmd)
7674
assert result.returncode == 0
75+
set_driver_in_scenario_molecule_yml(scenario_directory, "azure")
7776

7877
assert os.path.isdir(scenario_directory)
7978

test/containers/functional/test_containers.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323
import os
2424
from pathlib import Path
2525

26-
from conftest import change_dir_to, molecule_directory
26+
from conftest import (
27+
change_dir_to,
28+
molecule_directory,
29+
set_driver_in_scenario_molecule_yml,
30+
)
2731
from molecule import logger
2832
from molecule.app import get_app
2933

@@ -39,11 +43,10 @@ def test_containers_command_init_scenario(temp_dir):
3943
"init",
4044
"scenario",
4145
"default",
42-
"--driver-name",
43-
"containers",
4446
]
4547
result = get_app(Path()).run_command(cmd)
4648
assert result.returncode == 0
49+
set_driver_in_scenario_molecule_yml(scenario_directory, "containers")
4750

4851
assert os.path.isdir(scenario_directory)
4952

test/docker/test_func.py

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,29 @@
88

99
import pytest
1010

11-
from conftest import change_dir_to
11+
from conftest import change_dir_to, set_driver_in_scenario_molecule_yml
1212
from molecule import logger
1313
from molecule.app import get_app
1414

1515
LOG = logger.get_logger(__name__)
1616

17+
# Root of the test/docker tree (for scenario paths)
18+
TEST_DOCKER_DIR = pathlib.Path(__file__).resolve().parent
19+
20+
21+
def is_docker_available() -> bool:
22+
"""Return True if Docker daemon is reachable (e.g. docker info succeeds)."""
23+
try:
24+
r = subprocess.run(
25+
["docker", "info"],
26+
capture_output=True,
27+
timeout=10,
28+
check=False,
29+
)
30+
return r.returncode == 0
31+
except (FileNotFoundError, subprocess.TimeoutExpired):
32+
return False
33+
1734

1835
def format_result(result: subprocess.CompletedProcess):
1936
"""Return friendly representation of completed process run."""
@@ -24,7 +41,10 @@ def format_result(result: subprocess.CompletedProcess):
2441
)
2542

2643

27-
@pytest.mark.skip(reason="broken, fix welcomed")
44+
@pytest.mark.skipif(
45+
not is_docker_available(),
46+
reason="Docker not available or daemon not reachable",
47+
)
2848
def test_command_init_and_test_scenario(tmp_path: pathlib.Path, DRIVER: str) -> None:
2949
"""Verify that init scenario works."""
3050
shutil.rmtree(tmp_path, ignore_errors=True)
@@ -38,11 +58,11 @@ def test_command_init_and_test_scenario(tmp_path: pathlib.Path, DRIVER: str) ->
3858
"molecule",
3959
"init",
4060
"scenario",
41-
"--driver-name",
42-
DRIVER,
61+
scenario_name,
4362
]
4463
result = get_app(tmp_path).run_command(cmd)
4564
assert result.returncode == 0
65+
set_driver_in_scenario_molecule_yml(str(scenario_directory), DRIVER)
4666

4767
assert scenario_directory.exists()
4868

@@ -63,29 +83,41 @@ def test_command_init_and_test_scenario(tmp_path: pathlib.Path, DRIVER: str) ->
6383
assert result.returncode == 0
6484

6585

66-
@pytest.mark.skip(reason="broken, fix welcomed")
86+
@pytest.mark.skipif(
87+
not is_docker_available(),
88+
reason="Docker not available or daemon not reachable",
89+
)
6790
def test_command_static_scenario() -> None:
6891
"""Validate that the scenario we included with code still works."""
69-
cmd = ["molecule", "test"]
70-
71-
result = get_app(Path()).run_command(cmd)
72-
assert result.returncode == 0
92+
scenario_dir = TEST_DOCKER_DIR / "scenarios" / "with-context"
93+
with change_dir_to(str(scenario_dir)):
94+
result = get_app(Path()).run_command(["molecule", "test"])
95+
assert result.returncode == 0
7396

7497

75-
@pytest.mark.skip(reason="broken, fix welcomed")
98+
@pytest.mark.skipif(
99+
not is_docker_available(),
100+
reason="Docker not available or daemon not reachable",
101+
)
76102
def test_dockerfile_with_context() -> None:
77103
"""Verify that Dockerfile.j2 with context works."""
78-
with change_dir_to("test/docker/scenarios/with-context"):
79-
cmd = ["molecule", "--debug", "test"]
80-
result = get_app(Path()).run_command(cmd)
104+
scenario_dir = TEST_DOCKER_DIR / "scenarios" / "with-context"
105+
with change_dir_to(str(scenario_dir)):
106+
result = get_app(Path()).run_command(["molecule", "--debug", "test"])
81107
assert result.returncode == 0
82108

83109

84-
@pytest.mark.skip(reason="broken, fix welcomed")
110+
@pytest.mark.skipif(
111+
not is_docker_available(),
112+
reason="Docker not available or daemon not reachable",
113+
)
85114
def test_env_substitution() -> None:
86115
"""Verify that env variables in molecule.yml are replaced properly."""
87116
os.environ["MOLECULE_ROLE_IMAGE"] = "debian:bullseye"
88-
with change_dir_to("test/docker/scenarios/env-substitution"):
89-
cmd = ["molecule", "--debug", "test"]
90-
result = get_app(Path()).run_command(cmd)
91-
assert result.returncode == 0
117+
try:
118+
scenario_dir = TEST_DOCKER_DIR / "scenarios" / "env-substitution"
119+
with change_dir_to(str(scenario_dir)):
120+
result = get_app(Path()).run_command(["molecule", "--debug", "test"])
121+
assert result.returncode == 0
122+
finally:
123+
os.environ.pop("MOLECULE_ROLE_IMAGE", None)

test/ec2/functional/test_ec2.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,37 @@
2424

2525
import pytest
2626

27-
from conftest import change_dir_to, metadata_lint_update
27+
from conftest import (
28+
change_dir_to,
29+
set_driver_in_scenario_molecule_yml,
30+
)
2831
from molecule import logger
2932
from molecule.app import get_app
3033

3134
LOG = logger.get_logger(__name__)
3235

3336

34-
@pytest.mark.xfail(reason="need to fix template path")
3537
def test_ec2_command_init_scenario(temp_dir):
38+
"""Verify init scenario with ec2 driver; run molecule test only with AWS creds."""
3639
role_directory = os.path.join(temp_dir.strpath, "test-init")
37-
cmd = ["molecule", "init", "role", "test-init"]
40+
# molecule init role was removed in molecule 25.x; use ansible-galaxy like Azure test
41+
cmd = ["ansible-galaxy", "role", "init", "test-init"]
3842
assert get_app(Path()).run_command(cmd).returncode == 0
39-
metadata_lint_update(role_directory)
4043

4144
with change_dir_to(role_directory):
4245
molecule_directory = pytest.helpers.molecule_directory()
4346
scenario_directory = os.path.join(molecule_directory, "test-scenario")
44-
cmd = [
45-
"molecule",
46-
"init",
47-
"scenario",
48-
"test-scenario",
49-
"--role_name=test-init",
50-
"--driver-name=ec2",
51-
]
47+
cmd = ["molecule", "init", "scenario", "test-scenario"]
5248
assert get_app(Path()).run_command(cmd).returncode == 0
49+
set_driver_in_scenario_molecule_yml(scenario_directory, "ec2")
5350

5451
assert os.path.isdir(scenario_directory)
55-
os.unlink(os.path.join(scenario_directory, "create.yml"))
56-
os.unlink(os.path.join(scenario_directory, "destroy.yml"))
57-
58-
cmd = ["molecule", "test", "-s", "test-scenario"]
59-
assert get_app(Path()).run_command(cmd).returncode == 0
52+
has_creds = "AWS_ACCESS_KEY_ID" in os.environ or "AWS_PROFILE" in os.environ
53+
if not has_creds:
54+
os.unlink(os.path.join(scenario_directory, "create.yml"))
55+
os.unlink(os.path.join(scenario_directory, "destroy.yml"))
56+
57+
# Run full molecule test only when AWS credentials are available
58+
if has_creds:
59+
cmd = ["molecule", "test", "-s", "test-scenario"]
60+
assert get_app(Path()).run_command(cmd).returncode == 0

test/gce/functional/test_func.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,38 @@
2424

2525
import pytest
2626

27-
from conftest import change_dir_to, metadata_lint_update
27+
from conftest import (
28+
change_dir_to,
29+
set_driver_in_scenario_molecule_yml,
30+
)
2831
from molecule import logger
2932
from molecule.app import get_app
3033

3134
LOG = logger.get_logger(__name__)
3235
driver_name = __name__.split(".")[0].split("_")[-1]
3336

3437

35-
@pytest.mark.xfail(reason="need to fix template path")
3638
def test_gce_command_init_scenario(temp_dir):
37-
"""Test init scenario with driver."""
39+
"""Test init scenario with driver; run molecule test only with GCE creds."""
3840
role_directory = os.path.join(temp_dir.strpath, "test-init")
39-
cmd = ["molecule", "init", "role", "test-init"]
41+
# molecule init role was removed in molecule 25.x; use ansible-galaxy like Azure test
42+
cmd = ["ansible-galaxy", "role", "init", "test-init"]
4043
assert get_app(Path()).run_command(cmd).returncode == 0
41-
metadata_lint_update(role_directory)
4244

4345
with change_dir_to(role_directory):
4446
molecule_directory = pytest.helpers.molecule_directory()
4547
scenario_directory = os.path.join(molecule_directory, "test-scenario")
46-
cmd = [
47-
"molecule",
48-
"init",
49-
"scenario",
50-
"test-scenario",
51-
"--role-name",
52-
"test-init",
53-
"--driver-name",
54-
driver_name,
55-
]
48+
cmd = ["molecule", "init", "scenario", "test-scenario"]
5649
assert get_app(Path()).run_command(cmd).returncode == 0
50+
set_driver_in_scenario_molecule_yml(scenario_directory, driver_name)
5751

5852
assert os.path.isdir(scenario_directory)
59-
os.unlink(os.path.join(scenario_directory, "create.yml"))
60-
os.unlink(os.path.join(scenario_directory, "destroy.yml"))
61-
62-
cmd = ["molecule", "test", "-s", "test-scenario"]
63-
assert get_app(Path()).run_command(cmd).returncode == 0
53+
has_creds = "GOOGLE_APPLICATION_CREDENTIALS" in os.environ
54+
if not has_creds:
55+
os.unlink(os.path.join(scenario_directory, "create.yml"))
56+
os.unlink(os.path.join(scenario_directory, "destroy.yml"))
57+
58+
# Run full molecule test only when GCE credentials are available
59+
if has_creds:
60+
cmd = ["molecule", "test", "-s", "test-scenario"]
61+
assert get_app(Path()).run_command(cmd).returncode == 0

0 commit comments

Comments
 (0)