Commit 798588c4 authored by Leo Pound Singer's avatar Leo Pound Singer
Browse files

Use a real worker for condor tests

parent 1ce0d0b0
......@@ -12,5 +12,6 @@ ipython = "*"
ipykernel = "*"
flower = ">= 1"
# Dependencies needed for running unit tests
pytest-celery = "*"
pytest-flask = "*"
pytest-socket = "*"
{
"_meta": {
"hash": {
"sha256": "0ebcf10d8e8c6f8dd682dd472845837d61b6d9208fc6289fe5e077d44465cd9e"
"sha256": "b077c9e5005887b4af4e4799282bb95fc6621a44f8e33069bac7a9f4e9753416"
},
"pipfile-spec": 6,
"requires": {},
......@@ -26,7 +26,7 @@
"sha256:93aa393e9d6c54c5cd570ccadd8edad61ea0c4b9ea7a01409020c9aa019eb442",
"sha256:dd83cd4b5b460958838f6eb3000c660b1f9caf2a5b1de4264e941512f603258a"
],
"markers": "platform_system == 'Darwin'",
"markers": "sys_platform == 'darwin'",
"version": "==0.1.2"
},
"argcomplete": {
......@@ -516,7 +516,7 @@
"sha256:b618b6d2d5ffa2f16add5697cf57a46c76a56229b0ed1c438322e4e95645bd15",
"sha256:f284b3e11256ad1e5d03ab86bb2ccd6f5339688ff17a4d797a0fe7df326f23b1"
],
"markers": "python_version == '3.7'",
"markers": "python_version < '3.8'",
"version": "==4.8.1"
},
"incremental": {
......@@ -1227,6 +1227,14 @@
],
"version": "==6.2.5"
},
"pytest-celery": {
"hashes": [
"sha256:63dec132df3a839226ecb003ffdbb0c2cb88dd328550957e979c942766578060",
"sha256:cfd060fc32676afa1e4f51b2938f903f7f75d952186b8c6cf631628c4088f406"
],
"index": "pypi",
"version": "==0.0.0"
},
"pytest-flask": {
"hashes": [
"sha256:46fde652f77777bf02dc91205aec4ce20cdf2acbbbd66a918ab91f5c14693d3d",
......
from unittest import mock
from celery.contrib.testing.app import UnitLogging
import pytest
from pytest_socket import disable_socket
......@@ -8,12 +9,11 @@ from .process import starter # noqa: F401
@pytest.fixture(scope='session', autouse=True)
def celeryconf(tmp_path_factory):
broker_url = 'redis+socket://' + str(
tmp_path_factory.mktemp('sockets') / 'sock')
def noop_celery_config():
"""Ensure that the Celery app is disconnected from live services."""
new_conf = dict(
broker_url=broker_url,
result_backend=broker_url,
broker_url='redis://redis.invalid',
result_backend='redis://redis.invalid',
voevent_broadcaster_address='127.0.0.1:53410',
voevent_broadcaster_whitelist=['127.0.0.0/8'],
voevent_receiver_address='gcn.invalid:8099',
......@@ -29,6 +29,33 @@ def celeryconf(tmp_path_factory):
app.conf.update(tmp)
@pytest.fixture
def celery_config():
"""Celery application configuration for tests that need a real worker."""
return dict(
broker_url='memory://',
result_backend='cache+memory://',
task_always_eager=False,
task_eager_propagates=False,
)
@pytest.fixture
def celery_worker_parameters():
return dict(perform_ping_check=False)
@pytest.fixture
def celery_app(celery_config, celery_enable_logging, monkeypatch):
new_conf = celery_config
tmp = {key: app.conf[key] for key in new_conf.keys()}
app.conf.update(new_conf)
if not celery_enable_logging:
monkeypatch.setattr(app, 'log', UnitLogging(app))
yield app
app.conf.update(tmp)
@pytest.fixture(autouse=True)
def fake_gracedb_client(monkeypatch):
mock_client = mock.MagicMock()
......@@ -44,4 +71,12 @@ def fake_legacy_gracedb_client(monkeypatch):
def pytest_runtest_setup():
# Celery caches the backend instance.
# Since we switch backends between unit tests, make sure that the cached
# backend is cleared.
try:
del app._local.backend
except AttributeError:
pass
disable_socket()
......@@ -82,7 +82,7 @@ def mock_condor_submit(monkeypatch):
monkeypatch.setattr('subprocess.run', mock_run)
def test_check_output_error_on_submit(monkeypatch):
def test_check_output_error_on_submit(celery_worker, monkeypatch):
"""Test capturing an error from condor_submit."""
accounting_group = 'foo.bar'
cmd = ('sleep', '1')
......@@ -93,22 +93,25 @@ def test_check_output_error_on_submit(monkeypatch):
monkeypatch.setattr('subprocess.run', mock_run)
result = condor.check_output.delay(cmd, accounting_group=accounting_group)
with pytest.raises(subprocess.CalledProcessError) as exc_info:
condor.check_output.delay(cmd, accounting_group=accounting_group)
result.get()
assert 'accounting_group=' + accounting_group in exc_info.value.cmd
assert exc_info.value.output == msg
def test_check_output_aborted(mock_condor_submit_aborted):
def test_check_output_aborted(celery_worker, mock_condor_submit_aborted):
"""Test a job that is aborted."""
result = condor.check_output.delay(['sleep', '1'])
with pytest.raises(condor.JobAborted):
condor.check_output.delay(['sleep', '1'])
result.get()
def test_check_output_fails(mock_condor_submit):
def test_check_output_fails(celery_worker, mock_condor_submit):
"""Test a job that immediately fails."""
result = condor.check_output.delay(['sleep', '--foo="bar bat"', '1'])
with pytest.raises(condor.JobFailed) as exc_info:
condor.check_output.delay(['sleep', '--foo="bar bat"', '1'])
result.get()
assert exc_info.value.returncode == 1
......
......@@ -51,6 +51,7 @@ project_urls =
packages = find:
python_requires = >=3.7
tests_require =
pytest-celery
pytest-flask
pytest-socket
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment