def test_interface__compat(self): warnings.resetwarnings() with catch_warnings(record=True) as log: ts = TaskSet(MockTask, [[(2, 2)], [(4, 4)], [(8, 8)]]) self.assertTrue(log) self.assertIn("Using this invocation of TaskSet is deprecated", log[0].message.args[0]) self.assertListEqual(ts.tasks, [MockTask.subtask((i, i)) for i in (2, 4, 8)]) return ts # TaskSet.task (deprecated) with catch_warnings(record=True) as log: ts = TaskSet(MockTask, [[(2, 2)], [(4, 4)], [(8, 8)]]) self.assertEqual(ts.task.name, MockTask.name) self.assertTrue(log) self.assertIn("TaskSet.task is deprecated", log[0].message.args[0]) # TaskSet.task_name (deprecated) with catch_warnings(record=True) as log: ts = TaskSet(MockTask, [[(2, 2)], [(4, 4)], [(8, 8)]]) self.assertEqual(ts.task_name, MockTask.name) self.assertTrue(log) self.assertIn("TaskSet.task_name is deprecated", log[0].message.args[0])
def test_interface__compat(self): def with_catch_warnings(log): ts = TaskSet(MockTask, [[(2, 2)], [(4, 4)], [(8, 8)]]) self.assertTrue(log) self.assertIn("Using this invocation of TaskSet is deprecated", log[0].message.args[0]) return ts context = catch_warnings(record=True) ts = execute_context(context, with_catch_warnings) self.assertListEqual(ts.tasks, [MockTask.subtask((i, i)) for i in (2, 4, 8)]) # TaskSet.task (deprecated) def with_catch_warnings2(log): self.assertEqual(ts.task, MockTask) self.assertTrue(log) self.assertIn("TaskSet.task is deprecated", log[0].message.args[0]) execute_context(catch_warnings(record=True), with_catch_warnings2) # TaskSet.task_name (deprecated) def with_catch_warnings3(log): self.assertEqual(ts.task_name, MockTask.name) self.assertTrue(log) self.assertIn("TaskSet.task_name is deprecated", log[0].message.args[0]) execute_context(catch_warnings(record=True), with_catch_warnings3)
def test_interface__compat(self): def with_catch_warnings(log): ts = TaskSet(MockTask, [[(2, 2)], [(4, 4)], [(8, 8)]]) self.assertTrue(log) self.assertIn("Using this invocation of TaskSet is deprecated", log[0].message.args[0]) self.assertListEqual(ts.tasks, [MockTask.subtask((i, i)) for i in (2, 4, 8)]) return ts context = catch_warnings(record=True) execute_context(context, with_catch_warnings) # TaskSet.task (deprecated) def with_catch_warnings2(log): ts = TaskSet(MockTask, [[(2, 2)], [(4, 4)], [(8, 8)]]) self.assertEqual(ts.task, MockTask) self.assertTrue(log) self.assertIn("TaskSet.task is deprecated", log[0].message.args[0]) execute_context(catch_warnings(record=True), with_catch_warnings2) # TaskSet.task_name (deprecated) def with_catch_warnings3(log): ts = TaskSet(MockTask, [[(2, 2)], [(4, 4)], [(8, 8)]]) self.assertEqual(ts.task_name, MockTask.name) self.assertTrue(log) self.assertIn("TaskSet.task_name is deprecated", log[0].message.args[0]) execute_context(catch_warnings(record=True), with_catch_warnings3)
def test_load_settings(self): warnings.resetwarnings() with catch_warnings(record=True) as log: self.assertIs(loaders.load_settings(), self.app.conf) warning = log[0].message self.assertIsInstance(warning, CPendingDeprecationWarning) self.assertIn("deprecation", warning.args[0])
def setUp(self): warnings.resetwarnings() def with_catch_warnings(log): from celery import decorators return decorators context = catch_warnings(record=True) self.decorators = execute_context(context, with_catch_warnings)
def test_TaskSet_import_from_task_base(self): warnings.resetwarnings() with catch_warnings(record=True) as log: from celery.task.base import TaskSet, subtask TaskSet() subtask(PingTask) for w in (log[0].message, log[1].message): self.assertIsInstance(w, CDeprecationWarning) self.assertIn("is deprecated", w.args[0])
def test_receive_message_unknown(self): l = MyKombuConsumer(self.ready_queue, self.eta_schedule, self.logger, send_events=False) backend = Mock() m = create_message(backend, unknown={"baz": "!!!"}) l.event_dispatcher = Mock() l.pidbox_node = MockNode() with catch_warnings(record=True) as log: l.receive_message(m.decode(), m) self.assertTrue(log) self.assertIn("unknown message", log[0].message.args[0])
def test_mail_admins_errors(self): MockMail.Mailer.raise_on_send = True opts = dict(self.message_options, **self.server_options) with catch_warnings(record=True) as log: self.loader.mail_admins(fail_silently=True, **opts) warning = log[0].message self.assertIsInstance(warning, MockMail.SendmailWarning) self.assertIn("KeyError", warning.args[0]) self.assertRaises(KeyError, self.loader.mail_admins, fail_silently=False, **opts)
def test_TaskSet_import_from_task_base(self): warnings.resetwarnings() def block(log): from celery.task.base import TaskSet, subtask TaskSet() subtask(PingTask) return log[0].message, log[1].message for w in execute_context(catch_warnings(record=True), block): self.assertIsInstance(w, DeprecationWarning) self.assertIn("is deprecated", w.args[0])
def test_unconfigured_settings(self): context_executed = [False] class _Loader(default.Loader): def find_module(self, name): raise ImportError(name) with catch_warnings(record=True): l = _Loader() self.assertDictEqual(l.conf, {}) context_executed[0] = True self.assertTrue(context_executed[0])
def test_mail_admins_errors(self): MockMail.Mailer.raise_on_send = True opts = dict(self.message_options, **self.server_options) with catch_warnings(record=True) as log: self.loader.mail_admins(fail_silently=True, **opts) warning = log[0].message self.assertIsInstance(warning, MockMail.SendmailWarning) self.assertIn("KeyError", warning.args[0]) with self.assertRaises(KeyError): self.loader.mail_admins(fail_silently=False, **opts)
def test_apply_entry_error_not_handled(self, stdout, stderr): t = timer2.Timer() t.schedule.on_error = Mock() fun = Mock() fun.side_effect = ValueError() warnings.resetwarnings() with catch_warnings(record=True) as log: t.apply_entry(fun) fun.assert_called_with() self.assertFalse(log) self.assertFalse(stderr.getvalue())
def test_ping(self): warnings.resetwarnings() def block(log): prev = PingTask.app.conf.CELERY_ALWAYS_EAGER PingTask.app.conf.CELERY_ALWAYS_EAGER = True try: return ping(), log[0].message finally: PingTask.app.conf.CELERY_ALWAYS_EAGER = prev pong, warning = execute_context(catch_warnings(record=True), block) self.assertEqual(pong, "pong") self.assertIsInstance(warning, DeprecationWarning) self.assertIn("ping task has been deprecated", warning.args[0])
def test_ping(self): warnings.resetwarnings() with catch_warnings(record=True) as log: prev = PingTask.app.conf.CELERY_ALWAYS_EAGER PingTask.app.conf.CELERY_ALWAYS_EAGER = True try: pong = ping() warning = log[0].message self.assertEqual(pong, "pong") self.assertIsInstance(warning, CDeprecationWarning) self.assertIn("ping task has been deprecated", warning.args[0]) finally: PingTask.app.conf.CELERY_ALWAYS_EAGER = prev
def test_unconfigured_settings(self): context_executed = [False] class _Loader(default.Loader): def import_from_cwd(self, name): raise ImportError(name) def with_catch_warnings(log): l = _Loader() self.assertEqual(l.conf.CELERY_RESULT_BACKEND, "amqp") context_executed[0] = True context = catch_warnings(record=True) execute_context(context, with_catch_warnings) self.assertTrue(context_executed[0])
def test_receive_message_unknown(self): l = CarrotListener(self.ready_queue, self.eta_schedule, self.logger, send_events=False) backend = MockBackend() m = create_message(backend, unknown={"baz": "!!!"}) l.event_dispatcher = MockEventDispatcher() l.control_dispatch = MockControlDispatch() def with_catch_warnings(log): l.receive_message(m.decode(), m) self.assertTrue(log) self.assertIn("unknown message", log[0].message.args[0]) context = catch_warnings(record=True) execute_context(context, with_catch_warnings)
def test_mail_admins_errors(self): MockMail.Mailer.raise_on_send = True opts = dict(self.message_options, **self.server_options) def with_catch_warnings(log): self.loader.mail_admins(fail_silently=True, **opts) return log[0].message warning = execute_context(catch_warnings(record=True), with_catch_warnings) self.assertIsInstance(warning, MockMail.SendmailWarning) self.assertIn("KeyError", warning.args[0]) self.assertRaises(KeyError, self.loader.mail_admins, fail_silently=False, **opts)
def test_run(self): self.Worker().run() self.Worker(discard=True).run() worker = self.Worker() worker.init_loader() worker.settings.DEBUG = True def with_catch_warnings(log): worker.run() self.assertIn("memory leak", log[0].message.args[0]) context = catch_warnings(record=True) execute_context(context, with_catch_warnings) worker.settings.DEBUG = False
def test_execute_safe_catches_exception(self): old_exec = WorkerTaskTrace.execute def _error_exec(self, *args, **kwargs): raise KeyError("baz") WorkerTaskTrace.execute = _error_exec try: with catch_warnings(record=True) as log: res = execute_and_trace(mytask.name, uuid(), [4], {}) self.assertIsInstance(res, ExceptionInfo) self.assertTrue(log) self.assertIn("Exception outside", log[0].message.args[0]) self.assertIn("KeyError", log[0].message.args[0]) finally: WorkerTaskTrace.execute = old_exec
def test_warns_if_running_as_privileged_user(self): app = current_app if app.IS_WINDOWS: raise SkipTest("Not applicable on Windows") warnings.resetwarnings() def geteuid(): return 0 prev, os.geteuid = os.geteuid, geteuid try: with catch_warnings(record=True) as log: worker = self.Worker() worker.run() self.assertTrue(log) self.assertIn("superuser privileges is not encouraged", log[0].message.args[0]) finally: os.geteuid = prev
def test_execute_safe_catches_exception(self): warnings.resetwarnings() def _error_exec(self, *args, **kwargs): raise KeyError("baz") @task_dec def raising(): raise KeyError("baz") raising.request = None with catch_warnings(record=True) as log: res = execute_and_trace(raising.name, uuid(), [], {}) self.assertIsInstance(res, ExceptionInfo) self.assertTrue(log) self.assertIn("Exception outside", log[0].message.args[0]) self.assertIn("AttributeError", log[0].message.args[0])
def test_receieve_message_ack_raises(self): l = MyKombuConsumer(self.ready_queue, self.eta_schedule, self.logger, send_events=False) backend = Mock() m = create_message(backend, args=[2, 4, 8], kwargs={}) l.event_dispatcher = Mock() l.connection_errors = (socket.error, ) l.logger = Mock() m.ack = Mock() m.ack.side_effect = socket.error("foo") with catch_warnings(record=True) as log: self.assertFalse(l.receive_message(m.decode(), m)) self.assertTrue(log) self.assertIn("unknown message", log[0].message.args[0]) self.assertRaises(Empty, self.ready_queue.get_nowait) self.assertTrue(self.eta_schedule.empty()) m.ack.assert_called_with() self.assertTrue(l.logger.critical.call_count)
def test_warns_if_running_as_privileged_user(self): app = app_or_default() if app.IS_WINDOWS: raise SkipTest("Not applicable on Windows") warnings.resetwarnings() def geteuid(): return 0 prev, os.geteuid = os.geteuid, geteuid try: def with_catch_warnings(log): worker = self.Worker() worker.run() self.assertTrue(log) self.assertIn("superuser privileges is not encouraged", log[0].message.args[0]) context = catch_warnings(record=True) execute_context(context, with_catch_warnings) finally: os.geteuid = prev
def test_execute_safe_catches_exception(self): from celery.worker.job import execute_and_trace, WorkerTaskTrace old_exec = WorkerTaskTrace.execute def _error_exec(self, *args, **kwargs): raise KeyError("baz") WorkerTaskTrace.execute = _error_exec try: def with_catch_warnings(log): res = execute_and_trace(mytask.name, gen_unique_id(), [4], {}) self.assertIsInstance(res, ExceptionInfo) self.assertTrue(log) self.assertIn("Exception outside", log[0].message.args[0]) self.assertIn("KeyError", log[0].message.args[0]) context = catch_warnings(record=True) execute_context(context, with_catch_warnings) finally: WorkerTaskTrace.execute = old_exec
def setUp(self): warnings.resetwarnings() with catch_warnings(record=True): from celery import decorators self.decorators = decorators