File: generating-integration-test.md

package info (click to toggle)
python-azure 20201208%2Bgit-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,437,920 kB
  • sloc: python: 4,287,452; javascript: 269; makefile: 198; sh: 187; xml: 106
file content (134 lines) | stat: -rw-r--r-- 4,959 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Generating Python SDK Integration Test

Integration test can be generated from swagger examples using **autorest**.

## Prerequisites

You can install autorest following generic autorest documentation.
The best way of using **autorest.cli** is Docker container.

You need to clone following directories locally. I have added Python SDK here as well, it's not necessary, but may be useful.

    git clone https://github.com/Azure/azure-rest-api-specs.git
    git clone https://github.com/Azure/azure-sdk-for-python.git

For simplicity let's assume they are cloned under **c:\dev** directory on Windows machine.

The easiest way to use **autorest.cli** is the container:

    docker run -it --rm -v c:\dev:/_ mcr.microsoft.com/azure-cli/tools

You can also use the container without mapping your local folders:

    docker run -it --rm mcr.microsoft.com/azure-cli/tools

but you will need to make sure embedded copy of repositories are up to date:

    cd /_/azure-sdk-for-python; git pull
    cd /_/azure-rest-api-specs; git pull

## Configuration in **readme.cli.md** File

>NOTE: The same configuration will be used to generate Azure CLI commands tests.

In order to generate Python integration test you will need to create **readme.cli.md** file next to **readme.md** file in Azure REST API specification repository:

The minimal file should have following format:

    ## CLI

    These settings apply only when `--cli` is specified on the command line.

    ``` yaml $(cli)
    cli:
      namespace: azure.mgmt.attestation
    ```

## Generating Test

Run:

    autorest --cli --use-extension="{'@autorest/cli':'latest'}" --python-integration-test --output-folder=/_/azure-sdk-for-python /_/azure-rest-api-specs/specification/attestation/resource-manager/readme.md

You will get output like this:

    AutoRest code generation utility [version: 2.0.4407; node: v10.15.0]
    (C) 2018 Microsoft Corporation.
    https://aka.ms/autorest
    Loading AutoRest core      '/root/.autorest/@microsoft.azure_autorest-core@2.0.4407/node_modules/@microsoft.azure/autorest-core/dist' (2.0.4407)
    Loading AutoRest extension '@autorest/cli' (latest->0.1.889)
    Loading AutoRest extension '@microsoft.azure/autorest.modeler' (2.3.45->2.3.45)
    WARNING:
    WARNING: NO TEST SCENARIO PROVIDED - DEFAULT WILL BE USED
    WARNING: ADD FOLLOWING SECTION TO readme.cli.md FILE TO MODIFY IT
    WARNING: --------------------------------------------------------
    WARNING:   test-scenario:
    WARNING:     - name: AttestationProviders_Create
    WARNING:     - name: AttestationProviders_Get
    WARNING:     - name: AttestationProviders_ListByResourceGroup
    WARNING:     - name: AttestationProviders_List
    WARNING:     - name: Operations_List
    WARNING:     - name: AttestationProviders_Delete
    WARNING: --------------------------------------------------------

Default test-scenario was generated, you can copy and paste it into **readme.cli.md** file:


    ## CLI

    These settings apply only when `--cli` is specified on the command line.

    ``` yaml $(cli)
    cli:
      namespace: azure.mgmt.attestation
    test-scenario:
    - name: AttestationProviders_Create
    - name: AttestationProviders_Get
    - name: AttestationProviders_ListByResourceGroup
    - name: AttestationProviders_List
    - name: Operations_List
    - name: AttestationProviders_Delete
    ```

Now you can rerun **autorest** command and warning won't be visible any longer.
You can rearrange sequence or disable particular tests.

## Running Test

First of all set the variable, to enable live tests:

    export AZURE_TEST_RUN_LIVE=true

Live test requires credentials, in order to do that create **mgmt_settings_real.py** file:

    cd /_/azure-sdk-for-python
    cp tools/azure-sdk-tools/devtools_testutils/mgmt_settings_fake.py tools/azure-sdk-tools/devtools_testutils/mgmt_settings_real.py
    vi tools/azure-sdk-tools/devtools_testutils/mgmt_settings_real.py

and make sure your subscription ID is correct:

    SUBSCRIPTION_ID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

and **get_credentials** looks as follows:

    def get_credentials(**kwargs):
        from azure.common.credentials import ServicePrincipalCredentials
        return ServicePrincipalCredentials(
            client_id =  'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
            secret = 'XxxxXxxxXXXxxxXXXXxxxxXXxxxXxxx',
            tenant = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx'
        )

now you can run live integration test:

    pytest -s sdk/attestation/azure-mgmt-attestation

>NOTE: To create service principal, follow instructions here: https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal

## Fixing Test

It's obvious that when running test for the first time something is not going to work.

The best approach is to:
- fix the test manually
- when the test is fully working backpropagate any changes to readme file and examples in swagger.