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

adding an option to use preparers as a back up plan #15945

Merged
merged 2 commits into from
Jan 8, 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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@
CosmosPSPreparer = functools.partial(
PowerShellPreparer, "tables",
tables_cosmos_account_name="fake_cosmos_account",
tables_primary_cosmos_account_key="fakecosmosaccountkey")
tables_primary_cosmos_account_key="fakecosmosaccountkey",
preparers=[
CachedResourceGroupPreparer(name_prefix="tablestest"),
CachedCosmosAccountPreparer(name_prefix="tablestest"),
]
)

# ------------------------------------------------------------------------------
SERVICES = {
Expand Down
42 changes: 29 additions & 13 deletions tools/azure-sdk-tools/devtools_testutils/powershell_preparer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def __init__(
client_kwargs=None,
random_name_enabled=False,
use_cache=True,
preparers=None,
**kwargs
):
super(PowerShellPreparer, self).__init__(
Expand All @@ -29,6 +30,7 @@ def __init__(
self.fake_values = {}
self.real_values = {}
self._set_secrets(**kwargs)
self._backup_preparers=preparers

def _set_secrets(self, **kwargs):
keys = kwargs.keys()
Expand All @@ -52,20 +54,34 @@ def create_resource(self, name, **kwargs):

if self.is_live:
self._set_mgmt_settings_real_values()
for key in self.needed_keys:
try:
for key in self.needed_keys:

scrubbed_value = self.fake_values[key]
if scrubbed_value:
self.real_values[key.lower()] = os.environ[key.upper()]
self.test_class_instance.scrubber.register_name_pair(
self.real_values[key.lower()],
scrubbed_value
)
else:
template = 'To pass a live ID you must provide the scrubbed value for recordings to \
prevent secrets from being written to files. {} was not given. For example: \
@PowerShellPreparer("schemaregistry", schemaregistry_endpoint="fake_endpoint.servicebus.windows.net")'
raise AzureTestError(template.format(key))
except KeyError as key_error:
if not self._backup_preparers:
raise key_error
Copy link
Member

Choose a reason for hiding this comment

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

Just raise, this is re-raising the latest exception automatically. I believe this is equivalent but convention when you mostly let it go is to just write raise. Though I wonder if the stacktrace is impacted when you put a variable name like key_error in between.


self.real_values = {}
create_kwargs = {}
for preparer in self._backup_preparers:
resource_name, vals = preparer._prepare_create_resource(self.test_class_instance, **create_kwargs)
# vals = preparer.create_resource(name, **create_kwargs)
self.real_values.update(vals)
if 'resource_group' in self.real_values.keys():
create_kwargs['resource_group'] = self.real_values['resource_group']

scrubbed_value = self.fake_values[key]
if scrubbed_value:
self.real_values[key.lower()] = os.environ[key.upper()]
self.test_class_instance.scrubber.register_name_pair(
self.real_values[key.lower()],
scrubbed_value
)
else:
template = 'To pass a live ID you must provide the scrubbed value for recordings to \
prevent secrets from being written to files. {} was not given. For example: \
@PowerShellPreparer("schemaregistry", schemaregistry_endpoint="fake_endpoint.servicebus.windows.net")'
raise AzureTestError(template.format(key))
return self.real_values

else:
Expand Down