Passed
Pull Request — main (#152)
by Jan
05:54
created

tests.integration_tests.test_cli.test_load_file()   A

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nop 1
dl 0
loc 9
rs 9.95
c 0
b 0
f 0
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", "OLD-STYLE-HTML"],
56
        '<!DOCTYPE html>\n<html lang="en">'
57
    ),
58
    (
59
        ["-f", "JSON"],
60
        "{"
61
    )
62
])
63
def test_command_with_different_formats(arguments, expected_start_string):
64
    command_stdout = None
65
    with subprocess.Popen(CAT_ARF_FILE, stdout=subprocess.PIPE) as cat_command:
66
        command_stdout = subprocess.check_output(
67
            [OSCAP_REPORT_COMMAND, *arguments], stdin=cat_command.stdout)
68
    assert command_stdout.decode("utf-8").startswith(expected_start_string)
69
70
71
@pytest.mark.integration_test
72
@pytest.mark.usefixtures("remove_generated_file")
73
def test_command_with_input_from_file_and_output_to_stdout():
74
    command_stdout = subprocess.check_output([OSCAP_REPORT_COMMAND, str(PATH_TO_ARF)])
75
    assert command_stdout.decode("utf-8").startswith("<!DOCTYPE html>")
76
77
78
@pytest.mark.integration_test
79
@pytest.mark.usefixtures("remove_generated_file")
80
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
@pytest.mark.integration_test
92
@pytest.mark.usefixtures("remove_generated_file")
93
def test_command_with_input_from_file_and_output_to_file():
94
    arguments = [str(PATH_TO_ARF), "-o", str(PATH_TO_RESULT_FILE)]
95
    command_stdout = subprocess.check_output([OSCAP_REPORT_COMMAND, *arguments])
96
    assert not command_stdout.decode("utf-8")
97
    with open(PATH_TO_RESULT_FILE, "r", encoding="utf-8") as result_file:
98
        assert result_file.read().startswith("<!DOCTYPE html>")
99
100
101
@pytest.mark.integration_test
102
@pytest.mark.usefixtures("remove_generated_file")
103
def test_logging_to_file():
104
    log_file_path = Path(tempfile.gettempdir()) / "oscap-report-tests_log-file.log"
105
    command = [
106
        OSCAP_REPORT_COMMAND,
107
        str(PATH_TO_ARF),
108
        "--log-level", "DEBUG",
109
        "--log-file", str(log_file_path)
110
    ]
111
    command_stdout = subprocess.check_output(command)
112
    assert command_stdout.decode("utf-8").startswith("<!DOCTYPE html>")
113
    with open(log_file_path, "r", encoding="utf-8") as result_file:
114
        assert result_file.read().startswith("DEBUG:")
115
116
117
@pytest.mark.integration_test
118
def test_command_with_empty_input_file():
119
    arguments = [str(PATH_TO_EMPTY_FILE)]
120
    with subprocess.Popen([OSCAP_REPORT_COMMAND, *arguments], stderr=subprocess.PIPE) as command:
121
        command.wait()
122
        assert command.returncode == 1
123
        std_err = command.stderr.read().decode("utf-8")
124
        assert std_err.startswith("CRITICAL:")
125
        assert "empty" in std_err
126
127
128
@pytest.mark.integration_test
129
def test_command_with_empty_stdin():
130
    cat_command_args = ["cat", str(PATH_TO_EMPTY_FILE)]
131
    with subprocess.Popen(cat_command_args, stdout=subprocess.PIPE) as cat_command:
132
        with subprocess.Popen(
133
                OSCAP_REPORT_COMMAND,
134
                stdin=cat_command.stdout,
135
                stderr=subprocess.PIPE) as command:
136
            command.wait()
137
            assert command.returncode == 1
138
            std_err = command.stderr.read().decode("utf-8")
139
            assert std_err.startswith("CRITICAL:")
140
            assert "empty" in std_err
141