Passed
Pull Request — main (#137)
by Jan
07:10
created

tests.test_utils.get_ref_values()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
# Copyright 2022, Red Hat, Inc.
2
# SPDX-License-Identifier: LGPL-2.1-or-later
3
4
from dataclasses import replace
5
from functools import cache
6
7
from lxml import etree
8
9
from openscap_report.scap_results_parser import SCAPResultsParser
10
from openscap_report.scap_results_parser.namespaces import NAMESPACES
11
from openscap_report.scap_results_parser.parsers import RuleParser
12
13
from .constants import (PATH_TO_ARF,
14
                        PATH_TO_ARF_REPRODUCING_DANGLING_REFERENCE_TO)
15
16
17
@cache
18
def get_xml_data(file_path):
19
    with open(file_path, "r", encoding="utf-8") as xml_report:
20
        return xml_report.read().encode()
21
22
23
def get_parser(file_path):
24
    xml_data = get_xml_data(file_path)
25
    return SCAPResultsParser(xml_data)
26
27
28
BASIC_REPORT = get_parser(PATH_TO_ARF).parse_report()
29
30
31
def get_report(file_path=None):
32
    if file_path is None:
33
        return replace(BASIC_REPORT)
34
    return get_parser(file_path).parse_report()
35
36
37
REPORT_REPRODUCING_DANGLING_REFERENCE_TO = get_report(PATH_TO_ARF_REPRODUCING_DANGLING_REFERENCE_TO)
38
39
40
def get_root(file_path):
41
    xml_data = get_xml_data(file_path)
42
    return etree.XML(xml_data)
43
44
45
def get_benchmark(root):
46
    benchmark_el = root.find(".//xccdf:Benchmark", NAMESPACES)
47
    if "Benchmark" in root.tag:
48
        return root
49
    return benchmark_el
50
51
52
def get_test_results(root):
53
    return root.find('.//xccdf:TestResult', NAMESPACES)
54
55
56
def get_ref_values(root):
57
    return {
58
        ref_value.get("idref"): ref_value.text
59
        for ref_value in root.findall('.//xccdf:set-value', NAMESPACES)
60
    }
61
62
63
def get_rules(file_path=PATH_TO_ARF):
64
    root = get_root(file_path)
65
    test_results = get_test_results(root)
66
    ref_values = get_ref_values(root)
67
    rule_parser = RuleParser(root, test_results, ref_values)
68
    return rule_parser.get_rules()
69
70
71
DEFAULT_RULES = get_rules()
72