v2
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
# Copyright (c) 2017 Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
from ansible.module_utils.six import string_types
|
||||
from ansible.module_utils._text import to_bytes
|
||||
from ansible.module_utils.common._collections_compat import MutableMapping
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def patch_ansible_module(request, mocker):
|
||||
if isinstance(request.param, string_types):
|
||||
args = request.param
|
||||
elif isinstance(request.param, MutableMapping):
|
||||
if 'ANSIBLE_MODULE_ARGS' not in request.param:
|
||||
request.param = {'ANSIBLE_MODULE_ARGS': request.param}
|
||||
if '_ansible_remote_tmp' not in request.param['ANSIBLE_MODULE_ARGS']:
|
||||
request.param['ANSIBLE_MODULE_ARGS']['_ansible_remote_tmp'] = '/tmp'
|
||||
if '_ansible_keep_remote_files' not in request.param['ANSIBLE_MODULE_ARGS']:
|
||||
request.param['ANSIBLE_MODULE_ARGS']['_ansible_keep_remote_files'] = False
|
||||
args = json.dumps(request.param)
|
||||
else:
|
||||
raise Exception('Malformed data to the patch_ansible_module pytest fixture')
|
||||
|
||||
mocker.patch('ansible.module_utils.basic._ANSIBLE_ARGS', to_bytes(args))
|
||||
@@ -0,0 +1,63 @@
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
from ansible_collections.ansible.posix.tests.unit.compat import unittest
|
||||
from ansible_collections.ansible.posix.tests.unit.compat.mock import MagicMock
|
||||
from ansible.module_utils._text import to_bytes
|
||||
|
||||
from ansible_collections.ansible.posix.plugins.modules.mount import (
|
||||
get_linux_mounts,
|
||||
_set_mount_save_old,
|
||||
set_mount,
|
||||
)
|
||||
|
||||
|
||||
class LinuxMountsTestCase(unittest.TestCase):
|
||||
|
||||
def _create_file(self, content):
|
||||
tmp_file = tempfile.NamedTemporaryFile(prefix='ansible-test-', delete=False)
|
||||
tmp_file.write(to_bytes(content))
|
||||
tmp_file.close()
|
||||
self.addCleanup(os.unlink, tmp_file.name)
|
||||
return tmp_file.name
|
||||
|
||||
def test_code_comment(self):
|
||||
path = self._create_file(
|
||||
'140 136 253:2 /rootfs / rw - ext4 /dev/sdb2 rw\n'
|
||||
'141 140 253:2 /rootfs/tmp/aaa /tmp/bbb rw - ext4 /dev/sdb2 rw\n'
|
||||
)
|
||||
mounts = get_linux_mounts(None, path)
|
||||
self.assertEqual(mounts['/tmp/bbb']['src'], '/tmp/aaa')
|
||||
|
||||
def test_set_mount_save_old(self):
|
||||
module = MagicMock(name='AnsibleModule')
|
||||
module.check_mode = True
|
||||
module.params = {'backup': False}
|
||||
|
||||
fstab_data = [
|
||||
'UUID=8ac075e3-1124-4bb6-bef7-a6811bf8b870 / xfs defaults 0 0\n',
|
||||
'/swapfile none swap defaults 0 0\n'
|
||||
]
|
||||
path = self._create_file("".join(fstab_data))
|
||||
args = {
|
||||
'fstab': path,
|
||||
'name': '/data',
|
||||
'src': '/dev/sdb1',
|
||||
'fstype': 'ext4',
|
||||
'opts': 'defaults',
|
||||
'dump': '0',
|
||||
'passno': '0',
|
||||
}
|
||||
|
||||
name, changed = set_mount(module, args)
|
||||
self.assertEqual(name, '/data')
|
||||
self.assertTrue(changed)
|
||||
|
||||
name, backup_lines, changed = _set_mount_save_old(module, args)
|
||||
self.assertEqual(backup_lines, fstab_data)
|
||||
self.assertEqual(name, '/data')
|
||||
self.assertTrue(changed)
|
||||
@@ -0,0 +1,51 @@
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
import json
|
||||
|
||||
from ansible_collections.ansible.posix.tests.unit.compat import unittest
|
||||
from ansible_collections.ansible.posix.tests.unit.compat.mock import patch
|
||||
from ansible.module_utils import basic
|
||||
from ansible.module_utils._text import to_bytes
|
||||
|
||||
|
||||
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)
|
||||
Reference in New Issue
Block a user