def test_ProgressReportTask_init(self): task = ProgressReportTask(self.func_mock) fake_func = lambda x: x msg = "^Funcion is not celery task.$" with self.assertRaisesRegexp(ProgressreportException, msg): task = ProgressReportTask(fake_func)
def test_show_task_list(self): task = ProgressReportTask(self.func_mock) with nested(patch("sys.stdout", new_callable=StringIO.StringIO), patch("pgreport.tasks.celery")) as ( std_mock, cel_mock, ): stat_mock = MagicMock() stat_mock.active.return_value = {"queue": None} cel_mock.control.inspect.return_value = stat_mock task.show_task_list() cel_mock.control.inspect.assert_called_once_with() self.assertEquals(std_mock.getvalue(), "*** Active queues ***\n{}: []\n".format("queue")) status = {"args": "argment", "id": "task_id", "name": "task_name", "worker_pid": "worker_pid"} with nested(patch("sys.stdout", new_callable=StringIO.StringIO), patch("pgreport.tasks.celery")) as ( std_mock, cel_mock, ): stat_mock = MagicMock() stat_mock.active.return_value = {"queue": [status]} cel_mock.control.inspect.return_value = stat_mock task.show_task_list() self.assertEquals( std_mock.getvalue(), "*** Active queues ***\nqueue: [\n * Task id: {id}, Task args: {args}, Task name: {name}, Worker pid: {worker_pid}\n]\n".format( **status ), )
def test_send_tasks(self, module_mock, send_mock): task = ProgressReportTask(self.func_mock) module_mock().get_courses.return_value = self.courses task.send_tasks() module_mock.assert_called_with() module_mock().get_courses.assert_called_once_with() send_mock.assert_has_calls([call(self.course1.id), call(self.course2.id)])
def handle(self, *args, **options): """Handle command options.""" course_id = options['course_id'] task_id = options['task_id'] if course_id is not None: try: course_id = CourseLocator.from_string(course_id) except InvalidKeyError: raise CommandError( "'{}' is an invalid course_id".format(course_id)) if not modulestore().get_course(course_id): raise CommandError("The specified course does not exist.") if len(args) != 1: raise CommandError( 'Required subcommand, create, list, status, revoke or clear_cache.' ) command = args[0] task = ProgressReportTask(create_report_task) if command == "status": if task_id is None: raise CommandError('"status" subcommand required task_id.') task.show_task_status(task_id) elif command == "list": task.show_task_list() elif command == "revoke": if task_id is None: raise CommandError('"revoke" subcommand required task_id.') task.revoke_task(task_id) elif command == "create": if course_id is None: raise CommandError('"create" subcommand required course_id.') task.send_task(course_id) state = TaskState("pgreport.tasks.create_report_task", course_id) state.delete_task_state() elif command == "clear_cache": if course_id is None: raise CommandError( '"clear_cache" subcommand required course_id.') state = TaskState("pgreport.tasks.create_report_task", course_id) state.delete_task_state() else: raise CommandError('Invalid subcommand.')
def test_show_task_status(self): task = ProgressReportTask(self.func_mock) with patch("sys.stdout", new_callable=StringIO.StringIO) as std_mock: result_mock = MagicMock() result_mock.state = "PENDING" self.func_mock.AsyncResult.return_value = result_mock task.show_task_status("task_id") self.func_mock.AsyncResult.assert_called_once_with("task_id") self.assertEquals(std_mock.getvalue(), "Task not found or PENDING state\n") with patch("sys.stdout", new_callable=StringIO.StringIO) as std_mock: result_mock = MagicMock() result_mock.state = "SUCCESS" self.func_mock.AsyncResult.return_value = result_mock task.show_task_status("task_id") self.assertEquals(std_mock.getvalue(), "Current State: {}, {}\n".format(result_mock.state, result_mock.info))
def test_send_task(self, ts_mock): task = ProgressReportTask(self.func_mock) with patch("sys.stdout", new_callable=StringIO.StringIO) as std_mock: ts_mock().is_active = False task.send_task(self.course1.id) ts_mock.assert_called_with(task.task_name, self.course1.id) ts_mock().set_task_state.assert_called_once_with() self.func_mock.apply_async.assert_called_with( args=(ANY, unicode(self.course1.id)), task_id=ANY, expires=23.5 * 60 * 60, retry=False ) self.assertEquals(std_mock.getvalue(), "Send task (task_id: %s)\n" % (self.func_mock.apply_async().id)) with patch("sys.stdout", new_callable=StringIO.StringIO) as std_mock: ts_mock().is_active = True task.send_task(self.course1.id) self.assertEquals( std_mock.getvalue(), "Task is already running. (%s, %s)\n" % (task.task_name, self.course1.id) )
def test_show_task_list(self): task = ProgressReportTask(self.func_mock) with nested( patch('sys.stdout', new_callable=StringIO.StringIO), patch('pgreport.tasks.celery') ) as (std_mock, cel_mock): stat_mock = MagicMock() stat_mock.active.return_value = {"queue": None} cel_mock.control.inspect.return_value = stat_mock task.show_task_list() cel_mock.control.inspect.assert_called_once_with() self.assertEquals(std_mock.getvalue(), "*** Active queues ***\n{}: []\n".format("queue") ) status = { "args": "argment", "id": "task_id", "name": "task_name", "worker_pid": "worker_pid", } with nested( patch('sys.stdout', new_callable=StringIO.StringIO), patch('pgreport.tasks.celery') ) as (std_mock, cel_mock): stat_mock = MagicMock() stat_mock.active.return_value = {"queue": [status]} cel_mock.control.inspect.return_value = stat_mock task.show_task_list() self.assertEquals(std_mock.getvalue(), '*** Active queues ***\nqueue: [\n * Task id: {id}, Task args: {args}, Task name: {name}, Worker pid: {worker_pid}\n]\n'.format(**status) )
def handle(self, *args, **options): """Handle command options.""" course_id = options["course_id"] task_id = options["task_id"] if course_id is not None: try: course_id = CourseLocator.from_string(course_id) except InvalidKeyError: raise CommandError("'{}' is an invalid course_id".format(course_id)) if not modulestore().get_course(course_id): raise CommandError("The specified course does not exist.") if len(args) != 1: raise CommandError("Required subcommand, create, list, status, revoke or clear_cache.") command = args[0] task = ProgressReportTask(create_report_task) if command == "status": if task_id is None: raise CommandError('"status" subcommand required task_id.') task.show_task_status(task_id) elif command == "list": task.show_task_list() elif command == "revoke": if task_id is None: raise CommandError('"revoke" subcommand required task_id.') task.revoke_task(task_id) elif command == "create": if course_id is None: raise CommandError('"create" subcommand required course_id.') task.send_task(course_id) state = TaskState("pgreport.tasks.create_report_task", course_id) state.delete_task_state() elif command == "clear_cache": if course_id is None: raise CommandError('"clear_cache" subcommand required course_id.') state = TaskState("pgreport.tasks.create_report_task", course_id) state.delete_task_state() else: raise CommandError("Invalid subcommand.")
def test_show_task_status(self): task = ProgressReportTask(self.func_mock) with patch('sys.stdout', new_callable=StringIO.StringIO) as std_mock: result_mock = MagicMock() result_mock.state = "PENDING" self.func_mock.AsyncResult.return_value = result_mock task.show_task_status("task_id") self.func_mock.AsyncResult.assert_called_once_with("task_id") self.assertEquals(std_mock.getvalue(), "Task not found or PENDING state\n" ) with patch('sys.stdout', new_callable=StringIO.StringIO) as std_mock: result_mock = MagicMock() result_mock.state = "SUCCESS" self.func_mock.AsyncResult.return_value = result_mock task.show_task_status("task_id") self.assertEquals(std_mock.getvalue(), "Current State: {}, {}\n".format(result_mock.state, result_mock.info) )
def test_send_task(self, ts_mock): task = ProgressReportTask(self.func_mock) with patch('sys.stdout', new_callable=StringIO.StringIO) as std_mock: ts_mock().is_active = False task.send_task(self.course1.id) ts_mock.assert_called_with(task.task_name, self.course1.id) ts_mock().set_task_state.assert_called_once_with() self.func_mock.apply_async.assert_called_with( args=(ANY, unicode(self.course1.id)), task_id=ANY, expires=23.5*60*60, retry=False ) self.assertEquals(std_mock.getvalue(), "Send task (task_id: %s)\n" % (self.func_mock.apply_async().id) ) with patch('sys.stdout', new_callable=StringIO.StringIO) as std_mock: ts_mock().is_active = True task.send_task(self.course1.id) self.assertEquals(std_mock.getvalue(), "Task is already running. (%s, %s)\n" % (task.task_name, self.course1.id) )
def test_revoke_task(self): task = ProgressReportTask(self.func_mock) task.revoke_task("task_id") self.func_mock.AsyncResult.assert_called_once_with("task_id") self.func_mock.AsyncResult().revoke.assert_called_once_with(terminate=True)