test_utils_process.py 9.7 KB
Newer Older
1
import os
2
import unittest
3 4 5 6 7 8

try:
    from unittest import mock
except ImportError:
    import mock

9

10
from avocado.utils import gdb
11
from avocado.utils import process
12
from avocado.utils import path
13

14 15
TRUE_CMD = path.find_command('true')

16 17 18 19

class TestGDBProcess(unittest.TestCase):

    def setUp(self):
20
        self.current_runtime_expr = gdb.GDB_RUN_BINARY_NAMES_EXPR[:]
21 22

    def cleanUp(self):
23
        gdb.GDB_RUN_BINARY_NAMES_EXPR = self.current_runtime_expr
24 25

    def test_should_run_inside_gdb(self):
26
        gdb.GDB_RUN_BINARY_NAMES_EXPR = ['foo']
27 28 29 30
        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'))

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

35
        gdb.GDB_RUN_BINARY_NAMES_EXPR.append('bar:main.c:5')
36 37 38 39 40
        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'))

41 42 43
    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 ~!@#$%^*()-=[]{}|_+":;'`,>?. """
44 45 46
        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 ' "))
47

48
    def test_get_sub_process_klass(self):
49
        gdb.GDB_RUN_BINARY_NAMES_EXPR = []
50
        self.assertIs(process.get_sub_process_klass(TRUE_CMD),
51 52
                      process.SubProcess)

53
        gdb.GDB_RUN_BINARY_NAMES_EXPR.append('/bin/false')
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        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)

74 75 76 77 78 79 80 81 82

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):

83 84 85 86
    @mock.patch.object(path, 'find_command',
                       mock.MagicMock(return_value=TRUE_CMD))
    @mock.patch.object(os, 'getuid',
                       mock.MagicMock(return_value=1000))
87 88 89 90 91
    def test_subprocess_nosudo(self):
        expected_command = 'ls -l'
        p = process.SubProcess(cmd='ls -l')
        self.assertEqual(p.cmd, expected_command)

92 93 94
    @mock.patch.object(path, 'find_command',
                       mock.MagicMock(return_value=TRUE_CMD))
    @mock.patch.object(os, 'getuid', mock.MagicMock(return_value=0))
95 96 97 98 99
    def test_subprocess_nosudo_uid_0(self):
        expected_command = 'ls -l'
        p = process.SubProcess(cmd='ls -l')
        self.assertEqual(p.cmd, expected_command)

100 101 102 103
    @mock.patch.object(path, 'find_command',
                       mock.MagicMock(return_value=TRUE_CMD))
    @mock.patch.object(os, 'getuid',
                       mock.MagicMock(return_value=1000))
104
    def test_subprocess_sudo(self):
105
        expected_command = '%s -n ls -l' % TRUE_CMD
106 107 108
        p = process.SubProcess(cmd='ls -l', sudo=True)
        self.assertEqual(p.cmd, expected_command)

109 110
    @mock.patch.object(path, 'find_command', mock_fail_find_cmd)
    @mock.patch.object(os, 'getuid', mock.MagicMock(return_value=1000))
111 112 113 114 115
    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)

116 117 118
    @mock.patch.object(path, 'find_command',
                       mock.MagicMock(return_value=TRUE_CMD))
    @mock.patch.object(os, 'getuid', mock.MagicMock(return_value=0))
119 120 121 122 123
    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)

124 125 126
    @mock.patch.object(path, 'find_command',
                       mock.MagicMock(return_value=TRUE_CMD))
    @mock.patch.object(os, 'getuid', mock.MagicMock(return_value=1000))
127
    def test_subprocess_sudo_shell(self):
128
        expected_command = '%s -n -s ls -l' % TRUE_CMD
129 130 131
        p = process.SubProcess(cmd='ls -l', sudo=True, shell=True)
        self.assertEqual(p.cmd, expected_command)

132 133
    @mock.patch.object(path, 'find_command', mock_fail_find_cmd)
    @mock.patch.object(os, 'getuid', mock.MagicMock(return_value=1000))
134 135 136 137 138
    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)

139 140 141
    @mock.patch.object(path, 'find_command',
                       mock.MagicMock(return_value=TRUE_CMD))
    @mock.patch.object(os, 'getuid', mock.MagicMock(return_value=0))
142 143 144 145 146
    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)

147 148 149
    @mock.patch.object(path, 'find_command',
                       mock.MagicMock(return_value=TRUE_CMD))
    @mock.patch.object(os, 'getuid', mock.MagicMock(return_value=1000))
150 151 152 153 154
    def test_run_nosudo(self):
        expected_command = 'ls -l'
        p = process.run(cmd='ls -l', ignore_status=True)
        self.assertEqual(p.command, expected_command)

155 156 157
    @mock.patch.object(path, 'find_command',
                       mock.MagicMock(return_value=TRUE_CMD))
    @mock.patch.object(os, 'getuid', mock.MagicMock(return_value=0))
158 159 160 161 162
    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)

163 164 165
    @mock.patch.object(path, 'find_command',
                       mock.MagicMock(return_value=TRUE_CMD))
    @mock.patch.object(os, 'getuid', mock.MagicMock(return_value=1000))
166
    def test_run_sudo(self):
167
        expected_command = '%s -n ls -l' % TRUE_CMD
168 169 170
        p = process.run(cmd='ls -l', sudo=True, ignore_status=True)
        self.assertEqual(p.command, expected_command)

171 172
    @mock.patch.object(path, 'find_command', mock_fail_find_cmd)
    @mock.patch.object(os, 'getuid', mock.MagicMock(return_value=1000))
173 174 175 176 177
    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)

178 179 180
    @mock.patch.object(path, 'find_command',
                       mock.MagicMock(return_value=TRUE_CMD))
    @mock.patch.object(os, 'getuid', mock.MagicMock(return_value=0))
181 182 183 184 185
    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)

186 187 188
    @mock.patch.object(path, 'find_command',
                       mock.MagicMock(return_value=TRUE_CMD))
    @mock.patch.object(os, 'getuid', mock.MagicMock(return_value=1000))
189
    def test_run_sudo_shell(self):
190
        expected_command = '%s -n -s ls -l' % TRUE_CMD
191 192 193
        p = process.run(cmd='ls -l', sudo=True, shell=True, ignore_status=True)
        self.assertEqual(p.command, expected_command)

194 195
    @mock.patch.object(path, 'find_command', mock_fail_find_cmd)
    @mock.patch.object(os, 'getuid', mock.MagicMock(return_value=1000))
196 197 198 199 200
    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)

201 202 203
    @mock.patch.object(path, 'find_command',
                       mock.MagicMock(return_value=TRUE_CMD))
    @mock.patch.object(os, 'getuid', mock.MagicMock(return_value=0))
204 205 206 207 208
    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)

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

class MiscProcessTests(unittest.TestCase):

    def test_binary_from_shell(self):
        self.assertEqual("binary", process.binary_from_shell_cmd("binary"))
        res = process.binary_from_shell_cmd("MY_VAR=foo myV4r=123 "
                                            "quote='a b c' QUOTE=\"1 2 && b\" "
                                            "QuOtE=\"1 2\"foo' 3 4' first_bin "
                                            "second_bin VAR=xyz")
        self.assertEqual("first_bin", res)
        res = process.binary_from_shell_cmd("VAR=VALUE 1st_binary var=value "
                                            "second_binary")
        self.assertEqual("1st_binary", res)
        res = process.binary_from_shell_cmd("FOO=bar ./bin var=value")
        self.assertEqual("./bin", res)

L
Lukáš Doktor 已提交
225

226 227
if __name__ == "__main__":
    unittest.main()