1 # 2 # Copyright (C) 2016 The Android Open Source Project 3 # 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 # 16 17 import logging 18 import re 19 import uuid 20 21 from vts.runners.host import utils 22 from vts.testcases.template.binary_test import binary_test_case 23 from vts.utils.python.os import path_utils 24 25 26 class GtestTestCase(binary_test_case.BinaryTestCase): 27 '''A class to represent a gtest test case. 28 29 Attributes: 30 test_suite: string, test suite name 31 test_name: string, test case name which does not include test suite 32 path: string, absolute test binary path on device 33 tag: string, test tag 34 put_tag_func: function that takes a name and tag to output a combination 35 output_file_path: string, gtest output xml path on device 36 ''' 37 38 # @Override 39 def GetRunCommand(self, output_file_path=None, test_name=None): 40 '''Get the command to run the test. 41 42 Args: 43 output_file_path: file to store the gtest results. 44 test_name: name of the gtest test case. 45 Returns: 46 List of strings 47 ''' 48 if output_file_path: 49 self.output_file_path = output_file_path 50 if not test_name: 51 test_name = self.full_name 52 return [('{cmd} --gtest_filter={test} ' 53 '--gtest_output=xml:{output_file_path}').format( 54 cmd=super(GtestTestCase, self).GetRunCommand(), 55 test=test_name, 56 output_file_path=self.output_file_path), 57 'cat {output} && rm -rf {output}'.format( 58 output=self.output_file_path)] 59 60 @property 61 def output_file_path(self): 62 """Get output_file_path""" 63 if not hasattr(self, 64 '_output_file_path') or self._output_file_path is None: 65 self.output_file_path = '{directory}/gtest_output_{name}.xml'.format( 66 directory=path_utils.TargetDirName(self.path), 67 name=re.sub(r'\W+', '_', str(self))) 68 return self._output_file_path 69 70 @output_file_path.setter 71 def output_file_path(self, output_file_path): 72 """Set output_file_path. 73 74 Lengths of both file name and path will be checked. If longer than 75 maximum allowance, file name will be set to a random name, and 76 directory will be set to relative directory. 77 78 Args: 79 output_file_path: string, intended path of output xml file 80 """ 81 output_file_path = path_utils.TargetNormPath(output_file_path.strip()) 82 output_base_name = path_utils.TargetBaseName(output_file_path) 83 output_dir_name = path_utils.TargetDirName(output_file_path) 84 85 if len(output_base_name) > utils.MAX_FILENAME_LEN: 86 logging.warn('File name of output file "{}" is longer than {}.'. 87 format(output_file_path, utils.MAX_FILENAME_LEN)) 88 output_base_name = '{}.xml'.format(uuid.uuid4()) 89 output_file_path = path_utils.JoinTargetPath( 90 output_dir_name, output_base_name) 91 logging.info('Output file path is set as "%s".', output_file_path) 92 93 if len(output_file_path) > utils.MAX_PATH_LEN: 94 logging.warn('File path of output file "{}" is longer than {}.'. 95 format(output_file_path, utils.MAX_PATH_LEN)) 96 output_file_path = output_base_name 97 logging.info('Output file path is set as "%s".', output_file_path) 98 99 self._output_file_path = output_file_path 100