1
|
1 |
|
import argparse |
2
|
1 |
|
import subprocess |
3
|
1 |
|
import tempfile |
4
|
1 |
|
from io import BytesIO |
5
|
1 |
|
from pathlib import Path |
6
|
1 |
|
from unittest import mock |
7
|
|
|
|
8
|
1 |
|
import pytest |
9
|
|
|
|
10
|
1 |
|
from openscap_report.cli import CommandLineAPI |
11
|
1 |
|
from openscap_report.scap_results_parser.scap_results_parser import \ |
12
|
|
|
SCAPResultsParser |
13
|
|
|
|
14
|
1 |
|
from ..constants import PATH_TO_ARF, PATH_TO_EMPTY_FILE |
15
|
|
|
|
16
|
1 |
|
PATH_TO_RESULT_FILE = Path(tempfile.gettempdir()) / "oscap-report-tests_result.html" |
17
|
1 |
|
OSCAP_REPORT_COMMAND = "oscap-report" |
18
|
1 |
|
CAT_ARF_FILE = ["cat", str(PATH_TO_ARF)] |
19
|
|
|
|
20
|
|
|
|
21
|
1 |
|
def get_fake_args(): |
22
|
|
|
# pylint: disable=bad-option-value,R1732 |
23
|
1 |
|
input_file = open(PATH_TO_ARF, "r", encoding="utf-8") |
24
|
1 |
|
output_file = open(PATH_TO_RESULT_FILE, "wb") |
25
|
1 |
|
return argparse.Namespace( |
26
|
|
|
FILE=input_file, output=output_file, |
27
|
|
|
log_file=None, log_level="WARNING", format="HTML", |
28
|
|
|
debug=[""], |
29
|
|
|
) |
30
|
|
|
|
31
|
|
|
|
32
|
1 |
|
@mock.patch('argparse.ArgumentParser.parse_args', |
33
|
|
|
return_value=get_fake_args()) |
34
|
1 |
|
def test_load_file(mock_args): # pylint: disable=W0613 |
35
|
|
|
api = CommandLineAPI() |
36
|
|
|
xml_report = api.load_file() |
37
|
|
|
parser = SCAPResultsParser(xml_report) |
38
|
|
|
assert parser.validate(parser.arf_schemas_path) |
39
|
|
|
api.close_files() |
40
|
|
|
|
41
|
|
|
|
42
|
1 |
|
@pytest.mark.usefixtures("remove_generated_file") |
43
|
1 |
|
@mock.patch('argparse.ArgumentParser.parse_args', |
44
|
|
|
return_value=get_fake_args()) |
45
|
1 |
|
def test_store_file(mock_args): # pylint: disable=W0613 |
46
|
|
|
api = CommandLineAPI() |
47
|
|
|
data = BytesIO(b'<html><h1>TEST DATA</h1></html>') |
48
|
|
|
api.store_file(data) |
49
|
|
|
api.close_files() |
50
|
|
|
with open(PATH_TO_RESULT_FILE, "r", encoding="utf-8") as result_file: |
51
|
|
|
assert result_file.read() == data.getvalue().decode("utf-8") |
52
|
|
|
|
53
|
|
|
|
54
|
1 |
|
@mock.patch('argparse.ArgumentParser.parse_args', |
55
|
|
|
return_value=get_fake_args()) |
56
|
1 |
|
def test_generate_report(mock_args): # pylint: disable=W0613 |
57
|
|
|
data = None |
58
|
|
|
api = CommandLineAPI() |
59
|
|
|
with open(PATH_TO_ARF, "r", encoding="utf-8") as arf_report: |
60
|
|
|
parser = SCAPResultsParser(arf_report.read().encode()) |
61
|
|
|
data = api.generate_report(parser) |
62
|
|
|
assert data.read().decode("utf-8").startswith("<!DOCTYPE html>") |
63
|
|
|
|
64
|
|
|
|
65
|
1 |
|
@pytest.mark.usefixtures("remove_generated_file") |
66
|
1 |
|
def test_command_with_input_from_stdin_and_output_to_stdout(): |
67
|
|
|
command_stdout = None |
68
|
|
|
with subprocess.Popen(CAT_ARF_FILE, stdout=subprocess.PIPE) as cat_command: |
69
|
|
|
command_stdout = subprocess.check_output(OSCAP_REPORT_COMMAND, stdin=cat_command.stdout) |
70
|
|
|
assert command_stdout.decode("utf-8").startswith("<!DOCTYPE html>") |
71
|
|
|
|
72
|
|
|
|
73
|
1 |
|
@pytest.mark.usefixtures("remove_generated_file") |
74
|
1 |
|
def test_command_with_input_from_file_and_output_to_stdout(): |
75
|
|
|
command_stdout = subprocess.check_output([OSCAP_REPORT_COMMAND, str(PATH_TO_ARF)]) |
76
|
|
|
assert command_stdout.decode("utf-8").startswith("<!DOCTYPE html>") |
77
|
|
|
|
78
|
|
|
|
79
|
1 |
|
@pytest.mark.usefixtures("remove_generated_file") |
80
|
1 |
|
def test_command_with_input_from_stdin_and_output_to_file(): |
81
|
|
|
command_stdout = None |
82
|
|
|
arguments = ["-o", str(PATH_TO_RESULT_FILE)] |
83
|
|
|
with subprocess.Popen(CAT_ARF_FILE, stdout=subprocess.PIPE) as cat_command: |
84
|
|
|
command_stdout = subprocess.check_output( |
85
|
|
|
[OSCAP_REPORT_COMMAND, *arguments], stdin=cat_command.stdout) |
86
|
|
|
assert not command_stdout.decode("utf-8") |
87
|
|
|
with open(PATH_TO_RESULT_FILE, "r", encoding="utf-8") as result_file: |
88
|
|
|
assert result_file.read().startswith("<!DOCTYPE html>") |
89
|
|
|
|
90
|
|
|
|
91
|
1 |
|
@pytest.mark.usefixtures("remove_generated_file") |
92
|
1 |
|
def test_command_with_input_from_file_and_output_to_file(): |
93
|
|
|
arguments = [str(PATH_TO_ARF), "-o", str(PATH_TO_RESULT_FILE)] |
94
|
|
|
command_stdout = subprocess.check_output([OSCAP_REPORT_COMMAND, *arguments]) |
95
|
|
|
assert not command_stdout.decode("utf-8") |
96
|
|
|
with open(PATH_TO_RESULT_FILE, "r", encoding="utf-8") as result_file: |
97
|
|
|
assert result_file.read().startswith("<!DOCTYPE html>") |
98
|
|
|
|
99
|
|
|
|
100
|
1 |
|
@pytest.mark.usefixtures("remove_generated_file") |
101
|
1 |
|
def test_logging_to_file(): |
102
|
|
|
log_file_path = Path(tempfile.gettempdir()) / "oscap-report-tests_log-file.log" |
103
|
|
|
command = [ |
104
|
|
|
OSCAP_REPORT_COMMAND, |
105
|
|
|
str(PATH_TO_ARF), |
106
|
|
|
"--log-level", "DEBUG", |
107
|
|
|
"--log-file", str(log_file_path) |
108
|
|
|
] |
109
|
|
|
command_stdout = subprocess.check_output(command) |
110
|
|
|
assert command_stdout.decode("utf-8").startswith("<!DOCTYPE html>") |
111
|
|
|
with open(log_file_path, "r", encoding="utf-8") as result_file: |
112
|
|
|
assert result_file.read().startswith("DEBUG:") |
113
|
|
|
|
114
|
|
|
|
115
|
1 |
|
def test_command_with_empty_input_file(): |
116
|
|
|
arguments = [str(PATH_TO_EMPTY_FILE)] |
117
|
|
|
with subprocess.Popen([OSCAP_REPORT_COMMAND, *arguments], stderr=subprocess.PIPE) as command: |
118
|
|
|
command.wait() |
119
|
|
|
assert command.returncode == 1 |
120
|
|
|
std_err = command.stderr.read().decode("utf-8") |
121
|
|
|
assert std_err.startswith("CRITICAL:") |
122
|
|
|
assert "empty" in std_err |
123
|
|
|
|
124
|
|
|
|
125
|
1 |
|
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
|
|
|
|