2022-08-08 14:24:58 +02:00
|
|
|
# Copyright (c) Ansible project
|
2022-08-05 12:28:29 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-06-24 21:50:36 +02:00
|
|
|
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
import json
|
|
|
|
|
|
|
|
from ansible_collections.community.general.tests.unit.compat import unittest
|
|
|
|
from ansible_collections.community.general.tests.unit.compat.mock import patch
|
|
|
|
from ansible.module_utils import basic
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_bytes
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def set_module_args(args):
|
|
|
|
if '_ansible_remote_tmp' not in args:
|
|
|
|
args['_ansible_remote_tmp'] = '/tmp'
|
|
|
|
if '_ansible_keep_remote_files' not in args:
|
|
|
|
args['_ansible_keep_remote_files'] = False
|
|
|
|
|
|
|
|
args = json.dumps({'ANSIBLE_MODULE_ARGS': args})
|
|
|
|
basic._ANSIBLE_ARGS = to_bytes(args)
|
|
|
|
|
|
|
|
|
|
|
|
class AnsibleExitJson(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class AnsibleFailJson(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def exit_json(*args, **kwargs):
|
|
|
|
if 'changed' not in kwargs:
|
|
|
|
kwargs['changed'] = False
|
|
|
|
raise AnsibleExitJson(kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def fail_json(*args, **kwargs):
|
|
|
|
kwargs['failed'] = True
|
|
|
|
raise AnsibleFailJson(kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class ModuleTestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.mock_module = patch.multiple(basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json)
|
|
|
|
self.mock_module.start()
|
|
|
|
self.mock_sleep = patch('time.sleep')
|
|
|
|
self.mock_sleep.start()
|
|
|
|
set_module_args({})
|
|
|
|
self.addCleanup(self.mock_module.stop)
|
|
|
|
self.addCleanup(self.mock_sleep.stop)
|