mbligh | 7c8ea99 | 2009-06-22 19:03:08 +0000 | [diff] [blame^] | 1 | #!/usr/bin/python |
mbligh | f396438 | 2008-11-18 14:58:22 +0000 | [diff] [blame] | 2 | """Create new scenario test instance from an existing results directory. |
| 3 | |
| 4 | This automates creation of regression tests for the results parsers. |
| 5 | There are 2 primary use cases for this. |
| 6 | |
| 7 | 1) Bug fixing: Parser broke on some input in the field and we want |
| 8 | to start with a test that operates on that input and fails. We |
| 9 | then apply fixes to the parser implementation until it passes. |
| 10 | |
| 11 | 2) Regression alarms: We take input from various real scenarios that |
| 12 | work as expected with the parser. These will be used to ensure |
| 13 | we do not break the expected functionality of the parser while |
| 14 | refactoring it. |
| 15 | |
| 16 | While much is done automatically, a scenario harness is meant to |
| 17 | be easily extended and configured once generated. |
| 18 | """ |
| 19 | |
| 20 | import optparse, os, shutil, sys, tempfile |
| 21 | from os import path |
| 22 | |
| 23 | import common |
mbligh | f396438 | 2008-11-18 14:58:22 +0000 | [diff] [blame] | 24 | from autotest_lib.tko.parsers.test import scenario_base |
| 25 | |
mbligh | 4e7224b | 2008-11-19 00:34:11 +0000 | [diff] [blame] | 26 | usage = 'usage: %prog [options] results_dirpath scenerios_dirpath' |
mbligh | f396438 | 2008-11-18 14:58:22 +0000 | [diff] [blame] | 27 | parser = optparse.OptionParser(usage=usage) |
| 28 | parser.add_option( |
| 29 | '-n', '--name', |
| 30 | help='Name for new scenario instance. Will use dirname if not specified') |
| 31 | parser.add_option( |
| 32 | '-p', '--parser_result_tag', |
| 33 | default='v1', |
| 34 | help='Storage tag to use for initial parser result.') |
| 35 | parser.add_option( |
| 36 | '-t', '--template_type', |
| 37 | default='base', |
| 38 | help='Type of unittest module to copy into new scenario.') |
| 39 | |
| 40 | |
| 41 | def main(): |
| 42 | (options, args) = parser.parse_args() |
mbligh | 4e7224b | 2008-11-19 00:34:11 +0000 | [diff] [blame] | 43 | if len(args) < 2: |
mbligh | f396438 | 2008-11-18 14:58:22 +0000 | [diff] [blame] | 44 | 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 | |
mbligh | 4e7224b | 2008-11-19 00:34:11 +0000 | [diff] [blame] | 53 | 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 | |
mbligh | f396438 | 2008-11-18 14:58:22 +0000 | [diff] [blame] | 59 | 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( |
mbligh | 4e7224b | 2008-11-19 00:34:11 +0000 | [diff] [blame] | 65 | scenarios_dirpath, package_dirname) |
mbligh | f396438 | 2008-11-18 14:58:22 +0000 | [diff] [blame] | 66 | 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 | |
| 108 | if __name__ == '__main__': |
| 109 | main() |