From 5376ecf4e4916dc7bb6b58c8197f0a60da22e94d Mon Sep 17 00:00:00 2001 From: Andrew Schoen Date: Mon, 17 Nov 2014 10:14:36 -0600 Subject: [PATCH] refactored teuthology.coverage to use an dict from docopt instead of a namespace from argparse Signed-off-by: Andrew Schoen --- scripts/test/test_coverage.py | 12 ++++- teuthology/coverage.py | 82 ++++++++++++++++++----------------- 2 files changed, 54 insertions(+), 40 deletions(-) diff --git a/scripts/test/test_coverage.py b/scripts/test/test_coverage.py index dee8e0e053..c9ea2bbcc2 100644 --- a/scripts/test/test_coverage.py +++ b/scripts/test/test_coverage.py @@ -10,7 +10,7 @@ doc = coverage.__doc__ class TestCoverage(Script): script_name = 'teuthology-coverage' - def test_args(self): + def test_all_args(self): args = docopt(doc, [ "--skip-init", "--lcov-output=some/other/dir", @@ -25,3 +25,13 @@ class TestCoverage(Script): assert args["--html-output"] == "html/output/dir" assert args["--cov-tools-dir"] == "cov/tools/dir" assert args["--verbose"] + + def test_missing_optional_args(self): + args = docopt(doc, [ + "--lcov-output=some/other/dir", + "some/test/dir"] + ) + assert not args['--html-output'] + assert not args['--skip-init'] + assert not args['--verbose'] + assert args['--cov-tools-dir'] == "../../coverage" diff --git a/teuthology/coverage.py b/teuthology/coverage.py index f877ee0b04..3829695969 100644 --- a/teuthology/coverage.py +++ b/teuthology/coverage.py @@ -7,7 +7,7 @@ import MySQLdb import yaml import teuthology -from teuthology.misc import read_config +from teuthology.config import config log = logging.getLogger(__name__) @@ -32,19 +32,19 @@ CREATE TABLE `coverage` ( """ -def connect_to_db(ctx): +def connect_to_db(): db = MySQLdb.connect( - host=ctx.teuthology_config['coverage_db_host'], - user=ctx.teuthology_config['coverage_db_user'], - db=ctx.teuthology_config['coverage_db_name'], - passwd=ctx.teuthology_config['coverage_db_password'], + host=config.coverage_db_host, + user=config.coverage_db_user, + db=config.coverage_db_name, + passwd=config.coverage_db_password, ) db.autocommit(False) return db -def store_coverage(ctx, test_coverage, rev, suite): - with closing(connect_to_db(ctx)) as db: +def store_coverage(test_coverage, rev, suite): + with closing(connect_to_db()) as db: rows = [] for test, coverage in test_coverage.iteritems(): flattened_cov = [item for sublist in coverage for item in sublist] @@ -90,35 +90,39 @@ def read_coverage(output): def main(args): - if args.verbose: + if args["--verbose"]: teuthology.log.setLevel(logging.DEBUG) log = logging.getLogger(__name__) - read_config(args) - - log_path = os.path.join(args.test_dir, 'coverage.log') + log_path = os.path.join(args[''], 'coverage.log') teuthology.setup_log_file(log_path) try: - analyze(args) + analyze( + args[''], + args['--cov-tools-dir'], + args['--lcov-output'], + args['--html-output'], + args['--skip-init'] + ) except Exception: log.exception('error generating coverage') raise -def analyze(args): +def analyze(test_dir, cov_tools_dir, lcov_output, html_output, skip_init): tests = [ - f for f in sorted(os.listdir(args.test_dir)) + f for f in sorted(os.listdir(test_dir)) if not f.startswith('.') - and os.path.isdir(os.path.join(args.test_dir, f)) - and os.path.exists(os.path.join(args.test_dir, f, 'summary.yaml')) - and os.path.exists(os.path.join(args.test_dir, f, 'ceph-sha1'))] + and os.path.isdir(os.path.join(test_dir, f)) + and os.path.exists(os.path.join(test_dir, f, 'summary.yaml')) + and os.path.exists(os.path.join(test_dir, f, 'ceph-sha1'))] test_summaries = {} for test in tests: summary = {} - with file(os.path.join(args.test_dir, test, 'summary.yaml')) as f: + with file(os.path.join(test_dir, test, 'summary.yaml')) as f: g = yaml.safe_load_all(f) for new in g: summary.update(new) @@ -130,25 +134,25 @@ def analyze(args): assert len(test_summaries) > 0 - suite = os.path.basename(args.test_dir) + suite = os.path.basename(test_dir) # only run cov-init once. # this only works if all tests were run against the same version. - if not args.skip_init: + if not skip_init: log.info('initializing coverage data...') subprocess.check_call( args=[ - os.path.join(args.cov_tools_dir, 'cov-init.sh'), - os.path.join(args.test_dir, tests[0]), - args.lcov_output, + os.path.join(cov_tools_dir, 'cov-init.sh'), + os.path.join(test_dir, tests[0]), + lcov_output, os.path.join( - args.teuthology_config['ceph_build_output_dir'], + config.ceph_build_output_dir, '{suite}.tgz'.format(suite=suite), ), ]) shutil.copy( - os.path.join(args.lcov_output, 'base.lcov'), - os.path.join(args.lcov_output, 'total.lcov') + os.path.join(lcov_output, 'base.lcov'), + os.path.join(lcov_output, 'total.lcov') ) test_coverage = {} @@ -158,9 +162,9 @@ def analyze(args): log.info('analyzing coverage for %s', test) proc = subprocess.Popen( args=[ - os.path.join(args.cov_tools_dir, 'cov-analyze.sh'), - '-t', os.path.join(args.test_dir, test), - '-d', args.lcov_output, + os.path.join(cov_tools_dir, 'cov-analyze.sh'), + '-t', os.path.join(test_dir, test), + '-d', lcov_output, '-o', test, ], stdout=subprocess.PIPE, @@ -173,32 +177,32 @@ def analyze(args): proc = subprocess.Popen( args=[ 'lcov', - '-a', os.path.join(args.lcov_output, lcov_file), - '-a', os.path.join(args.lcov_output, 'total.lcov'), - '-o', os.path.join(args.lcov_output, 'total_tmp.lcov'), + '-a', os.path.join(lcov_output, lcov_file), + '-a', os.path.join(lcov_output, 'total.lcov'), + '-o', os.path.join(lcov_output, 'total_tmp.lcov'), ], stdout=subprocess.PIPE, ) output, _ = proc.communicate() os.rename( - os.path.join(args.lcov_output, 'total_tmp.lcov'), - os.path.join(args.lcov_output, 'total.lcov') + os.path.join(lcov_output, 'total_tmp.lcov'), + os.path.join(lcov_output, 'total.lcov') ) coverage = read_coverage(output) test_coverage['total for {suite}'.format(suite=suite)] = coverage log.debug('total coverage is %s', str(coverage)) - if args.html_output: + if html_output: subprocess.check_call( args=[ 'genhtml', '-s', - '-o', os.path.join(args.html_output, 'total'), + '-o', os.path.join(html_output, 'total'), '-t', 'Total for {suite}'.format(suite=suite), '--', - os.path.join(args.lcov_output, 'total.lcov'), + os.path.join(lcov_output, 'total.lcov'), ]) - store_coverage(args, test_coverage, summary['ceph-sha1'], suite) + store_coverage(test_coverage, summary['ceph-sha1'], suite) -- 2.39.5