4444E2E_TESTS_ROOT = TESTS_ROOT / E2E_TESTS_FOLDER
4545UNIT_TESTS_FOLDER = pathlib .Path ('unittests' )
4646UNIT_TESTS_ROOT = TESTS_ROOT / UNIT_TESTS_FOLDER
47- DEFAULT_WEBHOST_DLL_PATH = PROJECT_ROOT / 'build' / 'webhost' / \
48- 'Microsoft.Azure.WebJobs.Script.WebHost.dll'
47+ WEBHOST_DLL = "Microsoft.Azure.WebJobs.Script.WebHost.dll"
48+ DEFAULT_WEBHOST_DLL_PATH = PROJECT_ROOT / 'build' / 'webhost' / WEBHOST_DLL
4949EXTENSIONS_PATH = PROJECT_ROOT / 'build' / 'extensions' / 'bin'
5050FUNCS_PATH = TESTS_ROOT / UNIT_TESTS_FOLDER / 'http_functions'
5151WORKER_PATH = PROJECT_ROOT / 'python' / 'test'
5252WORKER_CONFIG = PROJECT_ROOT / '.testconfig'
5353ON_WINDOWS = platform .system () == 'Windows'
54+ LOCALHOST = "127.0.0.1"
5455
5556HOST_JSON_TEMPLATE = """\
5657 {
@@ -317,7 +318,7 @@ def __init__(self, loop, scripts_dir):
317318 self ._server = grpc .server (self ._threadpool )
318319 self ._servicer = _MockWebHostServicer (self )
319320 protos .add_FunctionRpcServicer_to_server (self ._servicer , self ._server )
320- self ._port = self ._server .add_insecure_port ('127.0.0.1 :0' )
321+ self ._port = self ._server .add_insecure_port (f' { LOCALHOST } :0' )
321322
322323 self ._worker_id = self .make_id ()
323324 self ._request_id = self .make_id ()
@@ -459,7 +460,7 @@ async def __aenter__(self):
459460 await self ._host .start ()
460461
461462 self ._worker = await dispatcher . \
462- Dispatcher .connect ('127.0.0.1' , self ._host ._port ,
463+ Dispatcher .connect (LOCALHOST , self ._host ._port ,
463464 self ._host .worker_id ,
464465 self ._host .request_id , connect_timeout = 5.0 )
465466
@@ -469,6 +470,7 @@ async def __aenter__(self):
469470 wait ([self ._host ._connected_fut , self ._worker_task ],
470471 return_when = asyncio .FIRST_COMPLETED )
471472
473+ # noinspection PyBroadException
472474 try :
473475 if self ._worker_task in done :
474476 self ._worker_task .result ()
@@ -502,7 +504,7 @@ async def __aexit__(self, *exc):
502504def start_mockhost (* , script_root = FUNCS_PATH ):
503505 tests_dir = TESTS_ROOT
504506 scripts_dir = tests_dir / script_root
505- if not scripts_dir .exists () or not scripts_dir .is_dir ():
507+ if not ( scripts_dir .exists () and scripts_dir .is_dir () ):
506508 raise RuntimeError (
507509 f'invalid script_root argument: '
508510 f'{ scripts_dir } directory does not exist' )
@@ -536,7 +538,7 @@ def close(self):
536538
537539def _find_open_port ():
538540 with socket .socket () as s :
539- s .bind (('127.0.0.1' , 0 ))
541+ s .bind ((LOCALHOST , 0 ))
540542 s .listen (1 )
541543 return s .getsockname ()[1 ]
542544
@@ -572,10 +574,10 @@ def popen_webhost(*, stdout, stderr, script_root=FUNCS_PATH, port=None):
572574 if not dll :
573575 dll = DEFAULT_WEBHOST_DLL_PATH
574576
575- secrets = SECRETS_TEMPLATE
576-
577577 os .makedirs (dll .parent / 'Secrets' , exist_ok = True )
578578 with open (dll .parent / 'Secrets' / 'host.json' , 'w' ) as f :
579+ secrets = SECRETS_TEMPLATE
580+
579581 f .write (secrets )
580582
581583 if dll and pathlib .Path (dll ).exists ():
@@ -605,11 +607,7 @@ def popen_webhost(*, stdout, stderr, script_root=FUNCS_PATH, port=None):
605607 ]))
606608
607609 worker_path = os .environ .get ('PYAZURE_WORKER_DIR' )
608- if not worker_path :
609- worker_path = WORKER_PATH
610- else :
611- worker_path = pathlib .Path (worker_path )
612-
610+ worker_path = WORKER_PATH if not worker_path else pathlib .Path (worker_path )
613611 if not worker_path .exists ():
614612 raise RuntimeError (f'Worker path { worker_path } does not exist' )
615613
@@ -640,6 +638,15 @@ def popen_webhost(*, stdout, stderr, script_root=FUNCS_PATH, port=None):
640638 if servicebus :
641639 extra_env ['AzureWebJobsServiceBusConnectionString' ] = servicebus
642640
641+ eventgrid_topic_uri = testconfig ['azure' ].get ('eventgrid_topic_uri' )
642+ if eventgrid_topic_uri :
643+ extra_env ['AzureWebJobsEventGridTopicUri' ] = eventgrid_topic_uri
644+
645+ eventgrid_topic_key = testconfig ['azure' ].get ('eventgrid_topic_key' )
646+ if eventgrid_topic_key :
647+ extra_env ['AzureWebJobsEventGridConnectionKey' ] = \
648+ eventgrid_topic_key
649+
643650 if port is not None :
644651 extra_env ['ASPNETCORE_URLS' ] = f'http://*:{ port } '
645652
@@ -655,11 +662,7 @@ def popen_webhost(*, stdout, stderr, script_root=FUNCS_PATH, port=None):
655662
656663
657664def start_webhost (* , script_dir = None , stdout = None ):
658- if script_dir :
659- script_root = TESTS_ROOT / script_dir
660- else :
661- script_root = FUNCS_PATH
662-
665+ script_root = TESTS_ROOT / script_dir if script_dir else FUNCS_PATH
663666 if stdout is None :
664667 if is_envvar_true (PYAZURE_WEBHOST_DEBUG ):
665668 stdout = sys .stdout
@@ -670,8 +673,8 @@ def start_webhost(*, script_dir=None, stdout=None):
670673 proc = popen_webhost (stdout = stdout , stderr = subprocess .STDOUT ,
671674 script_root = script_root , port = port )
672675
673- addr = f'http://127.0.0.1 :{ port } '
674- for n in range (10 ):
676+ addr = f'http://{ LOCALHOST } :{ port } '
677+ for _ in range (10 ):
675678 try :
676679 r = requests .get (f'{ addr } /api/ping' ,
677680 params = {'code' : 'testFunctionKey' })
@@ -682,11 +685,11 @@ def start_webhost(*, script_dir=None, stdout=None):
682685 except requests .exceptions .ConnectionError :
683686 pass
684687
685- time .sleep (0.5 )
688+ time .sleep (1 )
686689 else :
687690 proc .terminate ()
688691 try :
689- proc .wait (10 )
692+ proc .wait (20 )
690693 except subprocess .TimeoutExpired :
691694 proc .kill ()
692695 raise RuntimeError ('could not start the webworker' )
@@ -697,7 +700,7 @@ def start_webhost(*, script_dir=None, stdout=None):
697700def create_dummy_dispatcher ():
698701 dummy_event_loop = asyncio .new_event_loop ()
699702 disp = dispatcher .Dispatcher (
700- dummy_event_loop , '127.0.0.1' , 0 ,
703+ dummy_event_loop , LOCALHOST , 0 ,
701704 'test_worker_id' , 'test_request_id' ,
702705 1.0 , 1000 )
703706 dummy_event_loop .close ()
0 commit comments