Skip to content
Snippets Groups Projects
Commit 3345c0bd authored by Jorrit Schaap's avatar Jorrit Schaap
Browse files

TMSS-190: added option(s) to do/don't start services in TMSSTestEnvironment

parent 3e538238
Branches
Tags
1 merge request!252Resolve TMSS-190
...@@ -270,7 +270,8 @@ class TMSSTestEnvironment: ...@@ -270,7 +270,8 @@ class TMSSTestEnvironment:
def __init__(self, host: str='127.0.0.1', preferred_django_port: int=8000, public_host: str=None, def __init__(self, host: str='127.0.0.1', preferred_django_port: int=8000, public_host: str=None,
exchange: str=os.environ.get("TMSS_EXCHANGE", DEFAULT_BUSNAME), broker: str=os.environ.get("TMSS_BROKER", DEFAULT_BROKER), exchange: str=os.environ.get("TMSS_EXCHANGE", DEFAULT_BUSNAME), broker: str=os.environ.get("TMSS_BROKER", DEFAULT_BROKER),
populate_schemas:bool=False, populate_test_data:bool=False, populate_schemas:bool=False, populate_test_data:bool=False,
start_pg_listener: bool=True): start_ra_test_environment: bool=False, start_pg_listener: bool=False,
start_subtask_scheduler: bool=False, start_dynamic_scheduler: bool=False):
self._exchange = exchange self._exchange = exchange
self._broker = broker self._broker = broker
self._populate_schemas = populate_schemas self._populate_schemas = populate_schemas
...@@ -284,9 +285,19 @@ class TMSSTestEnvironment: ...@@ -284,9 +285,19 @@ class TMSSTestEnvironment:
public_host=public_host) public_host=public_host)
self.client_credentials = TemporaryCredentials(user=self.ldap_server.dbcreds.user, self.client_credentials = TemporaryCredentials(user=self.ldap_server.dbcreds.user,
password=self.ldap_server.dbcreds.password) password=self.ldap_server.dbcreds.password)
self._start_ra_test_environment = start_ra_test_environment
self.ra_test_environment = None
self._start_pg_listener = start_pg_listener self._start_pg_listener = start_pg_listener
self.pg_listener = None self.pg_listener = None
self._start_subtask_scheduler = start_subtask_scheduler
self.subtask_scheduler = None
self._start_dynamic_scheduler = start_dynamic_scheduler
self.dynamic_scheduler = None
# Check for correct Django version, should be at least 3.0 # Check for correct Django version, should be at least 3.0
if django.VERSION[0] < 3: if django.VERSION[0] < 3:
print("\nWARNING: YOU ARE USING DJANGO VERSION '%s', WHICH WILL NOT SUPPORT ALL FEATURES IN TMSS!\n" % print("\nWARNING: YOU ARE USING DJANGO VERSION '%s', WHICH WILL NOT SUPPORT ALL FEATURES IN TMSS!\n" %
...@@ -318,12 +329,26 @@ class TMSSTestEnvironment: ...@@ -318,12 +329,26 @@ class TMSSTestEnvironment:
user.is_superuser = True user.is_superuser = True
user.save() user.save()
if self._start_ra_test_environment:
self.ra_test_environment = RATestEnvironment(exchange=self._exchange, broker=self._broker)
self.ra_test_environment.start()
if self._start_pg_listener: if self._start_pg_listener:
# start the TMSSPGListener, so the changes in the database are posted as EventMessages on the bus # start the TMSSPGListener, so the changes in the database are posted as EventMessages on the bus
from lofar.sas.tmss.services.tmsspglistener import TMSSPGListener from lofar.sas.tmss.services.tmsspglistener import TMSSPGListener
self.pg_listener = TMSSPGListener(exchange=self._exchange, broker=self._broker, dbcreds=self.database.dbcreds) self.pg_listener = TMSSPGListener(exchange=self._exchange, broker=self._broker, dbcreds=self.database.dbcreds)
self.pg_listener.start() self.pg_listener.start()
if self._start_subtask_scheduler:
from lofar.sas.tmss.services.subtask_scheduling import create_service as create_subtask_scheduling_service
self.subtask_scheduler = create_subtask_scheduling_service(exchange=self._exchange, broker=self._broker)
self.subtask_scheduler.start_listening()
if self._start_dynamic_scheduler:
from lofar.sas.tmss.services.dynamic_scheduling import create_dynamic_scheduling_service
self.dynamic_scheduler = create_dynamic_scheduling_service(exchange=self._exchange, broker=self._broker)
self.dynamic_scheduler.start_listening()
if self._populate_schemas or self._populate_test_data: if self._populate_schemas or self._populate_test_data:
self.populate_schemas() self.populate_schemas()
...@@ -336,6 +361,18 @@ class TMSSTestEnvironment: ...@@ -336,6 +361,18 @@ class TMSSTestEnvironment:
self.pg_listener.stop() self.pg_listener.stop()
self.pg_listener = None self.pg_listener = None
if self.subtask_scheduler is not None:
self.subtask_scheduler.stop_listening()
self.subtask_scheduler = None
if self.dynamic_scheduler is not None:
self.dynamic_scheduler.stop_listening()
self.dynamic_scheduler = None
if self.ra_test_environment is not None:
self.ra_test_environment.stop()
self.ra_test_environment = None
self.django_server.stop() self.django_server.stop()
self.ldap_server.stop() self.ldap_server.stop()
self.database.destroy() self.database.destroy()
...@@ -406,9 +443,14 @@ def main_test_environment(): ...@@ -406,9 +443,14 @@ def main_test_environment():
group.add_option("-P", "--public_host", dest="public_host", type="string", default='127.0.0.1', group.add_option("-P", "--public_host", dest="public_host", type="string", default='127.0.0.1',
help="expose the TMSS Django REST API via this host. [default=%default]") help="expose the TMSS Django REST API via this host. [default=%default]")
group = OptionGroup(parser, 'Example/Test data') group = OptionGroup(parser, 'Example/Test data, schemas and services',
description='Options to enable/create example/test data, schemas and services. ' \
'Without these options you get a lean and mean TMSS test environment, but then you need to run the background services yourselves, and create test data yourself. ' \
'For standalone commissioning/testing/playing around you need all these options.')
parser.add_option_group(group) parser.add_option_group(group)
group.add_option('-d', '--data', dest='data', action='store_true', help='populate the test-database with test/example data') group.add_option('-d', '--data', dest='data', action='store_true', help='populate the test-database with test/example data')
group.add_option('-s', '--schemas', dest='schemas', action='store_true', help='populate the test-database with the TMSS JSON schemas')
group.add_option('-S', '--services', dest='services', action='store_true', help='start the TMSS background services.')
group = OptionGroup(parser, 'Messaging options') group = OptionGroup(parser, 'Messaging options')
parser.add_option_group(group) parser.add_option_group(group)
...@@ -419,40 +461,36 @@ def main_test_environment(): ...@@ -419,40 +461,36 @@ def main_test_environment():
logging.basicConfig(format = '%(asctime)s %(levelname)s %(message)s', level = logging.INFO) logging.basicConfig(format = '%(asctime)s %(levelname)s %(message)s', level = logging.INFO)
with RATestEnvironment(exchange=options.exchange, broker=options.broker): with TMSSTestEnvironment(host=options.host, preferred_django_port=options.port, public_host=options.public_host,
with TMSSTestEnvironment(host=options.host, preferred_django_port=options.port, public_host=options.public_host, exchange=options.exchange, broker=options.broker,
exchange=options.exchange, broker=options.broker, populate_schemas=options.schemas, populate_test_data=options.data,
populate_schemas=True, populate_test_data=False) as tmss_test_env: start_ra_test_environment=options.services, start_pg_listener=options.services,
start_subtask_scheduler=options.services, start_dynamic_scheduler=options.services) as tmss_test_env:
from lofar.sas.tmss.services.dynamic_scheduling import create_dynamic_scheduling_service
with create_dynamic_scheduling_service(options.exchange, options.broker): # print some nice info for the user to use the test servers...
if options.data: # use print instead of log for clean lines.
tmss_test_env.populate_test_data() for h in logging.root.handlers:
h.flush()
# print some nice info for the user to use the test servers... print()
# use print instead of log for clean lines. print()
for h in logging.root.handlers: print("*****************************************************")
h.flush() print("Test-TMSS database, LDAP and Django up and running...")
print() print("*****************************************************")
print() print("DB Credentials ID: %s" % (tmss_test_env.database.dbcreds_id, ))
print("*****************************************************") print("LDAP Credentials ID: %s" % (tmss_test_env.django_server.ldap_dbcreds_id, ))
print("Test-TMSS database, LDAP and Django up and running...") print("TMSS Client Credentials ID: %s" % (tmss_test_env.client_credentials.dbcreds_id, ))
print("*****************************************************") print("Django URL: %s" % (tmss_test_env.django_server.url))
print("DB Credentials ID: %s" % (tmss_test_env.database.dbcreds_id, )) print()
print("LDAP Credentials ID: %s" % (tmss_test_env.django_server.ldap_dbcreds_id, )) print("Example cmdlines to run tmss or tmss_manage_django:")
print("TMSS Client Credentials ID: %s" % (tmss_test_env.client_credentials.dbcreds_id, )) print("TMSS_DBCREDENTIALS=%s TMSS_LDAPCREDENTIALS=%s tmss" % (tmss_test_env.database.dbcreds_id, tmss_test_env.django_server.ldap_dbcreds_id))
print("Django URL: %s" % (tmss_test_env.django_server.url)) print("TMSS_DBCREDENTIALS=%s TMSS_LDAPCREDENTIALS=%s tmss_manage_django" % (tmss_test_env.database.dbcreds_id, tmss_test_env.django_server.ldap_dbcreds_id))
print() print()
print("Example cmdlines to run tmss or tmss_manage_django:") print("Example cmdline to run tmss client call:")
print("TMSS_DBCREDENTIALS=%s TMSS_LDAPCREDENTIALS=%s tmss" % (tmss_test_env.database.dbcreds_id, tmss_test_env.django_server.ldap_dbcreds_id)) print("TMSS_CLIENT_DBCREDENTIALS=%s tmss_set_subtask_state <id> <state>" % (tmss_test_env.client_credentials.dbcreds_id, ))
print("TMSS_DBCREDENTIALS=%s TMSS_LDAPCREDENTIALS=%s tmss_manage_django" % (tmss_test_env.database.dbcreds_id, tmss_test_env.django_server.ldap_dbcreds_id)) print()
print() print("Press Ctrl-C to exit (and remove the test database and django server automatically)")
print("Example cmdline to run tmss client call:")
print("TMSS_CLIENT_DBCREDENTIALS=%s tmss_set_subtask_state <id> <state>" % (tmss_test_env.client_credentials.dbcreds_id, )) waitForInterrupt()
print()
print("Press Ctrl-C to exit (and remove the test database and django server automatically)")
waitForInterrupt()
if __name__ == '__main__': if __name__ == '__main__':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment