summaryrefslogtreecommitdiffstats
path: root/test/units/modules/test_unarchive.py
blob: 6a2f0d9a676de6618f006c175dd1cee3b8f1b215 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Copyright: Contributors to the Ansible project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import annotations


import time
import pytest

from ansible.modules.unarchive import ZipArchive, TgzArchive


@pytest.fixture
def fake_ansible_module():
    return FakeAnsibleModule()


class FakeAnsibleModule:
    def __init__(self):
        self.params = {}
        self.tmpdir = None


class TestCaseZipArchive:
    @pytest.mark.parametrize(
        'side_effect, expected_reason', (
            ([ValueError, '/bin/zipinfo'], "Unable to find required 'unzip'"),
            (ValueError, "Unable to find required 'unzip' or 'zipinfo'"),
        )
    )
    def test_no_zip_zipinfo_binary(self, mocker, fake_ansible_module, side_effect, expected_reason):
        mocker.patch("ansible.modules.unarchive.get_bin_path", side_effect=side_effect)
        fake_ansible_module.params = {
            "extra_opts": "",
            "exclude": "",
            "include": "",
            "io_buffer_size": 65536,
        }

        z = ZipArchive(
            src="",
            b_dest="",
            file_args="",
            module=fake_ansible_module,
        )
        can_handle, reason = z.can_handle_archive()

        assert can_handle is False
        assert expected_reason in reason
        assert z.cmd_path is None

    @pytest.mark.parametrize(
        ("test_input", "expected"),
        [
            pytest.param(
                "19800000.000000",
                time.mktime(time.struct_time((1980, 0, 0, 0, 0, 0, 0, 0, 0))),
                id="invalid-month-1980",
            ),
            pytest.param(
                "19791231.000000",
                time.mktime(time.struct_time((1980, 1, 1, 0, 0, 0, 0, 0, 0))),
                id="invalid-year-1979",
            ),
            pytest.param(
                "19810101.000000",
                time.mktime(time.struct_time((1981, 1, 1, 0, 0, 0, 0, 0, 0))),
                id="valid-datetime",
            ),
            pytest.param(
                "21081231.000000",
                time.mktime(time.struct_time((2107, 12, 31, 23, 59, 59, 0, 0, 0))),
                id="invalid-year-2108",
            ),
            pytest.param(
                "INVALID_TIME_DATE",
                time.mktime(time.struct_time((1980, 1, 1, 0, 0, 0, 0, 0, 0))),
                id="invalid-datetime",
            ),
        ],
    )
    def test_valid_time_stamp(self, mocker, fake_ansible_module, test_input, expected):
        mocker.patch(
            "ansible.modules.unarchive.get_bin_path",
            side_effect=["/bin/unzip", "/bin/zipinfo"],
        )
        fake_ansible_module.params = {
            "extra_opts": "",
            "exclude": "",
            "include": "",
            "io_buffer_size": 65536,
        }

        z = ZipArchive(
            src="",
            b_dest="",
            file_args="",
            module=fake_ansible_module,
        )
        assert z._valid_time_stamp(test_input) == expected


class TestCaseTgzArchive:
    def test_no_tar_binary(self, mocker, fake_ansible_module):
        mocker.patch("ansible.modules.unarchive.get_bin_path", side_effect=ValueError)
        fake_ansible_module.params = {
            "extra_opts": "",
            "exclude": "",
            "include": "",
            "io_buffer_size": 65536,
        }
        fake_ansible_module.check_mode = False

        t = TgzArchive(
            src="",
            b_dest="",
            file_args="",
            module=fake_ansible_module,
        )
        can_handle, reason = t.can_handle_archive()

        assert can_handle is False
        assert 'Unable to find required' in reason
        assert t.cmd_path is None
        assert t.tar_type is None