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 17import os 18import operator 19import ntpath 20 21 22def _SafeStrip(value): 23 '''Strip string value if value is not None. 24 25 Args: 26 value: string, value to strip 27 28 Returns: 29 stripped string; None if input value is None. 30 ''' 31 if value is None: 32 return value 33 return value.strip() 34 35 36class BinaryTestCase(object): 37 '''A class to represent a binary test case. 38 39 Attributes: 40 test_suite: string, test suite name. 41 test_name: string, test case name which does not include test suite. 42 path: string, absolute test binary path on device. 43 tag: string, test tag. 44 put_tag_func: function that takes a name and tag to output a combination. 45 working_directory: string, working directory to call the command. 46 ld_library_path: string, a path for LD_LIBRARY_PATH environment variable. 47 profiling_library_path: string, path to lookup and load VTS profiling libraries. 48 cmd: string, a shell command to execute the test case. If empty, path will be used. 49 envp: string, environment veriable. shoud be in format 'name1=value1 name2=value2...' 50 Will be called using 'env <envp> <cmd> <args>' 51 args: string, arguments following cmd. 52 name_appendix: string, appendix attached to the test name in display, 53 typically contains info of parameters used in the test, 54 e.e. service name used for hal hidl test. 55 filter_file: string, a path pointing to the file containing the filters. 56 ''' 57 58 def __init__(self, 59 test_suite, 60 test_name, 61 path, 62 tag='', 63 put_tag_func=operator.add, 64 working_directory=None, 65 ld_library_path=None, 66 profiling_library_path=None, 67 cmd='', 68 envp='', 69 args='', 70 name_appendix=''): 71 self.test_suite = test_suite 72 self.test_name = test_name 73 self.path = path 74 self.tag = tag 75 self.put_tag_func = put_tag_func 76 self.working_directory = working_directory 77 self.ld_library_path = ld_library_path 78 self.profiling_library_path = profiling_library_path 79 self.cmd = cmd 80 self.envp = envp 81 self.args = args 82 self.name_appendix = name_appendix 83 self.filter_file = None 84 85 def __str__(self): 86 return self._GenerateDisplayName() 87 88 def _GenerateDisplayName(self): 89 '''Get a string of test name for display. 90 91 The display name contains three parts: the original full test name, the 92 name appendix which includes more info about the test run, and the 93 tag(s) used by the test. 94 ''' 95 return self.put_tag_func(self.full_name + self.name_appendix, self.tag) 96 97 @property 98 def name_appendix(self): 99 return self._name_appendix 100 101 @name_appendix.setter 102 def name_appendix(self, name_appendix): 103 self._name_appendix = name_appendix 104 105 @property 106 def full_name(self): 107 '''Get a string that represents the test. 108 109 Returns: 110 A string test name in format '<test suite>.<test name>' if 111 test_suite is not empty; '<test name>' otherwise 112 ''' 113 return getattr(self, '_full_name', '{}.{}'.format( 114 self.test_suite, self.test_name) 115 if self.test_suite else self.test_name) 116 117 @full_name.setter 118 def full_name(self, full_name): 119 self._full_name = full_name 120 121 def GetRunCommand(self): 122 '''Get the command to run the test. 123 124 Returns: 125 String, a command to run the test. 126 ''' 127 working_directory = ('cd %s && ' % self.working_directory 128 if self.working_directory else '') 129 130 envp = 'env %s ' % self.envp if self.envp else '' 131 ld_library_path = ('LD_LIBRARY_PATH=%s ' % self.ld_library_path 132 if self.ld_library_path else '') 133 134 if ld_library_path: 135 envp = ('{}{}'.format(envp, ld_library_path) 136 if envp else 'env %s ' % ld_library_path) 137 138 args = ' %s' % self.args if self.args else '' 139 140 return '{working_directory}{envp}{cmd}{args}'.format( 141 working_directory=working_directory, 142 envp=envp, 143 cmd=self.cmd, 144 args=args) 145 146 @property 147 def test_suite(self): 148 '''Get test_suite''' 149 return self._test_suite 150 151 @test_suite.setter 152 def test_suite(self, test_suite): 153 '''Set test_suite''' 154 self._test_suite = _SafeStrip(test_suite) 155 156 @property 157 def test_name(self): 158 '''Get test_name''' 159 return self._test_name 160 161 @test_name.setter 162 def test_name(self, test_name): 163 '''Set test_name''' 164 self._test_name = _SafeStrip(test_name) 165 166 @property 167 def path(self): 168 '''Get path''' 169 return self._path 170 171 @path.setter 172 def path(self, path): 173 '''Set path''' 174 self._path = _SafeStrip(path) 175 176 @property 177 def cmd(self): 178 '''Get test command. If command is empty, path is returned.''' 179 if not self._cmd: 180 return self.path 181 182 return self._cmd 183 184 @cmd.setter 185 def cmd(self, cmd): 186 '''Set path''' 187 self._cmd = _SafeStrip(cmd) 188 189 @property 190 def tag(self): 191 '''Get tag''' 192 return self._tag 193 194 @tag.setter 195 def tag(self, tag): 196 '''Set tag''' 197 self._tag = _SafeStrip(tag) 198 199 @property 200 def working_directory(self): 201 '''Get working_directory''' 202 return self._working_directory 203 204 @working_directory.setter 205 def working_directory(self, working_directory): 206 '''Set working_directory''' 207 self._working_directory = _SafeStrip(working_directory) 208 209 @property 210 def ld_library_path(self): 211 '''Get ld_library_path''' 212 return self._ld_library_path 213 214 @ld_library_path.setter 215 def ld_library_path(self, ld_library_path): 216 '''Set ld_library_path''' 217 self._ld_library_path = _SafeStrip(ld_library_path) 218 219 @property 220 def envp(self): 221 '''Get envp''' 222 return self._envp 223 224 @envp.setter 225 def envp(self, envp): 226 '''Set env''' 227 self._envp = _SafeStrip(envp) 228 229 @property 230 def args(self): 231 '''Get args''' 232 return self._args 233 234 @args.setter 235 def args(self, args): 236 '''Set args''' 237 self._args = _SafeStrip(args) 238