-
Notifications
You must be signed in to change notification settings - Fork 107
Change current working directory when reloading environment variables #613
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7541899
Update environment reload logic
9b11712
Add feature flag control
6476eb7
Added two more test cases
5f3a0fb
Fix linting
1a4a73b
Address CR issues
4bfb264
Add extension bundle for e2e test
32653ef
Retrigger CI
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
Empty file.
This file contains hidden or 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,15 @@ | ||
import os | ||
|
||
|
||
def is_true_like(setting: str): | ||
if setting is None: | ||
return False | ||
|
||
return setting.lower().strip() in ['1', 'true', 't', 'yes', 'y'] | ||
|
||
|
||
def is_envvar_true(env_key: str): | ||
if os.getenv(env_key) is None: | ||
return False | ||
|
||
return is_true_like(os.environ[env_key]) |
This file contains hidden or 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,21 @@ | ||
from .common import is_envvar_true | ||
|
||
|
||
def enable_feature_by(flag: str, default=None): | ||
def decorate(func): | ||
def call(*args, **kwargs): | ||
if is_envvar_true(flag): | ||
return func(*args, **kwargs) | ||
return default | ||
return call | ||
return decorate | ||
|
||
|
||
def disable_feature_by(flag: str, default=None): | ||
def decorate(func): | ||
def call(*args, **kwargs): | ||
if not is_envvar_true(flag): | ||
return func(*args, **kwargs) | ||
return default | ||
return call | ||
return decorate |
This file contains hidden or 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,15 @@ | ||
{ | ||
"scriptFile": "sub_module/main.py", | ||
"bindings": [ | ||
{ | ||
"type": "httpTrigger", | ||
"direction": "in", | ||
"name": "req" | ||
}, | ||
{ | ||
"type": "http", | ||
"direction": "out", | ||
"name": "$return" | ||
} | ||
] | ||
} |
This file contains hidden or 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 @@ | ||
MODULE_NAME = 'PARENTMODULE' |
Empty file.
5 changes: 5 additions & 0 deletions
5
tests/unittests/load_functions/parentmodule/sub_module/main.py
This file contains hidden or 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,5 @@ | ||
from .. import module | ||
|
||
|
||
def main(req) -> str: | ||
return module.__name__ |
This file contains hidden or 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,15 @@ | ||
{ | ||
"scriptFile": "main.py", | ||
"bindings": [ | ||
{ | ||
"type": "httpTrigger", | ||
"direction": "in", | ||
"name": "req" | ||
}, | ||
{ | ||
"type": "http", | ||
"direction": "out", | ||
"name": "$return" | ||
} | ||
] | ||
} |
This file contains hidden or 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,5 @@ | ||
from .sub_module import module | ||
|
||
|
||
def main(req) -> str: | ||
return module.__name__ |
Empty file.
This file contains hidden or 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 @@ | ||
MODULE_NAME = 'SUB_MODULE' |
This file contains hidden or 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 hidden or 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 hidden or 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,122 @@ | ||
import os | ||
import unittest | ||
import typing | ||
|
||
from azure_functions_worker.utils import common, wrappers | ||
|
||
|
||
TEST_FEATURE_FLAG = "APP_SETTING_FEATURE_FLAG" | ||
FEATURE_DEFAULT = 42 | ||
|
||
|
||
class MockFeature: | ||
@wrappers.enable_feature_by(TEST_FEATURE_FLAG) | ||
def mock_feature_enabled(self, output: typing.List[str]) -> str: | ||
result = 'mock_feature_enabled' | ||
output.append(result) | ||
return result | ||
|
||
@wrappers.disable_feature_by(TEST_FEATURE_FLAG) | ||
def mock_feature_disabled(self, output: typing.List[str]) -> str: | ||
result = 'mock_feature_disabled' | ||
output.append(result) | ||
return result | ||
|
||
@wrappers.enable_feature_by(TEST_FEATURE_FLAG, FEATURE_DEFAULT) | ||
def mock_feature_default(self, output: typing.List[str]) -> str: | ||
result = 'mock_feature_default' | ||
output.append(result) | ||
return result | ||
|
||
|
||
class TestUtilities(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self._pre_env = dict(os.environ) | ||
|
||
def tearDown(self): | ||
os.environ.clear() | ||
os.environ.update(self._pre_env) | ||
|
||
def test_is_true_like_accepted(self): | ||
self.assertTrue(common.is_true_like('1')) | ||
self.assertTrue(common.is_true_like('true')) | ||
self.assertTrue(common.is_true_like('T')) | ||
self.assertTrue(common.is_true_like('YES')) | ||
self.assertTrue(common.is_true_like('y')) | ||
|
||
def test_is_true_like_rejected(self): | ||
self.assertFalse(common.is_true_like(None)) | ||
self.assertFalse(common.is_true_like('')) | ||
self.assertFalse(common.is_true_like('secret')) | ||
|
||
def test_is_envvar_true(self): | ||
os.environ[TEST_FEATURE_FLAG] = 'true' | ||
self.assertTrue(common.is_envvar_true(TEST_FEATURE_FLAG)) | ||
|
||
def test_is_envvar_not_true_on_unset(self): | ||
self._unset_feature_flag() | ||
self.assertFalse(common.is_envvar_true(TEST_FEATURE_FLAG)) | ||
|
||
def test_disable_feature_with_no_feature_flag(self): | ||
mock_feature = MockFeature() | ||
output = [] | ||
result = mock_feature.mock_feature_enabled(output) | ||
self.assertIsNone(result) | ||
self.assertListEqual(output, []) | ||
|
||
def test_enable_feature_with_feature_flag(self): | ||
feature_flag = TEST_FEATURE_FLAG | ||
os.environ[feature_flag] = '1' | ||
mock_feature = MockFeature() | ||
output = [] | ||
result = mock_feature.mock_feature_enabled(output) | ||
self.assertEqual(result, 'mock_feature_enabled') | ||
self.assertListEqual(output, ['mock_feature_enabled']) | ||
|
||
def test_enable_feature_with_no_rollback_flag(self): | ||
mock_feature = MockFeature() | ||
output = [] | ||
result = mock_feature.mock_feature_disabled(output) | ||
self.assertEqual(result, 'mock_feature_disabled') | ||
self.assertListEqual(output, ['mock_feature_disabled']) | ||
|
||
def test_disable_feature_with_rollback_flag(self): | ||
rollback_flag = TEST_FEATURE_FLAG | ||
os.environ[rollback_flag] = '1' | ||
mock_feature = MockFeature() | ||
output = [] | ||
result = mock_feature.mock_feature_disabled(output) | ||
self.assertIsNone(result) | ||
self.assertListEqual(output, []) | ||
|
||
def test_enable_feature_with_rollback_flag_is_false(self): | ||
rollback_flag = TEST_FEATURE_FLAG | ||
os.environ[rollback_flag] = 'false' | ||
mock_feature = MockFeature() | ||
output = [] | ||
result = mock_feature.mock_feature_disabled(output) | ||
self.assertEqual(result, 'mock_feature_disabled') | ||
self.assertListEqual(output, ['mock_feature_disabled']) | ||
|
||
def test_fail_to_enable_feature_return_default_value(self): | ||
mock_feature = MockFeature() | ||
output = [] | ||
result = mock_feature.mock_feature_default(output) | ||
self.assertEqual(result, FEATURE_DEFAULT) | ||
self.assertListEqual(output, []) | ||
|
||
def test_disable_feature_with_false_flag_return_default_value(self): | ||
feature_flag = TEST_FEATURE_FLAG | ||
os.environ[feature_flag] = 'false' | ||
mock_feature = MockFeature() | ||
output = [] | ||
result = mock_feature.mock_feature_default(output) | ||
self.assertEqual(result, FEATURE_DEFAULT) | ||
self.assertListEqual(output, []) | ||
|
||
def _unset_feature_flag(self): | ||
try: | ||
os.environ.pop(TEST_FEATURE_FLAG) | ||
except KeyError: | ||
pass |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.