mirror of https://github.com/grafana/grafana
Alerting: Add tests for combination of switch modes in the alert rule form (#98052)
* Add tests for combination of switch * refactor test * use alerting option for default datasource in RuleEditor Grafana rules test * add test for grafana recording rules * use enum for grafana steps in tests * dont make each test dependent on previous localstorage * add test for local storagepull/98620/head
parent
299e16b61a
commit
9c7a355afd
@ -0,0 +1,119 @@ |
||||
import { UserEvent } from '@testing-library/user-event'; |
||||
import * as React from 'react'; |
||||
import { renderRuleEditor, ui } from 'test/helpers/alertingRuleEditor'; |
||||
import { clickSelectOption } from 'test/helpers/selectOptionInTest'; |
||||
import { screen } from 'test/test-utils'; |
||||
import { byRole } from 'testing-library-selector'; |
||||
|
||||
import { FeatureToggles } from '@grafana/data'; |
||||
import { contextSrv } from 'app/core/services/context_srv'; |
||||
import { setupMswServer } from 'app/features/alerting/unified/mockApi'; |
||||
import { PROMETHEUS_DATASOURCE_UID } from 'app/features/alerting/unified/mocks/server/constants'; |
||||
import { AccessControlAction } from 'app/types'; |
||||
|
||||
import { grantUserPermissions, mockDataSource } from './mocks'; |
||||
import { grafanaRulerGroup } from './mocks/grafanaRulerApi'; |
||||
import { captureRequests, serializeRequests } from './mocks/server/events'; |
||||
import { FOLDER_TITLE_HAPPY_PATH } from './mocks/server/handlers/search'; |
||||
import { testWithFeatureToggles } from './test/test-utils'; |
||||
import { setupDataSources } from './testSetup/datasources'; |
||||
|
||||
jest.mock('app/core/components/AppChrome/AppChromeUpdate', () => ({ |
||||
AppChromeUpdate: ({ actions }: { actions: React.ReactNode }) => <div>{actions}</div>, |
||||
})); |
||||
|
||||
jest.setTimeout(60 * 1000); |
||||
|
||||
setupMswServer(); |
||||
|
||||
const selectFolderAndGroup = async (user: UserEvent) => { |
||||
await user.click(await screen.findByRole('button', { name: /select folder/i })); |
||||
await user.click(await screen.findByLabelText(FOLDER_TITLE_HAPPY_PATH)); |
||||
const groupInput = await ui.inputs.group.find(); |
||||
await user.click(await byRole('combobox').find(groupInput)); |
||||
await clickSelectOption(groupInput, grafanaRulerGroup.name); |
||||
}; |
||||
|
||||
const dataSources = { |
||||
default: mockDataSource( |
||||
{ |
||||
type: 'prometheus', |
||||
name: 'Prom', |
||||
uid: PROMETHEUS_DATASOURCE_UID, |
||||
isDefault: true, |
||||
}, |
||||
{ alerting: true, module: 'core:plugin/prometheus' } |
||||
), |
||||
}; |
||||
describe('RuleEditor grafana recording rules', () => { |
||||
beforeEach(() => { |
||||
jest.clearAllMocks(); |
||||
setupDataSources(dataSources.default); |
||||
contextSrv.isEditor = true; |
||||
contextSrv.hasEditPermissionInFolders = true; |
||||
grantUserPermissions([ |
||||
AccessControlAction.AlertingRuleRead, |
||||
AccessControlAction.AlertingRuleUpdate, |
||||
AccessControlAction.AlertingRuleDelete, |
||||
AccessControlAction.AlertingRuleCreate, |
||||
AccessControlAction.DataSourcesRead, |
||||
AccessControlAction.DataSourcesWrite, |
||||
AccessControlAction.DataSourcesCreate, |
||||
AccessControlAction.FoldersWrite, |
||||
AccessControlAction.FoldersRead, |
||||
AccessControlAction.AlertingRuleExternalRead, |
||||
AccessControlAction.AlertingRuleExternalWrite, |
||||
]); |
||||
}); |
||||
|
||||
const testCreateGrafanaRR = (featureToggles: Array<keyof FeatureToggles>, testName: string) => { |
||||
testWithFeatureToggles(featureToggles); |
||||
|
||||
it(testName, async () => { |
||||
const capture = captureRequests((r) => r.method === 'POST' && r.url.includes('/api/ruler/')); |
||||
|
||||
const { user } = renderRuleEditor(undefined, 'grafana-recording'); |
||||
|
||||
await user.type(await ui.inputs.name.find(), 'my great new rule'); |
||||
await user.type(await ui.inputs.metric.find(), 'metricName'); |
||||
await selectFolderAndGroup(user); |
||||
|
||||
await user.click(ui.buttons.saveAndExit.get()); |
||||
|
||||
const requests = await capture; |
||||
const serializedRequests = await serializeRequests(requests); |
||||
expect(serializedRequests).toMatchSnapshot(); |
||||
}); |
||||
}; |
||||
|
||||
const testCreateGrafanaRRWithInvalidMetricName = (featureToggles: Array<keyof FeatureToggles>, testName: string) => { |
||||
testWithFeatureToggles(featureToggles); |
||||
|
||||
it(testName, async () => { |
||||
const capture = captureRequests((r) => r.method === 'POST' && r.url.includes('/api/ruler/')); |
||||
const { user } = renderRuleEditor(undefined, 'grafana-recording'); |
||||
|
||||
await user.type(await ui.inputs.name.find(), 'my great new rule'); |
||||
await selectFolderAndGroup(user); |
||||
|
||||
await user.click(ui.buttons.saveAndExit.get()); |
||||
const requests = await capture; |
||||
expect(requests).toHaveLength(0); |
||||
}); |
||||
}; |
||||
|
||||
testCreateGrafanaRR([], 'can create new grafana recording rule with simplified steps feature toggles disabled'); |
||||
testCreateGrafanaRR( |
||||
['alertingQueryAndExpressionsStepMode', 'alertingNotificationsStepMode'], |
||||
'can create new grafana recording rule with simplified steps enabled' |
||||
); |
||||
|
||||
testCreateGrafanaRRWithInvalidMetricName( |
||||
[], |
||||
'cannot create new grafana recording rule with invalid metric name with simplified steps feature toggles disabled' |
||||
); |
||||
testCreateGrafanaRRWithInvalidMetricName( |
||||
['alertingQueryAndExpressionsStepMode', 'alertingNotificationsStepMode'], |
||||
'cannot create new grafana recording rule with invalid metric name with simplified steps enabled' |
||||
); |
||||
}); |
@ -0,0 +1,259 @@ |
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP |
||||
|
||||
exports[`RuleEditor grafana recording rules can create new grafana recording rule with simplified steps enabled 1`] = ` |
||||
[ |
||||
{ |
||||
"body": { |
||||
"interval": "1m", |
||||
"name": "grafana-group-1", |
||||
"rules": [ |
||||
{ |
||||
"annotations": { |
||||
"summary": "Test alert", |
||||
}, |
||||
"for": "5m", |
||||
"grafana_alert": { |
||||
"condition": "A", |
||||
"data": [ |
||||
{ |
||||
"datasourceUid": "datasource-uid", |
||||
"model": { |
||||
"datasource": { |
||||
"type": "prometheus", |
||||
"uid": "datasource-uid", |
||||
}, |
||||
"expression": "vector(1)", |
||||
"queryType": "alerting", |
||||
"refId": "A", |
||||
}, |
||||
"queryType": "alerting", |
||||
"refId": "A", |
||||
"relativeTimeRange": { |
||||
"from": 1000, |
||||
"to": 2000, |
||||
}, |
||||
}, |
||||
], |
||||
"exec_err_state": "Error", |
||||
"is_paused": false, |
||||
"namespace_uid": "uuid020c61ef", |
||||
"no_data_state": "NoData", |
||||
"rule_group": "grafana-group-1", |
||||
"title": "Grafana-rule", |
||||
"uid": "4d7125fee983", |
||||
}, |
||||
"labels": { |
||||
"region": "nasa", |
||||
"severity": "critical", |
||||
}, |
||||
}, |
||||
{ |
||||
"annotations": {}, |
||||
"grafana_alert": { |
||||
"condition": "B", |
||||
"data": [ |
||||
{ |
||||
"datasourceUid": "prometheus", |
||||
"model": { |
||||
"instant": true, |
||||
"refId": "A", |
||||
}, |
||||
"queryType": "", |
||||
"refId": "A", |
||||
"relativeTimeRange": { |
||||
"from": 600, |
||||
"to": 0, |
||||
}, |
||||
}, |
||||
{ |
||||
"datasourceUid": "__expr__", |
||||
"model": { |
||||
"conditions": [ |
||||
{ |
||||
"evaluator": { |
||||
"params": [], |
||||
"type": "gt", |
||||
}, |
||||
"operator": { |
||||
"type": "and", |
||||
}, |
||||
"query": { |
||||
"params": [ |
||||
"B", |
||||
], |
||||
}, |
||||
"reducer": { |
||||
"params": [], |
||||
"type": "last", |
||||
}, |
||||
"type": "query", |
||||
}, |
||||
], |
||||
"datasource": { |
||||
"type": "__expr__", |
||||
"uid": "__expr__", |
||||
}, |
||||
"expression": "A", |
||||
"reducer": "last", |
||||
"refId": "B", |
||||
"type": "reduce", |
||||
}, |
||||
"queryType": "", |
||||
"refId": "B", |
||||
}, |
||||
], |
||||
"is_paused": false, |
||||
"record": { |
||||
"from": "B", |
||||
"metric": "metricName", |
||||
}, |
||||
"title": "my great new rule", |
||||
}, |
||||
"labels": {}, |
||||
}, |
||||
], |
||||
}, |
||||
"headers": [ |
||||
[ |
||||
"content-type", |
||||
"application/json", |
||||
], |
||||
[ |
||||
"accept", |
||||
"application/json, text/plain, */*", |
||||
], |
||||
], |
||||
"method": "POST", |
||||
"url": "http://localhost/api/ruler/grafana/api/v1/rules/uuid020c61ef?subtype=cortex", |
||||
}, |
||||
] |
||||
`; |
||||
|
||||
exports[`RuleEditor grafana recording rules can create new grafana recording rule with simplified steps feature toggles disabled 1`] = ` |
||||
[ |
||||
{ |
||||
"body": { |
||||
"interval": "1m", |
||||
"name": "grafana-group-1", |
||||
"rules": [ |
||||
{ |
||||
"annotations": { |
||||
"summary": "Test alert", |
||||
}, |
||||
"for": "5m", |
||||
"grafana_alert": { |
||||
"condition": "A", |
||||
"data": [ |
||||
{ |
||||
"datasourceUid": "datasource-uid", |
||||
"model": { |
||||
"datasource": { |
||||
"type": "prometheus", |
||||
"uid": "datasource-uid", |
||||
}, |
||||
"expression": "vector(1)", |
||||
"queryType": "alerting", |
||||
"refId": "A", |
||||
}, |
||||
"queryType": "alerting", |
||||
"refId": "A", |
||||
"relativeTimeRange": { |
||||
"from": 1000, |
||||
"to": 2000, |
||||
}, |
||||
}, |
||||
], |
||||
"exec_err_state": "Error", |
||||
"is_paused": false, |
||||
"namespace_uid": "uuid020c61ef", |
||||
"no_data_state": "NoData", |
||||
"rule_group": "grafana-group-1", |
||||
"title": "Grafana-rule", |
||||
"uid": "4d7125fee983", |
||||
}, |
||||
"labels": { |
||||
"region": "nasa", |
||||
"severity": "critical", |
||||
}, |
||||
}, |
||||
{ |
||||
"annotations": {}, |
||||
"grafana_alert": { |
||||
"condition": "B", |
||||
"data": [ |
||||
{ |
||||
"datasourceUid": "prometheus", |
||||
"model": { |
||||
"instant": true, |
||||
"refId": "A", |
||||
}, |
||||
"queryType": "", |
||||
"refId": "A", |
||||
"relativeTimeRange": { |
||||
"from": 600, |
||||
"to": 0, |
||||
}, |
||||
}, |
||||
{ |
||||
"datasourceUid": "__expr__", |
||||
"model": { |
||||
"conditions": [ |
||||
{ |
||||
"evaluator": { |
||||
"params": [], |
||||
"type": "gt", |
||||
}, |
||||
"operator": { |
||||
"type": "and", |
||||
}, |
||||
"query": { |
||||
"params": [ |
||||
"B", |
||||
], |
||||
}, |
||||
"reducer": { |
||||
"params": [], |
||||
"type": "last", |
||||
}, |
||||
"type": "query", |
||||
}, |
||||
], |
||||
"datasource": { |
||||
"type": "__expr__", |
||||
"uid": "__expr__", |
||||
}, |
||||
"expression": "A", |
||||
"reducer": "last", |
||||
"refId": "B", |
||||
"type": "reduce", |
||||
}, |
||||
"queryType": "", |
||||
"refId": "B", |
||||
}, |
||||
], |
||||
"is_paused": false, |
||||
"record": { |
||||
"from": "B", |
||||
"metric": "metricName", |
||||
}, |
||||
"title": "my great new rule", |
||||
}, |
||||
"labels": {}, |
||||
}, |
||||
], |
||||
}, |
||||
"headers": [ |
||||
[ |
||||
"content-type", |
||||
"application/json", |
||||
], |
||||
[ |
||||
"accept", |
||||
"application/json, text/plain, */*", |
||||
], |
||||
], |
||||
"method": "POST", |
||||
"url": "http://localhost/api/ruler/grafana/api/v1/rules/uuid020c61ef?subtype=cortex", |
||||
}, |
||||
] |
||||
`; |
@ -0,0 +1,168 @@ |
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP |
||||
|
||||
exports[`RuleEditor grafana managed rules can create new grafana managed alert 1`] = ` |
||||
[ |
||||
{ |
||||
"body": { |
||||
"interval": "1m", |
||||
"name": "grafana-group-1", |
||||
"rules": [ |
||||
{ |
||||
"annotations": { |
||||
"summary": "Test alert", |
||||
}, |
||||
"for": "5m", |
||||
"grafana_alert": { |
||||
"condition": "A", |
||||
"data": [ |
||||
{ |
||||
"datasourceUid": "datasource-uid", |
||||
"model": { |
||||
"datasource": { |
||||
"type": "prometheus", |
||||
"uid": "datasource-uid", |
||||
}, |
||||
"expression": "vector(1)", |
||||
"queryType": "alerting", |
||||
"refId": "A", |
||||
}, |
||||
"queryType": "alerting", |
||||
"refId": "A", |
||||
"relativeTimeRange": { |
||||
"from": 1000, |
||||
"to": 2000, |
||||
}, |
||||
}, |
||||
], |
||||
"exec_err_state": "Error", |
||||
"is_paused": false, |
||||
"namespace_uid": "uuid020c61ef", |
||||
"no_data_state": "NoData", |
||||
"rule_group": "grafana-group-1", |
||||
"title": "Grafana-rule", |
||||
"uid": "4d7125fee983", |
||||
}, |
||||
"labels": { |
||||
"region": "nasa", |
||||
"severity": "critical", |
||||
}, |
||||
}, |
||||
{ |
||||
"annotations": { |
||||
"description": "some description", |
||||
}, |
||||
"for": "1m", |
||||
"grafana_alert": { |
||||
"condition": "C", |
||||
"data": [ |
||||
{ |
||||
"datasourceUid": "prometheus", |
||||
"model": { |
||||
"instant": true, |
||||
"refId": "A", |
||||
}, |
||||
"queryType": "", |
||||
"refId": "A", |
||||
"relativeTimeRange": { |
||||
"from": 600, |
||||
"to": 0, |
||||
}, |
||||
}, |
||||
{ |
||||
"datasourceUid": "__expr__", |
||||
"model": { |
||||
"conditions": [ |
||||
{ |
||||
"evaluator": { |
||||
"params": [], |
||||
"type": "gt", |
||||
}, |
||||
"operator": { |
||||
"type": "and", |
||||
}, |
||||
"query": { |
||||
"params": [ |
||||
"B", |
||||
], |
||||
}, |
||||
"reducer": { |
||||
"params": [], |
||||
"type": "last", |
||||
}, |
||||
"type": "query", |
||||
}, |
||||
], |
||||
"datasource": { |
||||
"type": "__expr__", |
||||
"uid": "__expr__", |
||||
}, |
||||
"expression": "A", |
||||
"reducer": "last", |
||||
"refId": "B", |
||||
"type": "reduce", |
||||
}, |
||||
"queryType": "", |
||||
"refId": "B", |
||||
}, |
||||
{ |
||||
"datasourceUid": "__expr__", |
||||
"model": { |
||||
"conditions": [ |
||||
{ |
||||
"evaluator": { |
||||
"params": [ |
||||
0, |
||||
], |
||||
"type": "gt", |
||||
}, |
||||
"operator": { |
||||
"type": "and", |
||||
}, |
||||
"query": { |
||||
"params": [ |
||||
"C", |
||||
], |
||||
}, |
||||
"reducer": { |
||||
"params": [], |
||||
"type": "last", |
||||
}, |
||||
"type": "query", |
||||
}, |
||||
], |
||||
"datasource": { |
||||
"type": "__expr__", |
||||
"uid": "__expr__", |
||||
}, |
||||
"expression": "B", |
||||
"refId": "C", |
||||
"type": "threshold", |
||||
}, |
||||
"queryType": "", |
||||
"refId": "C", |
||||
}, |
||||
], |
||||
"exec_err_state": "Error", |
||||
"is_paused": false, |
||||
"no_data_state": "NoData", |
||||
"title": "my great new rule", |
||||
}, |
||||
"labels": {}, |
||||
}, |
||||
], |
||||
}, |
||||
"headers": [ |
||||
[ |
||||
"content-type", |
||||
"application/json", |
||||
], |
||||
[ |
||||
"accept", |
||||
"application/json, text/plain, */*", |
||||
], |
||||
], |
||||
"method": "POST", |
||||
"url": "http://localhost/api/ruler/grafana/api/v1/rules/uuid020c61ef?subtype=cortex", |
||||
}, |
||||
] |
||||
`; |
Loading…
Reference in new issue