Skip to content

Commit

Permalink
Test a similated failed Scoop capture.
Browse files Browse the repository at this point in the history
  • Loading branch information
rebeccacremona committed Sep 26, 2023
1 parent 5a23617 commit 0a561ff
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
38 changes: 37 additions & 1 deletion perma_web/api/tests/test_link_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from mock import patch
import pytest

from .utils import ApiResourceTestCase, ApiResourceTransactionTestCase, TEST_ASSETS_DIR, index_warc_file, raise_on_call, raise_after_call, MockResponse
from .utils import ApiResourceTestCase, ApiResourceTransactionTestCase, TEST_ASSETS_DIR, index_warc_file, raise_on_call, raise_after_call, return_on_call, MockResponse
from perma.models import Link, LinkUser, Folder


Expand Down Expand Up @@ -802,3 +802,39 @@ def test_scoop_capture_request_polling_error_limit_exceeded(self, mockrequest):
self.assertEqual(link.capture_job.status, 'failed')
captured = self.capsys.readouterr()
self.assertIn("HaltCaptureException thrown\n", captured.out)


############################
# Scoop simulated failures #
############################

if settings.CAPTURE_ENGINE == 'scoop-api':

@patch('perma.utils.requests.request', autospec=True)
def test_scoop_capture_hung(self, mockrequest):
# from https://perma-stage.org/admin/perma/capturejob/2059/change/
mockrequest.side_effect = return_on_call(orig_request, 2, MockResponse({
"url": "https://www.nytimes.com/",
"status": "failed",
"id_capture": "2ca5dad1-20fd-4550-9129-a0ce64ecc662",
"stderr_logs": None,
"stdout_logs": None,
"callback_url": None,
"ended_timestamp": "Wed, 20 Sep 2023 15:57:44 GMT",
"created_timestamp": "Wed, 20 Sep 2023 15:56:34 GMT",
"started_timestamp": "Wed, 20 Sep 2023 15:56:34 GMT",
"scoop_capture_summary": None},
200
))
with self.assertLogs('celery.django', level='ERROR') as logs:
obj = self.successful_post(self.list_url,
data={
'url': self.server_url + "/test.html"
},
user=self.org_user)
link = Link.objects.get(guid=obj['guid'])
self.assertEqual(link.primary_capture.status, 'failed')
self.assertEqual(link.capture_job.status, 'failed')

log_string = " ".join(logs.output)
self.assertTrue(log_string.endswith("'scoop_capture_summary': None}"))
24 changes: 23 additions & 1 deletion perma_web/api/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def raise_after_call(func, call_count, exc=Exception):
Example:
>>> def f(): pass
>>> bomb = raise_on_call(f, 3, ValueError('I exploded'))
>>> bomb = raise_after_call(f, 3, ValueError('I exploded'))
>>> bomb()
>>> bomb()
>>> with assert_raises(ValueError):
Expand All @@ -83,6 +83,28 @@ def wrapper(*args, **kwargs):
return wrapper


def return_on_call(func, call_count, val=None):
"""
Return a particular response, after a function is called a certain number of times.
Example:
>>> def f(): pass
>>> bomb = return_on_call(f, 3, 'I exploded')
>>> bomb()
>>> bomb()
>>> assert bomb() == "I exploded"
"""
@wraps(func)
def wrapper(*args, **kwargs):
wrapper.calls += 1
if wrapper.calls >= call_count:
return val
return func(*args, **kwargs)
wrapper.calls = 0
return wrapper


def copy_file_or_dir(src, dst):
dst_dir = os.path.abspath(os.path.dirname(dst))
if not os.path.isdir(dst_dir):
Expand Down

0 comments on commit 0a561ff

Please sign in to comment.