Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 1 | # resulttool - store test results |
| 2 | # |
| 3 | # Copyright (c) 2019, Intel Corporation. |
| 4 | # Copyright (c) 2019, Linux Foundation |
| 5 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame^] | 6 | # SPDX-License-Identifier: GPL-2.0-only |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 7 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame^] | 8 | |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 9 | import tempfile |
| 10 | import os |
| 11 | import subprocess |
| 12 | import json |
| 13 | import shutil |
| 14 | import scriptpath |
| 15 | scriptpath.add_bitbake_lib_path() |
| 16 | scriptpath.add_oe_lib_path() |
| 17 | import resulttool.resultutils as resultutils |
| 18 | import oeqa.utils.gitarchive as gitarchive |
| 19 | |
| 20 | |
| 21 | def store(args, logger): |
| 22 | tempdir = tempfile.mkdtemp(prefix='testresults.') |
| 23 | try: |
| 24 | results = {} |
| 25 | logger.info('Reading files from %s' % args.source) |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame^] | 26 | if resultutils.is_url(args.source) or os.path.isfile(args.source): |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 27 | resultutils.append_resultsdata(results, args.source) |
| 28 | else: |
| 29 | for root, dirs, files in os.walk(args.source): |
| 30 | for name in files: |
| 31 | f = os.path.join(root, name) |
| 32 | if name == "testresults.json": |
| 33 | resultutils.append_resultsdata(results, f) |
| 34 | elif args.all: |
| 35 | dst = f.replace(args.source, tempdir + "/") |
| 36 | os.makedirs(os.path.dirname(dst), exist_ok=True) |
| 37 | shutil.copyfile(f, dst) |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 38 | |
| 39 | revisions = {} |
| 40 | |
| 41 | if not results and not args.all: |
| 42 | if args.allow_empty: |
| 43 | logger.info("No results found to store") |
| 44 | return 0 |
| 45 | logger.error("No results found to store") |
| 46 | return 1 |
| 47 | |
| 48 | # Find the branch/commit/commit_count and ensure they all match |
| 49 | for suite in results: |
| 50 | for result in results[suite]: |
| 51 | config = results[suite][result]['configuration']['LAYERS']['meta'] |
| 52 | revision = (config['commit'], config['branch'], str(config['commit_count'])) |
| 53 | if revision not in revisions: |
| 54 | revisions[revision] = {} |
| 55 | if suite not in revisions[revision]: |
| 56 | revisions[revision][suite] = {} |
| 57 | revisions[revision][suite][result] = results[suite][result] |
| 58 | |
| 59 | logger.info("Found %d revisions to store" % len(revisions)) |
| 60 | |
| 61 | for r in revisions: |
| 62 | results = revisions[r] |
| 63 | keywords = {'commit': r[0], 'branch': r[1], "commit_count": r[2]} |
| 64 | subprocess.check_call(["find", tempdir, "!", "-path", "./.git/*", "-delete"]) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 65 | resultutils.save_resultsdata(results, tempdir, ptestlogs=True) |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 66 | |
| 67 | logger.info('Storing test result into git repository %s' % args.git_dir) |
| 68 | |
| 69 | gitarchive.gitarchive(tempdir, args.git_dir, False, False, |
| 70 | "Results of {branch}:{commit}", "branch: {branch}\ncommit: {commit}", "{branch}", |
| 71 | False, "{branch}/{commit_count}-g{commit}/{tag_number}", |
| 72 | 'Test run #{tag_number} of {branch}:{commit}', '', |
| 73 | [], [], False, keywords, logger) |
| 74 | |
| 75 | finally: |
| 76 | subprocess.check_call(["rm", "-rf", tempdir]) |
| 77 | |
| 78 | return 0 |
| 79 | |
| 80 | def register_commands(subparsers): |
| 81 | """Register subcommands from this plugin""" |
| 82 | parser_build = subparsers.add_parser('store', help='store test results into a git repository', |
| 83 | description='takes a results file or directory of results files and stores ' |
| 84 | 'them into the destination git repository, splitting out the results ' |
| 85 | 'files as configured', |
| 86 | group='setup') |
| 87 | parser_build.set_defaults(func=store) |
| 88 | parser_build.add_argument('source', |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame^] | 89 | help='source file/directory/URL that contain the test result files to be stored') |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 90 | parser_build.add_argument('git_dir', |
| 91 | help='the location of the git repository to store the results in') |
| 92 | parser_build.add_argument('-a', '--all', action='store_true', |
| 93 | help='include all files, not just testresults.json files') |
| 94 | parser_build.add_argument('-e', '--allow-empty', action='store_true', |
| 95 | help='don\'t error if no results to store are found') |
| 96 | |