From 13561378ab500f6d28cfc92a11b67589edfc6fbe Mon Sep 17 00:00:00 2001 From: nolash Date: Thu, 27 May 2021 09:12:27 +0200 Subject: [PATCH] Add liveness check tests --- apps/cic-eth/cic_eth/db/util.py | 8 ------ apps/cic-eth/tests/check/test_check_db.py | 8 ++++++ apps/cic-eth/tests/check/test_check_gas.py | 20 +++++++++++++++ apps/cic-eth/tests/check/test_check_redis.py | 16 ++++++++++++ apps/cic-eth/tests/check/test_check_signer.py | 13 ++++++++++ apps/cic-eth/tests/conftest.py | 25 +++++++++++++++++++ 6 files changed, 82 insertions(+), 8 deletions(-) delete mode 100644 apps/cic-eth/cic_eth/db/util.py create mode 100644 apps/cic-eth/tests/check/test_check_db.py create mode 100644 apps/cic-eth/tests/check/test_check_gas.py create mode 100644 apps/cic-eth/tests/check/test_check_redis.py create mode 100644 apps/cic-eth/tests/check/test_check_signer.py diff --git a/apps/cic-eth/cic_eth/db/util.py b/apps/cic-eth/cic_eth/db/util.py deleted file mode 100644 index c7620626..00000000 --- a/apps/cic-eth/cic_eth/db/util.py +++ /dev/null @@ -1,8 +0,0 @@ -import math - -def num_serialize(n): - if n == 0: - return b'\x00' - binlog = math.log2(n) - bytelength = int(binlog / 8 + 1) - return n.to_bytes(bytelength, 'big') diff --git a/apps/cic-eth/tests/check/test_check_db.py b/apps/cic-eth/tests/check/test_check_db.py new file mode 100644 index 00000000..941f8892 --- /dev/null +++ b/apps/cic-eth/tests/check/test_check_db.py @@ -0,0 +1,8 @@ +# local imports +from cic_eth.check.db import health + +def test_check_health( + init_database, + ): + + assert health() diff --git a/apps/cic-eth/tests/check/test_check_gas.py b/apps/cic-eth/tests/check/test_check_gas.py new file mode 100644 index 00000000..ea0ffb88 --- /dev/null +++ b/apps/cic-eth/tests/check/test_check_gas.py @@ -0,0 +1,20 @@ +# local imports +from cic_eth.check.gas import health +from cic_eth.db.models.role import AccountRole + +def test_check_gas( + config, + init_database, + default_chain_spec, + eth_rpc, + custodial_roles, + whoever, + ): + + config.add(str(default_chain_spec), 'CIC_CHAIN_SPEC', exists_ok=True) + config.add(100, 'ETH_GAS_GIFTER_MINIMUM_BALANCE', exists_ok=True) + assert health(config=config) + + AccountRole.set('GAS_GIFTER', whoever, session=init_database) + init_database.commit() + assert not health(config=config) diff --git a/apps/cic-eth/tests/check/test_check_redis.py b/apps/cic-eth/tests/check/test_check_redis.py new file mode 100644 index 00000000..02c3ba77 --- /dev/null +++ b/apps/cic-eth/tests/check/test_check_redis.py @@ -0,0 +1,16 @@ +# external imports +import pytest + +# local imports +from cic_eth.check.redis import health + + +def test_check_redis( + config, + have_redis, + ): + + if have_redis != None: + pytest.skip('cannot connect to redis, skipping test: {}'.format(have_redis)) + + assert health(unit='test', config=config) diff --git a/apps/cic-eth/tests/check/test_check_signer.py b/apps/cic-eth/tests/check/test_check_signer.py new file mode 100644 index 00000000..5572e195 --- /dev/null +++ b/apps/cic-eth/tests/check/test_check_signer.py @@ -0,0 +1,13 @@ +# local imports +from cic_eth.check.signer import health + + +def test_check_signer( + default_chain_spec, + config, + eth_signer, + eth_rpc, + ): + + config.add(str(default_chain_spec), 'CIC_CHAIN_SPEC', exists_ok=True) + assert health(config=config) diff --git a/apps/cic-eth/tests/conftest.py b/apps/cic-eth/tests/conftest.py index bc7a82bb..acac0433 100644 --- a/apps/cic-eth/tests/conftest.py +++ b/apps/cic-eth/tests/conftest.py @@ -2,9 +2,11 @@ import os import sys import logging +import uuid # external imports from eth_erc20 import ERC20 +import redis # local imports from cic_eth.api import Api @@ -55,3 +57,26 @@ def default_token( ): BaseTask.default_token_symbol = foo_token_symbol BaseTask.default_token_address = foo_token + + + +@pytest.fixture(scope='session') +def have_redis( + config, + ): + + r = redis.Redis( + host = config.get('REDIS_HOST'), + port = config.get('REDIS_PORT'), + db = config.get('REDIS_DB'), + ) + k = str(uuid.uuid4()) + try: + r.set(k, 'foo') + r.delete(k) + except redis.exceptions.ConnectionError as e: + return e + except TypeError as e: + return e + + return None