blob: e4a0807528714d7d496a79e7245d40768277043c [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)
Brad Bishop19323692019-04-05 15:28:33 -040032 if os.path.isfile(args.source):
33 resultutils.append_resultsdata(results, args.source)
34 else:
35 for root, dirs, files in os.walk(args.source):
36 for name in files:
37 f = os.path.join(root, name)
38 if name == "testresults.json":
39 resultutils.append_resultsdata(results, f)
40 elif args.all:
41 dst = f.replace(args.source, tempdir + "/")
42 os.makedirs(os.path.dirname(dst), exist_ok=True)
43 shutil.copyfile(f, dst)
Brad Bishop40320b12019-03-26 16:08:25 -040044
45 revisions = {}
46
47 if not results and not args.all:
48 if args.allow_empty:
49 logger.info("No results found to store")
50 return 0
51 logger.error("No results found to store")
52 return 1
53
54 # Find the branch/commit/commit_count and ensure they all match
55 for suite in results:
56 for result in results[suite]:
57 config = results[suite][result]['configuration']['LAYERS']['meta']
58 revision = (config['commit'], config['branch'], str(config['commit_count']))
59 if revision not in revisions:
60 revisions[revision] = {}
61 if suite not in revisions[revision]:
62 revisions[revision][suite] = {}
63 revisions[revision][suite][result] = results[suite][result]
64
65 logger.info("Found %d revisions to store" % len(revisions))
66
67 for r in revisions:
68 results = revisions[r]
69 keywords = {'commit': r[0], 'branch': r[1], "commit_count": r[2]}
70 subprocess.check_call(["find", tempdir, "!", "-path", "./.git/*", "-delete"])
Brad Bishop19323692019-04-05 15:28:33 -040071 resultutils.save_resultsdata(results, tempdir, ptestlogs=True)
Brad Bishop40320b12019-03-26 16:08:25 -040072
73 logger.info('Storing test result into git repository %s' % args.git_dir)
74
75 gitarchive.gitarchive(tempdir, args.git_dir, False, False,
76 "Results of {branch}:{commit}", "branch: {branch}\ncommit: {commit}", "{branch}",
77 False, "{branch}/{commit_count}-g{commit}/{tag_number}",
78 'Test run #{tag_number} of {branch}:{commit}', '',
79 [], [], False, keywords, logger)
80
81 finally:
82 subprocess.check_call(["rm", "-rf", tempdir])
83
84 return 0
85
86def register_commands(subparsers):
87 """Register subcommands from this plugin"""
88 parser_build = subparsers.add_parser('store', help='store test results into a git repository',
89 description='takes a results file or directory of results files and stores '
90 'them into the destination git repository, splitting out the results '
91 'files as configured',
92 group='setup')
93 parser_build.set_defaults(func=store)
94 parser_build.add_argument('source',
95 help='source file or directory that contain the test result files to be stored')
96 parser_build.add_argument('git_dir',
97 help='the location of the git repository to store the results in')
98 parser_build.add_argument('-a', '--all', action='store_true',
99 help='include all files, not just testresults.json files')
100 parser_build.add_argument('-e', '--allow-empty', action='store_true',
101 help='don\'t error if no results to store are found')
102