Home | History | Annotate | Download | only in shell
      1 #!/usr/bin/env python
      2 #
      3 # Copyright (C) 2016 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 
     18 import logging
     19 
     20 from vts.runners.host import asserts
     21 from vts.runners.host import base_test
     22 from vts.runners.host import test_runner
     23 from vts.utils.python.controllers import android_device
     24 from vts.runners.host import const
     25 
     26 
     27 class SampleShellTest(base_test.BaseTestClass):
     28     """A sample testcase for the shell driver."""
     29 
     30     REPEAT_COUNT = 10
     31 
     32     def setUpClass(self):
     33         self.dut = self.registerController(android_device)[0]
     34 
     35     def testOneCommand(self):
     36         """A simple testcase which just emulates a normal usage pattern."""
     37         self.dut.shell.InvokeTerminal("my_shell1")
     38         results = self.dut.shell.my_shell1.Execute("which ls")
     39         logging.info(str(results[const.STDOUT]))
     40         asserts.assertEqual(len(results[const.STDOUT]), 1)
     41         asserts.assertEqual(results[const.STDOUT][0].strip(), "/system/bin/ls")
     42         asserts.assertEqual(results[const.EXIT_CODE][0], 0)
     43 
     44     def testCommandList(self):
     45         """A simple testcase which just emulates a normal usage pattern."""
     46         self.dut.shell.InvokeTerminal("my_shell2")
     47         results = self.dut.shell.my_shell2.Execute(["which ls"] *
     48                                                    self.REPEAT_COUNT)
     49         logging.info(str(results[const.STDOUT]))
     50         asserts.assertEqual(len(results[const.STDOUT]), self.REPEAT_COUNT)
     51         for index in range(self.REPEAT_COUNT):
     52             asserts.assertEqual(results[const.STDOUT][index].strip(),
     53                                 "/system/bin/ls")
     54             asserts.assertEqual(results[const.EXIT_CODE][index], 0)
     55 
     56     def testMultipleCommands(self):
     57         """A simple testcase which just emulates a normal usage pattern."""
     58         self.dut.shell.InvokeTerminal("my_shell3")
     59         for _ in range(self.REPEAT_COUNT):
     60             results = self.dut.shell.my_shell3.Execute("which ls")
     61             logging.info(str(results[const.STDOUT]))
     62             asserts.assertEqual(len(results[const.STDOUT]), 1)
     63             asserts.assertEqual(results[const.STDOUT][0].strip(),
     64                                 "/system/bin/ls")
     65             asserts.assertEqual(results[const.EXIT_CODE][0], 0)
     66 
     67     def testCommandSequenceCd(self):
     68         """A simple test case that emulates using cd bash command sequence
     69            connected by '&&' under normal usage pattern.
     70         """
     71         self.dut.shell.InvokeTerminal("command_sequence_cd")
     72         directory = "/data/local"
     73         commands = ["cd %s && pwd" % directory, "'cd' '%s' && 'pwd'" %
     74                     directory, "\"cd\" \"%s\" && \"pwd\"" % directory]
     75         for cmd in commands:
     76             results = self.dut.shell.command_sequence_cd.Execute(cmd)
     77             asserts.assertEqual(results[const.EXIT_CODE][0], 0)
     78             asserts.assertEqual(results[const.STDOUT][0].strip(), directory)
     79 
     80     def testCommandSequenceExport(self):
     81         """A simple test case that emulates using export bash command sequence
     82            connected by '&&' under normal usage pattern.
     83         """
     84         self.dut.shell.InvokeTerminal("command_sequence_export")
     85         var_value = "helloworld"
     86         results = self.dut.shell.command_sequence_export.Execute(
     87             "export {var_name}={var_value} && echo ${var_name}".format(
     88                 var_name="TESTTMPVAR", var_value=var_value))
     89         asserts.assertEqual(results[const.EXIT_CODE][0], 0)
     90         asserts.assertEqual(results[const.STDOUT][0].strip(), var_value)
     91 
     92     def testCommandSequenceMktemp(self):
     93         """A simple test case that emulates using mktemp bash command sequence
     94            connected by '&&' under normal usage pattern.
     95         """
     96         self.dut.shell.InvokeTerminal("command_sequence_mktemp")
     97         results = self.dut.shell.command_sequence_mktemp.Execute(
     98             "TMPFILE=`mktemp /data/local/tmp/test.XXXXXXXXXXXX` "
     99             "&& ls $TMPFILE")
    100         asserts.assertEqual(results[const.EXIT_CODE][0], 0)
    101 
    102     def testMultipleShells(self):
    103         """A simple testcase which just emulates a normal usage pattern."""
    104         for index in range(self.REPEAT_COUNT):
    105             current_shell_name = "shell%s" % index
    106             self.dut.shell.InvokeTerminal(current_shell_name)
    107             current_shell = getattr(self.dut.shell, current_shell_name)
    108             results = current_shell.Execute("which ls")
    109             logging.info(str(results[const.STDOUT]))
    110             asserts.assertEqual(len(results[const.STDOUT]), 1)
    111             asserts.assertEqual(results[const.STDOUT][0].strip(),
    112                                 "/system/bin/ls")
    113             asserts.assertEqual(results[const.EXIT_CODE][0], 0)
    114 
    115 
    116 if __name__ == "__main__":
    117     test_runner.main()
    118