test_utils_process.py 8.6 KB
Newer Older
1
import os
2
import sys
3
from mock import MagicMock, patch
4 5 6 7 8

if sys.version_info[:2] == (2, 6):
    import unittest2 as unittest
else:
    import unittest
9

10
from avocado.utils import gdb
11
from avocado.utils import process
12
from avocado.utils import path
13 14 15 16 17


class TestGDBProcess(unittest.TestCase):

    def setUp(self):
18
        self.current_runtime_expr = gdb.GDB_RUN_BINARY_NAMES_EXPR[:]
19 20

    def cleanUp(self):
21
        gdb.GDB_RUN_BINARY_NAMES_EXPR = self.current_runtime_expr
22 23

    def test_should_run_inside_gdb(self):
24
        gdb.GDB_RUN_BINARY_NAMES_EXPR = ['foo']
25 26 27 28
        self.assertTrue(process.should_run_inside_gdb('foo'))
        self.assertTrue(process.should_run_inside_gdb('/usr/bin/foo'))
        self.assertFalse(process.should_run_inside_gdb('/usr/bin/fooz'))

29
        gdb.GDB_RUN_BINARY_NAMES_EXPR.append('foo:main')
30 31 32
        self.assertTrue(process.should_run_inside_gdb('foo'))
        self.assertFalse(process.should_run_inside_gdb('bar'))

33
        gdb.GDB_RUN_BINARY_NAMES_EXPR.append('bar:main.c:5')
34 35 36 37 38
        self.assertTrue(process.should_run_inside_gdb('bar'))
        self.assertFalse(process.should_run_inside_gdb('baz'))
        self.assertTrue(process.should_run_inside_gdb('bar 1 2 3'))
        self.assertTrue(process.should_run_inside_gdb('/usr/bin/bar 1 2 3'))

39 40 41
    def test_should_run_inside_gdb_malformed_command(self):
        gdb.GDB_RUN_BINARY_NAMES_EXPR = ['/bin/virsh']
        cmd = """/bin/virsh node-memory-tune --shm-sleep-millisecs ~!@#$%^*()-=[]{}|_+":;'`,>?. """
42 43 44
        self.assertTrue(process.should_run_inside_gdb(cmd))
        self.assertFalse(process.should_run_inside_gdb("foo bar baz"))
        self.assertFalse(process.should_run_inside_gdb("foo ' "))
45

46
    def test_get_sub_process_klass(self):
47
        gdb.GDB_RUN_BINARY_NAMES_EXPR = []
48 49 50
        self.assertIs(process.get_sub_process_klass('/bin/true'),
                      process.SubProcess)

51
        gdb.GDB_RUN_BINARY_NAMES_EXPR.append('/bin/false')
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
        self.assertIs(process.get_sub_process_klass('/bin/false'),
                      process.GDBSubProcess)
        self.assertIs(process.get_sub_process_klass('false'),
                      process.GDBSubProcess)
        self.assertIs(process.get_sub_process_klass('true'),
                      process.SubProcess)

    def test_split_gdb_expr(self):
        binary, breakpoint = process.split_gdb_expr('foo:debug_print')
        self.assertEqual(binary, 'foo')
        self.assertEqual(breakpoint, 'debug_print')
        binary, breakpoint = process.split_gdb_expr('bar')
        self.assertEqual(binary, 'bar')
        self.assertEqual(breakpoint, 'main')
        binary, breakpoint = process.split_gdb_expr('baz:main.c:57')
        self.assertEqual(binary, 'baz')
        self.assertEqual(breakpoint, 'main.c:57')
        self.assertIsInstance(process.split_gdb_expr('foo'), tuple)
        self.assertIsInstance(process.split_gdb_expr('foo:debug_print'), tuple)

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

def mock_fail_find_cmd(cmd, default=None):
    path_paths = ["/usr/libexec", "/usr/local/sbin", "/usr/local/bin",
                  "/usr/sbin", "/usr/bin", "/sbin", "/bin"]
    raise path.CmdNotFoundError(cmd, path_paths)


class TestProcessRun(unittest.TestCase):

    @patch.object(path, 'find_command',
                  MagicMock(return_value='/usr/bin/sudo'))
    @patch.object(os, 'getuid', MagicMock(return_value=1000))
    def test_subprocess_nosudo(self):
        expected_command = 'ls -l'
        p = process.SubProcess(cmd='ls -l')
        self.assertEqual(p.cmd, expected_command)

    @patch.object(path, 'find_command',
                  MagicMock(return_value='/usr/bin/sudo'))
    @patch.object(os, 'getuid', MagicMock(return_value=0))
    def test_subprocess_nosudo_uid_0(self):
        expected_command = 'ls -l'
        p = process.SubProcess(cmd='ls -l')
        self.assertEqual(p.cmd, expected_command)

    @patch.object(path, 'find_command',
                  MagicMock(return_value='/usr/bin/sudo'))
    @patch.object(os, 'getuid', MagicMock(return_value=1000))
    def test_subprocess_sudo(self):
        expected_command = '/usr/bin/sudo -n ls -l'
        p = process.SubProcess(cmd='ls -l', sudo=True)
        self.assertEqual(p.cmd, expected_command)

    @patch.object(path, 'find_command', mock_fail_find_cmd)
    @patch.object(os, 'getuid', MagicMock(return_value=1000))
    def test_subprocess_sudo_no_sudo_installed(self):
        expected_command = 'ls -l'
        p = process.SubProcess(cmd='ls -l', sudo=True)
        self.assertEqual(p.cmd, expected_command)

    @patch.object(path, 'find_command',
                  MagicMock(return_value='/usr/bin/sudo'))
    @patch.object(os, 'getuid', MagicMock(return_value=0))
    def test_subprocess_sudo_uid_0(self):
        expected_command = 'ls -l'
        p = process.SubProcess(cmd='ls -l', sudo=True)
        self.assertEqual(p.cmd, expected_command)

    @patch.object(path, 'find_command',
                  MagicMock(return_value='/usr/bin/sudo'))
    @patch.object(os, 'getuid', MagicMock(return_value=1000))
    def test_subprocess_sudo_shell(self):
        expected_command = '/usr/bin/sudo -n -s ls -l'
        p = process.SubProcess(cmd='ls -l', sudo=True, shell=True)
        self.assertEqual(p.cmd, expected_command)

    @patch.object(path, 'find_command', mock_fail_find_cmd)
    @patch.object(os, 'getuid', MagicMock(return_value=1000))
    def test_subprocess_sudo_shell_no_sudo_installed(self):
        expected_command = 'ls -l'
        p = process.SubProcess(cmd='ls -l', sudo=True, shell=True)
        self.assertEqual(p.cmd, expected_command)

    @patch.object(path, 'find_command',
                  MagicMock(return_value='/usr/bin/sudo'))
    @patch.object(os, 'getuid', MagicMock(return_value=0))
    def test_subprocess_sudo_shell_uid_0(self):
        expected_command = 'ls -l'
        p = process.SubProcess(cmd='ls -l', sudo=True, shell=True)
        self.assertEqual(p.cmd, expected_command)

    @patch.object(path, 'find_command',
                  MagicMock(return_value='/usr/bin/sudo'))
    @patch.object(os, 'getuid', MagicMock(return_value=1000))
    def test_run_nosudo(self):
        expected_command = 'ls -l'
        p = process.run(cmd='ls -l', ignore_status=True)
        self.assertEqual(p.command, expected_command)

    @patch.object(path, 'find_command',
                  MagicMock(return_value='/usr/bin/sudo'))
    @patch.object(os, 'getuid', MagicMock(return_value=0))
    def test_run_nosudo_uid_0(self):
        expected_command = 'ls -l'
        p = process.run(cmd='ls -l', ignore_status=True)
        self.assertEqual(p.command, expected_command)

    @patch.object(path, 'find_command',
                  MagicMock(return_value='/usr/bin/sudo'))
    @patch.object(os, 'getuid', MagicMock(return_value=1000))
    def test_run_sudo(self):
        expected_command = '/usr/bin/sudo -n ls -l'
        p = process.run(cmd='ls -l', sudo=True, ignore_status=True)
        self.assertEqual(p.command, expected_command)

    @patch.object(path, 'find_command', mock_fail_find_cmd)
    @patch.object(os, 'getuid', MagicMock(return_value=1000))
    def test_run_sudo_no_sudo_installed(self):
        expected_command = 'ls -l'
        p = process.run(cmd='ls -l', sudo=True, ignore_status=True)
        self.assertEqual(p.command, expected_command)

    @patch.object(path, 'find_command',
                  MagicMock(return_value='/usr/bin/sudo'))
    @patch.object(os, 'getuid', MagicMock(return_value=0))
    def test_run_sudo_uid_0(self):
        expected_command = 'ls -l'
        p = process.run(cmd='ls -l', sudo=True, ignore_status=True)
        self.assertEqual(p.command, expected_command)

    @patch.object(path, 'find_command',
                  MagicMock(return_value='/usr/bin/sudo'))
    @patch.object(os, 'getuid', MagicMock(return_value=1000))
    def test_run_sudo_shell(self):
        expected_command = '/usr/bin/sudo -n -s ls -l'
        p = process.run(cmd='ls -l', sudo=True, shell=True, ignore_status=True)
        self.assertEqual(p.command, expected_command)

    @patch.object(path, 'find_command', mock_fail_find_cmd)
    @patch.object(os, 'getuid', MagicMock(return_value=1000))
    def test_run_sudo_shell_no_sudo_installed(self):
        expected_command = 'ls -l'
        p = process.run(cmd='ls -l', sudo=True, shell=True, ignore_status=True)
        self.assertEqual(p.command, expected_command)

    @patch.object(path, 'find_command',
                  MagicMock(return_value='/usr/bin/sudo'))
    @patch.object(os, 'getuid', MagicMock(return_value=0))
    def test_run_sudo_shell_uid_0(self):
        expected_command = 'ls -l'
        p = process.run(cmd='ls -l', sudo=True, shell=True, ignore_status=True)
        self.assertEqual(p.command, expected_command)

205 206
if __name__ == "__main__":
    unittest.main()