88
99import pytest
1010
11- from conftest import change_dir_to
11+ from conftest import change_dir_to , set_driver_in_scenario_molecule_yml
1212from molecule import logger
1313from molecule .app import get_app
1414
1515LOG = 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
1835def 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+ )
2848def 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+ )
6790def 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+ )
76102def 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+ )
85114def 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 )
0 commit comments