Skip to content

Contributing to the tests

Laurent Mazuel edited this page Jan 28, 2016 · 23 revisions

Overview

This page is to help you writing tests for Azure Python SDK when these tests implies Azure HTTP requests. Our framework rests upon on a HTTP recording system (vcr.py). We will find here:

  • How to authenticate to Azure, in order to be able to record/play HTTP requests
  • How to write tests using our Python utilities classes

Authenticate

You have several ways to authenticate to Azure. To be able to use the tests framework, you must use an authentication which gives you a token:

  • OAuth authentication using Azure Active Directory user/password
  • OAuth authentication using Active Directory application and service principal

Certificate authentication does not allow you to record HTTP queries for testing.

For ARM tests, you can use any of the two authentication system. For ASM legacy tests, you have to authenticate using AAD user/password

Get a token with Azure Directory user/password

  1. Connect to the Azure Classic Portal with your admin account

  2. Create a user in your default AAD https://azure.microsoft.com/en-us/documentation/articles/active-directory-create-users/

    You must NOT activate Multi-Factor Authentication

  3. Go to Settings - Administrators

  4. Click on Add and enter the email of the new user. Check the checkbox of the subscription you want to test with this user.

  5. Login to Azure Portal with this new user to change the temporary password to a new one. You will not be able to use the temporary password for OAuth login.

You are now able to log in Python using OAuth. You can test with this code using adal:

import adal
adal.acquire_token_with_username_password(
    'https://login.microsoftonline.com/common/', # Do not change
    '[email protected]',                           # Your new user
    'my_smart_password',                         # Your password
)

Get a token with Active Directory application and service principal

Follow this detailed link: https://azure.microsoft.com/en-us/documentation/articles/resource-group-create-service-principal-portal/

You are now able to log in Python using OAuth. You can test with this code using adal:

import adal
adal.acquire_token_with_client_credentials(
    'https://login.microsoftonline.com/ABCDEFGH-1234-ABCD-1234-ABCDEFGHIJKL/', # Your OAuth 2.0 Token Endpoint for this app
    'ABCDEFGH-1234-ABCD-1234-ABCDEFGHIJKL',                                    # Your client id
    'generatedkey',                                                            # Your authentication key
)

Using the Azure Python SDK framework

Tests are currently located in three folders:

  • azure-mgmt/tests
  • azure-servicebus/tests
  • azure-servicemanagement-legacy/tests

These folders are designed the same:

  • A testsettings_local.json : Recording configuration file
  • A mgmt_settings_fake.py : Azure mock configuration file
  • A mgmt_settings_real.py : Azure real configuration file (not in the repository)
  • A <libtype>_testcase.py : Common code of the tests
  • Several test_<file>.py : Test files themselves

The testsettings_local.json must have this form:

{
    "mode" : "Playback"
}

Valid values are:

  • Playback : Use the recordings on disk to test (i.e. offline mock mode). Use configuration from mgmt_settings_fake.py
  • Live : Connect directly to azure. Use configuration from mgmt_settings_real.py
  • Record : Connect directly to azure and record HTTP queries/answers. Use configuration from mgmt_settings_real.py

The recordings are saved in the recordings subfolder.

The mgmt_settings_real.py should start from a copy of mgmt_settings_fake.py. Then, you change every fake settings by your actual settings. mgmt_settings_real.py is not intended to be committed since it contains your actual credentials. This file is added in the .gitignore for that purpose

To be able to be recorded, each test must have the @record decorator:

@record
def test_usage(self):
    usages = list(self.compute_client.usage.list(self.region))
    self.assertGreater(len(usages), 0)
Clone this wiki locally