Passed
Pull Request — master (#83)
by
unknown
01:50
created

test_common.test_extract_ssg_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
rpm_ssg_file_list = [
152
    "/usr/share/doc/scap-security-guide/Contributors.md",
153
    "/usr/share/doc/scap-security-guide/LICENSE",
154
    "/usr/share/doc/scap-security-guide/README.md",
155
    "/usr/share/man/man8/scap-security-guide.8.gz",
156
    "/usr/share/scap-security-guide/ansible",
157
    "/usr/share/scap-security-guide/ansible/ssg-fedora-role-default.yml",
158
    "/usr/share/scap-security-guide/ansible/ssg-fedora-role-ospp.yml",
159
    "/usr/share/scap-security-guide/ansible/ssg-fedora-role-pci-dss.yml",
160
    "/usr/share/scap-security-guide/ansible/ssg-fedora-role-standard.yml",
161
    "/usr/share/scap-security-guide/bash",
162
    "/usr/share/scap-security-guide/bash/ssg-fedora-role-default.sh",
163
    "/usr/share/scap-security-guide/bash/ssg-fedora-role-ospp.sh",
164
    "/usr/share/scap-security-guide/bash/ssg-fedora-role-pci-dss.sh",
165
    "/usr/share/scap-security-guide/bash/ssg-fedora-role-standard.sh",
166
    "/usr/share/xml/scap/ssg/content",
167
    "/usr/share/xml/scap/ssg/content/ssg-fedora-cpe-dictionary.xml",
168
    "/usr/share/xml/scap/ssg/content/ssg-fedora-cpe-oval.xml",
169
    "/usr/share/xml/scap/ssg/content/ssg-fedora-ds.xml",
170
    "/usr/share/xml/scap/ssg/content/ssg-fedora-ocil.xml",
171
    "/usr/share/xml/scap/ssg/content/ssg-fedora-oval.xml",
172
    "/usr/share/xml/scap/ssg/content/ssg-fedora-xccdf.xml",
173
    ]
174
175
176
def test_extract_ssg_rpm():
177
    temp_path = tempfile.mkdtemp(prefix="rpm")
178
179
    extracted_files = common._extract_rpm(
180
            TESTING_FILES_PATH + "/scap-security-guide.noarch.rpm",
181
            temp_path)
182
183
    assert len(rpm_ssg_file_list) == len(extracted_files)
184
    for rpm_file in rpm_ssg_file_list:
185
        assert temp_path + rpm_file in extracted_files
186
187
    shutil.rmtree(temp_path)
188
189
190
def test_extract_ssg_rpm_ensure_filepath_there():
191
    temp_path = tempfile.mkdtemp(prefix="rpm")
192
193
    extracted_files = common._extract_rpm(
194
            TESTING_FILES_PATH + "/scap-security-guide.noarch.rpm",
195
            temp_path,
196
            ["/usr/share/xml/scap/ssg/content/ssg-fedora-ds.xml"])
197
198
    assert len(rpm_ssg_file_list) == len(extracted_files)
199
    for rpm_file in rpm_ssg_file_list:
200
        assert temp_path + rpm_file in extracted_files
201
202
    shutil.rmtree(temp_path)
203
204
205
def test_extract_ssg_rpm_ensure_filepath_not_there():
206
    temp_path = tempfile.mkdtemp(prefix="rpm")
207
208
    with pytest.raises(common.ExtractionError) as excinfo:
209
        extracted_files = common._extract_rpm(
210
                TESTING_FILES_PATH + "/scap-security-guide.noarch.rpm",
211
                temp_path,
212
                ["/usr/share/xml/scap/ssg/content/ssg-fedora-content.xml"])
213
214
    assert "File '/usr/share/xml/scap/ssg/content/ssg-fedora-content.xml' "\
215
           "not found in the archive" in str(excinfo.value)
216
217
    shutil.rmtree(temp_path)
218
219
rpm_tailoring_file_list = [
220
    "/usr/share/xml/scap/ssg-fedora-ds-tailoring/ssg-fedora-ds.xml",
221
    "/usr/share/xml/scap/ssg-fedora-ds-tailoring/tailoring-xccdf.xml",
222
    ]
223
224
225
def test_extract_tailoring_rpm():
226
    temp_path = tempfile.mkdtemp(prefix="rpm")
227
228
    extracted_files = common._extract_rpm(
229
            TESTING_FILES_PATH + "/ssg-fedora-ds-tailoring-1-1.noarch.rpm",
230
            temp_path)
231
232
    assert len(rpm_tailoring_file_list) == len(extracted_files)
233
    for rpm_file in rpm_tailoring_file_list:
234
        assert temp_path + rpm_file in extracted_files
235
236
    shutil.rmtree(temp_path)
237
238
239
def test_extract_tailoring_rpm_ensure_filepath_there():
240
    temp_path = tempfile.mkdtemp(prefix="rpm")
241
242
    extracted_files = common._extract_rpm(
243
            TESTING_FILES_PATH + "/ssg-fedora-ds-tailoring-1-1.noarch.rpm",
244
            temp_path,
245
            ["/usr/share/xml/scap/ssg-fedora-ds-tailoring/ssg-fedora-ds.xml"])
246
247
    assert len(rpm_tailoring_file_list) == len(extracted_files)
248
    for rpm_file in rpm_tailoring_file_list:
249
        assert temp_path + rpm_file in extracted_files
250
251
    shutil.rmtree(temp_path)
252
253
254
def test_extract_tailoring_rpm_ensure_filename_there():
255
    temp_path = tempfile.mkdtemp(prefix="rpm")
256
257
    with pytest.raises(common.ExtractionError) as excinfo:
258
        extracted_files = common._extract_rpm(
259
                TESTING_FILES_PATH + "/ssg-fedora-ds-tailoring-1-1.noarch.rpm",
260
                temp_path,
261
                ["ssg-fedora-ds.xml"])
262
263
    assert "File 'ssg-fedora-ds.xml' not found in the archive" \
264
           in str(excinfo.value)
265
266
    shutil.rmtree(temp_path)
267