Skip to content

Commit 706e659

Browse files
Ukochkaacademo
andauthored
Playwright tests for datasource plugins examples (#315)
* WIP * datasource-logs e2e tests * WIP http-datasource * WIP: backend mock server * Update integration tests action * Fix action * Update action * Use github output * Add e2e script * Stop mockserver after canary tests * e2e tests for streaming-backend-datasource * e2e tests for streaming-websocket-datasource * return datasource-http test back * fix ci * WIP test CI * fix CI build * WIP fix the build * skip queryeditpr tests for datasource-streamiing-backend-websocket * return datasource.go to iitial state * change id to testid for datasource-logs tests --------- Co-authored-by: Esteban Beltran <[email protected]>
1 parent 6010cb9 commit 706e659

File tree

32 files changed

+8130
-16496
lines changed

32 files changed

+8130
-16496
lines changed

examples/datasource-http-backend/package-lock.json

+131-260
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/datasource-logs/package-lock.json

+101
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/datasource-logs/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"scripts": {
66
"build": "webpack -c ./.config/webpack/webpack.config.ts --env production",
77
"dev": "webpack -w -c ./.config/webpack/webpack.config.ts --env development",
8+
"e2e": "playwright test",
89
"test": "jest --watch --onlyChanged",
910
"test:ci": "jest --passWithNoTests --maxWorkers 4",
1011
"typecheck": "tsc --noEmit",
@@ -19,6 +20,8 @@
1920
"@babel/core": "^7.21.4",
2021
"@grafana/eslint-config": "^7.0.0",
2122
"@grafana/tsconfig": "1.3.0-rc1",
23+
"@grafana/plugin-e2e": "1.3.1",
24+
"@playwright/test": "^1.43.0",
2225
"@swc/core": "^1.3.90",
2326
"@swc/helpers": "^0.5.0",
2427
"@swc/jest": "^0.2.26",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import type { PluginOptions } from '@grafana/plugin-e2e';
2+
import { defineConfig, devices } from '@playwright/test';
3+
import { dirname } from 'node:path';
4+
5+
const pluginE2eAuth = `${dirname(require.resolve('@grafana/plugin-e2e'))}/auth`;
6+
7+
/**
8+
* Read environment variables from file.
9+
* https://github.com/motdotla/dotenv
10+
*/
11+
// require('dotenv').config();
12+
13+
/**
14+
* See https://playwright.dev/docs/test-configuration.
15+
*/
16+
export default defineConfig<PluginOptions>({
17+
testDir: './tests',
18+
/* Run tests in files in parallel */
19+
fullyParallel: true,
20+
/* Fail the build on CI if you accidentally left test.only in the source code. */
21+
forbidOnly: !!process.env.CI,
22+
/* Retry on CI only */
23+
retries: process.env.CI ? 2 : 0,
24+
/* Opt out of parallel tests on CI. */
25+
workers: process.env.CI ? 1 : undefined,
26+
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
27+
reporter: 'html',
28+
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
29+
use: {
30+
/* Base URL to use in actions like `await page.goto('/')`. */
31+
baseURL: 'http://localhost:3000',
32+
33+
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
34+
trace: 'on-first-retry',
35+
},
36+
37+
/* Configure projects for major browsers */
38+
projects: [
39+
// 1. Login to Grafana and store the cookie on disk for use in other tests.
40+
{
41+
name: 'auth',
42+
testDir: pluginE2eAuth,
43+
testMatch: [/.*\.js/],
44+
},
45+
// 2. Run tests in Google Chrome. Every test will start authenticated as admin user.
46+
{
47+
name: 'chromium',
48+
use: { ...devices['Desktop Chrome'], storageState: 'playwright/.auth/admin.json' },
49+
dependencies: ['auth'],
50+
},
51+
],
52+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
apiVersion: 1
2+
3+
datasources:
4+
- name: Datasource logs
5+
type: example-logs-datasource
6+
access: proxy
7+
isDefault: true
8+
orgId: 1
9+
version: 1
10+
editable: true
11+
jsonData:
12+
path: '/resources'
13+
defaultTimeField:
14+
secureJsonData:
15+
apiKey: 'api-key'

examples/datasource-logs/src/components/QueryEditor.tsx

+13-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,21 @@ export function QueryEditor({ query, onChange }: Props) {
1818
return (
1919
<Stack gap={0}>
2020
<InlineField label="Query" labelWidth={20} tooltip="Query text">
21-
<Input onChange={onQueryTextChange} value={query.queryText || ''} width={70} />
21+
<Input
22+
data-testid="query-editor-query-text"
23+
onChange={onQueryTextChange}
24+
value={query.queryText || ''}
25+
width={70}
26+
/>
2227
</InlineField>
2328
<InlineField label="Log limit" labelWidth={20} tooltip="Log limit">
24-
<Input onChange={onQueryLimitChange} value={query.limit || 100} width={20} type="number" />
29+
<Input
30+
data-testid="query-editor-log-limit"
31+
onChange={onQueryLimitChange}
32+
value={query.limit || 100}
33+
width={20}
34+
type="number"
35+
/>
2536
</InlineField>
2637
</Stack>
2738
);

examples/datasource-logs/src/plugin.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/grafana/grafana/main/docs/sources/developers/plugins/plugin.schema.json",
33
"type": "datasource",
4-
"name": "example-logs",
5-
"id": "grafana-logs-datasource",
4+
"name": "grafana-logs-datasource",
5+
"id": "example-logs-datasource",
66
"metrics": true,
77
"logs": true,
88
"streaming": true,

examples/datasource-logs/src/types.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DataQuery } from '@grafana/schema'
1+
import { DataQuery } from '@grafana/schema';
22

33
export interface MyQuery extends DataQuery {
44
queryText: string;
@@ -7,6 +7,10 @@ export interface MyQuery extends DataQuery {
77

88
export interface MyDataSourceOptions {}
99

10+
export interface DataSourceResponse {
11+
datapoints: string[];
12+
}
13+
1014
export const DEFAULT_QUERY: Partial<MyQuery> = {
1115
queryText: '',
1216
// default limit is 100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { test, expect } from '@grafana/plugin-e2e';
2+
import { MyQuery } from '../src/types';
3+
4+
test('"Save & test" should display success alert box when config is valid', async ({
5+
createDataSourceConfigPage,
6+
readProvisionedDataSource,
7+
page,
8+
selectors,
9+
}) => {
10+
const ds = await readProvisionedDataSource({ fileName: 'datasources.yml' });
11+
const configPage = await createDataSourceConfigPage({ type: ds.type });
12+
13+
await configPage.getByGrafanaSelector(selectors.pages.DataSource.saveAndTest).click();
14+
await expect(configPage).toHaveAlert('success');
15+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { test, expect } from '@grafana/plugin-e2e';
2+
3+
test('logs should show certain values', async ({ page, selectors, panelEditPage, readProvisionedDataSource }) => {
4+
const ds = await readProvisionedDataSource({ fileName: 'datasources.yml' });
5+
await panelEditPage.datasource.set(ds.name);
6+
await panelEditPage.setVisualization('Table');
7+
await panelEditPage.getQueryEditorRow('A').getByTestId('query-editor-query-text', { exact: true }).fill('number');
8+
await panelEditPage.getQueryEditorRow('A').getByTestId('query-editor-log-limit', { exact: true }).fill('10');
9+
const refreshPanelButton = panelEditPage.getByGrafanaSelector(selectors.components.RefreshPicker.runButtonV2, {
10+
root: panelEditPage.getByGrafanaSelector(selectors.components.PanelEditor.General.content),
11+
});
12+
await refreshPanelButton.click();
13+
await expect(panelEditPage.panel.data).toContainText(['number=']);
14+
});

examples/datasource-streaming-backend-websocket/streaming-backend-websocket-plugin/docker-compose.yaml

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ version: '3.0'
22

33
services:
44
grafana:
5-
container_name: 'example-websocket-datasource'
5+
container_name: 'example-websocket-backend-datasource'
66
build:
77
context: ./.config
88
args:
@@ -13,8 +13,10 @@ services:
1313
volumes:
1414
- ./dist:/var/lib/grafana/plugins/example-websocket-datasource
1515
- ./provisioning:/etc/grafana/provisioning
16-
websocket-server:
17-
container_name: 'websocket-server-example'
16+
extra_hosts:
17+
- 'host.docker.internal:host-gateway'
18+
mockserver:
19+
container_name: 'mockserver-streamingbackend-datasource'
1820
build:
1921
context: ../websocket-server
2022
ports:

0 commit comments

Comments
 (0)