Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
E2E tests (using postman) (#65)
Browse files Browse the repository at this point in the history
### Added

- E2E tests (using [postman](https://www.postman.com/))

### Fixed

- Wrong HTTP `Content-Type` header value for docker environment
  • Loading branch information
tarampampam authored Jan 15, 2021
1 parent 6d4bbb2 commit c7fa3ec
Show file tree
Hide file tree
Showing 15 changed files with 611 additions and 7 deletions.
43 changes: 43 additions & 0 deletions .github/actions/newman/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# https://help.github.com/en/articles/metadata-syntax-for-github-actions
name: Run Newman
description: Newman is CLI collection runner for Postman

inputs:
collection:
description: 'Path to the file with postman collection'
required: true
default: './test/postman/default.postman_collection.json'
environment:
description: 'Path to the file with postman environment'
required: true
default: './test/postman/default.postman_environment.json'
baseurl:
description: 'Application base url'
required: true
default: 'http://127.0.0.1:8080'

runs:
using: "composite"
steps:
- name: Pull newman docker image
shell: bash
run: |
if [[ "$(docker images -q postman/newman:5.2-alpine 2> /dev/null)" == "" ]]; then
docker pull postman/newman:5.2-alpine 1>/dev/null
fi
# Image page: <https://hub.docker.com/r/postman/newman>,
# CLI options: <https://www.npmjs.com/package/newman#command-line-options>
- name: Run Newman
shell: bash
run: |
docker run \
--rm \
--tty \
--net host \
--volume "$(pwd):/rootfs:ro" \
--workdir "/rootfs" \
postman/newman:5.2-alpine run "${{ inputs.collection }}" \
--environment "${{ inputs.environment }}" \
--env-var "base_url=${{ inputs.baseurl }}" \
--color on
39 changes: 39 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
documentation:
- '**/*.md'
- '**/*.MD'
- '!CHANGELOG.md'

.github:
- '.github/**/*'

CI:
- '.github/workflows/**/*'
- '.github/actions/**/*'

tests:
- '**/*.spec.js'
- '**/*.spec.ts'
- '**/*.test.js'
- '**/*.test.ts'
- '**/*_test.go'
- '**/testdata/**/*'
- 'test/**/*'
- '.golangci.yml'
- 'codecov.yml'

frontend:
- 'web/**/*'

hosts:
- '.hosts/**/*'

docker:
- 'Dockerfile'
- 'docker/**/*'
- '.dockerignore'

dev:
- 'Makefile'
- 'docker-compose.yml'
- '.gitignore'
- '.editorconfig'
12 changes: 12 additions & 0 deletions .github/workflows/labeler.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: "Pull Request Labeler"

on: [pull_request_target]

jobs:
triage:
runs-on: ubuntu-20.04
steps:
- uses: actions/labeler@v3
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
sync-labels: true
96 changes: 94 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,18 @@ jobs:
file: /tmp/coverage.txt

build:
name: Build for ${{ matrix.os }}
name: Build for ${{ matrix.os }} (${{ matrix.arch }})
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
os: [linux, windows, darwin] # linux, freebsd, darwin, windows
arch: [amd64] # amd64, 386
include:
- os: linux
arch: 386
- os: windows
arch: 386
needs: [golangci-lint, go-test]
steps:
- name: Set up Go
Expand Down Expand Up @@ -118,7 +123,41 @@ jobs:
if-no-files-found: error
retention-days: 7

image:
e2e:
name: End-to-End tests (cache in ${{ matrix.caching-engine }})
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
caching-engine: [memory, redis]
needs: [build]
steps:
- name: Check out code
uses: actions/checkout@v2

- name: Start redis server
if: matrix.caching-engine == 'redis'
run: docker run --rm -d -p "6379:6379/tcp" redis:6.0.9-alpine

- name: Download compiled binary file
uses: actions/download-artifact@v2
with:
name: mikrotik-hosts-parser-linux-amd64
path: .artifact

- name: Prepare binary file to run
working-directory: .artifact
run: mv ./mikrotik-hosts-parser ./../app && chmod +x ./../app

- name: Start HTTP server
run: ./app serve --debug --port 8081 --caching-engine "${{ matrix.caching-engine }}" --redis-dsn "redis://127.0.0.1:6379/0" &

- name: Run Newman
uses: ./.github/actions/newman
with:
baseurl: 'http://127.0.0.1:8081'

docker-image:
name: Build docker image
runs-on: ubuntu-20.04
needs: [golangci-lint, go-test]
Expand All @@ -142,3 +181,56 @@ jobs:
image: mikrotik-hosts-parser:local
fail-build: true
severity-cutoff: low # negligible, low, medium, high or critical

- name: Save docker image
run: docker save mikrotik-hosts-parser:local > ./docker-image.tar

- name: Upload artifact
uses: actions/upload-artifact@v2
with:
name: docker-image
path: ./docker-image.tar

docker-image-e2e:
name: Docker image End-to-End tests (cache in ${{ matrix.caching-engine }})
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
caching-engine: [memory, redis]
needs: [docker-image]
steps:
- name: Check out code
uses: actions/checkout@v2

- name: Create docker network
run: docker network create "app-network"

- name: Start redis server
if: matrix.caching-engine == 'redis'
run: docker run --rm -d --network "app-network" -p "6379:6379/tcp" --name=redis redis:6.0.9-alpine

- name: Download builded docker image
uses: actions/download-artifact@v2
with:
name: docker-image
path: .artifact

- name: Prepare image to run
working-directory: .artifact
run: docker load < docker-image.tar

- name: Run docker image with app
run: |
docker run --rm -d \
--network "app-network" \
-p "8081:8081/tcp" \
-e "CACHING_ENGINE=${{ matrix.caching-engine }}" \
-e "REDIS_DSN=redis://redis:6379/0" \
-e "LISTEN_PORT=8081" \
mikrotik-hosts-parser:local
- name: Run Newman
uses: ./.github/actions/newman
with:
baseurl: 'http://127.0.0.1:8081'
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,17 @@ The format is based on [Keep a Changelog][keepachangelog] and this project adher
- HTTP endpoints:
- `/live` for liveness probe
- `/ready` for readiness probe
- E2E tests (using [postman](https://www.postman.com/))

### Removed

- File-based cache support
- HTTP `/api/routes` handler

### Fixed

- Wrong HTTP `Content-Type` header value for docker environment

## v3.0.3

### Fixed
Expand Down
8 changes: 6 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ ARG APP_VERSION="undefined@docker"
RUN set -x \
&& mkdir /src \
# SSL ca certificates (ca-certificates is required to call HTTPS endpoints)
&& apk add --no-cache ca-certificates \
# packages mailcap and apache2 is needed for /etc/mime.types and /etc/apache2/mime.types files respectively
&& apk add --no-cache mailcap apache2 ca-certificates \
&& update-ca-certificates

WORKDIR /src
Expand All @@ -33,11 +34,13 @@ RUN set -x \

# prepare rootfs for runtime
RUN set -x \
&& mkdir -p /tmp/rootfs/etc/ssl \
&& mkdir -p /tmp/rootfs/etc/ssl /tmp/rootfs/etc/apache2 \
&& mkdir -p /tmp/rootfs/bin \
&& mkdir -p /tmp/rootfs/opt/mikrotik-hosts-parser \
&& mkdir -p --mode=777 /tmp/rootfs/tmp \
&& cp -R /etc/ssl/certs /tmp/rootfs/etc/ssl/certs \
&& cp /etc/mime.types /tmp/rootfs/etc/mime.types \
&& cp /etc/apache2/mime.types /tmp/rootfs/etc/apache2/mime.types \
&& cp -R /src/web /tmp/rootfs/opt/mikrotik-hosts-parser/web \
&& cp /src/configs/config.yml /tmp/rootfs/etc/config.yml \
&& echo 'appuser:x:10001:10001::/nonexistent:/sbin/nologin' > /tmp/rootfs/etc/passwd \
Expand Down Expand Up @@ -66,6 +69,7 @@ USER appuser
# Docs: <https://docs.docker.com/engine/reference/builder/#healthcheck>
HEALTHCHECK --interval=15s --timeout=3s --start-period=1s CMD [ \
"/bin/mikrotik-hosts-parser", "healthcheck", \
"--log-json", \
"--port", "8080" \
]

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ services:
<<: *app-service
ports:
- '8080:8080/tcp' # Open <http://127.0.0.1:8080>
command: go run ./cmd/mikrotik-hosts-parser serve --config ./configs/config.yml --resources-dir ./web #--caching-engine redis --redis-dsn "redis://redis:6379/0"
command: go run ./cmd/mikrotik-hosts-parser serve --config ./configs/config.yml --resources-dir ./web --caching-engine redis --redis-dsn "redis://redis:6379/0"
depends_on:
- redis

Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/cache/inmemory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ func TestInMemoryCache_Expiration(t *testing.T) {
found, _, _, _ := cache.Get(testKeyName) //nolint:dogsled
assert.True(t, found)

<-time.After(time.Millisecond * 98)
<-time.After(time.Millisecond * 90)

found, _, _, _ = cache.Get(testKeyName) //nolint:dogsled
assert.True(t, found)

<-time.After(time.Millisecond * 3)
<-time.After(time.Millisecond * 10)

found, _, _, _ = cache.Get(testKeyName) //nolint:dogsled
assert.False(t, found)
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/http/handlers/api/settings/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func NewHandler(cfg config.Config, cacher cache.Cacher) http.HandlerFunc { //nol
c, _ = json.Marshal(resp)
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(c)
}
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/http/handlers/api/settings/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestNewHandler(t *testing.T) {
NewHandler(cfg, cacher)(rr, req)

assert.Equal(t, rr.Code, http.StatusOK)
assert.Equal(t, rr.Header().Get("Content-Type"), "application/json")

assert.JSONEq(t, `{
"sources":{
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/http/handlers/api/version/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func NewHandler(ver string) http.HandlerFunc {
})
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(cache)
}
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/http/handlers/api/version/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ func TestNewHandler(t *testing.T) {
NewHandler("1.2.3@foo")(rr, req)

assert.Equal(t, rr.Code, http.StatusOK)
assert.Equal(t, rr.Header().Get("Content-Type"), "application/json")
assert.JSONEq(t, `{"version":"1.2.3@foo"}`, rr.Body.String())
}
Loading

0 comments on commit c7fa3ec

Please sign in to comment.