From f4d55d2612a2c9108766701a7c6f1d0ae00f000a Mon Sep 17 00:00:00 2001 From: Tom Ratcliffe Date: Wed, 15 May 2024 16:53:16 +0100 Subject: [PATCH] Alerting: Restructure mock server handlers slightly and remove unnecessary handlers (#87759) --- .../unified/RuleEditorCloudRules.test.tsx | 9 +---- .../unified/RuleEditorGrafanaRules.test.tsx | 11 ++---- .../unified/RuleEditorRecordingRule.test.tsx | 10 ++---- .../alerting/unified/Silences.test.tsx | 6 ++-- .../export/GrafanaModifyExport.test.tsx | 21 +++-------- .../form/GrafanaReceiverForm.test.tsx | 1 - .../onCall/useOnCallIntegration.test.ts | 1 - .../SimplifiedRuleEditor.test.tsx | 5 ++- .../rule-viewer/__mocks__/server.ts | 3 +- .../app/features/alerting/unified/mockApi.ts | 10 +----- .../alerting/unified/mocks/alertmanagerApi.ts | 25 +------------ .../unified/mocks/server/all-handlers.ts | 24 +++++++++++++ .../unified/mocks/server/configure.ts | 2 +- .../alerting/unified/mocks/server/handlers.ts | 36 ------------------- .../mocks/server/handlers/alertmanagers.ts | 30 ++++++++++++++++ .../{ => server/handlers}/datasources.ts | 15 +++++--- .../unified/mocks/server/handlers/eval.ts | 14 ++++++++ .../mocks/{ => server/handlers}/folders.ts | 6 +++- .../mocks/{ => server/handlers}/plugins.ts | 5 ++- .../mocks/{ => server/handlers}/silences.ts | 13 ++++--- .../alerting/unified/testSetup/plugins.ts | 27 ++++++++++++-- 21 files changed, 138 insertions(+), 136 deletions(-) create mode 100644 public/app/features/alerting/unified/mocks/server/all-handlers.ts delete mode 100644 public/app/features/alerting/unified/mocks/server/handlers.ts create mode 100644 public/app/features/alerting/unified/mocks/server/handlers/alertmanagers.ts rename public/app/features/alerting/unified/mocks/{ => server/handlers}/datasources.ts (61%) create mode 100644 public/app/features/alerting/unified/mocks/server/handlers/eval.ts rename public/app/features/alerting/unified/mocks/{ => server/handlers}/folders.ts (59%) rename public/app/features/alerting/unified/mocks/{ => server/handlers}/plugins.ts (79%) rename public/app/features/alerting/unified/mocks/{ => server/handlers}/silences.ts (80%) diff --git a/public/app/features/alerting/unified/RuleEditorCloudRules.test.tsx b/public/app/features/alerting/unified/RuleEditorCloudRules.test.tsx index e0fac544479..7868c5c3abe 100644 --- a/public/app/features/alerting/unified/RuleEditorCloudRules.test.tsx +++ b/public/app/features/alerting/unified/RuleEditorCloudRules.test.tsx @@ -14,12 +14,7 @@ import { fetchRulerRules, fetchRulerRulesGroup, fetchRulerRulesNamespace, setRul import { ExpressionEditorProps } from './components/rule-editor/ExpressionEditor'; import { mockApi, mockFeatureDiscoveryApi, setupMswServer } from './mockApi'; import { grantUserPermissions, labelsPluginMetaMock, mockDataSource } from './mocks'; -import { - defaultGrafanaAlertingConfigurationStatusResponse, - emptyExternalAlertmanagersResponse, - mockAlertmanagerChoiceResponse, - mockAlertmanagersResponse, -} from './mocks/alertmanagerApi'; +import { emptyExternalAlertmanagersResponse, mockAlertmanagersResponse } from './mocks/alertmanagerApi'; import { fetchRulerRulesIfNotFetchedYet } from './state/actions'; import { setupDataSources } from './testSetup/datasources'; import { buildInfoResponse } from './testSetup/featureDiscovery'; @@ -55,9 +50,7 @@ setupDataSources(dataSources.default); const server = setupMswServer(); mockFeatureDiscoveryApi(server).discoverDsFeatures(dataSources.default, buildInfoResponse.mimir); -mockAlertmanagerChoiceResponse(server, defaultGrafanaAlertingConfigurationStatusResponse); mockAlertmanagersResponse(server, emptyExternalAlertmanagersResponse); -mockApi(server).eval({ results: {} }); mockApi(server).plugins.getPluginSettings({ ...labelsPluginMetaMock, enabled: false }); // these tests are rather slow because we have to wait for various API calls and mocks to be called diff --git a/public/app/features/alerting/unified/RuleEditorGrafanaRules.test.tsx b/public/app/features/alerting/unified/RuleEditorGrafanaRules.test.tsx index 66d11fd9c22..f630d4abbc6 100644 --- a/public/app/features/alerting/unified/RuleEditorGrafanaRules.test.tsx +++ b/public/app/features/alerting/unified/RuleEditorGrafanaRules.test.tsx @@ -4,15 +4,10 @@ import React from 'react'; import { renderRuleEditor, ui } from 'test/helpers/alertingRuleEditor'; import { clickSelectOption } from 'test/helpers/selectOptionInTest'; import { byRole } from 'testing-library-selector'; -import 'whatwg-fetch'; import { setDataSourceSrv } from '@grafana/runtime'; import { contextSrv } from 'app/core/services/context_srv'; -import { mockApi, setupMswServer } from 'app/features/alerting/unified/mockApi'; -import { - defaultGrafanaAlertingConfigurationStatusResponse, - mockAlertmanagerChoiceResponse, -} from 'app/features/alerting/unified/mocks/alertmanagerApi'; +import { setupMswServer } from 'app/features/alerting/unified/mockApi'; import { DashboardSearchHit, DashboardSearchItemType } from 'app/features/search/types'; import { AccessControlAction } from 'app/types'; import { GrafanaAlertStateDecision, PromApplication } from 'app/types/unified-alerting-dto'; @@ -67,12 +62,10 @@ const mocks = { }, }; -const server = setupMswServer(); +setupMswServer(); describe('RuleEditor grafana managed rules', () => { beforeEach(() => { - mockApi(server).eval({ results: {} }); - mockAlertmanagerChoiceResponse(server, defaultGrafanaAlertingConfigurationStatusResponse); jest.clearAllMocks(); contextSrv.isEditor = true; contextSrv.hasEditPermissionInFolders = true; diff --git a/public/app/features/alerting/unified/RuleEditorRecordingRule.test.tsx b/public/app/features/alerting/unified/RuleEditorRecordingRule.test.tsx index bbb0aead53e..d8df9145395 100644 --- a/public/app/features/alerting/unified/RuleEditorRecordingRule.test.tsx +++ b/public/app/features/alerting/unified/RuleEditorRecordingRule.test.tsx @@ -4,11 +4,10 @@ import React from 'react'; import { renderRuleEditor, ui } from 'test/helpers/alertingRuleEditor'; import { clickSelectOption } from 'test/helpers/selectOptionInTest'; import { byText } from 'testing-library-selector'; -import 'whatwg-fetch'; import { setDataSourceSrv } from '@grafana/runtime'; import { contextSrv } from 'app/core/services/context_srv'; -import { mockApi, setupMswServer } from 'app/features/alerting/unified/mockApi'; +import { setupMswServer } from 'app/features/alerting/unified/mockApi'; import { AccessControlAction } from 'app/types'; import { PromApplication } from 'app/types/unified-alerting-dto'; @@ -17,7 +16,7 @@ import { searchFolders } from '../../manage-dashboards/state/actions'; import { discoverFeatures } from './api/buildInfo'; import { fetchRulerRules, fetchRulerRulesGroup, fetchRulerRulesNamespace, setRulerRuleGroup } from './api/ruler'; import { RecordingRuleEditorProps } from './components/rule-editor/RecordingRuleEditor'; -import { MockDataSourceSrv, grantUserPermissions, labelsPluginMetaMock, mockDataSource } from './mocks'; +import { MockDataSourceSrv, grantUserPermissions, mockDataSource } from './mocks'; import { fetchRulerRulesIfNotFetchedYet } from './state/actions'; import * as config from './utils/config'; @@ -93,13 +92,10 @@ const mocks = { }, }; -const server = setupMswServer(); -mockApi(server).plugins.getPluginSettings({ ...labelsPluginMetaMock, enabled: false }); -mockApi(server).eval({ results: { A: { frames: [] } } }); +setupMswServer(); describe('RuleEditor recording rules', () => { beforeEach(() => { - mockApi(server).eval({ results: {} }); jest.clearAllMocks(); contextSrv.isEditor = true; contextSrv.hasEditPermissionInFolders = true; diff --git a/public/app/features/alerting/unified/Silences.test.tsx b/public/app/features/alerting/unified/Silences.test.tsx index e80fca33996..909cf9db9f6 100644 --- a/public/app/features/alerting/unified/Silences.test.tsx +++ b/public/app/features/alerting/unified/Silences.test.tsx @@ -6,12 +6,12 @@ import { dateTime } from '@grafana/data'; import { selectors } from '@grafana/e2e-selectors'; import { config, locationService, setDataSourceSrv } from '@grafana/runtime'; import { setupMswServer } from 'app/features/alerting/unified/mockApi'; +import { waitForServerRequest } from 'app/features/alerting/unified/mocks/server/events'; import { MOCK_DATASOURCE_UID_BROKEN_ALERTMANAGER, MOCK_DATASOURCE_NAME_BROKEN_ALERTMANAGER, -} from 'app/features/alerting/unified/mocks/datasources'; -import { waitForServerRequest } from 'app/features/alerting/unified/mocks/server/events'; -import { silenceCreateHandler } from 'app/features/alerting/unified/mocks/silences'; +} from 'app/features/alerting/unified/mocks/server/handlers/datasources'; +import { silenceCreateHandler } from 'app/features/alerting/unified/mocks/server/handlers/silences'; import { MatcherOperator } from 'app/plugins/datasource/alertmanager/types'; import { AccessControlAction } from 'app/types'; diff --git a/public/app/features/alerting/unified/components/export/GrafanaModifyExport.test.tsx b/public/app/features/alerting/unified/components/export/GrafanaModifyExport.test.tsx index 48175f94ccf..3f2e0dd3a15 100644 --- a/public/app/features/alerting/unified/components/export/GrafanaModifyExport.test.tsx +++ b/public/app/features/alerting/unified/components/export/GrafanaModifyExport.test.tsx @@ -1,18 +1,12 @@ -import { render, waitFor, waitForElementToBeRemoved } from '@testing-library/react'; -import userEvent from '@testing-library/user-event'; import React from 'react'; import { Route } from 'react-router-dom'; import { Props } from 'react-virtualized-auto-sizer'; +import { render, waitFor, waitForElementToBeRemoved, userEvent } from 'test/test-utils'; import { byRole, byTestId, byText } from 'testing-library-selector'; -import { locationService } from '@grafana/runtime'; - -import { TestProvider } from '../../../../../../test/helpers/TestProvider'; -import { AlertmanagerChoice } from '../../../../../plugins/datasource/alertmanager/types'; import { DashboardSearchItemType } from '../../../../search/types'; -import { mockAlertRuleApi, mockApi, mockExportApi, mockSearchApi, setupMswServer } from '../../mockApi'; +import { mockAlertRuleApi, mockExportApi, mockSearchApi, setupMswServer } from '../../mockApi'; import { getGrafanaRule, mockDashboardSearchItem, mockDataSource } from '../../mocks'; -import { mockAlertmanagerChoiceResponse } from '../../mocks/alertmanagerApi'; import { setupDataSources } from '../../testSetup/datasources'; import { GRAFANA_RULES_SOURCE_NAME } from '../../utils/datasource'; @@ -62,17 +56,13 @@ const dataSources = { }; function renderModifyExport(ruleId: string) { - locationService.push(`/alerting/${ruleId}/modify-export`); - render(, { wrapper: TestProvider }); + render(, { + historyOptions: { initialEntries: [`/alerting/${ruleId}/modify-export`] }, + }); } const server = setupMswServer(); -mockAlertmanagerChoiceResponse(server, { - alertmanagersChoice: AlertmanagerChoice.Internal, - numExternalAlertmanagers: 0, -}); - describe('GrafanaModifyExport', () => { setupDataSources(dataSources.default); @@ -97,7 +87,6 @@ describe('GrafanaModifyExport', () => { }); it('Should render edit form for the specified rule', async () => { - mockApi(server).eval({ results: { A: { frames: [] } } }); mockSearchApi(server).search([ mockDashboardSearchItem({ title: grafanaRule.namespace.name, diff --git a/public/app/features/alerting/unified/components/receivers/form/GrafanaReceiverForm.test.tsx b/public/app/features/alerting/unified/components/receivers/form/GrafanaReceiverForm.test.tsx index 88640121466..3a5cda58e7a 100644 --- a/public/app/features/alerting/unified/components/receivers/form/GrafanaReceiverForm.test.tsx +++ b/public/app/features/alerting/unified/components/receivers/form/GrafanaReceiverForm.test.tsx @@ -36,7 +36,6 @@ const ui = { describe('GrafanaReceiverForm', () => { beforeEach(() => { - server.resetHandlers(); clearPluginSettingsCache(); }); diff --git a/public/app/features/alerting/unified/components/receivers/grafanaAppReceivers/onCall/useOnCallIntegration.test.ts b/public/app/features/alerting/unified/components/receivers/grafanaAppReceivers/onCall/useOnCallIntegration.test.ts index 6728360ef53..d9ea6b5ef78 100644 --- a/public/app/features/alerting/unified/components/receivers/grafanaAppReceivers/onCall/useOnCallIntegration.test.ts +++ b/public/app/features/alerting/unified/components/receivers/grafanaAppReceivers/onCall/useOnCallIntegration.test.ts @@ -15,7 +15,6 @@ const server = setupMswServer(); describe('useOnCallIntegration', () => { afterEach(() => { - server.resetHandlers(); clearPluginSettingsCache(); }); diff --git a/public/app/features/alerting/unified/components/rule-editor/alert-rule-form/simplifiedRouting/SimplifiedRuleEditor.test.tsx b/public/app/features/alerting/unified/components/rule-editor/alert-rule-form/simplifiedRouting/SimplifiedRuleEditor.test.tsx index 6535e123522..036efb074f1 100644 --- a/public/app/features/alerting/unified/components/rule-editor/alert-rule-form/simplifiedRouting/SimplifiedRuleEditor.test.tsx +++ b/public/app/features/alerting/unified/components/rule-editor/alert-rule-form/simplifiedRouting/SimplifiedRuleEditor.test.tsx @@ -19,7 +19,7 @@ import { } from 'app/features/alerting/unified/api/ruler'; import * as useContactPoints from 'app/features/alerting/unified/components/contact-points/useContactPoints'; import * as dsByPermission from 'app/features/alerting/unified/hooks/useAlertManagerSources'; -import { mockApi, setupMswServer } from 'app/features/alerting/unified/mockApi'; +import { setupMswServer } from 'app/features/alerting/unified/mockApi'; import { MockDataSourceSrv, grantUserPermissions, mockDataSource } from 'app/features/alerting/unified/mocks'; import { AlertmanagerProvider } from 'app/features/alerting/unified/state/AlertmanagerContext'; import { fetchRulerRulesIfNotFetchedYet } from 'app/features/alerting/unified/state/actions'; @@ -103,11 +103,10 @@ const mocks = { }, }; -const server = setupMswServer(); +setupMswServer(); describe('Can create a new grafana managed alert unsing simplified routing', () => { beforeEach(() => { - mockApi(server).eval({ results: {} }); jest.clearAllMocks(); contextSrv.isEditor = true; contextSrv.hasEditPermissionInFolders = true; diff --git a/public/app/features/alerting/unified/components/rule-viewer/__mocks__/server.ts b/public/app/features/alerting/unified/components/rule-viewer/__mocks__/server.ts index babf5bae230..bc05854ea04 100644 --- a/public/app/features/alerting/unified/components/rule-viewer/__mocks__/server.ts +++ b/public/app/features/alerting/unified/components/rule-viewer/__mocks__/server.ts @@ -2,11 +2,10 @@ import { http, HttpResponse, RequestHandler } from 'msw'; import { setupServer } from 'msw/node'; import { GrafanaAlertingConfigurationStatusResponse } from 'app/features/alerting/unified/api/alertmanagerApi'; +import { grafanaAlertingConfigurationStatusHandler } from 'app/features/alerting/unified/mocks/server/handlers/alertmanagers'; import { AlertmanagerChoice } from 'app/plugins/datasource/alertmanager/types'; import { AccessControlAction } from 'app/types'; -import { grafanaAlertingConfigurationStatusHandler } from '../../../mocks/alertmanagerApi'; - const grafanaAlertingConfigurationMockedResponse: GrafanaAlertingConfigurationStatusResponse = { alertmanagersChoice: AlertmanagerChoice.Internal, numExternalAlertmanagers: 0, diff --git a/public/app/features/alerting/unified/mockApi.ts b/public/app/features/alerting/unified/mockApi.ts index 77bf8b968fe..b34222c09a2 100644 --- a/public/app/features/alerting/unified/mockApi.ts +++ b/public/app/features/alerting/unified/mockApi.ts @@ -5,7 +5,7 @@ import { setupServer, SetupServer } from 'msw/node'; import { DataSourceInstanceSettings, PluginMeta } from '@grafana/data'; import { setBackendSrv } from '@grafana/runtime'; import { AlertRuleUpdated } from 'app/features/alerting/unified/api/alertRuleApi'; -import allHandlers from 'app/features/alerting/unified/mocks/server/handlers'; +import allHandlers from 'app/features/alerting/unified/mocks/server/all-handlers'; import { DashboardDTO, FolderDTO, NotifierDTO, OrgUser } from 'app/types'; import { PromBuildInfoResponse, @@ -28,7 +28,6 @@ import { import { DashboardSearchItem } from '../../search/types'; import { CreateIntegrationDTO, NewOnCallIntegrationDTO, OnCallIntegrationDTO } from './api/onCallApi'; -import { AlertingQueryResponse } from './state/AlertingQueryRunner'; type Configurator = (builder: T) => T; @@ -203,13 +202,6 @@ export function mockApi(server: SetupServer) { ); }, - eval: (response: AlertingQueryResponse) => { - server.use( - http.post('/api/v1/eval', () => { - return HttpResponse.json(response); - }) - ); - }, grafanaNotifiers: (response: NotifierDTO[]) => { server.use(http.get(`api/alert-notifiers`, () => HttpResponse.json(response))); }, diff --git a/public/app/features/alerting/unified/mocks/alertmanagerApi.ts b/public/app/features/alerting/unified/mocks/alertmanagerApi.ts index f519161dbfd..2de4df614ff 100644 --- a/public/app/features/alerting/unified/mocks/alertmanagerApi.ts +++ b/public/app/features/alerting/unified/mocks/alertmanagerApi.ts @@ -1,13 +1,11 @@ import { http, HttpResponse } from 'msw'; import { SetupServer } from 'msw/node'; -import { MOCK_SILENCE_ID_EXISTING, mockAlertmanagerAlert } from 'app/features/alerting/unified/mocks'; -import { MOCK_DATASOURCE_UID_BROKEN_ALERTMANAGER } from 'app/features/alerting/unified/mocks/datasources'; +import { grafanaAlertingConfigurationStatusHandler } from 'app/features/alerting/unified/mocks/server/handlers/alertmanagers'; import { AlertmanagerChoice, AlertManagerCortexConfig, - AlertState, ExternalAlertmanagersStatusResponse, } from '../../../../plugins/datasource/alertmanager/types'; import { GrafanaAlertingConfigurationStatusResponse } from '../api/alertmanagerApi'; @@ -18,10 +16,6 @@ export const defaultGrafanaAlertingConfigurationStatusResponse: GrafanaAlertingC numExternalAlertmanagers: 0, }; -export const grafanaAlertingConfigurationStatusHandler = ( - response = defaultGrafanaAlertingConfigurationStatusResponse -) => http.get('/api/v1/ngalert', () => HttpResponse.json(response)); - export function mockAlertmanagerChoiceResponse( server: SetupServer, response: GrafanaAlertingConfigurationStatusResponse @@ -50,20 +44,3 @@ export function mockAlertmanagerConfigResponse( ) ); } - -export const alertmanagerAlertsListHandler = () => - http.get<{ datasourceUid: string }>('/api/alertmanager/:datasourceUid/api/v2/alerts', ({ params }) => { - if (params.datasourceUid === MOCK_DATASOURCE_UID_BROKEN_ALERTMANAGER) { - return HttpResponse.json({ traceId: '' }, { status: 502 }); - } - return HttpResponse.json([ - mockAlertmanagerAlert({ - labels: { foo: 'bar', buzz: 'bazz' }, - status: { state: AlertState.Suppressed, silencedBy: [MOCK_SILENCE_ID_EXISTING], inhibitedBy: [] }, - }), - mockAlertmanagerAlert({ - labels: { foo: 'bar', buzz: 'bazz' }, - status: { state: AlertState.Suppressed, silencedBy: [MOCK_SILENCE_ID_EXISTING], inhibitedBy: [] }, - }), - ]); - }); diff --git a/public/app/features/alerting/unified/mocks/server/all-handlers.ts b/public/app/features/alerting/unified/mocks/server/all-handlers.ts new file mode 100644 index 00000000000..0ee78ae8e2e --- /dev/null +++ b/public/app/features/alerting/unified/mocks/server/all-handlers.ts @@ -0,0 +1,24 @@ +/** + * Contains all handlers that are required for test rendering of components within Alerting + */ + +import alertmanagerHandlers from 'app/features/alerting/unified/mocks/server/handlers/alertmanagers'; +import datasourcesHandlers from 'app/features/alerting/unified/mocks/server/handlers/datasources'; +import evalHandlers from 'app/features/alerting/unified/mocks/server/handlers/eval'; +import folderHandlers from 'app/features/alerting/unified/mocks/server/handlers/folders'; +import pluginsHandlers from 'app/features/alerting/unified/mocks/server/handlers/plugins'; +import silenceHandlers from 'app/features/alerting/unified/mocks/server/handlers/silences'; + +/** + * Array of all mock handlers that are required across Alerting tests + */ +const allHandlers = [ + ...alertmanagerHandlers, + ...datasourcesHandlers, + ...evalHandlers, + ...folderHandlers, + ...pluginsHandlers, + ...silenceHandlers, +]; + +export default allHandlers; diff --git a/public/app/features/alerting/unified/mocks/server/configure.ts b/public/app/features/alerting/unified/mocks/server/configure.ts index 1dda45e266b..f02bb547d47 100644 --- a/public/app/features/alerting/unified/mocks/server/configure.ts +++ b/public/app/features/alerting/unified/mocks/server/configure.ts @@ -1,5 +1,5 @@ import server from 'app/features/alerting/unified/mockApi'; -import { grafanaAlertingConfigurationStatusHandler } from 'app/features/alerting/unified/mocks/alertmanagerApi'; +import { grafanaAlertingConfigurationStatusHandler } from 'app/features/alerting/unified/mocks/server/handlers/alertmanagers'; import { AlertmanagerChoice } from 'app/plugins/datasource/alertmanager/types'; /** diff --git a/public/app/features/alerting/unified/mocks/server/handlers.ts b/public/app/features/alerting/unified/mocks/server/handlers.ts deleted file mode 100644 index 9e1d459e668..00000000000 --- a/public/app/features/alerting/unified/mocks/server/handlers.ts +++ /dev/null @@ -1,36 +0,0 @@ -/** - * Contains all handlers that are required for test rendering of components within Alerting - */ - -import { - alertmanagerAlertsListHandler, - grafanaAlertingConfigurationStatusHandler, -} from 'app/features/alerting/unified/mocks/alertmanagerApi'; -import { datasourceBuildInfoHandler } from 'app/features/alerting/unified/mocks/datasources'; -import { folderHandler } from 'app/features/alerting/unified/mocks/folders'; -import { pluginsHandler } from 'app/features/alerting/unified/mocks/plugins'; -import { - silenceCreateHandler, - silenceGetHandler, - silencesListHandler, -} from 'app/features/alerting/unified/mocks/silences'; - -/** - * All mock handlers that are required across Alerting tests - */ -const allHandlers = [ - grafanaAlertingConfigurationStatusHandler(), - alertmanagerAlertsListHandler(), - - folderHandler(), - - pluginsHandler(), - - silencesListHandler(), - silenceGetHandler(), - silenceCreateHandler(), - - datasourceBuildInfoHandler(), -]; - -export default allHandlers; diff --git a/public/app/features/alerting/unified/mocks/server/handlers/alertmanagers.ts b/public/app/features/alerting/unified/mocks/server/handlers/alertmanagers.ts new file mode 100644 index 00000000000..06fdf657ebd --- /dev/null +++ b/public/app/features/alerting/unified/mocks/server/handlers/alertmanagers.ts @@ -0,0 +1,30 @@ +import { http, HttpResponse } from 'msw'; + +import { MOCK_SILENCE_ID_EXISTING, mockAlertmanagerAlert } from 'app/features/alerting/unified/mocks'; +import { defaultGrafanaAlertingConfigurationStatusResponse } from 'app/features/alerting/unified/mocks/alertmanagerApi'; +import { MOCK_DATASOURCE_UID_BROKEN_ALERTMANAGER } from 'app/features/alerting/unified/mocks/server/handlers/datasources'; +import { AlertState } from 'app/plugins/datasource/alertmanager/types'; + +export const grafanaAlertingConfigurationStatusHandler = ( + response = defaultGrafanaAlertingConfigurationStatusResponse +) => http.get('/api/v1/ngalert', () => HttpResponse.json(response)); + +export const alertmanagerAlertsListHandler = () => + http.get<{ datasourceUid: string }>('/api/alertmanager/:datasourceUid/api/v2/alerts', ({ params }) => { + if (params.datasourceUid === MOCK_DATASOURCE_UID_BROKEN_ALERTMANAGER) { + return HttpResponse.json({ traceId: '' }, { status: 502 }); + } + return HttpResponse.json([ + mockAlertmanagerAlert({ + labels: { foo: 'bar', buzz: 'bazz' }, + status: { state: AlertState.Suppressed, silencedBy: [MOCK_SILENCE_ID_EXISTING], inhibitedBy: [] }, + }), + mockAlertmanagerAlert({ + labels: { foo: 'bar', buzz: 'bazz' }, + status: { state: AlertState.Suppressed, silencedBy: [MOCK_SILENCE_ID_EXISTING], inhibitedBy: [] }, + }), + ]); + }); + +const handlers = [alertmanagerAlertsListHandler(), grafanaAlertingConfigurationStatusHandler()]; +export default handlers; diff --git a/public/app/features/alerting/unified/mocks/datasources.ts b/public/app/features/alerting/unified/mocks/server/handlers/datasources.ts similarity index 61% rename from public/app/features/alerting/unified/mocks/datasources.ts rename to public/app/features/alerting/unified/mocks/server/handlers/datasources.ts index 04f579a7c24..e07327c6a1a 100644 --- a/public/app/features/alerting/unified/mocks/datasources.ts +++ b/public/app/features/alerting/unified/mocks/server/handlers/datasources.ts @@ -1,10 +1,15 @@ -import { HttpResponse, http } from 'msw'; - -// TODO: Add more accurate endpoint responses as tests require -export const datasourceBuildInfoHandler = () => - http.get('/api/datasources/proxy/uid/:datasourceUid/api/v1/status/buildinfo', () => HttpResponse.json({})); +import { http, HttpResponse } from 'msw'; /** UID of the alertmanager that is expected to be broken in tests */ export const MOCK_DATASOURCE_UID_BROKEN_ALERTMANAGER = 'FwkfQfEmYlAthB'; /** Display name of the alertmanager that is expected to be broken in tests */ export const MOCK_DATASOURCE_NAME_BROKEN_ALERTMANAGER = 'broken alertmanager'; +export const MOCK_DATASOURCE_EXTERNAL_VANILLA_ALERTMANAGER_UID = 'vanilla-alertmanager'; +export const MOCK_DATASOURCE_PROVISIONED_MIMIR_ALERTMANAGER_UID = 'provisioned-alertmanager'; + +// TODO: Add more accurate endpoint responses as tests require +export const datasourceBuildInfoHandler = () => + http.get('/api/datasources/proxy/uid/:datasourceUid/api/v1/status/buildinfo', () => HttpResponse.json({})); + +const datasourcesHandlers = [datasourceBuildInfoHandler()]; +export default datasourcesHandlers; diff --git a/public/app/features/alerting/unified/mocks/server/handlers/eval.ts b/public/app/features/alerting/unified/mocks/server/handlers/eval.ts new file mode 100644 index 00000000000..8b21b0e7acd --- /dev/null +++ b/public/app/features/alerting/unified/mocks/server/handlers/eval.ts @@ -0,0 +1,14 @@ +import { HttpResponse, http } from 'msw'; + +import { AlertingQueryResponse } from 'app/features/alerting/unified/state/AlertingQueryRunner'; + +const defaultPostEvalResponse = { + results: {}, +}; +const postEvalHandler = (response: AlertingQueryResponse = defaultPostEvalResponse) => + http.post('/api/v1/eval', () => { + return HttpResponse.json(response); + }); + +const handlers = [postEvalHandler()]; +export default handlers; diff --git a/public/app/features/alerting/unified/mocks/folders.ts b/public/app/features/alerting/unified/mocks/server/handlers/folders.ts similarity index 59% rename from public/app/features/alerting/unified/mocks/folders.ts rename to public/app/features/alerting/unified/mocks/server/handlers/folders.ts index 9b20e2d826f..f3f51f64e70 100644 --- a/public/app/features/alerting/unified/mocks/folders.ts +++ b/public/app/features/alerting/unified/mocks/server/handlers/folders.ts @@ -2,5 +2,9 @@ import { HttpResponse, http } from 'msw'; import { mockFolder } from 'app/features/alerting/unified/mocks'; -export const folderHandler = (response = mockFolder()) => +export const getFolderHandler = (response = mockFolder()) => http.get(`/api/folders/:folderUid`, () => HttpResponse.json(response)); + +const handlers = [getFolderHandler()]; + +export default handlers; diff --git a/public/app/features/alerting/unified/mocks/plugins.ts b/public/app/features/alerting/unified/mocks/server/handlers/plugins.ts similarity index 79% rename from public/app/features/alerting/unified/mocks/plugins.ts rename to public/app/features/alerting/unified/mocks/server/handlers/plugins.ts index 54d0038f757..b5a70ea71aa 100644 --- a/public/app/features/alerting/unified/mocks/plugins.ts +++ b/public/app/features/alerting/unified/mocks/server/handlers/plugins.ts @@ -3,10 +3,13 @@ import { http, HttpResponse } from 'msw'; import { PluginMeta } from '@grafana/data'; import { plugins } from 'app/features/alerting/unified/testSetup/plugins'; -export const pluginsHandler = (pluginsArray: PluginMeta[] = plugins) => +export const getPluginsHandler = (pluginsArray: PluginMeta[] = plugins) => http.get<{ pluginId: string }>(`/api/plugins/:pluginId/settings`, ({ params: { pluginId } }) => { const matchingPlugin = pluginsArray.find((plugin) => plugin.id === pluginId); return matchingPlugin ? HttpResponse.json(matchingPlugin) : HttpResponse.json({ message: 'Plugin not found, no installed plugin with that id' }, { status: 404 }); }); + +const handlers = [getPluginsHandler()]; +export default handlers; diff --git a/public/app/features/alerting/unified/mocks/silences.ts b/public/app/features/alerting/unified/mocks/server/handlers/silences.ts similarity index 80% rename from public/app/features/alerting/unified/mocks/silences.ts rename to public/app/features/alerting/unified/mocks/server/handlers/silences.ts index 8ef9944d83f..5e659aa15f7 100644 --- a/public/app/features/alerting/unified/mocks/silences.ts +++ b/public/app/features/alerting/unified/mocks/server/handlers/silences.ts @@ -1,13 +1,9 @@ import { HttpResponse, http } from 'msw'; import { mockSilences } from 'app/features/alerting/unified/mocks'; -import { MOCK_DATASOURCE_UID_BROKEN_ALERTMANAGER } from 'app/features/alerting/unified/mocks/datasources'; +import { MOCK_DATASOURCE_UID_BROKEN_ALERTMANAGER } from 'app/features/alerting/unified/mocks/server/handlers/datasources'; -////////////// -// Silences // -////////////// - -export const silencesListHandler = (silences = mockSilences) => +const silencesListHandler = (silences = mockSilences) => http.get<{ datasourceUid: string }>('/api/alertmanager/:datasourceUid/api/v2/silences', ({ params }) => { if (params.datasourceUid === MOCK_DATASOURCE_UID_BROKEN_ALERTMANAGER) { return HttpResponse.json({ traceId: '' }, { status: 502 }); @@ -15,7 +11,7 @@ export const silencesListHandler = (silences = mockSilences) => return HttpResponse.json(silences); }); -export const silenceGetHandler = () => +const silenceGetHandler = () => http.get<{ uuid: string }>('/api/alertmanager/:datasourceUid/api/v2/silence/:uuid', ({ params }) => { const { uuid } = params; const matchingMockSilence = mockSilences.find((silence) => silence.id === uuid); @@ -30,3 +26,6 @@ export const silenceCreateHandler = () => http.post('/api/alertmanager/:datasourceUid/api/v2/silences', () => HttpResponse.json({ silenceId: '4bda5b38-7939-4887-9ec2-16323b8e3b4e' }) ); + +const handlers = [silencesListHandler(), silenceGetHandler(), silenceCreateHandler()]; +export default handlers; diff --git a/public/app/features/alerting/unified/testSetup/plugins.ts b/public/app/features/alerting/unified/testSetup/plugins.ts index 4efa4d9c3e1..f56ecb62875 100644 --- a/public/app/features/alerting/unified/testSetup/plugins.ts +++ b/public/app/features/alerting/unified/testSetup/plugins.ts @@ -4,7 +4,7 @@ import { PluginMeta, PluginType } from '@grafana/data'; import { config, setPluginExtensionsHook } from '@grafana/runtime'; import { mockPluginLinkExtension } from '../mocks'; -import { pluginsHandler } from '../mocks/plugins'; +import { getPluginsHandler } from '../mocks/server/handlers/plugins'; export function setupPlugins(plugins: PluginMeta[]): { apiHandlers: RequestHandler[] } { plugins.forEach((plugin) => { @@ -18,7 +18,7 @@ export function setupPlugins(plugins: PluginMeta[]): { apiHandlers: RequestHandl }); return { - apiHandlers: [pluginsHandler(plugins)], + apiHandlers: [getPluginsHandler(plugins)], }; } @@ -105,4 +105,27 @@ export const plugins: PluginMeta[] = [ module: 'public/plugins/grafana-asserts-app/module.js', baseUrl: 'public/plugins/grafana-asserts-app', }, + { + id: 'grafana-oncall-app', + name: 'OnCall', + type: PluginType.app, + enabled: true, + info: { + author: { + name: 'Grafana Labs', + url: '', + }, + description: 'OnCall', + links: [], + logos: { + small: '', + large: '', + }, + screenshots: [], + version: 'local-dev', + updated: '2024-04-09', + }, + module: 'public/plugins/grafana-oncall-app/module.js', + baseUrl: 'public/plugins/grafana-oncall-app', + }, ];