Passed
Pull Request — master (#72)
by Matěj
01:09
created

test_common   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 81
dl 0
loc 146
rs 10
c 0
b 0
f 0

9 Functions

Rating   Name   Duplication   Size   Complexity  
C run_oscap_remediate_profile() 0 33 7
A test_run_oscap_remediate_profile_only_test() 0 5 1
A test_run_oscap_remediate_create_dir() 0 6 1
A mock_subprocess() 0 16 1
A test_run_oscap_remediate_with_ds_xccdf() 0 5 1
A test_run_oscap_remediate_with_ds() 0 5 1
A mock_run_remediate() 0 8 1
A _run_oscap() 0 15 1
A test_run_oscap_remediate_create_chroot_dir() 0 6 1
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
26
import pytest
27
28
from org_fedora_oscap import common
29
30
31
@pytest.fixture()
32
def mock_subprocess():
33
    mock_subprocess = mock.Mock()
34
    mock_subprocess.Popen = mock.Mock()
35
    mock_popen = mock.Mock()
36
    mock_communicate = mock.Mock()
37
38
    mock_communicate.return_value = (b"", b"")
39
40
    mock_popen.communicate = mock_communicate
41
    mock_popen.returncode = 0
42
43
    mock_subprocess.Popen.return_value = mock_popen
44
    mock_subprocess.PIPE = mock.Mock()
45
46
    return mock_subprocess
47
48
49
def mock_run_remediate(mock_subprocess, monkeypatch):
50
    mock_utils = mock.Mock()
51
    mock_utils.ensure_dir_exists = mock.Mock()
52
53
    common_module_symbols = common.__dict__
54
55
    monkeypatch.setitem(common_module_symbols, "subprocess", mock_subprocess)
56
    monkeypatch.setitem(common_module_symbols, "utils", mock_utils)
57
58
59
def _run_oscap(mock_subprocess, additional_args):
60
    expected_args = [
61
        "oscap", "xccdf", "eval", "--remediate",
62
        "--results=%s" % common.RESULTS_PATH,
63
        "--report=%s" % common.REPORT_PATH,
64
        "--profile=myprofile",
65
    ]
66
    expected_args.extend(additional_args)
67
68
    kwargs = {
69
        "stdout": mock_subprocess.PIPE,
70
        "stderr": mock_subprocess.PIPE,
71
    }
72
73
    return expected_args, kwargs
74
75
76
def test_run_oscap_remediate_profile_only_test(mock_subprocess, monkeypatch):
77
    return run_oscap_remediate_profile(
78
        mock_subprocess, monkeypatch,
79
        ["myprofile", "my_ds.xml"],
80
        ["my_ds.xml"])
81
82
83
def test_run_oscap_remediate_with_ds(mock_subprocess, monkeypatch):
84
    return run_oscap_remediate_profile(
85
        mock_subprocess, monkeypatch,
86
        ["myprofile", "my_ds.xml", "my_ds_id"],
87
        ["--datastream-id=my_ds_id", "my_ds.xml"])
88
89
90
def test_run_oscap_remediate_with_ds_xccdf(mock_subprocess, monkeypatch):
91
    return run_oscap_remediate_profile(
92
        mock_subprocess, monkeypatch,
93
        ["myprofile", "my_ds.xml", "my_ds_id", "my_xccdf_id"],
94
        ["--datastream-id=my_ds_id", "--xccdf-id=my_xccdf_id", "my_ds.xml"])
95
96
97
def run_oscap_remediate_profile(
98
        mock_subprocess, monkeypatch,
99
        anaconda_remediate_args, oscap_remediate_args):
100
    mock_run_remediate(mock_subprocess, monkeypatch)
101
    common.run_oscap_remediate(* anaconda_remediate_args)
102
103
    expected_args = [
104
        "oscap", "xccdf", "eval", "--remediate",
105
        "--results=%s" % common.RESULTS_PATH,
106
        "--report=%s" % common.REPORT_PATH,
107
        "--profile=myprofile",
108
    ]
109
    expected_args.extend(oscap_remediate_args)
110
111
    kwargs = {
112
        "stdout": mock_subprocess.PIPE,
113
        "stderr": mock_subprocess.PIPE,
114
    }
115
116
    # it's impossible to check the preexec_func as it is an internal
117
    # function of the run_oscap_remediate function
118
    for arg in expected_args:
119
        assert arg in mock_subprocess.Popen.call_args[0][0]
120
        mock_subprocess.Popen.call_args[0][0].remove(arg)
121
122
    # nothing else should have been passed
123
    assert not mock_subprocess.Popen.call_args[0][0]
124
125
    for (key, val) in kwargs.items():
126
        assert kwargs[key] == mock_subprocess.Popen.call_args[1].pop(key)
127
128
    # plus the preexec_fn kwarg should have been passed
129
    assert "preexec_fn" in mock_subprocess.Popen.call_args[1]
130
131
132
def test_run_oscap_remediate_create_dir(mock_subprocess, monkeypatch):
133
    mock_run_remediate(mock_subprocess, monkeypatch)
134
    common.run_oscap_remediate("myprofile", "my_ds.xml")
135
136
    common.utils.ensure_dir_exists.assert_called_with(
137
        os.path.dirname(common.RESULTS_PATH))
138
139
140
def test_run_oscap_remediate_create_chroot_dir(mock_subprocess, monkeypatch):
141
    mock_run_remediate(mock_subprocess, monkeypatch)
142
    common.run_oscap_remediate("myprofile", "my_ds.xml", chroot="/mnt/test")
143
144
    chroot_dir = "/mnt/test" + os.path.dirname(common.RESULTS_PATH)
145
    common.utils.ensure_dir_exists.assert_called_with(chroot_dir)
146