-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun
executable file
·203 lines (177 loc) · 7.72 KB
/
run
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
#!/usr/bin/python
"""
Run virt tests outside the autotest client harness.
@copyright: Red Hat 2012
"""
import os, sys, traceback, signal
class StreamProxy(object):
"""
Mechanism to supress stdout output, while keeping the original stdout.
"""
def __init__(self, filename='/dev/null', stream=sys.stdout):
"""
Keep 2 streams to write to, and eventually switch.
"""
self.terminal = stream
if filename is None:
self.log = stream
else:
self.log = open(filename, "a")
self.stream = self.log
def write(self, message):
"""
Write to the current stream.
"""
self.stream.write(message)
def flush(self):
"""
Flush the current stream.
"""
self.stream.flush()
def switch(self):
"""
Switch between the 2 currently available streams.
"""
if self.stream == self.log:
self.stream = self.terminal
else:
self.stream = self.log
if __name__ == '__main__':
import optparse
option_parser = optparse.OptionParser()
option_parser.add_option("-v", "--verbose",
action="store_true", dest="verbose",
help="Exhibit debug messages")
option_parser.add_option("-t", "--type",
action="store", dest="type",
help="Choose test type (kvm, libvirt, v2v)")
option_parser.add_option("-c", "--config",
action="store", dest="config",
help="Explicitly choose a cartesian config")
option_parser.add_option("-r", "--restore-image",
action="store_true", dest="restore",
help="Restore image from pristine image")
option_parser.add_option("--qemu-bin", action="store", dest="qemu",
help="Path to a custom qemu binary to be tested")
option_parser.add_option("--tests", action="store", dest="tests",
help=('List of space separated tests to be executed'
' - example: --tests "boot, reboot, shutdown"'))
option_parser.add_option("--list-tests", action="store_true", dest="list",
help="List tests available")
option_parser.add_option("--data-dir", action="store", dest="datadir",
help="Path to a data dir (that locates ISOS and images)")
options, args = option_parser.parse_args()
if (not options.type) and (not options.config):
print("No type (-t) or config (-c) options specified, aborting...")
option_parser.print_help()
sys.exit(1)
if not options.verbose:
# Effectively points the stderr FD (2) to /dev/null, silencing
# stderr.
out_fd = os.open('/dev/null', os.O_WRONLY | os.O_CREAT)
try:
os.dup2(out_fd, 2)
finally:
os.close(out_fd)
sys.stderr = os.fdopen(2, 'w')
# Replace stdout by our StreamProxy object, so we can supress all output
# but the one we specifically want.
sys.stdout = StreamProxy(filename="/dev/null", stream=sys.stdout)
else:
# We have full stdout output
sys.stdout = StreamProxy(filename=None, stream=sys.stdout)
# From now on, since the stdout might be proxied, let's run this all under
# a try/except block, if something goes wrong we tell the user to use
# --verbose.
try:
try:
from autotest.client import setup_modules
except ImportError:
try:
autotest_dir = os.environ['AUTOTEST_PATH']
except KeyError:
try:
sys.stdout.switch()
except AttributeError:
pass
print("Environment variable $AUTOTEST_PATH not set. "
"please set it to a path containing an autotest checkout")
print("Or install the autotest-framework package for your distro")
sys.exit(1)
client_dir = os.path.join(autotest_dir, 'client')
sys.path.insert(0, client_dir)
import setup_modules
sys.path.pop(0)
setup_modules.setup(base_path=client_dir,
root_module_name="autotest.client")
test_dir = os.path.abspath(os.path.dirname(sys.modules[__name__].__file__))
sys.path.append(test_dir)
from virttest import cartesian_config, standalone_test, utils_misc
from virttest import data_dir
standalone_test.configure_logging()
standalone_test.configure_console_logging()
if options.datadir:
data_dir.set_backing_data_dir(options.datadir)
standalone_test.create_config_files(options)
cartesian_parser = cartesian_config.Parser()
if options.config is not None:
cfg = os.path.abspath(options.config)
if options.config is None:
cfg = os.path.join(test_dir, options.type, "cfg", "tests.cfg")
cartesian_parser.parse_file(cfg)
if options.list:
standalone_test.print_test_list(options, cartesian_parser)
sys.exit(0)
else:
standalone_test.bootstrap_tests(options)
if options.type == 'kvm':
qemu_bin_path = None
if options.qemu:
if not os.path.isfile(options.qemu):
raise RuntimeError("Invalid qemu binary provided (%s)" %
options.qemu)
qemu_bin_path = options.qemu
else:
try:
qemu_bin_path = utils_misc.find_command('qemu-kvm')
except ValueError:
qemu_bin_path = utils_misc.find_command('kvm')
qemu_dirname = os.path.dirname(qemu_bin_path)
parent_qemu_dirname = os.path.dirname(qemu_dirname)
qemu_img_path = os.path.join(parent_qemu_dirname, 'qemu-img')
qemu_io_path = os.path.join(parent_qemu_dirname, 'qemu-io')
if not os.path.exists(qemu_img_path):
qemu_img_path = utils_misc.find_command('qemu-img')
if not os.path.exists(qemu_io_path):
qemu_io_path = utils_misc.find_command('qemu-io')
cartesian_parser.parse_string("qemu_binary = %s" % qemu_bin_path)
cartesian_parser.parse_string("qemu_img_binary = %s" % qemu_img_path)
cartesian_parser.parse_string("qemu_io_binary = %s" % qemu_io_path)
if options.type and options.tests:
tests = options.tests.split(" ")
cartesian_parser.parse_string("only %s" % ", ".join(tests))
elif options.type and not options.tests and not options.config:
if options.type == 'kvm':
cartesian_parser.parse_string("only migrate")
elif options.type == 'libvirt':
cartesian_parser.parse_string("only "
"unattended_install.import.import, "
"virsh_domname, "
"remove_guest.without_disk")
standalone_test.run_tests(cartesian_parser, options)
except KeyboardInterrupt:
pid = os.getpid()
os.kill(pid, signal.SIGTERM)
except Exception:
try:
sys.stdout.switch()
except AttributeError:
pass
print("Internal error, traceback follows...")
exc_type, exc_value, exc_traceback = sys.exc_info()
tb_info = traceback.format_exception(exc_type, exc_value,
exc_traceback.tb_next)
tb_info = "".join(tb_info)
for e_line in tb_info.splitlines():
print(e_line)
sys.exit(1)