forked from LNST-project/lnst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lnst-ctl
executable file
·357 lines (316 loc) · 12.1 KB
/
lnst-ctl
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#! /usr/bin/env python2
"""
Net test controller
Copyright 2011 Red Hat, Inc.
Licensed under the GNU General Public License, version 2 as
published by the Free Software Foundation; see COPYING for details.
"""
__author__ = """
[email protected] (Jiri Pirko)
"""
import getopt
import sys
import logging
import os
import re
import datetime
from lnst.Common.Logs import LoggingCtl, log_exc_traceback
from lnst.Common.Config import lnst_config
from lnst.Common.Colours import load_presets_from_config
from lnst.Common.Utils import mkdir_p
from lnst.Controller.NetTestController import NetTestController, NetTestError
from lnst.Controller.NetTestController import NoMatchError
from lnst.Controller.NetTestResultSerializer import NetTestResultSerializer
from lnst.Controller.XmlProcessing import XmlProcessingError
RETVAL_PASS = 0
RETVAL_FAIL = 1
RETVAL_ERR = 2
def usage(retval=0):
"""
Print usage of this app
"""
print "Usage: %s [OPTIONS...] ACTION [RECIPES...]" % sys.argv[0]
print ""
print "ACTION = [ run | config_only | deconfigure | match_setup ]"
print ""
print "OPTIONS"
print " -A, --override-alias name=value define top-level alias that " \
"will override any other definitions in the recipe"
print " -a, --define-alias name=value define top-level alias"
print " -c, --config=FILE load additional config file"
print " -d, --debug emit debugging messages"
print " --dump-config dumps the join of all loaded " \
"configuration files on stdout and exits"
print " -h, --help print this message"
print " -m, --no-colours disable coloured terminal output"
print " -o, --disable-pool-checks don't check the availability of " \
"machines in the pool"
print " -p, --packet-capture capture and log all ongoing " \
"network communication during the test"
print " -r, --reduce-sync reduces resource synchronization "\
"for python tasks, see documentation"
print " -s, --xslt-url=URL URL to a XSLT document that will "\
"be used when transforming the xml result file, only useful "\
"when -t is used as well"
print " -t, --html=FILE generate a formatted result html"
print " -u, --multi-match run each recipe with every "\
"pool match possible"
print " -x, --result=FILE file to write xml_result"
sys.exit(retval)
def store_alias(alias_def, aliases_dict):
try:
name, value = alias_def.split("=")
except:
msg = "The alias definition '%s' not supported. The proper" + \
" format is alias_name=alias_value."
raise Exception(msg)
if name in aliases_dict:
msg = "The same alias %s was defined multiple times through CLI."
logging.warning(msg)
aliases_dict[name] = value
def exec_action(action, nettestctl):
if action == "run":
return nettestctl.run_recipe()
elif action == "config_only":
return nettestctl.config_only_recipe()
elif action == "match_setup":
return nettestctl.match_setup()
def get_recipe_result(action, file_path, log_ctl, res_serializer,
pool_checks, packet_capture,
defined_aliases, overriden_aliases,
reduce_sync, multi_match):
retval = RETVAL_PASS
matches = 1
no_match = False
log_ctl.set_recipe(file_path, expand="match_%d" % matches)
log_dir = log_ctl.get_recipe_log_path()
recipe_head_log_entry(file_path, log_dir, matches)
res_serializer.add_recipe(file_path, matches)
res = {}
try:
nettestctl = NetTestController(file_path, log_ctl,
res_serializer=res_serializer,
pool_checks=pool_checks,
packet_capture=packet_capture,
defined_aliases=defined_aliases,
overriden_aliases=overriden_aliases,
reduce_sync=reduce_sync)
except XmlProcessingError as err:
log_exc_traceback()
logging.error(err)
res["passed"] = False
res["err_msg"] = str(err)
retval = RETVAL_ERR
res_serializer.set_recipe_result(res)
return retval
while True:
res = {}
if matches == 1:
try:
nettestctl.provision_machines()
nettestctl.print_match_description()
res = exec_action(action, nettestctl)
except Exception as err:
no_match = True
log_exc_traceback()
logging.error(err)
res["passed"] = False
res["err_msg"] = str(err)
retval = RETVAL_ERR
elif matches > 1:
try:
nettestctl.provision_machines()
log_ctl.set_recipe(file_path, expand="match_%d" % matches)
recipe_head_log_entry(file_path, log_dir, matches)
res_serializer.add_recipe(file_path, matches)
nettestctl.print_match_description()
res = exec_action(action, nettestctl)
except NoMatchError as err:
no_match = True
log_ctl.unset_recipe()
logging.warning("Match %d not possible." % matches)
except Exception as err:
no_match = True
log_exc_traceback()
logging.error(err)
res["passed"] = False
res["err_msg"] = str(err)
retval = RETVAL_ERR
if no_match and matches > 1:
break
res_serializer.set_recipe_pool_match(nettestctl.get_pool_match())
res_serializer.set_recipe_result(res)
# The test failed, but don't override erro
if not res["passed"] and retval < RETVAL_FAIL:
retval = RETVAL_FAIL
if not multi_match or no_match:
break
matches += 1
return retval
def recipe_head_log_entry(filename, log_dir, match_num=1):
head_str = "\nTrying recipe file \"%s\" match %d\n" % (filename,
match_num)
log_dir_str = "Logs for this recipe will be stored in '%s'\n" % log_dir
dash_count = max(len(head_str.strip()), len(log_dir_str.strip()))
logging.info("-" * dash_count
+ head_str
+ log_dir_str
+ "-" * dash_count)
def main():
"""
Main function
"""
try:
opts, args = getopt.getopt(
sys.argv[1:],
"A:a:c:dhmoprs:t:ux:",
[
"override_alias=",
"define_alias=",
"config=",
"debug",
"dump-config",
"help",
"no-colours",
"disable-pool-checks",
"packet-capture",
"reduce-sync",
"xslt-url=",
"html=",
"multi-match",
"result=",
]
)
except getopt.GetoptError as err:
print str(err)
usage(RETVAL_ERR)
lnst_config.controller_init()
dirname = os.path.dirname(sys.argv[0])
gitcfg = os.path.join(dirname, "lnst-ctl.conf")
if os.path.isfile(gitcfg):
lnst_config.load_config(gitcfg)
else:
lnst_config.load_config('/etc/lnst-ctl.conf')
usr_cfg = os.path.expanduser('~/.lnst/lnst-ctl.conf')
if os.path.isfile(usr_cfg):
lnst_config.load_config(usr_cfg)
else:
usr_cfg_dir = os.path.dirname(usr_cfg)
pool_dir = usr_cfg_dir + "/pool"
mkdir_p(pool_dir)
global_pool = lnst_config.get_option("environment", "pool_dirs")
if (len(global_pool) == 0):
lnst_config.set_option("environment", "pool_dirs", [pool_dir])
with open(usr_cfg, 'w') as f:
f.write(lnst_config.dump_config())
debug = 0
result_path = None
html_result_path = None
xslt_url = None
packet_capture = False
pool_checks = True
coloured_output = True
defined_aliases = {}
overriden_aliases = {}
reduce_sync = False
multi_match = False
dump_config = False
for opt, arg in opts:
if opt in ("-d", "--debug"):
debug += 1
elif opt in ("-h", "--help"):
usage(RETVAL_PASS)
elif opt in ("-c", "--config"):
if not os.path.isfile(arg):
print "File '%s' doesn't exist!" % arg
usage(RETVAL_ERR)
else:
lnst_config.load_config(arg)
elif opt in ("-x", "--result"):
result_path = arg
elif opt in ("-t", "--html"):
html_result_path = arg
elif opt in ("-p", "--packet-capture"):
packet_capture = True
elif opt in ("-o", "--disable-pool-checks"):
pool_checks = False
elif opt in ("-m", "--no-colours"):
coloured_output = False
elif opt in ("-a", "--define-alias"):
store_alias(arg, defined_aliases)
elif opt in ("-A", "--override-alias"):
store_alias(arg, overriden_aliases)
elif opt in ("-r", "--reduce-sync"):
reduce_sync = True
elif opt in ("-s", "--xslt-url"):
xslt_url = arg
elif opt in ("-u", "--multi-match"):
multi_match = True
elif opt in ("--dump-config"):
dump_config = True
if xslt_url != None:
lnst_config.set_option("environment", "xslt_url", xslt_url)
if dump_config:
print lnst_config.dump_config()
return RETVAL_PASS
if coloured_output:
coloured_output = not lnst_config.get_option("colours",
"disable_colours")
load_presets_from_config(lnst_config)
date = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
log_ctl = LoggingCtl(debug,
log_dir=lnst_config.get_option('environment', 'log_dir'),
log_subdir=date, colours=coloured_output)
if len(args) <= 0:
logging.error("No action specified")
usage(RETVAL_ERR)
action = args[0]
recipes = args[1:]
if not action in ['run', 'config_only', 'deconfigure', 'match_setup']:
logging.error("Action '%s' not recognised" % action)
usage(RETVAL_ERR)
if action == 'deconfigure':
NetTestController.remove_saved_machine_config()
return 0
if recipes == []:
logging.error("No recipe specified!")
usage(RETVAL_ERR)
recipe_files = []
for recipe_path in recipes:
if os.path.isdir(recipe_path):
all_files = []
for root, dirs, files in os.walk(recipe_path):
dirs[:] = [] # do not walk subdirs
all_files += files
all_files.sort()
for f in all_files:
recipe_file = os.path.join(recipe_path, f)
if re.match(r'^.*\.xml$', recipe_file):
recipe_files.append(recipe_file)
else:
recipe_files.append(recipe_path)
retval = RETVAL_PASS
res_serializer = NetTestResultSerializer()
for recipe_file in recipe_files:
rv = get_recipe_result(action, recipe_file, log_ctl, res_serializer,
pool_checks, packet_capture,
defined_aliases, overriden_aliases,
reduce_sync, multi_match)
if rv > retval:
retval = rv
log_ctl.set_recipe("", clean=False)
res_serializer.print_summary()
log_ctl.print_log_dir()
if result_path:
result_path = os.path.expanduser(result_path)
handle = open(result_path, "w")
handle.write(res_serializer.get_result_xml())
handle.close()
if html_result_path:
html_result_path = os.path.expanduser(html_result_path)
handle = open(html_result_path, "w")
handle.write(res_serializer.get_result_html())
handle.close()
return retval
if __name__ == "__main__":
sys.exit(main())