change for tests

This commit is contained in:
Emmanuel Garette 2020-08-19 11:36:18 +02:00
parent e787eb2ef5
commit a64131fb03
6 changed files with 398 additions and 282 deletions

View File

@ -42,7 +42,7 @@ class CallDispatcher:
raise Exception('hu?') raise Exception('hu?')
else: else:
for ret in returns: for ret in returns:
async with await Config(response) as config: async with await Config(response, display_name=lambda self, dyn_name, suffix: self.impl_getname()) as config:
await config.property.read_write() await config.property.read_write()
try: try:
for key, value in ret.items(): for key, value in ret.items():

View File

@ -20,9 +20,6 @@ from . import services
extra_routes = {} extra_routes = {}
RISOTTO_MODULES = services.get_services_list()
def create_context(request): def create_context(request):
risotto_context = Context() risotto_context = Context()
risotto_context.username = request.match_info.get('username', risotto_context.username = request.match_info.get('username',
@ -100,8 +97,8 @@ async def handle(request):
async def api(request, async def api(request,
risotto_context): risotto_context):
global tiramisu global TIRAMISU
if not tiramisu: if not TIRAMISU:
# check all URI that have an associated role # check all URI that have an associated role
# all URI without role is concidered has a private URI # all URI without role is concidered has a private URI
uris = [] uris = []
@ -114,13 +111,14 @@ async def api(request,
WHERE RoleURI.URIId = URI.URIId WHERE RoleURI.URIId = URI.URIId
''' '''
uris = [uri['uriname'] for uri in await connection.fetch(sql)] uris = [uri['uriname'] for uri in await connection.fetch(sql)]
async with await Config(get_messages(current_module_names=RISOTTO_MODULES, risotto_modules = services.get_services_list()
async with await Config(get_messages(current_module_names=risotto_modules,
load_shortarg=True, load_shortarg=True,
current_version=risotto_context.version, current_version=risotto_context.version,
uris=uris)[1]) as config: uris=uris)[1]) as config:
await config.property.read_write() await config.property.read_write()
tiramisu = await config.option.dict(remotable='none') TIRAMISU = await config.option.dict(remotable='none')
return tiramisu return TIRAMISU
async def get_app(loop): async def get_app(loop):
@ -169,4 +167,4 @@ async def get_app(loop):
return await loop.create_server(app.make_handler(), '*', get_config()['http_server']['port']) return await loop.create_server(app.make_handler(), '*', get_config()['http_server']['port'])
tiramisu = None TIRAMISU = None

View File

@ -3,7 +3,7 @@ try:
except: except:
from tiramisu import Config from tiramisu import Config
from inspect import signature from inspect import signature
from typing import Callable, Optional from typing import Callable, Optional, List
import asyncpg import asyncpg
from json import dumps, loads from json import dumps, loads
import risotto import risotto
@ -25,9 +25,12 @@ class Services():
self.services.setdefault(entry_point.name, []) self.services.setdefault(entry_point.name, [])
self.services_loaded = True self.services_loaded = True
def load_modules(self): def load_modules(self,
limit_services: Optional[List[str]]=None,
) -> None:
for entry_point in iter_entry_points(group='risotto_modules'): for entry_point in iter_entry_points(group='risotto_modules'):
service_name, module_name = entry_point.name.split('.') service_name, module_name = entry_point.name.split('.')
if limit_services is None or service_name in limit_services:
setattr(self, module_name, entry_point.load()) setattr(self, module_name, entry_point.load())
self.services[service_name].append(module_name) self.services[service_name].append(module_name)
self.modules_loaded = True self.modules_loaded = True
@ -37,9 +40,11 @@ class Services():
self.load_services() self.load_services()
return [(s, getattr(self, s)) for s in self.services] return [(s, getattr(self, s)) for s in self.services]
def get_modules(self): def get_modules(self,
limit_services: Optional[List[str]]=None,
) -> List[str]:
if not self.modules_loaded: if not self.modules_loaded:
self.load_modules() self.load_modules(limit_services=limit_services)
return [(m, getattr(self, m)) for s in self.services.values() for m in s] return [(m, getattr(self, m)) for s in self.services.values() for m in s]
def get_services_list(self): def get_services_list(self):
@ -52,8 +57,9 @@ class Services():
dispatcher, dispatcher,
validate: bool=True, validate: bool=True,
test: bool=False, test: bool=False,
limit_services: Optional[List[str]]=None,
): ):
for module_name, module in self.get_modules(): for module_name, module in self.get_modules(limit_services=limit_services):
dispatcher.set_module(module_name, dispatcher.set_module(module_name,
module, module,
test) test)
@ -65,6 +71,7 @@ services = Services()
services.load_services() services.load_services()
setattr(risotto, 'services', services) setattr(risotto, 'services', services)
def register(uris: str, def register(uris: str,
notification: str=None): notification: str=None):
""" Decorator to register function to the dispatcher """ Decorator to register function to the dispatcher
@ -255,7 +262,9 @@ class RegisterDispatcher:
if missing_messages: if missing_messages:
raise RegistrationError(_(f'no matching function for uri {missing_messages}')) raise RegistrationError(_(f'no matching function for uri {missing_messages}'))
async def on_join(self): async def on_join(self,
truncate: bool=False,
) -> None:
internal_user = get_config()['global']['internal_user'] internal_user = get_config()['global']['internal_user']
async with self.pool.acquire() as connection: async with self.pool.acquire() as connection:
await connection.set_type_codec( await connection.set_type_codec(
@ -264,6 +273,9 @@ class RegisterDispatcher:
decoder=loads, decoder=loads,
schema='pg_catalog' schema='pg_catalog'
) )
if truncate:
async with connection.transaction():
await connection.execute('TRUNCATE applicationservicedependency, deployment, factoryclusternode, factorycluster, log, release, userrole, risottouser, roleuri, infraserver, settingserver, servermodel, site, source, uri, userrole, zone, applicationservice')
async with connection.transaction(): async with connection.transaction():
for module_name, module in self.injected_self.items(): for module_name, module in self.injected_self.items():
risotto_context = Context() risotto_context = Context()
@ -286,6 +298,8 @@ class RegisterDispatcher:
for version, messages in self.messages.items(): for version, messages in self.messages.items():
for message, message_infos in messages.items(): for message, message_infos in messages.items():
if message_infos['pattern'] == 'rpc': if message_infos['pattern'] == 'rpc':
# module not available during test
if 'module' in message_infos:
module_name = message_infos['module'] module_name = message_infos['module']
function = message_infos['function'] function = message_infos['function']
await self.valid_rpc_params(version, await self.valid_rpc_params(version,

View File

@ -1,5 +1,15 @@
try:
from tiramisu3 import Storage
except:
from tiramisu import Storage from tiramisu import Storage
from risotto.config import DATABASE_DIR from os.path import isfile as _isfile
import os as _os
_envfile = '/etc/risotto/risotto.conf'
if _isfile(_envfile):
with open(_envfile, 'r') as fh_env:
for line in fh_env.readlines():
key, value = line.strip().split('=')
_os.environ[key] = value
STORAGE = Storage(engine='sqlite3', dir_database=DATABASE_DIR, name='test') STORAGE = Storage(engine='sqlite3')

View File

@ -1,20 +1,29 @@
from importlib import import_module from importlib import import_module
import pytest import pytest
from tiramisu import list_sessions, delete_session try:
from tiramisu3 import list_sessions, delete_session as _delete_session
except:
from tiramisu import list_sessions, delete_session as _delete_session
from .storage import STORAGE from .storage import STORAGE
from risotto import services
from risotto.context import Context from risotto.context import Context
from risotto.services import load_services #from risotto.services import load_services
from risotto.dispatcher import dispatcher from risotto.dispatcher import dispatcher
SOURCE_NAME = 'test'
SERVERMODEL_NAME = 'sm1'
def setup_module(module): def setup_module(module):
load_services(['config'], # load_services(['config'],
validate=False) # validate=False)
services.link_to_dispatcher(dispatcher, limit_services=['setting'], validate=False)
config_module = dispatcher.get_service('config') config_module = dispatcher.get_service('config')
config_module.save_storage = STORAGE config_module.save_storage = STORAGE
dispatcher.set_module('server', import_module(f'.server', 'fake_services'), True) #dispatcher.set_module('server', import_module(f'.server', 'fake_services'), True)
dispatcher.set_module('servermodel', import_module(f'.servermodel', 'fake_services'), True) #dispatcher.set_module('servermodel', import_module(f'.servermodel', 'fake_services'), True)
def setup_function(function): def setup_function(function):
@ -23,11 +32,11 @@ def setup_function(function):
config_module.servermodel = {} config_module.servermodel = {}
def teardown_function(function): async def delete_session():
# delete all sessions # delete all sessions
config_module = dispatcher.get_service('config') config_module = dispatcher.get_service('config')
for session in list_sessions(storage=config_module.save_storage): for session in await list_sessions(storage=config_module.save_storage):
delete_session(storage=config_module.save_storage, session_id=session) await _delete_session(storage=config_module.save_storage, session_id=session)
def get_fake_context(module_name): def get_fake_context(module_name):
@ -38,195 +47,170 @@ def get_fake_context(module_name):
return risotto_context return risotto_context
@pytest.mark.asyncio async def onjoin(source=True):
async def test_on_join():
config_module = dispatcher.get_service('config') config_module = dispatcher.get_service('config')
assert config_module.servermodel == {} assert config_module.servermodel == {}
assert config_module.server == {} assert config_module.server == {}
# #
#config_module.cache_root_path = 'tests/data'
await dispatcher.load()
await dispatcher.on_join(truncate=True)
if source:
fake_context = get_fake_context('config') fake_context = get_fake_context('config')
config_module.cache_root_path = 'tests/data' await dispatcher.call('v1',
await config_module.on_join(fake_context) 'setting.source.create',
assert list(config_module.servermodel.keys()) == [1, 2] fake_context,
assert list(config_module.server) == [3] source_name=SOURCE_NAME,
assert set(config_module.server[3]) == {'server', 'server_to_deploy', 'funcs_file'} source_directory='tests/data',
assert config_module.server[3]['funcs_file'] == 'tests/data/1/funcs.py' )
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_server_created(): async def test_on_join():
config_module = dispatcher.get_service('config') await onjoin(False)
fake_context = get_fake_context('config') await delete_session()
config_module.cache_root_path = 'tests/data'
await config_module.on_join(fake_context)
#
assert list(config_module.server) == [3]
await dispatcher.publish('v1',
'server.created',
fake_context,
server_id=4,
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'] == 'tests/data/2/funcs.py'
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_server_deleted(): async def test_source_create():
await onjoin()
config_module = dispatcher.get_service('config') config_module = dispatcher.get_service('config')
assert list(config_module.servermodel.keys()) == ['last_base']
assert list(config_module.server) == []
await delete_session()
# FIXME {source|release}.list {source|release}.describe {source|release}.delete, ...
async def create_servermodel(name=SERVERMODEL_NAME,
parents_name=['base'],
):
fake_context = get_fake_context('config') fake_context = get_fake_context('config')
config_module.cache_root_path = 'tests/data' await dispatcher.call('v1',
await config_module.on_join(fake_context) 'setting.servermodel.create',
#
assert list(config_module.server) == [3]
await dispatcher.publish('v1',
'server.created',
fake_context, fake_context,
server_id=4, servermodel_name=name,
server_name='name4', servermodel_description='servermodel 1',
server_description='description4', parents_name=parents_name,
server_servermodel_id=2) source_name=SOURCE_NAME,
assert list(config_module.server) == [3, 4] release_distribution='last',
await dispatcher.publish('v1', )
'server.deleted',
fake_context,
server_id=4)
assert list(config_module.server) == [3]
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_servermodel_created(): async def test_servermodel_created():
await onjoin()
config_module = dispatcher.get_service('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)
# #
assert list(config_module.servermodel) == [1, 2] assert list(config_module.servermodel) == ['last_base']
servermodel = {'servermodeid': 3, await create_servermodel()
'servermodelname': 'name3'} assert list(config_module.servermodel) == ['last_base', 'last_sm1']
await dispatcher.publish('v1', assert not list(await config_module.servermodel['last_base'].config.parents())
'servermodel.created', assert len(list(await config_module.servermodel['last_sm1'].config.parents())) == 1
fake_context, await delete_session()
servermodel_id=3,
servermodel_description='name3',
release_id=1,
servermodel_name='name3')
assert list(config_module.servermodel) == [1, 2, 3]
assert not list(await config_module.servermodel[3].config.parents())
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_servermodel_herited_created(): async def test_servermodel_created():
await onjoin()
config_module = dispatcher.get_service('config') config_module = dispatcher.get_service('config')
fake_context = get_fake_context('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] assert list(config_module.servermodel) == ['last_base']
await dispatcher.publish('v1', await dispatcher.call('v1',
'servermodel.created', 'setting.servermodel.create',
fake_context, fake_context,
servermodel_id=3, servermodel_name='sm1',
servermodel_name='name3', servermodel_description='servermodel 1',
release_id=1, parents_name=['base'],
servermodel_description='name3', source_name=SOURCE_NAME,
servermodel_parents_id=[1]) release_distribution='last',
assert list(config_module.servermodel) == [1, 2, 3] )
assert len(list(await config_module.servermodel[3].config.parents())) == 1 assert list(config_module.servermodel) == ['last_base', 'last_sm1']
assert not list(await config_module.servermodel['last_base'].config.parents())
assert len(list(await config_module.servermodel['last_sm1'].config.parents())) == 1
await delete_session()
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_servermodel_multi_herited_created(): async def test_server_created_base():
await onjoin()
config_module = dispatcher.get_service('config') config_module = dispatcher.get_service('config')
fake_context = get_fake_context('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] assert list(config_module.server) == []
await dispatcher.on_join(truncate=True)
server_name = 'dns.test.lan'
#FIXME pas de release ??
await dispatcher.publish('v1', await dispatcher.publish('v1',
'servermodel.created', 'infra.server.created',
fake_context, fake_context,
servermodel_id=3, server_name=server_name,
servermodel_name='name3', server_description='description_created',
release_id=1, servermodel_name='base',
servermodel_description='name3', release_distribution='last',
servermodel_parents_id=[1, 2]) site_name='site_1',
assert list(config_module.servermodel) == [1, 2, 3] zones_name=['zones'],
assert len(list(await config_module.servermodel[3].config.parents())) == 2 zones_ip=['1.1.1.1'],
)
assert list(config_module.server) == [server_name]
assert set(config_module.server[server_name]) == {'server', 'server_to_deploy', 'funcs_file'}
assert config_module.server[server_name]['funcs_file'] == '/var/cache/risotto/servermodel/last/base/funcs.py'
await delete_session()
#@pytest.mark.asyncio #@pytest.mark.asyncio
#async def test_servermodel_updated_not_exists(): #async def test_server_deleted():
# config_module = dispatcher.get_service('config')
# config_module.cache_root_path = 'tests/data'
# await config_module.on_join(fake_context)
# #
# assert list(config_module.server) == [3]
# await dispatcher.publish('v1',
# 'server.created',
# fake_context,
# server_id=4,
# server_name='name4',
# server_description='description4',
# server_servermodel_id=2)
# assert list(config_module.server) == [3, 4]
# await dispatcher.publish('v1',
# 'server.deleted',
# fake_context,
# server_id=4)
# assert list(config_module.server) == [3]
# await delete_session()
#
#
#@pytest.mark.asyncio
#async def test_servermodel_created():
# config_module = dispatcher.get_service('config') # config_module = dispatcher.get_service('config')
# fake_context = get_fake_context('config') # fake_context = get_fake_context('config')
# config_module.cache_root_path = 'tests/data' # config_module.cache_root_path = 'tests/data'
# await config_module.on_join(fake_context) # await config_module.on_join(fake_context)
# # # #
# assert list(config_module.servermodel) == [1, 2] # assert list(config_module.servermodel) == [1, 2]
# servermodel = {'servermodeid': 3,
# 'servermodelname': 'name3'}
# await dispatcher.publish('v1', # await dispatcher.publish('v1',
# 'servermodel.updated', # 'servermodel.created',
# fake_context, # fake_context,
# servermodel_id=3, # servermodel_id=3,
# servermodel_name='name3',
# release_id=1,
# servermodel_description='name3', # 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, # release_id=1,
# servermodel_description='name1-1') # servermodel_name='name3')
# assert set(config_module.servermodel) == {1, 2} # assert list(config_module.servermodel) == [1, 2, 3]
# assert config_module.servermodel[1].information.get('servermodel_name') == 'name1-1' # assert not list(await config_module.servermodel[3].config.parents())
# assert metaconfig1 != config_module.servermodel[1] # await delete_session()
# 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 #@pytest.mark.asyncio
# async def test_servermodel_updated2(): #async def test_servermodel_herited_created():
# config_module = dispatcher.get_service('config') # config_module = dispatcher.get_service('config')
# fake_context = get_fake_context('config') # fake_context = get_fake_context('config')
# config_module.cache_root_path = 'tests/data' # config_module.cache_root_path = 'tests/data'
# await config_module.on_join(fake_context) # await config_module.on_join(fake_context)
# # create a new servermodel # #
# assert list(config_module.servermodel) == [1, 2] # 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', # await dispatcher.publish('v1',
# 'servermodel.created', # 'servermodel.created',
# fake_context, # fake_context,
@ -237,102 +221,212 @@ async def test_servermodel_multi_herited_created():
# servermodel_parents_id=[1]) # servermodel_parents_id=[1])
# assert list(config_module.servermodel) == [1, 2, 3] # assert list(config_module.servermodel) == [1, 2, 3]
# assert len(list(await config_module.servermodel[3].config.parents())) == 1 # assert len(list(await config_module.servermodel[3].config.parents())) == 1
# assert await config_module.servermodel[3].information.get('servermodel_name') == 'name3' # await delete_session()
# 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 #@pytest.mark.asyncio
# async def test_servermodel_updated_config(): #async def test_servermodel_multi_herited_created():
# config_module = dispatcher.get_service('config') # config_module = dispatcher.get_service('config')
# fake_context = get_fake_context('config') # fake_context = get_fake_context('config')
# config_module.cache_root_path = 'tests/data' # config_module.cache_root_path = 'tests/data'
# await config_module.on_join(fake_context) # await config_module.on_join(fake_context)
# # # #
# config_module.servermodel[1].property.read_write() # assert list(config_module.servermodel) == [1, 2]
# 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', # await dispatcher.publish('v1',
# 'servermodel.updated', # 'servermodel.created',
# fake_context, # fake_context,
# servermodel_id=1, # servermodel_id=3,
# servermodel_name='name1-1', # servermodel_name='name3',
# release_id=1, # release_id=1,
# servermodel_description='name1-1') # servermodel_description='name3',
# assert config_module.servermodel[1].option('creole.general.mode_conteneur_actif').value.get() == 'oui' # 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 # await delete_session()
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)
# #
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', ##@pytest.mark.asyncio
'config.configuration.server.get', ##async def test_servermodel_updated_not_exists():
fake_context, ## config_module = dispatcher.get_service('config')
server_id=3) ## fake_context = get_fake_context('config')
configuration = {'configuration': ## config_module.cache_root_path = 'tests/data'
{'creole.general.mode_conteneur_actif': 'non', ## await config_module.on_join(fake_context)
'creole.general.master.master': [], ## #
'creole.general.master.slave1': [], ## assert list(config_module.servermodel) == [1, 2]
'creole.general.master.slave2': [], ## await dispatcher.publish('v1',
'containers.container0.files.file0.mkdir': False, ## 'servermodel.updated',
'containers.container0.files.file0.name': '/etc/mailname', ## fake_context,
'containers.container0.files.file0.rm': False, ## servermodel_id=3,
'containers.container0.files.file0.source': 'mailname', ## servermodel_name='name3',
'containers.container0.files.file0.activate': True}, ## release_id=1,
'server_id': 3, ## servermodel_description='name3',
'deployed': True} ## servermodel_parents_id=[1, 2])
assert values == configuration ## assert list(config_module.servermodel) == [1, 2, 3]
## assert len(list(await config_module.servermodel[3].config.parents())) == 2
## await delete_session()
##
##
## @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
## await delete_session()
##
##
## @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
## await delete_session()
##
##
## @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'
## await delete_session()
# #
values = await dispatcher.call('v1',
'config.configuration.server.get',
fake_context,
server_id=3,
deployed=False)
configuration['configuration']['creole.general.mode_conteneur_actif'] = 'oui'
configuration['deployed'] = False
assert values == configuration
@pytest.mark.asyncio
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)
# #
await config_module.server[3]['server_to_deploy'].property.read_write() #@pytest.mark.asyncio
assert await config_module.server[3]['server_to_deploy'].option('creole.general.mode_conteneur_actif').value.get() == 'non' #async def test_server_configuration_get():
await config_module.server[3]['server_to_deploy'].option('creole.general.mode_conteneur_actif').value.set('oui') # config_module = dispatcher.get_service('config')
assert await config_module.server[3]['server_to_deploy'].option('creole.general.mode_conteneur_actif').value.get() == 'oui' # fake_context = get_fake_context('config')
assert await config_module.server[3]['server'].option('creole.general.mode_conteneur_actif').value.get() == 'non' # config_module.cache_root_path = 'tests/data'
values = await dispatcher.publish('v1', # await config_module.on_join(fake_context)
'config.configuration.server.deploy', # #
fake_context, # await config_module.server[3]['server_to_deploy'].property.read_write()
server_id=3) # assert await config_module.server[3]['server_to_deploy'].option('creole.general.mode_conteneur_actif').value.get() == 'non'
assert await config_module.server[3]['server_to_deploy'].option('creole.general.mode_conteneur_actif').value.get() == 'oui' # await config_module.server[3]['server_to_deploy'].option('creole.general.mode_conteneur_actif').value.set('oui')
assert await 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() == 'non'
# #
# values = await dispatcher.call('v1',
# 'config.configuration.server.get',
# fake_context,
# server_id=3)
# configuration = {'configuration':
# {'creole.general.mode_conteneur_actif': 'non',
# 'creole.general.master.master': [],
# 'creole.general.master.slave1': [],
# 'creole.general.master.slave2': [],
# 'containers.container0.files.file0.mkdir': False,
# 'containers.container0.files.file0.name': '/etc/mailname',
# 'containers.container0.files.file0.rm': False,
# 'containers.container0.files.file0.source': 'mailname',
# 'containers.container0.files.file0.activate': True},
# 'server_id': 3,
# 'deployed': True}
# assert values == configuration
# #
# values = await dispatcher.call('v1',
# 'config.configuration.server.get',
# fake_context,
# server_id=3,
# deployed=False)
# configuration['configuration']['creole.general.mode_conteneur_actif'] = 'oui'
# configuration['deployed'] = False
# assert values == configuration
# await delete_session()
#
#
#@pytest.mark.asyncio
#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)
# #
# 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 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'
# await delete_session()

View File

@ -2,7 +2,7 @@ from importlib import import_module
import pytest import pytest
from .storage import STORAGE from .storage import STORAGE
from risotto.context import Context from risotto.context import Context
from risotto.services import load_services #from risotto.services import load_services
from risotto.dispatcher import dispatcher from risotto.dispatcher import dispatcher
from risotto.services.session.storage import storage_server, storage_servermodel from risotto.services.session.storage import storage_server, storage_servermodel
@ -16,9 +16,9 @@ def get_fake_context(module_name):
def setup_module(module): def setup_module(module):
load_services(['config', 'session'], #load_services(['config', 'session'],
validate=False, # validate=False,
test=True) # test=True)
config_module = dispatcher.get_service('config') config_module = dispatcher.get_service('config')
config_module.save_storage = STORAGE config_module.save_storage = STORAGE
dispatcher.set_module('server', import_module(f'.server', 'fake_services'), True) dispatcher.set_module('server', import_module(f'.server', 'fake_services'), True)