Skip to content

Commit 58c9fc6

Browse files
authored
🧪🚀 Automated Postman tests (#79)
* Fix API ports in make start, update documentation * 🧪 Run automated Postman tests in Docker * Add the --reload flag to the Docker command (See #80)
1 parent fb6918e commit 58c9fc6

File tree

9 files changed

+101
-28
lines changed

9 files changed

+101
-28
lines changed

.github/workflows/pull_request.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,5 @@ jobs:
3737
steps:
3838
- name: Checkout Code
3939
uses: actions/checkout@v2
40-
- name: Set up Python ${{ env.PYTHON_VERSION }}
41-
uses: actions/setup-python@v1
42-
with:
43-
python-version: ${{ env.PYTHON_VERSION }}
44-
- name: Build Docker Image
45-
run: make docker-build
40+
- name: Run Tests
41+
run: make docker-test

.github/workflows/release.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,8 @@ jobs:
3939
steps:
4040
- name: Checkout Code
4141
uses: actions/checkout@v2
42-
- name: Set up Python ${{ env.PYTHON_VERSION }}
43-
uses: actions/setup-python@v1
44-
with:
45-
python-version: ${{ env.PYTHON_VERSION }}
46-
- name: Build Docker Image
47-
run: make docker-build
42+
- name: Run Tests
43+
run: make docker-test
4844

4945
release:
5046
name: Release

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ CMD uvicorn app.main:app --reload --host "$host" --port "$port"
1818
FROM base AS prod
1919
COPY ./app /app
2020
EXPOSE $port
21-
CMD uvicorn app.main:app --host "$host" --port "$port"
21+
# TODO: We should not have to use the --reload flag here! See issue #80
22+
CMD uvicorn app.main:app --reload --host "$host" --port "$port"

Makefile

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
OS ?= $(shell python -c 'import platform; print(platform.system())')
44
WINDOWS_ERROR = ⚠️ UNSUPPORTED WINDOWS INSTALL ⚠️
5-
CONTAINER_NAME = electionguard_web_api
6-
CONTAINER_ADDRESS ?= 0.0.0.0
7-
CONTAINER_PORT ?= 8000
85
IMAGE_NAME = electionguard_web_api
96
# Supports either "guardian" or "mediator" modes
10-
API_MODE = mediator
7+
API_MODE ?= mediator
8+
ifeq ($(API_MODE), mediator)
9+
PORT ?= 8000
10+
else
11+
PORT ?= 8001
12+
endif
1113

1214
all: environment lint start
1315

@@ -47,9 +49,9 @@ install-gmp-linux:
4749
sudo apt-get install libmpfr-dev
4850
sudo apt-get install libmpc-dev
4951

50-
# Server
52+
# Dev Server
5153
start:
52-
pipenv run uvicorn app.main:app --reload
54+
pipenv run uvicorn app.main:app --reload --port $(PORT)
5355

5456
# Docker
5557
docker-build:
@@ -61,6 +63,14 @@ docker-run:
6163
docker-dev:
6264
COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 docker-compose -f docker-compose.dev.yml up --build
6365

66+
docker-test:
67+
@echo 🧪 RUNNING TESTS IN DOCKER
68+
COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 docker-compose \
69+
-f tests/docker-compose.yml up \
70+
--build \
71+
--abort-on-container-exit \
72+
--exit-code-from test-runner
73+
6474
# Linting
6575
lint:
6676
@echo 💚 LINT

README.md

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ The application can run in one of two modes:
1515

1616
In practice, you will likely need to run at least one instance of each mode. We provide a single codebase and Docker image, but the mode can be set at runtime.
1717

18+
## 💻 Requirements
19+
20+
> NOTE:<br>
21+
> 🐳 = required for running with Docker.<br>
22+
> 🐍 = required for running with Python.
23+
24+
- 🐳🐍 [GNU Make](https://www.gnu.org/software/make/manual/make.html) is used to simplify the commands and GitHub Actions. This approach is recommended to simplify the command line experience. This is built in for MacOS and Linux. For Windows, setup is simpler with [Chocolatey](https://chocolatey.org/install) and installing the provided [make package](https://chocolatey.org/packages/make). The other Windows option is [manually installing make](http://gnuwin32.sourceforge.net/packages/make.htm).
25+
- 🐍 [Python 3.8](https://www.python.org/downloads/) is <ins>**required**</ins> to develop this API. If developer uses multiple versions of python, [pyenv](https://github.com/pyenv/pyenv) is suggested to assist version management.
26+
- 🐍 [pipenv](https://github.com/pypa/pipenv) is used to configure the python environment. Installation instructions can be found [here](https://github.com/pypa/pipenv#installation).
27+
28+
_These requirements line up with [electionguard-python](https://github.com/microsoft/electionguard-python/blob/main/README.md#-requirements)._
29+
1830
## 🐳 Running with Docker
1931

2032
If you run Docker and want to get started quickly, we provide a Dockerfile and docker-compose.yml.
@@ -35,14 +47,6 @@ After either command, you will find the `mediator` API running at http://127.0.0
3547

3648
## 🐍 Running with Python
3749

38-
### Requirements
39-
40-
_These requirements line up with [electionguard-python](https://github.com/microsoft/electionguard-python/blob/main/README.md#-requirements)._
41-
42-
- [Python 3.8](https://www.python.org/downloads/) is <ins>**required**</ins> to develop this API. If developer uses multiple versions of python, [pyenv](https://github.com/pyenv/pyenv) is suggested to assist version management.
43-
- [GNU Make](https://www.gnu.org/software/make/manual/make.html) is used to simplify the commands and GitHub Actions. This approach is recommended to simplify the command line experience. This is built in for MacOS and Linux. For Windows, setup is simpler with [Chocolatey](https://chocolatey.org/install) and installing the provided [make package](https://chocolatey.org/packages/make). The other Windows option is [manually installing make](http://gnuwin32.sourceforge.net/packages/make.htm).
44-
- [pipenv](https://github.com/pypa/pipenv) is used to configure the python environment. Installation instructions can be found [here](https://github.com/pypa/pipenv#installation).
45-
4650
### Quick Start
4751

4852
Using [**make**](https://www.gnu.org/software/make/manual/make.html), installation and startup can be run with one command:
@@ -73,7 +77,14 @@ If the code fails to run, [make sure your Python interpreter is set](https://cod
7377

7478
## 🧪 Testing
7579

76-
A Postman collection is available to test the API located in the `/tests` folder. This can be imported into Postman for easy testing. This suite works on a local run server or the preconfigured docker settings.
80+
A Postman collection is available to test the API located in the `/tests` folder. You can do a few things with this:
81+
82+
- [Import into Postman](https://learning.postman.com/docs/getting-started/importing-and-exporting-data/#importing-data-into-postman) for easy manual testing.
83+
- Run locally with the [Newman CLI](https://github.com/postmanlabs/newman).
84+
- Run the APIs and tests entirely in Docker by running:
85+
```bash
86+
make docker-test
87+
```
7788

7889
## 📝 Documentation
7990

docker-compose.dev.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# This hosts both APIs locally in parallel
2+
# It mounts the codebase within the container and runs the APIs
3+
# with hot reload enabled.
4+
15
version: "3.8"
26
services:
37
mediator:

tests/.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!*.postman_collection.json

tests/Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM postman/newman:5-ubuntu
2+
3+
# Install dockerize to support waiting for the target APIs to start up
4+
# https://github.com/jwilder/dockerize#ubuntu-images
5+
RUN apt-get update && apt-get install -y wget
6+
ENV DOCKERIZE_VERSION v0.6.1
7+
RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
8+
&& tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
9+
&& rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz

tests/docker-compose.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# This will spin up three parallel containers:
2+
# - One for each API under test
3+
# - One to house the test runner
4+
#
5+
# The test runner will wait for both API containers to run, and validate that
6+
# the APIs have started by checking their ping endpoints.
7+
#
8+
# This should be run with the --build and --abort-on-container-exit flags
9+
10+
version: "3.8"
11+
services:
12+
mediator:
13+
build:
14+
context: ..
15+
target: prod
16+
expose:
17+
- 80
18+
environment:
19+
API_MODE: "mediator"
20+
PROJECT_NAME: "ElectionGuard Mediator API"
21+
port: 80
22+
23+
guardian:
24+
build:
25+
context: ..
26+
target: prod
27+
expose:
28+
- 80
29+
environment:
30+
API_MODE: "guardian"
31+
PROJECT_NAME: "ElectionGuard Guardian API"
32+
port: 80
33+
34+
test-runner:
35+
build:
36+
context: .
37+
depends_on:
38+
- mediator
39+
- guardian
40+
volumes:
41+
- ".:/tests"
42+
entrypoint: dockerize
43+
command: -wait http://guardian/api/v1/ping -wait http://mediator/api/v1/ping -timeout 10s
44+
bash -c "newman run /tests/*.postman_collection.json --timeout-request 60000 --env-var guardian-url=http://guardian --env-var mediator-url=http://mediator"

0 commit comments

Comments
 (0)