Completed
Push — rhel9-branch ( 56a153...16c604 )
by Jan
02:45 queued 02:40
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
from unittest.mock import Mock
22
23
from pyanaconda.modules.common.errors.installation import NonCriticalInstallationError
24
25
from org_fedora_oscap.service import installation
26
from org_fedora_oscap.structures import PolicyData
27
28
# FIXME: Extend the tests to test all paths of the installation tasks.
29
30
31
@pytest.fixture()
32
def file_path():
33
    with tempfile.NamedTemporaryFile() as f:
34
        yield f.name
35
36
37
@pytest.fixture()
38
def content_path():
39
    with tempfile.TemporaryDirectory() as tmpdir:
40
        yield tmpdir
41
42
43
@pytest.fixture()
44
def tailoring_path():
45
    with tempfile.NamedTemporaryFile() as f:
46
        yield f.name
47
48
49
@pytest.fixture()
50
def sysroot_path():
51
    with tempfile.TemporaryDirectory() as tmpdir:
52
        yield tmpdir
53
54
55
@pytest.fixture()
56
def rule_evaluator(monkeypatch):
57
    mock = Mock(return_value=[])
58
    monkeypatch.setattr("org_fedora_oscap.rule_handling.RuleData.eval_rules", mock)
59
    return mock
60
61
62
def test_fetch_content_task(caplog, file_path, content_path):
63
    data = PolicyData()
64
    task = installation.PrepareValidContent(
65
        policy_data=data,
66
        file_path=file_path,
67
        content_path=content_path,
68
    )
69
70
    assert task.name == "Fetch the content, and optionally perform check or archive extraction"
71
72
    with pytest.raises(NonCriticalInstallationError, match="Couldn't find a valid datastream"):
73
        task.run()
74
75
76
@pytest.mark.skip(reason="Test seems to require Anaconda listening on dbus")
77
def test_evaluate_rules_task(rule_evaluator, content_path, tailoring_path):
78
    data = PolicyData()
79
    task = installation.EvaluateRulesTask(
80
        policy_data=data,
81
        content_path=content_path,
82
        tailoring_path=tailoring_path
83
    )
84
85
    assert task.name == "Evaluate the rules"
86
    task.run()
87
88
    rule_evaluator.assert_called_once()
89
90
91
def test_install_content_task(sysroot_path, file_path, content_path, tailoring_path):
92
    data = PolicyData()
93
    data.content_type = "scap-security-guide"
94
95
    task = installation.InstallContentTask(
96
        sysroot=sysroot_path,
97
        policy_data=data,
98
        file_path=file_path,
99
        content_path=content_path,
100
        tailoring_path=tailoring_path,
101
        target_directory="target_dir"
102
    )
103
104
    assert task.name == "Install the content"
105
    task.run()
106
107
108
def test_remediate_system_task(sysroot_path, content_path, tailoring_path):
109
    data = PolicyData()
110
    task = installation.RemediateSystemTask(
111
        sysroot=sysroot_path,
112
        policy_data=data,
113
        target_content_path=content_path,
114
        target_tailoring_path=tailoring_path
115
    )
116
117
    assert task.name == "Remediate the system"
118
    task.run()
119