Skip to content
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 5 commits into from
Apr 26, 2021
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
3 changes: 2 additions & 1 deletion sdk/keyvault/azure-keyvault-secrets/dev_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
-e ../azure-mgmt-keyvault
-e ../../../tools/azure-sdk-tools
../azure-keyvault-nspkg
aiohttp>=3.0; python_version >= '3.5'
aiohttp>=3.0; python_version >= '3.5'
parameterized>=0.7.3
63 changes: 63 additions & 0 deletions sdk/keyvault/azure-keyvault-secrets/tests/_test_case.py
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"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

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!

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")
Loading