Passed
Pull Request — master (#83)
by
unknown
54s
created

test_common.test_extract_tailoring_rpm()   A

Complexity

Conditions 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 12
rs 9.95
c 0
b 0
f 0
cc 2
nop 0
1
#
2
# Copyright (C) 2013  Red Hat, Inc.
3
#
4
# This copyrighted material is made available to anyone wishing to use,
5
# modify, copy, or redistribute it subject to the terms and conditions of
6
# the GNU General Public License v.2, or (at your option) any later version.
7
# This program is distributed in the hope that it will be useful, but WITHOUT
8
# ANY WARRANTY expressed or implied, including the implied warranties of
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
10
# Public License for more details.  You should have received a copy of the
11
# GNU General Public License along with this program; if not, write to the
12
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
13
# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
14
# source code or documentation are not subject to the GNU General Public
15
# License and may only be used or replicated with the express permission of
16
# Red Hat, Inc.
17
#
18
# Red Hat Author(s): Vratislav Podzimek <[email protected]>
19
#
20
21
"""Module with unit tests for the common.py module"""
22
23
import os
24
import mock
25
import shutil
26
27
import pytest
28
import tempfile
29
30
from org_fedora_oscap import common
31
32
TESTING_FILES_PATH = os.path.join(
33
    os.path.dirname(__file__), os.path.pardir, "testing_files")
34
35
@pytest.fixture()
36
def mock_subprocess():
37
    mock_subprocess = mock.Mock()
38
    mock_subprocess.Popen = mock.Mock()
39
    mock_popen = mock.Mock()
40
    mock_communicate = mock.Mock()
41
42
    mock_communicate.return_value = (b"", b"")
43
44
    mock_popen.communicate = mock_communicate
45
    mock_popen.returncode = 0
46
47
    mock_subprocess.Popen.return_value = mock_popen
48
    mock_subprocess.PIPE = mock.Mock()
49
50
    return mock_subprocess
51
52
53
def mock_run_remediate(mock_subprocess, monkeypatch):
54
    mock_utils = mock.Mock()
55
    mock_utils.ensure_dir_exists = mock.Mock()
56
57
    common_module_symbols = common.__dict__
58
59
    monkeypatch.setitem(common_module_symbols, "subprocess", mock_subprocess)
60
    monkeypatch.setitem(common_module_symbols, "utils", mock_utils)
61
62
63
def _run_oscap(mock_subprocess, additional_args):
64
    expected_args = [
65
        "oscap", "xccdf", "eval", "--remediate",
66
        "--results=%s" % common.RESULTS_PATH,
67
        "--report=%s" % common.REPORT_PATH,
68
        "--profile=myprofile",
69
    ]
70
    expected_args.extend(additional_args)
71
72
    kwargs = {
73
        "stdout": mock_subprocess.PIPE,
74
        "stderr": mock_subprocess.PIPE,
75
    }
76
77
    return expected_args, kwargs
78
79
80
def test_run_oscap_remediate_profile_only(mock_subprocess, monkeypatch):
81
    return run_oscap_remediate_profile(
82
        mock_subprocess, monkeypatch,
83
        ["myprofile", "my_ds.xml"],
84
        ["my_ds.xml"])
85
86
87
def test_run_oscap_remediate_with_ds(mock_subprocess, monkeypatch):
88
    return run_oscap_remediate_profile(
89
        mock_subprocess, monkeypatch,
90
        ["myprofile", "my_ds.xml", "my_ds_id"],
91
        ["--datastream-id=my_ds_id", "my_ds.xml"])
92
93
94
def test_run_oscap_remediate_with_ds_xccdf(mock_subprocess, monkeypatch):
95
    return run_oscap_remediate_profile(
96
        mock_subprocess, monkeypatch,
97
        ["myprofile", "my_ds.xml", "my_ds_id", "my_xccdf_id"],
98
        ["--datastream-id=my_ds_id", "--xccdf-id=my_xccdf_id", "my_ds.xml"])
99
100
101
def run_oscap_remediate_profile(
102
        mock_subprocess, monkeypatch,
103
        anaconda_remediate_args, oscap_remediate_args):
104
    mock_run_remediate(mock_subprocess, monkeypatch)
105
    common.run_oscap_remediate(* anaconda_remediate_args)
106
107
    expected_args = [
108
        "oscap", "xccdf", "eval", "--remediate",
109
        "--results=%s" % common.RESULTS_PATH,
110
        "--report=%s" % common.REPORT_PATH,
111
        "--profile=myprofile",
112
    ]
113
    expected_args.extend(oscap_remediate_args)
114
115
    kwargs = {
116
        "stdout": mock_subprocess.PIPE,
117
        "stderr": mock_subprocess.PIPE,
118
    }
119
120
    # it's impossible to check the preexec_func as it is an internal
121
    # function of the run_oscap_remediate function
122
    for arg in expected_args:
123
        assert arg in mock_subprocess.Popen.call_args[0][0]
124
        mock_subprocess.Popen.call_args[0][0].remove(arg)
125
126
    # nothing else should have been passed
127
    assert not mock_subprocess.Popen.call_args[0][0]
128
129
    for (key, val) in kwargs.items():
130
        assert kwargs[key] == mock_subprocess.Popen.call_args[1].pop(key)
131
132
    # plus the preexec_fn kwarg should have been passed
133
    assert "preexec_fn" in mock_subprocess.Popen.call_args[1]
134
135
136
def test_run_oscap_remediate_create_dir(mock_subprocess, monkeypatch):
137
    mock_run_remediate(mock_subprocess, monkeypatch)
138
    common.run_oscap_remediate("myprofile", "my_ds.xml")
139
140
    common.utils.ensure_dir_exists.assert_called_with(
141
        os.path.dirname(common.RESULTS_PATH))
142
143
144
def test_run_oscap_remediate_create_chroot_dir(mock_subprocess, monkeypatch):
145
    mock_run_remediate(mock_subprocess, monkeypatch)
146
    common.run_oscap_remediate("myprofile", "my_ds.xml", chroot="/mnt/test")
147
148
    chroot_dir = "/mnt/test" + os.path.dirname(common.RESULTS_PATH)
149
    common.utils.ensure_dir_exists.assert_called_with(chroot_dir)
150
151
152
rpm_ssg_file_list = [
153
    "/usr/share/doc/scap-security-guide/Contributors.md",
154
    "/usr/share/doc/scap-security-guide/LICENSE",
155
    "/usr/share/doc/scap-security-guide/README.md",
156
    "/usr/share/man/man8/scap-security-guide.8.gz",
157
    "/usr/share/scap-security-guide/ansible",
158
    "/usr/share/scap-security-guide/ansible/ssg-fedora-role-default.yml",
159
    "/usr/share/scap-security-guide/ansible/ssg-fedora-role-ospp.yml",
160
    "/usr/share/scap-security-guide/ansible/ssg-fedora-role-pci-dss.yml",
161
    "/usr/share/scap-security-guide/ansible/ssg-fedora-role-standard.yml",
162
    "/usr/share/scap-security-guide/bash",
163
    "/usr/share/scap-security-guide/bash/ssg-fedora-role-default.sh",
164
    "/usr/share/scap-security-guide/bash/ssg-fedora-role-ospp.sh",
165
    "/usr/share/scap-security-guide/bash/ssg-fedora-role-pci-dss.sh",
166
    "/usr/share/scap-security-guide/bash/ssg-fedora-role-standard.sh",
167
    "/usr/share/xml/scap/ssg/content",
168
    "/usr/share/xml/scap/ssg/content/ssg-fedora-cpe-dictionary.xml",
169
    "/usr/share/xml/scap/ssg/content/ssg-fedora-cpe-oval.xml",
170
    "/usr/share/xml/scap/ssg/content/ssg-fedora-ds.xml",
171
    "/usr/share/xml/scap/ssg/content/ssg-fedora-ocil.xml",
172
    "/usr/share/xml/scap/ssg/content/ssg-fedora-oval.xml",
173
    "/usr/share/xml/scap/ssg/content/ssg-fedora-xccdf.xml",
174
    ]
175
176
177
def test_extract_ssg_rpm():
178
    temp_path = tempfile.mkdtemp(prefix="rpm")
179
180
    extracted_files = common._extract_rpm(
181
            TESTING_FILES_PATH + "/scap-security-guide.noarch.rpm",
182
            temp_path)
183
184
    assert len(rpm_ssg_file_list) == len(extracted_files)
185
    for rpm_file in rpm_ssg_file_list:
186
        assert temp_path + rpm_file in extracted_files
187
188
    shutil.rmtree(temp_path)
189
190
191
def test_extract_ssg_rpm_ensure_filepath_there():
192
    temp_path = tempfile.mkdtemp(prefix="rpm")
193
194
    extracted_files = common._extract_rpm(
195
            TESTING_FILES_PATH + "/scap-security-guide.noarch.rpm",
196
            temp_path,
197
            ["/usr/share/xml/scap/ssg/content/ssg-fedora-ds.xml"])
198
199
    assert len(rpm_ssg_file_list) == len(extracted_files)
200
    for rpm_file in rpm_ssg_file_list:
201
        assert temp_path + rpm_file in extracted_files
202
203
    shutil.rmtree(temp_path)
204
205
206
def test_extract_ssg_rpm_ensure_filepath_not_there():
207
    temp_path = tempfile.mkdtemp(prefix="rpm")
208
209
    with pytest.raises(common.ExtractionError) as excinfo:
210
        extracted_files = common._extract_rpm(
211
                TESTING_FILES_PATH + "/scap-security-guide.noarch.rpm",
212
                temp_path,
213
                ["/usr/share/xml/scap/ssg/content/ssg-fedora-content.xml"])
214
215
    assert "File '/usr/share/xml/scap/ssg/content/ssg-fedora-content.xml' "\
216
           "not found in the archive" in str(excinfo.value)
217
218
    shutil.rmtree(temp_path)
219
220
221
rpm_tailoring_file_list = [
222
    "/usr/share/xml/scap/ssg-fedora-ds-tailoring/ssg-fedora-ds.xml",
223
    "/usr/share/xml/scap/ssg-fedora-ds-tailoring/tailoring-xccdf.xml",
224
    ]
225
226
227
def test_extract_tailoring_rpm():
228
    temp_path = tempfile.mkdtemp(prefix="rpm")
229
230
    extracted_files = common._extract_rpm(
231
            TESTING_FILES_PATH + "/ssg-fedora-ds-tailoring-1-1.noarch.rpm",
232
            temp_path)
233
234
    assert len(rpm_tailoring_file_list) == len(extracted_files)
235
    for rpm_file in rpm_tailoring_file_list:
236
        assert temp_path + rpm_file in extracted_files
237
238
    shutil.rmtree(temp_path)
239
240
241
def test_extract_tailoring_rpm_ensure_filepath_there():
242
    temp_path = tempfile.mkdtemp(prefix="rpm")
243
244
    extracted_files = common._extract_rpm(
245
            TESTING_FILES_PATH + "/ssg-fedora-ds-tailoring-1-1.noarch.rpm",
246
            temp_path,
247
            ["/usr/share/xml/scap/ssg-fedora-ds-tailoring/ssg-fedora-ds.xml"])
248
249
    assert len(rpm_tailoring_file_list) == len(extracted_files)
250
    for rpm_file in rpm_tailoring_file_list:
251
        assert temp_path + rpm_file in extracted_files
252
253
    shutil.rmtree(temp_path)
254
255
256
def test_extract_tailoring_rpm_ensure_filename_there():
257
    temp_path = tempfile.mkdtemp(prefix="rpm")
258
259
    with pytest.raises(common.ExtractionError) as excinfo:
260
        extracted_files = common._extract_rpm(
261
                TESTING_FILES_PATH + "/ssg-fedora-ds-tailoring-1-1.noarch.rpm",
262
                temp_path,
263
                ["ssg-fedora-ds.xml"])
264
265
    assert "File 'ssg-fedora-ds.xml' not found in the archive" \
266
           in str(excinfo.value)
267
268
    shutil.rmtree(temp_path)
269