From f77674c79be3e00b0cc6557d792b48a63f8f4e2a Mon Sep 17 00:00:00 2001 From: Jorrit Schaap <schaap@astron.nl> Date: Fri, 15 May 2020 17:05:04 +0200 Subject: [PATCH] TMSS-60: made exchange and broker configurable for tmss(_test_environment) --- SAS/TMSS/bin/tmss | 15 +++++++++++-- .../src/tmss/tmssapp/models/scheduling.py | 6 ++++-- SAS/TMSS/test/test_utils.py | 21 ++++++++++++++----- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/SAS/TMSS/bin/tmss b/SAS/TMSS/bin/tmss index b4898cee091..078fab655d8 100755 --- a/SAS/TMSS/bin/tmss +++ b/SAS/TMSS/bin/tmss @@ -24,18 +24,27 @@ PORT=8008 CREDENTIALS="tmss" LDAP_CREDENTIALS="tmss_ldap" +EXCHANGE="lofar" +BROKER="localhost" # Parse args: -while getopts "p:C:L:h" opt; do +while getopts "p:C:L:e:b:h" opt; do case ${opt} in p ) PORT=${OPTARG} ;; C ) CREDENTIALS=${OPTARG} ;; L ) LDAP_CREDENTIALS=${OPTARG} ;; + e ) EXCHANGE=${OPTARG} ;; + b ) BROKER=${OPTARG} ;; h ) echo "usage: tmss [OPTIONS]" echo " where options are:" echo " -p <port> the port where django runs the rest http interface on. default=$PORT" echo " -C <credentials-name> the name of the database credentials in ~/.lofar/dbcredentials. default=$CREDENTIALS" echo " -L <credentials-name> the name of the ldap credentials in ~/.lofar/dbcredentials. default=$LDAP_CREDENTIALS" + echo "" + echo " Messaging options:" + echo " -b BROKER, Address of the message broker, default: $BROKER" + echo " -e EXCHANGE Bus or queue where the TMSS messages are published. [default: $EXCHANGE]" + exit 0 ;; esac @@ -43,10 +52,12 @@ done echo "!!! This tmss application is for testing only, properly deploy in Nginx or Apache for production use !!!" -echo "Using Django port=$PORT credentials=$CREDENTIALS ldap_credentials=$LDAP_CREDENTIALS" +echo "Using Django port=$PORT credentials=$CREDENTIALS ldap_credentials=$LDAP_CREDENTIALS exchange=$EXCHANGE broker=$BROKER" export TMSS_DBCREDENTIALS=$CREDENTIALS export TMSS_LDAPCREDENTIALS=$LDAP_CREDENTIALS +export TMSS_EXCHANGE=$EXCHANGE +export TMSS_BROKER=$BROKER DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" diff --git a/SAS/TMSS/src/tmss/tmssapp/models/scheduling.py b/SAS/TMSS/src/tmss/tmssapp/models/scheduling.py index e33dd8b1c65..e530b52169d 100644 --- a/SAS/TMSS/src/tmss/tmssapp/models/scheduling.py +++ b/SAS/TMSS/src/tmss/tmssapp/models/scheduling.py @@ -2,6 +2,7 @@ This file contains the database models """ +import os import logging logger = logging.getLogger(__name__) @@ -15,7 +16,7 @@ from rest_framework.serializers import HyperlinkedRelatedField from django.dispatch import receiver from lofar.sas.tmss.tmss.tmssapp.validation import validate_json_against_schema -from lofar.messaging.messagebus import ToBus +from lofar.messaging.messagebus import ToBus, DEFAULT_BROKER, DEFAULT_BUSNAME from lofar.messaging.messages import EventMessage from lofar.sas.tmss.client.tmssbuslistener import DEFAULT_TMSS_SUBTASK_NOTIFICATION_PREFIX @@ -156,7 +157,8 @@ class Subtask(BasicCommon): @staticmethod def _send_state_change_event_message(subtask_id:int, old_state: str, new_state: str): - with ToBus() as tobus: #TODO: do we want to connect to the bus for each new message, or have some global tobus? + with ToBus(exchange=os.environ.get("TMSS_EXCHANGE", DEFAULT_BUSNAME), + broker=os.environ.get("TMSS_BROKER", DEFAULT_BROKER)) as tobus: #TODO: do we want to connect to the bus for each new message, or have some global tobus? msg = EventMessage(subject="%s.%s" % (DEFAULT_TMSS_SUBTASK_NOTIFICATION_PREFIX, new_state.capitalize()), content={'subtask_id': subtask_id, 'old_state': old_state, 'new_state': new_state}) tobus.send(msg) diff --git a/SAS/TMSS/test/test_utils.py b/SAS/TMSS/test/test_utils.py index 34e44c6f384..cf5d274a010 100644 --- a/SAS/TMSS/test/test_utils.py +++ b/SAS/TMSS/test/test_utils.py @@ -32,6 +32,7 @@ from lofar.common.dbcredentials import Credentials, DBCredentials from lofar.common.util import find_free_port, waitForInterrupt from lofar.sas.tmss.test.ldap_test_service import TestLDAPServer from lofar.sas.tmss.tmss.exceptions import TMSSException +from lofar.messaging.config import DEFAULT_BROKER, DEFAULT_BUSNAME def assertDataWithUrls(self, data, expected): """ @@ -113,12 +114,14 @@ class TMSSPostgresTestMixin(PostgresTestMixin): class TMSSDjangoServerInstance(): ''' Creates a running django TMSS server at the requested port with the requested database credentials. ''' - def __init__(self, db_dbcreds_id: str="TMSS", ldap_dbcreds_id: str="TMSS_LDAP", host: str='127.0.0.1', port: int=8000): + def __init__(self, db_dbcreds_id: str="TMSS", ldap_dbcreds_id: str="TMSS_LDAP", host: str='127.0.0.1', port: int=8000, exchange: str=DEFAULT_BUSNAME, broker: str=DEFAULT_BROKER): self._db_dbcreds_id = db_dbcreds_id self._ldap_dbcreds_id = ldap_dbcreds_id self.host = host self.port = port self._server_process = None + self._exchange = exchange + self._broker = broker @property def address(self): @@ -160,6 +163,8 @@ class TMSSDjangoServerInstance(): # set these here, run django setup, and start the server os.environ["TMSS_LDAPCREDENTIALS"] = self.ldap_dbcreds_id os.environ["TMSS_DBCREDENTIALS"] = self.database_dbcreds_id + os.environ["TMSS_EXCHANGE"] = self._exchange + os.environ["TMSS_BROKER"] = self._broker os.environ["DJANGO_SETTINGS_MODULE"] = "lofar.sas.tmss.tmss.settings" django.setup() @@ -241,13 +246,15 @@ class TMSSDjangoServerInstance(): class TMSSTestEnvironment: '''Create and run a test django TMSS server against a newly created test database and a test ldap server (and cleanup automagically)''' - def __init__(self, host: str='127.0.0.1', preferred_django_port: int=8000): + def __init__(self, host: str='127.0.0.1', preferred_django_port: int=8000, exchange: str=DEFAULT_BUSNAME, broker: str=DEFAULT_BROKER): self.ldap_server = TestLDAPServer(user='test', password='test') self.database = TMSSTestDatabaseInstance() self.django_server = TMSSDjangoServerInstance(db_dbcreds_id=self.database.dbcreds_id, ldap_dbcreds_id=self.ldap_server.dbcreds_id, host=host, - port=find_free_port(preferred_django_port)) + port=find_free_port(preferred_django_port), + exchange=exchange, + broker=broker) def start(self): self.ldap_server.start() @@ -307,7 +314,7 @@ def main_test_database(): def main_test_environment(): """instantiate, run and destroy a full tmss test environment (postgress database, ldap server, django server)""" - from optparse import OptionParser + from optparse import OptionParser, OptionGroup os.environ['TZ'] = 'UTC' parser = OptionParser('%prog [options]', @@ -316,11 +323,15 @@ def main_test_environment(): help="expose the TMSS Django REST API via this host. [default=%default]") parser.add_option("-p", "--port", dest="port", type="int", default=find_free_port(8000), help="try to use this port for the DJANGO REST API. If not available, then a random free port is used and logged. [default=%default]") + group = OptionGroup(parser, 'Messaging options') + group.add_option('-b', '--broker', dest='broker', type='string', default=DEFAULT_BROKER, help='Address of the message broker, default: %default') + group.add_option('-e', "--exchange", dest="exchange", type="string", default=DEFAULT_BUSNAME, help="Bus or queue where the TMSS messages are published. [default: %default]") + parser.add_option_group(group) (options, args) = parser.parse_args() logging.basicConfig(format = '%(asctime)s %(levelname)s %(message)s', level = logging.INFO) - with TMSSTestEnvironment(host=options.host, preferred_django_port=options.port) as instance: + with TMSSTestEnvironment(host=options.host, preferred_django_port=options.port, exchange=options.exchange, broker=options.broker) as instance: # print some nice info for the user to use the test servers... # use print instead of log for clean lines. for h in logging.root.handlers: -- GitLab