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

test_command_arf_to_json   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 133
dl 0
loc 159
rs 10
c 0
b 0
f 0
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A tests.test_command_arf_to_json.test_command_parameter_all_and_show_failed_rules() 0 15 2
A tests.test_command_arf_to_json.test_inquirer_choice_rule() 0 22 2
A tests.test_command_arf_to_json.test_bad_command_arf_to_json() 0 14 1
A tests.test_command_arf_to_json.test_command_arf_to_json() 0 12 2
A tests.test_command_arf_to_json.test_bad_command_arf_to_json_with_verbose() 0 15 1
A tests.test_command_arf_to_json.run_commad_and_save_output_to_file() 0 5 2
A tests.test_command_arf_to_json.test_command_with_parameter_out() 0 24 2
A tests.test_command_arf_to_json.test_command_parameter_all() 0 14 2
A tests.test_command_arf_to_json.test_command_arf_to_json_is_tty() 0 11 1
1
import subprocess
2
import os
3
import time
4
import json
5
import pytest
6
from readchar import key
7
8
import tests.any_test_help
9
10
11
def run_commad_and_save_output_to_file(parameters):
12
    src = tests.any_test_help.get_random_dir_in_tmp() + '.json'
13
    with open(src, 'w+') as output:
14
        subprocess.check_call(parameters, stdout=output)
15
    return src
16
17
18
def test_command_arf_to_json():
19
    src = tests.any_test_help.get_random_dir_in_tmp() + '.json'
20
    out = subprocess.check_output(['python3',
21
                                   '-m',
22
                                   'oval_graph.command_line',
23
                                   'arf-to-json',
24
                                   'tests/test_data/ssg-fedora-ds-arf.xml',
25
                                   'xccdf_org.ssgproject.content_rule_package_abrt_removed'
26
                                   ])
27
    with open(src, "w+") as f:
28
        f.writelines(out.decode('utf-8'))
29
    tests.any_test_help.compare_results_json(src)
30
31
32
def test_command_arf_to_json_is_tty():
33
    src = run_commad_and_save_output_to_file(
34
        [
35
            'python3',
36
            '-m',
37
            'oval_graph.command_line',
38
            'arf-to-json',
39
            'tests/test_data/ssg-fedora-ds-arf.xml',
40
            'xccdf_org.ssgproject.content_rule_package_abrt_removed'
41
        ])
42
    tests.any_test_help.compare_results_json(src)
43
44
45
def test_inquirer_choice_rule():
46
    pexpect = pytest.importorskip("pexpect")
47
    src = tests.any_test_help.get_random_dir_in_tmp() + '.json'
48
    sut = pexpect.spawn('python3',
49
                        ['-m',
50
                         'oval_graph.command_line',
51
                         'arf-to-json',
52
                         'tests/test_data/ssg-fedora-ds-arf.xml',
53
                         r'_package_\w+_removed'
54
                         ])
55
    sut.expect(r'\w+')
56
    sut.send(key.DOWN)
57
    sut.send(key.SPACE)
58
    sut.send(key.SPACE)
59
    sut.send(key.UP)
60
    sut.send(key.SPACE)
61
    sut.send(key.ENTER)
62
    sut.wait()
63
    out = sut.readlines()
64
    with open(src, "w+") as f:
65
        f.writelines(row.decode("utf-8") for row in out[24:])
66
    tests.any_test_help.compare_results_json(src)
67
68
69
def test_command_parameter_all():
70
    src = run_commad_and_save_output_to_file(
71
        [
72
            'python3',
73
            '-m',
74
            'oval_graph.command_line',
75
            'arf-to-json',
76
            '--all',
77
            'tests/test_data/ssg-fedora-ds-arf.xml',
78
            '.'
79
        ])
80
    with open(src, "r") as f:
81
        rules = json.load(f)
82
    assert len(rules.keys()) == 184
83
84
85
def test_command_parameter_all_and_show_failed_rules():
86
    src = run_commad_and_save_output_to_file(
87
        [
88
            'python3',
89
            '-m',
90
            'oval_graph.command_line',
91
            'arf-to-json',
92
            '--all',
93
            '--show-failed-rules',
94
            'tests/test_data/ssg-fedora-ds-arf.xml',
95
            r'_package_\w+_removed'
96
        ])
97
    with open(src, "r") as f:
98
        rules = json.load(f)
99
    assert len(rules.keys()) == 1
100
101
102
def test_command_with_parameter_out():
103
    src = run_commad_and_save_output_to_file(
104
        [
105
            'python3',
106
            '-m',
107
            'oval_graph.command_line',
108
            'arf-to-json',
109
            '--all',
110
            'tests/test_data/ssg-fedora-ds-arf.xml',
111
            r'_package_\w+_removed'
112
        ])
113
    time.sleep(5)
114
    subprocess.check_call(['python3',
115
                           '-m',
116
                           'oval_graph.command_line',
117
                           'arf-to-json',
118
                           '--all',
119
                           'tests/test_data/ssg-fedora-ds-arf.xml',
120
                           r'_package_\w+_removed',
121
                           '-o' + src
122
                           ])
123
    with open(src, "r") as f:
124
        rules = json.load(f)
125
    assert len(rules.keys()) == 4
126
127
128
def test_bad_command_arf_to_json_with_verbose():
129
    out = subprocess.check_output(
130
        [
131
            'python3',
132
            '-m',
133
            'oval_graph.command_line',
134
            'arf-to-json',
135
            '-v',
136
            'tests/test_data/xccdf_org.ssgproject.content_profile_ospp-results-initial.xml',
137
            '.'],
138
        stderr=subprocess.STDOUT)
139
    out_string = out.decode('utf-8')
140
    assert out_string.find("Traceback") > -1
141
    assert out_string.find("Warning:") > -1
142
    assert out_string.find("Error:") > -1
143
144
145
def test_bad_command_arf_to_json():
146
    out = subprocess.check_output(
147
        [
148
            'python3',
149
            '-m',
150
            'oval_graph.command_line',
151
            'arf-to-graph',
152
            'tests/test_data/xccdf_org.ssgproject.content_profile_ospp-results-initial.xml',
153
            '.'],
154
        stderr=subprocess.STDOUT)
155
    out_string = out.decode('utf-8')
156
    assert out.decode('utf-8').find("Traceback") == -1
157
    assert out.decode('utf-8').find("Warning:") > -1
158
    assert out.decode('utf-8').find("Error:") > -1
159