Skip to content

Commit

Permalink
Use test proxy in quickstart templates (#28753)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
mccoyp and msyyc authored Feb 13, 2023
1 parent c53e865 commit 43e56be
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 15 deletions.
1 change: 1 addition & 0 deletions scripts/quickstart_tooling_dpg/template_ci/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
22 changes: 22 additions & 0 deletions scripts/quickstart_tooling_dpg/template_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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")
8 changes: 6 additions & 2 deletions scripts/quickstart_tooling_dpg/template_tests/test_smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 3 additions & 6 deletions scripts/quickstart_tooling_dpg/template_tests/testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 43e56be

Please sign in to comment.