Skip to content

Adding Support for Cosmos db Emulator for Test

PieritoAlva95 edited this page Jun 25, 2021 · 1 revision

Azure Cosmos DB Linux Emulator

The Azure Cosmos DB Linux Emulator provides a local environment that emulates the Azure Cosmos DB service for development purposes. Currently, the Linux emulator only supports SQL API [Source]. The Time tracker works with the SQL API so this emulator is good for testing purposes.

Installing Cosmos DB Linux Emulator

You can run Cosmos DB Linux Emulator on Windows with WSL but we recommend running the emulator on linux to avoid problems related to the SSL.

Prerequisites:

  1. Retrieve the IP address of your local machine
ipaddr="`ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk '{print $2}' | head -n 1`"
  1. Pull the Docker image from the registry.
docker pull mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator
  1. Run the Docker image with the following configurations:
docker run -p 8081:8081 -p 10251:10251 -p 10252:10252 -p 10253:10253 -p 10254:10254  -m 3g --cpus=2.0 --name=azurecosmosemulator -e AZURE_COSMOS_EMULATOR_PARTITION_COUNT=7 -e AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE=true -e AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE=$ipaddr -e AZURE_COSMOS_EMULATOR_ARGS=/alternativenames=azurecosmosemulator -it mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator

Install the Certificate

  1. In a different terminal, retrieve the IP address of your local machine.
ipaddr="`ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk '{print $2}' | head -n 1`"
  1. Download the certificate:
curl -k https://$ipaddr:8081/_explorer/emulator.pem > ~/emulatorcert.crt
  1. Copy the CRT file to the folder that contains custom certificates in your Linux distribution. Commonly on Debian distributions, it is located on /usr/local/share/ca-certificates/.
cp ~/emulatorcert.crt /usr/local/share/ca-certificates/
  1. Update the TLS/SSL certificates, which will update the /etc/ssl/certs/ folder.
update-ca-certificates
  1. Now, to use the certificates on the TimeTracker BK, set this env:
export REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt

If you go to https://127.0.0.1:8081/_explorer/index.html you could see the db key and connection URI.

  1. Change the environment variables DATABASE_ACCOUNT_URI and DATABASE_MASTER_KEY with the ones in https://127.0.0.1:8081/_explorer/index.html .
export DATABASE_ACCOUNT_URI=URL_EMULATOR
export DATABASE_MASTER_KEY=DATABASE_KEY
  1. Finally you have to create the cosmos containers to use the database. to do this, MAKE SURE TO USE THE CORRECT ENV VARIABLES, and use the script cosmosdb-emulator/init_emulator_db.py.

  2. You can do your testing and create documents in the emulated database!!

Support for docker-compose and Cosmosdb emulator

To build the development environment in docker compose, two containers are created: One with the cosmosdb emulator and the other with the backend project. The first container is defined as the service cosmosdb and the second is defined as api service in the docker-compose.yml file. For the api service, Alpine Linux 3.9 is used as you can see in the Dockerfile.dev, and the volumes in the repository are shared with the container. To use the emulator in Alpine linux, the SSL Installation process is slightly different: Instead of step 3 and 4 you should do:

cp ~/emulatorcert.crt /usr/local/share/ca-certificates/
cp cosmosdb-emulator/emulatorcert.crt /usr/share/ca-certificates/
update-ca-certificates --fresh

And in step 5, the environment variable should be:

export REQUESTS_CA_BUNDLE=/etc/ssl/certs/

The api container has to wait until the database is ready, for that reason, we create the entrypoint.sh. This script runs before the api service and ensures that the db emulator is ready before starting the flask application.

Running in docker compose

Create the .env file with the bash variables on Set environment variables section in the readme and add the following variable:

export REQUESTS_CA_BUNDLE=/etc/ssl/certs/

Note: Change the environment variables DATABASE_ACCOUNT_URI and DATABASE_MASTER_KEY with your local variables. To run the dev environment in docker-compose:

docker-compose up

This terminal will show any changes in your code. If you close the terminal, you can do:

docker logs time-tracker-backend_api_1

To run the test you should start a new terminal and run:

docker exec -it time-tracker-backend_api_1 ash

and follow the steps in the Readme

To stop the docker-compose environment you can do Ctr-C or from other terminal:

docker-compose stop

To delete the docker-compose environment:

docker-compose down
Clone this wiki locally