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 (keys) #18149

Merged
merged 9 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 22 additions & 2 deletions sdk/keyvault/azure-keyvault-keys/tests/_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,33 @@
import os

from azure.keyvault.keys._shared import HttpChallengeCache
from azure.keyvault.keys._shared.client_base import ApiVersion, DEFAULT_VERSION
from devtools_testutils import AzureTestCase
from parameterized import parameterized
import pytest
from six.moves.urllib_parse import urlparse


def _get_test_parameters():
combinations = []
hsm_supported_versions = {ApiVersion.V7_2_preview}
for api_version in ApiVersion:
if api_version in hsm_supported_versions:
combinations.append([api_version, True])
combinations.append([api_version, False])
return combinations


def suffixed_test_name(testcase_func, param_num, param):
api_version = param.kwargs.get("api_version")
suffix = "mhsm" if param.kwargs.get("is_hsm") else "vault"
return "{}_{}".format(testcase_func.__name__, parameterized.to_safe_name(suffix))
return "{}_{}_{}".format(
testcase_func.__name__, parameterized.to_safe_name(api_version), parameterized.to_safe_name(suffix)
)


# parameters for test case parameterization, where [x, y] = [api_version, is_hsm]
PARAMETER_COMBINATIONS = _get_test_parameters()
mccoyp marked this conversation as resolved.
Show resolved Hide resolved


class KeysTestCase(AzureTestCase):
Expand Down Expand Up @@ -69,6 +87,8 @@ def _set_mgmt_settings_real_values(self):
os.environ["AZURE_CLIENT_ID"] = os.environ["KEYVAULT_CLIENT_ID"]
os.environ["AZURE_CLIENT_SECRET"] = os.environ["KEYVAULT_CLIENT_SECRET"]

def _skip_if_not_configured(self, is_hsm):
def _skip_if_not_configured(self, api_version, is_hsm):
if self.is_live and api_version != DEFAULT_VERSION:
pytest.skip("This test only uses the default API version for live tests")
if self.is_live and is_hsm and self.managed_hsm_url is None:
pytest.skip("No HSM endpoint for live testing")
131 changes: 80 additions & 51 deletions sdk/keyvault/azure-keyvault-keys/tests/test_crypto_client.py

Large diffs are not rendered by default.

131 changes: 80 additions & 51 deletions sdk/keyvault/azure-keyvault-keys/tests/test_crypto_client_async.py

Large diffs are not rendered by default.

53 changes: 16 additions & 37 deletions sdk/keyvault/azure-keyvault-keys/tests/test_examples_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,24 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import functools

from azure.keyvault.keys import KeyClient
from azure.keyvault.keys import ApiVersion
from azure.keyvault.keys.crypto import CryptographyClient
from azure.keyvault.keys._shared import HttpChallengeCache
from devtools_testutils import PowerShellPreparer

from _shared.test_case import KeyVaultTestCase
from parameterized import parameterized, param

KeyVaultPreparer = functools.partial(
PowerShellPreparer,
"keyvault",
azure_keyvault_url="https://vaultname.vault.azure.net"
)
from _test_case import KeysTestCase, suffixed_test_name


class TestCryptoExamples(KeyVaultTestCase):
class TestCryptoExamples(KeysTestCase):
def __init__(self, *args, **kwargs):
kwargs["match_body"] = False
super(TestCryptoExamples, self).__init__(*args, **kwargs)

def tearDown(self):
HttpChallengeCache.clear()
assert len(HttpChallengeCache._cache) == 0
super(TestCryptoExamples, self).tearDown()

def create_key_client(self, vault_uri, **kwargs):
credential = self.get_credential(KeyClient)
return self.create_client_from_credential(KeyClient, credential=credential, vault_url=vault_uri, **kwargs)

def get_crypto_client_credential(self):
return self.get_credential(CryptographyClient)

# pylint:disable=unused-variable

@KeyVaultPreparer()
def test_encrypt_decrypt(self, azure_keyvault_url, **kwargs):
key_client = self.create_key_client(azure_keyvault_url)
credential = self.get_crypto_client_credential()
@parameterized.expand([param(api_version=api_version) for api_version in ApiVersion], name_func=suffixed_test_name)
def test_encrypt_decrypt(self, **kwargs):
key_client = self.create_key_client(self.vault_url, **kwargs)
credential = self.get_credential(CryptographyClient)
key_name = self.get_resource_name("crypto-test-encrypt-key")
key_client.create_rsa_key(key_name)

Expand Down Expand Up @@ -73,10 +52,10 @@ def test_encrypt_decrypt(self, azure_keyvault_url, **kwargs):
print(result.plaintext)
# [END decrypt]

@KeyVaultPreparer()
def test_wrap_unwrap(self, azure_keyvault_url, **kwargs):
key_client = self.create_key_client(azure_keyvault_url)
credential = self.get_crypto_client_credential()
@parameterized.expand([param(api_version=api_version) for api_version in ApiVersion], name_func=suffixed_test_name)
def test_wrap_unwrap(self, **kwargs):
key_client = self.create_key_client(self.vault_url, **kwargs)
credential = self.get_credential(CryptographyClient)
key_name = self.get_resource_name("crypto-test-wrapping-key")
key = key_client.create_rsa_key(key_name)
client = CryptographyClient(key, credential)
Expand All @@ -100,10 +79,10 @@ def test_wrap_unwrap(self, azure_keyvault_url, **kwargs):
key = result.key
# [END unwrap_key]

@KeyVaultPreparer()
def test_sign_verify(self, azure_keyvault_url, **kwargs):
key_client = self.create_key_client(azure_keyvault_url)
credential = self.get_crypto_client_credential()
@parameterized.expand([param(api_version=api_version) for api_version in ApiVersion], name_func=suffixed_test_name)
def test_sign_verify(self, **kwargs):
key_client = self.create_key_client(self.vault_url, **kwargs)
credential = self.get_credential(CryptographyClient)
key_name = self.get_resource_name("crypto-test-wrapping-key")
key = key_client.create_rsa_key(key_name)
client = CryptographyClient(key, credential)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,24 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import functools

from azure.keyvault.keys.aio import KeyClient
from azure.keyvault.keys import ApiVersion
from azure.keyvault.keys.crypto.aio import CryptographyClient
from azure.keyvault.keys._shared import HttpChallengeCache
from devtools_testutils import PowerShellPreparer
from _shared.test_case_async import KeyVaultTestCase
from parameterized import parameterized, param

KeyVaultPreparer = functools.partial(
PowerShellPreparer,
"keyvault",
azure_keyvault_url="https://vaultname.vault.azure.net"
)
from _test_case import KeysTestCase, suffixed_test_name


class TestCryptoExamples(KeyVaultTestCase):
class TestCryptoExamples(KeysTestCase):
def __init__(self, *args, **kwargs):
kwargs["match_body"] = False
super(TestCryptoExamples, self).__init__(*args, **kwargs)

def tearDown(self):
HttpChallengeCache.clear()
assert len(HttpChallengeCache._cache) == 0
super(TestCryptoExamples, self).tearDown()

def create_key_client(self, vault_uri, **kwargs):
credential = self.get_credential(KeyClient, is_async=True)
return self.create_client_from_credential(KeyClient, credential=credential, vault_url=vault_uri, **kwargs)

def get_crypto_client_credential(self):
return self.get_credential(CryptographyClient, is_async=True)

# pylint:disable=unused-variable

@KeyVaultPreparer()
async def test_encrypt_decrypt_async(self, azure_keyvault_url, **kwargs):
key_client = self.create_key_client(azure_keyvault_url)
credential = self.get_crypto_client_credential()
@parameterized.expand([param(api_version=api_version) for api_version in ApiVersion], name_func=suffixed_test_name)
@PowerShellPreparer("keyvault")
async def test_encrypt_decrypt_async(self, **kwargs):
key_client = self.create_key_client(self.vault_url, is_async=True, **kwargs)
credential = self.get_credential(CryptographyClient, is_async=True)
key_name = self.get_resource_name("crypto-test-encrypt-key")
await key_client.create_rsa_key(key_name)

Expand Down Expand Up @@ -77,10 +57,11 @@ async def test_encrypt_decrypt_async(self, azure_keyvault_url, **kwargs):
print(result.plaintext)
# [END decrypt]

@KeyVaultPreparer()
async def test_wrap_unwrap_async(self, azure_keyvault_url, **kwargs):
key_client = self.create_key_client(azure_keyvault_url)
credential = self.get_crypto_client_credential()
@parameterized.expand([param(api_version=api_version) for api_version in ApiVersion], name_func=suffixed_test_name)
@PowerShellPreparer("keyvault")
async def test_wrap_unwrap_async(self, **kwargs):
key_client = self.create_key_client(self.vault_url, is_async=True, **kwargs)
credential = self.get_credential(CryptographyClient, is_async=True)
key_name = self.get_resource_name("crypto-test-wrapping-key")
key = await key_client.create_rsa_key(key_name)
client = CryptographyClient(key, credential)
Expand All @@ -103,10 +84,11 @@ async def test_wrap_unwrap_async(self, azure_keyvault_url, **kwargs):
result = await client.unwrap_key(KeyWrapAlgorithm.rsa_oaep, encrypted_key)
# [END unwrap_key]

@KeyVaultPreparer()
async def test_sign_verify_async(self, azure_keyvault_url, **kwargs):
key_client = self.create_key_client(azure_keyvault_url)
credential = self.get_crypto_client_credential()
@parameterized.expand([param(api_version=api_version) for api_version in ApiVersion], name_func=suffixed_test_name)
@PowerShellPreparer("keyvault")
async def test_sign_verify_async(self, **kwargs):
key_client = self.create_key_client(self.vault_url, is_async=True, **kwargs)
credential = self.get_credential(CryptographyClient, is_async=True)
key_name = self.get_resource_name("crypto-test-wrapping-key")
key = await key_client.create_rsa_key(key_name)
client = CryptographyClient(key, credential)
Expand Down
Loading