1
|
|
|
import json |
2
|
|
|
import os |
3
|
|
|
import re |
4
|
|
|
import tempfile |
5
|
|
|
import uuid |
6
|
|
|
from pathlib import Path |
7
|
|
|
|
8
|
|
|
import pytest |
9
|
|
|
|
10
|
|
|
TOP_PATH = Path(__file__).parent |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class TestTools(): |
14
|
|
|
@staticmethod |
15
|
|
|
def find_files(file_name, search_path): |
16
|
|
|
result = [] |
17
|
|
|
# root, directory, files |
18
|
|
|
for root, _, files in os.walk(search_path): |
19
|
|
|
for filename in files: |
20
|
|
|
if file_name in filename: |
21
|
|
|
result.append(os.path.abspath(os.path.join(root, filename))) |
22
|
|
|
return result |
23
|
|
|
|
24
|
|
|
@staticmethod |
25
|
|
|
def get_text_file(src): |
26
|
|
|
path = TOP_PATH / src |
27
|
|
|
with open(path, 'r') as data: |
28
|
|
|
return data.readlines() |
29
|
|
|
|
30
|
|
|
@staticmethod |
31
|
|
|
def get_random_dir_in_tmp(): |
32
|
|
|
return os.path.join(tempfile.gettempdir(), str(uuid.uuid4())) |
33
|
|
|
|
34
|
|
View Code Duplication |
@staticmethod |
|
|
|
|
35
|
|
|
def compare_results_html(result): |
36
|
|
|
result_ = TestTools.get_text_file(result) |
37
|
|
|
reference_pattern = TestTools.get_text_file( |
38
|
|
|
'test_commands/test_data/referenc_pattern_html_report.txt') |
39
|
|
|
prefix_start = '<script>var data_of_tree = ' |
40
|
|
|
prefix_end = ';</script><div>\n' |
41
|
|
|
data_in_html = "" |
42
|
|
|
matched = False |
43
|
|
|
for row in result_: |
44
|
|
|
if prefix_start in row and prefix_end in row: |
45
|
|
|
matched = True |
46
|
|
|
data_in_html = row |
47
|
|
|
break |
48
|
|
|
assert matched |
49
|
|
|
|
50
|
|
|
tmp_json_str = data_in_html.replace(prefix_start, '').replace(prefix_end, '') |
51
|
|
|
tmp_json = json.loads(tmp_json_str) |
52
|
|
|
data_in_html = prefix_start + json.dumps(tmp_json, indent=4, sort_keys=False) + prefix_end |
53
|
|
|
|
54
|
|
|
count_row = 0 |
55
|
|
|
rule_name = 'xccdforgssgprojectcontentrulepackageabrtremoved' |
56
|
|
|
for row in reference_pattern: |
57
|
|
|
if row in data_in_html or rule_name in row: |
58
|
|
|
count_row += 1 |
59
|
|
|
assert count_row == len(reference_pattern) |
60
|
|
|
|
61
|
|
|
@staticmethod |
62
|
|
|
def get_data_json(src): |
63
|
|
|
path = TOP_PATH / src |
64
|
|
|
with open(path, 'r') as data: |
65
|
|
|
return json.load(data) |
66
|
|
|
|
67
|
|
|
@staticmethod |
68
|
|
|
def compare_results_json(result): |
69
|
|
|
result = TestTools.get_data_json(result) |
70
|
|
|
reference_result = TestTools.get_data_json( |
71
|
|
|
'test_commands/test_data/referenc_result_data_json.json') |
72
|
|
|
rule_name = "xccdf_org.ssgproject.content_rule_package_abrt_removed" |
73
|
|
|
result_rule_name = [ |
74
|
|
|
x for x in result.keys() if re.search( |
75
|
|
|
rule_name, x)] |
76
|
|
|
assert result[result_rule_name[0]] == reference_result[rule_name] |
77
|
|
|
|
78
|
|
|
@staticmethod |
79
|
|
|
def find_all_in_string(regex, count, string): |
80
|
|
|
assert len(re.findall(regex, string)) == count |
81
|
|
|
|
82
|
|
|
@staticmethod |
83
|
|
|
def get_questions_not_selected(capsys, client, result): |
84
|
|
|
out = client.get_questions()[0].choices |
85
|
|
|
assert out == result |
86
|
|
|
captured = capsys.readouterr() |
87
|
|
|
regex = r'rule_package_\w+_removed +\(Not selected\)' |
88
|
|
|
TestTools.find_all_in_string(regex, 6, captured.out) |
89
|
|
|
|
90
|
|
|
@staticmethod |
91
|
|
|
def get_questions_with_option_show_failed_rules(client): |
92
|
|
|
out = client.get_questions()[0].choices |
93
|
|
|
rule1 = 'xccdf_org.ssgproject.content_rule_package_abrt_removed' |
94
|
|
|
assert out[0] == rule1 |
95
|
|
|
with pytest.raises(Exception, match="list index out of range"): |
96
|
|
|
assert out[2] is None |
97
|
|
|
|