Exemple #1
0
 def setUp(self):
     super(TestProcessListUtility, self).setUp()
     self.ps = ProcessList()
Exemple #2
0
class TestProcessListUtility(ScalyrTestCase):
    @skipIf(
        not psutil,
        "Skipping tests because psutil is not available (likely running under Python 2.6 on Circle CI)",
    )
    def setUp(self):
        super(TestProcessListUtility, self).setUp()
        self.ps = ProcessList()

    @skipIf(
        not psutil,
        "Skipping tests because psutil is not available (likely running under Python 2.6 on Circle CI)",
    )
    def test_no_process(self):
        # override
        self.ps.parent_to_children_map = defaultdict(list)
        self.ps.processes = []

        self.assertEqual(self.ps.get_child_processes("bad pid"), [])
        self.assertEqual(self.ps.get_matches_commandline(".*"), [])
        self.assertEqual(self.ps.get_matches_commandline_with_children(".*"),
                         [])

    @skipIf(
        not psutil,
        "Skipping tests because psutil is not available (likely running under Python 2.6 on Circle CI)",
    )
    def test_single_process_no_children(self):
        # override
        # process id 0 is basically no process. PID 1 is the main process of a terminal
        self.ps.processes = [
            {
                "pid": 2,
                "ppid": 1,
                "cmd": "python hello.py"
            },
            {
                "pid": 1,
                "ppid": 0,
                "cmd": "/bin/bash"
            },
        ]
        self.ps.parent_to_children_map = defaultdict(list)
        self.ps.parent_to_children_map[1] = [2]
        self.ps.parent_to_children_map[0] = [1]

        self.assertEqual(self.ps.get_child_processes("bad pid"), [])
        self.assertEqual(self.ps.get_child_processes(1), [2])
        # positive match
        self.assertEqual(set(self.ps.get_matches_commandline(".*")),
                         set([1, 2]))
        self.assertEqual(self.ps.get_matches_commandline(".*bash.*"), [1])
        self.assertEqual(self.ps.get_matches_commandline(".*py.*"), [2])
        self.assertEqual(
            set(self.ps.get_matches_commandline_with_children(".*")),
            set([1, 2]))

    @skipIf(
        not psutil,
        "Skipping tests because psutil is not available (likely running under Python 2.6 on Circle CI)",
    )
    def test_single_process_with_children(self):
        # override
        # process id 0 is basically no process. PID 1 is the main process of a terminal
        self.ps.processes = [
            {
                "pid": 2,
                "ppid": 1,
                "cmd": "python hello.py"
            },
            {
                "pid": 3,
                "ppid": 2,
                "cmd": "sleep 2"
            },
            {
                "pid": 1,
                "ppid": 0,
                "cmd": "/bin/bash"
            },
        ]
        self.ps.parent_to_children_map = defaultdict(list)
        self.ps.parent_to_children_map[1] = [2]
        self.ps.parent_to_children_map[2] = [3]
        self.ps.parent_to_children_map[0] = [1]

        self.assertEqual(self.ps.get_child_processes("bad pid"), [])
        self.assertEqual(set(self.ps.get_child_processes(1)), set([2, 3]))
        self.assertEqual(self.ps.get_child_processes(2), [3])
        # positive match
        self.assertEqual(set(self.ps.get_matches_commandline(".*")),
                         set([1, 2, 3]))
        self.assertEqual(self.ps.get_matches_commandline(".*bash.*"), [1])
        self.assertEqual(self.ps.get_matches_commandline(".*py.*"), [2])
        self.assertEqual(
            set(self.ps.get_matches_commandline_with_children(".*")),
            set([1, 2, 3]))

    @skipIf(
        not psutil,
        "Skipping tests because psutil is not available (likely running under Python 2.6 on Circle CI)",
    )
    def test_multiple_processes_with_children(self):
        # override
        # process id 0 is basically no process. PID 1 is the main process of a terminal
        self.ps.processes = [
            {
                "pid": 2,
                "ppid": 1,
                "cmd": "python hello.py"
            },
            {
                "pid": 3,
                "ppid": 2,
                "cmd": "sleep 2"
            },
            {
                "pid": 1,
                "ppid": 0,
                "cmd": "/bin/bash"
            },
            {
                "pid": 4,
                "ppid": 0,
                "cmd": "sleep 10000"
            },
        ]
        self.ps.parent_to_children_map = defaultdict(list)
        self.ps.parent_to_children_map[1] = [2]
        self.ps.parent_to_children_map[2] = [3]
        self.ps.parent_to_children_map[0] = [1, 4]

        self.assertEqual(self.ps.get_child_processes("bad pid"), [])
        self.assertEqual(set(self.ps.get_child_processes(1)), set([2, 3]))
        self.assertEqual(self.ps.get_child_processes(2), [3])
        # positive match
        self.assertEqual(set(self.ps.get_matches_commandline(".*")),
                         set([1, 2, 3, 4]))
        self.assertEqual(self.ps.get_matches_commandline(".*bash.*"), [1])
        self.assertEqual(self.ps.get_matches_commandline(".*py.*"), [2])
        self.assertEqual(
            set(self.ps.get_matches_commandline_with_children(".*")),
            set([1, 2, 3, 4]))
Exemple #3
0
 def setUp(self):
     self.ps = ProcessList()
Exemple #4
0
class TestProcessListUtility(ScalyrTestCase):
    def setUp(self):
        self.ps = ProcessList()

    def test_no_process(self):
        # override
        self.ps.parent_to_children_map = defaultdict(list)
        self.ps.processes = []

        self.assertEqual(self.ps.get_child_processes('bad pid'), [])
        self.assertEqual(self.ps.get_matches_commandline('.*'), [])
        self.assertEqual(self.ps.get_matches_commandline_with_children('.*'),
                         [])

    def test_single_process_no_children(self):
        # override
        # process id 0 is basically no process. PID 1 is the main process of a terminal
        self.ps.processes = [{
            'pid': 2,
            'ppid': 1,
            'cmd': 'python hello.py'
        }, {
            'pid': 1,
            'ppid': 0,
            'cmd': '/bin/bash'
        }]
        self.ps.parent_to_children_map = defaultdict(list)
        self.ps.parent_to_children_map[1] = [2]
        self.ps.parent_to_children_map[0] = [1]

        self.assertEqual(self.ps.get_child_processes('bad pid'), [])
        self.assertEqual(self.ps.get_child_processes(1), [2])
        # positive match
        self.assertEqual(set(self.ps.get_matches_commandline('.*')),
                         set([1, 2]))
        self.assertEqual(self.ps.get_matches_commandline('.*bash.*'), [1])
        self.assertEqual(self.ps.get_matches_commandline('.*py.*'), [2])
        self.assertEqual(
            set(self.ps.get_matches_commandline_with_children('.*')),
            set([1, 2]))

    def test_single_process_with_children(self):
        # override
        # process id 0 is basically no process. PID 1 is the main process of a terminal
        self.ps.processes = [{
            'pid': 2,
            'ppid': 1,
            'cmd': 'python hello.py'
        }, {
            'pid': 3,
            'ppid': 2,
            'cmd': 'sleep 2'
        }, {
            'pid': 1,
            'ppid': 0,
            'cmd': '/bin/bash'
        }]
        self.ps.parent_to_children_map = defaultdict(list)
        self.ps.parent_to_children_map[1] = [2]
        self.ps.parent_to_children_map[2] = [3]
        self.ps.parent_to_children_map[0] = [1]

        self.assertEqual(self.ps.get_child_processes('bad pid'), [])
        self.assertEqual(set(self.ps.get_child_processes(1)), set([2, 3]))
        self.assertEqual(self.ps.get_child_processes(2), [3])
        # positive match
        self.assertEqual(set(self.ps.get_matches_commandline('.*')),
                         set([1, 2, 3]))
        self.assertEqual(self.ps.get_matches_commandline('.*bash.*'), [1])
        self.assertEqual(self.ps.get_matches_commandline('.*py.*'), [2])
        self.assertEqual(
            set(self.ps.get_matches_commandline_with_children('.*')),
            set([1, 2, 3]))

    def test_multiple_processes_with_children(self):
        # override
        # process id 0 is basically no process. PID 1 is the main process of a terminal
        self.ps.processes = [{
            'pid': 2,
            'ppid': 1,
            'cmd': 'python hello.py'
        }, {
            'pid': 3,
            'ppid': 2,
            'cmd': 'sleep 2'
        }, {
            'pid': 1,
            'ppid': 0,
            'cmd': '/bin/bash'
        }, {
            'pid': 4,
            'ppid': 0,
            'cmd': 'sleep 10000'
        }]
        self.ps.parent_to_children_map = defaultdict(list)
        self.ps.parent_to_children_map[1] = [2]
        self.ps.parent_to_children_map[2] = [3]
        self.ps.parent_to_children_map[0] = [1, 4]

        self.assertEqual(self.ps.get_child_processes('bad pid'), [])
        self.assertEqual(set(self.ps.get_child_processes(1)), set([2, 3]))
        self.assertEqual(self.ps.get_child_processes(2), [3])
        # positive match
        self.assertEqual(set(self.ps.get_matches_commandline('.*')),
                         set([1, 2, 3, 4]))
        self.assertEqual(self.ps.get_matches_commandline('.*bash.*'), [1])
        self.assertEqual(self.ps.get_matches_commandline('.*py.*'), [2])
        self.assertEqual(
            set(self.ps.get_matches_commandline_with_children('.*')),
            set([1, 2, 3, 4]))
 def setUp(self):
     self.ps = ProcessList()
class TestProcessListUtility(ScalyrTestCase):
    def setUp(self):
        self.ps = ProcessList()

    def test_no_process(self):
        # override
        self.ps.parent_to_children_map = defaultdict(list)
        self.ps.processes = []
        
        self.assertEqual(self.ps.get_child_processes('bad pid'), [])
        self.assertEqual(self.ps.get_matches_commandline('.*'), [])
        self.assertEqual(self.ps.get_matches_commandline_with_children('.*'), [])

    def test_single_process_no_children(self):
        # override
        # process id 0 is basically no process. PID 1 is the main process of a terminal
        self.ps.processes = [
            {'pid': 2, 'ppid': 1, 'cmd': 'python hello.py'},
            {'pid': 1, 'ppid': 0, 'cmd': '/bin/bash'}
        ]
        self.ps.parent_to_children_map = defaultdict(list)
        self.ps.parent_to_children_map[1] = [2]
        self.ps.parent_to_children_map[0] = [1]

        self.assertEqual(self.ps.get_child_processes('bad pid'), [])
        self.assertEqual(self.ps.get_child_processes(1), [2])
        # positive match
        self.assertEqual(set(self.ps.get_matches_commandline('.*')), set([1, 2]))
        self.assertEqual(self.ps.get_matches_commandline('.*bash.*'), [1])
        self.assertEqual(self.ps.get_matches_commandline('.*py.*'), [2])
        self.assertEqual(set(self.ps.get_matches_commandline_with_children('.*')), set([1, 2]))

    def test_single_process_with_children(self):
        # override
        # process id 0 is basically no process. PID 1 is the main process of a terminal
        self.ps.processes = [
            {'pid': 2, 'ppid': 1, 'cmd': 'python hello.py'},
            {'pid': 3, 'ppid': 2, 'cmd': 'sleep 2'},
            {'pid': 1, 'ppid': 0, 'cmd': '/bin/bash'}
        ]
        self.ps.parent_to_children_map = defaultdict(list)
        self.ps.parent_to_children_map[1] = [2]
        self.ps.parent_to_children_map[2] = [3]
        self.ps.parent_to_children_map[0] = [1]

        self.assertEqual(self.ps.get_child_processes('bad pid'), [])
        self.assertEqual(set(self.ps.get_child_processes(1)), set([2, 3]))
        self.assertEqual(self.ps.get_child_processes(2), [3])
        # positive match
        self.assertEqual(set(self.ps.get_matches_commandline('.*')), set([1, 2, 3]))
        self.assertEqual(self.ps.get_matches_commandline('.*bash.*'), [1])
        self.assertEqual(self.ps.get_matches_commandline('.*py.*'), [2])
        self.assertEqual(set(self.ps.get_matches_commandline_with_children('.*')), set([1, 2, 3]))

    def test_multiple_processes_with_children(self):
        # override
        # process id 0 is basically no process. PID 1 is the main process of a terminal
        self.ps.processes = [
            {'pid': 2, 'ppid': 1, 'cmd': 'python hello.py'},
            {'pid': 3, 'ppid': 2, 'cmd': 'sleep 2'},
            {'pid': 1, 'ppid': 0, 'cmd': '/bin/bash'},
            {'pid': 4, 'ppid': 0, 'cmd': 'sleep 10000'}
        ]
        self.ps.parent_to_children_map = defaultdict(list)
        self.ps.parent_to_children_map[1] = [2]
        self.ps.parent_to_children_map[2] = [3]
        self.ps.parent_to_children_map[0] = [1, 4]

        self.assertEqual(self.ps.get_child_processes('bad pid'), [])
        self.assertEqual(set(self.ps.get_child_processes(1)), set([2, 3]))
        self.assertEqual(self.ps.get_child_processes(2), [3])
        # positive match
        self.assertEqual(set(self.ps.get_matches_commandline('.*')), set([1, 2, 3, 4]))
        self.assertEqual(self.ps.get_matches_commandline('.*bash.*'), [1])
        self.assertEqual(self.ps.get_matches_commandline('.*py.*'), [2])
        self.assertEqual(set(self.ps.get_matches_commandline_with_children('.*')), set([1, 2, 3, 4]))