示例#1
0
    def test_signal_kill(self, mock_kill, mock_children, mock_suspend):
        proc = PostmasterProcess(123)

        # all processes successfully stopped
        mock_children.return_value = [Mock()]
        mock_children.return_value[0].kill.side_effect = psutil.Error
        self.assertTrue(proc.signal_kill())

        # postmaster has gone before suspend
        mock_suspend.side_effect = psutil.NoSuchProcess(123)
        self.assertTrue(proc.signal_kill())

        # postmaster has gone before we got a list of children
        mock_suspend.side_effect = psutil.Error()
        mock_children.side_effect = psutil.NoSuchProcess(123)
        self.assertTrue(proc.signal_kill())

        # postmaster has gone after we got a list of children
        mock_children.side_effect = psutil.Error()
        mock_kill.side_effect = psutil.NoSuchProcess(123)
        self.assertTrue(proc.signal_kill())

        # failed to kill postmaster
        mock_kill.side_effect = psutil.AccessDenied(123)
        self.assertFalse(proc.signal_kill())
 def test_cancel(self):
     self.c._process = Mock()
     self.c._process.is_running.return_value = True
     self.c._process.children.side_effect = psutil.Error()
     self.c._process.suspend.side_effect = psutil.Error()
     self.c.cancel()
     self.c._process.is_running.side_effect = [True, False]
     self.c.cancel()
示例#3
0
    def test_start_scan_error(
        self, mock_popen: MagicMock, mock_logger: MagicMock
    ):
        mock_popen.side_effect = psutil.Error('foo')

        proc = Openvas.start_scan('scan_1')

        mock_popen.assert_called_with(
            ['openvas', '--scan-start', 'scan_1'], shell=False
        )

        self.assertIsNone(proc)

        self.assertEqual(mock_logger.warning.call_count, 1)

        mock_popen.reset_mock()
        mock_logger.reset_mock()

        mock_popen.side_effect = OSError('foo')

        proc = Openvas.start_scan('scan_1')

        mock_popen.assert_called_with(
            ['openvas', '--scan-start', 'scan_1'], shell=False
        )

        self.assertIsNone(proc)

        self.assertEqual(mock_logger.warning.call_count, 1)
    def test_callback_executor(self, mock_popen):
        mock_popen.return_value.children.return_value = []
        mock_popen.return_value.is_running.return_value = True

        ce = CallbackExecutor()
        ce._kill_children = Mock(side_effect=Exception)
        ce._invoke_excepthook = Mock()
        self.assertIsNone(ce.call([]))
        ce.join()

        self.assertIsNone(ce.call([]))

        mock_popen.return_value.kill.side_effect = psutil.AccessDenied()
        self.assertIsNone(ce.call([]))

        ce._process_children = []
        mock_popen.return_value.children.side_effect = psutil.Error()
        mock_popen.return_value.kill.side_effect = psutil.NoSuchProcess(123)
        self.assertIsNone(ce.call([]))

        mock_popen.side_effect = Exception
        ce = CallbackExecutor()
        ce._condition.wait = Mock(side_effect=[None, Exception])
        ce._invoke_excepthook = Mock()
        self.assertIsNone(ce.call([]))
        ce.join()
示例#5
0
 def test_check_space_bad_path(self, mock_disk_usage):
     """Test that the error when a bad path."""
     test_cart = self.create_sample_cart()
     test_file = self.create_sample_file(test_cart)
     cart_utils = Cartutils()
     mock_disk_usage.side_effect = psutil.Error(mock.Mock())
     rtn = cart_utils.check_space_requirements(test_file, test_cart, 10,
                                               False)
     self.assertEqual(rtn, False)
     self.assertEqual(test_file.status, 'error')
    def test_check_space_bad_path(self, mock_disk_usage):
        """test that the error when a bad path"""
        with test_database(SqliteDatabase(':memory:'), (Cart, File)):

            test_cart = Cart.create(cart_uid='1', status='staging')
            test_file = File.create(cart=test_cart,
                                    file_name='1.txt',
                                    bundle_path='/tmp/1/1.txt')
            cart_utils = Cartutils()
            mock_disk_usage.side_effect = psutil.Error(mock.Mock())
            rtn = cart_utils.check_space_requirements(test_file, test_cart, 10,
                                                      False)
            self.assertEqual(rtn, False)
            self.assertEqual(test_file.status, 'error')
    create_time=1,
    memory_percent=1.1,
)
access_denied_process2 = PsutilProcessMock(
    pid=10,
    cmdline=["/usr/bin/python3.9", "test.py"],
    cpu_percent=psutil.AccessDenied(),
    username="******",
    threads=[],
    create_time=1,
    memory_percent=1.1,
)
unknown_error_process = PsutilProcessMock(
    pid=10,
    cmdline=["/usr/bin/python3.9", "test.py"],
    cpu_percent=psutil.Error(),
    username="******",
    threads=[],
    create_time=1,
    memory_percent=1.1,
)

expected_calls = [
    (
        "AddProcess",
        {
            "pid": python_process_without_threads.pid,
            "user": python_process_without_threads._username,
            "cmd": " ".join(python_process_without_threads._cmdline),
            "threads": len(python_process_without_threads._threads),
            "time": python_process_without_threads._create_time,
示例#8
0
 def test_error__str__(self):
     self.assertEqual(str(psutil.Error()), "")
示例#9
0
 def test_error__repr__(self):
     self.assertEqual(repr(psutil.Error()), "psutil.Error()")