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
26class VtsCodelabHelloWorldTest(base_test.BaseTestClass):
27    '''Two hello world test cases which use the shell driver.'''
28
29    def setUpClass(self):
30        logging.info('number of device: %s', self.android_devices)
31
32        asserts.assertEqual(
33            len(self.android_devices), 2, 'number of device is wrong.')
34
35        self.dut1 = self.android_devices[0]
36        self.dut2 = self.android_devices[1]
37        self.shell1 = self.dut1.shell
38        self.shell2 = self.dut2.shell
39
40    def testHelloWorld(self):
41        '''A simple testcase which sends a hello world command.'''
42        command = 'echo Hello World!'
43
44        res1 = self.shell1.Execute(command)
45        res2 = self.shell2.Execute(command)
46
47        asserts.assertFalse(
48            any(res1[const.EXIT_CODE]),
49            'command for device 1 failed: %s' % res1)  # checks the exit code
50        asserts.assertFalse(
51            any(res2[const.EXIT_CODE]),
52            'command for device 2 failed: %s' % res2)  # checks the exit code
53
54    def testSerialNotEqual(self):
55        '''Checks serial number from two device not being equal.'''
56        command = 'getprop | grep ro.serial'
57
58        res1 = self.shell1.Execute(command)
59        res2 = self.shell2.Execute(command)
60
61        asserts.assertFalse(
62            any(res1[const.EXIT_CODE]),
63            'command for device 1 failed: %s' % res1)  # checks the exit code
64        asserts.assertFalse(
65            any(res2[const.EXIT_CODE]),
66            'command for device 2 failed: %s' % res2)  # checks the exit code
67
68        def getSerial(output):
69            '''Get serial from getprop query'''
70            return output.strip().split(' ')[-1][1:-1]
71
72        serial1 = getSerial(res1[const.STDOUT][0])
73        serial2 = getSerial(res2[const.STDOUT][0])
74
75        logging.info('Serial number of device 1: %s', serial1)
76        logging.info('Serial number of device 2: %s', serial2)
77
78        asserts.assertNotEqual(
79            serial1, serial2,
80            'serials from two devices should not be the same')
81
82
83if __name__ == '__main__':
84    test_runner.main()
85