Passed
Pull Request — rhel9-branch (#185)
by Matěj
01:23 queued 21s
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
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 import common
27
from org_fedora_oscap.structures import PolicyData
28
29
# FIXME: Extend the tests to test all paths of the installation tasks.
30
31
32
@pytest.fixture()
33
def file_path():
34
    with tempfile.NamedTemporaryFile() as f:
35
        yield f.name
36
37
38
@pytest.fixture()
39
def content_path():
40
    with tempfile.TemporaryDirectory() as tmpdir:
41
        yield tmpdir
42
43
44
@pytest.fixture()
45
def tailoring_path():
46
    with tempfile.NamedTemporaryFile() as f:
47
        yield f.name
48
49
50
@pytest.fixture()
51
def sysroot_path():
52
    with tempfile.TemporaryDirectory() as tmpdir:
53
        yield tmpdir
54
55
56
@pytest.fixture()
57
def rule_evaluator(monkeypatch):
58
    mock = Mock(return_value=[])
59
    monkeypatch.setattr("org_fedora_oscap.rule_handling.RuleData.eval_rules", mock)
60
    return mock
61
62
63
def test_fetch_content_task(caplog, file_path, content_path):
64
    data = PolicyData()
65
    task = installation.PrepareValidContent(
66
        policy_data=data,
67
        file_path=file_path,
68
        content_path=content_path,
69
    )
70
71
    assert task.name == "Fetch the content, and optionally perform check or archive extraction"
72
73
    with pytest.raises(NonCriticalInstallationError, match="Couldn't find a valid datastream"):
74
        task.run()
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
    with pytest.raises(common.OSCAPaddonError, match="No such file")
120
        task.run()
121