Skip to content
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
12 changes: 10 additions & 2 deletions eng/templates/jobs/ci-emulator-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ jobs:
eng/scripts/test-setup.sh
displayName: 'Install test python extension, dependencies and the worker'
condition: or(eq(variables.isExtensionsRelease, true), eq(variables['USETESTPYTHONEXTENSIONS'], true))
- bash: |
docker pull mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:vnext-preview
docker run --detach --publish 8081:8081 --publish 1234:1234 mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:vnext-preview
docker ps
displayName: "Start CosmosDB Emulator"
- bash: |
docker compose -f workers/tests/emulator_tests/utils/eventhub/docker-compose.yml pull
docker compose -f workers/tests/emulator_tests/utils/eventhub/docker-compose.yml up -d
Expand All @@ -83,7 +88,10 @@ jobs:
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;"
AzureWebJobsEventHubConnectionString: $(EmulatorEventHubConnectionString)
AzureWebJobsCosmosDBConnectionString: $(EmulatorCosmosDBConnectionString)
CosmosDBEmulatorUrl: $(CosmosDBEmulatorUrl)
CosmosDBEmulatorKey: $(CosmosDBEmulatorKey)
workingDirectory: $(Build.SourcesDirectory)/workers
displayName: "Running $(PYTHON_VERSION) Python Linux Emulator Tests"
- bash: |
Expand All @@ -99,6 +107,6 @@ jobs:
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;"
AzureWebJobsServiceBusConnectionString: $(EmulatorServiceBusConnectionString)
workingDirectory: $(Build.SourcesDirectory)/workers
displayName: "Running $(PYTHON_VERSION) Python ServiceBus Linux Emulator Tests"
1 change: 1 addition & 0 deletions workers/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Repository = "https://github.com/Azure/azure-functions-python-worker"

[project.optional-dependencies]
dev = [
"azure-cosmos", # Used for CosmosDB Emulator tests
"azure-eventhub", # Used for EventHub E2E tests
"azure-functions-durable", # Used for Durable E2E tests
"azure-monitor-opentelemetry; python_version >= '3.8'", # Used for Azure Monitor unit tests
Expand Down
110 changes: 110 additions & 0 deletions workers/tests/emulator_tests/test_cosmosdb_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import json
import os
import time

from azure.cosmos import CosmosClient, PartitionKey
from unittest import skip

from tests.utils import testutils

url = os.getenv("CosmosDBEmulatorUrl")
key = os.getenv("CosmosDBEmulatorKey")
client = CosmosClient(url, key)


# Create a database in the account using the CosmosClient
database_name = "test"
try:
database = client.create_database(id=database_name)
except Exception:
database = client.get_database_client(database=database_name)

# Create a container
container_name = "items"
try:
container = database.create_container(
id=container_name, partition_key=PartitionKey(path="/id")
)
except Exception:
container = database.get_container_client(container_name)

# Create a lease container
lease_container_name = "leases"
try:
lease_container = database.create_container(
id=lease_container_name, partition_key=PartitionKey(path="/id")
)
except Exception:
lease_container = database.get_container_client(lease_container_name)


class TestCosmosDBFunctions(testutils.WebHostTestCase):

@classmethod
def get_script_dir(cls):
return testutils.EMULATOR_TESTS_FOLDER / 'cosmosdb_functions'

def test_cosmosdb_trigger(self):
data = str(round(time.time()))
doc = {'id': 'cosmosdb-trigger-test',
'data': data}
r = self.webhost.request('POST', 'put_document',
data=json.dumps(doc))
self.assertEqual(r.status_code, 200)
self.assertEqual(r.text, 'OK')

time.sleep(5) # Wait for the trigger to execute

r = self.webhost.request('GET', 'get_cosmosdb_triggered')
self.assertEqual(r.status_code, 200)
response = r.json()
response.pop('_metadata', None)

self.assertEqual(response['id'], doc['id'])
self.assertTrue('_etag' in response)
self.assertTrue('_lsn' in response)
self.assertTrue('_rid' in response)
self.assertTrue('_ts' in response)

@skip("Waiting for 'Read collection feed' support in CosmosDB Emulator")
def test_cosmosdb_input(self):
data = str(round(time.time()))
doc = {'id': 'cosmosdb-input-test',
'data': data}
r = self.webhost.request('POST', 'put_document',
data=json.dumps(doc))
self.assertEqual(r.status_code, 200)
self.assertEqual(r.text, 'OK')

time.sleep(5) # Wait for the trigger to execute

r = self.webhost.request('GET', 'cosmosdb_input')
self.assertEqual(r.status_code, 200)
response = r.json()

# _lsn is present for cosmosdb change feed only,
# ref https://aka.ms/cosmos-change-feed
self.assertEqual(response['id'], doc['id'])
self.assertEqual(response['data'], doc['data'])
self.assertTrue('_etag' in response)
self.assertTrue('_rid' in response)
self.assertTrue('_self' in response)
self.assertTrue('_ts' in response)


class TestCosmosDBFunctionsStein(TestCosmosDBFunctions):

@classmethod
def get_script_dir(cls):
return testutils.EMULATOR_TESTS_FOLDER / 'cosmosdb_functions' / \
'cosmosdb_functions_stein'


class TestCosmosDBFunctionsSteinGeneric(TestCosmosDBFunctions):

@classmethod
def get_script_dir(cls):
return testutils.EMULATOR_TESTS_FOLDER / 'cosmosdb_functions' / \
'cosmosdb_functions_stein' / 'generic'
102 changes: 0 additions & 102 deletions workers/tests/endtoend/test_cosmosdb_functions.py

This file was deleted.

Loading