Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

Commit 26aed91

Browse files
Merge pull request #231 from grafana/e2e-docker
Run E2E Tests with Docker Compose
2 parents 8fb95bd + 286e7cb commit 26aed91

11 files changed

+1801
-1429
lines changed

Makefile

+17
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,27 @@ test-update: # Run all unit tests while copying test_output.json to compiled.js
2323
bitnami/jsonnet:0.16.0 \
2424
tests.sh update
2525

26+
E2E_GRAFANA_VERSION=7.0.3
27+
28+
e2e: # Run all end-to-end tests.
29+
GRAFANA_VERSION=${E2E_GRAFANA_VERSION} \
30+
docker-compose -f e2e/docker-compose.yml up \
31+
--abort-on-container-exit \
32+
--exit-code-from e2e
33+
34+
e2e-dev: # Run e2e tests in Cypress test runner.
35+
GRAFANA_VERSION=${E2E_GRAFANA_VERSION} \
36+
DISPLAY=$$(ipconfig getifaddr en0):0 \
37+
docker-compose -f e2e/docker-compose.dev.yml up \
38+
--abort-on-container-exit \
39+
--exit-code-from e2e
40+
2641
gen-api-docs: # Generate api-docs.md from source code comments.
2742
@docker run --rm \
2843
-w $$PWD \
2944
-v $$PWD:$$PWD \
3045
trotttrotttrott/jsonnetdoc:219e41b \
3146
grafonnet --markdown \
3247
> docs/api-docs.md
48+
49+
.PHONY: help test test-update e2e gen-api-docs

e2e/Dockerfile

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
ARG CYPRESS_IMAGE
2+
3+
FROM $CYPRESS_IMAGE
4+
WORKDIR /e2e
5+
6+
# dependencies will be installed only if the package files change
7+
COPY package.json .
8+
COPY package-lock.json .
9+
10+
# by setting CI environment variable we switch the Cypress install messages
11+
# to small "started / finished" and avoid 1000s of lines of progress messages
12+
# https://github.com/cypress-io/cypress/issues/1243
13+
ENV CI=1
14+
RUN npm ci
15+
# verify that Cypress has been installed correctly.
16+
# running this command separately from "cypress run" will also cache its result
17+
# to avoid verifying again when running the tests
18+
RUN npx cypress verify

e2e/README.md

+16-19
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,27 @@ These tests attempt to assert truth to the following:
1111
* Do elements get configured as intended?
1212
* Do the configured elements do what they're expected to do?
1313

14-
Some of this is automated here. However, the visual aspects are difficult for machines to cover. Even some behavioral aspects are as well because they incur an impractical amount of complexity, time, or cost. For those aspects, these tests provide a way to quickly generate dashboards consistently so we can use our human abilities to assert truth.
14+
Some of this is automated here. However, the visual aspects are difficult for
15+
machines to cover. Even some behavioral aspects are as well because they incur
16+
an impractical amount of complexity, time, or cost. For those aspects, these
17+
tests provide a way to quickly generate dashboards consistently so we can use
18+
our human abilities to assert truth.
1519

1620
## Usage
1721

18-
Install dependencies:
22+
`docker-compose` is used to run Cypress and Grafana. There are two targets in
23+
[Makefile](../Makefile) to help run it.
1924

20-
```
21-
yarn install
22-
```
25+
`make e2e`: runs tests headless and exits.
2326

24-
Run a Grafana instance to test with:
25-
26-
```
27-
yarn run grafana-instance
28-
```
29-
30-
Launch the [Cypress Test Runner](https://docs.cypress.io/guides/core-concepts/test-runner.html):
31-
32-
```
33-
yarn run cypress open
34-
```
27+
`make e2e-dev`: opens the [the test
28+
runner](https://docs.cypress.io/guides/core-concepts/test-runner.html#Overview)
29+
and exposes Grafana to the host machine - http://localhost:3030. This requires
30+
an X11 server to work. [This
31+
post](https://www.cypress.io/blog/2019/05/02/run-cypress-with-a-single-docker-command/#Interactive-mode)
32+
describes how to set this up with [XQuartz](https://www.xquartz.org/).
3533

3634
## Notes
3735

38-
Tests depend on compiled artifacts in [tests](../tests) for generating dashboards.
39-
40-
Tests do not destroy the dashboards they create after they're run. This is to facilitate manual inspection. Restart your Grafana instance to start fresh.
36+
Tests depend on compiled artifacts in [tests](../tests) for generating
37+
dashboards.

e2e/cypress.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
{
2-
"baseUrl": "http://admin:admin@localhost:3030"
32
}

e2e/cypress/integration/graph_panel_spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ describe('Graph Panel', function() {
55
let panelTitles = []
66

77
before(function() {
8-
cy.readFile('../tests/graph_panel/test_compiled.json').then(function(str) {
8+
cy.readFile('./tests/graph_panel/test_compiled.json').then(function(str) {
99
let panels = []
1010
for (let [i, [name, panel]] of Object.entries(Object.entries(str))) {
1111
panel['id'] = parseInt(i)

e2e/cypress/support/commands.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
const http = require("http")
22

3+
Cypress.Commands.overwrite('visit', (orig, url, options) => {
4+
options = options || {}
5+
options.auth = {
6+
username: 'admin',
7+
password: 'admin',
8+
}
9+
return orig(url, options)
10+
})
11+
312
Cypress.Commands.add('createDashboard', function(dashboardJSON) {
413

514
const payload = JSON.stringify({
@@ -9,8 +18,8 @@ Cypress.Commands.add('createDashboard', function(dashboardJSON) {
918

1019
const options = {
1120
auth: 'admin:admin',
12-
hostname: 'localhost',
13-
port: 3030,
21+
hostname: 'grafana',
22+
port: 3000,
1423
path: '/api/dashboards/db',
1524
method: 'POST',
1625
headers: {

e2e/docker-compose.dev.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
version: '3'
2+
services:
3+
grafana:
4+
image: grafana/grafana:${GRAFANA_VERSION?err}
5+
ports:
6+
- "3030:3000"
7+
e2e:
8+
build:
9+
context: .
10+
args:
11+
CYPRESS_IMAGE: cypress/included:4.7.0
12+
image: grafonnet-e2e-dev
13+
entrypoint: cypress open --project .
14+
depends_on:
15+
- grafana
16+
environment:
17+
- CYPRESS_baseUrl=http://grafana:3000
18+
- CYPRESS_video=false
19+
- DISPLAY=${DISPLAY?err}
20+
volumes:
21+
- ./cypress:/e2e/cypress
22+
- ./cypress.json:/e2e/cypress.json
23+
- ../tests:/e2e/tests
24+
- /tmp/.X11-unix:/tmp/.X11-unix

e2e/docker-compose.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
version: '3'
2+
services:
3+
grafana:
4+
image: grafana/grafana:${GRAFANA_VERSION?err}
5+
e2e:
6+
build:
7+
context: .
8+
args:
9+
CYPRESS_IMAGE: cypress/base:12
10+
image: grafonnet-e2e
11+
command: npx cypress run
12+
depends_on:
13+
- grafana
14+
environment:
15+
- CYPRESS_baseUrl=http://grafana:3000
16+
- CYPRESS_video=false
17+
volumes:
18+
- ./cypress:/e2e/cypress
19+
- ./cypress.json:/e2e/cypress.json
20+
- ../tests:/e2e/tests

0 commit comments

Comments
 (0)