diff --git a/aries_cloudagent/holder/tests/test_indy.py b/aries_cloudagent/holder/tests/test_indy.py index f390e2fdda..be73bd7108 100644 --- a/aries_cloudagent/holder/tests/test_indy.py +++ b/aries_cloudagent/holder/tests/test_indy.py @@ -5,24 +5,10 @@ import pytest -try: - from indy.libindy import _cdll - - _cdll() -except ImportError: - pytest.skip( - "skipping Indy-specific tests: python module not installed", - allow_module_level=True, - ) -except OSError: - pytest.skip( - "skipping Indy-specific tests: shared library not loaded", - allow_module_level=True, - ) - from aries_cloudagent.holder.indy import IndyHolder +@pytest.mark.indy class TestIndyHolder(AsyncTestCase): def test_init(self): holder = IndyHolder("wallet") diff --git a/aries_cloudagent/ledger/tests/test_indy.py b/aries_cloudagent/ledger/tests/test_indy.py index f1395257b8..b242997d55 100644 --- a/aries_cloudagent/ledger/tests/test_indy.py +++ b/aries_cloudagent/ledger/tests/test_indy.py @@ -6,21 +6,6 @@ import pytest -try: - from indy.libindy import _cdll - - _cdll() -except ImportError: - pytest.skip( - "skipping Indy-specific tests: python module not installed", - allow_module_level=True, - ) -except OSError: - pytest.skip( - "skipping Indy-specific tests: shared library not loaded", - allow_module_level=True, - ) - from aries_cloudagent.ledger.indy import ( IndyLedger, GENESIS_TRANSACTION_PATH, @@ -30,6 +15,7 @@ ) +@pytest.mark.indy class TestIndyLedger(AsyncTestCase): @async_mock.patch("builtins.open") def test_init(self, mock_open): diff --git a/aries_cloudagent/messaging/problem_report/handler.py b/aries_cloudagent/messaging/problem_report/handler.py index 6ee7d85d94..ac8e2528c3 100644 --- a/aries_cloudagent/messaging/problem_report/handler.py +++ b/aries_cloudagent/messaging/problem_report/handler.py @@ -1,6 +1,6 @@ """Generic problem report handler.""" -from ...base_handler import BaseHandler, BaseResponder, RequestContext +from ..base_handler import BaseHandler, BaseResponder, RequestContext from .message import ProblemReport diff --git a/aries_cloudagent/storage/tests/test_indy_storage.py b/aries_cloudagent/storage/tests/test_indy_storage.py index 88ab730b49..a4055a94fd 100644 --- a/aries_cloudagent/storage/tests/test_indy_storage.py +++ b/aries_cloudagent/storage/tests/test_indy_storage.py @@ -1,19 +1,5 @@ import pytest - -try: - from indy.libindy import _cdll - - _cdll() -except ImportError: - pytest.skip( - "skipping Indy-specific tests: python module not installed", - allow_module_level=True, - ) -except OSError: - pytest.skip( - "skipping Indy-specific tests: shared library not loaded", - allow_module_level=True, - ) +import os from aries_cloudagent.wallet.indy import IndyWallet from aries_cloudagent.storage.indy import IndyStorage @@ -33,15 +19,21 @@ async def store(): await wallet.close() +@pytest.mark.indy class TestIndyStorage(test_basic_storage.TestBasicStorage): """ """ # TODO get these to run in docker ci/cd - # @pytest.mark.asyncio + @pytest.mark.asyncio + @pytest.mark.postgres async def test_postgres_wallet_storage_works(self): """ Ensure that postgres wallet operations work (create and open wallet, store and search, drop wallet) """ + postgres_url = os.environ.get("POSTGRES_URL") + if not postgres_url: + pytest.fail("POSTGRES_URL not configured") + load_postgres_plugin() postgres_wallet = IndyWallet( { @@ -50,7 +42,7 @@ async def test_postgres_wallet_storage_works(self): "name": "test_pg_wallet", "key": "my_postgres", "storage_type": "postgres_storage", - "storage_config": '{"url":"host.docker.internal:5432", "max_connections":5}', + "storage_config": '{"url":"' + postgres_url + '", "max_connections":5}', "storage_creds": '{"account":"postgres","password":"mysecretpassword","admin_account":"postgres","admin_password":"mysecretpassword"}', } ) diff --git a/aries_cloudagent/verifier/tests/test_indy.py b/aries_cloudagent/verifier/tests/test_indy.py index 7037319a74..82e0c20b7a 100644 --- a/aries_cloudagent/verifier/tests/test_indy.py +++ b/aries_cloudagent/verifier/tests/test_indy.py @@ -5,21 +5,6 @@ import pytest -try: - from indy.libindy import _cdll - - _cdll() -except ImportError: - pytest.skip( - "skipping Indy-specific tests: python module not installed", - allow_module_level=True, - ) -except OSError: - pytest.skip( - "skipping Indy-specific tests: shared library not loaded", - allow_module_level=True, - ) - from aries_cloudagent.ledger.indy import ( IndyLedger, GENESIS_TRANSACTION_PATH, @@ -31,6 +16,7 @@ from aries_cloudagent.verifier.indy import IndyVerifier +@pytest.mark.indy class TestIndyVerifier(AsyncTestCase): def test_init(self): verifier = IndyVerifier("wallet") diff --git a/aries_cloudagent/wallet/tests/test_indy_wallet.py b/aries_cloudagent/wallet/tests/test_indy_wallet.py index 98ac854445..ad2bf588e7 100644 --- a/aries_cloudagent/wallet/tests/test_indy_wallet.py +++ b/aries_cloudagent/wallet/tests/test_indy_wallet.py @@ -1,19 +1,5 @@ import pytest - -try: - from indy.libindy import _cdll - - _cdll() -except ImportError: - pytest.skip( - "skipping Indy-specific tests: python module not installed", - allow_module_level=True, - ) -except OSError: - pytest.skip( - "skipping Indy-specific tests: shared library not loaded", - allow_module_level=True, - ) +import os from aries_cloudagent.wallet.basic import BasicWallet from aries_cloudagent.wallet.indy import IndyWallet @@ -40,10 +26,12 @@ async def wallet(): await wallet.close() +@pytest.mark.indy class TestIndyWallet(test_basic_wallet.TestBasicWallet): """Apply all BasicWallet tests against IndyWallet""" +@pytest.mark.indy class TestWalletCompat: """ """ @@ -130,11 +118,16 @@ async def test_compare_pack(self, basic_wallet, wallet): assert self.test_message == unpacked # TODO get these to run in docker ci/cd - # @pytest.mark.asyncio + @pytest.mark.asyncio + @pytest.mark.postgres async def test_postgres_wallet_works(self): """ Ensure that postgres wallet operations work (create and open wallet, create did, drop wallet) """ + postgres_url = os.environ.get("POSTGRES_URL") + if not postgres_url: + pytest.fail("POSTGRES_URL not configured") + load_postgres_plugin() postgres_wallet = IndyWallet( { @@ -143,7 +136,7 @@ async def test_postgres_wallet_works(self): "name": "test_pg_wallet", "key": "my_postgres", "storage_type": "postgres_storage", - "storage_config": '{"url":"host.docker.internal:5432"}', + "storage_config": '{"url":"' + postgres_url + '"}', "storage_creds": '{"account":"postgres","password":"mysecretpassword","admin_account":"postgres","admin_password":"mysecretpassword"}', } ) diff --git a/conftest.py b/conftest.py new file mode 100644 index 0000000000..312d77220a --- /dev/null +++ b/conftest.py @@ -0,0 +1,63 @@ +import os +import sys +from unittest import mock + +import pytest + +INDY_FOUND = False +INDY_STUB = None +POSTGRES_URL = None + + +def pytest_sessionstart(session): + global INDY_FOUND, INDY_STUB, POSTGRES_URL + + # detect indy module + try: + from indy.libindy import _cdll + + _cdll() + + INDY_FOUND = True + except ImportError: + "skipping Indy-specific tests: python module not installed", + except OSError: + "skipping Indy-specific tests: shared library not loaded", + + if not INDY_FOUND: + modules = {} + package_name = "indy" + modules[package_name] = mock.MagicMock() + for mod in [ + "anoncreds", + "crypto", + "did", + "error", + "pool", + "ledger", + "non_secrets", + "pairwise", + "wallet", + ]: + submod = f"{package_name}.{mod}" + modules[submod] = mock.MagicMock() + INDY_STUB = mock.patch.dict(sys.modules, modules) + INDY_STUB.start() + + POSTGRES_URL = os.getenv("POSTGRES_URL") + + +def pytest_sessionfinish(session): + global INDY_STUB + if INDY_STUB: + INDY_STUB.stop() + INDY_STUB = None + + +def pytest_runtest_setup(item: pytest.Item): + + if tuple(item.iter_markers(name="indy")) and not INDY_FOUND: + pytest.skip("test requires Indy support") + + if tuple(item.iter_markers(name="postgres")) and not POSTGRES_URL: + pytest.skip("test requires Postgres support") diff --git a/docker/Dockerfile.demo b/docker/Dockerfile.demo index 9b2cfd90ac..e4b7f5f69e 100644 --- a/docker/Dockerfile.demo +++ b/docker/Dockerfile.demo @@ -10,6 +10,7 @@ RUN pip3 install --no-cache-dir -r requirements.txt -r requirements.dev.txt ADD aries_cloudagent ./aries_cloudagent ADD bin ./bin +ADD README.md ./ ADD scripts ./scripts ADD setup.py ./ diff --git a/scripts/run_tests_indy b/scripts/run_tests_indy index d4ae1d07fb..42a0243117 100755 --- a/scripts/run_tests_indy +++ b/scripts/run_tests_indy @@ -13,6 +13,17 @@ else DOCKER="docker" fi +if [ -z "$POSTGRES_URL" ]; then + if [ ! -z $(docker ps --filter name=indy-demo-postgres --quiet) ]; then + DOCKER_ARGS="$DOCKER_ARGS --link indy-demo-postgres" + POSTGRES_URL="indy-demo-postgres" + fi +fi +if [ ! -z "$POSTGRES_URL" ]; then + DOCKER_ARGS="$DOCKER_ARGS -e POSTGRES_URL=$POSTGRES_URL" +fi + $DOCKER run --rm -ti --name aries-cloudagent-runner \ -v "$(pwd)/../test-reports:/usr/src/app/test-reports" \ + $DOCKER_ARGS \ aries-cloudagent-test "$@" diff --git a/setup.cfg b/setup.cfg index 226371e0e7..43298ba02d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,12 @@ [tool:pytest] testpaths = aries_cloudagent -addopts = --quiet --junitxml=./test-reports/junit.xml --cov-config .coveragerc --cov=aries_cloudagent --cov-report term --cov-report xml --flake8 +addopts = + --quiet --junitxml=./test-reports/junit.xml + --cov-config .coveragerc --cov=aries_cloudagent --cov-report term-missing --cov-report xml + --flake8 +markers = + indy: Tests specifically relating to Hyperledger Indy support + postgres: Tests relating to the postgres storage plugin for Indy [flake8] # https://github.com/ambv/black#line-length