Home | History | Annotate | Download | only in lib
      1 # Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 """Control file generation for the autoupdate_EndToEnd server-side test."""
      6 
      7 import os
      8 import re
      9 
     10 import common
     11 from autotest_lib.client.common_lib import control_data
     12 
     13 
     14 _name_re = re.compile('\s*NAME\s*=')
     15 _autotest_test_name = 'autoupdate_EndToEndTest'
     16 
     17 
     18 def generate_full_control_file(test, env, orig_control_code):
     19     """Returns the parameterized control file for the test config.
     20 
     21     @param test: the test config object (TestConfig)
     22     @param env: the test environment parameters (TestEnv or None)
     23     @param orig_control_code: string containing the template control code
     24 
     25     @returns Parameterized control file based on args (string)
     26 
     27     """
     28     orig_name = control_data.parse_control_string(orig_control_code).name
     29     code_lines = orig_control_code.splitlines()
     30     for i, line in enumerate(code_lines):
     31         if _name_re.match(line):
     32             new_name = '%s_%s' % (orig_name, test.unique_name_suffix())
     33             code_lines[i] = line.replace(orig_name, new_name)
     34             break
     35 
     36     env_code_args = env.get_code_args() if env else ''
     37     return test.get_code_args() + env_code_args + '\n'.join(code_lines) + '\n'
     38 
     39 
     40 def dump_autotest_control_file(test, env, control_code, directory):
     41     """Creates control file for test and returns the path to created file.
     42 
     43     @param test: the test config object (TestConfig)
     44     @param env: the test environment parameters (TestEnv)
     45     @param control_code: string containing the template control code
     46     @param directory: the directory to dump the control file to
     47 
     48     @returns Path to the newly dumped control file
     49 
     50     """
     51     if not os.path.exists(directory):
     52         os.makedirs(directory)
     53 
     54     parametrized_control_code = generate_full_control_file(
     55             test, env, control_code)
     56 
     57     control_file = os.path.join(directory,
     58                                 test.get_control_file_name())
     59     with open(control_file, 'w') as fh:
     60         fh.write(parametrized_control_code)
     61 
     62     return control_file
     63 
     64 
     65 def get_test_name():
     66   """Returns the name of the server side test."""
     67   return _autotest_test_name
     68 
     69 
     70 def get_control_file_name():
     71   """Returns the path of the end-to-end base control file."""
     72   return os.path.join(
     73       common.autotest_dir, 'server', 'site_tests',
     74       get_test_name(), 'control')
     75