Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_archive.py

raw
1 # Copyright (c) Ansible project
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import annotations
6
7 from unittest.mock import Mock, patch
8
9 import pytest
10 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
11 ModuleTestCase,
12 set_module_args,
13 )
14
15 from ansible_collections.community.general.plugins.modules.archive import common_path, create_module, get_archive
16
17
18 class TestArchive(ModuleTestCase):
19 def setUp(self):
20 super().setUp()
21
22 self.mock_os_path_isdir = patch("os.path.isdir")
23 self.os_path_isdir = self.mock_os_path_isdir.start()
24
25 def tearDown(self):
26 self.os_path_isdir = self.mock_os_path_isdir.stop()
27
28 def test_archive_removal_safety(self):
29 with set_module_args(dict(path=["/foo", "/bar", "/baz"], dest="/foo/destination.tgz", remove=True)):
30 module = create_module()
31
32 self.os_path_isdir.side_effect = [True, False, False, True]
33
34 module.fail_json = Mock()
35
36 archive = get_archive(module)
37
38 module.fail_json.assert_called_once_with(
39 path=b", ".join(archive.paths),
40 msg="Error, created archive can not be contained in source paths when remove=true",
41 )
42
43
44 PATHS: tuple[tuple[list[str | bytes], str | bytes], ...] = (
45 ([], ""),
46 (["/"], "/"),
47 ([b"/"], b"/"),
48 (["/foo", "/bar", "/baz", "/foobar", "/barbaz", "/foo/bar"], "/"),
49 ([b"/foo", b"/bar", b"/baz", b"/foobar", b"/barbaz", b"/foo/bar"], b"/"),
50 (["/foo/bar/baz", "/foo/bar"], "/foo/"),
51 (["/foo/bar/baz", "/foo/bar/"], "/foo/bar/"),
52 )
53
54
55 @pytest.mark.parametrize("paths,root", PATHS)
56 def test_common_path(paths: list[str | bytes], root: str | bytes) -> None:
57 assert common_path(paths) == root