This guide will assist you in setting up a local development environment for this extension.
We also regularly utilize GitHub Codespaces, and have automated this environment setup in our devcontainer config
You can run all commands in this setup in
bashorcmdenvironments, this documentation just shows thepowershellflavor.
- Get Python 3: https://www.python.org/downloads/
-
Create a virtual environment (example, named 'env3')
python -m venv env3 -
Activate your virtual environment
Windows:
./env3/Scripts/activate
Unix:
source ./env3/bin/activate
IMPORTANT: Ensure you keep this Python virtual environment. It is required for development.
git clone https://github.com/Azure/azure-iot-ops-cli-extensiongit clone https://github.com/Azure/azure-cliTo assist in local development, it's suggested to utilize a local clone of the core azure-cli repository.
You may also utilize azdev setup (explained in this later section) to customize your local CLI installation, some options will download a version of CLI core on your behalf.
IMPORTANT: When cloning the repositories and environments, ensure they are all siblings to each other. This makes things much easier down the line.
Example folder structure:
source/
|-- .env3/
|-- azure-cli/
|-- azure-iot-ops-cli-extension/
|-- extensions/
The extensions/ folder is optional, but allows you to isolate your installed extensions with an environment variable named AZURE_EXTENSION_DIR that points to it:
$env:AZURE_EXTENSION_DIR="path/to/source/extensions"Otherwise, you can utilize the default extension path ~/.azure/cliextensions, but note that this may cause conflicts if your machine has an existing installation of az CLI and/or other CLI extensions.
Install and configure Azure CLI dev tools
With your virtual environment activated:
-
(env) pip install azdev
-
(env) azdev setup -c ./path_to/azure-cli
azdev setupcan utilize your local copy of theazure-clirepository, or it can download one for you:azdev setup -c ./path_to/azure-cliwill use your local copy of theazure-clirepoazdev setup -c EDGEwill install the current dev branch of theazure-clirepoazdev setupwith no arguments provides an interactive setup experienceazdev setup -hwill display other configuration options.
-
Verify your CLI is configured correctly:
(env) az -vYou should see
Python locationwith a Python executable in your localenv3path:Python location 'C:\src\azure-iot-ops-cli-extension\env3\Scripts\python.exe'You should see
Extensions directorywith the value you set forAZURE_EXTENSION_DIR, or~/.azure/cliextensions:Extensions directory 'path/to/source/extensions'
Inside your azure-iot-ops-cli-extension directory, run
(env) pip install -U --target $env:AZURE_EXTENSION_DIR/azure-iot-opsNote, if you did not setup the
AZURE_EXTENSION_DIRvariable in the previous step, this value is~/.azure/cliextensions
Run a command that is present in the iot extension space
az iot ops -hIf this works, then you should now be able to make changes to the extension and have them reflected immediately in your az CLI.
Our tests are located inside azext_edge/tests/:
azext_edge/tests/edge- core functionality testsazext_edge/tests/utility- utility unit tests
Inside azext_edge/tests/edge - the tests are broken up by category:
checks- Check testsinit- Init testsmq- Tests for MQTTBrokerorchestration- Init/Create/Clone orchestration testsrpsaas- Cloud resource provider testssupport- Support bundle tests
We support running tests locally using Tox - our Tox testing guide has more detailed information on our configuration.
Tox can be installed as part of our dev requirements, which are required to run tests locally:
pip install -r path/to/source/dev_requirements.txtTo see available tox environments:
tox -avTo run linters and unit test checks inside their own virtual environments, simply run:
tox Unit tests end in _unit.py so execute the following command to run all unit tests:
pytest -k "_unit.py"Execute the following command to run the support bundle unit tests:
pytest azext_edge/tests/edge/support -k "_unit.py"Integration tests are run against Azure resources and depend on various configured environment variables. Some tests also require a connection to a connected cluster with Azure IoT Operations deployed.
You can create a local pytest.ini using our example file by running:
cp ./pytest.ini.example ./pytest.iniIntegration tests end in _int.py so execute the following command to run all integration tests,
pytest -k "_int.py"Example int tests runs:
Init:
pytest azext_edge/tests/edge/init/int/test_init_int.pyAll support bundle int tests:
pytest azext_edge/tests/edge/support/create_bundle_int -k "_int.py"You can also target specific test functions in any test file, such as:
pytest azext_edge/tests/edge/checks/int/test_dataflow_int.py::test_dataflow_checkPull request titles must follow the conventional commits specification before merging. This ensures that our production-ready branches can be easily parsed and processed by downstream tooling and aids us in creating effective release notes.
Pull requests should be opened in DRAFT mode unless they are considered ready for review to prevent churn on code that is still changing.
To simulate most of the CI checks running against your code, you can run tox locally to check the linters and unit tests in an isolated environment.
-
Install VSCode
-
Install the required extensions
ms-python.pythonis recommendedpython blackfor linting and auto-formatting
-
Set up
settings.json{ "python.pythonPath": "path/to/source/env3/Scripts/python.exe", "python.venvPath": "path/to/source/", "python.linting.pylintEnabled": true, "python.autoComplete.extraPaths": [ "path/to/source/env3/Lib/site-packages" ], "python.linting.flake8Enabled": true, "python.linting.flake8Args": [ "--config=setup.cfg" ], } -
Set up
launch.json{ "version": "0.2.0", "configurations": [ { "name": "Azure CLI Debug (Integrated Console)", "type": "python", "request": "launch", "pythonPath": "${config:python.pythonPath}", "program": "${workspaceRoot}/../azure-cli/src/azure-cli/azure/cli/__main__.py", "cwd": "${workspaceRoot}", "args": [ "--help" ], "console": "integratedTerminal", "debugOptions": [ "WaitOnAbnormalExit", "WaitOnNormalExit", "RedirectOutput" ], "justMyCode": false } ] }-
launch.json was derived from this file
-
Note: your "program" path might be different if you did not set up the folder structure as siblings as recommended above
-
Note: when passing args, ensure they are all comma separated.
Correct:
"args": [ "--a", "value", "--b", "value" ],
Incorrect:
"args": [ "--a value --b value" ],
-
-
You should now be able to place breakpoints in VSCode and see execution halt as the code hits them.
https://docs.python.org/3/library/pdb.html
pip install pdbpp- If you need a breakpoint, put
import pdb; pdb.set_trace()in your code - Run your command, it should break execution wherever you put the breakpoint.
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.