Passed
Pull Request — rhel8-branch (#134)
by Matěj
01:08
created

test_installation.test_install_content_task()   A

Complexity

Conditions 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nop 4
dl 0
loc 15
rs 9.8
c 0
b 0
f 0
1
#
2
# Copyright (C) 2021  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
import logging
19
import tempfile
20
import pytest
21
22
from unittest.mock import Mock
23
24
from org_fedora_oscap.service import installation
25
from org_fedora_oscap.structures import PolicyData
26
27
# FIXME: Extend the tests to test all paths of the installation tasks.
28
29
30
@pytest.fixture()
31
def file_path():
32
    with tempfile.NamedTemporaryFile() as f:
33
        yield f.name
34
35
36
@pytest.fixture()
37
def content_path():
38
    with tempfile.TemporaryDirectory() as tmpdir:
39
        yield tmpdir
40
41
42
@pytest.fixture()
43
def tailoring_path():
44
    with tempfile.NamedTemporaryFile() as f:
45
        yield f.name
46
47
48
@pytest.fixture()
49
def sysroot_path():
50
    with tempfile.TemporaryDirectory() as tmpdir:
51
        yield tmpdir
52
53
54
@pytest.fixture()
55
def rule_evaluator(monkeypatch):
56
    mock = Mock(return_value=[])
57
    monkeypatch.setattr("org_fedora_oscap.rule_handling.RuleData.eval_rules", mock)
58
    return mock
59
60
61
def test_fetch_content_task(caplog, file_path, content_path):
62
    data = PolicyData()
63
    task = installation.FetchContentTask(
64
        policy_data=data,
65
        file_path=file_path,
66
        content_path=content_path,
67
    )
68
69
    assert task.name == "Fetch the content"
70
71
    with caplog.at_level(logging.DEBUG):
72
        task.run()
73
74
    assert "Content is already available. Skip." in caplog.text
75
76
77
def test_check_fingerprint_task(caplog, file_path):
78
    data = PolicyData()
79
    task = installation.CheckFingerprintTask(
80
        policy_data=data,
81
        file_path=file_path
82
    )
83
84
    assert task.name == "Check the fingerprint"
85
86
    with caplog.at_level(logging.DEBUG):
87
        task.run()
88
89
    assert "No fingerprint is provided. Skip." in caplog.text
90
91
92
def test_evaluate_rules_task(rule_evaluator, content_path, tailoring_path):
93
    data = PolicyData()
94
    task = installation.EvaluateRulesTask(
95
        policy_data=data,
96
        content_path=content_path,
97
        tailoring_path=tailoring_path
98
    )
99
100
    assert task.name == "Evaluate the rules"
101
    task.run()
102
103
    rule_evaluator.assert_called_once()
104
105
106
def test_install_content_task(sysroot_path, file_path, content_path, tailoring_path):
107
    data = PolicyData()
108
    data.content_type = "scap-security-guide"
109
110
    task = installation.InstallContentTask(
111
        sysroot=sysroot_path,
112
        policy_data=data,
113
        file_path=file_path,
114
        content_path=content_path,
115
        tailoring_path=tailoring_path,
116
        target_directory="target_dir"
117
    )
118
119
    assert task.name == "Install the content"
120
    task.run()
121
122
123
def test_remediate_system_task(sysroot_path, content_path, tailoring_path):
124
    data = PolicyData()
125
    task = installation.RemediateSystemTask(
126
        sysroot=sysroot_path,
127
        policy_data=data,
128
        target_content_path=content_path,
129
        target_tailoring_path=tailoring_path
130
    )
131
132
    assert task.name == "Remediate the system"
133
    task.run()
134