diff --git a/src/risotto/controller.py b/src/risotto/controller.py index 8c2d653..8a8a1be 100644 --- a/src/risotto/controller.py +++ b/src/risotto/controller.py @@ -5,6 +5,10 @@ from .context import Context class Controller: """Common controller used to add a service in Risotto """ + def __init__(self, + test: bool): + pass + async def call(self, uri: str, risotto_context: Context, diff --git a/src/risotto/register.py b/src/risotto/register.py index 582136d..4290691 100644 --- a/src/risotto/register.py +++ b/src/risotto/register.py @@ -218,11 +218,11 @@ class RegisterDispatcher: dico['notification'] = notification self.messages[version][message]['functions'].append(dico) - def set_module(self, module_name, module): + def set_module(self, module_name, module, test): """ register and instanciate a new module """ try: - self.injected_self[module_name] = module.Risotto() + self.injected_self[module_name] = module.Risotto(test) except AttributeError as err: raise RegistrationError(_(f'unable to register the module {module_name}, this module must have Risotto class')) diff --git a/src/risotto/services/__init__.py b/src/risotto/services/__init__.py index a9be627..e83d35b 100644 --- a/src/risotto/services/__init__.py +++ b/src/risotto/services/__init__.py @@ -5,7 +5,8 @@ from ..dispatcher import dispatcher def load_services(modules=None, - validate: bool=True): + validate: bool=True, + test: bool=False): abs_here = dirname(abspath(__file__)) here = basename(abs_here) module = basename(dirname(abs_here)) @@ -14,6 +15,6 @@ def load_services(modules=None, for filename in modules: absfilename = join(abs_here, filename) if isdir(absfilename) and isfile(join(absfilename, '__init__.py')): - dispatcher.set_module(filename, import_module(f'.{here}.{filename}', module)) + dispatcher.set_module(filename, import_module(f'.{here}.{filename}', module), test) if validate: dispatcher.validate() diff --git a/src/risotto/services/config/config.py b/src/risotto/services/config/config.py index 3033396..4b99afb 100644 --- a/src/risotto/services/config/config.py +++ b/src/risotto/services/config/config.py @@ -19,15 +19,18 @@ from ...logger import log class Risotto(Controller): - def __init__(self) -> None: + def __init__(self, + test) -> None: + global conf_storage self.cache_root_path = join(get_config().get('cache').get('root_path'), 'servermodel') for dirname in [self.cache_root_path, DATABASE_DIR]: if not isdir(dirname): raise RegistrationError(_(f'unable to find the cache dir "{dirname}"')) - self.save_storage = Storage(engine='sqlite3', dir_database=DATABASE_DIR) + if not test: + self.save_storage = Storage(engine='sqlite3', dir_database=DATABASE_DIR) self.servermodel = {} self.server = {} - super().__init__() + super().__init__(test) async def on_join(self, risotto_context: Context) -> None: @@ -80,28 +83,7 @@ class Risotto(Controller): None, f'Load servermodel {servermodel_name} ({servermodel_id})') - # use file in cache if found, otherwise retrieve it in servermodel context -# if isfile(cache_file): -# fileio = open(cache_file) -# else: -# servermodel = await self.call('v1.servermodel.describe', -# risotto_context, -# servermodel_id=servermodel_id, -# inheritance=False, -# resolvdepends=False, -# schema=True, -# creolefuncs=True) -# fileio = BytesIO() -# fileio.write(servermodel['schema'].encode()) -# fileio.seek(0) -# -# with open(cache_file, 'w') as cache: -# cache.write(servermodel['schema']) -# with open(funcs_file, 'w') as cache: -# cache.write(servermodel['creolefuncs']) -# del servermodel -# -# # loads tiramisu config and store it + # use file in cache with open(cache_file) as fileio: xmlroot = parse(fileio).getroot() self.servermodel[servermodel_id] = await self.build_metaconfig(servermodel_id, @@ -289,7 +271,7 @@ class Risotto(Controller): for server_type in ['server', 'server_to_deploy']: config = self.server[server_id]['server'] for parent in await config.config.parents(): - await parent.config.pop(config.config.name()) + await parent.config.pop(await config.config.name()) delete_session(storage=self.save_storage, session_id=await config.config.name()) # delete metaconfig @@ -341,46 +323,46 @@ class Risotto(Controller): delete_session(storage=self.save_storage, session_id=await metaconfig.config.name()) return children - - @register('v1.servermodel.updated') - async def servermodel_updated(self, - risotto_context: Context, - servermodel_id: int, - servermodel_name: str, - servermodel_parents_id: List[int]) -> None: - log.info_msg(risotto_context, - None, - f'Reload servermodel {servermodel_name} ({servermodel_id})') - # unlink cache to force download new aggregated file - cache_file = join(self.cache_root_path, str(servermodel_id)+".xml") - if isfile(cache_file): - unlink(cache_file) - - # store all informations - if servermodel_id in self.servermodel: - old_values = await self.servermodel[servermodel_id].value.exportation() - old_permissives = await self.servermodel[servermodel_id].permissive.exportation() - old_properties = await self.servermodel[servermodel_id].property.exportation() - children = await self.servermodel_delete(servermodel_id) - else: - old_values = None - - # create new one - await self.load_and_link_servermodel(risotto_context, - servermodel_id, - servermodel_name, - servermodel_parents_id) - - # migrates informations - if old_values is not None: - await self.servermodel[servermodel_id].value.importation(old_values) - await self.servermodel[servermodel_id].permissive.importation(old_permissives) - await self.servermodel[servermodel_id].property.importation(old_properties) - for child in children: - await self.servermodel_legacy(risotto_context, - await child.information.get('servermodel_name'), - await child.information.get('servermodel_id'), - servermodel_id) +# +# @register('v1.servermodel.updated') +# async def servermodel_updated(self, +# risotto_context: Context, +# servermodel_id: int, +# servermodel_name: str, +# servermodel_parents_id: List[int]) -> None: +# log.info_msg(risotto_context, +# None, +# f'Reload servermodel {servermodel_name} ({servermodel_id})') +# # unlink cache to force download new aggregated file +# cache_file = join(self.cache_root_path, str(servermodel_id)+".xml") +# if isfile(cache_file): +# unlink(cache_file) +# +# # store all informations +# if servermodel_id in self.servermodel: +# old_values = await self.servermodel[servermodel_id].value.exportation() +# old_permissives = await self.servermodel[servermodel_id].permissive.exportation() +# old_properties = await self.servermodel[servermodel_id].property.exportation() +# children = await self.servermodel_delete(servermodel_id) +# else: +# old_values = None +# +# # create new one +# await self.load_and_link_servermodel(risotto_context, +# servermodel_id, +# servermodel_name, +# servermodel_parents_id) +# +# # migrates informations +# if old_values is not None: +# await self.servermodel[servermodel_id].value.importation(old_values) +# await self.servermodel[servermodel_id].permissive.importation(old_permissives) +# await self.servermodel[servermodel_id].property.importation(old_properties) +# for child in children: +# await self.servermodel_legacy(risotto_context, +# await child.information.get('servermodel_name'), +# await child.information.get('servermodel_id'), +# servermodel_id) @register('v1.config.configuration.server.get', None) async def get_configuration(self, diff --git a/src/risotto/services/server/server.py b/src/risotto/services/server/server.py index 1fa2c50..37caf07 100644 --- a/src/risotto/services/server/server.py +++ b/src/risotto/services/server/server.py @@ -41,9 +41,9 @@ class Risotto(Controller): 'server_servermodel_id': server_servermodel_id} @register('v1.server.describe', None, database=True) - async def server_create(self, - risotto_context: Context, - server_name: str) -> Dict: + async def server_describe(self, + risotto_context: Context, + server_name: str) -> Dict: sql = ''' SELECT ServerId as server_id, ServerName as server_name, ServerDescription as server_description, ServerServermodelId as server_servermodel_id FROM Server diff --git a/src/risotto/services/session/session.py b/src/risotto/services/session/session.py index 8233fc4..5eaebc8 100644 --- a/src/risotto/services/session/session.py +++ b/src/risotto/services/session/session.py @@ -16,7 +16,8 @@ from ...dispatcher import dispatcher class Risotto(Controller): - def __init__(self): + def __init__(self, + test): self.modify_storage = Storage(engine='dictionary') def get_storage(self, @@ -221,7 +222,7 @@ class Risotto(Controller): type) info = self.format_session(session_id, session) if name is not None: - content = {name: await session['option'].option(name).value.get()} + content = {name: await session['config'].option(name).value.get()} else: content = await session['option'].value.dict(fullpath=True, leader_to_list=True) diff --git a/src/risotto/services/session/storage.py b/src/risotto/services/session/storage.py index 9e2854c..984d62c 100644 --- a/src/risotto/services/session/storage.py +++ b/src/risotto/services/session/storage.py @@ -44,8 +44,8 @@ class Storage(object): break await config.property.read_write() # set the default owner - self.set_owner(config, - username) + await self.set_owner(config, + username) # store it self.sessions[session_id] = {'config': config, @@ -113,10 +113,10 @@ class StorageServer(Storage): server_id: int): return f'std_{server_id}' - def set_owner(self, - config: Config, - username: str): - config.owner.set(username) + async def set_owner(self, + config: Config, + username: str): + await config.owner.set(username) class StorageServermodel(Storage): @@ -124,10 +124,10 @@ class StorageServermodel(Storage): server_id: int): return f'v_{server_id}' - def set_owner(self, - config: Config, - username: str): - config.owner.set('servermodel_' + username) + async def set_owner(self, + config: Config, + username: str): + await config.owner.set('servermodel_' + username) storage_server = StorageServer() diff --git a/tests/data/1/dictionaries.xml b/tests/data/1/dictionaries.xml new file mode 100644 index 0000000..bd88237 --- /dev/null +++ b/tests/data/1/dictionaries.xml @@ -0,0 +1,50 @@ + + + + + + + + False + + + /etc/mailname + + + False + + + mailname + + + True + + + + basic + + + + + normal + + oui + non + mandatory + normal + non + + + normal + + + normal + + + normal + + + + + + diff --git a/tests/data/1/funcs.py b/tests/data/1/funcs.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/data/2/dictionaries.xml b/tests/data/2/dictionaries.xml new file mode 100644 index 0000000..bd88237 --- /dev/null +++ b/tests/data/2/dictionaries.xml @@ -0,0 +1,50 @@ + + + + + + + + False + + + /etc/mailname + + + False + + + mailname + + + True + + + + basic + + + + + normal + + oui + non + mandatory + normal + non + + + normal + + + normal + + + normal + + + + + + diff --git a/tests/data/2/funcs.py b/tests/data/2/funcs.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/data/3/dictionaries.xml b/tests/data/3/dictionaries.xml new file mode 100644 index 0000000..bd88237 --- /dev/null +++ b/tests/data/3/dictionaries.xml @@ -0,0 +1,50 @@ + + + + + + + + False + + + /etc/mailname + + + False + + + mailname + + + True + + + + basic + + + + + normal + + oui + non + mandatory + normal + non + + + normal + + + normal + + + normal + + + + + + diff --git a/tests/data/3/funcs.py b/tests/data/3/funcs.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/fake_services/server/server.py b/tests/fake_services/server/server.py index 9782c8d..8db1f9f 100644 --- a/tests/fake_services/server/server.py +++ b/tests/fake_services/server/server.py @@ -5,4 +5,4 @@ from risotto.register import register class Risotto(Controller): @register('v1.server.list', None) async def server_list(self): - return [{'server_id': 3, 'servername': 'one', 'serverdescription': 'the first', 'servermodelid': 1}] + return [{'server_id': 3, 'server_name': 'one', 'server_description': 'the first', 'server_servermodel_id': 1}] diff --git a/tests/fake_services/servermodel/servermodel.py b/tests/fake_services/servermodel/servermodel.py index a85415e..e183b69 100644 --- a/tests/fake_services/servermodel/servermodel.py +++ b/tests/fake_services/servermodel/servermodel.py @@ -13,63 +13,3 @@ class Risotto(Controller): 'release_id': 2, 'servermodel_description': 'description2', 'servermodel_parents_id': [1]}] - -# @register('v1.servermodel.describe', None) -# async def servermodel_describe(self, -# servermodel_id, -# inheritance, -# resolv_depends): -# schema = """ -# -# -# -# -# -# -# False -# -# -# /etc/mailname -# -# -# False -# -# -# mailname -# -# -# True -# -# -# -# basic -# -# -# -# -# normal -# -# oui -# non -# mandatory -# normal -# non -# -# -# normal -# -# -# normal -# -# -# normal -# -# -# -# -# -#""" -# return {'servermodel_id': 1, -# 'servermodel_name': 'name', -# 'servermodel_description': 'description', -# 'release_id': 1} diff --git a/tests/storage.py b/tests/storage.py new file mode 100644 index 0000000..8a47d7b --- /dev/null +++ b/tests/storage.py @@ -0,0 +1,5 @@ +from tiramisu import Storage +from risotto.config import DATABASE_DIR + + +STORAGE = Storage(engine='sqlite3', dir_database=DATABASE_DIR, name='test') diff --git a/tests/test_config.py b/tests/test_config.py index b57b257..d340bc9 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,20 +1,20 @@ from importlib import import_module import pytest -from tiramisu import Storage, list_sessions, delete_session +from tiramisu import list_sessions, delete_session +from .storage import STORAGE from risotto.context import Context from risotto.services import load_services from risotto.dispatcher import dispatcher -from risotto.config import DATABASE_DIR def setup_module(module): load_services(['config'], validate=False) config_module = dispatcher.get_service('config') - config_module.save_storage = Storage(engine='sqlite3', dir_database=DATABASE_DIR, name='test') - dispatcher.set_module('server', import_module(f'.server', 'fake_services')) - dispatcher.set_module('servermodel', import_module(f'.servermodel', 'fake_services')) + config_module.save_storage = STORAGE + dispatcher.set_module('server', import_module(f'.server', 'fake_services'), True) + dispatcher.set_module('servermodel', import_module(f'.servermodel', 'fake_services'), True) def setup_function(function): @@ -45,17 +45,19 @@ async def test_on_join(): assert config_module.server == {} # fake_context = get_fake_context('config') + config_module.cache_root_path = 'tests/data' await config_module.on_join(fake_context) assert list(config_module.servermodel.keys()) == [1, 2] assert list(config_module.server) == [3] assert set(config_module.server[3]) == {'server', 'server_to_deploy', 'funcs_file'} - assert config_module.server[3]['funcs_file'] == 'cache/1.creolefuncs' + assert config_module.server[3]['funcs_file'] == 'tests/data/1/funcs.py' @pytest.mark.asyncio async def test_server_created(): config_module = dispatcher.get_service('config') fake_context = get_fake_context('config') + config_module.cache_root_path = 'tests/data' await config_module.on_join(fake_context) # assert list(config_module.server) == [3] @@ -63,18 +65,19 @@ async def test_server_created(): 'server.created', fake_context, server_id=4, - servername='name3', - serverdescription='description3', - servermodelid=2) + server_name='name3', + server_description='description3', + server_servermodel_id=2) assert list(config_module.server) == [3, 4] assert set(config_module.server[4]) == {'server', 'server_to_deploy', 'funcs_file'} - assert config_module.server[4]['funcs_file'] == 'cache/2.creolefuncs' + assert config_module.server[4]['funcs_file'] == 'tests/data/2/funcs.py' @pytest.mark.asyncio async def test_server_deleted(): config_module = dispatcher.get_service('config') fake_context = get_fake_context('config') + config_module.cache_root_path = 'tests/data' await config_module.on_join(fake_context) # assert list(config_module.server) == [3] @@ -82,9 +85,9 @@ async def test_server_deleted(): 'server.created', fake_context, server_id=4, - servername='name4', - serverdescription='description4', - servermodelid=2) + server_name='name4', + server_description='description4', + server_servermodel_id=2) assert list(config_module.server) == [3, 4] await dispatcher.publish('v1', 'server.deleted', @@ -97,6 +100,7 @@ async def test_server_deleted(): async def test_servermodel_created(): config_module = dispatcher.get_service('config') fake_context = get_fake_context('config') + config_module.cache_root_path = 'tests/data' await config_module.on_join(fake_context) # assert list(config_module.servermodel) == [1, 2] @@ -110,13 +114,14 @@ async def test_servermodel_created(): release_id=1, servermodel_name='name3') assert list(config_module.servermodel) == [1, 2, 3] - assert not list(config_module.servermodel[3].config.parents()) + assert not list(await config_module.servermodel[3].config.parents()) @pytest.mark.asyncio async def test_servermodel_herited_created(): config_module = dispatcher.get_service('config') fake_context = get_fake_context('config') + config_module.cache_root_path = 'tests/data' await config_module.on_join(fake_context) # assert list(config_module.servermodel) == [1, 2] @@ -129,13 +134,14 @@ async def test_servermodel_herited_created(): servermodel_description='name3', servermodel_parents_id=[1]) assert list(config_module.servermodel) == [1, 2, 3] - assert len(list(config_module.servermodel[3].config.parents())) == 1 + assert len(list(await config_module.servermodel[3].config.parents())) == 1 @pytest.mark.asyncio async def test_servermodel_multi_herited_created(): config_module = dispatcher.get_service('config') fake_context = get_fake_context('config') + config_module.cache_root_path = 'tests/data' await config_module.on_join(fake_context) # assert list(config_module.servermodel) == [1, 2] @@ -148,136 +154,141 @@ async def test_servermodel_multi_herited_created(): servermodel_description='name3', servermodel_parents_id=[1, 2]) assert list(config_module.servermodel) == [1, 2, 3] - assert len(list(config_module.servermodel[3].config.parents())) == 2 + assert len(list(await config_module.servermodel[3].config.parents())) == 2 -@pytest.mark.asyncio -async def test_servermodel_updated_not_exists(): - config_module = dispatcher.get_service('config') - fake_context = get_fake_context('config') - await config_module.on_join(fake_context) - # - assert list(config_module.servermodel) == [1, 2] - await dispatcher.publish('v1', - 'servermodel.updated', - fake_context, - servermodel_id=3, - servermodel_name='name3', - release_id=1, - servermodel_description='name3', - servermodel_parents_id=[1, 2]) - assert list(config_module.servermodel) == [1, 2, 3] - assert len(list(config_module.servermodel[3].config.parents())) == 2 - - -@pytest.mark.asyncio -async def test_servermodel_updated1(): - config_module = dispatcher.get_service('config') - fake_context = get_fake_context('config') - await config_module.on_join(fake_context) - # - assert list(config_module.servermodel) == [1, 2] - metaconfig1 = config_module.servermodel[1] - metaconfig2 = config_module.servermodel[2] - mixconfig1 = next(metaconfig1.config.list()) - mixconfig2 = next(metaconfig2.config.list()) - assert len(list(metaconfig1.config.parents())) == 0 - assert len(list(metaconfig2.config.parents())) == 1 - assert len(list(mixconfig1.config.list())) == 1 - assert len(list(mixconfig2.config.list())) == 0 - # - await dispatcher.publish('v1', - 'servermodel.updated', - fake_context, - servermodel_id=1, - servermodel_name='name1-1', - release_id=1, - servermodel_description='name1-1') - assert set(config_module.servermodel) == {1, 2} - assert config_module.servermodel[1].information.get('servermodel_name') == 'name1-1' - assert metaconfig1 != config_module.servermodel[1] - assert metaconfig2 == config_module.servermodel[2] - metaconfig1 = config_module.servermodel[1] - assert mixconfig1 != next(metaconfig1.config.list()) - mixconfig1 = next(metaconfig1.config.list()) - # - assert len(list(metaconfig1.config.parents())) == 0 - assert len(list(metaconfig2.config.parents())) == 1 - assert len(list(mixconfig1.config.list())) == 1 - assert len(list(mixconfig2.config.list())) == 0 - - -@pytest.mark.asyncio -async def test_servermodel_updated2(): - config_module = dispatcher.get_service('config') - fake_context = get_fake_context('config') - await config_module.on_join(fake_context) - # create a new servermodel - assert list(config_module.servermodel) == [1, 2] - mixconfig1 = next(config_module.servermodel[1].config.list()) - mixconfig2 = next(config_module.servermodel[2].config.list()) - assert len(list(mixconfig1.config.list())) == 1 - assert len(list(mixconfig2.config.list())) == 0 - await dispatcher.publish('v1', - 'servermodel.created', - fake_context, - servermodel_id=3, - servermodel_name='name3', - release_id=1, - servermodel_description='name3', - servermodel_parents_id=[1]) - assert list(config_module.servermodel) == [1, 2, 3] - assert len(list(config_module.servermodel[3].config.parents())) == 1 - assert config_module.servermodel[3].information.get('servermodel_name') == 'name3' - assert len(list(mixconfig1.config.list())) == 2 - assert len(list(mixconfig2.config.list())) == 0 - # - await dispatcher.publish('v1', - 'servermodel.updated', - fake_context, - servermodel_id=3, - servermodel_name='name3-1', - release_id=1, - servermodel_description='name3-1', - servermodel_parents_id=[1, 2]) - assert list(config_module.servermodel) == [1, 2, 3] - assert config_module.servermodel[3].information.get('servermodel_name') == 'name3-1' - assert len(list(mixconfig1.config.list())) == 2 - assert len(list(mixconfig2.config.list())) == 1 - - -@pytest.mark.asyncio -async def test_servermodel_updated_config(): - config_module = dispatcher.get_service('config') - fake_context = get_fake_context('config') - await config_module.on_join(fake_context) - # - config_module.servermodel[1].property.read_write() - assert config_module.servermodel[1].option('creole.general.mode_conteneur_actif').value.get() == 'non' - config_module.servermodel[1].option('creole.general.mode_conteneur_actif').value.set('oui') - assert config_module.servermodel[1].option('creole.general.mode_conteneur_actif').value.get() == 'oui' - # - await dispatcher.publish('v1', - 'servermodel.updated', - fake_context, - servermodel_id=1, - servermodel_name='name1-1', - release_id=1, - servermodel_description='name1-1') - assert config_module.servermodel[1].option('creole.general.mode_conteneur_actif').value.get() == 'oui' +#@pytest.mark.asyncio +#async def test_servermodel_updated_not_exists(): +# config_module = dispatcher.get_service('config') +# fake_context = get_fake_context('config') +# config_module.cache_root_path = 'tests/data' +# await config_module.on_join(fake_context) +# # +# assert list(config_module.servermodel) == [1, 2] +# await dispatcher.publish('v1', +# 'servermodel.updated', +# fake_context, +# servermodel_id=3, +# servermodel_name='name3', +# release_id=1, +# servermodel_description='name3', +# servermodel_parents_id=[1, 2]) +# assert list(config_module.servermodel) == [1, 2, 3] +# assert len(list(await config_module.servermodel[3].config.parents())) == 2 +# +# +# @pytest.mark.asyncio +# async def test_servermodel_updated1(): +# config_module = dispatcher.get_service('config') +# fake_context = get_fake_context('config') +# config_module.cache_root_path = 'tests/data' +# await config_module.on_join(fake_context) +# # +# assert list(config_module.servermodel) == [1, 2] +# metaconfig1 = config_module.servermodel[1] +# metaconfig2 = config_module.servermodel[2] +# mixconfig1 = (await metaconfig1.config.list())[0] +# mixconfig2 = (await metaconfig2.config.list())[0] +# assert len(list(await metaconfig1.config.parents())) == 0 +# assert len(list(await metaconfig2.config.parents())) == 1 +# assert len(list(await mixconfig1.config.list())) == 1 +# assert len(list(await mixconfig2.config.list())) == 0 +# # +# await dispatcher.publish('v1', +# 'servermodel.updated', +# fake_context, +# servermodel_id=1, +# servermodel_name='name1-1', +# release_id=1, +# servermodel_description='name1-1') +# assert set(config_module.servermodel) == {1, 2} +# assert config_module.servermodel[1].information.get('servermodel_name') == 'name1-1' +# assert metaconfig1 != config_module.servermodel[1] +# assert metaconfig2 == config_module.servermodel[2] +# metaconfig1 = config_module.servermodel[1] +# assert mixconfig1 != next(metaconfig1.config.list()) +# mixconfig1 = next(metaconfig1.config.list()) +# # +# assert len(list(await metaconfig1.config.parents())) == 0 +# assert len(list(await metaconfig2.config.parents())) == 1 +# assert len(list(await mixconfig1.config.list())) == 1 +# assert len(list(await mixconfig2.config.list())) == 0 +# +# +# @pytest.mark.asyncio +# async def test_servermodel_updated2(): +# config_module = dispatcher.get_service('config') +# fake_context = get_fake_context('config') +# config_module.cache_root_path = 'tests/data' +# await config_module.on_join(fake_context) +# # create a new servermodel +# assert list(config_module.servermodel) == [1, 2] +# mixconfig1 = next(config_module.servermodel[1].config.list()) +# mixconfig2 = next(config_module.servermodel[2].config.list()) +# assert len(list(mixconfig1.config.list())) == 1 +# assert len(list(mixconfig2.config.list())) == 0 +# await dispatcher.publish('v1', +# 'servermodel.created', +# fake_context, +# servermodel_id=3, +# servermodel_name='name3', +# release_id=1, +# servermodel_description='name3', +# servermodel_parents_id=[1]) +# assert list(config_module.servermodel) == [1, 2, 3] +# assert len(list(await config_module.servermodel[3].config.parents())) == 1 +# assert await config_module.servermodel[3].information.get('servermodel_name') == 'name3' +# assert len(list(await mixconfig1.config.list())) == 2 +# assert len(list(await mixconfig2.config.list())) == 0 +# # +# await dispatcher.publish('v1', +# 'servermodel.updated', +# fake_context, +# servermodel_id=3, +# servermodel_name='name3-1', +# release_id=1, +# servermodel_description='name3-1', +# servermodel_parents_id=[1, 2]) +# assert list(config_module.servermodel) == [1, 2, 3] +# assert config_module.servermodel[3].information.get('servermodel_name') == 'name3-1' +# assert len(list(mixconfig1.config.list())) == 2 +# assert len(list(mixconfig2.config.list())) == 1 +# +# +# @pytest.mark.asyncio +# async def test_servermodel_updated_config(): +# config_module = dispatcher.get_service('config') +# fake_context = get_fake_context('config') +# config_module.cache_root_path = 'tests/data' +# await config_module.on_join(fake_context) +# # +# config_module.servermodel[1].property.read_write() +# assert config_module.servermodel[1].option('creole.general.mode_conteneur_actif').value.get() == 'non' +# config_module.servermodel[1].option('creole.general.mode_conteneur_actif').value.set('oui') +# assert config_module.servermodel[1].option('creole.general.mode_conteneur_actif').value.get() == 'oui' +# # +# await dispatcher.publish('v1', +# 'servermodel.updated', +# fake_context, +# servermodel_id=1, +# servermodel_name='name1-1', +# release_id=1, +# servermodel_description='name1-1') +# assert config_module.servermodel[1].option('creole.general.mode_conteneur_actif').value.get() == 'oui' @pytest.mark.asyncio async def test_server_configuration_get(): config_module = dispatcher.get_service('config') fake_context = get_fake_context('config') + config_module.cache_root_path = 'tests/data' await config_module.on_join(fake_context) # - config_module.server[3]['server_to_deploy'].property.read_write() - assert config_module.server[3]['server_to_deploy'].option('creole.general.mode_conteneur_actif').value.get() == 'non' - config_module.server[3]['server_to_deploy'].option('creole.general.mode_conteneur_actif').value.set('oui') - assert config_module.server[3]['server_to_deploy'].option('creole.general.mode_conteneur_actif').value.get() == 'oui' - assert config_module.server[3]['server'].option('creole.general.mode_conteneur_actif').value.get() == 'non' + await config_module.server[3]['server_to_deploy'].property.read_write() + assert await config_module.server[3]['server_to_deploy'].option('creole.general.mode_conteneur_actif').value.get() == 'non' + await config_module.server[3]['server_to_deploy'].option('creole.general.mode_conteneur_actif').value.set('oui') + assert await config_module.server[3]['server_to_deploy'].option('creole.general.mode_conteneur_actif').value.get() == 'oui' + assert await config_module.server[3]['server'].option('creole.general.mode_conteneur_actif').value.get() == 'non' # values = await dispatcher.call('v1', 'config.configuration.server.get', @@ -311,16 +322,17 @@ async def test_server_configuration_get(): async def test_config_deployed(): config_module = dispatcher.get_service('config') fake_context = get_fake_context('config') + config_module.cache_root_path = 'tests/data' await config_module.on_join(fake_context) # - config_module.server[3]['server_to_deploy'].property.read_write() - assert config_module.server[3]['server_to_deploy'].option('creole.general.mode_conteneur_actif').value.get() == 'non' - config_module.server[3]['server_to_deploy'].option('creole.general.mode_conteneur_actif').value.set('oui') - assert config_module.server[3]['server_to_deploy'].option('creole.general.mode_conteneur_actif').value.get() == 'oui' - assert config_module.server[3]['server'].option('creole.general.mode_conteneur_actif').value.get() == 'non' + await config_module.server[3]['server_to_deploy'].property.read_write() + assert await config_module.server[3]['server_to_deploy'].option('creole.general.mode_conteneur_actif').value.get() == 'non' + await config_module.server[3]['server_to_deploy'].option('creole.general.mode_conteneur_actif').value.set('oui') + assert await config_module.server[3]['server_to_deploy'].option('creole.general.mode_conteneur_actif').value.get() == 'oui' + assert await config_module.server[3]['server'].option('creole.general.mode_conteneur_actif').value.get() == 'non' values = await dispatcher.publish('v1', 'config.configuration.server.deploy', fake_context, server_id=3) - assert config_module.server[3]['server_to_deploy'].option('creole.general.mode_conteneur_actif').value.get() == 'oui' - assert config_module.server[3]['server'].option('creole.general.mode_conteneur_actif').value.get() == 'oui' + assert await config_module.server[3]['server_to_deploy'].option('creole.general.mode_conteneur_actif').value.get() == 'oui' + assert await config_module.server[3]['server'].option('creole.general.mode_conteneur_actif').value.get() == 'oui' diff --git a/tests/test_session.py b/tests/test_session.py index 64c403a..e3663ea 100644 --- a/tests/test_session.py +++ b/tests/test_session.py @@ -1,10 +1,9 @@ from importlib import import_module import pytest -from tiramisu import Storage +from .storage import STORAGE from risotto.context import Context from risotto.services import load_services from risotto.dispatcher import dispatcher -from risotto.config import DATABASE_DIR from risotto.services.session.storage import storage_server, storage_servermodel @@ -18,11 +17,13 @@ def get_fake_context(module_name): def setup_module(module): load_services(['config', 'session'], - validate=False) + validate=False, + test=True) config_module = dispatcher.get_service('config') - config_module.save_storage = Storage(engine='sqlite3', dir_database=DATABASE_DIR, name='test') - dispatcher.set_module('server', import_module(f'.server', 'fake_services')) - dispatcher.set_module('servermodel', import_module(f'.servermodel', 'fake_services')) + config_module.save_storage = STORAGE + dispatcher.set_module('server', import_module(f'.server', 'fake_services'), True) + dispatcher.set_module('servermodel', import_module(f'.servermodel', 'fake_services'), True) + dispatcher.injected_self['servermodel'].cache_root_path = 'tests/data' def teardown_function(function): @@ -35,9 +36,10 @@ def teardown_function(function): async def test_server_start(): fake_context = get_fake_context('session') config_module = dispatcher.get_service('config') + config_module.cache_root_path = 'tests/data' if not config_module.server: await config_module.on_join(fake_context) - await dispatcher.call('v1', + await dispatcher.call('v1', 'session.server.start', fake_context, id=3) @@ -47,16 +49,17 @@ async def test_server_start(): async def test_server_list(): fake_context = get_fake_context('session') config_module = dispatcher.get_service('config') + config_module.cache_root_path = 'tests/data' if not config_module.server: await config_module.on_join(fake_context) - assert not await dispatcher.call('v1', + assert not await dispatcher.call('v1', 'session.server.list', fake_context) - await dispatcher.call('v1', + await dispatcher.call('v1', 'session.server.start', fake_context, id=3) - assert await dispatcher.call('v1', + assert await dispatcher.call('v1', 'session.server.list', fake_context) @@ -65,9 +68,10 @@ async def test_server_list(): async def test_server_filter_namespace(): fake_context = get_fake_context('session') config_module = dispatcher.get_service('config') + config_module.cache_root_path = 'tests/data' if not config_module.server: await config_module.on_join(fake_context) - session = await dispatcher.call('v1', + session = await dispatcher.call('v1', 'session.server.start', fake_context, id=3) @@ -78,7 +82,7 @@ async def test_server_filter_namespace(): fake_context, session_id=session_id, namespace=namespace) - list_result = await dispatcher.call('v1', + list_result = await dispatcher.call('v1', 'session.server.list', fake_context) assert list_result[0]['namespace'] == namespace @@ -88,9 +92,10 @@ async def test_server_filter_namespace(): async def test_server_filter_mode(): fake_context = get_fake_context('session') config_module = dispatcher.get_service('config') + config_module.cache_root_path = 'tests/data' if not config_module.server: await config_module.on_join(fake_context) - session = await dispatcher.call('v1', + session = await dispatcher.call('v1', 'session.server.start', fake_context, id=3) @@ -102,7 +107,7 @@ async def test_server_filter_mode(): fake_context, session_id=session_id, mode=mode) - list_result = await dispatcher.call('v1', + list_result = await dispatcher.call('v1', 'session.server.list', fake_context) assert list_result[0]['mode'] == mode @@ -112,9 +117,10 @@ async def test_server_filter_mode(): async def test_server_filter_debug(): fake_context = get_fake_context('session') config_module = dispatcher.get_service('config') + config_module.cache_root_path = 'tests/data' if not config_module.server: await config_module.on_join(fake_context) - session = await dispatcher.call('v1', + session = await dispatcher.call('v1', 'session.server.start', fake_context, id=3) @@ -126,7 +132,7 @@ async def test_server_filter_debug(): fake_context, session_id=session_id, debug=debug) - list_result = await dispatcher.call('v1', + list_result = await dispatcher.call('v1', 'session.server.list', fake_context) assert list_result[0]['debug'] == debug @@ -137,9 +143,10 @@ async def test_server_filter_debug(): async def test_server_filter_get(): fake_context = get_fake_context('session') config_module = dispatcher.get_service('config') + config_module.cache_root_path = 'tests/data' if not config_module.server: await config_module.on_join(fake_context) - session = await dispatcher.call('v1', + session = await dispatcher.call('v1', 'session.server.start', fake_context, id=3) @@ -148,10 +155,8 @@ async def test_server_filter_get(): 'session.server.get', fake_context, session_id=session_id) - assert values == {'content': {"general.mode_conteneur_actif": "non", - "general.master.master": [], - "general.master.slave1": [], - "general.master.slave2": []}, + assert values == {'content': {"creole.general.mode_conteneur_actif": "non", + "creole.general.master.master": []}, 'debug': False, 'id': 3, 'mode': 'normal', @@ -165,9 +170,10 @@ async def test_server_filter_get(): async def test_server_filter_get_one_value(): fake_context = get_fake_context('session') config_module = dispatcher.get_service('config') + config_module.cache_root_path = 'tests/data' if not config_module.server: await config_module.on_join(fake_context) - session = await dispatcher.call('v1', + session = await dispatcher.call('v1', 'session.server.start', fake_context, id=3) @@ -176,8 +182,8 @@ async def test_server_filter_get_one_value(): 'session.server.get', fake_context, session_id=session_id, - name="general.mode_conteneur_actif") - assert values == {'content': {"general.mode_conteneur_actif": "non"}, + name="creole.general.mode_conteneur_actif") + assert values == {'content': {"creole.general.mode_conteneur_actif": "non"}, 'debug': False, 'id': 3, 'mode': 'normal', @@ -191,9 +197,10 @@ async def test_server_filter_get_one_value(): async def test_server_filter_configure(): fake_context = get_fake_context('session') config_module = dispatcher.get_service('config') + config_module.cache_root_path = 'tests/data' if not config_module.server: await config_module.on_join(fake_context) - session = await dispatcher.call('v1', + session = await dispatcher.call('v1', 'session.server.start', fake_context, id=3) @@ -203,17 +210,17 @@ async def test_server_filter_configure(): fake_context, session_id=session_id, action='modify', - name='general.mode_conteneur_actif', + name='creole.general.mode_conteneur_actif', value='oui') - list_result = await dispatcher.call('v1', + list_result = await dispatcher.call('v1', 'session.server.list', fake_context) values = await dispatcher.call('v1', 'session.server.get', fake_context, session_id=session_id, - name="general.mode_conteneur_actif") - assert values == {'content': {"general.mode_conteneur_actif": "oui"}, + name="creole.general.mode_conteneur_actif") + assert values == {'content': {"creole.general.mode_conteneur_actif": "oui"}, 'debug': False, 'id': 3, 'mode': 'normal', @@ -227,9 +234,10 @@ async def test_server_filter_configure(): async def test_server_filter_validate(): fake_context = get_fake_context('session') config_module = dispatcher.get_service('config') + config_module.cache_root_path = 'tests/data' if not config_module.server: await config_module.on_join(fake_context) - session = await dispatcher.call('v1', + session = await dispatcher.call('v1', 'session.server.start', fake_context, id=3) @@ -244,24 +252,25 @@ async def test_server_filter_validate(): async def test_server_stop(): fake_context = get_fake_context('session') config_module = dispatcher.get_service('config') + config_module.cache_root_path = 'tests/data' if not config_module.server: await config_module.on_join(fake_context) - assert not await dispatcher.call('v1', + assert not await dispatcher.call('v1', 'session.server.list', fake_context) - start = await dispatcher.call('v1', + start = await dispatcher.call('v1', 'session.server.start', fake_context, id=3) session_id = start['session_id'] - assert await dispatcher.call('v1', + assert await dispatcher.call('v1', 'session.server.list', fake_context) - await dispatcher.call('v1', + await dispatcher.call('v1', 'session.server.stop', fake_context, session_id=session_id) - assert not await dispatcher.call('v1', + assert not await dispatcher.call('v1', 'session.server.list', fake_context) @@ -271,9 +280,10 @@ async def test_server_stop(): async def test_servermodel_start(): fake_context = get_fake_context('session') config_module = dispatcher.get_service('config') + config_module.cache_root_path = 'tests/data' if not config_module.servermodel: await config_module.on_join(fake_context) - await dispatcher.call('v1', + await dispatcher.call('v1', 'session.servermodel.start', fake_context, id=1) @@ -283,16 +293,17 @@ async def test_servermodel_start(): async def test_servermodel_list(): fake_context = get_fake_context('session') config_module = dispatcher.get_service('config') + config_module.cache_root_path = 'tests/data' if not config_module.servermodel: await config_module.on_join(fake_context) - assert not await dispatcher.call('v1', + assert not await dispatcher.call('v1', 'session.servermodel.list', fake_context) - await dispatcher.call('v1', + await dispatcher.call('v1', 'session.servermodel.start', fake_context, id=1) - assert await dispatcher.call('v1', + assert await dispatcher.call('v1', 'session.servermodel.list', fake_context) @@ -301,9 +312,10 @@ async def test_servermodel_list(): async def test_servermodel_filter_namespace(): fake_context = get_fake_context('session') config_module = dispatcher.get_service('config') + config_module.cache_root_path = 'tests/data' if not config_module.servermodel: await config_module.on_join(fake_context) - session = await dispatcher.call('v1', + session = await dispatcher.call('v1', 'session.servermodel.start', fake_context, id=1) @@ -314,7 +326,7 @@ async def test_servermodel_filter_namespace(): fake_context, session_id=session_id, namespace=namespace) - list_result = await dispatcher.call('v1', + list_result = await dispatcher.call('v1', 'session.servermodel.list', fake_context) assert list_result[0]['namespace'] == namespace @@ -324,9 +336,10 @@ async def test_servermodel_filter_namespace(): async def test_servermodel_filter_mode(): fake_context = get_fake_context('session') config_module = dispatcher.get_service('config') + config_module.cache_root_path = 'tests/data' if not config_module.servermodel: await config_module.on_join(fake_context) - session = await dispatcher.call('v1', + session = await dispatcher.call('v1', 'session.servermodel.start', fake_context, id=1) @@ -338,7 +351,7 @@ async def test_servermodel_filter_mode(): fake_context, session_id=session_id, mode=mode) - list_result = await dispatcher.call('v1', + list_result = await dispatcher.call('v1', 'session.servermodel.list', fake_context) assert list_result[0]['mode'] == mode @@ -348,9 +361,10 @@ async def test_servermodel_filter_mode(): async def test_servermodel_filter_debug(): fake_context = get_fake_context('session') config_module = dispatcher.get_service('config') + config_module.cache_root_path = 'tests/data' if not config_module.servermodel: await config_module.on_join(fake_context) - session = await dispatcher.call('v1', + session = await dispatcher.call('v1', 'session.servermodel.start', fake_context, id=1) @@ -362,7 +376,7 @@ async def test_servermodel_filter_debug(): fake_context, session_id=session_id, debug=debug) - list_result = await dispatcher.call('v1', + list_result = await dispatcher.call('v1', 'session.servermodel.list', fake_context) assert list_result[0]['debug'] == debug @@ -372,9 +386,10 @@ async def test_servermodel_filter_debug(): async def test_servermodel_filter_get(): fake_context = get_fake_context('session') config_module = dispatcher.get_service('config') + config_module.cache_root_path = 'tests/data' if not config_module.servermodel: await config_module.on_join(fake_context) - session = await dispatcher.call('v1', + session = await dispatcher.call('v1', 'session.servermodel.start', fake_context, id=1) @@ -383,10 +398,8 @@ async def test_servermodel_filter_get(): 'session.servermodel.get', fake_context, session_id=session_id) - assert values == {'content': {"general.mode_conteneur_actif": "non", - "general.master.master": [], - "general.master.slave1": [], - "general.master.slave2": []}, + assert values == {'content': {"creole.general.mode_conteneur_actif": "non", + "creole.general.master.master": []}, 'debug': False, 'id': 1, 'mode': 'normal', @@ -400,9 +413,10 @@ async def test_servermodel_filter_get(): async def test_servermodel_filter_get_one_value(): fake_context = get_fake_context('session') config_module = dispatcher.get_service('config') + config_module.cache_root_path = 'tests/data' if not config_module.servermodel: await config_module.on_join(fake_context) - session = await dispatcher.call('v1', + session = await dispatcher.call('v1', 'session.servermodel.start', fake_context, id=1) @@ -411,8 +425,8 @@ async def test_servermodel_filter_get_one_value(): 'session.servermodel.get', fake_context, session_id=session_id, - name="general.mode_conteneur_actif") - assert values == {'content': {"general.mode_conteneur_actif": "non"}, + name="creole.general.mode_conteneur_actif") + assert values == {'content': {"creole.general.mode_conteneur_actif": "non"}, 'debug': False, 'id': 1, 'mode': 'normal', @@ -426,9 +440,10 @@ async def test_servermodel_filter_get_one_value(): async def test_servermodel_filter_configure(): fake_context = get_fake_context('session') config_module = dispatcher.get_service('config') + config_module.cache_root_path = 'tests/data' if not config_module.servermodel: await config_module.on_join(fake_context) - session = await dispatcher.call('v1', + session = await dispatcher.call('v1', 'session.servermodel.start', fake_context, id=1) @@ -438,17 +453,17 @@ async def test_servermodel_filter_configure(): fake_context, session_id=session_id, action='modify', - name='general.mode_conteneur_actif', + name='creole.general.mode_conteneur_actif', value='oui') - list_result = await dispatcher.call('v1', + list_result = await dispatcher.call('v1', 'session.servermodel.list', fake_context) values = await dispatcher.call('v1', 'session.servermodel.get', fake_context, session_id=session_id, - name="general.mode_conteneur_actif") - assert values == {'content': {"general.mode_conteneur_actif": "oui"}, + name="creole.general.mode_conteneur_actif") + assert values == {'content': {"creole.general.mode_conteneur_actif": "oui"}, 'debug': False, 'id': 1, 'mode': 'normal', @@ -462,9 +477,10 @@ async def test_servermodel_filter_configure(): async def test_servermodel_filter_validate(): fake_context = get_fake_context('session') config_module = dispatcher.get_service('config') + config_module.cache_root_path = 'tests/data' if not config_module.servermodel: await config_module.on_join(fake_context) - session = await dispatcher.call('v1', + session = await dispatcher.call('v1', 'session.servermodel.start', fake_context, id=1) @@ -479,23 +495,24 @@ async def test_servermodel_filter_validate(): async def test_servermodel_stop(): fake_context = get_fake_context('session') config_module = dispatcher.get_service('config') + config_module.cache_root_path = 'tests/data' if not config_module.servermodel: await config_module.on_join(fake_context) - assert not await dispatcher.call('v1', + assert not await dispatcher.call('v1', 'session.servermodel.list', fake_context) - start = await dispatcher.call('v1', + start = await dispatcher.call('v1', 'session.servermodel.start', fake_context, id=1) session_id = start['session_id'] - assert await dispatcher.call('v1', + assert await dispatcher.call('v1', 'session.servermodel.list', fake_context) - await dispatcher.call('v1', + await dispatcher.call('v1', 'session.servermodel.stop', fake_context, session_id=session_id) - assert not await dispatcher.call('v1', + assert not await dispatcher.call('v1', 'session.servermodel.list', fake_context)