[halfapi] config option "--dry-run", used in test_domain

This commit is contained in:
Maxime Alves LIRMM@home 2021-12-01 21:11:26 +01:00
parent 2f9005a1a5
commit 038715e94a
4 changed files with 33 additions and 6 deletions

View File

@ -22,11 +22,11 @@ from ..lib.schemas import schema_csv_dict
@click.option('--loglevel', default=LOGLEVEL) @click.option('--loglevel', default=LOGLEVEL)
@click.option('--prefix', default='/') @click.option('--prefix', default='/')
@click.option('--check', default=True) @click.option('--check', default=True)
@click.option('--dryrun', default=False, is_flag=True)
@click.argument('schema', type=click.File('r'), required=False) @click.argument('schema', type=click.File('r'), required=False)
@click.argument('router', required=False)
@click.argument('domain', required=False) @click.argument('domain', required=False)
@cli.command() @cli.command()
def run(host, port, reload, secret, production, loglevel, prefix, check, schema, router, domain): def run(host, port, reload, secret, production, loglevel, prefix, check, dryrun, schema, domain):
""" """
The "halfapi run" command The "halfapi run" command
""" """
@ -51,7 +51,6 @@ def run(host, port, reload, secret, production, loglevel, prefix, check, schema,
click.echo(f'Launching application {PROJECT_NAME}') click.echo(f'Launching application {PROJECT_NAME}')
CONFIG.get('domain')['name'] = domain CONFIG.get('domain')['name'] = domain
CONFIG.get('domain')['router'] = router
if schema: if schema:
# Populate the SCHEMA global with the data from the given file # Populate the SCHEMA global with the data from the given file
@ -67,6 +66,9 @@ def run(host, port, reload, secret, production, loglevel, prefix, check, schema,
f'reload: {reload}\n' f'reload: {reload}\n'
) )
if dryrun:
CONFIG['dryrun'] = True
uvicorn.run('halfapi.app:application', uvicorn.run('halfapi.app:application',
host=host, host=host,
port=int(port), port=int(port),

View File

@ -17,6 +17,7 @@ It defines the following globals :
- HOST (str) - HALFAPI_HOST - HOST (str) - HALFAPI_HOST
- PORT (int) - HALFAPI_PORT - PORT (int) - HALFAPI_PORT
- CONF_DIR (str) - HALFAPI_CONF_DIR - CONF_DIR (str) - HALFAPI_CONF_DIR
- DRYRUN (bool) - HALFAPI_DRYRUN
- config (ConfigParser) - config (ConfigParser)
It reads the following ressource : It reads the following ressource :
@ -53,6 +54,7 @@ HOST = '127.0.0.1'
PORT = '3000' PORT = '3000'
SECRET = '' SECRET = ''
CONF_FILE = os.environ.get('HALFAPI_CONF_FILE', '.halfapi/config') CONF_FILE = os.environ.get('HALFAPI_CONF_FILE', '.halfapi/config')
DRYRUN = bool(os.environ.get('HALFAPI_DRYRUN', False))
DOMAIN = None DOMAIN = None
ROUTER = None ROUTER = None
@ -143,6 +145,7 @@ CONFIG = {
'secret': SECRET, 'secret': SECRET,
'host': HOST, 'host': HOST,
'port': PORT, 'port': PORT,
'dryrun': DRYRUN,
'domain': { 'domain': {
'name': None, 'name': None,
'router': None 'router': None

View File

@ -52,11 +52,12 @@ class HalfAPI:
SECRET = config.get('secret') SECRET = config.get('secret')
PRODUCTION = config.get('production', True) PRODUCTION = config.get('production', True)
CONFIG = config.get('config', {}) CONFIG = config.get('config', {})
DRYRUN = config.get('dryrun', False)
domain = config.get('domain')['name'] domain = config.get('domain')['name']
router = config.get('domain').get('router', None) router = config.get('domain').get('router', None)
if not (domain and router): if not (domain):
raise NoDomainsException() raise NoDomainsException()
self.PRODUCTION = PRODUCTION self.PRODUCTION = PRODUCTION
@ -95,6 +96,13 @@ class HalfAPI:
for route in gen_domain_routes(m_domain_router): for route in gen_domain_routes(m_domain_router):
routes.append(route) routes.append(route)
startup_fcts = []
if DRYRUN:
startup_fcts.append(
HalfAPI.wait_quit()
)
self.__application = Starlette( self.__application = Starlette(
debug=not PRODUCTION, debug=not PRODUCTION,
routes=routes, routes=routes,
@ -104,7 +112,8 @@ class HalfAPI:
500: InternalServerErrorResponse, 500: InternalServerErrorResponse,
501: NotImplementedResponse, 501: NotImplementedResponse,
503: ServiceUnavailableResponse 503: ServiceUnavailableResponse
} },
on_startup=startup_fcts
) )
self.__application.add_middleware( self.__application.add_middleware(
@ -181,3 +190,12 @@ class HalfAPI:
@staticmethod @staticmethod
def api_schema(domain): def api_schema(domain):
pass pass
@staticmethod
def wait_quit():
""" sleeps 1 second and quits. used in dry-run mode
"""
import time
import sys
time.sleep(1)
sys.exit(0)

View File

@ -48,4 +48,8 @@ class TestDomain(TestCase):
result = self.runner.invoke(cli, ['domain', self.DOMAIN]) result = self.runner.invoke(cli, ['domain', self.DOMAIN])
self.assertEqual(result.exit_code, 0) self.assertEqual(result.exit_code, 0)
result_d = json.loads(result.stdout) result_d = json.loads(result.stdout)
return result.stdout result = self.runner.invoke(cli, ['run', '--dryrun', self.DOMAIN])
print(result.stdout)
self.assertEqual(result.exit_code, 0)
return result_d