Skip to content

Commit

Permalink
move servicebus tests to emulators
Browse files Browse the repository at this point in the history
  • Loading branch information
Victoria Hall committed Dec 18, 2024
1 parent 40ce8b4 commit 5f6e2a9
Show file tree
Hide file tree
Showing 20 changed files with 465 additions and 97 deletions.
4 changes: 3 additions & 1 deletion eng/ci/official-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ extends:
- stage: RunEmulatorTests
dependsOn: Build
jobs:
- template: /eng/templates/official/jobs/ci-emulator-tests.yml@self
- template: /eng/templates/jobs/ci-emulator-tests.yml@self
parameters:
PoolName: 1es-pool-azfunc
- stage: RunUnitTests
dependsOn: Build
jobs:
Expand Down
4 changes: 3 additions & 1 deletion eng/ci/public-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,6 @@ extends:
- stage: RunEmulatorTests
dependsOn: Build
jobs:
- template: /eng/templates/jobs/ci-emulator-tests.yml@self
- template: /eng/templates/jobs/ci-emulator-tests.yml@self
parameters:
PoolName: 1es-pool-azfunc-public
25 changes: 19 additions & 6 deletions eng/templates/jobs/ci-emulator-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ jobs:
displayName: "Run Python Emulator Tests"

pool:
name: 1es-pool-azfunc-public
name: ${{ parameters.PoolName }}
image: 1es-ubuntu-22.04
os: linux

Expand Down Expand Up @@ -74,12 +74,25 @@ jobs:
displayName: 'Install test python extension, dependencies and the worker'
condition: or(eq(variables.isExtensionsRelease, true), eq(variables['USETESTPYTHONEXTENSIONS'], true))
- bash: |
docker compose -f tests/emulator_tests/utils/docker-compose.yml pull
docker compose -f tests/emulator_tests/utils/docker-compose.yml up -d
displayName: 'Install Azurite and Start Emulators'
docker compose -f tests/emulator_tests/utils/eventhub/docker-compose.yml pull
docker compose -f tests/emulator_tests/utils/eventhub/docker-compose.yml up -d
displayName: 'Install Azurite and Start EventHub Emulator'
- bash: |
python -m pytest -q -n auto --dist loadfile --reruns 4 tests/emulator_tests
python -m pytest -q -n auto --dist loadfile --reruns 4 --ignore=tests/emulator_tests/test_servicebus_functions.py tests/emulator_tests
env:
AzureWebJobsStorage: "UseDevelopmentStorage=true"
AzureWebJobsEventHubConnectionString: "Endpoint=sb://localhost;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true;"
displayName: "Running $(PYTHON_VERSION) Python Emulator Tests"
displayName: "Running $(PYTHON_VERSION) Python Linux Emulator Tests"
- bash: |
# Stop and remove EventHub Emulator container to free up the port
docker stop microsoft-azure-eventhub-emulator
docker container rm --force microsoft-azure-eventhub-emulator
docker compose -f tests/emulator_tests/utils/servicebus/docker-compose.yml pull
docker compose -f tests/emulator_tests/utils/servicebus/docker-compose.yml up -d
displayName: 'Install Azurite and Start ServiceBus Emulator'
- bash: |
python -m pytest -q -n auto --dist loadfile --reruns 4 tests/emulator_tests/test_servicebus_functions.py
env:
AzureWebJobsStorage: "UseDevelopmentStorage=true"
AzureWebJobsServiceBusConnectionString: "Endpoint=sb://localhost;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true;"
displayName: "Running $(PYTHON_VERSION) Python ServiceBus Linux Emulator Tests"
85 changes: 0 additions & 85 deletions eng/templates/official/jobs/ci-emulator-tests.yml

This file was deleted.

2 changes: 1 addition & 1 deletion python/test/worker.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"description":{
"language":"python",
"extensions":[".py"],
"defaultExecutablePath":"python",
"defaultExecutablePath":"C:\\Users\\victoriahall\\Documents\\repos\\azure-functions-python-worker\\.venv\\Scripts\\python.exe",
"defaultWorkerPath":"worker.py",
"workerIndexing": "true"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import azure.functions as func


def main(req: func.HttpRequest, file: func.InputStream) -> str:
return func.HttpResponse(
file.read().decode('utf-8'), mimetype='application/json')
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"scriptFile": "__init__.py",
"bindings": [
{
"type": "httpTrigger",
"direction": "in",
"name": "req",
"authLevel": "anonymous"
},
{
"type": "blob",
"direction": "in",
"name": "file",
"connection": "AzureWebJobsStorage",
"path": "python-worker-tests/test-servicebus-triggered.txt"
},
{
"type": "http",
"direction": "out",
"name": "$return",
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import azure.functions as azf


def main(req: azf.HttpRequest, msg: azf.Out[str]):
msg.set(req.get_body().decode('utf-8'))

return 'OK'
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"scriptFile": "__init__.py",

"bindings": [
{
"type": "httpTrigger",
"direction": "in",
"name": "req",
"authLevel": "anonymous"
},
{
"direction": "out",
"name": "msg",
"queueName": "testqueue",
"connection": "AzureWebJobsServiceBusConnectionString",
"type": "serviceBus"
},
{
"direction": "out",
"name": "$return",
"type": "http"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import json

import azure.functions as func

app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)


@app.route(route="put_message")
@app.service_bus_queue_output(
arg_name="msg",
connection="AzureWebJobsServiceBusConnectionString",
queue_name="testqueue")
def put_message(req: func.HttpRequest, msg: func.Out[str]):
msg.set(req.get_body().decode('utf-8'))
return 'OK'


@app.route(route="get_servicebus_triggered")
@app.blob_input(arg_name="file",
path="python-worker-tests/test-servicebus-triggered.txt",
connection="AzureWebJobsStorage")
def get_servicebus_triggered(req: func.HttpRequest,
file: func.InputStream) -> str:
return func.HttpResponse(
file.read().decode('utf-8'), mimetype='application/json')


@app.service_bus_queue_trigger(
arg_name="msg",
connection="AzureWebJobsServiceBusConnectionString",
queue_name="testqueue")
@app.blob_output(arg_name="$return",
path="python-worker-tests/test-servicebus-triggered.txt",
connection="AzureWebJobsStorage")
def servicebus_trigger(msg: func.ServiceBusMessage) -> str:
result = json.dumps({
'message_id': msg.message_id,
'body': msg.get_body().decode('utf-8'),
'content_type': msg.content_type,
'delivery_count': msg.delivery_count,
'expiration_time': (msg.expiration_time.isoformat() if
msg.expiration_time else None),
'label': msg.label,
'partition_key': msg.partition_key,
'reply_to': msg.reply_to,
'reply_to_session_id': msg.reply_to_session_id,
'scheduled_enqueue_time': (msg.scheduled_enqueue_time.isoformat() if
msg.scheduled_enqueue_time else None),
'session_id': msg.session_id,
'time_to_live': msg.time_to_live,
'to': msg.to,
'user_properties': msg.user_properties,

'application_properties': msg.application_properties,
'correlation_id': msg.correlation_id,
'dead_letter_error_description': msg.dead_letter_error_description,
'dead_letter_reason': msg.dead_letter_reason,
'dead_letter_source': msg.dead_letter_source,
'enqueued_sequence_number': msg.enqueued_sequence_number,
'enqueued_time_utc': (msg.enqueued_time_utc.isoformat() if
msg.enqueued_time_utc else None),
'expires_at_utc': (msg.expires_at_utc.isoformat() if
msg.expires_at_utc else None),
'locked_until': (msg.locked_until.isoformat() if
msg.locked_until else None),
'lock_token': msg.lock_token,
'sequence_number': msg.sequence_number,
'state': msg.state,
'subject': msg.subject,
'transaction_partition_key': msg.transaction_partition_key
})

return result
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import json

import azure.functions as func

app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)


@app.function_name(name="put_message")
@app.generic_trigger(arg_name="req", type="httpTrigger", route="put_message")
@app.generic_output_binding(arg_name="msg",
type="serviceBus",
connection="AzureWebJobsServiceBusConnectionString",
queue_name="testqueue")
@app.generic_output_binding(arg_name="$return", type="http")
def put_message(req: func.HttpRequest, msg: func.Out[str]):
msg.set(req.get_body().decode('utf-8'))
return 'OK'


@app.function_name(name="get_servicebus_triggered")
@app.generic_trigger(arg_name="req", type="httpTrigger",
route="get_servicebus_triggered")
@app.generic_input_binding(arg_name="file",
type="blob",
path="python-worker-tests/test-servicebus-triggered.txt", # NoQA
connection="AzureWebJobsStorage")
@app.generic_output_binding(arg_name="$return", type="http")
def get_servicebus_triggered(req: func.HttpRequest,
file: func.InputStream) -> str:
return func.HttpResponse(
file.read().decode('utf-8'), mimetype='application/json')


@app.generic_trigger(
arg_name="msg",
type="serviceBusTrigger",
connection="AzureWebJobsServiceBusConnectionString",
queue_name="testqueue")
@app.generic_output_binding(arg_name="$return",
path="python-worker-tests/test-servicebus-triggered.txt", # NoQA
type="blob",
connection="AzureWebJobsStorage")
def servicebus_trigger(msg: func.ServiceBusMessage) -> str:
result = json.dumps({
'message_id': msg.message_id,
'body': msg.get_body().decode('utf-8'),
'content_type': msg.content_type,
'delivery_count': msg.delivery_count,
'expiration_time': (msg.expiration_time.isoformat() if
msg.expiration_time else None),
'label': msg.label,
'partition_key': msg.partition_key,
'reply_to': msg.reply_to,
'reply_to_session_id': msg.reply_to_session_id,
'scheduled_enqueue_time': (msg.scheduled_enqueue_time.isoformat() if
msg.scheduled_enqueue_time else None),
'session_id': msg.session_id,
'time_to_live': msg.time_to_live,
'to': msg.to,
'user_properties': msg.user_properties,

'application_properties': msg.application_properties,
'correlation_id': msg.correlation_id,
'dead_letter_error_description': msg.dead_letter_error_description,
'dead_letter_reason': msg.dead_letter_reason,
'dead_letter_source': msg.dead_letter_source,
'enqueued_sequence_number': msg.enqueued_sequence_number,
'enqueued_time_utc': (msg.enqueued_time_utc.isoformat() if
msg.enqueued_time_utc else None),
'expires_at_utc': (msg.expires_at_utc.isoformat() if
msg.expires_at_utc else None),
'locked_until': (msg.locked_until.isoformat() if
msg.locked_until else None),
'lock_token': msg.lock_token,
'sequence_number': msg.sequence_number,
'state': msg.state,
'subject': msg.subject,
'transaction_partition_key': msg.transaction_partition_key
})

return result
Loading

0 comments on commit 5f6e2a9

Please sign in to comment.