1
|
|
|
import pytest |
2
|
|
|
import tempfile |
3
|
|
|
import os |
4
|
|
|
import uuid |
5
|
|
|
import json |
6
|
|
|
import mock |
7
|
|
|
import sys |
8
|
|
|
from datetime import datetime |
9
|
|
|
import time |
10
|
|
|
|
11
|
|
|
from oval_graph.arf_to_json import ArfToJson |
12
|
|
|
import tests.any_test_help |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
def get_client_arf_to_json(src, rule): |
16
|
|
|
return ArfToJson( |
17
|
|
|
[tests.any_test_help.get_src(src), rule]) |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
def get_client_arf_to_json_with_define_dest(src, rule, out_src=None): |
21
|
|
|
out_src = str(uuid.uuid4()) + ".json" if out_src is None else out_src |
22
|
|
|
return ArfToJson(["--output", |
23
|
|
|
tests.any_test_help.get_src(os.path.join(tempfile.gettempdir(), out_src)), |
24
|
|
|
tests.any_test_help.get_src(src), |
25
|
|
|
rule]) |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
def get_client_arf_to_json_with_option_show_failed_rules(src, rule): |
29
|
|
|
return ArfToJson(["--show-failed-rules", |
30
|
|
|
tests.any_test_help.get_src(src), rule]) |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
def get_client_arf_to_json_with_option_show_not_selected_rules(src, rule): |
34
|
|
|
return ArfToJson(["--show-not-selected-rules", |
35
|
|
|
tests.any_test_help.get_src(src), |
36
|
|
|
rule]) |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
def get_client_arf_to_json_with_option_show_not_selected_rules_and_show_failed_rules( |
40
|
|
|
src, |
41
|
|
|
rule): |
42
|
|
|
return ArfToJson(["--show-not-selected-rules", |
43
|
|
|
"--show-failed-rules", |
44
|
|
|
tests.any_test_help.get_src(src), |
45
|
|
|
rule]) |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
def try_expection_for_prepare_graph(src, rule, err): |
49
|
|
|
client = get_client_arf_to_json(src, rule) |
50
|
|
|
rules = {'rules': [rule]} |
51
|
|
|
with pytest.raises(Exception, match=err): |
52
|
|
|
assert client.prepare_data(rules) |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
def test_prepare_graph_with_non_existent_rule(): |
56
|
|
|
src = 'test_data/ssg-fedora-ds-arf.xml' |
57
|
|
|
rule = 'non-existent_rule' |
58
|
|
|
try_expection_for_prepare_graph(src, rule, '404') |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
def test_prepare_graph_with_not_selected_rule(): |
62
|
|
|
src = 'test_data/ssg-fedora-ds-arf.xml' |
63
|
|
|
rule = 'xccdf_org.ssgproject.content_rule_package_nis_removed' |
64
|
|
|
try_expection_for_prepare_graph(src, rule, 'not selected') |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
def test_prepare_json(capsys): |
68
|
|
|
src = 'test_data/ssg-fedora-ds-arf.xml' |
69
|
|
|
rule = 'xccdf_org.ssgproject.content_rule_package_abrt_removed' |
70
|
|
|
client = get_client_arf_to_json(src, rule) |
71
|
|
|
rules = {'rules': [rule]} |
72
|
|
|
results_src = client.prepare_data(rules) |
73
|
|
|
assert not results_src |
74
|
|
|
captured = capsys.readouterr() |
75
|
|
|
assert captured.out == ( |
76
|
|
|
'{\n' |
77
|
|
|
' "graph-of-xccdf_org.ssgproject.content_rule_package_abrt_removed' + |
78
|
|
|
client.date + |
79
|
|
|
'": {\n' |
80
|
|
|
' "node_id": "xccdf_org.ssgproject.content_rule_package_abrt_removed",\n' |
81
|
|
|
' "type": "operator",\n' |
82
|
|
|
' "value": "and",\n' |
83
|
|
|
' "negation": false,\n' |
84
|
|
|
' "comment": "Package abrt Removed",\n' |
85
|
|
|
' "tag": "Rule",\n' |
86
|
|
|
' "test_result_details": null,\n' |
87
|
|
|
' "child": [\n' |
88
|
|
|
' {\n' |
89
|
|
|
' "node_id": "oval:ssg-package_abrt_removed:def:1",\n' |
90
|
|
|
' "type": "operator",\n' |
91
|
|
|
' "value": "and",\n' |
92
|
|
|
' "negation": false,\n' |
93
|
|
|
' "comment": "The RPM package abrt should be removed.",\n' |
94
|
|
|
' "tag": "Definition",\n' |
95
|
|
|
' "test_result_details": null,\n' |
96
|
|
|
' "child": [\n' |
97
|
|
|
' {\n' |
98
|
|
|
' "node_id": "oval:ssg-test_package_abrt_removed:tst:1",\n' |
99
|
|
|
' "type": "value",\n' |
100
|
|
|
' "value": "false",\n' |
101
|
|
|
' "negation": false,\n' |
102
|
|
|
' "comment": "package abrt is removed",\n' |
103
|
|
|
' "tag": "Test",\n' |
104
|
|
|
' "test_result_details": {\n' |
105
|
|
|
' "id": "oval:ssg-test_package_abrt_removed:tst:1",\n' |
106
|
|
|
' "comment": "package abrt is removed",\n' |
107
|
|
|
' "objects": [\n' |
108
|
|
|
' {\n' |
109
|
|
|
' "oval:ssg-obj_package_abrt_removed:obj:1":' |
110
|
|
|
' "complete",\n' |
111
|
|
|
' "rpminfo_object": {\n' |
112
|
|
|
' "name": "abrt",\n' |
113
|
|
|
' "arch": "x86_64",\n' |
114
|
|
|
' "epoch": "(none)",\n' |
115
|
|
|
' "release": "2.fc30",\n' |
116
|
|
|
' "version": "2.12.0",\n' |
117
|
|
|
' "evr": "0:2.12.0-2.fc30",\n' |
118
|
|
|
' "signature_keyid": "ef3c111fcfc659b9",\n' |
119
|
|
|
' "extended_name":' |
120
|
|
|
' "abrt-0:2.12.0-2.fc30.x86_64"\n' |
121
|
|
|
' }\n' |
122
|
|
|
' }\n' |
123
|
|
|
' ]\n' |
124
|
|
|
' },\n' |
125
|
|
|
' "child": null\n' |
126
|
|
|
' }\n' |
127
|
|
|
' ]\n' |
128
|
|
|
' }\n' |
129
|
|
|
' ]\n' |
130
|
|
|
' }\n' |
131
|
|
|
'}\n') |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
def test_prepare_json_and_save_in_defined_destination(): |
135
|
|
|
src = 'test_data/ssg-fedora-ds-arf.xml' |
136
|
|
|
rule = 'xccdf_org.ssgproject.content_rule_package_abrt_removed' |
137
|
|
|
client = get_client_arf_to_json_with_define_dest(src, rule) |
138
|
|
|
rules = {'rules': [rule]} |
139
|
|
|
results_src = client.prepare_data(rules) |
140
|
|
|
tests.any_test_help.compare_results_json(results_src[0]) |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
def test_is_empty_file(): |
144
|
|
|
src = 'test_data/ssg-fedora-ds-arf.xml' |
145
|
|
|
rule = 'xccdf_org.ssgproject.content_rule_package_abrt_removed' |
146
|
|
|
client = get_client_arf_to_json_with_define_dest(src, rule) |
147
|
|
|
empty_file_src = tests.any_test_help.get_src('test_data/empty_file.json') |
148
|
|
|
assert client.file_is_empty(empty_file_src) |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
def test_if_file_has_content(): |
152
|
|
|
src = 'test_data/ssg-fedora-ds-arf.xml' |
153
|
|
|
rule = 'xccdf_org.ssgproject.content_rule_package_abrt_removed' |
154
|
|
|
client = get_client_arf_to_json_with_define_dest(src, rule) |
155
|
|
|
file_src = tests.any_test_help.get_src('test_data/JsTree_json0.json') |
156
|
|
|
assert not client.file_is_empty(file_src) |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
def test_creation_json_with_two_more_rule(): |
160
|
|
|
src = 'test_data/ssg-fedora-ds-arf.xml' |
161
|
|
|
rule = 'xccdf_org.ssgproject.content_rule_package_abrt_removed' |
162
|
|
|
client = get_client_arf_to_json_with_define_dest(src, rule) |
163
|
|
|
rules = {'rules': [rule]} |
164
|
|
|
result_src = client.prepare_data(rules) |
165
|
|
|
time.sleep(1) # wait for change time in saved rule name |
166
|
|
|
client1 = get_client_arf_to_json_with_define_dest(src, rule, result_src[0]) |
167
|
|
|
result_src_second_rule = client1.prepare_data(rules) |
168
|
|
|
assert result_src == result_src_second_rule |
169
|
|
|
data = None |
170
|
|
|
with open(result_src[-1], 'r') as f: |
171
|
|
|
data = json.load(f) |
172
|
|
|
rules_id = list(data.keys()) |
173
|
|
|
assert len(rules_id) == 2 |
174
|
|
|
assert data[rules_id[0]] == data[rules_id[1]] |
175
|
|
|
referenc_result = tests.any_test_help.any_get_test_data_json( |
176
|
|
|
'test_data/referenc_result_data_json.json') |
177
|
|
|
assert referenc_result[ |
178
|
|
|
"xccdf_org.ssgproject.content_rule_package_abrt_removed"] == data[rules_id[0]] |
179
|
|
|
|
180
|
|
|
|
181
|
|
|
def test_creation_json_two_selected_rules(): |
182
|
|
|
src = 'test_data/ssg-fedora-ds-arf.xml' |
183
|
|
|
rule = 'xccdf_org.ssgproject.content_rule_package_abrt_removed' |
184
|
|
|
rule1 = 'xccdf_org.ssgproject.content_rule_disable_host_auth' |
185
|
|
|
client = get_client_arf_to_json_with_define_dest(src, rule) |
186
|
|
|
rules = {'rules': [rule, rule1]} |
187
|
|
|
result_src = client.prepare_data(rules) |
188
|
|
|
data = None |
189
|
|
|
with open(result_src[-1], 'r') as f: |
190
|
|
|
data = json.load(f) |
191
|
|
|
rules_id = list(data.keys()) |
192
|
|
|
assert len(rules_id) == 2 |
193
|
|
|
referenc_result = tests.any_test_help.any_get_test_data_json( |
194
|
|
|
'test_data/referenc_result_data_json.json') |
195
|
|
|
assert referenc_result[ |
196
|
|
|
"xccdf_org.ssgproject.content_rule_package_abrt_removed"] == data[rules_id[0]] |
197
|
|
|
assert rule1 in rules_id[1] |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
def test_get_questions_not_selected(capsys): |
201
|
|
|
src = 'test_data/ssg-fedora-ds-arf.xml' |
202
|
|
|
regex = r'_package_\w+_removed' |
203
|
|
|
client = get_client_arf_to_json_with_option_show_not_selected_rules( |
204
|
|
|
src, regex) |
205
|
|
|
tests.any_test_help.get_questions_not_selected(capsys, client) |
206
|
|
|
|
207
|
|
|
|
208
|
|
|
def test_get_questions_not_selected_and_show_failed_rules(capsys): |
209
|
|
|
src = 'test_data/ssg-fedora-ds-arf.xml' |
210
|
|
|
regex = r'_package_\w+_removed' |
211
|
|
|
client = get_client_arf_to_json_with_option_show_not_selected_rules_and_show_failed_rules( |
212
|
|
|
src, regex) |
213
|
|
|
tests.any_test_help.get_questions_not_selected_and_show_failed_rules( |
214
|
|
|
capsys, client) |
215
|
|
|
|
216
|
|
|
|
217
|
|
|
def test_get_questions_with_option_show_failed_rules(): |
218
|
|
|
src = 'test_data/ssg-fedora-ds-arf.xml' |
219
|
|
|
regex = r'_package_\w+_removed' |
220
|
|
|
client = get_client_arf_to_json_with_option_show_failed_rules(src, regex) |
221
|
|
|
tests.any_test_help.get_questions_with_option_show_failed_rules(client) |
222
|
|
|
|
223
|
|
|
|
224
|
|
|
def test_if_not_installed_inquirer_with_option_show_failed_rules(capsys): |
225
|
|
|
with mock.patch.dict(sys.modules, {'inquirer': None}): |
226
|
|
|
src = 'test_data/ssg-fedora-ds-arf.xml' |
227
|
|
|
regex = r'_package_\w+_removed' |
228
|
|
|
client = get_client_arf_to_json_with_option_show_failed_rules( |
229
|
|
|
src, regex) |
230
|
|
|
client.isatty = True |
231
|
|
|
tests.any_test_help.if_not_installed_inquirer_with_option_show_failed_rules( |
232
|
|
|
capsys, client) |
233
|
|
|
|
234
|
|
|
|
235
|
|
|
def test_if_not_installed_inquirer_with_option_show_not_selected_rules( |
236
|
|
|
capsys): |
237
|
|
|
with mock.patch.dict(sys.modules, {'inquirer': None}): |
238
|
|
|
src = 'test_data/ssg-fedora-ds-arf.xml' |
239
|
|
|
regex = r'_package_\w+_removed' |
240
|
|
|
client = get_client_arf_to_json_with_option_show_not_selected_rules( |
241
|
|
|
src, regex) |
242
|
|
|
client.isatty = True |
243
|
|
|
tests.any_test_help.if_not_installed_inquirer_with_option_show_not_selected_rules( |
244
|
|
|
capsys, client) |
245
|
|
|
|
246
|
|
|
|
247
|
|
|
def test_if_not_installed_inquirer_with_option_show_not_selected_rules_and_show_failed_rules( |
248
|
|
|
capsys): |
249
|
|
|
with mock.patch.dict(sys.modules, {'inquirer': None}): |
250
|
|
|
src = 'test_data/ssg-fedora-ds-arf.xml' |
251
|
|
|
regex = r'_package_\w+_removed' |
252
|
|
|
client = get_client_arf_to_json_with_option_show_not_selected_rules_and_show_failed_rules( |
253
|
|
|
src, regex) |
254
|
|
|
client.isatty = True |
255
|
|
|
(tests.any_test_help. |
256
|
|
|
if_not_installed_inquirer_with_option_show_not_selected_rules_and_show_failed_rules( |
257
|
|
|
capsys, client)) |
258
|
|
|
|