Skip to content

Commit

Permalink
Add unit and E2E tests (#479)
Browse files Browse the repository at this point in the history
* set up test environment

* fix and clean up tests

* update Drone config to run tests

* clean up docker-compose file

* fix Grafana setup step

* init yarn in test step

* Update common.star

* Update .drone.yml

* Update browser.test.ts

* add volumes for puppeteer cache

* fix after merge

* install browser in drone workspace

* try to fix workspace

* test other image to run tests

* Update .drone.yml

* trying things

* Update ci image

* fix test endpoint

* add debug logs

* fix provisioning file path

* Update http-server.integration.test.ts

* fix volumes

* update provisioning

* fix dashboards provisioning path

* remove debug logs

* add image log

* tmp comment out tests

* Update http-server.integration.test.ts

* Update http-server.integration.test.ts

* test with deviceScaleFactor

* Update http-server.integration.test.ts

* display styles to compare CI vs local

* Update browser.ts

* Use pixelmatch to compare test images + clean up

* remove failing test + clean up provisioning files

* switch image to grafana-enterprise

* update test image

* Update README.md

* fix docker-compose path

* Add instructions to run on Drone-like env

* update golden files

* remove broken test

* remove slow.png

* move reading env settings

* update yarn-test to embed running Grafana Docker

* Update Drone Docker and instructions

* hide logs on success
  • Loading branch information
AgnesToulet authored Jan 30, 2024
1 parent ecaca47 commit aca8ee6
Show file tree
Hide file tree
Showing 26 changed files with 2,903 additions and 92 deletions.
97 changes: 81 additions & 16 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ node:
platform:
arch: amd64
os: linux
services: []
services:
- environment:
GF_FEATURE_TOGGLES_ENABLE: renderAuthJWT
GF_PATHS_PROVISIONING: /drone/src/scripts/drone/provisioning
image: grafana/grafana-enterprise:latest
name: grafana
steps:
- commands:
- mkdir -p bin
Expand All @@ -24,6 +29,8 @@ steps:
- yarn install --frozen-lockfile --no-progress
depends_on:
- grabpl
environment:
PUPPETEER_CACHE_DIR: /drone/src/cache
image: grafana/grafana-plugin-ci:1.9.0
name: yarn-install
- commands:
Expand All @@ -33,6 +40,20 @@ steps:
- yarn-install
image: grafana/grafana-plugin-ci:1.9.0
name: yarn-build
- commands:
- dockerize -wait http://grafana:3000 -timeout 120s
image: jwilder/dockerize:0.6.1
name: wait-for-grafana
- commands:
- yarn test
depends_on:
- wait-for-grafana
- yarn-build
environment:
CI: "true"
PUPPETEER_CACHE_DIR: /drone/src/cache
image: grafana/docker-puppeteer:pre-node-20
name: yarn-test
- commands:
- . ~/.init-nvm.sh
- echo "Starting veracode scan..."
Expand All @@ -55,7 +76,7 @@ steps:
- bin/grabpl build-plugin-manifest ./dist/plugin-linux-x64-glibc || true
- ./scripts/archive_target.sh linux-x64-glibc
depends_on:
- yarn-build
- yarn-test
environment:
GRAFANA_API_KEY:
from_secret: grafana_api_key
Expand All @@ -67,7 +88,7 @@ steps:
- bin/grabpl build-plugin-manifest ./dist/plugin-darwin-x64-unknown || true
- ./scripts/archive_target.sh darwin-x64-unknown
depends_on:
- yarn-build
- yarn-test
environment:
GRAFANA_API_KEY:
from_secret: grafana_api_key
Expand All @@ -79,7 +100,7 @@ steps:
- bin/grabpl build-plugin-manifest ./dist/plugin-win32-x64-unknown || true
- ./scripts/archive_target.sh win32-x64-unknown
depends_on:
- yarn-build
- yarn-test
environment:
GRAFANA_API_KEY:
from_secret: grafana_api_key
Expand All @@ -92,7 +113,7 @@ steps:
true
- ./scripts/archive_target.sh linux-x64-glibc plugin-linux-x64-glibc-no-chromium
depends_on:
- yarn-build
- yarn-test
environment:
GRAFANA_API_KEY:
from_secret: grafana_api_key
Expand All @@ -119,7 +140,12 @@ node:
platform:
arch: amd64
os: linux
services: []
services:
- environment:
GF_FEATURE_TOGGLES_ENABLE: renderAuthJWT
GF_PATHS_PROVISIONING: /drone/src/scripts/drone/provisioning
image: grafana/grafana-enterprise:latest
name: grafana
steps:
- commands:
- mkdir -p bin
Expand All @@ -132,6 +158,8 @@ steps:
- yarn install --frozen-lockfile --no-progress
depends_on:
- grabpl
environment:
PUPPETEER_CACHE_DIR: /drone/src/cache
image: grafana/grafana-plugin-ci:1.9.0
name: yarn-install
- commands:
Expand All @@ -141,6 +169,20 @@ steps:
- yarn-install
image: grafana/grafana-plugin-ci:1.9.0
name: yarn-build
- commands:
- dockerize -wait http://grafana:3000 -timeout 120s
image: jwilder/dockerize:0.6.1
name: wait-for-grafana
- commands:
- yarn test
depends_on:
- wait-for-grafana
- yarn-build
environment:
CI: "true"
PUPPETEER_CACHE_DIR: /drone/src/cache
image: grafana/docker-puppeteer:pre-node-20
name: yarn-test
- commands:
- . ~/.init-nvm.sh
- echo "Starting veracode scan..."
Expand All @@ -163,7 +205,7 @@ steps:
- bin/grabpl build-plugin-manifest ./dist/plugin-linux-x64-glibc
- ./scripts/archive_target.sh linux-x64-glibc
depends_on:
- yarn-build
- yarn-test
environment:
GRAFANA_API_KEY:
from_secret: grafana_api_key
Expand All @@ -175,7 +217,7 @@ steps:
- bin/grabpl build-plugin-manifest ./dist/plugin-darwin-x64-unknown
- ./scripts/archive_target.sh darwin-x64-unknown
depends_on:
- yarn-build
- yarn-test
environment:
GRAFANA_API_KEY:
from_secret: grafana_api_key
Expand All @@ -187,7 +229,7 @@ steps:
- bin/grabpl build-plugin-manifest ./dist/plugin-win32-x64-unknown
- ./scripts/archive_target.sh win32-x64-unknown
depends_on:
- yarn-build
- yarn-test
environment:
GRAFANA_API_KEY:
from_secret: grafana_api_key
Expand All @@ -199,14 +241,16 @@ steps:
- bin/grabpl build-plugin-manifest ./dist/plugin-linux-x64-glibc-no-chromium
- ./scripts/archive_target.sh linux-x64-glibc plugin-linux-x64-glibc-no-chromium
depends_on:
- yarn-build
- yarn-test
environment:
GRAFANA_API_KEY:
from_secret: grafana_api_key
image: grafana/grafana-plugin-ci:1.9.0
name: package-linux-x64-glibc-no-chromium
- commands:
- ./scripts/build_push_docker.sh master
depends_on:
- yarn-test
environment:
DOCKER_PASS:
from_secret: docker_pass
Expand Down Expand Up @@ -241,7 +285,12 @@ node:
platform:
arch: amd64
os: linux
services: []
services:
- environment:
GF_FEATURE_TOGGLES_ENABLE: renderAuthJWT
GF_PATHS_PROVISIONING: /drone/src/scripts/drone/provisioning
image: grafana/grafana-enterprise:latest
name: grafana
steps:
- commands:
- mkdir -p bin
Expand All @@ -254,6 +303,8 @@ steps:
- yarn install --frozen-lockfile --no-progress
depends_on:
- grabpl
environment:
PUPPETEER_CACHE_DIR: /drone/src/cache
image: grafana/grafana-plugin-ci:1.9.0
name: yarn-install
- commands:
Expand All @@ -263,6 +314,20 @@ steps:
- yarn-install
image: grafana/grafana-plugin-ci:1.9.0
name: yarn-build
- commands:
- dockerize -wait http://grafana:3000 -timeout 120s
image: jwilder/dockerize:0.6.1
name: wait-for-grafana
- commands:
- yarn test
depends_on:
- wait-for-grafana
- yarn-build
environment:
CI: "true"
PUPPETEER_CACHE_DIR: /drone/src/cache
image: grafana/docker-puppeteer:pre-node-20
name: yarn-test
- commands:
- . ~/.init-nvm.sh
- echo "Starting veracode scan..."
Expand All @@ -285,7 +350,7 @@ steps:
- bin/grabpl build-plugin-manifest ./dist/plugin-linux-x64-glibc
- ./scripts/archive_target.sh linux-x64-glibc
depends_on:
- yarn-build
- yarn-test
environment:
GRAFANA_API_KEY:
from_secret: grafana_api_key
Expand All @@ -297,7 +362,7 @@ steps:
- bin/grabpl build-plugin-manifest ./dist/plugin-darwin-x64-unknown
- ./scripts/archive_target.sh darwin-x64-unknown
depends_on:
- yarn-build
- yarn-test
environment:
GRAFANA_API_KEY:
from_secret: grafana_api_key
Expand All @@ -309,7 +374,7 @@ steps:
- bin/grabpl build-plugin-manifest ./dist/plugin-win32-x64-unknown
- ./scripts/archive_target.sh win32-x64-unknown
depends_on:
- yarn-build
- yarn-test
environment:
GRAFANA_API_KEY:
from_secret: grafana_api_key
Expand All @@ -321,7 +386,7 @@ steps:
- bin/grabpl build-plugin-manifest ./dist/plugin-linux-x64-glibc-no-chromium
- ./scripts/archive_target.sh linux-x64-glibc plugin-linux-x64-glibc-no-chromium
depends_on:
- yarn-build
- yarn-test
environment:
GRAFANA_API_KEY:
from_secret: grafana_api_key
Expand Down Expand Up @@ -414,6 +479,6 @@ kind: secret
name: srcclr_api_token
---
kind: signature
hmac: eeb3dd651cc32e9e32188e7f904e97131911d9699c7610c4b33a19098c257d9f
hmac: f6cb2de5719e26d46b642bb10f322f1b269310d013408b72a0d72b1c05d4ebea

...
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ dist/**/*
# Ignore output from coverage report
coverage

scripts/tmp
scripts/tmp

tests/testdata/diff_*

cache
81 changes: 61 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,29 @@ The following example shows how you can run Grafana and the remote HTTP renderin

1. Create a `docker-compose.yml` with the following content:

```yaml
version: '2'

services:
grafana:
image: grafana/grafana:latest
ports:
- '3000:3000'
environment:
GF_RENDERING_SERVER_URL: http://renderer:8081/render
GF_RENDERING_CALLBACK_URL: http://grafana:3000/
GF_LOG_FILTERS: rendering:debug
renderer:
image: grafana/grafana-image-renderer:latest
ports:
- 8081
```
```yaml
version: '2'

services:
grafana:
image: grafana/grafana:latest
ports:
- '3000:3000'
environment:
GF_RENDERING_SERVER_URL: http://renderer:8081/render
GF_RENDERING_CALLBACK_URL: http://grafana:3000/
GF_LOG_FILTERS: rendering:debug
renderer:
image: grafana/grafana-image-renderer:latest
ports:
- 8081
```
1. Next, run docker compose.
```bash
docker-compose up
```
```bash
docker-compose up
```

### Run as standalone Node.js application

Expand Down Expand Up @@ -122,3 +122,44 @@ For available configuration settings, please refer to [Grafana Image Rendering d

For troubleshooting help, refer to
[Grafana Image Rendering troubleshooting documentation](https://grafana.com/docs/grafana/latest/image-rendering/troubleshooting/).

## Testing

In order to run the image-renderer automated test suites, you need to run the following command from the root folder:

```
yarn test
```

This will launch a Grafana instance in Docker and, then, run the test suites.

_Notes:_

If there are some expected changes in the reference image files (located in `/tests/testdata`), run `yarn test-update` and push the updated references.

If the tests are failing and you want to see the difference between the image you get and the reference image, run `yarn test-diff`. This will generate images (called `diff_<test case>.png`) containing the differences in the `/tests/testdata` folder.

### Fixing Drone issues

If tests are successful in your local environement but fail in Drone. You can follow these steps to run the tests in an environment similar to the Drone pipeline. This will mount your local files of the `grafana-image-renderer` repo in the Docker image so any change that happens in the Docker image will be available in your local environment. This allows you to run `yarn test-diff` and `yarn test-update` in Docker and see the results locally.

1. Run the Drone environment in Docker:

```
cd ./devenv/docker/drone
docker-compose up
```

2. Open a terminal within the `drone-docker-puppeteer` container and run the following commands:

```
cd /drone/src
PUPPETEER_CACHE_DIR=/drone/src/cache yarn install --frozen-lockfile --no-progress
PUPPETEER_CACHE_DIR=/drone/src/cache CI=true yarn test-ci
```

_Notes:_
The tests might take longer in the Docker container. If you run into timeout issues, you can run the test command with the `--testTimeout option`:
```
PUPPETEER_CACHE_DIR=/drone/src/cache CI=true yarn test-ci --testTimeout=10000
```
20 changes: 20 additions & 0 deletions devenv/docker/drone/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: '2'

services:
docker-puppeteer:
image: grafana/docker-puppeteer:pre-node-20
volumes:
- "../../..:/drone/src"
stdin_open: true # docker run -i
tty: true # docker run -t
entrypoint: bash
grafana:
image: grafana/grafana-enterprise:latest
ports:
- 3000:3000
environment:
GF_FEATURE_TOGGLES_ENABLE: renderAuthJWT
volumes:
- ../test/dashboards.yaml:/etc/grafana/provisioning/dashboards/dashboards.yaml
- ../../../scripts/drone/provisioning/dashboards:/usr/share/grafana/dashboards
- ../../../scripts/drone/provisioning/datasources/datasources.yaml:/etc/grafana/provisioning/datasources/datasources.yaml
1 change: 1 addition & 0 deletions devenv/docker/test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This Docker environment is used to run E2E tests for the image renderer locally.
11 changes: 11 additions & 0 deletions devenv/docker/test/dashboards.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: 1

providers:
- name: 'Tests'
folder: 'tests'
folderUid: ''
type: file
allowUiUpdates: false
updateIntervalSeconds: 60
options:
path: dashboards
Loading

0 comments on commit aca8ee6

Please sign in to comment.