mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-10-23 04:24:00 -07:00
Some checks are pending
EOL CI / EOL Sanity (Ⓐ2.17) (push) Waiting to run
EOL CI / EOL Units (Ⓐ2.17+py3.10) (push) Waiting to run
EOL CI / EOL Units (Ⓐ2.17+py3.12) (push) Waiting to run
EOL CI / EOL Units (Ⓐ2.17+py3.7) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+alpine319+py:azp/posix/1/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+alpine319+py:azp/posix/2/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+alpine319+py:azp/posix/3/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+fedora39+py:azp/posix/1/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+fedora39+py:azp/posix/2/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+fedora39+py:azp/posix/3/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+ubuntu2004+py:azp/posix/1/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+ubuntu2004+py:azp/posix/2/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+ubuntu2004+py:azp/posix/3/) (push) Waiting to run
nox / Run extra sanity tests (push) Waiting to run
* Adjust all __future__ imports: for i in $(grep -REl "__future__.*absolute_import" plugins/ tests/); do sed -e 's/from __future__ import .*/from __future__ import annotations/g' -i $i; done * Remove all UTF-8 encoding specifications for Python source files: for i in $(grep -REl '[-][*]- coding: utf-8 -[*]-' plugins/ tests/); do sed -e '/^# -\*- coding: utf-8 -\*-/d' -i $i; done * Remove __metaclass__ = type: for i in $(grep -REl '__metaclass__ = type' plugins/ tests/); do sed -e '/^__metaclass__ = type/d' -i $i; done
72 lines
2.6 KiB
Python
72 lines
2.6 KiB
Python
# Copyright (c) Ansible project
|
|
# 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
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import Mock, patch
|
|
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ModuleTestCase, set_module_args
|
|
from ansible_collections.community.general.plugins.modules.archive import get_archive, common_path
|
|
|
|
|
|
class TestArchive(ModuleTestCase):
|
|
def setUp(self):
|
|
super(TestArchive, self).setUp()
|
|
|
|
self.mock_os_path_isdir = patch('os.path.isdir')
|
|
self.os_path_isdir = self.mock_os_path_isdir.start()
|
|
|
|
def tearDown(self):
|
|
self.os_path_isdir = self.mock_os_path_isdir.stop()
|
|
|
|
def test_archive_removal_safety(self):
|
|
with set_module_args(
|
|
dict(
|
|
path=['/foo', '/bar', '/baz'],
|
|
dest='/foo/destination.tgz',
|
|
remove=True
|
|
)
|
|
):
|
|
module = AnsibleModule(
|
|
argument_spec=dict(
|
|
path=dict(type='list', elements='path', required=True),
|
|
format=dict(type='str', default='gz', choices=['bz2', 'gz', 'tar', 'xz', 'zip']),
|
|
dest=dict(type='path'),
|
|
exclude_path=dict(type='list', elements='path', default=[]),
|
|
exclusion_patterns=dict(type='list', elements='path'),
|
|
force_archive=dict(type='bool', default=False),
|
|
remove=dict(type='bool', default=False),
|
|
),
|
|
add_file_common_args=True,
|
|
supports_check_mode=True,
|
|
)
|
|
|
|
self.os_path_isdir.side_effect = [True, False, False, True]
|
|
|
|
module.fail_json = Mock()
|
|
|
|
archive = get_archive(module)
|
|
|
|
module.fail_json.assert_called_once_with(
|
|
path=b', '.join(archive.paths),
|
|
msg='Error, created archive can not be contained in source paths when remove=true'
|
|
)
|
|
|
|
|
|
PATHS = (
|
|
([], ''),
|
|
(['/'], '/'),
|
|
([b'/'], b'/'),
|
|
(['/foo', '/bar', '/baz', '/foobar', '/barbaz', '/foo/bar'], '/'),
|
|
([b'/foo', b'/bar', b'/baz', b'/foobar', b'/barbaz', b'/foo/bar'], b'/'),
|
|
(['/foo/bar/baz', '/foo/bar'], '/foo/'),
|
|
(['/foo/bar/baz', '/foo/bar/'], '/foo/bar/'),
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("paths,root", PATHS)
|
|
def test_common_path(paths, root):
|
|
assert common_path(paths) == root
|