-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
python specific app setting logs added (#1353)
* python specific app setting logs added * lint * refactor * codeowner * manual convert dict to string * moved to utils, more efficient * added to old tests, created new * addressing comments * fixing tests, efficiency * python worker ext * removed comma * FLAKE --------- Co-authored-by: Varad Meru <vrdmr@users.noreply.github.com>
- Loading branch information
1 parent
d4200f8
commit 56f5caf
Showing
5 changed files
with
172 additions
and
6 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
import os | ||
import sys | ||
|
||
from ..constants import (PYTHON_ROLLBACK_CWD_PATH, | ||
PYTHON_THREADPOOL_THREAD_COUNT, | ||
PYTHON_ISOLATE_WORKER_DEPENDENCIES, | ||
PYTHON_ENABLE_WORKER_EXTENSIONS, | ||
PYTHON_ENABLE_WORKER_EXTENSIONS_DEFAULT, | ||
PYTHON_ENABLE_WORKER_EXTENSIONS_DEFAULT_39, | ||
PYTHON_ENABLE_DEBUG_LOGGING, | ||
FUNCTIONS_WORKER_SHARED_MEMORY_DATA_TRANSFER_ENABLED) | ||
|
||
|
||
def get_python_appsetting_state(): | ||
current_vars = os.environ.copy() | ||
python_specific_settings = \ | ||
[PYTHON_ROLLBACK_CWD_PATH, | ||
PYTHON_THREADPOOL_THREAD_COUNT, | ||
PYTHON_ISOLATE_WORKER_DEPENDENCIES, | ||
PYTHON_ENABLE_DEBUG_LOGGING, | ||
PYTHON_ENABLE_WORKER_EXTENSIONS, | ||
FUNCTIONS_WORKER_SHARED_MEMORY_DATA_TRANSFER_ENABLED] | ||
|
||
app_setting_states = "".join( | ||
f"{app_setting}: {current_vars[app_setting]} " | ||
for app_setting in python_specific_settings | ||
if app_setting in current_vars | ||
) | ||
|
||
# Special case for extensions | ||
if 'PYTHON_ENABLE_WORKER_EXTENSIONS' not in current_vars: | ||
if sys.version_info.minor == 9: | ||
app_setting_states += \ | ||
(f"{PYTHON_ENABLE_WORKER_EXTENSIONS}: " | ||
f"{str(PYTHON_ENABLE_WORKER_EXTENSIONS_DEFAULT_39)}") | ||
else: | ||
app_setting_states += \ | ||
(f"{PYTHON_ENABLE_WORKER_EXTENSIONS}: " | ||
f"{str(PYTHON_ENABLE_WORKER_EXTENSIONS_DEFAULT)} ") | ||
|
||
return app_setting_states |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
import collections as col | ||
import os | ||
|
||
from unittest.mock import patch | ||
|
||
from tests.utils import testutils | ||
from azure_functions_worker.utils.app_setting_manager import \ | ||
get_python_appsetting_state | ||
from azure_functions_worker.constants import PYTHON_THREADPOOL_THREAD_COUNT, \ | ||
PYTHON_ENABLE_DEBUG_LOGGING | ||
|
||
SysVersionInfo = col.namedtuple("VersionInfo", ["major", "minor", "micro", | ||
"releaselevel", "serial"]) | ||
DISPATCHER_FUNCTIONS_DIR = testutils.UNIT_TESTS_FOLDER / 'dispatcher_functions' | ||
DISPATCHER_STEIN_FUNCTIONS_DIR = testutils.UNIT_TESTS_FOLDER / \ | ||
'dispatcher_functions' / \ | ||
'dispatcher_functions_stein' | ||
DISPATCHER_STEIN_INVALID_FUNCTIONS_DIR = testutils.UNIT_TESTS_FOLDER / \ | ||
'broken_functions' / \ | ||
'invalid_stein' | ||
|
||
|
||
class TestDefaultAppSettingsLogs(testutils.AsyncTestCase): | ||
"""Tests for default app settings logs.""" | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
cls._ctrl = testutils.start_mockhost( | ||
script_root=DISPATCHER_FUNCTIONS_DIR) | ||
os_environ = os.environ.copy() | ||
cls._patch_environ = patch.dict('os.environ', os_environ) | ||
cls._patch_environ.start() | ||
super().setUpClass() | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
super().tearDownClass() | ||
cls._patch_environ.stop() | ||
|
||
async def test_initialize_worker_logging(self): | ||
"""Test if the dispatcher's log can be flushed out during worker | ||
initialization | ||
""" | ||
async with self._ctrl as host: | ||
r = await host.init_worker('3.0.12345') | ||
self.assertTrue('App Settings state: ' in log for log in r.logs) | ||
self.assertTrue('PYTHON_ENABLE_WORKER_EXTENSIONS: ' | ||
in log for log in r.logs) | ||
|
||
def test_get_python_appsetting_state(self): | ||
app_setting_state = get_python_appsetting_state() | ||
expected_string = "PYTHON_ENABLE_WORKER_EXTENSIONS: " | ||
self.assertIn(expected_string, app_setting_state) | ||
|
||
|
||
class TestNonDefaultAppSettingsLogs(testutils.AsyncTestCase): | ||
"""Tests for non-default app settings logs.""" | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
cls._ctrl = testutils.start_mockhost( | ||
script_root=DISPATCHER_FUNCTIONS_DIR) | ||
os_environ = os.environ.copy() | ||
os_environ[PYTHON_THREADPOOL_THREAD_COUNT] = '20' | ||
os_environ[PYTHON_ENABLE_DEBUG_LOGGING] = '1' | ||
cls._patch_environ = patch.dict('os.environ', os_environ) | ||
cls._patch_environ.start() | ||
super().setUpClass() | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
super().tearDownClass() | ||
cls._patch_environ.stop() | ||
|
||
async def test_initialize_worker_logging(self): | ||
"""Test if the dispatcher's log can be flushed out during worker | ||
initialization | ||
""" | ||
async with self._ctrl as host: | ||
r = await host.init_worker('3.0.12345') | ||
self.assertTrue('App Settings state: ' in log for log in r.logs) | ||
self.assertTrue('PYTHON_THREADPOOL_THREAD_COUNT: ' | ||
in log for log in r.logs) | ||
self.assertTrue('PYTHON_ENABLE_DEBUG_LOGGING: ' | ||
in log for log in r.logs) | ||
|
||
def test_get_python_appsetting_state(self): | ||
app_setting_state = get_python_appsetting_state() | ||
self.assertIn("PYTHON_THREADPOOL_THREAD_COUNT: 20 ", app_setting_state) | ||
self.assertIn("PYTHON_ENABLE_DEBUG_LOGGING: 1 ", app_setting_state) | ||
self.assertIn("PYTHON_ENABLE_WORKER_EXTENSIONS: ", app_setting_state) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters