-
Notifications
You must be signed in to change notification settings - Fork 2
/
check.py
executable file
·286 lines (247 loc) · 8.48 KB
/
check.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/python
# -*- coding: utf-8 -*-
# ####################################################################
# specker-lib - spec file manipulation library
# Copyright (C) 2015 Fridolin Pokorny, [email protected]
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# ####################################################################
'''
Library check tool
@author: Fridolin Pokorny
@contact: [email protected]
@organization: Red Hat Inc.
@license: GPL 2.0
'''
import unittest
import sys
import logging
import optparse
from subprocess import PIPE, Popen
LOGGER = logging.getLogger('specker-check')
VERBOSE = False
def run_specker(args, stdin = None):
'''
Run specker with args
@param args: args to run specker with
@type args: list of strings
@param stdin: file hanler for stdin
@type stdin: file handler
@return: process info
@rtype: dict {'stdout', 'stderr', 'returncode'}
'''
prog = ["./specker"] + args
LOGGER.debug(">>> args: " + str(args))
if stdin:
fin = open(stdin, 'r')
else:
fin = None
process = Popen(prog, stdin = fin, stderr = PIPE, stdout = PIPE, shell = False)
stdout, stderr = process.communicate()
returncode = process.returncode
if stdin:
fin.close()
return { 'stdout': stdout, 'stderr': stderr, 'returncode': returncode }
def print_status(status, verbose = VERBOSE, additional = None):
'''
Print status of a process, if verbose
@param status: process status to be printed
@type status: dict {'stdout', 'stderr', 'returncode'}
@param verbose: if True, print status
@type verbose: Boolean
@return: None
@rtype: None
'''
if VERBOSE:
LOGGER.debug(">>> FAILED!!!")
LOGGER.debug(">>> Stdout:\n%s", status['stdout'])
LOGGER.debug(">>> Stderr:\n%s", status['stderr'])
LOGGER.debug(">>> Return code:\n%s", status['returncode'])
if additional:
LOGGER.debug(">>> Additional info:\n%s", additional)
def assertContains(pattern, output, status):
'''
Check if pattern is in output
@param pattern: pattern to be checked
@type pattern: string
@param output: string to check against
@type output: string
@param status: status to be printed if assertion fails
@type status: dict {'stdout', 'stderr', 'returncode'}
'''
if pattern not in output:
print_status(status)
assert False
def assertTrue(expr, status):
'''
Check true value
@param expr: expression which should be true
@type expr: Boolean
@param status: status to be printed if assertion fails
@type status: dict {'stdout', 'stderr', 'returncode'}
'''
if not expr:
print_status(status)
assert False
def assertFalse(expr, status):
'''
Check false value
@param expr: expression which should be false
@type expr: Boolean
@param status: status to be printed if assertion fails
@type status: dict {'stdout', 'stderr', 'returncode'}
'''
assertTrue(not expr, status)
def assertEqual(cmp1, cmp2, status):
'''
Check equal
@param cmp1: expression 1 to be checked
@param cmp2: expression 2 to be checked
@param status: status to be printed if assertion fails
@type status: dict {'stdout', 'stderr', 'returncode'}
'''
if cmp1 != cmp2:
print_status(status)
assert False
def assertNotEqual(cmp1, cmp2, status):
'''
Check not equal
@param cmp1: expression 1 to be checked
@param cmp2: expression 2 to be checked
@param status: status to be printed if assertion fails
@type status: dict {'stdout', 'stderr', 'returncode'}
'''
if cmp1 == cmp2:
print_status(status)
assert False
def assertNoDiff(stdout, f, status):
with open(f, 'r') as fout:
content = fout.read()
if stdout != content:
cmd = Popen(["diff", "-Naur", f, '-'], stdout=PIPE, stdin=PIPE)
diff = cmd.communicate(input = stdout)[0]
print_status(status, additional = diff)
assert False
################################################################################
class TestGeneric(unittest.TestCase):
'''
Generic tests
'''
def test_nonexisting_file(self):
input_file = "somenonlongnameofanonexistentfile"
result = run_specker([input_file])
assertContains('No such file or directory', result['stderr'], result)
assertNotEqual(0, result['returncode'], result)
def test_golang_flannel(self):
input_file = "./testsuite/golang-flannel.spec"
result = run_specker([input_file])
assertEqual(0, result['returncode'], result)
assertNoDiff(result['stdout'], input_file, result)
def test_golang_flannel(self):
input_file = "./testsuite/EMPTY"
result = run_specker([input_file])
assertEqual(0, result['returncode'], result)
assertNoDiff(result['stdout'], input_file, result)
def test_custom_editor(self):
input_file = "./testsuite/custom_editor.spec"
source_file = "./testsuite/custom_editor.py"
output_file = "./testsuite/custom_editor_out.spec"
result = run_specker([
'--custom-editor=%s' % source_file,
'--install-edit=777',
input_file,
],
stdin = source_file)
assertEqual(0, result['returncode'], result)
assertNoDiff(result['stdout'], output_file, result)
def test_custom_manipulator_editor(self):
input_file = "./testsuite/custom_manipulator_editor.spec"
source_file = "./testsuite/custom_manipulator_editor.py"
output_file = "./testsuite/custom_manipulator_editor_out.txt"
result = run_specker([
"--custom-manipulator-renderer=%s" % source_file,
"--provides-show=*",
input_file
],
stdin = source_file)
assertEqual(0, result['returncode'], result)
assertNoDiff(result['stdout'], output_file, result)
def test_custom_model_writer(self):
input_file = "./testsuite/custom_model_writer.spec"
source_file = "./testsuite/custom_model_writer.py"
output_file = "./testsuite/custom_model_writer_out.spec"
result = run_specker([
"--custom-model-writer=%s" % source_file,
"--provides-remove=/bin/cp",
input_file
],
stdin = source_file)
assertEqual(0, result['returncode'], result)
assertNoDiff(result['stdout'], output_file, result)
################################################################################
class TestModel(unittest.TestCase):
'''
Test L{SpecModel}
'''
def test_sections_add(self):
input_file1 = "./testsuite/sections_add_in1.spec"
input_file2 = "./testsuite/sections_add_in2.spec"
output_file = './testsuite/sections_add_out.spec'
result = run_specker([input_file1, '--sections-add'], stdin = input_file2)
assertEqual(0, result['returncode'], result)
assertNoDiff(result['stdout'], output_file, result)
################################################################################
class TestFileParser(unittest.TestCase):
'''
Test L{SpecFileParser}
'''
pass
################################################################################
class TestDefaultEditor(unittest.TestCase):
'''
Test L{SpecDefaultEditor}
'''
pass
################################################################################
class TestFileRenderer(unittest.TestCase):
'''
Test L{SpecFileRenderer}
'''
pass
################################################################################
if __name__ == '__main__':
LOGGER.addHandler(logging.StreamHandler(sys.stderr))
parser = optparse.OptionParser("%prog OPTIONS")
parser.add_option(
"", "-v", "--verbose", dest="verbose", action = "store_true",
default = False, help = "verbose output"
)
options, args = parser.parse_args()
if len(args) > 0:
LOGGER.error("Error: Incorrect number of arguments")
exit(1)
if options.verbose:
VERBOSE = True
LOGGER.setLevel(logging.DEBUG)
LOGGER.debug(">>> running in verbose mode")
unittest_verbosity = 0
else:
unittest_verbosity = 2
loader = unittest.TestLoader()
suites_list = []
for test_class in [TestGeneric, TestFileParser, TestModel, TestDefaultEditor, TestFileRenderer]:
suites_list.append(loader.loadTestsFromTestCase(test_class))
ret = unittest.TextTestRunner(verbosity = unittest_verbosity).run(unittest.TestSuite(suites_list))
sys.exit(not (len(ret.errors) == 0 and len(ret.failures) == 0))