1 # Copyright 2015 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 import logging 6 import json 7 import os 8 9 import common 10 from autotest_lib.client.common_lib import error 11 from autotest_lib.client.common_lib import global_config 12 from autotest_lib.server import test 13 14 15 CONFIG_FOLDER_LOCATION = global_config.global_config.get_config_value( 16 'ACTS', 'acts_config_folder', default='') 17 18 TEST_CONFIG_FILE_FOLDER = 'autotest_config' 19 TEST_CAMPAIGN_FILE_FOLDER = 'autotest_campaign' 20 21 class android_ACTS(test.test): 22 '''Run an Android CTS test case.''' 23 version = 1 24 acts_result_to_autotest = { 25 'PASS': 'GOOD', 26 'FAIL': 'FAIL', 27 'UNKNOWN': 'WARN', 28 'SKIP': 'ABORT' 29 } 30 31 def fetch_file(self, input_path, sub_dir_name): 32 """Ensures the file specified by a path exists locally. If the file 33 specified by input_path does not exist, attempt to locate it in ACTS 34 dirctory. 35 36 @param input_path: A string that's the path to a file. 37 @param sub_dir_name: A string that's the subdirectory name of where the 38 file exists. 39 """ 40 if os.path.exists(input_path): 41 self.test_station.send_file(input_path, self.ts_tempfolder) 42 return 43 actual_path = os.path.join(CONFIG_FOLDER_LOCATION, 44 sub_dir_name, 45 input_path) 46 actual_path = os.path.realpath(actual_path) 47 if not os.path.exists(actual_path): 48 raise error.TestFail('File: %s does not exist' % actual_path) 49 self.test_station.send_file(actual_path, self.ts_tempfolder) 50 51 52 def run_once(self, testbed=None, config_file=None, testbed_name=None, 53 test_case=None, test_file=None): 54 """Run ACTS on the DUT. 55 56 Exactly one of test_case and test_file should be provided. 57 58 @param testbed: Testbed representing the testbed under test. Required. 59 @param config_file: Path to config file locally. Required. 60 @param testbed_name: A string that's passed to act.py's -tb option. 61 Required. 62 @param test_case: A string that's passed to act.py's -tc option. 63 @param test_file: A string that's passed to act.py's -tf option. 64 """ 65 self.test_station = testbed.get_test_station() 66 # Get a tempfolder on the device. 67 self.ts_tempfolder = self.test_station.get_tmp_dir() 68 if not config_file: 69 raise error.TestFail('A config file must be specified.') 70 self.fetch_file(config_file, TEST_CONFIG_FILE_FOLDER) 71 72 if test_file: 73 self.fetch_file(test_file, TEST_CAMPAIGN_FILE_FOLDER) 74 act_base_cmd = 'act.py -c %s -tb %s ' % ( 75 os.path.join(self.ts_tempfolder, os.path.basename(config_file)), 76 testbed_name) 77 # Run the acts script. 78 if test_case: 79 act_cmd = '%s -tc %s' % (act_base_cmd, test_case) 80 elif test_file: 81 act_cmd = '%s -tf %s' % (act_base_cmd, 82 os.path.join(self.ts_tempfolder, os.path.basename(test_file))) 83 else: 84 raise error.TestFail('No test was specified, abort!') 85 logging.debug('Running: %s', act_cmd) 86 # TODO: Change below to be test_bed.teststation_host.run 87 act_result = self.test_station.run(act_cmd) 88 logging.debug('ACTS Output:\n%s', act_result.stdout) 89 90 # Transport all the logs to local. 91 with open(config_file, 'r') as f: 92 configs = json.load(f) 93 log_path = os.path.join(configs['logpath'], testbed_name, 'latest') 94 self.test_station.get_file(log_path, self.resultsdir) 95 # Load summary json file. 96 summary_path = os.path.join(self.resultsdir, 97 'latest', 98 'test_run_summary.json') 99 with open(summary_path, 'r') as f: 100 results = json.load(f)['Results'] 101 # Report results to Autotest. 102 for result in results: 103 verdict = self.acts_result_to_autotest[result['Result']] 104 details = result['Details'] 105 self.job.record(verdict, None, test_case, status=(details or '')) 106