def execute(self, exit=True): util.print_info( 'Idempotence test in progress (can take a few minutes)...') c = converge.Converge(self.command_args, self.args, self.molecule) status, output = c.execute(idempotent=True, exit=False, hide_errors=True) if status is not None: msg = 'Skipping due to errors during converge.' util.print_info(msg) return status, None idempotent, changed_tasks = self.molecule._parse_provisioning_output( output) if idempotent: util.print_success('Idempotence test passed.') return None, None # Display the details of the idempotence test. if changed_tasks: LOG.error( 'Idempotence test failed because of the following tasks:') LOG.error('{}'.format('\n'.join(changed_tasks))) else: # But in case the idempotence callback plugin was not found, we just display an error message. LOG.error('Idempotence test failed.') warning_msg = "The idempotence plugin was not found or did not provide the required information. " \ "Therefore the failure details cannot be displayed." LOG.warning(warning_msg) if exit: util.sysexit() return 1, None
def test_vagrant_converge(molecule_file): c = converge.Converge([], []) try: c.execute() except SystemExit as f: assert f.code == 0
def execute(self, exit=True): """ Execute the actions necessary to perform a `molecule idempotence` and return a tuple. :param exit: An optional flag to toggle the exiting of the module on command failure. :return: Return a tuple of (`exit status`, `command output`), otherwise sys.exit on command failure. """ util.print_info( 'Idempotence test in progress (can take a few minutes) ...') c = converge.Converge(self.args, self.command_args, self.molecule) status, output = c.execute( idempotent=True, exit=False, hide_errors=True) if status is not None: msg = 'Skipping due to errors during converge.' util.print_info(msg) return status, None idempotent = self._is_idempotent(output) if idempotent: util.print_success('Idempotence test passed.') return None, None else: LOG.error( 'Idempotence test failed because of the following tasks:') LOG.error('\n'.join(self._non_idempotent_tasks(output))) if exit: util.sysexit() return 1, None
def test_execute_raises_on_exit(patched_create, patched_ansible_playbook, patched_create_inventory, molecule_instance): patched_ansible_playbook.return_value = (1, None) c = converge.Converge({}, {}, molecule_instance) with pytest.raises(SystemExit): result = c.execute() assert (1, None) == result
def test_execute_with_tags(mocker, patched_create, patched_ansible_playbook, patched_add_cli_arg, patched_create_inventory, molecule_instance): command_args = {'tags': 'foo,bar'} c = converge.Converge({}, command_args, molecule_instance) c.execute() patched_add_cli_arg.assert_called_with('tags', 'foo,bar')
def test_execute_does_not_create_inventory( patched_create, patched_ansible_playbook, patched_create_inventory, molecule_instance): molecule_instance.state.change_state('converged', True) c = converge.Converge({}, {}, molecule_instance) c.execute() assert not patched_create_inventory.called
def test_execute_does_not_raise_on_exit( patched_create, patched_ansible_playbook, patched_create_inventory, molecule_instance): patched_ansible_playbook.return_value = (1, None) c = converge.Converge({}, {}, molecule_instance) result = c.execute(exit=False) assert (1, None) == result
def test_execute_installs_dependencies( patched_create, patched_ansible_playbook, patched_dependency, patched_create_inventory, molecule_instance): molecule_instance.config.config['dependency']['requirements_file'] = True c = converge.Converge({}, {}, molecule_instance) c.execute() patched_dependency.assert_called_once()
def test_execute_create_inventory_and_instances_with_platform_all_state_file( patched_create, patched_ansible_playbook, patched_create_inventory, molecule_instance): molecule_instance.state.change_state('multiple_platforms', True) c = converge.Converge({}, {}, molecule_instance) c.execute() patched_create.assert_called_once_with() patched_create_inventory.assert_called_once_with()
def test_execute_create_inventory_and_instances_with_platform_all( patched_create, patched_ansible_playbook, patched_create_inventory, molecule_instance): molecule_instance.state.change_state('created', True) command_args = {'platform': 'all'} c = converge.Converge({}, command_args, molecule_instance) c.execute() patched_create.assert_called_once_with() patched_create_inventory.assert_called_once_with()
def test_execute_creates_instances(patched_create, patched_ansible_playbook, patched_create_inventory, patched_print_info, molecule_instance): c = converge.Converge({}, {}, molecule_instance) result = c.execute() msg = 'Starting Ansible Run ...' patched_print_info.assert_called_once_with(msg) patched_ansible_playbook.assert_called_once_with(hide_errors=True) assert (None, None) == result assert molecule_instance.state.converged
def test_execute(mocker, patched_logger_info, patched_ansible_converge, config_instance): c = converge.Converge(config_instance) c.execute() x = [ mocker.call("Scenario: 'default'"), mocker.call("Action: 'converge'"), ] assert x == patched_logger_info.mock_calls patched_ansible_converge.assert_called_once_with() assert config_instance.state.converged
def test_execute(mocker, patched_logger_info, patched_ansible_converge, config_instance): c = converge.Converge(config_instance) c.execute() x = [ mocker.call('Scenario: [default]'), mocker.call('Provisioner: [ansible]'), mocker.call('Playbook: [playbook.yml]') ] assert x == patched_logger_info.mock_calls patched_ansible_converge.assert_called_once_with() assert config_instance.state.converged
def test_execute_adds_idempotency_flags( mocker, patched_create, patched_ansible_playbook, patched_create_inventory, patched_add_env_arg, patched_remove_cli_arg, molecule_instance): c = converge.Converge({}, {}, molecule_instance) c.execute(idempotent=True) expected = [mocker.call('_out'), mocker.call('_err')] assert expected == patched_remove_cli_arg.mock_calls assert mocker.call('ANSIBLE_NOCOLOR', 'true') in patched_add_env_arg.mock_calls assert mocker.call('ANSIBLE_FORCE_COLOR', 'false') in patched_add_env_arg.mock_calls
def test_execute_with_debug(patched_create, patched_ansible_playbook, patched_create_inventory, patched_print_debug, molecule_instance): args = {'debug': True} c = converge.Converge(args, {}, molecule_instance) c.execute() executable = sh.ansible_playbook playbook = molecule_instance.config.config['ansible']['playbook'] expected = ['--connection=ssh', '--diff', '--inventory-file=test/inventory_file', '--limit=all', '--sudo', '--timeout=30', '--user=vagrant', '-vvvv', executable, playbook] args, _ = patched_print_debug.call_args assert expected == sorted(args[1].split())
def test_execute_with_debug(patched_create, patched_ansible_playbook, patched_create_inventory, patched_print_debug, molecule_instance): args = {'debug': True} c = converge.Converge(args, {}, molecule_instance) c.execute() patched_ansible_playbook.assert_called_once() x = ("ANSIBLE_CONFIG: test/config_file\n" "ANSIBLE_FORCE_COLOR: 'true'\n" "ANSIBLE_HOST_KEY_CHECKING: 'false'\n" "ANSIBLE_SSH_ARGS: -o UserKnownHostsFile=/dev/null " "-o IdentitiesOnly=yes " "-o ControlMaster=auto\n " "-o ControlPersist=60s\n") patched_print_debug.assert_called_with('ANSIBLE ENVIRONMENT', x)
def test_execute( mocker, patched_logger_info, patched_ansible_converge, patched_config_validate, config_instance, ): 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 patched_ansible_converge.assert_called_once_with() assert config_instance.state.converged