Skip to content

Commit

Permalink
E2E Testing with Cypress (#191)
Browse files Browse the repository at this point in the history
Introduction of end to end tests to facilitate with with development, code review, regression, etc.

* bootstrap cypress
* graph_panel_spec
  • Loading branch information
trotttrotttrott authored Apr 19, 2020
1 parent 877fed0 commit b495d3c
Show file tree
Hide file tree
Showing 12 changed files with 1,594 additions and 57 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
site
*_test_output.json
e2e/node_modules
.DS_Store
40 changes: 40 additions & 0 deletions e2e/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Grafonnet End to End Testing

Grafonnet uses [Cypress](https://www.cypress.io).

## Purpose

These tests attempt to assert truth to the following:

* Does this library generate valid dashboard JSON?
* Are dashboard elements displayed as expected?
* Do elements get configured as intended?
* Do the configured elements do what they're expected to do?

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.

## Usage

Install dependencies:

```
yarn install
```

Run a Grafana instance to test with:

```
yarn run grafana-instance
```

Launch the [Cypress Test Runner](https://docs.cypress.io/guides/core-concepts/test-runner.html):

```
yarn run cypress open
```

## Notes

Tests depend on compiled artifacts in [tests](../tests) for generating dashboards.

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.
3 changes: 3 additions & 0 deletions e2e/cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"baseUrl": "http://admin:admin@localhost:3030"
}
Empty file added e2e/cypress/fixtures/.keep
Empty file.
36 changes: 36 additions & 0 deletions e2e/cypress/integration/graph_panel_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const fs = require('fs')

describe('Graph Panel', function() {

let panelTitles = []

before(function() {
cy.readFile('../tests/graph_panel/test_compiled.json').then(function(str) {
let panels = []
for (let [i, [name, panel]] of Object.entries(Object.entries(str))) {
panel['id'] = parseInt(i)
panel['gridPos'] = {'w': 6, 'h': 4, 'x': i * 6 % 24 }
if (name == "alerts" || name == "alertsWithMultipleConditions") {
// Skip panels with alerts. They are incompatible with the
// test datasource and result in 500 errors.
continue
}
panelTitles.push(panel.title)
panels.push(panel)
}
let dashboardJSON = {
"uid": "graph-panel",
"title": "Graph Panel",
"panels": panels
}
cy.createDashboard(dashboardJSON)
})
})

it('renders all graph panels', function() {
cy.visit('/d/graph-panel/graph-panel')
for (const title of panelTitles) {
cy.contains(title)
}
})
})
21 changes: 21 additions & 0 deletions e2e/cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// <reference types="cypress" />
// ***********************************************************
// This example plugins/index.js can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************

// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)

/**
* @type {Cypress.PluginConfig}
*/
module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
}
24 changes: 24 additions & 0 deletions e2e/cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const http = require("http")

Cypress.Commands.add('createDashboard', function(dashboardJSON) {

const payload = JSON.stringify({
dashboard: dashboardJSON,
overwrite: true
})

const options = {
auth: 'admin:admin',
hostname: 'localhost',
port: 3030,
path: '/api/dashboards/db',
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
}

const req = http.request(options)
req.write(payload)
req.end()
})
1 change: 1 addition & 0 deletions e2e/cypress/support/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./commands')
9 changes: 9 additions & 0 deletions e2e/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"license": "Apache-2.0",
"scripts": {
"grafana-instance": "docker run --rm --name=grafana -p 3030:3000 grafana/grafana:6.6.2"
},
"devDependencies": {
"cypress": "^4.4.0"
}
}
Loading

0 comments on commit b495d3c

Please sign in to comment.