blob: 5e33716c3dcf2b227e7aceaddcbdaa4fa2fa4fbc [file] [log] [blame]
Brad Bishop40320b12019-03-26 16:08:25 -04001# resulttool - store test results
2#
3# Copyright (c) 2019, Intel Corporation.
4# Copyright (c) 2019, Linux Foundation
5#
6# This program is free software; you can redistribute it and/or modify it
7# under the terms and conditions of the GNU General Public License,
8# version 2, as published by the Free Software Foundation.
9#
10# This program is distributed in the hope it will be useful, but WITHOUT
11# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13# more details.
14#
15import tempfile
16import os
17import subprocess
18import json
19import shutil
20import scriptpath
21scriptpath.add_bitbake_lib_path()
22scriptpath.add_oe_lib_path()
23import resulttool.resultutils as resultutils
24import oeqa.utils.gitarchive as gitarchive
25
26
27def store(args, logger):
28 tempdir = tempfile.mkdtemp(prefix='testresults.')
29 try:
30 results = {}
31 logger.info('Reading files from %s' % args.source)
32 for root, dirs, files in os.walk(args.source):
33 for name in files:
34 f = os.path.join(root, name)
35 if name == "testresults.json":
36 resultutils.append_resultsdata(results, f)
37 elif args.all:
38 dst = f.replace(args.source, tempdir + "/")
39 os.makedirs(os.path.dirname(dst), exist_ok=True)
40 shutil.copyfile(f, dst)
41
42 revisions = {}
43
44 if not results and not args.all:
45 if args.allow_empty:
46 logger.info("No results found to store")
47 return 0
48 logger.error("No results found to store")
49 return 1
50
51 # Find the branch/commit/commit_count and ensure they all match
52 for suite in results:
53 for result in results[suite]:
54 config = results[suite][result]['configuration']['LAYERS']['meta']
55 revision = (config['commit'], config['branch'], str(config['commit_count']))
56 if revision not in revisions:
57 revisions[revision] = {}
58 if suite not in revisions[revision]:
59 revisions[revision][suite] = {}
60 revisions[revision][suite][result] = results[suite][result]
61
62 logger.info("Found %d revisions to store" % len(revisions))
63
64 for r in revisions:
65 results = revisions[r]
66 keywords = {'commit': r[0], 'branch': r[1], "commit_count": r[2]}
67 subprocess.check_call(["find", tempdir, "!", "-path", "./.git/*", "-delete"])
68 resultutils.save_resultsdata(results, tempdir)
69
70 logger.info('Storing test result into git repository %s' % args.git_dir)
71
72 gitarchive.gitarchive(tempdir, args.git_dir, False, False,
73 "Results of {branch}:{commit}", "branch: {branch}\ncommit: {commit}", "{branch}",
74 False, "{branch}/{commit_count}-g{commit}/{tag_number}",
75 'Test run #{tag_number} of {branch}:{commit}', '',
76 [], [], False, keywords, logger)
77
78 finally:
79 subprocess.check_call(["rm", "-rf", tempdir])
80
81 return 0
82
83def register_commands(subparsers):
84 """Register subcommands from this plugin"""
85 parser_build = subparsers.add_parser('store', help='store test results into a git repository',
86 description='takes a results file or directory of results files and stores '
87 'them into the destination git repository, splitting out the results '
88 'files as configured',
89 group='setup')
90 parser_build.set_defaults(func=store)
91 parser_build.add_argument('source',
92 help='source file or directory that contain the test result files to be stored')
93 parser_build.add_argument('git_dir',
94 help='the location of the git repository to store the results in')
95 parser_build.add_argument('-a', '--all', action='store_true',
96 help='include all files, not just testresults.json files')
97 parser_build.add_argument('-e', '--allow-empty', action='store_true',
98 help='don\'t error if no results to store are found')
99