Completed
Push — master ( 79f3e5...52b895 )
by Matěj
27s queued 14s
created

test_installation.tailoring_path()   A

Complexity

Conditions 2

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nop 0
dl 0
loc 4
rs 10
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
@pytest.fixture()
63
def mock_payload(monkeypatch):
64
    proxy = Mock()
65
    monkeypatch.setattr("org_fedora_oscap.common.get_payload_proxy", proxy)
66
    return proxy
67
68
69
def test_fetch_content_task(caplog, file_path, content_path):
70
    data = PolicyData()
71
    task = installation.PrepareValidContent(
72
        policy_data=data,
73
        file_path=file_path,
74
        content_path=content_path,
75
    )
76
77
    assert task.name == "Fetch the content, and optionally perform check or archive extraction"
78
79
    with pytest.raises(NonCriticalInstallationError, match="Couldn't find a valid datastream"):
80
        task.run()
81
82
83
def test_evaluate_rules_task(rule_evaluator, content_path, tailoring_path, mock_payload):
84
    data = PolicyData()
85
    task = installation.EvaluateRulesTask(
86
        policy_data=data,
87
        content_path=content_path,
88
        tailoring_path=tailoring_path
89
    )
90
91
    assert task.name == "Evaluate the rules"
92
    task.run()
93
94
    rule_evaluator.assert_called_once()
95
96
97
def test_install_content_task(sysroot_path, file_path, content_path, tailoring_path):
98
    data = PolicyData()
99
    data.content_type = "scap-security-guide"
100
101
    task = installation.InstallContentTask(
102
        sysroot=sysroot_path,
103
        policy_data=data,
104
        file_path=file_path,
105
        content_path=content_path,
106
        tailoring_path=tailoring_path,
107
        target_directory="target_dir"
108
    )
109
110
    assert task.name == "Install the content"
111
    task.run()
112
113
114
def test_remediate_system_task(sysroot_path, content_path, tailoring_path):
115
    data = PolicyData()
116
    task = installation.RemediateSystemTask(
117
        sysroot=sysroot_path,
118
        policy_data=data,
119
        target_content_path=content_path,
120
        target_tailoring_path=tailoring_path
121
    )
122
123
    assert task.name == "Remediate the system"
124
    task.run()
125