1#!/usr/bin/env python
2#
3# Copyright (C) 2017 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18import logging
19
20from vts.runners.host import asserts
21from vts.runners.host import base_test
22from vts.runners.host import const
23from vts.runners.host import test_runner
24
25
26def AssertShellCommandSuccess(command_results, num_of_commands):
27    '''Check shell command result with assertions.
28
29    Given a shell command output, this command checks several things:
30    1. result is not None
31    2. result is not empty
32    3. number of results is consistant with number of commands
33    4. there is no error message on STDERR
34    5. return code of commands are all 0
35
36    Args:
37        command_results: dict, shell command results
38        num_of_commands: int, number of commands
39    '''
40    asserts.assertTrue(command_results is not None,
41                       'command result cannot be None')
42    asserts.assertEqual(len(command_results), 3, 'command result is empty')
43    for item in command_results:
44        asserts.assertEqual(
45            len(command_results[item]), num_of_commands,
46            'number of command result is not %s: %s' % (num_of_commands,
47                                                        command_results))
48    asserts.assertFalse(
49        any(command_results[const.STDERR]),
50        'received error message from stderr: %s' % command_results)
51    asserts.assertFalse(
52        any(command_results[const.EXIT_CODE]),
53        'received non zero return code: %s' % command_results)
54
55
56class VtsSelfTestBaseTest(base_test.BaseTestClass):
57    '''Two hello world test cases which use the shell driver.
58
59    Attributes:
60        is_first_run: bool, whether this test run is the first run with retry attempts.
61    '''
62    run_count = 0
63
64    def setUpClass(self):
65        # Since we are running the actual test cases, run_as_vts_self_test
66        # must be set to False.
67        self.run_as_vts_self_test = False
68
69        self.dut = self.android_devices[0]
70        self.shell = self.dut.shell
71
72    def tearDownClass(self):
73        self.run_count += 1
74
75    def testShellEcho1(self):
76        '''A simple testcase which sends a command.'''
77        results = self.shell.Execute(
78            "echo hello_world")  # runs a shell command.
79        AssertShellCommandSuccess(results, 1)
80        logging.info(str(results[const.STDOUT]))  # prints the stdout
81        asserts.assertEqual(results[const.STDOUT][0].strip(),
82                            "hello_world")  # checks the stdout
83
84    def testShellEcho2(self):
85        '''A simple testcase which sends two commands.'''
86        results = self.shell.Execute(['echo hello', 'echo world'])
87        AssertShellCommandSuccess(results, 2)
88        logging.info(str(results[const.STDOUT]))
89        asserts.assertEqual(results[const.STDOUT][0].strip(), 'hello')
90        asserts.assertEqual(results[const.STDOUT][1].strip(), 'world')
91
92    def testDeviceTotalMem(self):
93        '''Test AndroidDevice class total_memory getter function'''
94        asserts.assertTrue(self.dut.total_memory > 0,
95                           'Failed to get device memory info.')
96
97    def test_getUserConfigStr1(self):
98        '''Test getUserConfigStr.'''
99        asserts.assertEqual(self.getUserConfigStr('a'), 'a')
100
101    def test_getUserConfigStr2(self):
102        '''Test getUserConfigStr.'''
103        asserts.assertEqual(self.getUserConfigStr('b'), 'b')
104
105    def test_getUserConfigStr3(self):
106        '''Test getUserConfigStr.'''
107        asserts.assertEqual(self.getUserConfigStr('c'), None)
108
109    def test_getUserConfigStr4(self):
110        '''Test getUserConfigStr.'''
111        asserts.assertEqual(self.getUserConfigStr('c', to_str=True), None)
112
113    def test_getUserConfigInt1(self):
114        '''Test getUserConfigInt.'''
115        asserts.assertEqual(self.getUserConfigInt('a'), 1)
116
117    def test_getUserConfigInt2(self):
118        '''Test getUserConfigInt.'''
119        asserts.assertEqual(self.getUserConfigInt('b'), 2)
120
121    def test_getUserConfigInt3(self):
122        '''Test getUserConfigInt.'''
123        asserts.assertEqual(self.getUserConfigInt('b', to_str=True), '2')
124
125    def test_getUserConfigInt4(self):
126        '''Test getUserConfigInt.'''
127        asserts.assertEqual(self.getUserConfigInt('c'), None)
128
129    def test_getUserConfigInt5(self):
130        '''Test getUserConfigInt.'''
131        asserts.assertEqual(self.getUserConfigInt('c', to_str=True), None)
132
133    def test_getUserConfigBool1(self):
134        '''Test getUserConfigBool.'''
135        asserts.assertEqual(self.getUserConfigBool('a'), True)
136
137    def test_getUserConfigBool2(self):
138        '''Test getUserConfigBool.'''
139        asserts.assertEqual(self.getUserConfigBool('b'), False)
140
141    def test_getUserConfigBool3(self):
142        '''Test getUserConfigBool.'''
143        asserts.assertEqual(self.getUserConfigBool('b', to_str=True), 'False')
144
145    def test_getUserConfigBool4(self):
146        '''Test getUserConfigBool.'''
147        asserts.assertEqual(self.getUserConfigBool('c'), None)
148
149    def test_getUserConfigBool5(self):
150        '''Test getUserConfigBool.'''
151        asserts.assertEqual(self.getUserConfigBool('c', to_str=True), None)
152
153    def test_retry_run_pass_on_2nd(self):
154        """Tests retry feature."""
155        asserts.assertEqual(self.run_count, 1)
156
157    def test_retry_run_pass_on_3rd(self):
158        """Tests retry feature."""
159        asserts.assertEqual(self.run_count, 2)
160
161if __name__ == "__main__":
162    test_runner.main()
163