Skip to content

Dataplane Codegen Quick Start for Test

Yuchao Yan edited this page Nov 15, 2022 · 28 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 env
C:\Users> env\scripts\activate       # PowerShell only
C:\Users> source env/bin/activate    # Linux shell (Bash, ZSH, etc.) only
C:\Users> env\scripts\activate.bat   # Windows CMD only
(env)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:

(env)azure-sdk-for-python\sdk\my-directory\my-library> pip install pytest
(env)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.

(env)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_schemareg_client(self, endpoint):
        credential = self.get_credential(SchemaRegistryClient)
        client = self.create_client_from_credential(SchemaRegistryClient, credential=credential, endpoint=endpoint)
        return client

    ...

# Write your tests
    @SchemaRegistryPreparer()
    @recorded_by_proxy
    def test_client_creation(self, schemaregistry_endpoint):
        client = self.create_schemareg_client(schemaregistry_endpoint)
        assert client is not None
  1. Set the environment variables with real value in .env file at the root of the 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
  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.

    (env)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. 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 SchemaRegistryTestCase(AzureTestCase):

    ...
    @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)