Passed
Pull Request — rhel9-branch (#159)
by Matěj
01:02
created

test_installation.test_check_fingerprint_task()   A

Complexity

Conditions 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nop 2
dl 0
loc 13
rs 9.95
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.PrepareValidContent(
64
        policy_data=data,
65
        file_path=file_path,
66
        content_path=content_path,
67
    )
68
69
    assert task.name == "Fetch the content, and optionally perform check or archive extraction"
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
@pytest.mark.skip(reason="Test seems to require Anaconda listening on dbus")
78
def test_evaluate_rules_task(rule_evaluator, content_path, tailoring_path):
79
    data = PolicyData()
80
    task = installation.EvaluateRulesTask(
81
        policy_data=data,
82
        content_path=content_path,
83
        tailoring_path=tailoring_path
84
    )
85
86
    assert task.name == "Evaluate the rules"
87
    task.run()
88
89
    rule_evaluator.assert_called_once()
90
91
92
def test_install_content_task(sysroot_path, file_path, content_path, tailoring_path):
93
    data = PolicyData()
94
    data.content_type = "scap-security-guide"
95
96
    task = installation.InstallContentTask(
97
        sysroot=sysroot_path,
98
        policy_data=data,
99
        file_path=file_path,
100
        content_path=content_path,
101
        tailoring_path=tailoring_path,
102
        target_directory="target_dir"
103
    )
104
105
    assert task.name == "Install the content"
106
    task.run()
107
108
109
def test_remediate_system_task(sysroot_path, content_path, tailoring_path):
110
    data = PolicyData()
111
    task = installation.RemediateSystemTask(
112
        sysroot=sysroot_path,
113
        policy_data=data,
114
        target_content_path=content_path,
115
        target_tailoring_path=tailoring_path
116
    )
117
118
    assert task.name == "Remediate the system"
119
    task.run()
120