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] Update tests which disable soft-delete #15352

Merged
merged 6 commits into from
Nov 18, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,26 @@ def setUp(self):
self.list_test_size = 7
super(KeyVaultTestCase, self).setUp()

async def _poll_until_no_exception(self, fn, *resource_names, expected_exception, max_retries=20, retry_delay=3):
async def _poll_until_no_exception(self, fn, expected_exception, max_retries=20, retry_delay=3):
"""polling helper for live tests because some operations take an unpredictable amount of time to complete"""

for name in resource_names:
for i in range(max_retries):
try:
# TODO: better for caller to apply args to fn; could also gather
await fn(name)
break
except expected_exception:
if i == max_retries - 1:
raise
if self.is_live:
await asyncio.sleep(retry_delay)

async def _poll_until_exception(self, fn, *resource_names, expected_exception, max_retries=20, retry_delay=3):
for i in range(max_retries):
try:
return await fn()
except expected_exception:
if i == max_retries - 1:
raise
if self.is_live:
await asyncio.sleep(retry_delay)

async def _poll_until_exception(self, fn, expected_exception, max_retries=20, retry_delay=3):
"""polling helper for live tests because some operations take an unpredictable amount of time to complete"""

for name in resource_names:
for _ in range(max_retries):
try:
# TODO: better for caller to apply args to fn; could also gather
await fn(name)
if self.is_live:
await asyncio.sleep(retry_delay)
except expected_exception:
return
for _ in range(max_retries):
try:
await fn()
if self.is_live:
await asyncio.sleep(retry_delay)
except expected_exception:
return
self.fail("expected exception {expected_exception} was not raised")

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import time
import pytest

from azure.core.exceptions import ResourceExistsError
from azure_devtools.scenario_tests import RecordingProcessor, RequestUrlNormalizer

from azure.keyvault.certificates import (
Expand Down Expand Up @@ -480,7 +481,7 @@ def test_get_pending_certificate_signing_request(self, client, **kwargs):
self.assertEqual(client.get_certificate_operation(certificate_name=cert_name).csr, pending_version_csr)

@ResourceGroupPreparer(random_name_enabled=True)
@KeyVaultPreparer(enable_soft_delete=False)
@KeyVaultPreparer()
@KeyVaultClientPreparer()
def test_backup_restore(self, client, **kwargs):
policy = CertificatePolicy.get_default()
Expand All @@ -496,8 +497,12 @@ def test_backup_restore(self, client, **kwargs):
# delete the certificate
client.begin_delete_certificate(certificate_name=cert_name).wait()

# purge the certificate
client.purge_deleted_certificate(certificate_name=cert_name)

# restore certificate
restored_certificate = client.restore_certificate_backup(backup=certificate_backup)
restore_function = functools.partial(client.restore_certificate_backup, certificate_backup)
restored_certificate = self._poll_until_no_exception(restore_function, ResourceExistsError)
self._validate_certificate_bundle(cert=restored_certificate, cert_name=cert_name, cert_policy=policy)

@ResourceGroupPreparer(random_name_enabled=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging
import json

from azure.core.exceptions import ResourceExistsError
from azure_devtools.scenario_tests import RecordingProcessor
from azure.keyvault.certificates import (
AdministratorContact,
Expand Down Expand Up @@ -492,7 +493,7 @@ async def test_get_pending_certificate_signing_request(self, client, **kwargs):
self.assertEqual((await client.get_certificate_operation(certificate_name=cert_name)).csr, pending_version_csr)

@ResourceGroupPreparer(random_name_enabled=True)
@KeyVaultPreparer(enable_soft_delete=False)
@KeyVaultPreparer()
@KeyVaultClientPreparer()
async def test_backup_restore(self, client, **kwargs):
cert_name = self.get_resource_name("cert")
Expand All @@ -508,8 +509,14 @@ async def test_backup_restore(self, client, **kwargs):
# delete the certificate
await client.delete_certificate(certificate_name=cert_name)

# purge the certificate
await client.purge_deleted_certificate(certificate_name=cert_name)

# restore certificate
restored_certificate = await client.restore_certificate_backup(backup=certificate_backup)
restore_function = functools.partial(client.restore_certificate_backup, certificate_backup)
restored_certificate = await self._poll_until_no_exception(
restore_function, expected_exception=ResourceExistsError
)
self._validate_certificate_bundle(cert=restored_certificate, cert_name=cert_name, cert_policy=policy)

@ResourceGroupPreparer(random_name_enabled=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# ------------------------------------
from __future__ import print_function
import functools
import time

from azure.keyvault.certificates import (
CertificateClient,
Expand All @@ -28,14 +29,12 @@ def test_create_certificate_client():
vault_url = "vault_url"
# pylint:disable=unused-variable
# [START create_certificate_client]

from azure.identity import DefaultAzureCredential
from azure.keyvault.certificates import CertificateClient

# Create a CertificateClient using default Azure credentials
credential = DefaultAzureCredential()
certificate_client = CertificateClient(vault_url=vault_url, credential=credential)

# [END create_certificate_client]


Expand Down Expand Up @@ -77,33 +76,27 @@ def test_example_certificate_crud_operations(self, client, **kwargs):
print(certificate.id)
print(certificate.name)
print(certificate.policy.issuer_name)

# [END create_certificate]

# [START get_certificate]

# get the certificate
certificate = certificate_client.get_certificate(cert_name)

print(certificate.id)
print(certificate.name)
print(certificate.policy.issuer_name)

# [END get_certificate]

version = certificate.properties.version

# [START get_certificate_version]

certificate = certificate_client.get_certificate_version(cert_name, version)

print(certificate.id)
print(certificate.properties.version)

# [END get_certificate_version]

# [START update_certificate]

# update attributes of an existing certificate
tags = {"foo": "updated tag"}
updated_certificate = certificate_client.update_certificate_properties(
Expand All @@ -113,10 +106,9 @@ def test_example_certificate_crud_operations(self, client, **kwargs):
print(updated_certificate.properties.version)
print(updated_certificate.properties.updated_on)
print(updated_certificate.properties.tags)

# [END update_certificate]
# [START delete_certificate]

# [START delete_certificate]
# delete a certificate
deleted_certificate = certificate_client.begin_delete_certificate(certificate.name).result()

Expand All @@ -127,7 +119,6 @@ def test_example_certificate_crud_operations(self, client, **kwargs):
print(deleted_certificate.deleted_on)
print(deleted_certificate.scheduled_purge_date)
print(deleted_certificate.recovery_id)

# [END delete_certificate]

@ResourceGroupPreparer(random_name_enabled=True)
Expand All @@ -153,7 +144,6 @@ def test_example_certificate_list_operations(self, client, **kwargs):
certificate_client.begin_create_certificate(certificate_name, cert_policy).wait()

# [START list_properties_of_certificates]

# get an iterator of certificates
certificates = certificate_client.list_properties_of_certificates()

Expand All @@ -163,28 +153,24 @@ def test_example_certificate_list_operations(self, client, **kwargs):
print(certificate.name)
print(certificate.updated_on)
print(certificate.enabled)

# [END list_properties_of_certificates]

# create a second version of the cert
certificate_client.begin_create_certificate(certificate_name, cert_policy).wait()

# [START list_properties_of_certificate_versions]

# get an iterator of a certificate's versions
certificate_versions = certificate_client.list_properties_of_certificate_versions(certificate_name)

for certificate in certificate_versions:
print(certificate.id)
print(certificate.updated_on)
print(certificate.version)

# [END list_properties_of_certificate_versions]

certificate_client.begin_delete_certificate(certificate_name).wait()

# [START list_deleted_certificates]

# get an iterator of deleted certificates (requires soft-delete enabled for the vault)
deleted_certificates = certificate_client.list_deleted_certificates()

Expand All @@ -194,11 +180,10 @@ def test_example_certificate_list_operations(self, client, **kwargs):
print(certificate.deleted_on)
print(certificate.scheduled_purge_date)
print(certificate.deleted_on)

# [END list_deleted_certificates]

@ResourceGroupPreparer(random_name_enabled=True)
@KeyVaultPreparer(enable_soft_delete=False)
@KeyVaultPreparer()
@KeyVaultClientPreparer()
def test_example_certificate_backup_restore(self, client, **kwargs):
certificate_client = client
Expand All @@ -220,26 +205,26 @@ def test_example_certificate_backup_restore(self, client, **kwargs):
certificate_client.begin_create_certificate(certificate_name=cert_name, policy=cert_policy).wait()

# [START backup_certificate]

# backup certificate
certificate_backup = certificate_client.backup_certificate(cert_name)

# returns the raw bytes of the backed up certificate
print(certificate_backup)

# [END backup_certificate]

certificate_client.begin_delete_certificate(certificate_name=cert_name).wait()
certificate_client.purge_deleted_certificate(certificate_name=cert_name)

# [START restore_certificate]
if self.is_live:
time.sleep(15)

# [START restore_certificate]
# restore a certificate backup
restored_certificate = certificate_client.restore_certificate_backup(certificate_backup)

print(restored_certificate.id)
print(restored_certificate.name)
print(restored_certificate.properties.version)

# [END restore_certificate]

@ResourceGroupPreparer(random_name_enabled=True)
Expand Down Expand Up @@ -268,7 +253,6 @@ def test_example_certificate_recover(self, client, **kwargs):

certificate_client.begin_delete_certificate(certificate_name=cert_name).wait()
# [START get_deleted_certificate]

# get a deleted certificate (requires soft-delete enabled for the vault)
deleted_certificate = certificate_client.get_deleted_certificate(cert_name)
print(deleted_certificate.name)
Expand All @@ -278,16 +262,14 @@ def test_example_certificate_recover(self, client, **kwargs):
print(deleted_certificate.deleted_on)
print(deleted_certificate.scheduled_purge_date)
print(deleted_certificate.recovery_id)

# [END get_deleted_certificate]
# [START recover_deleted_certificate]

# [START recover_deleted_certificate]
# recover a deleted certificate to its latest version (requires soft-delete enabled for the vault)
recovered_certificate = certificate_client.begin_recover_deleted_certificate(cert_name).result()

print(recovered_certificate.id)
print(recovered_certificate.name)

# [END recover_deleted_certificate]

@ResourceGroupPreparer(random_name_enabled=True)
Expand All @@ -310,30 +292,25 @@ def test_example_contacts(self, client, **kwargs):
print(contact.name)
print(contact.email)
print(contact.phone)

# [END set_contacts]

# [START get_contacts]

contacts = certificate_client.get_contacts()

# Loop through the certificate contacts for this key vault.
for contact in contacts:
print(contact.name)
print(contact.email)
print(contact.phone)

# [END get_contacts]

# [START delete_contacts]

deleted_contacts = certificate_client.delete_contacts()

for deleted_contact in deleted_contacts:
print(deleted_contact.name)
print(deleted_contact.email)
print(deleted_contact.phone)

# [END delete_contacts]

@ResourceGroupPreparer(random_name_enabled=True)
Expand Down Expand Up @@ -367,11 +344,9 @@ def test_example_issuers(self, client, **kwargs):
print(contact.last_name)
print(contact.email)
print(contact.phone)

# [END create_issuer]

# [START get_issuer]

issuer = certificate_client.get_issuer("issuer1")

print(issuer.name)
Expand All @@ -383,25 +358,21 @@ def test_example_issuers(self, client, **kwargs):
print(contact.last_name)
print(contact.email)
print(contact.phone)

# [END get_issuer]

certificate_client.create_issuer(
issuer_name="issuer2", provider="Test", account_id="keyvaultuser", enabled=True
)

# [START list_properties_of_issuers]

issuers = certificate_client.list_properties_of_issuers()

for issuer in issuers:
print(issuer.name)
print(issuer.provider)

# [END list_properties_of_issuers]

# [START delete_issuer]

deleted_issuer = certificate_client.delete_issuer("issuer1")

print(deleted_issuer.name)
Expand All @@ -413,5 +384,4 @@ def test_example_issuers(self, client, **kwargs):
print(contact.last_name)
print(contact.email)
print(contact.phone)

# [END delete_issuer]
Loading