-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
[Key Vault] Target multiple API versions with tests (secrets) #18183
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 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,63 @@ | ||
# ------------------------------------ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
# ------------------------------------ | ||
import functools | ||
|
||
from azure.keyvault.secrets import ApiVersion | ||
from azure.keyvault.secrets._shared import HttpChallengeCache | ||
from azure.keyvault.secrets._shared.client_base import DEFAULT_VERSION | ||
from devtools_testutils import AzureTestCase, PowerShellPreparer | ||
from parameterized import parameterized, param | ||
import pytest | ||
|
||
|
||
def client_setup(testcase_func): | ||
"""decorator that creates a client to be passed in to a test method""" | ||
@PowerShellPreparer("keyvault", azure_keyvault_url="https://vaultname.vault.azure.net") | ||
@functools.wraps(testcase_func) | ||
def wrapper(test_class_instance, azure_keyvault_url, api_version, **kwargs): | ||
test_class_instance._skip_if_not_configured(api_version) | ||
client = test_class_instance.create_client(azure_keyvault_url, api_version=api_version, **kwargs) | ||
|
||
if kwargs.get("is_async"): | ||
import asyncio | ||
|
||
coroutine = testcase_func(test_class_instance, client) | ||
loop = asyncio.get_event_loop() | ||
loop.run_until_complete(coroutine) | ||
else: | ||
testcase_func(test_class_instance, client) | ||
return wrapper | ||
|
||
|
||
def get_decorator(**kwargs): | ||
"""returns a test decorator for test parameterization""" | ||
params = [param(api_version=api_version, **kwargs) for api_version in ApiVersion] | ||
return functools.partial(parameterized.expand, params, name_func=suffixed_test_name) | ||
|
||
|
||
def suffixed_test_name(testcase_func, param_num, param): | ||
return "{}_{}".format(testcase_func.__name__, parameterized.to_safe_name(param.kwargs.get("api_version"))) | ||
|
||
|
||
class SecretsTestCase(AzureTestCase): | ||
def tearDown(self): | ||
HttpChallengeCache.clear() | ||
assert len(HttpChallengeCache._cache) == 0 | ||
super(SecretsTestCase, self).tearDown() | ||
|
||
def create_client(self, vault_uri, **kwargs): | ||
if kwargs.pop("is_async", False): | ||
from azure.keyvault.secrets.aio import SecretClient | ||
credential = self.get_credential(SecretClient, is_async=True) | ||
else: | ||
from azure.keyvault.secrets import SecretClient | ||
credential = self.get_credential(SecretClient) | ||
return self.create_client_from_credential( | ||
SecretClient, credential=credential, vault_url=vault_uri, **kwargs | ||
) | ||
|
||
def _skip_if_not_configured(self, api_version, **kwargs): | ||
if self.is_live and api_version != DEFAULT_VERSION: | ||
pytest.skip("This test only uses the default API version for live tests") |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you need
is_async
for anything else? If not, consider this: https://github.com/Azure/azure-sdk-for-python/blob/2794bfcc88aad415920c234a848701f2bcfeeb88/tools/azure-devtools/src/azure_devtools/scenario_tests/preparers.py#L145-155There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is_async
is also provided to create_client, but I'll keep this in mind!