test_utils_process.py 9.2 KB
Newer Older
1
import os
2
import unittest
3
from mock import MagicMock, patch
4

5
from avocado.utils import gdb
6
from avocado.utils import process
7
from avocado.utils import path
8

9 10
TRUE_CMD = path.find_command('true')

11 12 13 14

class TestGDBProcess(unittest.TestCase):

    def setUp(self):
15
        self.current_runtime_expr = gdb.GDB_RUN_BINARY_NAMES_EXPR[:]
16 17

    def cleanUp(self):
18
        gdb.GDB_RUN_BINARY_NAMES_EXPR = self.current_runtime_expr
19 20

    def test_should_run_inside_gdb(self):
21
        gdb.GDB_RUN_BINARY_NAMES_EXPR = ['foo']
22 23 24 25
        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'))

26
        gdb.GDB_RUN_BINARY_NAMES_EXPR.append('foo:main')
27 28 29
        self.assertTrue(process.should_run_inside_gdb('foo'))
        self.assertFalse(process.should_run_inside_gdb('bar'))

30
        gdb.GDB_RUN_BINARY_NAMES_EXPR.append('bar:main.c:5')
31 32 33 34 35
        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'))

36 37 38
    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 ~!@#$%^*()-=[]{}|_+":;'`,>?. """
39 40 41
        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 ' "))
42

43
    def test_get_sub_process_klass(self):
44
        gdb.GDB_RUN_BINARY_NAMES_EXPR = []
45
        self.assertIs(process.get_sub_process_klass(TRUE_CMD),
46 47
                      process.SubProcess)

48
        gdb.GDB_RUN_BINARY_NAMES_EXPR.append('/bin/false')
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
        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)

69 70 71 72 73 74 75 76 77 78

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',
79
                  MagicMock(return_value=TRUE_CMD))
80 81 82 83 84 85 86
    @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',
87
                  MagicMock(return_value=TRUE_CMD))
88 89 90 91 92 93 94
    @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',
95
                  MagicMock(return_value=TRUE_CMD))
96 97
    @patch.object(os, 'getuid', MagicMock(return_value=1000))
    def test_subprocess_sudo(self):
98
        expected_command = '%s -n ls -l' % TRUE_CMD
99 100 101 102 103 104 105 106 107 108 109
        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',
110
                  MagicMock(return_value=TRUE_CMD))
111 112 113 114 115 116 117
    @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',
118
                  MagicMock(return_value=TRUE_CMD))
119 120
    @patch.object(os, 'getuid', MagicMock(return_value=1000))
    def test_subprocess_sudo_shell(self):
121
        expected_command = '%s -n -s ls -l' % TRUE_CMD
122 123 124 125 126 127 128 129 130 131 132
        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',
133
                  MagicMock(return_value=TRUE_CMD))
134 135 136 137 138 139 140
    @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',
141
                  MagicMock(return_value=TRUE_CMD))
142 143 144 145 146 147 148
    @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',
149
                  MagicMock(return_value=TRUE_CMD))
150 151 152 153 154 155 156
    @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',
157
                  MagicMock(return_value=TRUE_CMD))
158 159
    @patch.object(os, 'getuid', MagicMock(return_value=1000))
    def test_run_sudo(self):
160
        expected_command = '%s -n ls -l' % TRUE_CMD
161 162 163 164 165 166 167 168 169 170 171
        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',
172
                  MagicMock(return_value=TRUE_CMD))
173 174 175 176 177 178 179
    @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',
180
                  MagicMock(return_value=TRUE_CMD))
181 182
    @patch.object(os, 'getuid', MagicMock(return_value=1000))
    def test_run_sudo_shell(self):
183
        expected_command = '%s -n -s ls -l' % TRUE_CMD
184 185 186 187 188 189 190 191 192 193 194
        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',
195
                  MagicMock(return_value=TRUE_CMD))
196 197 198 199 200 201
    @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)

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

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 已提交
218

219 220
if __name__ == "__main__":
    unittest.main()