Skip to content

Dataplane Codegen Quick Start for Test

Yuchao Yan edited this page Aug 22, 2023 · 27 revisions

Overview

The doc will show how to add testcase for Dataplane Codegen SDK quickly.

Setup your development environment

It is recommended to do your development work in Python3

C:\Users> python -m venv venv-sdk
C:\Users> venv-sdk\scripts\Activate.ps1       # PowerShell only
C:\Users> source venv-sdk/bin/activate    # Linux shell (Bash, ZSH, etc.) only
C:\Users> venv-sdk\scripts\activate.bat   # Windows CMD only
(venv-sdk)C:\Users>

Dependency installation

Our SDK will have dependencies on other packages in the Azure Python SDK ecosystem. In order to run our tests and samples, we will need to setup our virtual environment to be able to find these external dependencies within the repo. We use the dev_requirements.txt (template) to list these dependencies as relative paths (along with any other external packages that should be installed from Pypi). The libraries currently listed in this file include azure-core and azure-identity as well as some internal tooling packages and our testing framework libraries. These dependencies can be installed with the following command:

(venv-sdk)azure-sdk-for-python\sdk\my-directory\my-library> pip install pytest
(venv-sdk)azure-sdk-for-python\sdk\my-directory\my-library> pip install -r dev_requirements.txt

Next we will install our Python SDK to the virtual environment as an 'editable install' - this means that as we work on the implementation, we will be able to run the package as it develops, as opposed to having to periodically rebuild and reinstall.

(venv-sdk)azure-sdk-for-python\sdk\my-directory\my-library> pip install -e .

Prepare credentials

Prepare subcription_id, tenant_id, client_id and client_secret through Azure portal which is necessary to run live test.

Writing New Tests

In the tests directory create a file with the naming pattern test_<what_you_are_testing>.py. The base of each testing file will be roughly the same:

  1. Create Live Resources in Azure portal

  2. Write your test with the following structure

import functools
import pytest

from devtools_testutils import AzureRecordedTestCase, PowerShellPreparer, recorded_by_proxy
from azure.schemaregistry import SchemaRegistryClient

SchemaRegistryPreparer = functools.partial(
    PowerShellPreparer, 'schemaregistry',
    schemaregistry_endpoint="fake_resource.servicebus.windows.net/",
    schemaregistry_group="fakegroup"
)

class TestSchemaRegistry(AzureRecordedTestCase):

# Start with any helper functions you might need, for example a client creation method:
    def create_client(self, endpoint):
        credential = self.get_credential(SchemaRegistryClient)
        client = self.create_client_from_credential(SchemaRegistryClient, credential=credential, endpoint=endpoint)
        return client

    ...

# Write your tests
class TestSchemaRegistryCase(TestSchemaRegistry):
    @SchemaRegistryPreparer()
    @recorded_by_proxy
    def test_client_creation(self, schemaregistry_endpoint):
        client = self.create_client(schemaregistry_endpoint)
        assert client is not None

nit: test class name begins with Test and test case name begins with test_

  1. Set the environment variables with real value in .env file which shall be put in same folder with azure-sdk-for-python repo. The format is like:
AZURE_TEST_RUN_LIVE=true
SCHEMAREGISTRY_SUBSCRIPTION_ID=0000000000000000000000000000
SCHEMAREGISTRY_TENANT_ID=000000000000000000000000000
SCHEMAREGISTRY_CLIENT_ID=0000000000000000000000000000
SCHEMAREGISTRY_CLIENT_SECRET=000000000000000000000000000
SCHEMAREGISTRY_ENDPOINT=real_resource.servicebus.windows.net
SCHEMAREGISTRY_GROUP=realgroup
|_ azure-sdk-for-python
|_ .env
  1. Run and record the test

    When you run test for first time, you need to prepare environment of test proxy

    From your terminal run the pytest command to run all the tests that you have written so far.

    (venv-sdk)azure-sdk-for-python\sdk\my-directory\my-library> pytest
    

    Your update should run smooth and have green dots representing passing tests. Now if you look at the contents of your tests directory there should be a new directory called recording with four .json files. Each json file is a recording for a single test. To run a test in playback mode change the AZURE_TEST_RUN_LIVE in .env to false and rerun the tests with the same command. The test infrastructure will use the automatically created .json recordings to mock the HTTP traffic and run the tests.

  2. Hide sensitive info in recordings for security

    Please follow register-sanitizers to avoid exposing secrets, and you can also refer to other services.

  3. Migrate recording files out of SDK repo

    Please follow recording_migration_guide to migrate the files out of SDK repo. If it has been complete, please follow update-test-recordings to push your updated recording files.

  4. Update ci.yml

    Please add TestProxy: true between ServiceDirectory and Artifacts: if it is not defined in ci.yml like here

An example test

An example test for schemaregistry looks like:

class TestSchemaRegistryCase(TestSchemaRegistry):

    ...
    @SchemaRegistryPreparer()
    @recorded_by_proxy
    def test_schema_basic(self, schemaregistry_endpoint, schemaregistry_group):
        client = self.create_client(schemaregistry_endpoint)
        schema_name = self.get_resource_name('test-schema-basic')
        schema_str = """{"namespace":"example.avro","type":"record","name":"User","fields":[{"name":"name","type":"string"},{"name":"favorite_number","type":["int","null"]},{"name":"favorite_color","type":["string","null"]}]}"""
        serialization_type = "Avro"
        schema_properties = client.register_schema(schemaregistry_group, schema_name, serialization_type, schema_str)

        assert schema_properties.schema_id is not None
        assert schema_properties.location is not None
        assert schema_properties.location_by_id is not None
        assert schema_properties.version is 1
        assert schema_properties.serialization_type == "Avro"

        with pytest.raises(HttpResponseError):
            client.get_schema('a' * 32)

Note

For more info about test, please refer to https://github.com/Azure/azure-sdk-for-python/blob/main/doc/dev/tests.md