1
|
|
|
# Copyright 2022, Red Hat, Inc. |
2
|
|
|
# SPDX-License-Identifier: LGPL-2.1-or-later |
3
|
|
|
|
4
|
|
|
import subprocess |
5
|
|
|
import tempfile |
6
|
|
|
from pathlib import Path |
7
|
|
|
from unittest import mock |
8
|
|
|
|
9
|
|
|
import pytest |
10
|
|
|
|
11
|
|
|
from openscap_report.cli import CommandLineAPI |
12
|
|
|
from openscap_report.scap_results_parser import SCAPResultsParser |
13
|
|
|
|
14
|
|
|
from ..constants import PATH_TO_ARF, PATH_TO_EMPTY_FILE |
15
|
|
|
from ..test_utils import get_fake_args |
16
|
|
|
|
17
|
|
|
PATH_TO_RESULT_FILE = Path(tempfile.gettempdir()) / "oscap-report-tests_result.html" |
18
|
|
|
OSCAP_REPORT_COMMAND = "oscap-report" |
19
|
|
|
CAT_ARF_FILE = ["cat", str(PATH_TO_ARF)] |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
@pytest.mark.integration_test |
23
|
|
|
@mock.patch('argparse.ArgumentParser.parse_args', |
24
|
|
|
return_value=get_fake_args()) |
25
|
|
|
def test_generate_report(mock_args): # pylint: disable=W0613 |
26
|
|
|
data = None |
27
|
|
|
api = CommandLineAPI() |
28
|
|
|
with open(PATH_TO_ARF, "r", encoding="utf-8") as arf_report: |
29
|
|
|
parser = SCAPResultsParser(arf_report.read().encode()) |
30
|
|
|
data = api.generate_report(parser) |
31
|
|
|
assert data.read().decode("utf-8").startswith("<!DOCTYPE html>") |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
@pytest.mark.integration_test |
35
|
|
|
@pytest.mark.usefixtures("remove_generated_file") |
36
|
|
|
def test_command_with_input_from_stdin_and_output_to_stdout(): |
37
|
|
|
command_stdout = None |
38
|
|
|
with subprocess.Popen(CAT_ARF_FILE, stdout=subprocess.PIPE) as cat_command: |
39
|
|
|
command_stdout = subprocess.check_output(OSCAP_REPORT_COMMAND, stdin=cat_command.stdout) |
40
|
|
|
assert command_stdout.decode("utf-8").startswith("<!DOCTYPE html>") |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
@pytest.mark.integration_test |
44
|
|
|
@pytest.mark.usefixtures("remove_generated_file") |
45
|
|
|
@pytest.mark.parametrize("arguments, expected_start_string", [ |
46
|
|
|
( |
47
|
|
|
[], |
48
|
|
|
'<!DOCTYPE html><html lang="en" class="pf-m-redhat-font">' |
49
|
|
|
), |
50
|
|
|
( |
51
|
|
|
["-f", "HTML"], |
52
|
|
|
'<!DOCTYPE html><html lang="en" class="pf-m-redhat-font">' |
53
|
|
|
), |
54
|
|
|
( |
55
|
|
|
["-f", "JSON"], |
56
|
|
|
"{" |
57
|
|
|
) |
58
|
|
|
]) |
59
|
|
|
def test_command_with_different_formats(arguments, expected_start_string): |
60
|
|
|
command_stdout = None |
61
|
|
|
with subprocess.Popen(CAT_ARF_FILE, stdout=subprocess.PIPE) as cat_command: |
62
|
|
|
command_stdout = subprocess.check_output( |
63
|
|
|
[OSCAP_REPORT_COMMAND, *arguments], stdin=cat_command.stdout) |
64
|
|
|
assert command_stdout.decode("utf-8").startswith(expected_start_string) |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
@pytest.mark.integration_test |
68
|
|
|
@pytest.mark.usefixtures("remove_generated_file") |
69
|
|
|
def test_command_with_input_from_file_and_output_to_stdout(): |
70
|
|
|
command_stdout = subprocess.check_output([OSCAP_REPORT_COMMAND, str(PATH_TO_ARF)]) |
71
|
|
|
assert command_stdout.decode("utf-8").startswith("<!DOCTYPE html>") |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
@pytest.mark.integration_test |
75
|
|
|
@pytest.mark.usefixtures("remove_generated_file") |
76
|
|
|
def test_command_with_input_from_stdin_and_output_to_file(): |
77
|
|
|
command_stdout = None |
78
|
|
|
arguments = ["-o", str(PATH_TO_RESULT_FILE)] |
79
|
|
|
with subprocess.Popen(CAT_ARF_FILE, stdout=subprocess.PIPE) as cat_command: |
80
|
|
|
command_stdout = subprocess.check_output( |
81
|
|
|
[OSCAP_REPORT_COMMAND, *arguments], stdin=cat_command.stdout) |
82
|
|
|
assert not command_stdout.decode("utf-8") |
83
|
|
|
with open(PATH_TO_RESULT_FILE, "r", encoding="utf-8") as result_file: |
84
|
|
|
assert result_file.read().startswith("<!DOCTYPE html>") |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
@pytest.mark.integration_test |
88
|
|
|
@pytest.mark.usefixtures("remove_generated_file") |
89
|
|
|
def test_command_with_input_from_file_and_output_to_file(): |
90
|
|
|
arguments = [str(PATH_TO_ARF), "-o", str(PATH_TO_RESULT_FILE)] |
91
|
|
|
command_stdout = subprocess.check_output([OSCAP_REPORT_COMMAND, *arguments]) |
92
|
|
|
assert not command_stdout.decode("utf-8") |
93
|
|
|
with open(PATH_TO_RESULT_FILE, "r", encoding="utf-8") as result_file: |
94
|
|
|
assert result_file.read().startswith("<!DOCTYPE html>") |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
@pytest.mark.integration_test |
98
|
|
|
@pytest.mark.usefixtures("remove_generated_file") |
99
|
|
|
def test_logging_to_file(): |
100
|
|
|
log_file_path = Path(tempfile.gettempdir()) / "oscap-report-tests_log-file.log" |
101
|
|
|
command = [ |
102
|
|
|
OSCAP_REPORT_COMMAND, |
103
|
|
|
str(PATH_TO_ARF), |
104
|
|
|
"--log-level", "DEBUG", |
105
|
|
|
"--log-file", str(log_file_path) |
106
|
|
|
] |
107
|
|
|
command_stdout = subprocess.check_output(command) |
108
|
|
|
assert command_stdout.decode("utf-8").startswith("<!DOCTYPE html>") |
109
|
|
|
with open(log_file_path, "r", encoding="utf-8") as result_file: |
110
|
|
|
assert result_file.read().startswith("DEBUG:") |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
@pytest.mark.integration_test |
114
|
|
|
def test_command_with_empty_input_file(): |
115
|
|
|
arguments = [str(PATH_TO_EMPTY_FILE)] |
116
|
|
|
with subprocess.Popen([OSCAP_REPORT_COMMAND, *arguments], stderr=subprocess.PIPE) as command: |
117
|
|
|
command.wait() |
118
|
|
|
assert command.returncode == 1 |
119
|
|
|
std_err = command.stderr.read().decode("utf-8") |
120
|
|
|
assert std_err.startswith("CRITICAL:") |
121
|
|
|
assert "empty" in std_err |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
@pytest.mark.integration_test |
125
|
|
|
def test_command_with_empty_stdin(): |
126
|
|
|
cat_command_args = ["cat", str(PATH_TO_EMPTY_FILE)] |
127
|
|
|
with subprocess.Popen(cat_command_args, stdout=subprocess.PIPE) as cat_command: |
128
|
|
|
with subprocess.Popen( |
129
|
|
|
OSCAP_REPORT_COMMAND, |
130
|
|
|
stdin=cat_command.stdout, |
131
|
|
|
stderr=subprocess.PIPE) as command: |
132
|
|
|
command.wait() |
133
|
|
|
assert command.returncode == 1 |
134
|
|
|
std_err = command.stderr.read().decode("utf-8") |
135
|
|
|
assert std_err.startswith("CRITICAL:") |
136
|
|
|
assert "empty" in std_err |
137
|
|
|
|