Skip to content

blueprint change: worker indexing functionregister #1065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions azure_functions_worker/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,19 @@ def index_function_app(function_path: str):
module_name = pathlib.Path(function_path).stem
imported_module = importlib.import_module(module_name)

from azure.functions import FunctionApp
app: Optional[FunctionApp] = None
from azure.functions import FunctionRegister
app: Optional[FunctionRegister] = None
for i in imported_module.__dir__():
if isinstance(getattr(imported_module, i, None), FunctionApp):
if isinstance(getattr(imported_module, i, None), FunctionRegister):
if not app:
app = getattr(imported_module, i, None)
else:
raise ValueError(
"Multiple instances of FunctionApp are defined")
f"More than one {app.__class__.__name__} or other top "
f"level function app instances are defined.")

if not app:
raise ValueError("Could not find instance of FunctionApp in "
raise ValueError("Could not find top level function app instances in "
f"{SCRIPT_FILE_NAME}.")

return app.get_functions()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"grpcio~=1.43.0",
"grpcio-tools~=1.43.0",
"protobuf~=3.19.3",
'azure-functions==1.11.3b2',
"azure-functions==1.11.3b2",
"python-dateutil~=2.8.2"
]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import logging

import azure.functions as func

bp = func.Blueprint()


@bp.route(route="default_template")
def default_template(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')

name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')

if name:
return func.HttpResponse(
f"Hello, {name}. This HTTP triggered function "
f"executed successfully.")
else:
return func.HttpResponse(
"This HTTP triggered function executed successfully. "
"Pass a name in the query string or in the request body for a"
" personalized response.",
status_code=200
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import azure.functions as func
from blueprint import bp

app = func.FunctionApp()

app.register_functions(bp)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import logging

import azure.functions as func

bp = func.Blueprint()


@bp.route(route="default_template")
def default_template(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')

name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')

if name:
return func.HttpResponse(
f"Hello, {name}. This HTTP triggered function "
f"executed successfully.")
else:
return func.HttpResponse(
"This HTTP triggered function executed successfully. "
"Pass a name in the query string or in the request body for a"
" personalized response.",
status_code=200
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import azure.functions as func

from blueprint import bp

app = func.FunctionApp()

app.register_blueprint(bp)


@app.route(route="return_http")
def return_http(req: func.HttpRequest):
return func.HttpResponse('<h1>Hello World™</h1>',
mimetype='text/html')
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import azure.functions as func

app = func.FunctionApp()


@app.route(route="return_http")
def return_http(req: func.HttpRequest):
return func.HttpResponse('<h1>Hello World™</h1>',
mimetype='text/html')


asgi_app = func.AsgiFunctionApp()
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import logging

import azure.functions as func

bp = func.Blueprint()


@bp.route(route="default_template")
def default_template(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')

name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')

if name:
return func.HttpResponse(
f"Hello, {name}. This HTTP triggered function "
f"executed successfully.")
else:
return func.HttpResponse(
"This HTTP triggered function executed successfully. "
"Pass a name in the query string or in the request body for a"
" personalized response.",
status_code=200
)
59 changes: 59 additions & 0 deletions tests/endtoend/test_blueprint_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

from azure_functions_worker import testutils


class TestFunctionInBluePrintOnly(testutils.WebHostTestCase):
@classmethod
def get_script_dir(cls):
return testutils.E2E_TESTS_FOLDER / 'blueprint_functions' / \
'functions_in_blueprint_only'

@testutils.retryable_test(3, 5)
def test_function_in_blueprint_only(self):
r = self.webhost.request('GET', 'default_template')
self.assertTrue(r.ok)


class TestFunctionsInBothBlueprintAndFuncApp(testutils.WebHostTestCase):
@classmethod
def get_script_dir(cls):
return testutils.E2E_TESTS_FOLDER / 'blueprint_functions' / \
'functions_in_both_blueprint_functionapp'

@testutils.retryable_test(3, 5)
def test_functions_in_both_blueprint_functionapp(self):
r = self.webhost.request('GET', 'default_template')
self.assertTrue(r.ok)

r = self.webhost.request('GET', 'return_http')
self.assertTrue(r.ok)


class TestMultipleFunctionRegisters(testutils.WebHostTestCase):
@classmethod
def get_script_dir(cls):
return testutils.E2E_TESTS_FOLDER / 'blueprint_functions' / \
'multiple_function_registers'

@testutils.retryable_test(3, 5)
def test_function_in_blueprint_only(self):
r = self.webhost.request('GET', 'return_http')
self.assertEqual(r.status_code, 404)


class TestOnlyBlueprint(testutils.WebHostTestCase):
@classmethod
def get_script_dir(cls):
return testutils.E2E_TESTS_FOLDER / 'blueprint_functions' / \
'only_blueprint'

@testutils.retryable_test(3, 5)
def test_only_blueprint(self):
"""Test if the default template of Http trigger in Python
Function app
will return OK
"""
r = self.webhost.request('GET', 'default_template')
self.assertEqual(r.status_code, 404)
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ async def raise_http_exception():
raise HTTPException(status_code=404, detail="Item not found")


app = func.FunctionApp(asgi_app=fast_app,
http_auth_level=func.AuthLevel.ANONYMOUS)
app = func.AsgiFunctionApp(app=fast_app,
http_auth_level=func.AuthLevel.ANONYMOUS)
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ def raise_http_exception():
return {"detail": "Item not found"}, 404


app = func.FunctionApp(wsgi_app=flask_app.wsgi_app,
http_auth_level=func.AuthLevel.ANONYMOUS)
app = func.WsgiFunctionApp(app=flask_app.wsgi_app,
http_auth_level=func.AuthLevel.ANONYMOUS)
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,5 @@ async def unhandled_unserializable_error():
raise UnserializableException('foo')


app = func.FunctionApp(asgi_app=fast_app,
http_auth_level=func.AuthLevel.ANONYMOUS)
app = func.AsgiFunctionApp(app=fast_app,
http_auth_level=func.AuthLevel.ANONYMOUS)
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,5 @@ def unhandled_unserializable_error():
raise UnserializableException('foo')


app = func.FunctionApp(wsgi_app=flask_app.wsgi_app,
http_auth_level=func.AuthLevel.ANONYMOUS)
app = func.WsgiFunctionApp(app=flask_app.wsgi_app,
http_auth_level=func.AuthLevel.ANONYMOUS)