diff --git a/SAS/TMSS/test/test_utils.py b/SAS/TMSS/test/test_utils.py index 8191b2186065976eebf208065557d4e3786ba2c7..a7730873bdd068cc88788071d904e8bf8edaf286 100644 --- a/SAS/TMSS/test/test_utils.py +++ b/SAS/TMSS/test/test_utils.py @@ -134,12 +134,13 @@ 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, public_host: str=None, exchange: str=os.environ.get("TMSS_EXCHANGE", DEFAULT_BUSNAME), broker: str=os.environ.get("TMSS_EXCHANGE", DEFAULT_BUSNAME)): self._db_dbcreds_id = db_dbcreds_id self._ldap_dbcreds_id = ldap_dbcreds_id self.host = host self.port = port + self.public_host = public_host or host self._server_process = None self._exchange = exchange self._broker = broker @@ -147,7 +148,7 @@ class TMSSDjangoServerInstance(): @property def address(self): ''':returns the address and port of the django server''' - return "%s:%d" % (self.host, self.port) + return "%s:%d" % (self.public_host, self.port) @property def url(self): @@ -265,7 +266,7 @@ 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, public_host: str=None, 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): self._populate_schemas = populate_schemas @@ -276,6 +277,7 @@ class TMSSTestEnvironment: ldap_dbcreds_id=self.ldap_server.dbcreds_id, host=host, port=find_free_port(preferred_django_port), + public_host=public_host, exchange=exchange, broker=broker) self.client_credentials = TemporaryCredentials(user=self.ldap_server.dbcreds.user, @@ -291,7 +293,7 @@ class TMSSTestEnvironment: self.django_server.start() # store client credentials in the TemporaryCredentials file... - self.client_credentials.dbcreds.host = self.django_server.host + self.client_credentials.dbcreds.host = self.django_server.public_host self.client_credentials.dbcreds.port = self.django_server.port self.client_credentials.dbcreds.type = "http" self.client_credentials.create() @@ -378,22 +380,32 @@ def main_test_environment(): parser = OptionParser('%prog [options]', description='setup/run/teardown a full TMSS test environment including a fresh and isolated database, LDAP server and DJANGO REST server.') - parser.add_option("-H", "--host", dest="host", type="string", default='127.0.0.1', - 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), + + group = OptionGroup(parser, 'Network') + parser.add_option_group(group) + group.add_option("-H", "--host", dest="host", type="string", default='0.0.0.0', + help="serve the TMSS Django REST API server via this host. [default=%default]") + group.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]") - parser.add_option('-d', '--data', dest='data', action='store_true', help='populate the test-database with test/example data') + 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]") + + group = OptionGroup(parser, 'Example/Test data') + 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 = OptionGroup(parser, 'Messaging options') + parser.add_option_group(group) 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 RATestEnvironment(exchange=options.exchange, broker=options.broker): - with TMSSTestEnvironment(host=options.host, preferred_django_port=options.port, exchange=options.exchange, broker=options.broker, + with TMSSTestEnvironment(host=options.host, preferred_django_port=options.port, public_host=options.public_host, + exchange=options.exchange, broker=options.broker, populate_schemas=True, populate_test_data=options.data) as instance: # print some nice info for the user to use the test servers...