blob: 37efdcfcbd7407c5260ee5df91caa467a1103cce [file] [log] [blame]
mbligh7c8ea992009-06-22 19:03:08 +00001#!/usr/bin/python
mblighf3964382008-11-18 14:58:22 +00002"""Create new scenario test instance from an existing results directory.
3
4This automates creation of regression tests for the results parsers.
5There are 2 primary use cases for this.
6
71) Bug fixing: Parser broke on some input in the field and we want
8to start with a test that operates on that input and fails. We
9then apply fixes to the parser implementation until it passes.
10
112) Regression alarms: We take input from various real scenarios that
12work as expected with the parser. These will be used to ensure
13we do not break the expected functionality of the parser while
14refactoring it.
15
16While much is done automatically, a scenario harness is meant to
17be easily extended and configured once generated.
18"""
19
20import optparse, os, shutil, sys, tempfile
21from os import path
22
23import common
mblighf3964382008-11-18 14:58:22 +000024from autotest_lib.tko.parsers.test import scenario_base
25
mbligh4e7224b2008-11-19 00:34:11 +000026usage = 'usage: %prog [options] results_dirpath scenerios_dirpath'
mblighf3964382008-11-18 14:58:22 +000027parser = optparse.OptionParser(usage=usage)
28parser.add_option(
29 '-n', '--name',
30 help='Name for new scenario instance. Will use dirname if not specified')
31parser.add_option(
32 '-p', '--parser_result_tag',
33 default='v1',
34 help='Storage tag to use for initial parser result.')
35parser.add_option(
36 '-t', '--template_type',
37 default='base',
38 help='Type of unittest module to copy into new scenario.')
39
40
41def main():
42 (options, args) = parser.parse_args()
mbligh4e7224b2008-11-19 00:34:11 +000043 if len(args) < 2:
mblighf3964382008-11-18 14:58:22 +000044 parser.print_help()
45 sys.exit(1)
46
47 results_dirpath = path.normpath(args[0])
48 if not path.exists(results_dirpath) or not path.isdir(results_dirpath):
49 print 'Invalid results_dirpath:', results_dirpath
50 parser.print_help()
51 sys.exit(1)
52
mbligh4e7224b2008-11-19 00:34:11 +000053 scenarios_dirpath = path.normpath(args[1])
54 if not path.exists(scenarios_dirpath) or not path.isdir(scenarios_dirpath):
55 print 'Invalid scenarios_dirpath:', scenarios_dirpath
56 parser.print_help()
57 sys.exit(1)
58
mblighf3964382008-11-18 14:58:22 +000059 results_dirname = path.basename(results_dirpath)
60 # Not everything is a valid python package name, fix if necessary
61 package_dirname = scenario_base.fix_package_dirname(
62 options.name or results_dirname)
63
64 scenario_package_dirpath = path.join(
mbligh4e7224b2008-11-19 00:34:11 +000065 scenarios_dirpath, package_dirname)
mblighf3964382008-11-18 14:58:22 +000066 if path.exists(scenario_package_dirpath):
67 print (
68 'Scenario package already exists at path: %s' %
69 scenario_package_dirpath)
70 parser.print_help()
71 sys.exit(1)
72
73 # Create new scenario package
74 os.mkdir(scenario_package_dirpath)
75
76 # Create tmp_dir
77 tmp_dirpath = tempfile.mkdtemp()
78 copied_dirpath = path.join(tmp_dirpath, results_dirname)
79 # Copy results_dir
80 shutil.copytree(results_dirpath, copied_dirpath)
81
82 # scenario_base.sanitize_results_data(copied_dirpath)
83
84 # Launch parser on copied_dirpath, collect emitted test objects.
85 harness = scenario_base.new_parser_harness(copied_dirpath)
86 try:
87 parser_result = harness.execute()
88 except Exception, e:
89 parser_result = e
90
91 scenario_base.store_parser_result(
92 scenario_package_dirpath, parser_result,
93 options.parser_result_tag)
94
95 scenario_base.store_results_dir(
96 scenario_package_dirpath, copied_dirpath)
97
98 scenario_base.write_config(
99 scenario_package_dirpath,
100 status_version=harness.status_version,
101 parser_result_tag=options.parser_result_tag,
102 )
103
104 scenario_base.install_unittest_module(
105 scenario_package_dirpath, options.template_type)
106
107
108if __name__ == '__main__':
109 main()