test_utils_process.py 2.7 KB
Newer Older
1 2 3 4 5 6
import sys

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

8
from avocado.utils import gdb
9 10 11 12 13 14
from avocado.utils import process


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 39 40
    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 ~!@#$%^*()-=[]{}|_+":;'`,>?. """
        self.assertFalse(process.should_run_inside_gdb(cmd))

41
    def test_get_sub_process_klass(self):
42
        gdb.GDB_RUN_BINARY_NAMES_EXPR = []
43 44 45
        self.assertIs(process.get_sub_process_klass('/bin/true'),
                      process.SubProcess)

46
        gdb.GDB_RUN_BINARY_NAMES_EXPR.append('/bin/false')
47 48 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)

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