store.py 4.7 KB

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