Skip to content

Management Plane Codegen Quick Start for Test

Yuchao Yan edited this page Nov 13, 2024 · 2 revisions

Overview

The doc will show how to add testcase for Management Plane 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. add conftest.py

The content is:

import os
import pytest
from dotenv import load_dotenv
from devtools_testutils import (
    test_proxy,
    add_general_regex_sanitizer,
    add_body_key_sanitizer,
    add_header_regex_sanitizer,
)

load_dotenv()

# aovid record sensitive identity information in recordings
@pytest.fixture(scope="session", autouse=True)
def add_sanitizers(test_proxy):
    subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "00000000-0000-0000-0000-000000000000")
    tenant_id = os.environ.get("AZURE_TENANT_ID", "00000000-0000-0000-0000-000000000000")
    client_id = os.environ.get("AZURE_CLIENT_ID", "00000000-0000-0000-0000-000000000000")
    client_secret = os.environ.get("AZURE_CLIENT_SECRET", "00000000-0000-0000-0000-000000000000")
    add_general_regex_sanitizer(regex=subscription_id, value="00000000-0000-0000-0000-000000000000")
    add_general_regex_sanitizer(regex=tenant_id, value="00000000-0000-0000-0000-000000000000")
    add_general_regex_sanitizer(regex=client_id, value="00000000-0000-0000-0000-000000000000")
    add_general_regex_sanitizer(regex=client_secret, value="00000000-0000-0000-0000-000000000000")

    add_header_regex_sanitizer(key="Set-Cookie", value="[set-cookie;]")
    add_header_regex_sanitizer(key="Cookie", value="cookie;")
    add_body_key_sanitizer(json_path="$..access_token", value="access_token")
  1. Write your test with the following structure
import pytest
from azure.mgmt.schemaregistry import SchemaRegistryMgmtClient

from devtools_testutils import AzureMgmtRecordedTestCase, RandomNameResourceGroupPreparer, recorded_by_proxy

class TestSchemaRegistry(AzureMgmtRecordedTestCase):
    def setup_method(self, method):
        self.client = self.create_mgmt_client(SchemaRegistryMgmtClient)

    @RandomNameResourceGroupPreparer(location="eastus")
    @recorded_by_proxy
    def test_get(self, resource_group):
        response = self.client.operations.get(...)
        assert response

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
AZURE_SUBSCRIPTION_ID=0000000000000000000000000000
AZURE_TENANT_ID=000000000000000000000000000
AZURE_CLIENT_ID=0000000000000000000000000000
AZURE_CLIENT_SECRET=000000000000000000000000000
|_ 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

Note

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