Skip to content

Commit

Permalink
refactor locking Core functionality to separate library (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
jupe authored Oct 3, 2020
1 parent 213d923 commit 867d2e6
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 321 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ jobs:
command: |
mkdir junit || true
. venv/bin/activate;
nosetests --with-xunit --with-coverage --cover-package=lockable --cover-html --cover-html-dir=htmlcov --cover-xml-file=coverage.xml --xunit-file=junit/results.xml
nosetests --with-xunit --with-coverage --cover-package=pytest_lockable --cover-html --cover-html-dir=htmlcov --cover-xml-file=coverage.xml --xunit-file=junit/results.xml
coveralls || true
- run:
name: pylint
command: |
. venv/bin/activate;
pylint lockable
pylint pytest_lockable
- run:
name: run
command: |
Expand Down
2 changes: 1 addition & 1 deletion example/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
PLUGIN_DIR = os.path.join(TEST_DIR, '../')
sys.path.insert(0, PLUGIN_DIR)

pytest_plugins = ("lockable.plugin", "metadata") # pylint: disable=invalid-name
pytest_plugins = ("pytest_lockable.plugin", "metadata") # pylint: disable=invalid-name
187 changes: 0 additions & 187 deletions lockable/plugin.py

This file was deleted.

File renamed without changes.
65 changes: 65 additions & 0 deletions pytest_lockable/plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
""" Lockable plugin for pytest """
import json
import socket
import tempfile
from contextlib import contextmanager
import pytest
from lockable import parse_requirements, get_requirements, read_resources_list, lock


def pytest_addoption(parser):
"""
register argparse-style options and ini-style config values,
called once at the beginning of a test run
"""
group = parser.getgroup("lockable")
group.addoption("--allocation_hostname", default=socket.gethostname(), help="Allocation host")
group.addoption("--allocation_requirements", default=None, help="Resource requirements to be allocate")
group.addoption("--allocation_timeout", default=10, help="Allocation timeout")
group.addoption("--allocation_resource_list_file", default='resources.json', help="Available resorces list")
group.addoption("--allocation_lock_folder", default=tempfile.gettempdir(), help="Allocation lockfiles folder")


@pytest.fixture(scope="session", autouse=True)
def lockable(pytestconfig, record_testsuite_property):
"""
pytest fixture that yields function for allocate any resource
.. code-block:: python
def test_foo(lockable_allocate):
with lockable({my: "resource}) as resource:
print(resource)
"""
resource_list = read_resources_list(pytestconfig.getoption('allocation_resource_list_file'))
timeout_s = pytestconfig.getoption('allocation_timeout')
lock_folder = pytestconfig.getoption('allocation_lock_folder')

@contextmanager
def _lock(requirements, prefix='resource'):
nonlocal resource_list, timeout_s, lock_folder
requirements = parse_requirements(requirements)
predicate = get_requirements(requirements, pytestconfig.getoption('allocation_hostname'))
print(f"Use lock folder: {lock_folder}")
print(f"Requirements: {json.dumps(predicate)}")
print(f"Resource list: {json.dumps(resource_list)}")
with lock(predicate, resource_list, timeout_s, lock_folder) as resource:
for key, value in resource.items():
record_testsuite_property(f'resource_{key}', value)
if pytestconfig.pluginmanager.hasplugin('metadata'):
# pylint: disable=protected-access
pytestconfig._metadata[f'{prefix}_{key}'] = value
yield resource

yield _lock


@pytest.fixture(scope="session", autouse=True)
def lockable_resource(pytestconfig, lockable): # pylint: disable=redefined-outer-name
"""
pytest fixture that lock suitable resource and yield it
.. code-block:: python
def test_foo(lockable_resource):
print(f'Testing with resource: {lockable_resource}')
"""
requirements = pytestconfig.getoption('allocation_requirements')
with lockable(requirements) as resource:
yield resource
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
python_requires='>=3.7, <4',
install_requires=[
'pytest',
'lockable==0.1.1',
'func_timeout',
'filelock',
'pydash'
Expand Down
Loading

0 comments on commit 867d2e6

Please sign in to comment.