Best Python code snippet using molecule_python
test_idempotence.py
Source:test_idempotence.py
1# Copyright (c) 2015-2018 Cisco Systems, Inc.2#3# Permission is hereby granted, free of charge, to any person obtaining a copy4# of this software and associated documentation files (the "Software"), to5# deal in the Software without restriction, including without limitation the6# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or7# sell copies of the Software, and to permit persons to whom the Software is8# furnished to do so, subject to the following conditions:9#10# The above copyright notice and this permission notice shall be included in11# all copies or substantial portions of the Software.12#13# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE16# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER17# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING18# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER19# DEALINGS IN THE SOFTWARE.20import pytest21from molecule.command import idempotence22@pytest.fixture23def _patched_is_idempotent(mocker):24 return mocker.patch("molecule.command.idempotence.Idempotence._is_idempotent")25# NOTE(retr0h): The use of the `patched_config_validate` fixture, disables26# config.Config._validate from executing. Thus preventing odd side-effects27# throughout patched.assert_called unit tests.28@pytest.fixture29def _instance(patched_config_validate, config_instance):30 config_instance.state.change_state("converged", True)31 return idempotence.Idempotence(config_instance)32def test_execute(33 mocker,34 patched_logger_info,35 patched_ansible_converge,36 _patched_is_idempotent,37 patched_logger_success,38 _instance,39):40 _instance.execute()41 x = [mocker.call("Scenario: 'default'"), mocker.call("Action: 'idempotence'")]42 assert x == patched_logger_info.mock_calls43 patched_ansible_converge.assert_called_once_with(out=None, err=None)44 _patched_is_idempotent.assert_called_once_with("patched-ansible-converge-stdout")45 msg = "Idempotence completed successfully."46 patched_logger_success.assert_called_once_with(msg)47def test_execute_raises_when_not_converged(48 patched_logger_critical, patched_ansible_converge, _instance49):50 _instance._config.state.change_state("converged", False)51 with pytest.raises(SystemExit) as e:52 _instance.execute()53 assert 1 == e.value.code54 msg = "Instances not converged. Please converge instances first."55 patched_logger_critical.assert_called_once_with(msg)56def test_execute_raises_when_fails_idempotence(57 mocker,58 patched_logger_critical,59 patched_ansible_converge,60 _patched_is_idempotent,61 _instance,62):63 _patched_is_idempotent.return_value = False64 with pytest.raises(SystemExit) as e:65 _instance.execute()66 assert 1 == e.value.code67 msg = "Idempotence test failed because of the following tasks:\n"68 patched_logger_critical.assert_called_once_with(msg)69def test_is_idempotent(_instance):70 output = """71PLAY RECAP ***********************************************************72check-command-01: ok=3 changed=0 unreachable=0 failed=073 """74 assert _instance._is_idempotent(output)75def test_is_idempotent_not_idempotent(_instance):76 output = """77PLAY RECAP ***********************************************************78check-command-01: ok=2 changed=1 unreachable=0 failed=079check-command-02: ok=2 changed=1 unreachable=0 failed=080 """81 assert not _instance._is_idempotent(output)82def test_non_idempotent_tasks_idempotent(_instance):83 output = """84PLAY [all] ***********************************************************85GATHERING FACTS ******************************************************86ok: [check-command-01]87TASK: [Idempotence test] *********************************************88ok: [check-command-01]89PLAY RECAP ***********************************************************90check-command-01: ok=3 changed=0 unreachable=0 failed=091"""92 result = _instance._non_idempotent_tasks(output)93 assert result == []94def test_non_idempotent_tasks_not_idempotent(_instance):95 output = """96PLAY [all] ***********************************************************97GATHERING FACTS ******************************************************98ok: [check-command-01]99ok: [check-command-02]100TASK: [Idempotence test] *********************************************101changed: [check-command-01]102changed: [check-command-02]103PLAY RECAP ***********************************************************104check-command-01: ok=2 changed=1 unreachable=0 failed=0105check-command-02: ok=2 changed=1 unreachable=0 failed=0106"""107 result = _instance._non_idempotent_tasks(output)108 assert result == [109 "* [check-command-01] => Idempotence test",110 "* [check-command-02] => Idempotence test",...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!