mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-17 10:13:56 -05:00
Adds further testing to cover scripts with non-zero exit codes
This commit is contained in:
parent
c4965580de
commit
057f6016cc
@ -163,7 +163,7 @@ class Consumer(LoggingMixin):
|
|||||||
# Raises exception on non-zero output
|
# Raises exception on non-zero output
|
||||||
completed_proc.check_returncode()
|
completed_proc.check_returncode()
|
||||||
|
|
||||||
except Exception as e: # pragma: nocover
|
except Exception as e:
|
||||||
self._fail(
|
self._fail(
|
||||||
MESSAGE_PRE_CONSUME_SCRIPT_ERROR,
|
MESSAGE_PRE_CONSUME_SCRIPT_ERROR,
|
||||||
f"Error while executing pre-consume script: {e}",
|
f"Error while executing pre-consume script: {e}",
|
||||||
@ -237,7 +237,7 @@ class Consumer(LoggingMixin):
|
|||||||
# Raises exception on non-zero output
|
# Raises exception on non-zero output
|
||||||
completed_proc.check_returncode()
|
completed_proc.check_returncode()
|
||||||
|
|
||||||
except Exception as e: # pragma: nocover
|
except Exception as e:
|
||||||
self._fail(
|
self._fail(
|
||||||
MESSAGE_POST_CONSUME_SCRIPT_ERROR,
|
MESSAGE_POST_CONSUME_SCRIPT_ERROR,
|
||||||
f"Error while executing post-consume script: {e}",
|
f"Error while executing post-consume script: {e}",
|
||||||
|
@ -803,6 +803,15 @@ class TestConsumerCreatedDate(DirectoriesMixin, TestCase):
|
|||||||
|
|
||||||
|
|
||||||
class PreConsumeTestCase(TestCase):
|
class PreConsumeTestCase(TestCase):
|
||||||
|
def setUp(self) -> None:
|
||||||
|
|
||||||
|
# this prevents websocket message reports during testing.
|
||||||
|
patcher = mock.patch("documents.consumer.Consumer._send_progress")
|
||||||
|
self._send_progress = patcher.start()
|
||||||
|
self.addCleanup(patcher.stop)
|
||||||
|
|
||||||
|
return super().setUp()
|
||||||
|
|
||||||
@mock.patch("documents.consumer.run")
|
@mock.patch("documents.consumer.run")
|
||||||
@override_settings(PRE_CONSUME_SCRIPT=None)
|
@override_settings(PRE_CONSUME_SCRIPT=None)
|
||||||
def test_no_pre_consume_script(self, m):
|
def test_no_pre_consume_script(self, m):
|
||||||
@ -868,8 +877,41 @@ class PreConsumeTestCase(TestCase):
|
|||||||
mocked_log.assert_any_call("info", "This message goes to stdout")
|
mocked_log.assert_any_call("info", "This message goes to stdout")
|
||||||
mocked_log.assert_any_call("warning", "This message goes to stderr")
|
mocked_log.assert_any_call("warning", "This message goes to stderr")
|
||||||
|
|
||||||
|
def test_script_exit_non_zero(self):
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A script which exits with a non-zero exit code
|
||||||
|
WHEN:
|
||||||
|
- The script is executed as a pre-consume script
|
||||||
|
THEN:
|
||||||
|
- A ConsumerError is raised
|
||||||
|
"""
|
||||||
|
with tempfile.NamedTemporaryFile(mode="w") as script:
|
||||||
|
# Write up a little script
|
||||||
|
with script.file as outfile:
|
||||||
|
outfile.write("#!/usr/bin/env bash\n")
|
||||||
|
outfile.write("exit 100\n")
|
||||||
|
|
||||||
|
# Make the file executable
|
||||||
|
st = os.stat(script.name)
|
||||||
|
os.chmod(script.name, st.st_mode | stat.S_IEXEC)
|
||||||
|
|
||||||
|
with override_settings(PRE_CONSUME_SCRIPT=script.name):
|
||||||
|
c = Consumer()
|
||||||
|
c.path = "path-to-file"
|
||||||
|
self.assertRaises(ConsumerError, c.run_pre_consume_script)
|
||||||
|
|
||||||
|
|
||||||
class PostConsumeTestCase(TestCase):
|
class PostConsumeTestCase(TestCase):
|
||||||
|
def setUp(self) -> None:
|
||||||
|
|
||||||
|
# this prevents websocket message reports during testing.
|
||||||
|
patcher = mock.patch("documents.consumer.Consumer._send_progress")
|
||||||
|
self._send_progress = patcher.start()
|
||||||
|
self.addCleanup(patcher.stop)
|
||||||
|
|
||||||
|
return super().setUp()
|
||||||
|
|
||||||
@mock.patch("documents.consumer.run")
|
@mock.patch("documents.consumer.run")
|
||||||
@override_settings(POST_CONSUME_SCRIPT=None)
|
@override_settings(POST_CONSUME_SCRIPT=None)
|
||||||
def test_no_post_consume_script(self, m):
|
def test_no_post_consume_script(self, m):
|
||||||
@ -930,3 +972,29 @@ class PostConsumeTestCase(TestCase):
|
|||||||
self.assertEqual(command[6], f"/api/documents/{doc.pk}/thumb/")
|
self.assertEqual(command[6], f"/api/documents/{doc.pk}/thumb/")
|
||||||
self.assertEqual(command[7], "my_bank")
|
self.assertEqual(command[7], "my_bank")
|
||||||
self.assertCountEqual(command[8].split(","), ["a", "b"])
|
self.assertCountEqual(command[8].split(","), ["a", "b"])
|
||||||
|
|
||||||
|
def test_script_exit_non_zero(self):
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A script which exits with a non-zero exit code
|
||||||
|
WHEN:
|
||||||
|
- The script is executed as a post-consume script
|
||||||
|
THEN:
|
||||||
|
- A ConsumerError is raised
|
||||||
|
"""
|
||||||
|
with tempfile.NamedTemporaryFile(mode="w") as script:
|
||||||
|
# Write up a little script
|
||||||
|
with script.file as outfile:
|
||||||
|
outfile.write("#!/usr/bin/env bash\n")
|
||||||
|
outfile.write("exit -500\n")
|
||||||
|
|
||||||
|
# Make the file executable
|
||||||
|
st = os.stat(script.name)
|
||||||
|
os.chmod(script.name, st.st_mode | stat.S_IEXEC)
|
||||||
|
|
||||||
|
with override_settings(POST_CONSUME_SCRIPT=script.name):
|
||||||
|
c = Consumer()
|
||||||
|
doc = Document.objects.create(title="Test", mime_type="application/pdf")
|
||||||
|
c.path = "path-to-file"
|
||||||
|
with self.assertRaises(ConsumerError):
|
||||||
|
c.run_post_consume_script(doc)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user