mirror of
https://github.com/planetmint/planetmint.git
synced 2025-11-26 23:45:47 +00:00
fixed some backend init issues and error handling
Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
parent
f85af010ea
commit
b7c4acd830
@ -6,6 +6,7 @@
|
|||||||
import logging
|
import logging
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
from planetmint.config import Config
|
from planetmint.config import Config
|
||||||
|
from planetmint.common.exceptions import ConfigurationError
|
||||||
|
|
||||||
BACKENDS = { # This is path to MongoDBClass
|
BACKENDS = { # This is path to MongoDBClass
|
||||||
'tarantool_db': 'planetmint.backend.tarantool.connection.TarantoolDB',
|
'tarantool_db': 'planetmint.backend.tarantool.connection.TarantoolDB',
|
||||||
@ -30,40 +31,43 @@ def Connection(host: str = None, port: int = None, login: str = None, password:
|
|||||||
port = port or Config().get()['database']['port'] if not kwargs.get("port") else kwargs["port"]
|
port = port or Config().get()['database']['port'] if not kwargs.get("port") else kwargs["port"]
|
||||||
login = login or Config().get()["database"]["login"] if not kwargs.get("login") else kwargs["login"]
|
login = login or Config().get()["database"]["login"] if not kwargs.get("login") else kwargs["login"]
|
||||||
password = password or Config().get()["database"]["password"]
|
password = password or Config().get()["database"]["password"]
|
||||||
if backend == "tarantool_db":
|
try:
|
||||||
modulepath, _, class_name = BACKENDS[backend].rpartition('.')
|
if backend == "tarantool_db":
|
||||||
Class = getattr(import_module(modulepath), class_name)
|
modulepath, _, class_name = BACKENDS[backend].rpartition('.')
|
||||||
print("LOGIN " + str(login))
|
Class = getattr(import_module(modulepath), class_name)
|
||||||
print("PASSWORD " + str(password))
|
print("LOGIN " + str(login))
|
||||||
return Class(host=host, port=port, user=login, password=password)
|
print("PASSWORD " + str(password))
|
||||||
elif backend == "localmongodb":
|
return Class(host=host, port=port, user=login, password=password)
|
||||||
modulepath, _, class_name = BACKENDS[backend].rpartition('.')
|
elif backend == "localmongodb":
|
||||||
Class = getattr(import_module(modulepath), class_name)
|
modulepath, _, class_name = BACKENDS[backend].rpartition('.')
|
||||||
print(Config().get())
|
Class = getattr(import_module(modulepath), class_name)
|
||||||
dbname = _kwargs_parser(key="name", kwargs=kwargs) or Config().get()['database']['name']
|
print(Config().get())
|
||||||
replicaset = _kwargs_parser(key="replicaset", kwargs=kwargs) or Config().get()['database']['replicaset']
|
dbname = _kwargs_parser(key="name", kwargs=kwargs) or Config().get()['database']['name']
|
||||||
ssl = _kwargs_parser(key="ssl", kwargs=kwargs) or Config().get()['database']['ssl']
|
replicaset = _kwargs_parser(key="replicaset", kwargs=kwargs) or Config().get()['database']['replicaset']
|
||||||
login = login or Config().get()['database']['login'] if _kwargs_parser(key="login",
|
ssl = _kwargs_parser(key="ssl", kwargs=kwargs) or Config().get()['database']['ssl']
|
||||||
kwargs=kwargs) is None else _kwargs_parser(
|
login = login or Config().get()['database']['login'] if _kwargs_parser(key="login",
|
||||||
key="login", kwargs=kwargs)
|
kwargs=kwargs) is None else _kwargs_parser(
|
||||||
password = password or Config().get()['database']['password'] if _kwargs_parser(key="password",
|
key="login", kwargs=kwargs)
|
||||||
kwargs=kwargs) is None else _kwargs_parser(
|
password = password or Config().get()['database']['password'] if _kwargs_parser(key="password",
|
||||||
key="password", kwargs=kwargs)
|
kwargs=kwargs) is None else _kwargs_parser(
|
||||||
ca_cert = _kwargs_parser(key="ca_cert", kwargs=kwargs) or Config().get()['database']['ca_cert']
|
key="password", kwargs=kwargs)
|
||||||
certfile = _kwargs_parser(key="certfile", kwargs=kwargs) or Config().get()['database']['certfile']
|
ca_cert = _kwargs_parser(key="ca_cert", kwargs=kwargs) or Config().get()['database']['ca_cert']
|
||||||
keyfile = _kwargs_parser(key="keyfile", kwargs=kwargs) or Config().get()['database']['keyfile']
|
certfile = _kwargs_parser(key="certfile", kwargs=kwargs) or Config().get()['database']['certfile']
|
||||||
keyfile_passphrase = _kwargs_parser(key="keyfile_passphrase", kwargs=kwargs) or Config().get()['database'][
|
keyfile = _kwargs_parser(key="keyfile", kwargs=kwargs) or Config().get()['database']['keyfile']
|
||||||
'keyfile_passphrase']
|
keyfile_passphrase = _kwargs_parser(key="keyfile_passphrase", kwargs=kwargs) or Config().get()['database'][
|
||||||
crlfile = _kwargs_parser(key="crlfile", kwargs=kwargs) or Config().get()['database']['crlfile']
|
'keyfile_passphrase']
|
||||||
max_tries = _kwargs_parser(key="max_tries", kwargs=kwargs)
|
crlfile = _kwargs_parser(key="crlfile", kwargs=kwargs) or Config().get()['database']['crlfile']
|
||||||
connection_timeout = _kwargs_parser(key="connection_timeout", kwargs=kwargs)
|
max_tries = _kwargs_parser(key="max_tries", kwargs=kwargs)
|
||||||
|
connection_timeout = _kwargs_parser(key="connection_timeout", kwargs=kwargs)
|
||||||
return Class(host=host, port=port, dbname=dbname,
|
|
||||||
max_tries=max_tries, connection_timeout=connection_timeout,
|
|
||||||
replicaset=replicaset, ssl=ssl, login=login, password=password,
|
|
||||||
ca_cert=ca_cert, certfile=certfile, keyfile=keyfile,
|
|
||||||
keyfile_passphrase=keyfile_passphrase, crlfile=crlfile)
|
|
||||||
|
|
||||||
|
return Class(host=host, port=port, dbname=dbname,
|
||||||
|
max_tries=max_tries, connection_timeout=connection_timeout,
|
||||||
|
replicaset=replicaset, ssl=ssl, login=login, password=password,
|
||||||
|
ca_cert=ca_cert, certfile=certfile, keyfile=keyfile,
|
||||||
|
keyfile_passphrase=keyfile_passphrase, crlfile=crlfile)
|
||||||
|
except:
|
||||||
|
logger.info('Exception in _connect(): {}')
|
||||||
|
raise ConfigurationError
|
||||||
|
|
||||||
def _kwargs_parser(key, kwargs):
|
def _kwargs_parser(key, kwargs):
|
||||||
if kwargs.get(key):
|
if kwargs.get(key):
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import logging
|
|||||||
import tarantool
|
import tarantool
|
||||||
|
|
||||||
from planetmint.config import Config
|
from planetmint.config import Config
|
||||||
|
from planetmint.common.exceptions import ConfigurationError
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -14,22 +15,29 @@ logger = logging.getLogger(__name__)
|
|||||||
class TarantoolDB:
|
class TarantoolDB:
|
||||||
def __init__(self, host: str = "localhost", port: int = 3303, user: str = None, password: str = None,
|
def __init__(self, host: str = "localhost", port: int = 3303, user: str = None, password: str = None,
|
||||||
reset_database: bool = False):
|
reset_database: bool = False):
|
||||||
self.host = host
|
try:
|
||||||
self.port = port
|
self.host = host
|
||||||
# TODO add user support later on
|
self.port = port
|
||||||
print(f"host : {host}")
|
# TODO add user support later on
|
||||||
print(f"port : {port}")
|
print(f"host : {host}")
|
||||||
# self.db_connect = tarantool.connect(host=host, port=port, user=user, password=password)
|
print(f"port : {port}")
|
||||||
self.db_connect = tarantool.connect(host=self.host, port=self.port)
|
# self.db_connect = tarantool.connect(host=host, port=port, user=user, password=password)
|
||||||
self.init_path = Config().get()["database"]["init_config"]["absolute_path"]
|
#TODO : raise configuraiton error if the connection cannot be established
|
||||||
self.drop_path = Config().get()["database"]["drop_config"]["absolute_path"]
|
self.db_connect = tarantool.connect(host=self.host, port=self.port)
|
||||||
if reset_database:
|
print( f"connection : {self.db_connect}")
|
||||||
self.drop_database()
|
self.init_path = Config().get()["database"]["init_config"]["absolute_path"]
|
||||||
self.init_database()
|
self.drop_path = Config().get()["database"]["drop_config"]["absolute_path"]
|
||||||
self._reconnect()
|
if reset_database:
|
||||||
self.SPACE_NAMES = ["abci_chains", "assets", "blocks", "blocks_tx",
|
self.drop_database()
|
||||||
"elections", "meta_data", "pre_commits", "validators",
|
self.init_database()
|
||||||
"transactions", "inputs", "outputs", "keys"]
|
self._reconnect()
|
||||||
|
self.SPACE_NAMES = ["abci_chains", "assets", "blocks", "blocks_tx",
|
||||||
|
"elections", "meta_data", "pre_commits", "validators",
|
||||||
|
"transactions", "inputs", "outputs", "keys"]
|
||||||
|
except:
|
||||||
|
logger.info('Exception in _connect(): {}')
|
||||||
|
raise ConfigurationError
|
||||||
|
|
||||||
|
|
||||||
def _reconnect(self):
|
def _reconnect(self):
|
||||||
self.db_connect = tarantool.connect(host=self.host, port=self.port)
|
self.db_connect = tarantool.connect(host=self.host, port=self.port)
|
||||||
|
|||||||
@ -113,16 +113,23 @@ def _configure_planetmint(request):
|
|||||||
|
|
||||||
@pytest.fixture(scope='session')
|
@pytest.fixture(scope='session')
|
||||||
def _setup_database(_configure_planetmint): # TODO Here is located setup database
|
def _setup_database(_configure_planetmint): # TODO Here is located setup database
|
||||||
# from planetmint.backend.connection_tarantool import init_tarantool, drop_tarantool
|
from planetmint.backend.tarantool.connection import TarantoolDB
|
||||||
# print('Initializing test db')
|
|
||||||
# init_tarantool()
|
|
||||||
# print('Finishing init database')
|
print('Deleting `{}` database')
|
||||||
|
db_conn = TarantoolDB("localhost", 3303)
|
||||||
|
db_conn.drop_database()
|
||||||
|
db_conn.init_database()
|
||||||
|
print('Finished deleting ``')
|
||||||
|
|
||||||
yield
|
yield
|
||||||
|
|
||||||
# print('Deleting `{}` database')
|
|
||||||
# drop_tarantool()
|
print('Initializing test db')
|
||||||
# print('Finished deleting ``')
|
db_conn2 = TarantoolDB("localhost", 3303)
|
||||||
|
db_conn2.drop_database()
|
||||||
|
print('Finishing init database')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
@ -130,8 +137,7 @@ def _bdb(_setup_database ):
|
|||||||
from planetmint.backend import Connection
|
from planetmint.backend import Connection
|
||||||
from planetmint.common.memoize import to_dict, from_dict
|
from planetmint.common.memoize import to_dict, from_dict
|
||||||
from planetmint.models import Transaction
|
from planetmint.models import Transaction
|
||||||
#conn = Connection( backend='tarantool_db', port=3301, host='localhost', login='guest')
|
conn = Connection()
|
||||||
#conn = Connection()
|
|
||||||
yield
|
yield
|
||||||
|
|
||||||
to_dict.cache_clear()
|
to_dict.cache_clear()
|
||||||
@ -510,14 +516,14 @@ def unspent_outputs(unspent_output_0, unspent_output_1, unspent_output_2):
|
|||||||
return unspent_output_0, unspent_output_1, unspent_output_2
|
return unspent_output_0, unspent_output_1, unspent_output_2
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
#@pytest.fixture
|
||||||
def mongo_client(db_context): # TODO Here add TarantoolConnectionClass
|
#def mongo_client(db_context): # TODO Here add TarantoolConnectionClass
|
||||||
return None # MongoClient(host=db_context.host, port=db_context.port)
|
# return None # MongoClient(host=db_context.host, port=db_context.port)
|
||||||
|
#
|
||||||
|
#
|
||||||
@pytest.fixture
|
#@pytest.fixture
|
||||||
def utxo_collection(db_context, mongo_client):
|
#def utxo_collection(db_context, mongo_client):
|
||||||
return mongo_client[db_context.name].utxos
|
# return mongo_client[db_context.name].utxos
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
@ -529,12 +535,12 @@ def dummy_unspent_outputs():
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
#@pytest.fixture
|
||||||
def utxoset(dummy_unspent_outputs, utxo_collection):
|
#def utxoset(dummy_unspent_outputs, utxo_collection):
|
||||||
res = utxo_collection.insert_many(copy.deepcopy(dummy_unspent_outputs))
|
# res = utxo_collection.insert_many(copy.deepcopy(dummy_unspent_outputs))
|
||||||
assert res.acknowledged
|
# assert res.acknowledged
|
||||||
assert len(res.inserted_ids) == 3
|
# assert len(res.inserted_ids) == 3
|
||||||
return dummy_unspent_outputs, utxo_collection
|
# return dummy_unspent_outputs, utxo_collection
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user