def _pre_setup(self): super(TestCase, self)._pre_setup() # Clean the slate. cache.clear() post_request_task._discard_tasks() post_request_task._stop_queuing_tasks() trans_real.deactivate() trans_real._translations = {} # Django fails to clear this cache. trans_real.activate(settings.LANGUAGE_CODE) self.mock_browser_id() global JINJA_INSTRUMENTED if not JINJA_INSTRUMENTED: import jinja2 old_render = jinja2.Template.render def instrumented_render(self, *args, **kwargs): context = dict(*args, **kwargs) test.signals.template_rendered.send(sender=self, template=self, context=context) return old_render(self, *args, **kwargs) jinja2.Template.render = instrumented_render JINJA_INSTRUMENTED = True
def test_request_failed(self, _mock): request_started.send(sender=self) test_task.delay() self._verify_task_filled() # Simulate a request exception. _discard_tasks() self._verify_task_empty() # Assert the original `apply_async` was not called. assert not _mock.called, ( 'Unexpected PostRequestTask.original_apply_async call')
def tearDown(self): fake_task_func.reset_mock() _discard_tasks() _stop_queuing_tasks()
def setUp(self): super(TestTaskQueued, self).setUp() create_switch('activate-django-post-request') fake_task_func.reset_mock() _discard_tasks()
def tearDown(self): super().tearDown() fake_task_func.reset_mock() _discard_tasks() _stop_queuing_tasks()
def setUp(self): super().setUp() fake_task_func.reset_mock() _discard_tasks()
def process(self, version): """Process a single version, figuring out if it should be auto-approved and calling the approval code if necessary.""" already_locked = AutoApprovalSummary.check_is_locked(version) if not already_locked: # Lock the addon for ourselves if possible. Even though # AutoApprovalSummary.create_summary_for_version() will do # call check_is_locked() again later when calculating the verdict, # we have to do it now to prevent overwriting an existing lock with # our own. set_reviewing_cache(version.addon.pk, settings.TASK_USER_ID) # Discard any existing celery tasks that may have been queued before: # If there are any left at this point, it means the transaction from # the previous loop iteration was not committed and we shouldn't # trigger the corresponding tasks. _discard_tasks() # Queue celery tasks for this version, avoiding triggering them too # soon... _start_queuing_tasks() try: with transaction.atomic(): # ...and release the queued tasks to celery once transaction # is committed. transaction.on_commit(_send_tasks_and_stop_queuing) log.info( 'Processing %s version %s...', str(version.addon.name), str(version.version), ) if waffle.switch_is_active('run-action-in-auto-approve'): # We want to execute `run_action()` only once. summary_exists = AutoApprovalSummary.objects.filter( version=version).exists() if summary_exists: log.info('Not running run_action() because it has ' 'already been executed') else: ScannerResult.run_action(version) summary, info = AutoApprovalSummary.create_summary_for_version( version, dry_run=self.dry_run) self.stats.update({k: int(v) for k, v in info.items()}) if summary.verdict == self.successful_verdict: if summary.verdict == amo.AUTO_APPROVED: self.approve(version) self.stats['auto_approved'] += 1 verdict_string = summary.get_verdict_display() else: verdict_string = '%s (%s)' % ( summary.get_verdict_display(), ', '.join(summary.verdict_info_prettifier(info)), ) log.info( 'Auto Approval for %s version %s: %s', str(version.addon.name), str(version.version), verdict_string, ) # At this point, any exception should have rolled back the transaction, # so even if we did create/update an AutoApprovalSummary instance that # should have been rolled back. This ensures that, for instance, a # signing error doesn't leave the version and its autoapprovalsummary # in conflicting states. except (AutoApprovalNotEnoughFilesError, AutoApprovalNoValidationResultError): log.info( 'Version %s was skipped either because it had no ' 'files or because it had no validation attached.', version, ) self.stats['error'] += 1 except SigningError: statsd.incr('reviewers.auto_approve.approve.failure') log.info('Version %s was skipped because of a signing error', version) self.stats['error'] += 1 finally: # Always clear our own lock no matter what happens (but only ours). if not already_locked: clear_reviewing_cache(version.addon.pk) # Stop post request task queue before moving on (useful in tests to # leave a fresh state for the next test. Note that we don't want to # send or clear queued tasks (they may belong to a transaction that # has been rolled back, or they may not have been processed by the # on commit handler yet). _stop_queuing_tasks()
def tearDown(self): task_mock.reset_mock() _discard_tasks() _stop_queuing_tasks() super(TestTask, self).tearDown()
def setUp(self): super(TestTask, self).setUp() task_mock.reset_mock() _discard_tasks()
def setUp(self): super(TestTaskQueued, self).setUp() fake_task_func.reset_mock() _discard_tasks()