Skip to content

Commit f3887cb

Browse files
Philipp SiegmantelTheSpacyCat
authored andcommitted
Add cypress e2e tests
1 parent 0330d2a commit f3887cb

File tree

15 files changed

+1382
-0
lines changed

15 files changed

+1382
-0
lines changed

.github/workflows/e2e.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: Run the e2e tests
2+
on: push
3+
jobs:
4+
e2e:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- uses: actions/checkout@v3
8+
- name: run the e2e tests
9+
run: docker-compose up --exit-code-from=cypress
10+
working-directory: tests/

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ e2e-results/
2929
# Editor
3030
.idea
3131

32+
33+
# cypress atrifacts
34+
tests/cypress/screenshots/
35+
tests/cypress/videos/

DEVELOPMENT.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,52 @@ yarn upgrade --latest
4646
## Release
4747

4848
TBD
49+
50+
51+
### E2E Tests
52+
There are serval ways to run the e2e tests.
53+
Make sure to have a up to date `dist/` folder using `yarn build`.
54+
55+
#### Local development use case
56+
```BASH
57+
cd tests/
58+
docker-compose up -d checkmk grafana
59+
yarn run cypress open
60+
```
61+
This will show you a nice interactive GUI to run and debug your E2E tests.
62+
See the official [docs](https://docs.cypress.io/guides/overview/why-cypress) for more information.
63+
64+
#### No Interactivity use case (e.g. CI)
65+
```BASH
66+
cd tests/
67+
docker-compose up --exit-code-from=cypress
68+
```
69+
This will run all tests without any further interaction necessary.
70+
71+
#### No docker use case
72+
If you don't want to or can't use docker at all, make sure you have a Grafana and a CheckMK instance running somewhere.
73+
The Plugin you want to test needs to be installed in you Grafana instance.
74+
75+
76+
You also need to set serval environment variables.
77+
78+
| Variable | Description |
79+
|-----------------------------|-------------|
80+
| CYPRESS_baseUrl | The URL to your Grafana instance |
81+
| CYPRESS_grafanaUsername | The username used to log into Grafana |
82+
| CYPRESS_grafanaPassword | the password used to log into Grafana|
83+
| CYPRESS_grafanaToCheckmkUrl | The url from which grafana can reach CheckMK |
84+
| CYPRESS_cypressToCheckmkUrl | The url from which cypress can reach CheckMK |
85+
| CYPRESS_cmkUsername | The username of a CheckMK admin |
86+
| CYPRESS_cmkPassword | The password of that CheckMK admin |
87+
88+
If everything is set up, just start cypress the usual way.
89+
```BASH
90+
yarn run cypress open
91+
92+
# or if you just want to see the results
93+
yarn run cypress run
94+
```
95+
96+
Please note that the test have side effects on your Grafana and CheckMK instance,
97+
such as creating a new automation user.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#! /usr/bin/env bash
2+
3+
echo "disable_web_api = False" >> /omd/sites/cmk/etc/check_mk/multisite.d/wato/global.mk
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
htpasswd -b -m /omd/sites/cmk/etc/htpasswd cmkadmin abskjfdalkdhjbld

tests/cypress.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = require('cypress').defineConfig({
2+
e2e: {
3+
baseUrl: 'http://localhost:3000',
4+
supportFile: false,
5+
specPattern: 'cypress/e2e/**/*.cy.{js,jsx,ts,tsx}',
6+
},
7+
});

tests/cypress.env.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"grafanaUsername": "admin",
3+
"grafanaPassword": "password",
4+
"grafanaToCheckmkUrl": "http://checkmk:5000/cmk",
5+
"cypressToCheckmkUrl": "http://localhost:12345/cmk",
6+
"cmkUsername": "cmkadmin",
7+
"cmkPassword": "abskjfdalkdhjbld"
8+
}

tests/cypress/e2e/spec.cy.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
describe('Source configuration', () => {
2+
const cmkUser = 'cmkuser';
3+
const cmkPassword = 'somepassword123457';
4+
5+
function createCmkAutomationUser() {
6+
cy.request({
7+
method: 'DELETE',
8+
url: Cypress.env('cypressToCheckmkUrl') + '/check_mk/api/1.0/objects/user_config/' + cmkUser,
9+
auth: {
10+
bearer: `${Cypress.env('cmkUsername')} ${Cypress.env('cmkPassword')}`,
11+
},
12+
failOnStatusCode: false,
13+
});
14+
cy.request({
15+
method: 'POST',
16+
url: Cypress.env('cypressToCheckmkUrl') + '/check_mk/api/1.0/domain-types/user_config/collections/all',
17+
auth: {
18+
bearer: `${Cypress.env('cmkUsername')} ${Cypress.env('cmkPassword')}`,
19+
},
20+
body: {
21+
username: cmkUser,
22+
fullname: cmkUser,
23+
roles: ['admin'],
24+
auth_option: {
25+
auth_type: 'automation',
26+
secret: cmkPassword,
27+
},
28+
},
29+
});
30+
}
31+
32+
it('configures the datasource correctly', () => {
33+
createCmkAutomationUser();
34+
35+
cy.visit('/');
36+
cy.get('input[name="user"]').type(Cypress.env('grafanaUsername'));
37+
cy.get('input[name="password"]').type(Cypress.env('grafanaPassword'));
38+
cy.get('[aria-label="Login button"]').click();
39+
40+
cy.visit('/datasources/new');
41+
cy.get('button[aria-label="Add data source Checkmk"]').contains('Checkmk').click();
42+
43+
cy.get('[data-test-id="checkmk-url"]').type(Cypress.env('grafanaToCheckmkUrl'));
44+
cy.get('[data-test-id="checkmk-username"]').type(cmkUser);
45+
cy.get('[data-test-id="checkmk-password"]').type(cmkPassword);
46+
47+
cy.get('[aria-label="Data source settings page Save and Test button"]').click();
48+
49+
cy.get('[data-testid="data-testid Alert success"]').should('be.visible');
50+
});
51+
});

tests/cypress/start.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
npx wait-on http://checkmk:5000 && cypress run

tests/cypress/support/commands.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/// <reference types="cypress" />
2+
// ***********************************************
3+
// This example commands.ts shows you how to
4+
// create various custom commands and overwrite
5+
// existing commands.
6+
//
7+
// For more comprehensive examples of custom
8+
// commands please read more here:
9+
// https://on.cypress.io/custom-commands
10+
// ***********************************************
11+
//
12+
//
13+
// -- This is a parent command --
14+
// Cypress.Commands.add('login', (email, password) => { ... })
15+
//
16+
//
17+
// -- This is a child command --
18+
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
19+
//
20+
//
21+
// -- This is a dual command --
22+
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
23+
//
24+
//
25+
// -- This will overwrite an existing command --
26+
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
27+
//
28+
// declare global {
29+
// namespace Cypress {
30+
// interface Chainable {
31+
// login(email: string, password: string): Chainable<void>
32+
// drag(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
33+
// dismiss(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
34+
// visit(originalFn: CommandOriginalFn, url: string, options: Partial<VisitOptions>): Chainable<Element>
35+
// }
36+
// }
37+
// }

0 commit comments

Comments
 (0)