page_type | languages | products | description | urlFragment | |||||
---|---|---|---|---|---|---|---|---|---|
sample |
|
|
This sample demonstrates how to manage Azurite as a background task both on local and in a container. |
azurite-tox-automation |
This repo presents a sample for automating tests required Azurite in Tox which mainly focuses running the service as a background process in a container and also on local.
The code contains two simple Fast API calls, one for uploading data.txt into a blob storage and one for uploading the txt to local. And same scenario in the code is tested by reproduction with Azurite in conftest.py
.
tox.ini
file includes two environments, first environment is for running the tests in a Linux container which may be a case for isolation required scenarios. And the other environment is for the scenarios where local automation is satisfactory.
Here's the folder structure for the sample:
container
Dockerfile
- Dockerfile which builds Linux container with all required components for running testsservice_start.sh
- shell script being used for running Azurite in the container
data
- a data.txt sample to upload to/download from blob storagesrc
__init__.py
app.py
- code file including FastAPI callsmain.py
.env
- Environment variable files which includes Storage Account connection string to run FastAPI calls
tests
__init__.py
conftest.py
- Configuration file for running teststest_example.py
- Azure Blob Storage Test Code.test_env
- Environment variable connection details for Azurite
requirements.txt
- Required pip packages to run the code and teststox.ini
- tox configuration file
The minimum requirement to see tests are running in the container and on your local is mainly:
- Stable Python Version.
- Pip
- Tox
- Docker: to build container image and also run Azurite with docker compose on local
If you prefer to run FastAPI calls to see what code does which isn't the main focus of the sample though, you need also a real blob storage in Azure Portal which means, following are prerequisites:
- Azure Subscription
- Azure Resource Group
- Storage Account - the connection string of your account should be added to .env file
For a better UI experience, it's recommended to use Storage Explorer to track the blob processes on your desktop;
As it's mentioned in Prerequisites section, we need to install Tox and then run the environments.
Running tox is platform agnostic. As soon as you have a Docker installed local environment to build a container, you can use Windows or Linux or Powershell/cmd or wsl/bash.
Tox is an PyPI package which means as soon as you have Pip installation on your local, you can install it via
# Install Tox
pip install tox
In tox.ini
file, you can see two environments in envlist section.
-
test_azurite_in_container: virtual environment which builds our Linux image with the repo code and Azurite service script then run the tests in this container.
-
test_azurite_on_local: virtual environment which runs all prerequisites for tests and run them on your local.
To run a specific environment in tox;
# Run test_azurite_in_container venv
tox -e test_azurite_in_container
# Run test_azurite_on_local venv
tox -e test_azurite_on_local
To run both environments at once;
# Run all env at once
tox
the output should be similar to:
If you run test_azurite_in_container environment, you can see that it builds a container image on your local.
-
The image is used to run the tests and build with Dockerfile
-
On manual execution, Azurite kills the session and requires a new cmd session start. To run Azurite service at the background in the container, service_starts.sh which starts the service is copied into the container and being triggered from outside in tox environment execution.
-
When tox completes test_azurite_in_container build, you will have a container image called 'pytest_linux' on your local. You can check it
# List docker images
docker images
- For manual checks, if you prefer to see the container content being run, you can run the image interactively
# Run image interactively
docker run -it pytest_linux bash
See LICENSE.
See CODE_OF_CONDUCT
See CONTRIBUTING Additionally kindly check Issue Template and PR Template