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 ''' 56 57 def __init__(self, 58 test_suite, 59 test_name, 60 path, 61 tag='', 62 put_tag_func=operator.add, 63 working_directory=None, 64 ld_library_path=None, 65 profiling_library_path=None, 66 cmd='', 67 envp='', 68 args='', 69 name_appendix=''): 70 self.test_suite = test_suite 71 self.test_name = test_name 72 self.path = path 73 self.tag = tag 74 self.put_tag_func = put_tag_func 75 self.working_directory = working_directory 76 self.ld_library_path = ld_library_path 77 self.profiling_library_path = profiling_library_path 78 self.cmd = cmd 79 self.envp = envp 80 self.args = args 81 self.name_appendix = name_appendix 82 83 def __str__(self): 84 return self._GenerateDisplayName() 85 86 def _GenerateDisplayName(self): 87 '''Get a string of test name for display. 88 89 The display name contains three parts: the original full test name, the 90 name appendix which includes more info about the test run, and the 91 tag(s) used by the test. 92 ''' 93 return self.put_tag_func(self.full_name + self.name_appendix, self.tag) 94 95 @property 96 def name_appendix(self): 97 return self._name_appendix 98 99 @name_appendix.setter 100 def name_appendix(self, name_appendix): 101 self._name_appendix = name_appendix 102 103 @property 104 def full_name(self): 105 '''Get a string that represents the test. 106 107 Returns: 108 A string test name in format '<test suite>.<test name>' if 109 test_suite is not empty; '<test name>' otherwise 110 ''' 111 return getattr(self, '_full_name', '{}.{}'.format( 112 self.test_suite, self.test_name) 113 if self.test_suite else self.test_name) 114 115 @full_name.setter 116 def full_name(self, full_name): 117 self._full_name = full_name 118 119 def GetRunCommand(self): 120 '''Get the command to run the test. 121 122 Returns: 123 String, a command to run the test. 124 ''' 125 working_directory = ('cd %s && ' % self.working_directory 126 if self.working_directory else '') 127 128 envp = 'env %s ' % self.envp if self.envp else '' 129 ld_library_path = ('LD_LIBRARY_PATH=%s ' % self.ld_library_path 130 if self.ld_library_path else '') 131 132 if ld_library_path: 133 envp = ('{}{}'.format(envp, ld_library_path) 134 if envp else 'env %s ' % ld_library_path) 135 136 args = ' %s' % self.args if self.args else '' 137 138 return '{working_directory}{envp}{cmd}{args}'.format( 139 working_directory=working_directory, 140 envp=envp, 141 cmd=self.cmd, 142 args=args) 143 144 @property 145 def test_suite(self): 146 '''Get test_suite''' 147 return self._test_suite 148 149 @test_suite.setter 150 def test_suite(self, test_suite): 151 '''Set test_suite''' 152 self._test_suite = _SafeStrip(test_suite) 153 154 @property 155 def test_name(self): 156 '''Get test_name''' 157 return self._test_name 158 159 @test_name.setter 160 def test_name(self, test_name): 161 '''Set test_name''' 162 self._test_name = _SafeStrip(test_name) 163 164 @property 165 def path(self): 166 '''Get path''' 167 return self._path 168 169 @path.setter 170 def path(self, path): 171 '''Set path''' 172 self._path = _SafeStrip(path) 173 174 @property 175 def cmd(self): 176 '''Get test command. If command is empty, path is returned.''' 177 if not self._cmd: 178 return self.path 179 180 return self._cmd 181 182 @cmd.setter 183 def cmd(self, cmd): 184 '''Set path''' 185 self._cmd = _SafeStrip(cmd) 186 187 @property 188 def tag(self): 189 '''Get tag''' 190 return self._tag 191 192 @tag.setter 193 def tag(self, tag): 194 '''Set tag''' 195 self._tag = _SafeStrip(tag) 196 197 @property 198 def working_directory(self): 199 '''Get working_directory''' 200 return self._working_directory 201 202 @working_directory.setter 203 def working_directory(self, working_directory): 204 '''Set working_directory''' 205 self._working_directory = _SafeStrip(working_directory) 206 207 @property 208 def ld_library_path(self): 209 '''Get ld_library_path''' 210 return self._ld_library_path 211 212 @ld_library_path.setter 213 def ld_library_path(self, ld_library_path): 214 '''Set ld_library_path''' 215 self._ld_library_path = _SafeStrip(ld_library_path) 216 217 @property 218 def envp(self): 219 '''Get envp''' 220 return self._envp 221 222 @envp.setter 223 def envp(self, envp): 224 '''Set env''' 225 self._envp = _SafeStrip(envp) 226 227 @property 228 def args(self): 229 '''Get args''' 230 return self._args 231 232 @args.setter 233 def args(self, args): 234 '''Set args''' 235 self._args = _SafeStrip(args) 236