From 8f1134d1b24da7d579099e0facc527f19fef4eee Mon Sep 17 00:00:00 2001 From: gavin-aguiar Date: Tue, 23 Jan 2024 17:55:07 -0600 Subject: [PATCH 1/3] Resource templates for tests --- tests/templates/cosmosdb.bicep | 25 +++++++++++++++++++++++++ tests/templates/eventgrid.bicep | 13 +++++++++++++ tests/templates/eventhub.bicep | 13 +++++++++++++ tests/templates/servicebus.bicep | 15 +++++++++++++++ tests/templates/storage_account.bicep | 25 +++++++++++++++++++++++++ 5 files changed, 91 insertions(+) create mode 100644 tests/templates/cosmosdb.bicep create mode 100644 tests/templates/eventgrid.bicep create mode 100644 tests/templates/eventhub.bicep create mode 100644 tests/templates/servicebus.bicep create mode 100644 tests/templates/storage_account.bicep diff --git a/tests/templates/cosmosdb.bicep b/tests/templates/cosmosdb.bicep new file mode 100644 index 00000000..406d6a39 --- /dev/null +++ b/tests/templates/cosmosdb.bicep @@ -0,0 +1,25 @@ +param python_version string +param location string = resourceGroup().location + +var cosmosdb_name = 'python-worker-${python_version}-cdb' + +resource cosmosdb 'Microsoft.DocumentDB/databaseAccounts@2022-08-15' = { + name: cosmosdb_name + kind: 'GlobalDocumentDB' + location: location + properties: { + consistencyPolicy: { defaultConsistencyLevel: 'Session' } + locations: [ + { + locationName: location + failoverPriority: 0 + isZoneRedundant: false + } + ] + databaseAccountOfferType: 'Standard' + enableAutomaticFailover: false + enableMultipleWriteLocations: false + apiProperties: {} + capabilities: [ { name: 'EnableServerless' } ] + } +} diff --git a/tests/templates/eventgrid.bicep b/tests/templates/eventgrid.bicep new file mode 100644 index 00000000..a5f71be8 --- /dev/null +++ b/tests/templates/eventgrid.bicep @@ -0,0 +1,13 @@ +param python_version string +param location string = resourceGroup().location + +var eventgrid_name = 'python-worker-${python_version}-egt' + +resource eventgrid 'Microsoft.EventGrid/topics@2023-12-15-preview' = { + name: eventgrid_name + location: location + sku: { + name: 'Basic' + } + kind: 'Azure' +} \ No newline at end of file diff --git a/tests/templates/eventhub.bicep b/tests/templates/eventhub.bicep new file mode 100644 index 00000000..694dabd5 --- /dev/null +++ b/tests/templates/eventhub.bicep @@ -0,0 +1,13 @@ +param python_version string +param location string = resourceGroup().location + +var eventhubname = 'python-worker-${python_version}-ehns' + +resource eventhub 'Microsoft.EventHub/namespaces@2022-10-01-preview' = { + name: eventhubname + location: location + sku: { + name: 'Standard' + tier: 'Standard' + } +} diff --git a/tests/templates/servicebus.bicep b/tests/templates/servicebus.bicep new file mode 100644 index 00000000..73398170 --- /dev/null +++ b/tests/templates/servicebus.bicep @@ -0,0 +1,15 @@ +param python_version string +param location string = resourceGroup().location + + +var serviceBusNamespaceName = 'python-worker-${python_version}-sbns' + +resource serviceBusNamespace 'Microsoft.ServiceBus/namespaces@2022-01-01-preview' = { + name: serviceBusNamespaceName + location: location + sku: { + name: 'Standard' + } + properties: {} +} + diff --git a/tests/templates/storage_account.bicep b/tests/templates/storage_account.bicep new file mode 100644 index 00000000..17108f90 --- /dev/null +++ b/tests/templates/storage_account.bicep @@ -0,0 +1,25 @@ +param location string = resourceGroup().location +param python_version string + +var storage_account_name = 'pythonworker${python_version}sa' + +resource storageAccount_input 'Microsoft.Storage/storageAccounts@2022-09-01' = { + name: storage_account_name + location: location + sku: { + name: 'Standard_LRS' + } + kind: 'StorageV2' + properties: { + networkAcls: { + bypass: 'AzureServices' + defaultAction: 'Allow' + } + accessTier: 'Hot' + publicNetworkAccess: 'Enabled' + dnsEndpointType: 'Standard' + allowBlobPublicAccess: true + minimumTlsVersion: 'TLS1_2' + allowSharedKeyAccess: true + } +} From 28ce9614dd120f8c789ac8a76eb4ef7f9ee5eb3a Mon Sep 17 00:00:00 2001 From: gavin-aguiar Date: Mon, 29 Jan 2024 09:51:20 -0600 Subject: [PATCH 2/3] Added script to run templates --- tests/templates/deploy_templates.py | 58 +++++++++++++++++++++++++++++ tests/unittests/test_dispatcher.py | 3 +- 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 tests/templates/deploy_templates.py diff --git a/tests/templates/deploy_templates.py b/tests/templates/deploy_templates.py new file mode 100644 index 00000000..83b0d861 --- /dev/null +++ b/tests/templates/deploy_templates.py @@ -0,0 +1,58 @@ +import os +import re +import subprocess + +subscription_id = "0b894477-1614-4c8d-8a9b-a697a24596b8" +resource_group = "AzureFunctionsPythonWorkerCILinuxDevOps" + + +def deploy_bicep_files(): + """ + Deploy selected Bicep files compatible with the specified Python version to the given Azure resource group, + with user-provided parameters for each template. + + :param resource_group: The Azure resource group to deploy to. + :param python_version: The Python version to filter Bicep files. + """ + # Get the current working directory + directory = os.getcwd() + + python_version = input("Enter the Python version (e.g., 3.8): ") + + # Regex pattern to match files compatible with the specified Python version + pattern = ".*\\.bicep$" + + # List all Bicep files in the directory compatible with the specified Python version + all_bicep_files = [f for f in os.listdir(directory) if re.search(pattern, f)] + + if not all_bicep_files: + print(f"No Bicep files found") + return + + # Display the list of files to the user + print("Available Bicep Templates:") + for idx, file in enumerate(all_bicep_files, 1): + print(f"{idx}. {file}") + + # Ask user to select files to deploy + selected = input("Enter the numbers of the templates to deploy (comma separated), or type 'all' to deploy all: ") + if selected.lower() == 'all': + bicep_files_to_deploy = all_bicep_files + else: + selected_indices = [int(i) - 1 for i in selected.split(',') if i.isdigit()] + bicep_files_to_deploy = [all_bicep_files[i] for i in selected_indices if 0 <= i < len(all_bicep_files)] + + # Deploy the selected Bicep files + for bicep_file in bicep_files_to_deploy: + file_path = os.path.join(directory, bicep_file) + + params_string = f"--parameters python_version={python_version.replace('.', '')}" + + # Deploy the Bicep file + print(f"Deploying {bicep_file}...") + deploy_command = (f"az deployment group create --subscription {subscription_id} " + f"--resource-group {resource_group} --template-file {file_path} {params_string}") + subprocess.run(deploy_command, shell=True) + + +deploy_bicep_files() diff --git a/tests/unittests/test_dispatcher.py b/tests/unittests/test_dispatcher.py index d607216e..67c2274d 100644 --- a/tests/unittests/test_dispatcher.py +++ b/tests/unittests/test_dispatcher.py @@ -362,8 +362,9 @@ async def test_sync_invocation_request_log_threads(self): 'sync threadpool max workers: 5' ) + @patch.dict('os.environ', {PYTHON_THREADPOOL_THREAD_COUNT: '4'}) async def test_async_invocation_request_log_threads(self): - os.environ.update({PYTHON_THREADPOOL_THREAD_COUNT: '4'}) + #os.environ.update({PYTHON_THREADPOOL_THREAD_COUNT: '4'}) with patch('azure_functions_worker.dispatcher.logger') as mock_logger: async with self._ctrl as host: From 3cfcc620bd54f82057770cfe3935ecad636156fd Mon Sep 17 00:00:00 2001 From: gavin-aguiar Date: Mon, 29 Jan 2024 09:57:57 -0600 Subject: [PATCH 3/3] Removed unwanted changes --- tests/unittests/test_dispatcher.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/unittests/test_dispatcher.py b/tests/unittests/test_dispatcher.py index 67c2274d..d607216e 100644 --- a/tests/unittests/test_dispatcher.py +++ b/tests/unittests/test_dispatcher.py @@ -362,9 +362,8 @@ async def test_sync_invocation_request_log_threads(self): 'sync threadpool max workers: 5' ) - @patch.dict('os.environ', {PYTHON_THREADPOOL_THREAD_COUNT: '4'}) async def test_async_invocation_request_log_threads(self): - #os.environ.update({PYTHON_THREADPOOL_THREAD_COUNT: '4'}) + os.environ.update({PYTHON_THREADPOOL_THREAD_COUNT: '4'}) with patch('azure_functions_worker.dispatcher.logger') as mock_logger: async with self._ctrl as host: