Skip to content

Commit

Permalink
Move visual tests to e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
mormahr committed Sep 13, 2021
1 parent d757a5f commit fd83e19
Show file tree
Hide file tree
Showing 28 changed files with 168 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -277,5 +277,5 @@ dmypy.json

# End of https://www.toptal.com/developers/gitignore/api/python,intellij+all,macos,linux

test-data/*.pdf
e2e
.github
16 changes: 9 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,17 @@ jobs:
runs-on: ubuntu-20.04
needs:
- build
services:
pdf:
image: mormahr/pdf-service:sha-${{ github.sha }}
ports:
- 8080:8080

steps:
- name: "Generate basic PDF without returning invalid status code"
run: curl -f --data "<p>Hello World</p>" http://localhost:8080/generate > test.pdf
- name: Checkout code
uses: actions/checkout@v2

- name: Run visual tests
run: |
cd e2e
export TAG="sha-$GITHUB_SHA"
docker-compose up --build test
tag:
runs-on: ubuntu-20.04
if: github.ref == 'refs/heads/main'
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -277,5 +277,5 @@ dmypy.json

# End of https://www.toptal.com/developers/gitignore/api/python,intellij+all,macos,linux

test-data/*.pdf
e2e/data/**/*.pdf
coverage
5 changes: 5 additions & 0 deletions e2e/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.gitignore
docker-compose.yml
Dockerfile
diffs
data/**/generated.*
2 changes: 2 additions & 0 deletions e2e/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
diffs
data/**/generated.*
14 changes: 14 additions & 0 deletions e2e/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM alpine:3.14

WORKDIR /root
ENV PDF_SERVICE_URL=http://pdf:8080
VOLUME /root/diffs

RUN apk add --no-cache \
bash \
curl \
imagemagick

ADD . .

CMD [ "/bin/bash", "./scripts/run.sh" ]
File renamed without changes.
Binary file added e2e/data/basic/reference.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions e2e/data/basic/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

set -e

if [ "$PDF_SERVICE_URL" = "" ]; then
echo "\$PDF_SERVICE_URL has to be set."
fi

cd "$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )"

curl \
--fail \
--silent \
-F [email protected] \
"$PDF_SERVICE_URL/generate" \
> generated.pdf

../../scripts/create_reference_or_diff.sh
File renamed without changes.
Binary file added e2e/data/fonts/reference.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions e2e/data/fonts/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

set -e

if [ "$PDF_SERVICE_URL" = "" ]; then
echo "\$PDF_SERVICE_URL has to be set."
fi

cd "$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )"

curl \
--fail \
--silent \
-F [email protected] \
"$PDF_SERVICE_URL/generate" \
> generated.pdf

../../scripts/create_reference_or_diff.sh
File renamed without changes
File renamed without changes.
Binary file added e2e/data/multipart-with-image/reference.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions e2e/data/multipart-with-image/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

set -e

if [ "$PDF_SERVICE_URL" = "" ]; then
echo "\$PDF_SERVICE_URL has to be set."
fi

cd "$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )"

curl \
--fail \
--silent \
-F [email protected] \
-F test.png=@assets/test.png \
"$PDF_SERVICE_URL/generate" \
> generated.pdf

../../scripts/create_reference_or_diff.sh
8 changes: 8 additions & 0 deletions e2e/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: "3.8"
services:
pdf:
image: "mormahr/pdf-service:${TAG}"
test:
build: .
links:
- "pdf:pdf"
1 change: 1 addition & 0 deletions e2e/regenerate
1 change: 1 addition & 0 deletions e2e/run
8 changes: 8 additions & 0 deletions e2e/scripts/clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

set -e

cd "$( cd "$(dirname "${BASH_SOURCE[0]}")" || exit; pwd -P )" || exit

rm -f ../data/**/diff.png
rm -f ../data/**/generated.*
5 changes: 5 additions & 0 deletions e2e/scripts/convert.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

set -e

convert -density 200 -depth 8 -quality 100 generated.pdf -background white -flatten -strip PNG24:$1
29 changes: 29 additions & 0 deletions e2e/scripts/create_reference_or_diff.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

set -e

if [ ! -f reference.png ]; then
# Reference file doesn't exist yet -> create
echo "Generating for $(basename "$PWD")"
../../scripts/convert.sh reference.png
else
../../scripts/convert.sh generated.png

# Remove old diffs
mkdir -p ../../diffs/
rm -f "../../diffs/$(basename "$PWD").*"

# Create diff image
# Ignore comparison errors here because we do a byte comparison later
compare reference.png generated.png -compose Src "../../diffs/$(basename "$PWD").png" || true

# Generate animated GIF to illustrate differences
convert -delay 50 reference.png generated.png -loop 0 "../../diffs/$(basename "$PWD").gif"

# Byte comparison of reference and current image
if ! cmp reference.png generated.png > /dev/null 2>&1
then
echo "Reference and generated image differ for $(basename "$PWD")"
exit 1
fi
fi
9 changes: 9 additions & 0 deletions e2e/scripts/regenerate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

set -e

cd "$( cd "$(dirname "${BASH_SOURCE[0]}")" || exit; pwd -P )" || exit

./clean.sh
rm -f ../data/**/reference.png
./run.sh
20 changes: 20 additions & 0 deletions e2e/scripts/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

if [ "$PDF_SERVICE_URL" = "" ]; then
echo "\$PDF_SERVICE_URL has to be set."
fi

# Wait for pdf service to become available. (1s retry, 30s timeout)
timeout 30 bash -c 'while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' $PDF_SERVICE_URL/health)" != "200" ]]; do sleep 1; done' || false

cd "$( cd "$(dirname "${BASH_SOURCE[0]}")" || exit; pwd -P )" || exit

for f in ../data/*
do
# Guard against empty directory
[ -e "$f" ] || continue
echo "Running $(basename "$f")"

# Propagate errors because -e isn't set
"$f/run.sh" || exit 1
done
Binary file removed test-data/basic_0001-1.png
Binary file not shown.
Binary file removed test-data/fonts_0001-1.png
Binary file not shown.
Binary file removed test-data/multipart-with-image_0001-1.png
Binary file not shown.
54 changes: 0 additions & 54 deletions tests/test_visual.py

This file was deleted.

0 comments on commit fd83e19

Please sign in to comment.