Debian Codegraph @

test/units/modules/test_unarchive.py

raw
1 # Copyright: Contributors to the Ansible project
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3 from __future__ import annotations
4
5
6 import time
7 import pytest
8
9 from ansible.modules.unarchive import ZipArchive, TgzArchive
10
11
12 @pytest.fixture
13 def fake_ansible_module():
14 return FakeAnsibleModule()
15
16
17 def max_zip_timestamp():
18 """Return the max clamp value that will be selected."""
19 try:
20 return time.mktime(time.struct_time((2107, 12, 31, 23, 59, 59, 0, 0, 0)))
21 except OverflowError:
22 return time.mktime(time.struct_time((2038, 1, 1, 0, 0, 0, 0, 0, 0)))
23
24
25 class FakeAnsibleModule:
26 def __init__(self):
27 self.params = {}
28 self.tmpdir = None
29
30
31 class TestCaseZipArchive:
32 @pytest.mark.parametrize(
33 'side_effect, expected_reason', (
34 ([ValueError, '/bin/zipinfo'], "Unable to find required 'unzip'"),
35 (ValueError, "Unable to find required 'unzip' or 'zipinfo'"),
36 )
37 )
38 def test_no_zip_zipinfo_binary(self, mocker, fake_ansible_module, side_effect, expected_reason):
39 mocker.patch("ansible.modules.unarchive.get_bin_path", side_effect=side_effect)
40 fake_ansible_module.params = {
41 "extra_opts": "",
42 "exclude": "",
43 "include": "",
44 "io_buffer_size": 65536,
45 }
46
47 z = ZipArchive(
48 src="",
49 b_dest="",
50 file_args="",
51 module=fake_ansible_module,
52 )
53 can_handle, reason = z.can_handle_archive()
54
55 assert can_handle is False
56 assert expected_reason in reason
57 assert z.cmd_path is None
58
59 @pytest.mark.parametrize(
60 ("test_input", "expected"),
61 [
62 pytest.param(
63 "19800000.000000",
64 time.mktime(time.struct_time((1980, 0, 0, 0, 0, 0, 0, 0, 0))),
65 id="invalid-month-1980",
66 ),
67 pytest.param(
68 "19791231.000000",
69 time.mktime(time.struct_time((1980, 1, 1, 0, 0, 0, 0, 0, 0))),
70 id="invalid-year-1979",
71 ),
72 pytest.param(
73 "19810101.000000",
74 time.mktime(time.struct_time((1981, 1, 1, 0, 0, 0, 0, 0, 0))),
75 id="valid-datetime",
76 ),
77 pytest.param(
78 "21081231.000000",
79 max_zip_timestamp(),
80 id="invalid-year-2108",
81 ),
82 pytest.param(
83 "INVALID_TIME_DATE",
84 time.mktime(time.struct_time((1980, 1, 1, 0, 0, 0, 0, 0, 0))),
85 id="invalid-datetime",
86 ),
87 ],
88 )
89 def test_valid_time_stamp(self, mocker, fake_ansible_module, test_input, expected):
90 mocker.patch(
91 "ansible.modules.unarchive.get_bin_path",
92 side_effect=["/bin/unzip", "/bin/zipinfo"],
93 )
94 fake_ansible_module.params = {
95 "extra_opts": "",
96 "exclude": "",
97 "include": "",
98 "io_buffer_size": 65536,
99 }
100
101 z = ZipArchive(
102 src="",
103 b_dest="",
104 file_args="",
105 module=fake_ansible_module,
106 )
107 assert z._valid_time_stamp(test_input) == expected
108
109
110 class TestCaseTgzArchive:
111 def test_no_tar_binary(self, mocker, fake_ansible_module):
112 mocker.patch("ansible.modules.unarchive.get_bin_path", side_effect=ValueError)
113 fake_ansible_module.params = {
114 "extra_opts": "",
115 "exclude": "",
116 "include": "",
117 "io_buffer_size": 65536,
118 }
119 fake_ansible_module.check_mode = False
120
121 t = TgzArchive(
122 src="",
123 b_dest="",
124 file_args="",
125 module=fake_ansible_module,
126 )
127 can_handle, reason = t.can_handle_archive()
128
129 assert can_handle is False
130 assert 'Unable to find required' in reason
131 assert t.cmd_path is None
132 assert t.tar_type is None