Home | History | Annotate | Download | only in binary_test
      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 os
     18 import operator
     19 import ntpath
     20 
     21 
     22 def _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 
     36 class 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     '''
     53 
     54     def __init__(self,
     55                  test_suite,
     56                  test_name,
     57                  path,
     58                  tag='',
     59                  put_tag_func=operator.add,
     60                  working_directory=None,
     61                  ld_library_path=None,
     62                  profiling_library_path=None,
     63                  cmd='',
     64                  envp='',
     65                  args=''):
     66         self.test_suite = test_suite
     67         self.test_name = test_name
     68         self.path = path
     69         self.tag = tag
     70         self.put_tag_func = put_tag_func
     71         self.working_directory = working_directory
     72         self.ld_library_path = ld_library_path
     73         self.profiling_library_path = profiling_library_path
     74         self.cmd = cmd
     75         self.envp = envp
     76         self.args = args
     77 
     78     def __str__(self):
     79         return self.put_tag_func(self.full_name, self.tag)
     80 
     81     @property
     82     def full_name(self):
     83         '''Get a string that represents the test.
     84 
     85         Returns:
     86             A string test name in format '<test suite>.<test name>' if
     87             test_suite is not empty; '<test name>' otherwise
     88         '''
     89         return getattr(self, '_full_name', '{}.{}'.format(
     90             self.test_suite, self.test_name)
     91                        if self.test_suite else self.test_name)
     92 
     93     @full_name.setter
     94     def full_name(self, full_name):
     95         self._full_name = full_name
     96 
     97     def GetRunCommand(self):
     98         '''Get the command to run the test.
     99 
    100         Returns:
    101             String, a command to run the test.
    102         '''
    103         working_directory = ('cd %s && ' % self.working_directory
    104                              if self.working_directory else '')
    105 
    106         envp = 'env %s ' % self.envp if self.envp else ''
    107         ld_library_path = ('LD_LIBRARY_PATH=%s ' % self.ld_library_path
    108                            if self.ld_library_path else '')
    109 
    110         if ld_library_path:
    111             envp = ('{}{}'.format(envp, ld_library_path)
    112                     if envp else 'env %s ' % ld_library_path)
    113 
    114         args = ' %s' % self.args if self.args else ''
    115 
    116         return '{working_directory}{envp}{cmd}{args}'.format(
    117             working_directory=working_directory,
    118             envp=envp,
    119             cmd=self.cmd,
    120             args=args)
    121 
    122     @property
    123     def test_suite(self):
    124         '''Get test_suite'''
    125         return self._test_suite
    126 
    127     @test_suite.setter
    128     def test_suite(self, test_suite):
    129         '''Set test_suite'''
    130         self._test_suite = _SafeStrip(test_suite)
    131 
    132     @property
    133     def test_name(self):
    134         '''Get test_name'''
    135         return self._test_name
    136 
    137     @test_name.setter
    138     def test_name(self, test_name):
    139         '''Set test_name'''
    140         self._test_name = _SafeStrip(test_name)
    141 
    142     @property
    143     def path(self):
    144         '''Get path'''
    145         return self._path
    146 
    147     @path.setter
    148     def path(self, path):
    149         '''Set path'''
    150         self._path = _SafeStrip(path)
    151 
    152     @property
    153     def cmd(self):
    154         '''Get test command. If command is empty, path is returned.'''
    155         if not self._cmd:
    156             return self.path
    157 
    158         return self._cmd
    159 
    160     @cmd.setter
    161     def cmd(self, cmd):
    162         '''Set path'''
    163         self._cmd = _SafeStrip(cmd)
    164 
    165     @property
    166     def tag(self):
    167         '''Get tag'''
    168         return self._tag
    169 
    170     @tag.setter
    171     def tag(self, tag):
    172         '''Set tag'''
    173         self._tag = _SafeStrip(tag)
    174 
    175     @property
    176     def working_directory(self):
    177         '''Get working_directory'''
    178         return self._working_directory
    179 
    180     @working_directory.setter
    181     def working_directory(self, working_directory):
    182         '''Set working_directory'''
    183         self._working_directory = _SafeStrip(working_directory)
    184 
    185     @property
    186     def ld_library_path(self):
    187         '''Get ld_library_path'''
    188         return self._ld_library_path
    189 
    190     @ld_library_path.setter
    191     def ld_library_path(self, ld_library_path):
    192         '''Set ld_library_path'''
    193         self._ld_library_path = _SafeStrip(ld_library_path)
    194 
    195     @property
    196     def envp(self):
    197         '''Get envp'''
    198         return self._envp
    199 
    200     @envp.setter
    201     def envp(self, envp):
    202         '''Set env'''
    203         self._envp = _SafeStrip(envp)
    204 
    205     @property
    206     def args(self):
    207         '''Get args'''
    208         return self._args
    209 
    210     @args.setter
    211     def args(self, args):
    212         '''Set args'''
    213         self._args = _SafeStrip(args)
    214