Passed
Pull Request — master (#161)
by Jan
04:56
created

tests.test_command_json_to_graph   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 160
dl 0
loc 188
rs 10
c 0
b 0
f 0
wmc 13

7 Functions

Rating   Name   Duplication   Size   Complexity  
B test_inquirer_choice_rule() 0 41 2
A test_command_json_to_graph_with_verbose() 0 25 2
A test_command_json_to_graph_is_tty() 0 23 2
A test_command_json_to_graph() 0 24 2
A test_bad_command_json_to_graph_with_verbose() 0 13 1
A test_bad_command_json_to_graph() 0 12 1
A test_command_parameter_all() 0 27 3
1
import subprocess
2
import os
3
import json
4
import pytest
5
import tempfile
6
from readchar import key
7
8
import tests.any_test_help
9
10
11
@pytest.mark.usefixtures("remove_generated_reports_in_root")
12
def test_command_json_to_graph():
13
    src = tests.any_test_help.get_random_dir_in_tmp() + '.json'
14
    out = subprocess.check_output(['python3',
15
                                   '-m',
16
                                   'oval_graph.command_line',
17
                                   'arf-to-json',
18
                                   'tests/test_data/ssg-fedora-ds-arf.xml',
19
                                   'xccdf_org.ssgproject.content_rule_package_abrt_removed'
20
                                   ])
21
    with open(src, "w+") as f:
22
        f.writelines(out.decode('utf-8'))
23
    subprocess.check_call(['python3',
24
                           '-m',
25
                           'oval_graph.command_line',
26
                           'json-to-graph',
27
                           '-o', '.',
28
                           src,
29
                           'xccdf_org.ssgproject.content_rule_package_abrt_removed'
30
                           ], cwd='./')
31
    file_src = tests.any_test_help.find_files(
32
        "graph-of-xccdf_org.ssgproject.content_rule_package_abrt_removed",
33
        '../')
34
    tests.any_test_help.compare_results_html(file_src[0])
35
36
37
@pytest.mark.usefixtures("remove_generated_reports_in_root")
38
def test_command_json_to_graph_with_verbose():
39
    src = tests.any_test_help.get_random_dir_in_tmp() + '.json'
40
    out = subprocess.check_output(['python3',
41
                                   '-m',
42
                                   'oval_graph.command_line',
43
                                   'arf-to-json',
44
                                   'tests/test_data/ssg-fedora-ds-arf.xml',
45
                                   'xccdf_org.ssgproject.content_rule_package_abrt_removed'
46
                                   ])
47
    with open(src, "w+") as f:
48
        f.writelines(out.decode('utf-8'))
49
    out = subprocess.check_output(['python3',
50
                                   '-m',
51
                                   'oval_graph.command_line',
52
                                   'json-to-graph',
53
                                   '-o', '.',
54
                                   '--verbose',
55
                                   src,
56
                                   'xccdf_org.ssgproject.content_rule_package_abrt_removed'
57
                                   ],
58
                                  cwd='./',
59
                                  stderr=subprocess.STDOUT)
60
    src = out.decode('utf-8').split('\n')[-2]
61
    tests.any_test_help.compare_results_html('.' + src)
62
63
64
def test_command_json_to_graph_is_tty():
65
    src = tests.any_test_help.get_random_dir_in_tmp() + '.json'
66
    with open(src, 'w+')as output:
67
        subprocess.check_call(['python3',
68
                               '-m',
69
                               'oval_graph.command_line',
70
                               'arf-to-json',
71
                               'tests/test_data/ssg-fedora-ds-arf.xml',
72
                               'xccdf_org.ssgproject.content_rule_package_abrt_removed'
73
                               ],
74
                              stdout=output)
75
    out_dir = tests.any_test_help.get_random_dir_in_tmp()
76
    out = subprocess.check_output(['python3',
77
                                   '-m',
78
                                   'oval_graph.command_line',
79
                                   'json-to-graph',
80
                                   '--out',
81
                                   out_dir,
82
                                   src,
83
                                   'xccdf_org.ssgproject.content_rule_package_abrt_removed'
84
                                   ])
85
    tests.any_test_help.compare_results_html(
86
        os.path.join(out_dir, os.listdir(out_dir)[0]))
87
88
89
def test_inquirer_choice_rule():
90
    pexpect = pytest.importorskip("pexpect")
91
    src = tests.any_test_help.get_random_dir_in_tmp() + '.json'
92
    sut = pexpect.spawn('python3',
93
                        ['-m',
94
                         'oval_graph.command_line',
95
                         'arf-to-json',
96
                         'tests/test_data/ssg-fedora-ds-arf.xml',
97
                         r'_package_\w+_removed'
98
                         ])
99
    sut.expect(r'\w+')
100
    sut.send(key.DOWN)
101
    sut.send(key.SPACE)
102
    sut.send(key.UP)
103
    sut.send(key.SPACE)
104
    sut.send(key.ENTER)
105
    sut.wait()
106
    out = sut.readlines()
107
108
    with open(src, "w+") as f:
109
        f.writelines(row.decode("utf-8") for row in out[20:])
110
    tests.any_test_help.compare_results_json(src)
111
112
    out_dir = tests.any_test_help.get_random_dir_in_tmp()
113
    sut = pexpect.spawn('python3',
114
                        ['-m',
115
                         'oval_graph.command_line',
116
                         'json-to-graph',
117
                         '-o',
118
                         out_dir,
119
                         src,
120
                         '.'
121
                         ])
122
    sut.expect(r'\w+')
123
    sut.send(key.DOWN)
124
    sut.send(key.SPACE)
125
    sut.send(key.ENTER)
126
    sut.wait()
127
    assert len(os.listdir(out_dir)) == 1
128
    assert ("xccdf_org.ssgproject.content_rule_package_abrt_removed"
129
            in os.listdir(out_dir)[0])
130
131
132
def test_command_parameter_all():
133
    src = tests.any_test_help.get_random_dir_in_tmp() + '.json'
134
    with open(src, 'w+')as output:
135
        subprocess.check_call(['python3',
136
                               '-m',
137
                               'oval_graph.command_line',
138
                               'arf-to-json',
139
                               '--all',
140
                               'tests/test_data/ssg-fedora-ds-arf.xml',
141
                               '.'
142
                               ],
143
                              stdout=output)
144
    with open(src, "r") as f:
145
        rules = json.load(f)
146
    assert len(rules.keys()) == 184
147
    out_dir = tests.any_test_help.get_random_dir_in_tmp()
148
    subprocess.check_call(['python3',
149
                           '-m',
150
                           'oval_graph.command_line',
151
                           'json-to-graph',
152
                           src,
153
                           '.',
154
                           '--all',
155
                           '-o',
156
                           out_dir
157
                           ])
158
    assert len(os.listdir(out_dir)) == 184
159
160
161
def test_bad_command_json_to_graph_with_verbose():
162
    out = subprocess.check_output(['python3',
163
                                   '-m',
164
                                   'oval_graph.command_line',
165
                                   'json-to-graph',
166
                                   '-v',
167
                                   'tests/test_data/ssg-fedora-ds-arf.xml',
168
                                   '.'
169
                                   ],
170
                                  stderr=subprocess.STDOUT)
171
    out_string = out.decode('utf-8')
172
    assert out_string.find("Traceback") > -1
173
    assert out_string.find("Error:") > -1
174
175
176
def test_bad_command_json_to_graph():
177
    out = subprocess.check_output(['python3',
178
                                   '-m',
179
                                   'oval_graph.command_line',
180
                                   'json-to-graph',
181
                                   'tests/test_data/ssg-fedora-ds-arf.xml',
182
                                   '.'
183
                                   ],
184
                                  stderr=subprocess.STDOUT)
185
    out_string = out.decode('utf-8')
186
    assert out_string.find("Traceback") == -1
187
    assert out_string.find("Error:") > -1
188