Skip to content

Contributing to the tests

Laurent Mazuel edited this page Feb 26, 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:

from azure.common.credentials import UserPassCredentials
credentials = UserPassCredentials(
    '[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/

At this point, you must have:

  • Your client id. Found in the "client id" box in the "Configure" page of your application in the Azure portal
  • Your secret key. Generated when you have created the application. You cannot show the key after creation. If you've lost the current key, you must create a new one in the "Configure" page of your application.
  • You AD tenant id. It's an UUID (e.g. ABCDEFGH-1234-ABCD-1234-ABCDEFGHIJKL) which point to the AD containing your application. You will found it in the URL when you are in the Azure portal in your AD, or in the "view endpoints" in any of the given url.

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

from azure.common.credentials import ServicePrincipalCredentials

credentials = ServicePrincipalCredentials(
    client_id =  'ABCDEFGH-1234-ABCD-1234-ABCDEFGHIJKL',
    secret = 'XXXXXXXXXXXXXXXXXXXXXXXX',
    tenant = 'ABCDEFGH-1234-ABCD-1234-ABCDEFGHIJKL'
)

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