From 43e56bef1267a914ccde9ee2b2e2431a235931a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?McCoy=20Pati=C3=B1o?= <39780829+mccoyp@users.noreply.github.com> Date: Mon, 13 Feb 2023 09:32:41 -0800 Subject: [PATCH] Use test proxy in quickstart templates (#28753) * Use test proxy tooling * update * annotation * Use string sanitizer load_dotenv() is already called elsewhere in test tooling, so this also removes the duplicate call to preserve expected behavior --------- Co-authored-by: Yuchao Yan --- .../quickstart_tooling_dpg/template_ci/ci.yml | 1 + .../template_tests/conftest.py | 22 +++++++++++++++++++ .../template_tests/test_smoke.py | 8 +++++-- .../template_tests/test_smoke_async.py | 7 ++++-- .../template_tests/testcase.py | 9 +++----- .../template_tests/testcase_async.py | 7 ++---- 6 files changed, 39 insertions(+), 15 deletions(-) create mode 100644 scripts/quickstart_tooling_dpg/template_tests/conftest.py diff --git a/scripts/quickstart_tooling_dpg/template_ci/ci.yml b/scripts/quickstart_tooling_dpg/template_ci/ci.yml index c259fdd6d2d8..1a25a0ebc9a5 100644 --- a/scripts/quickstart_tooling_dpg/template_ci/ci.yml +++ b/scripts/quickstart_tooling_dpg/template_ci/ci.yml @@ -28,6 +28,7 @@ extends: template: ../../eng/pipelines/templates/stages/archetype-sdk-client.yml parameters: ServiceDirectory: {{ service_name }} + TestProxy: true Artifacts: - name: {{ package_name }} safeName: {{ safe_name }} diff --git a/scripts/quickstart_tooling_dpg/template_tests/conftest.py b/scripts/quickstart_tooling_dpg/template_tests/conftest.py new file mode 100644 index 000000000000..73bf140c1abf --- /dev/null +++ b/scripts/quickstart_tooling_dpg/template_tests/conftest.py @@ -0,0 +1,22 @@ +# coding: utf-8 +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# ------------------------------------------------------------------------- +import os +import pytest + +from devtools_testutils import test_proxy, add_general_string_sanitizer + +# For more info about add_sanitizers, please refer to https://github.com/Azure/azure-sdk-for-python/blob/main/doc/dev/test_proxy_migration_guide.md#register-sanitizers +@pytest.fixture(scope="session", autouse=True) +def add_sanitizers(test_proxy): + subscription_id = os.environ.get("{{ test_prefix | upper }}_SUBSCRIPTION_ID", "00000000-0000-0000-0000-000000000000") + tenant_id = os.environ.get("{{ test_prefix | upper }}_TENANT_ID", "00000000-0000-0000-0000-000000000000") + client_id = os.environ.get("{{ test_prefix | upper }}_CLIENT_ID", "00000000-0000-0000-0000-000000000000") + client_secret = os.environ.get("{{ test_prefix | upper }}_CLIENT_SECRET", "00000000-0000-0000-0000-000000000000") + add_general_string_sanitizer(target=subscription_id, value="00000000-0000-0000-0000-000000000000") + add_general_string_sanitizer(target=tenant_id, value="00000000-0000-0000-0000-000000000000") + add_general_string_sanitizer(target=client_id, value="00000000-0000-0000-0000-000000000000") + add_general_string_sanitizer(target=client_secret, value="00000000-0000-0000-0000-000000000000") diff --git a/scripts/quickstart_tooling_dpg/template_tests/test_smoke.py b/scripts/quickstart_tooling_dpg/template_tests/test_smoke.py index 2d33bc8c38fe..0d9bf8d42535 100644 --- a/scripts/quickstart_tooling_dpg/template_tests/test_smoke.py +++ b/scripts/quickstart_tooling_dpg/template_tests/test_smoke.py @@ -4,12 +4,16 @@ # Licensed under the MIT License. See License.txt in the project root for # license information. # ------------------------------------------------------------------------- -from testcase import {{ test_prefix.capitalize() }}Test, {{ test_prefix.capitalize() }}PowerShellPreparer +from devtools_testutils import recorded_by_proxy +from testcase import {{ test_prefix.capitalize() }}Test, {{ test_prefix.capitalize() }}Preparer + +# For more info about how to write and run test, please refer to https://github.com/Azure/azure-sdk-for-python/wiki/Dataplane-Codegen-Quick-Start-for-Test class {{ test_prefix.capitalize() }}SmokeTest({{ test_prefix.capitalize() }}Test): - @{{ test_prefix.capitalize() }}PowerShellPreparer() + @{{ test_prefix.capitalize() }}Preparer() + @recorded_by_proxy def test_smoke(self, {{ test_prefix }}_endpoint): client = self.create_client(endpoint={{ test_prefix }}_endpoint) # test your code here, for example: diff --git a/scripts/quickstart_tooling_dpg/template_tests/test_smoke_async.py b/scripts/quickstart_tooling_dpg/template_tests/test_smoke_async.py index bede22a1bbdc..14952c1c85da 100644 --- a/scripts/quickstart_tooling_dpg/template_tests/test_smoke_async.py +++ b/scripts/quickstart_tooling_dpg/template_tests/test_smoke_async.py @@ -4,13 +4,16 @@ # Licensed under the MIT License. See License.txt in the project root for # license information. # ------------------------------------------------------------------------- -from testcase import {{ test_prefix.capitalize() }}PowerShellPreparer +from devtools_testutils.aio import recorded_by_proxy_async +from testcase import {{ test_prefix.capitalize() }}Preparer from testcase_async import {{ test_prefix.capitalize() }}AsyncTest +# For more info about how to write and run test, please refer to https://github.com/Azure/azure-sdk-for-python/wiki/Dataplane-Codegen-Quick-Start-for-Test class {{ test_prefix.capitalize() }}SmokeAsyncTest({{ test_prefix.capitalize() }}AsyncTest): - @{{ test_prefix.capitalize() }}PowerShellPreparer() + @{{ test_prefix.capitalize() }}Preparer() + @recorded_by_proxy_async async def test_smoke_async(self, {{ test_prefix }}_endpoint): client = self.create_client(endpoint={{ test_prefix }}_endpoint) # test your code here, for example: diff --git a/scripts/quickstart_tooling_dpg/template_tests/testcase.py b/scripts/quickstart_tooling_dpg/template_tests/testcase.py index 8b78e39a36a1..ab39c725d779 100644 --- a/scripts/quickstart_tooling_dpg/template_tests/testcase.py +++ b/scripts/quickstart_tooling_dpg/template_tests/testcase.py @@ -5,14 +5,11 @@ # license information. # -------------------------------------------------------------------------- import functools -from devtools_testutils import AzureTestCase, PowerShellPreparer +from devtools_testutils import AzureRecordedTestCase, PowerShellPreparer from {{ namespace }} import {{ client_name }} -class {{ test_prefix.capitalize() }}Test(AzureTestCase): - def __init__(self, method_name, **kwargs): - super({{ test_prefix.capitalize() }}Test, self).__init__(method_name, **kwargs) - +class {{ test_prefix.capitalize() }}Test(AzureRecordedTestCase): def create_client(self, endpoint): credential = self.get_credential({{ client_name }}) return self.create_client_from_credential( @@ -22,7 +19,7 @@ def create_client(self, endpoint): ) -{{ test_prefix.capitalize() }}PowerShellPreparer = functools.partial( +{{ test_prefix.capitalize() }}Preparer = functools.partial( PowerShellPreparer, "{{ test_prefix }}", {{ test_prefix }}_endpoint="https://myservice.azure.com" diff --git a/scripts/quickstart_tooling_dpg/template_tests/testcase_async.py b/scripts/quickstart_tooling_dpg/template_tests/testcase_async.py index c120f7aa3a22..525ebac9dc1f 100644 --- a/scripts/quickstart_tooling_dpg/template_tests/testcase_async.py +++ b/scripts/quickstart_tooling_dpg/template_tests/testcase_async.py @@ -4,14 +4,11 @@ # Licensed under the MIT License. See License.txt in the project root for # license information. # -------------------------------------------------------------------------- -from devtools_testutils import AzureTestCase +from devtools_testutils import AzureRecordedTestCase from {{ namespace }}.aio import {{ client_name}} -class {{ test_prefix.capitalize() }}AsyncTest(AzureTestCase): - def __init__(self, method_name, **kwargs): - super({{ test_prefix.capitalize() }}AsyncTest, self).__init__(method_name, **kwargs) - +class {{ test_prefix.capitalize() }}AsyncTest(AzureRecordedTestCase): def create_client(self, endpoint): credential = self.get_credential({{ client_name}}, is_async=True) return self.create_client_from_credential(