Skip to content

Preparing the test environment

Heikki Johannes Hildén edited this page Jan 18, 2018 · 53 revisions

This page explains the individual commands in the containers.sh (mega-)script. This script can be used to set up a Docker-based environment for testing the registration service—part of the new service-based Uliza architecture.

Note: The instructions on this page cover more advanced use-cases. To simply run the registration tests, see Testing the registration service for a step-by-step explanation.

Discard containers left over from previous tests

To avoid "name already in use" errors in subsequent commands; we first stop and remove any existing containers with names beginning with ulizatests.

docker rm --force $(docker ps -aq --filter "name=ulizatests*") 2> /dev/null

Create a database container

This command runs the mysql:5.7 image to set up a fresh MySQL installation for the Uliza API, and create a database named uliza_core. The -p "0:3306" flag will map the container port to the first available port on the host system.

If the mysql image doesn't exist locally, the Docker client will automatically fetch the image from Docker Hub.

docker run \
  -d \
  -e "MYSQL_DATABASE=uliza_core" \
  -e "MYSQL_ROOT_PASSWORD=root" \
  -p "0:3306" \
  --name ulizatestsdb \
  mysql:5.7

Wait for database server to accept connections

You need to have the mysqladmin client tool installed for this step. On Ubuntu, it is part of the mysql-client package. A full MySQL server installation is not required.

This command will wait for the database server to become available to accept connections.

until mysqladmin ping \
  -uroot \
  -proot \
  -h0.0.0.0 \
  -P$(docker ps \
        --format "{{.Ports}}" \
        --filter "name=ulizatestsdb" | sed -n 's/[^:]*:\([0-9]*\).*/\1/p') \
        2> /dev/null ; do echo -n "." ; sleep 3 ; done

Create a mock VOTO API container

This service is used in the Mocha-based tests. It mimics the behavior of the VOTO API and delivers mock responses for a number of endpoints used in the registration process.

docker run \
  -d \
  -p "0:8089" \
  --name ulizatestsvoto \
  farmradio/voto_mock_api

Create Uliza API container

Next, we create the Uliza API server and link this container to the ulizatestsdb container we created earlier.

docker run \
  -d \
  -e "DEBUG=true" \
  -e "DB_ENGINE=django.db.backends.mysql" \
  -e "DB_NAME=uliza_core" \
  -e "DB_USER=root" \
  -e "DB_PASSWORD=root" \
  -e "DB_SERVICE_HOST=ulizatestsdb" \
  -e "DB_SERVICE_PORT=3306" \
  -p "0:8000" \
  --link ulizatestsdb \
  --name ulizatestsapi \
  farmradio/uliza_api

Create the registration service container

The last container is the registration service itself. It communicates with the Uliza API, as well as with the mock VOTO service API. For interactive tests, VOTO_API_URL should point to the actual VOTO API, and the --link ulizatestsvoto flag should not be included.

docker run \
  -d \
  -p "0:3034" \
  -e "PORT=3034" \
  -e "LOG_LEVEL=DEBUG" \
  -e "VOTO_API_URL=http://ulizatestsvoto:8089/api/v1" \
  -e "ULIZA_API_URL=http://ulizatestsapi:8000/api/v1" \
  -e "CALL_SCHEDULE_OFFSET=600" \
  -e "MIN_RESCHEDULE_DELAY=172800" \
  --link ulizatestsapi \
  --link ulizatestsvoto \
  --name ulizatestsmiddleware \
  farmradio/registration_service

Apply database migrations

docker exec -it ulizatestsapi ./django.sh makemigrations
docker exec -it ulizatestsapi ./django.sh migrate
Known issue
The Django migrations seem to fail sometimes with an error: Unable to create the django_migrations table ((1050, "Table 'django_migrations' already exists")), which results in some tables missing. Right now I am not sure why this happens, but as a workaround, simply run the script again.

List running containers

docker ps --format="table {{.Image}}\t{{.Names}}\t{{.Ports}}"

The output of this command should be a list of four running containers. (The actual host port numbers may be different.)

IMAGE                            NAMES                   PORTS
farmradio/registration_service   ulizatestsmiddleware   0.0.0.0:32771->3034/tcp
farmradio/uliza_api              ulizatestsapi          0.0.0.0:32770->8000/tcp
farmradio/voto_mock_api          ulizatestsvoto         0.0.0.0:32769->8089/tcp
mysql:5.7                        ulizatestsdb           0.0.0.0:32768->3306/tcp

API url

This url can be used to interact with the Uliza API, either using a command-line tool such as curl, or using the browsable API.

echo -e "http://$(docker ps \
  --filter "name=ulizatestsapi" \
  --format "{{.Ports}}" | sed -n 's/\([0-9.:]*\).*/\1/p')/api/v1"

This command should output an url of the following format.

http://0.0.0.0:xxxxx/api/v1