diff --git a/src/molecule/provisioner/ansible_playbook.py b/src/molecule/provisioner/ansible_playbook.py index aec4ec417..af20f8e63 100644 --- a/src/molecule/provisioner/ansible_playbook.py +++ b/src/molecule/provisioner/ansible_playbook.py @@ -20,6 +20,7 @@ """Ansible-Playbook Provisioner Module.""" import logging +import shlex import warnings from molecule import util @@ -116,8 +117,10 @@ def execute(self, action_args=None): result = util.run_command(self._ansible_command, debug=self._config.debug) if result.returncode != 0: + from rich.markup import escape + util.sysexit_with_message( - f"Ansible return code was {result.returncode}, command was: {result.args}", + f"Ansible return code was {result.returncode}, command was: [dim]{escape(shlex.join(result.args))}[/dim]", result.returncode, warns=warns, ) diff --git a/src/molecule/test/unit/command/init/test_role.py b/src/molecule/test/unit/command/init/test_role.py index fc6baa3a0..825fbf870 100644 --- a/src/molecule/test/unit/command/init/test_role.py +++ b/src/molecule/test/unit/command/init/test_role.py @@ -57,7 +57,7 @@ def test_execute(temp_dir, _instance, patched_logger_info): patched_logger_info.assert_any_call(msg) -def test_execute_role_exists(temp_dir, _instance, patched_logger_critical): +def test_execute_role_exists(temp_dir, _instance, caplog): _instance.execute() with pytest.raises(SystemExit) as e: @@ -66,4 +66,4 @@ def test_execute_role_exists(temp_dir, _instance, patched_logger_critical): assert e.value.code == 1 msg = "The directory test_role exists. Cannot create new role." - patched_logger_critical.assert_called_once_with(msg) + assert msg in caplog.text diff --git a/src/molecule/test/unit/command/init/test_scenario.py b/src/molecule/test/unit/command/init/test_scenario.py index a88e47f09..a654dcfd3 100644 --- a/src/molecule/test/unit/command/init/test_scenario.py +++ b/src/molecule/test/unit/command/init/test_scenario.py @@ -63,7 +63,7 @@ def test_execute(temp_dir, _instance, patched_logger_info): patched_logger_info.assert_any_call(msg) -def test_execute_scenario_exists(temp_dir, _instance, patched_logger_critical): +def test_execute_scenario_exists(temp_dir, _instance, caplog): _instance.execute() with pytest.raises(SystemExit) as e: @@ -72,14 +72,14 @@ def test_execute_scenario_exists(temp_dir, _instance, patched_logger_critical): assert e.value.code == 1 msg = "The directory molecule/test-scenario exists. Cannot create new scenario." - patched_logger_critical.assert_called_once_with(msg) + assert msg in caplog.text def test_execute_with_invalid_driver( temp_dir, _instance, _command_args, - patched_logger_critical, + caplog, ): _command_args["driver_name"] = "ec3" diff --git a/src/molecule/test/unit/command/test_base.py b/src/molecule/test/unit/command/test_base.py index de2e8c38a..a0c58c655 100644 --- a/src/molecule/test/unit/command/test_base.py +++ b/src/molecule/test/unit/command/test_base.py @@ -303,18 +303,18 @@ def test_verify_configs(config_instance): assert base._verify_configs(configs) is None -def test_verify_configs_raises_with_no_configs(patched_logger_critical): +def test_verify_configs_raises_with_no_configs(caplog): with pytest.raises(SystemExit) as e: base._verify_configs([]) assert e.value.code == 1 msg = "'molecule/*/molecule.yml' glob failed. Exiting." - patched_logger_critical.assert_called_once_with(msg) + assert msg in caplog.text def test_verify_configs_raises_with_duplicate_configs( - patched_logger_critical, + caplog, config_instance, ): with pytest.raises(SystemExit) as e: @@ -324,7 +324,7 @@ def test_verify_configs_raises_with_duplicate_configs( assert e.value.code == 1 msg = "Duplicate scenario name 'default' found. Exiting." - patched_logger_critical.assert_called_once_with(msg) + assert msg in caplog.text def test_get_subcommand(): diff --git a/src/molecule/test/unit/command/test_check.py b/src/molecule/test/unit/command/test_check.py index ffdf50c1a..b8d1034f2 100644 --- a/src/molecule/test/unit/command/test_check.py +++ b/src/molecule/test/unit/command/test_check.py @@ -33,7 +33,7 @@ def _patched_ansible_check(mocker): # throughout patched.assert_called unit tests. def test_execute( mocker, - patched_logger_info, + caplog, _patched_ansible_check, patched_config_validate, config_instance, @@ -41,7 +41,5 @@ def test_execute( c = check.Check(config_instance) c.execute() - assert len(patched_logger_info.mock_calls) == 1 - name, args, kwargs = patched_logger_info.mock_calls[0] - assert "default" in args - assert "check" in args + assert "default" in caplog.text + assert "check" in caplog.text diff --git a/src/molecule/test/unit/command/test_cleanup.py b/src/molecule/test/unit/command/test_cleanup.py index 0fdf2be1a..9c9a584c0 100644 --- a/src/molecule/test/unit/command/test_cleanup.py +++ b/src/molecule/test/unit/command/test_cleanup.py @@ -47,7 +47,7 @@ def _patched_ansible_cleanup(mocker): def test_execute( mocker, _patched_ansible_cleanup, - patched_logger_info, + caplog, patched_config_validate, config_instance, ): @@ -57,15 +57,13 @@ def test_execute( cu = cleanup.Cleanup(config_instance) cu.execute() - assert len(patched_logger_info.mock_calls) == 1 - name, args, kwargs = patched_logger_info.mock_calls[0] - assert "cleanup" in args + assert "cleanup" in caplog.text _patched_ansible_cleanup.assert_called_once_with() def test_execute_skips_when_playbook_not_configured( - patched_logger_warning, + caplog, _patched_ansible_cleanup, config_instance, ): @@ -73,6 +71,6 @@ def test_execute_skips_when_playbook_not_configured( cu.execute() msg = "Skipping, cleanup playbook not configured." - patched_logger_warning.assert_called_once_with(msg) + assert msg in caplog.text assert not _patched_ansible_cleanup.called diff --git a/src/molecule/test/unit/command/test_converge.py b/src/molecule/test/unit/command/test_converge.py index c9b873af4..f34b1d966 100644 --- a/src/molecule/test/unit/command/test_converge.py +++ b/src/molecule/test/unit/command/test_converge.py @@ -29,7 +29,7 @@ # throughout patched.assert_called unit tests. def test_execute( mocker, - patched_logger_info, + caplog, patched_ansible_converge, patched_config_validate, config_instance, @@ -37,10 +37,8 @@ def test_execute( c = converge.Converge(config_instance) c.execute() - assert len(patched_logger_info.mock_calls) == 1 - name, args, kwargs = patched_logger_info.mock_calls[0] - assert "default" in args - assert "converge" in args + assert "default" in caplog.text + assert "converge" in caplog.text patched_ansible_converge.assert_called_once_with() diff --git a/src/molecule/test/unit/command/test_create.py b/src/molecule/test/unit/command/test_create.py index 01ba4fe73..5f4bbefc4 100644 --- a/src/molecule/test/unit/command/test_create.py +++ b/src/molecule/test/unit/command/test_create.py @@ -18,6 +18,7 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. + import pytest from molecule.command import create @@ -34,7 +35,7 @@ def _patched_create_setup(mocker): @pytest.mark.skip(reason="create not running for delegated") def test_execute( mocker, - patched_logger_info, + caplog, command_patched_ansible_create, patched_config_validate, config_instance, @@ -42,10 +43,8 @@ def test_execute( c = create.Create(config_instance) c.execute() - assert len(patched_logger_info.mock_calls) == 1 - name, args, kwargs = patched_logger_info.mock_calls[0] - assert "default" in args - assert "converge" in args + assert "default" in caplog.text + assert "converge" in caplog.text assert config_instance.state.driver == "delegated" @@ -61,7 +60,7 @@ def test_execute( ) def test_execute_skips_when_delegated_driver( _patched_create_setup, - patched_logger_warning, + caplog, command_patched_ansible_create, config_instance, ): @@ -69,14 +68,14 @@ def test_execute_skips_when_delegated_driver( c.execute() msg = "Skipping, instances are delegated." - patched_logger_warning.assert_called_once_with(msg) + assert msg in caplog.text assert not command_patched_ansible_create.called @pytest.mark.skip(reason="create not running for delegated") def test_execute_skips_when_instances_already_created( - patched_logger_warning, + caplog, command_patched_ansible_create, config_instance, ): @@ -85,6 +84,6 @@ def test_execute_skips_when_instances_already_created( c.execute() msg = "Skipping, instances already created." - patched_logger_warning.assert_called_once_with(msg) + assert msg in caplog.text assert not command_patched_ansible_create.called diff --git a/src/molecule/test/unit/command/test_dependency.py b/src/molecule/test/unit/command/test_dependency.py index d5c92719c..a4920e1d3 100644 --- a/src/molecule/test/unit/command/test_dependency.py +++ b/src/molecule/test/unit/command/test_dependency.py @@ -26,7 +26,7 @@ # throughout patched.assert_called unit tests. def test_execute( mocker, - patched_logger_info, + caplog, patched_ansible_galaxy, patched_config_validate, config_instance, @@ -36,7 +36,5 @@ def test_execute( patched_ansible_galaxy.assert_called_once_with() - assert len(patched_logger_info.mock_calls) == 1 - name, args, kwargs = patched_logger_info.mock_calls[0] - assert "default" in args - assert "dependency" in args + assert "default" in caplog.text + assert "dependency" in caplog.text diff --git a/src/molecule/test/unit/command/test_destroy.py b/src/molecule/test/unit/command/test_destroy.py index f4753a11b..c507b156a 100644 --- a/src/molecule/test/unit/command/test_destroy.py +++ b/src/molecule/test/unit/command/test_destroy.py @@ -18,6 +18,7 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. + import pytest from molecule.command import destroy @@ -39,7 +40,7 @@ def _patched_destroy_setup(mocker): @pytest.mark.skip(reason="destroy not running for delegated") def test_execute( mocker, - patched_logger_info, + caplog, patched_config_validate, _patched_ansible_destroy, config_instance, @@ -47,11 +48,9 @@ def test_execute( d = destroy.Destroy(config_instance) d.execute() - assert len(patched_logger_info.mock_calls) == 1 - name, args, kwargs = patched_logger_info.mock_calls[0] - assert "destroy" in args + assert "destroy" in caplog.text - assert "verify" in args + assert "verify" in caplog.text _patched_ansible_destroy.assert_called_once_with() @@ -66,7 +65,7 @@ def test_execute( ) def test_execute_skips_when_destroy_strategy_is_never( _patched_destroy_setup, - patched_logger_warning, + caplog, _patched_ansible_destroy, config_instance, ): @@ -76,7 +75,7 @@ def test_execute_skips_when_destroy_strategy_is_never( d.execute() msg = "Skipping, '--destroy=never' requested." - patched_logger_warning.assert_called_once_with(msg) + assert msg in caplog.text assert not _patched_ansible_destroy.called @@ -88,7 +87,7 @@ def test_execute_skips_when_destroy_strategy_is_never( ) def test_execute_skips_when_delegated_driver( _patched_destroy_setup, - patched_logger_warning, + caplog, _patched_ansible_destroy, config_instance, ): @@ -96,6 +95,6 @@ def test_execute_skips_when_delegated_driver( d.execute() msg = "Skipping, instances are delegated." - patched_logger_warning.assert_called_once_with(msg) + assert msg in caplog.text assert not _patched_ansible_destroy.called diff --git a/src/molecule/test/unit/command/test_idempotence.py b/src/molecule/test/unit/command/test_idempotence.py index fe12be56a..c33154da7 100644 --- a/src/molecule/test/unit/command/test_idempotence.py +++ b/src/molecule/test/unit/command/test_idempotence.py @@ -17,6 +17,7 @@ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. + import pytest from molecule.command import idempotence @@ -39,28 +40,26 @@ def _instance(patched_config_validate, config_instance): def test_execute( mocker, - patched_logger_info, + caplog, patched_ansible_converge, _patched_is_idempotent, _instance, ): _instance.execute() - assert len(patched_logger_info.mock_calls) == 2 - name, args, kwargs = patched_logger_info.mock_calls[0] - assert "default" in args - assert "idempotence" in args + assert "default" in caplog.text + assert "idempotence" in caplog.text patched_ansible_converge.assert_called_once_with() _patched_is_idempotent.assert_called_once_with("patched-ansible-converge-stdout") msg = "Idempotence completed successfully." - patched_logger_info.assert_any_call(msg) + assert msg in caplog.text def test_execute_raises_when_not_converged( - patched_logger_critical, + caplog, patched_ansible_converge, _instance, ): @@ -71,12 +70,12 @@ def test_execute_raises_when_not_converged( assert e.value.code == 1 msg = "Instances not converged. Please converge instances first." - patched_logger_critical.assert_called_once_with(msg) + assert msg in caplog.text def test_execute_raises_when_fails_idempotence( mocker, - patched_logger_critical, + caplog, patched_ansible_converge, _patched_is_idempotent, _instance, @@ -88,7 +87,7 @@ def test_execute_raises_when_fails_idempotence( assert e.value.code == 1 msg = "Idempotence test failed because of the following tasks:\n" - patched_logger_critical.assert_called_once_with(msg) + assert msg in caplog.text def test_is_idempotent(_instance): diff --git a/src/molecule/test/unit/command/test_login.py b/src/molecule/test/unit/command/test_login.py index 1bb5a3d6f..7c5b7353a 100644 --- a/src/molecule/test/unit/command/test_login.py +++ b/src/molecule/test/unit/command/test_login.py @@ -18,6 +18,7 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. + import pytest from molecule.command import login @@ -44,7 +45,7 @@ def test_execute(mocker, _instance): ["command_driver_delegated_managed_section_data"], indirect=True, ) -def test_execute_raises_when_not_created(patched_logger_critical, _instance): +def test_execute_raises_when_not_created(caplog, _instance): _instance._config.state.change_state("created", False) with pytest.raises(SystemExit) as e: @@ -53,10 +54,10 @@ def test_execute_raises_when_not_created(patched_logger_critical, _instance): assert e.value.code == 1 msg = "Instances not created. Please create instances first." - patched_logger_critical.assert_called_once_with(msg) + assert msg in caplog.text -def test_get_hostname_does_not_match(patched_logger_critical, _instance): +def test_get_hostname_does_not_match(caplog, _instance): _instance._config.command_args = {"host": "invalid"} hosts = ["instance-1"] with pytest.raises(SystemExit) as e: @@ -68,7 +69,7 @@ def test_get_hostname_does_not_match(patched_logger_critical, _instance): "There are no hosts that match 'invalid'. You " "can only login to valid hosts." ) - patched_logger_critical.assert_called_once_with(msg) + assert msg in caplog.text def test_get_hostname_exact_match_with_one_host(_instance): @@ -100,7 +101,7 @@ def test_get_hostname_partial_match_with_multiple_hosts(_instance): def test_get_hostname_partial_match_with_multiple_hosts_raises( - patched_logger_critical, + caplog, _instance, ): _instance._config.command_args = {"host": "inst"} @@ -117,7 +118,7 @@ def test_get_hostname_partial_match_with_multiple_hosts_raises( "instance-1\n" "instance-2" ) - patched_logger_critical.assert_called_once_with(msg) + assert msg in caplog.text def test_get_hostname_no_host_flag_specified_on_cli(_instance): @@ -129,7 +130,7 @@ def test_get_hostname_no_host_flag_specified_on_cli(_instance): def test_get_hostname_no_host_flag_specified_on_cli_with_multiple_hosts_raises( - patched_logger_critical, + caplog, _instance, ): _instance._config.command_args = {} @@ -146,4 +147,4 @@ def test_get_hostname_no_host_flag_specified_on_cli_with_multiple_hosts_raises( "instance-1\n" "instance-2" ) - patched_logger_critical.assert_called_once_with(msg) + assert msg in caplog.text diff --git a/src/molecule/test/unit/command/test_prepare.py b/src/molecule/test/unit/command/test_prepare.py index 64d047d4c..0bd377a88 100644 --- a/src/molecule/test/unit/command/test_prepare.py +++ b/src/molecule/test/unit/command/test_prepare.py @@ -36,7 +36,7 @@ def _patched_ansible_prepare(mocker): # throughout patched.assert_called unit tests. def test_execute( mocker, - patched_logger_info, + caplog, _patched_ansible_prepare, patched_config_validate, config_instance, @@ -47,10 +47,8 @@ def test_execute( p = prepare.Prepare(config_instance) p.execute() - assert len(patched_logger_info.mock_calls) == 1 - name, args, kwargs = patched_logger_info.mock_calls[0] - assert "default" in args - assert "prepare" in args + assert "default" in caplog.text + assert "prepare" in caplog.text _patched_ansible_prepare.assert_called_once_with() @@ -58,7 +56,7 @@ def test_execute( def test_execute_skips_when_instances_already_prepared( - patched_logger_warning, + caplog, _patched_ansible_prepare, config_instance, ): @@ -67,13 +65,13 @@ def test_execute_skips_when_instances_already_prepared( p.execute() msg = "Skipping, instances already prepared." - patched_logger_warning.assert_called_once_with(msg) + assert msg in caplog.text assert not _patched_ansible_prepare.called def test_execute_skips_when_playbook_not_configured( - patched_logger_warning, + caplog, _patched_ansible_prepare, config_instance, ): @@ -81,14 +79,14 @@ def test_execute_skips_when_playbook_not_configured( p.execute() msg = "Skipping, prepare playbook not configured." - patched_logger_warning.assert_called_once_with(msg) + assert msg in caplog.text assert not _patched_ansible_prepare.called def test_execute_when_instances_already_prepared_but_force_provided( mocker, - patched_logger_warning, + caplog, _patched_ansible_prepare, config_instance, ): diff --git a/src/molecule/test/unit/command/test_side_effect.py b/src/molecule/test/unit/command/test_side_effect.py index fbd182bef..777c11ce9 100644 --- a/src/molecule/test/unit/command/test_side_effect.py +++ b/src/molecule/test/unit/command/test_side_effect.py @@ -52,7 +52,7 @@ def _patched_ansible_side_effect(mocker): def test_execute( mocker, _patched_ansible_side_effect, - patched_logger_info, + caplog, patched_config_validate, config_instance, ): @@ -62,16 +62,14 @@ def test_execute( se = side_effect.SideEffect(config_instance) se.execute() - assert len(patched_logger_info.mock_calls) == 1 - name, args, kwargs = patched_logger_info.mock_calls[0] - assert "default" in args - assert "side_effect" in args + assert "default" in caplog.text + assert "side_effect" in caplog.text _patched_ansible_side_effect.assert_called_once_with(None) def test_execute_skips_when_playbook_not_configured( - patched_logger_warning, + caplog, _patched_ansible_side_effect, config_instance, ): @@ -79,6 +77,6 @@ def test_execute_skips_when_playbook_not_configured( se.execute() msg = "Skipping, side effect playbook not configured." - patched_logger_warning.assert_called_once_with(msg) + assert msg in caplog.text assert not _patched_ansible_side_effect.called diff --git a/src/molecule/test/unit/command/test_syntax.py b/src/molecule/test/unit/command/test_syntax.py index 8ca03f1ef..21e5c930d 100644 --- a/src/molecule/test/unit/command/test_syntax.py +++ b/src/molecule/test/unit/command/test_syntax.py @@ -33,7 +33,7 @@ def _patched_ansible_syntax(mocker): # throughout patched.assert_called unit tests. def test_execute( mocker, - patched_logger_info, + caplog, _patched_ansible_syntax, patched_config_validate, config_instance, @@ -41,9 +41,7 @@ def test_execute( s = syntax.Syntax(config_instance) s.execute() - assert len(patched_logger_info.mock_calls) == 1 - name, args, kwargs = patched_logger_info.mock_calls[0] - assert "default" in args - assert "syntax" in args + assert "default" in caplog.text + assert "syntax" in caplog.text _patched_ansible_syntax.assert_called_once_with() diff --git a/src/molecule/test/unit/command/test_verify.py b/src/molecule/test/unit/command/test_verify.py index e950ebfc3..b42d4de04 100644 --- a/src/molecule/test/unit/command/test_verify.py +++ b/src/molecule/test/unit/command/test_verify.py @@ -26,7 +26,7 @@ # throughout patched.assert_called unit tests. def test_execute( mocker, - patched_logger_info, + caplog, patched_default_verifier, patched_config_validate, config_instance, @@ -34,7 +34,5 @@ def test_execute( v = verify.Verify(config_instance) v.execute() - assert len(patched_logger_info.mock_calls) == 1 - name, args, kwargs = patched_logger_info.mock_calls[0] - assert "default" in args - assert "verify" in args + assert "default" in caplog.text + assert "verify" in caplog.text diff --git a/src/molecule/test/unit/conftest.py b/src/molecule/test/unit/conftest.py index a7bec9b15..a79204710 100644 --- a/src/molecule/test/unit/conftest.py +++ b/src/molecule/test/unit/conftest.py @@ -184,21 +184,11 @@ def patched_logger_debug(mocker): return mocker.patch("logging.Logger.debug") -@pytest.fixture() -def patched_logger_warning(mocker): - return mocker.patch("logging.Logger.warning") - - @pytest.fixture() def patched_logger_error(mocker): return mocker.patch("logging.Logger.error") -@pytest.fixture() -def patched_logger_critical(mocker): - return mocker.patch("logging.Logger.critical") - - @pytest.fixture() def patched_run_command(mocker): m = mocker.patch("molecule.util.run_command") diff --git a/src/molecule/test/unit/dependency/ansible_galaxy/test_collections.py b/src/molecule/test/unit/dependency/ansible_galaxy/test_collections.py index 950c8ebcf..c000f3520 100644 --- a/src/molecule/test/unit/dependency/ansible_galaxy/test_collections.py +++ b/src/molecule/test/unit/dependency/ansible_galaxy/test_collections.py @@ -147,7 +147,7 @@ def test_collections_bake(_instance, role_file, roles_path): def test_execute( patched_run_command, _patched_ansible_galaxy_has_requirements_file, - patched_logger_info, + caplog, _instance, ): _instance._sh_command = "patched-command" @@ -166,12 +166,12 @@ def test_execute( ) msg = "Dependency completed successfully." - patched_logger_info.assert_called_once_with(msg) + assert msg in caplog.text def test_execute_does_not_execute_when_disabled( patched_run_command, - patched_logger_warning, + caplog, _instance, ): _instance._config.config["dependency"]["enabled"] = False @@ -180,13 +180,13 @@ def test_execute_does_not_execute_when_disabled( assert not patched_run_command.called msg = "Skipping, dependency is disabled." - patched_logger_warning.assert_called_once_with(msg) + assert msg in caplog.text def test_execute_does_not_execute_when_no_requirements_file( patched_run_command, _patched_ansible_galaxy_has_requirements_file, - patched_logger_warning, + caplog, _instance, ): _patched_ansible_galaxy_has_requirements_file.return_value = False @@ -195,7 +195,7 @@ def test_execute_does_not_execute_when_no_requirements_file( assert not patched_run_command.called msg = "Skipping, missing the requirements file." - patched_logger_warning.assert_called_once_with(msg) + assert msg in caplog.text def test_execute_bakes( diff --git a/src/molecule/test/unit/dependency/ansible_galaxy/test_roles.py b/src/molecule/test/unit/dependency/ansible_galaxy/test_roles.py index 9fa2b2255..2087e9282 100644 --- a/src/molecule/test/unit/dependency/ansible_galaxy/test_roles.py +++ b/src/molecule/test/unit/dependency/ansible_galaxy/test_roles.py @@ -145,7 +145,7 @@ def test_galaxy_bake(_instance, role_file, roles_path): def test_execute( patched_run_command, _patched_ansible_galaxy_has_requirements_file, - patched_logger_info, + caplog, _instance, ): _instance._sh_command = "patched-command" @@ -164,12 +164,12 @@ def test_execute( ) msg = "Dependency completed successfully." - patched_logger_info.assert_called_once_with(msg) + assert msg in caplog.text def test_execute_does_not_execute_when_disabled( patched_run_command, - patched_logger_warning, + caplog, _instance, ): _instance._config.config["dependency"]["enabled"] = False @@ -178,13 +178,13 @@ def test_execute_does_not_execute_when_disabled( assert not patched_run_command.called msg = "Skipping, dependency is disabled." - patched_logger_warning.assert_called_once_with(msg) + assert msg in caplog.text def test_execute_does_not_execute_when_no_requirements_file( patched_run_command, _patched_ansible_galaxy_has_requirements_file, - patched_logger_warning, + caplog, _instance, ): _patched_ansible_galaxy_has_requirements_file.return_value = False @@ -193,7 +193,7 @@ def test_execute_does_not_execute_when_no_requirements_file( assert not patched_run_command.called msg = "Skipping, missing the requirements file." - patched_logger_warning.assert_called_once_with(msg) + assert msg in caplog.text def test_execute_bakes( diff --git a/src/molecule/test/unit/dependency/test_shell.py b/src/molecule/test/unit/dependency/test_shell.py index 33e539d35..945f1b7d3 100644 --- a/src/molecule/test/unit/dependency/test_shell.py +++ b/src/molecule/test/unit/dependency/test_shell.py @@ -18,6 +18,7 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. + import pytest from molecule import config @@ -90,7 +91,7 @@ def test_env_property(_instance): assert _instance.env["FOO"] == "bar" -def test_execute(patched_run_command, patched_logger_info, _instance): +def test_execute(patched_run_command, caplog, _instance): _instance._sh_command = "patched-command" _instance.execute() @@ -101,12 +102,12 @@ def test_execute(patched_run_command, patched_logger_info, _instance): ) msg = "Dependency completed successfully." - patched_logger_info.assert_called_once_with(msg) + assert msg in caplog.text def test_execute_does_not_execute_when_disabled( patched_run_command, - patched_logger_warning, + caplog, _instance, ): _instance._config.config["dependency"]["enabled"] = False @@ -115,7 +116,7 @@ def test_execute_does_not_execute_when_disabled( assert not patched_run_command.called msg = "Skipping, dependency is disabled." - patched_logger_warning.assert_called_once_with(msg) + assert msg in caplog.text @pytest.mark.parametrize("config_instance", ["_dependency_section_data"], indirect=True) diff --git a/src/molecule/test/unit/provisioner/test_ansible.py b/src/molecule/test/unit/provisioner/test_ansible.py index adab3d807..3a5248e34 100644 --- a/src/molecule/test/unit/provisioner/test_ansible.py +++ b/src/molecule/test/unit/provisioner/test_ansible.py @@ -704,7 +704,7 @@ def test_link_vars(_instance): assert os.path.lexists(target_host_vars) -def test_link_vars_raises_when_source_not_found(_instance, patched_logger_critical): +def test_link_vars_raises_when_source_not_found(_instance, caplog): c = _instance._config.config c["provisioner"]["inventory"]["links"] = {"foo": "../bar"} @@ -715,7 +715,7 @@ def test_link_vars_raises_when_source_not_found(_instance, patched_logger_critic source = os.path.join(_instance._config.scenario.directory, os.path.pardir, "bar") msg = f"The source path '{source}' does not exist." - patched_logger_critical.assert_called_once_with(msg) + assert msg in caplog.text def test_verify_inventory(_instance): @@ -724,7 +724,7 @@ def test_verify_inventory(_instance): def test_verify_inventory_raises_when_missing_hosts( temp_dir, - patched_logger_critical, + caplog, _instance, ): _instance._config.config["platforms"] = [] @@ -734,7 +734,7 @@ def test_verify_inventory_raises_when_missing_hosts( assert e.value.code == 1 msg = "Instances missing from the 'platform' section of molecule.yml." - patched_logger_critical.assert_called_once_with(msg) + assert msg in caplog.text def test_vivify(_instance): diff --git a/src/molecule/test/unit/provisioner/test_ansible_playbook.py b/src/molecule/test/unit/provisioner/test_ansible_playbook.py index e107908fc..cd9d7690b 100644 --- a/src/molecule/test/unit/provisioner/test_ansible_playbook.py +++ b/src/molecule/test/unit/provisioner/test_ansible_playbook.py @@ -241,7 +241,6 @@ def test_execute_bakes_with_ansible_args( def test_executes_catches_and_exits_return_code( patched_run_command, - patched_logger_critical, _instance, ): patched_run_command.side_effect = [ diff --git a/src/molecule/test/unit/test_config.py b/src/molecule/test/unit/test_config.py index 7fbf53dfc..658bf01a9 100644 --- a/src/molecule/test/unit/test_config.py +++ b/src/molecule/test/unit/test_config.py @@ -168,7 +168,7 @@ def test_get_driver_name(config_instance): def test_get_driver_name_raises_when_different_driver_used( - patched_logger_critical, + caplog, config_instance, ): config_instance.state.change_state("driver", "foo") @@ -183,7 +183,7 @@ def test_get_driver_name_raises_when_different_driver_used( "but the subcommand is using 'bar' driver." ) - patched_logger_critical.assert_called_once_with(msg) + assert msg in caplog.text def test_get_config(config_instance): @@ -215,42 +215,42 @@ def test_reget_config(config_instance): assert isinstance(config_instance._reget_config(), dict) -def test_interpolate(patched_logger_critical, config_instance): +def test_interpolate(config_instance): string = "foo: $HOME" x = f"foo: {os.environ['HOME']}" assert x == config_instance._interpolate(string, os.environ, None) -def test_interpolate_curly(patched_logger_critical, config_instance): +def test_interpolate_curly(config_instance): string = "foo: ${HOME}" x = f"foo: {os.environ['HOME']}" assert x == config_instance._interpolate(string, os.environ, None) -def test_interpolate_default(patched_logger_critical, config_instance): +def test_interpolate_default(config_instance): string = "foo: ${NONE-default}" x = "foo: default" assert x == config_instance._interpolate(string, os.environ, None) -def test_interpolate_default_colon(patched_logger_critical, config_instance): +def test_interpolate_default_colon(config_instance): string = "foo: ${NONE:-default}" x = "foo: default" assert x == config_instance._interpolate(string, os.environ, None) -def test_interpolate_default_variable(patched_logger_critical, config_instance): +def test_interpolate_default_variable(config_instance): string = "foo: ${NONE:-$HOME}" x = f"foo: {os.environ['HOME']}" assert x == config_instance._interpolate(string, os.environ, None) -def test_interpolate_curly_default_variable(patched_logger_critical, config_instance): +def test_interpolate_curly_default_variable(config_instance): string = "foo: ${NONE-$HOME}" x = f"foo: {os.environ['HOME']}" @@ -258,7 +258,7 @@ def test_interpolate_curly_default_variable(patched_logger_critical, config_inst def test_interpolate_raises_on_failed_interpolation( - patched_logger_critical, + caplog, config_instance, ): string = "$6$8I5Cfmpr$kGZB" @@ -273,7 +273,7 @@ def test_interpolate_raises_on_failed_interpolation( "Invalid placeholder in string: line 1, col 1\n" "$6$8I5Cfmpr$kGZB" ) - patched_logger_critical.assert_called_once_with(msg) + assert msg in caplog.text def test_get_defaults(config_instance, mocker): @@ -299,7 +299,7 @@ def test_validate(mocker, config_instance, patched_logger_debug): def test_validate_exists_when_validation_fails( mocker, - patched_logger_critical, + caplog, config_instance, ): m = mocker.patch("molecule.model.schema_v3.validate") @@ -311,7 +311,7 @@ def test_validate_exists_when_validation_fails( assert e.value.code == 1 msg = f"Failed to validate {config_instance.molecule_file}\n\nvalidation errors" - patched_logger_critical.assert_called_once_with(msg) + assert msg in caplog.text def test_molecule_directory(): diff --git a/src/molecule/test/unit/test_scenarios.py b/src/molecule/test/unit/test_scenarios.py index 83865add2..8bed23207 100644 --- a/src/molecule/test/unit/test_scenarios.py +++ b/src/molecule/test/unit/test_scenarios.py @@ -115,7 +115,7 @@ def test_verify_does_not_raise_when_found(_instance): assert _instance._verify() is None -def test_verify_raises_when_scenario_not_found(_instance, patched_logger_critical): +def test_verify_raises_when_scenario_not_found(_instance, caplog): _instance._scenario_name = "invalid" with pytest.raises(SystemExit) as e: _instance._verify() @@ -123,7 +123,7 @@ def test_verify_raises_when_scenario_not_found(_instance, patched_logger_critica assert e.value.code == 1 msg = "Scenario 'invalid' not found. Exiting." - patched_logger_critical.assert_called_once_with(msg) + assert msg in caplog.text def test_filter_for_scenario(_instance): diff --git a/src/molecule/test/unit/test_util.py b/src/molecule/test/unit/test_util.py index b0f78d884..4d775b2d1 100644 --- a/src/molecule/test/unit/test_util.py +++ b/src/molecule/test/unit/test_util.py @@ -85,16 +85,16 @@ def test_sysexit_with_custom_code(): assert e.value.code == 2 -def test_sysexit_with_message(patched_logger_critical): +def test_sysexit_with_message(caplog): with pytest.raises(SystemExit) as e: util.sysexit_with_message("foo") assert e.value.code == 1 - patched_logger_critical.assert_called_once_with("foo") + assert "foo" in caplog.text -def test_sysexit_with_warns(patched_logger_critical, patched_logger_warning): +def test_sysexit_with_warns(caplog): with pytest.raises(SystemExit) as e: with warnings.catch_warnings(record=True) as warns: warnings.filterwarnings("default", category=MoleculeRuntimeWarning) @@ -104,17 +104,17 @@ def test_sysexit_with_warns(patched_logger_critical, patched_logger_warning): assert e.value.code == 1 - patched_logger_critical.assert_called_once_with("foo") - patched_logger_warning.assert_called_once_with("xxx") + assert "foo" in caplog.text + assert "xxx" in caplog.text -def test_sysexit_with_message_and_custom_code(patched_logger_critical): +def test_sysexit_with_message_and_custom_code(caplog): with pytest.raises(SystemExit) as e: util.sysexit_with_message("foo", 2) assert e.value.code == 2 - patched_logger_critical.assert_called_once_with("foo") + assert "foo" in caplog.text def test_run_command(): diff --git a/src/molecule/test/unit/verifier/test_ansible.py b/src/molecule/test/unit/verifier/test_ansible.py index 393fe103f..9a48a70da 100644 --- a/src/molecule/test/unit/verifier/test_ansible.py +++ b/src/molecule/test/unit/verifier/test_ansible.py @@ -76,21 +76,21 @@ def test_options_property_handles_cli_args(_instance): assert x == _instance.options -def test_execute(patched_logger_info, _patched_ansible_verify, _instance): +def test_execute(caplog, _patched_ansible_verify, _instance): _instance.execute() _patched_ansible_verify.assert_called_once_with(None) msg = "Running Ansible Verifier" - patched_logger_info.assert_any_call(msg) + assert msg in caplog.text msg = "Verifier completed successfully." - patched_logger_info.assert_any_call(msg) + assert msg in caplog.text def test_execute_does_not_execute( patched_ansible_converge, - patched_logger_warning, + caplog, _instance, ): _instance._config.config["verifier"]["enabled"] = False @@ -99,4 +99,4 @@ def test_execute_does_not_execute( assert not patched_ansible_converge.called msg = "Skipping, verifier is disabled." - patched_logger_warning.assert_called_once_with(msg) + assert msg in caplog.text diff --git a/src/molecule/test/unit/verifier/test_testinfra.py b/src/molecule/test/unit/verifier/test_testinfra.py index 34964f118..0e454ff08 100644 --- a/src/molecule/test/unit/verifier/test_testinfra.py +++ b/src/molecule/test/unit/verifier/test_testinfra.py @@ -19,7 +19,6 @@ # DEALINGS IN THE SOFTWARE. import os -from unittest.mock import call import pytest @@ -224,7 +223,7 @@ def test_bake(_patched_testinfra_get_tests, inventory_directory, _instance): def test_execute( - patched_logger_info, + caplog, patched_run_command, _patched_testinfra_get_tests, _instance, @@ -235,15 +234,14 @@ def test_execute( msg = f"Executing Testinfra tests found in {_instance.directory}/..." msg2 = "Verifier completed successfully." - calls = [call(msg), call(msg2)] - patched_logger_info.assert_has_calls(calls) - + assert msg in caplog.text + assert msg2 in caplog.text assert "pytest" in patched_run_command.call_args[0][0].cmd def test_execute_does_not_execute( patched_run_command, - patched_logger_warning, + caplog, _instance, ): _instance._config.config["verifier"]["enabled"] = False @@ -252,12 +250,12 @@ def test_execute_does_not_execute( assert not patched_run_command.called msg = "Skipping, verifier is disabled." - patched_logger_warning.assert_called_once_with(msg) + assert msg in caplog.text def test_does_not_execute_without_tests( patched_run_command, - patched_logger_warning, + caplog, _instance, ): _instance.execute() @@ -265,7 +263,7 @@ def test_does_not_execute_without_tests( assert not patched_run_command.called msg = "Skipping, no tests found." - patched_logger_warning.assert_called_once_with(msg) + assert msg in caplog.text def test_execute_bakes(patched_run_command, _patched_testinfra_get_tests, _instance): diff --git a/src/molecule/util.py b/src/molecule/util.py index f486eee32..ff7d5aeff 100644 --- a/src/molecule/util.py +++ b/src/molecule/util.py @@ -108,7 +108,7 @@ def sysexit_with_message( if detail: detail_str = safe_dump(detail) if isinstance(detail, dict) else str(detail) print(detail_str) - LOG.critical(msg) + LOG.critical(msg, extra={"highlighter": False}) for warn in warns: LOG.warning(warn.__dict__["message"].args[0])