• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python3
2"""Unit tests for shell_wrapper.py."""
3
4import unittest
5from unittest import mock
6
7from autotest_lib.client.cros.faft.utils import shell_wrapper
8
9
10class TestLocalShell(unittest.TestCase):
11    """Tests for shell_wrapper.LocalShell()."""
12
13    @mock.patch('subprocess.Popen')
14    def testSuccessTokenAbsent(self, mock_subproc_popen):
15        cmd = 'foo'
16        success_token = 'unexpected'
17        mock_process = mock.Mock()
18        mock_subproc_popen.return_value = mock_process
19        attrs = {
20                'communicate.return_value': (b'sucessfully executed foo', b'')
21        }
22        mock_process.configure_mock(**attrs)
23        os_if = mock.Mock()
24        local_shell = shell_wrapper.LocalShell(os_if)
25        self.assertFalse(
26                local_shell.run_command_check_output(cmd, success_token))
27
28    @mock.patch('subprocess.Popen')
29    def testSuccessTokenPresent(self, mock_subproc_popen):
30        cmd = 'bar'
31        success_token = 'expected'
32        mock_process = mock.Mock()
33        mock_subproc_popen.return_value = mock_process
34        attrs = {
35                'communicate.return_value':
36                (b'successfully executed bar. expected is expected.', b'')
37        }
38        mock_process.configure_mock(**attrs)
39        os_if = mock.Mock()
40        local_shell = shell_wrapper.LocalShell(os_if)
41        self.assertTrue(
42                local_shell.run_command_check_output(cmd, success_token))
43
44    @mock.patch('subprocess.Popen')
45    def testSuccessTokenMalformed(self, mock_subproc_popen):
46        cmd = 'baz'
47        success_token = 'malformed token \n'
48        mock_process = mock.Mock()
49        mock_subproc_popen.return_value = mock_process
50        attrs = {
51                'communicate.return_value': (b'successfully executed baz', b'')
52        }
53        mock_process.configure_mock(**attrs)
54        os_if = mock.Mock()
55        local_shell = shell_wrapper.LocalShell(os_if)
56        self.assertRaises(shell_wrapper.UnsupportedSuccessToken,
57                          local_shell.run_command_check_output, cmd,
58                          success_token)
59
60
61if __name__ == '__main__':
62    unittest.main()
63