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 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
|
#!/usr/bin/env python3
# SPDX-License-Identifier: LGPL-2.1-only
#
# Main entry point for the libcgroup functional tests
#
# Copyright (c) 2019-2021 Oracle and/or its affiliates.
# Author: Tom Hromatka <tom.hromatka@oracle.com>
#
from config import Config
from run import Run
import datetime
import argparse
import consts
import time
import log
import sys
import os
setup_time = 0.0
teardown_time = 0.0
Log = log.Log
def parse_args():
parser = argparse.ArgumentParser("Libcgroup Functional Tests")
parser.add_argument(
'-n', '--name',
help='name of the container',
required=False,
type=str,
default=consts.DEFAULT_CONTAINER_NAME
)
parser.add_argument(
'-d', '--distro',
help='linux distribution to use as a template',
required=False,
type=str,
default=None
)
parser.add_argument(
'-r', '--release',
help="distribution release, e.g.'trusty'",
required=False,
type=str,
default=None
)
parser.add_argument(
'-a', '--arch',
help='processor architecture',
required=False,
type=str,
default=None
)
parser.add_argument(
'-t', '--timeout',
help='wait timeout (sec) before stopping the '
'container',
required=False,
type=int,
default=None
)
parser.add_argument(
'-l', '--loglevel',
help='log level',
required=False,
type=int,
default=None
)
parser.add_argument(
'-L', '--logfile',
help='log file',
required=False,
type=str,
default=None
)
parser.add_argument(
'-N', '--num',
help='Test number to run. If unspecified, all '
'tests are run',
required=False,
default=consts.TESTS_RUN_ALL,
type=int
)
parser.add_argument(
'-S', '--skip',
help="Test number(s) to skip. If unspecified, all"
" tests are run. To skip multiple tests, "
"separate them via a ',', e.g. '5,7,12'",
required=False,
default='',
type=str
)
parser.add_argument(
'-s', '--suite',
help='Test suite to run, e.g. cpuset',
required=False,
default=consts.TESTS_RUN_ALL_SUITES,
type=str
)
container_parser = parser.add_mutually_exclusive_group(required=False)
container_parser.add_argument(
'--container',
action='store_true',
help='Run the tests in a container. Note '
'that some tests cannot be run in a '
'container.',
dest='container'
)
container_parser.add_argument(
'--no-container',
action='store_false',
help='Do not run the tests in a container.'
' Note that some tests are '
'destructive and will modify your '
'cgroup hierarchy.',
dest='container'
)
parser.set_defaults(container=True)
parser.add_argument(
'-v', '--verbose',
help='Print all information about this test run',
default=True,
required=False,
action="store_false"
)
config = Config(parser.parse_args())
if config.args.skip is None or config.args.skip == '':
pass
elif config.args.skip.find(',') < 0:
config.skip_list.append(int(config.args.skip))
else:
# multiple tests are being skipped
for test_num in config.args.skip.split(','):
config.skip_list.append(int(test_num))
if config.args.loglevel:
log.log_level = config.args.loglevel
if config.args.logfile:
log.log_file = config.args.logfile
return config
# this function maps the container UID to the host UID. By doing
# this, we can write to a bind-mounted device - and thus generate
# code coverage data in the LXD container
def update_host_subuid():
subuid_line1 = 'lxd:{}:1'.format(os.getuid())
subuid_line2 = 'root:{}:1'.format(os.getuid())
found_line1 = False
found_line2 = False
with open('/etc/subuid') as ufile:
for line in ufile.readlines():
if line.strip() == subuid_line1:
found_line1 = True
elif line.strip() == subuid_line2:
found_line2 = True
if not found_line1:
Run.run('sudo sh -c "echo {} >> /etc/subuid"'.format(
subuid_line1), shell_bool=True)
if not found_line2:
Run.run('sudo sh -c "echo {} >> /etc/subuid"'.format(
subuid_line2), shell_bool=True)
# this function maps the container GID to the host GID. By doing
# this, we can write to a bind-mounted device - and thus generate
# code coverage data in the LXD container
def update_host_subgid():
subgid_line1 = 'lxd:{}:1'.format(os.getgid())
subgid_line2 = 'root:{}:1'.format(os.getgid())
found_line1 = False
found_line2 = False
with open('/etc/subgid') as ufile:
for line in ufile.readlines():
if line.strip() == subgid_line1:
found_line1 = True
elif line.strip() == subgid_line2:
found_line2 = True
if not found_line1:
Run.run('sudo sh -c "echo {} >> /etc/subgid"'.format(
subgid_line1), shell_bool=True)
if not found_line2:
Run.run('sudo sh -c "echo {} >> /etc/subgid"'.format(
subgid_line2), shell_bool=True)
def setup(config, do_teardown=True, record_time=False):
global setup_time
start_time = time.time()
if do_teardown:
# belt and suspenders here. In case a previous run wasn't properly
# cleaned up, let's try and clean it up here
try:
teardown(config)
except Exception as e:
# log but ignore all exceptions
Log.log_debug(e)
if config.args.container:
# this command initializes the lxd storage, networking, etc.
Run.run(['sudo', 'lxd', 'init', '--auto'])
update_host_subuid()
update_host_subgid()
config.container.create()
config.container.config()
config.container.start()
# add the libcgroup library to the container's ld
libcgrp_lib_path = os.path.join(consts.LIBCG_MOUNT_POINT, 'src/.libs')
echo_cmd = ([
'bash',
'-c',
'echo {} >> /etc/ld.so.conf.d/libcgroup.conf'
''.format(libcgrp_lib_path)
])
config.container.run(echo_cmd)
config.container.run('ldconfig')
if record_time:
setup_time = time.time() - start_time
def run_tests(config):
passed_tests = []
failed_tests = []
skipped_tests = []
filename_max = 0
for root, dirs, filenames in os.walk(config.ftest_dir):
for filename in filenames:
if os.path.splitext(filename)[-1] != ".py":
# ignore non-python files
continue
filenum = filename.split('-')[0]
try:
filenum_int = int(filenum)
except ValueError:
# D'oh. This file must not be a test. Skip it
Log.log_debug(
'Skipping {}. It doesn\'t start with an int'
''.format(filename)
)
continue
try:
filesuite = filename.split('-')[1]
except IndexError:
Log.log_error(
'Skipping {}. It doesn\'t conform to the '
'filename format'
''.format(filename)
)
continue
if config.args.suite == consts.TESTS_RUN_ALL_SUITES or \
config.args.suite == filesuite:
if config.args.num == consts.TESTS_RUN_ALL or \
config.args.num == filenum_int:
if config.args.suite == consts.TESTS_RUN_ALL_SUITES and \
filesuite == 'sudo':
# Don't run the 'sudo' tests if all tests have been specified.
# The sudo tests must be run as sudo and thus need to be separately
# invoked.
continue
if filenum_int in config.skip_list:
continue
if len(filename) > filename_max:
filename_max = len(filename)
test = __import__(os.path.splitext(filename)[0])
failure_cause = None
start_time = time.time()
try:
Log.log_debug('Running test {}.'.format(filename))
[ret, failure_cause] = test.main(config)
except Exception as e:
# catch all exceptions. you never know when there's
# a crummy test
failure_cause = e
Log.log_debug(e)
ret = consts.TEST_FAILED
finally:
run_time = time.time() - start_time
if ret == consts.TEST_PASSED:
passed_tests.append([filename, run_time])
elif ret == consts.TEST_FAILED:
failed_tests.append([
filename,
run_time,
failure_cause
])
elif ret == consts.TEST_SKIPPED:
skipped_tests.append([
filename,
run_time,
failure_cause
])
else:
raise ValueError('Unexpected ret: {}'.format(ret))
passed_cnt = len(passed_tests)
failed_cnt = len(failed_tests)
skipped_cnt = len(skipped_tests)
print("-----------------------------------------------------------------")
print("Test Results:")
date_str = datetime.datetime.now().strftime('%b %d %H:%M:%S')
print(
'\t{}{}'.format(
'{0: <35}'.format("Run Date:"),
'{0: >15}'.format(date_str)
)
)
test_str = "{} test(s)".format(passed_cnt)
print(
'\t{}{}'.format(
'{0: <35}'.format("Passed:"),
'{0: >15}'.format(test_str)
)
)
test_str = "{} test(s)".format(skipped_cnt)
print(
'\t{}{}'.format(
'{0: <35}'.format("Skipped:"),
'{0: >15}'.format(test_str)
)
)
test_str = "{} test(s)".format(failed_cnt)
print(
'\t{}{}'.format(
'{0: <35}'.format("Failed:"),
'{0: >15}'.format(test_str)
)
)
for test in failed_tests:
print(
"\t\tTest:\t\t\t\t{} - {}"
''.format(test[0], str(test[2]))
)
print("-----------------------------------------------------------------")
global setup_time
global teardown_time
if config.args.verbose:
print("Timing Results:")
print(
'\t{}{}'.format(
'{0: <{1}}'.format("Test", filename_max),
'{0: >15}'.format("Time (sec)")
)
)
print(
# 15 is padding space of "Time (sec)"
'\t{}'.format('-' * (filename_max + 15))
)
time_str = "{0: 2.2f}".format(setup_time)
print(
'\t{}{}'.format(
'{0: <{1}}'.format('setup', filename_max),
'{0: >15}'.format(time_str)
)
)
all_tests = passed_tests + skipped_tests + failed_tests
all_tests.sort()
for test in all_tests:
time_str = "{0: 2.2f}".format(test[1])
print(
'\t{}{}'.format(
'{0: <{1}}'.format(test[0],
filename_max),
'{0: >15}'.format(time_str)
)
)
time_str = "{0: 2.2f}".format(teardown_time)
print(
'\t{}{}'
''.format(
'{0: <{1}}'.format('teardown', filename_max),
'{0: >15}'.format(time_str)
)
)
total_run_time = setup_time + teardown_time
for test in passed_tests:
total_run_time += test[1]
for test in failed_tests:
total_run_time += test[1]
total_str = "{0: 5.2f}".format(total_run_time)
print('\t{}'.format('-' * (filename_max + 15)))
print(
'\t{}{}'
''.format(
'{0: <{1}}'
''.format("Total Run Time", filename_max),
'{0: >15}'.format(total_str)
)
)
return [passed_cnt, failed_cnt, skipped_cnt]
def teardown(config, record_time=False):
global teardown_time
start_time = time.time()
config.process.join_children(config)
if config.args.container:
try:
config.container.stop()
except Exception as e:
# log but ignore all exceptions
Log.log_debug(e)
try:
config.container.delete()
except Exception as e:
# log but ignore all exceptions
Log.log_debug(e)
if record_time:
teardown_time = time.time() - start_time
def main(config):
AUTOMAKE_SKIPPED = 77
AUTOMAKE_HARD_ERROR = 99
AUTOMAKE_PASSED = 0
try:
setup(config, record_time=True)
[passed_cnt, failed_cnt, skipped_cnt] = run_tests(config)
finally:
teardown(config, record_time=True)
if failed_cnt > 0:
return failed_cnt
if passed_cnt > 0:
return AUTOMAKE_PASSED
if skipped_cnt > 0:
return AUTOMAKE_SKIPPED
return AUTOMAKE_HARD_ERROR
if __name__ == '__main__':
config = parse_args()
sys.exit(main(config))
# vim: set et ts=4 sw=4:
|