77from pathlib import Path
88from typing import Dict
99
10+ import docker
1011import pytest
1112import trafaret_config
1213import yaml
13-
1414from simcore_service_webserver .application_config import app_schema
1515from simcore_service_webserver .cli import create_environ
1616from simcore_service_webserver .resources import resources as app_resources
@@ -38,7 +38,7 @@ def here():
3838 return Path (sys .argv [0 ] if __name__ == "__main__" else __file__ ).resolve ().parent
3939
4040@pytest .fixture (scope = "module" )
41- def webserver_environ (request , devel_environ , services_docker_compose ) -> Dict [str , str ]:
41+ def webserver_environ (request , devel_environ , services_docker_compose , docker_stack ) -> Dict [str , str ]:
4242 """ Environment variables for the webserver application
4343
4444 """
@@ -53,18 +53,21 @@ def webserver_environ(request, devel_environ, services_docker_compose) -> Dict[s
5353
5454 # get the list of core services the test module wants
5555 core_services = getattr (request .module , 'core_services' , [])
56-
5756 # OVERRIDES:
5857 # One of the biggest differences with respect to the real system
5958 # is that the webserver application is replaced by a light-weight
6059 # version tha loads only the subsystems under test. For that reason,
6160 # the test webserver is built-up in webserver_service fixture that runs
6261 # on the host.
6362 for name in core_services :
63+ if 'ports' not in services_docker_compose ['services' ][name ]:
64+ continue
65+
66+ # published port is sometimes dynamically defined by the swarm
67+
6468 environ ['%s_HOST' % name .upper ()] = '127.0.0.1'
65- environ ['%s_PORT' % name .upper ()] = \
66- services_docker_compose ['services' ][name ]['ports' ][0 ].split (':' )[0 ] # takes port exposed
67- # to swarm boundary since webserver is installed in the host and therefore outside the swarm's network
69+ environ ['%s_PORT' % name .upper ()] = get_service_published_port (name )
70+ # to swarm boundary since webserver is installed in the host and therefore outside the swarm's network
6871 from pprint import pprint
6972 pprint (environ )
7073 return environ
@@ -77,9 +80,6 @@ def _recreate_config_file():
7780 cfg = yaml .safe_load (f )
7881 # test webserver works in host
7982 cfg ["main" ]['host' ] = '127.0.0.1'
80- cfg ["rabbit" ]["host" ] = '127.0.0.1'
81- cfg ["rabbit" ]["port" ] = "5672"
82- cfg ["director" ]["host" ] = "127.0.0.1"
8383
8484 with config_file_path .open ('wt' ) as f :
8585 yaml .dump (cfg , f , default_flow_style = False )
@@ -90,7 +90,6 @@ def _recreate_config_file():
9090 config_environ = {}
9191 config_environ .update (webserver_environ )
9292 config_environ .update ( create_environ (skip_host_environ = True ) ) # TODO: can be done monkeypathcing os.environ and calling create_environ as well
93-
9493 # validates
9594 cfg_dict = trafaret_config .read_and_validate (config_file_path , app_schema , vars = config_environ )
9695
@@ -114,3 +113,17 @@ def resolve_environ(service, environ):
114113 value = environ .get (value , value )
115114 _environs [key ] = value
116115 return _environs
116+
117+ def get_service_published_port (service_name : str ) -> str :
118+ published_port = "-1"
119+ client = docker .from_env ()
120+ services = [x for x in client .services .list () if service_name in x .name ]
121+ if not services :
122+ return published_port
123+ service_endpoint = services [0 ].attrs ["Endpoint" ]
124+
125+ if "Ports" not in service_endpoint or not service_endpoint ["Ports" ]:
126+ return published_port
127+
128+ published_port = service_endpoint ["Ports" ][0 ]["PublishedPort" ]
129+ return str (published_port )
0 commit comments