• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    '''
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.GetFullName(), self.tag)
80
81    def GetFullName(self):
82        '''Get a string that represents the test.
83
84        Returns:
85            A string test name in format '<test suite>.<test name>' if
86            test_suite is not empty; '<test name>' otherwise
87        '''
88        return '{}.{}'.format(
89            self.test_suite,
90            self.test_name) if self.test_suite else self.test_name
91
92    def GetRunCommand(self):
93        '''Get the command to run the test.
94
95        Returns:
96            String, a command to run the test.
97        '''
98        working_directory = ('cd %s &&' % self.working_directory
99                             if self.working_directory else '')
100
101        envp = 'env %s' % self.envp if self.envp else ''
102        ld_library_path = ('LD_LIBRARY_PATH=%s ' % self.ld_library_path
103                           if self.ld_library_path else '')
104
105        if ld_library_path:
106            envp = ('{}{}'.format(envp, ld_library_path)
107                    if envp else 'env %s' % ld_library_path)
108
109        args = ' %s' % self.args if self.args else ''
110
111        return '{working_directory}{envp}{cmd}{args}'.format(
112            working_directory=working_directory,
113            envp=envp,
114            cmd=self.cmd,
115            args=args)
116
117    @property
118    def test_suite(self):
119        '''Get test_suite'''
120        return self._test_suite
121
122    @test_suite.setter
123    def test_suite(self, test_suite):
124        '''Set test_suite'''
125        self._test_suite = _SafeStrip(test_suite)
126
127    @property
128    def test_name(self):
129        '''Get test_name'''
130        return self._test_name
131
132    @test_name.setter
133    def test_name(self, test_name):
134        '''Set test_name'''
135        self._test_name = _SafeStrip(test_name)
136
137    @property
138    def path(self):
139        '''Get path'''
140        return self._path
141
142    @path.setter
143    def path(self, path):
144        '''Set path'''
145        self._path = _SafeStrip(path)
146
147    @property
148    def cmd(self):
149        '''Get test command. If command is empty, path is returned.'''
150        if not self._cmd:
151            return self.path
152
153        return self._cmd
154
155    @cmd.setter
156    def cmd(self, cmd):
157        '''Set path'''
158        self._cmd = _SafeStrip(cmd)
159
160    @property
161    def tag(self):
162        '''Get tag'''
163        return self._tag
164
165    @tag.setter
166    def tag(self, tag):
167        '''Set tag'''
168        self._tag = _SafeStrip(tag)
169
170    @property
171    def working_directory(self):
172        '''Get working_directory'''
173        return self._working_directory
174
175    @working_directory.setter
176    def working_directory(self, working_directory):
177        '''Set working_directory'''
178        self._working_directory = _SafeStrip(working_directory)
179
180    @property
181    def ld_library_path(self):
182        '''Get ld_library_path'''
183        return self._ld_library_path
184
185    @ld_library_path.setter
186    def ld_library_path(self, ld_library_path):
187        '''Set ld_library_path'''
188        self._ld_library_path = _SafeStrip(ld_library_path)
189
190    @property
191    def envp(self):
192        '''Get envp'''
193        return self._envp
194
195    @envp.setter
196    def envp(self, envp):
197        '''Set env'''
198        self._envp = _SafeStrip(envp)
199
200    @property
201    def args(self):
202        '''Get args'''
203        return self._args
204
205    @args.setter
206    def args(self, args):
207        '''Set args'''
208        self._args = _SafeStrip(args)
209