Skip to content

Commit

Permalink
Add container-structure-test, documentation update
Browse files Browse the repository at this point in the history
  • Loading branch information
rafzei committed Jan 7, 2022
1 parent bde172c commit c14921f
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 2 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Note: Because it's an example project, I won't use WSGI production-ready server

## Application

The application is created in Flask (python framework). It serves HTTP API endpoint with sample data from https://jsonplaceholder.typicode.com/users. It exposes Prometheus metrics on port 8080.
The application is created in Flask (python framework). It serves HTTP API endpoint with sample data from https://jsonplaceholder.typicode.com/users.

Requirements:
Python3
Expand Down Expand Up @@ -53,11 +53,17 @@ docker build --tag http-api:latest .
docker run -d --name http-api:latest
```

The image is also available on Dockerhub:

```sh
docker pull rafzei/http-api:latest
```

## Monitoring

In addition, you could use provided docker-compose file. It contains Prometheus and Grafana image definitions. In `./configs` directory you can find config files for both, as well as Grafana dashboard.

The Flask application exposes its metrics via endpoint:
The Flask application exposes its metrics on `/metrics` endpoint. Example:

```sh
<url>:5000/metrics
Expand All @@ -73,6 +79,20 @@ To reach provided Grafana dashboard:

### Helm chart

To run a chart execute the following command:

```sh
helm helm upgrade --install http-api --namespace http-api --create-namespace ./http-api/
```

Note: To be able to run ingress on minikube, enable addon:

```sh
minikube addons enable ingress
```

For details see [the official documentation](https://kubernetes.io/docs/tasks/access-application-cluster/ingress-minikube/)

### Helm hook

### Helm test
Expand Down
89 changes: 89 additions & 0 deletions docs/tests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Tests and scans documentation

## Docker image - Snyk scan

Docker image was scanned by Snyk:

```sh
docker scan http-api:latest
```

output:

```sh
Package manager: apk
Project name: docker-image|http-api
Docker image: http-api
Platform: linux/amd64
Base image: python:3.10.1-alpine3.15
Licenses: enabled

✔ Tested 37 dependencies for known issues, no vulnerable paths found.

According to our scan, you are currently using the most secure version of the selected base image
```

## Docker image - container-structure-test

Docker image tested by container-structure-test:

```sh
container-structure-test test --image http-api:latest --config tests/container.yml
```

output:

```sh
======================================
====== Test file: container.yml ======
======================================
=== RUN: Command Test: Check python
--- PASS
duration: 757.87055ms
stdout: Python 3.10.1

=== RUN: File Existence Test: /app/app.py
--- PASS
duration: 0s
=== RUN: File Existence Test: /app/payload.py
--- PASS
duration: 0s
=== RUN: File Existence Test: /app/requirements.txt
--- PASS
duration: 0s
=== RUN: Metadata Test
--- PASS
duration: 0s

=======================================
=============== RESULTS ===============
=======================================
Passes: 5
Failures: 0
Duration: 757.87055ms
Total tests: 5
```


## Helm test

To run Helm test, execute:

```sh
helm test -n http-api http-api
```
where `-n http-api` pointing to the namespace.

output:

```sh
NAME: http-api
LAST DEPLOYED: Fri Jan 7 18:02:22 2022
NAMESPACE: http-api
STATUS: deployed
REVISION: 9
TEST SUITE: http-api-test-connection
Last Started: Fri Jan 7 18:05:49 2022
Last Completed: Fri Jan 7 18:05:58 2022
Phase: Succeeded
```
2 changes: 2 additions & 0 deletions http-api/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from flask import Flask, jsonify, request
from prometheus_flask_exporter import PrometheusMetrics
# TODO: Get data from database
from payload import users


app = Flask(__name__)
metrics = PrometheusMetrics(app)

Expand Down
45 changes: 45 additions & 0 deletions tests/container.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Config for container-structure-test, see ..docs/tests.md

schemaVersion: '2.0.0'
commandTests:
- name: 'Check python'
command: 'python'
args: ['--version']
expectedOutput: ['Python 3.*']
exitCode: 0
# TODO: check if possible to add timeout
# - name: 'Check app'
# setup: [['python', '/app/app.py']]
# command: 'ps'
# args: ['-aux']
fileExistenceTests:
- name: '/app/app.py'
path: '/app/app.py'
shouldExist: true
permissions: '-rw-rw-r--'
uid: 0
gid: 0
- name: '/app/payload.py'
path: '/app/payload.py'
shouldExist: true
permissions: '-rw-rw-r--'
uid: 0
gid: 0
- name: '/app/requirements.txt'
path: '/app/requirements.txt'
shouldExist: true
permissions: '-rw-rw-r--'
uid: 0
gid: 0
metadataTest:
labels:
- key: 'org.opencontainers.image.authors'
value: 'Rafal Zeidler'
- key: 'org.opencontainers.image.version'
value: '^[0-9]\.[0-9]\.[0-9]$'
isRegex: true
exposedPorts: ["5000"]
entrypoint: []
cmd: ["python", "app.py"]
workdir: "/app"
user: "app"

0 comments on commit c14921f

Please sign in to comment.