From 11ed882c8477f205af2a20e5a3baa140b81105a1 Mon Sep 17 00:00:00 2001 From: Tom Ratcliffe Date: Thu, 25 Apr 2024 16:10:12 +0100 Subject: [PATCH] Add mock server handlers necessary for Silences --- public/app/features/alerting/unified/mocks.ts | 7 +++ .../alerting/unified/mocks/server/handlers.ts | 53 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/public/app/features/alerting/unified/mocks.ts b/public/app/features/alerting/unified/mocks.ts index 7b97e7aeb8c..978cf70a859 100644 --- a/public/app/features/alerting/unified/mocks.ts +++ b/public/app/features/alerting/unified/mocks.ts @@ -18,6 +18,7 @@ import { import { DataSourceSrv, GetDataSourceListFilters, config } from '@grafana/runtime'; import { defaultDashboard } from '@grafana/schema'; import { contextSrv } from 'app/core/services/context_srv'; +import { parseMatchers } from 'app/features/alerting/unified/utils/alertmanager'; import { DatasourceSrv } from 'app/features/plugins/datasource_srv'; import { AlertManagerCortexConfig, @@ -306,6 +307,12 @@ export const mockSilence = (partial: Partial = {}): Silence => { }; }; +export const mockSilences = [ + mockSilence({ id: '12345' }), + mockSilence({ id: '67890', matchers: parseMatchers('foo!=bar'), comment: 'Catch all' }), + mockSilence({ id: '1111', status: { state: SilenceState.Expired } }), +]; + export const mockNotifiersState = (partial: Partial = {}): NotifiersState => { return { email: [ diff --git a/public/app/features/alerting/unified/mocks/server/handlers.ts b/public/app/features/alerting/unified/mocks/server/handlers.ts index 9861af3aa07..aea95b81cf5 100644 --- a/public/app/features/alerting/unified/mocks/server/handlers.ts +++ b/public/app/features/alerting/unified/mocks/server/handlers.ts @@ -4,7 +4,60 @@ import { HttpResponse, http } from 'msw'; +import { mockAlertmanagerAlert, mockSilences } from 'app/features/alerting/unified/mocks'; import { defaultAlertmanagerChoiceResponse } from 'app/features/alerting/unified/mocks/alertmanagerApi'; +import { AlertState } from 'app/plugins/datasource/alertmanager/types'; + +/////////////////// +// Alertmanagers // +/////////////////// export const alertmanagerChoiceHandler = (response = defaultAlertmanagerChoiceResponse) => http.get('/api/v1/ngalert', () => HttpResponse.json(response)); + +const alertmanagerAlertsListHandler = () => + http.get('/api/alertmanager/:datasourceUid/api/v2/alerts', () => + HttpResponse.json([ + mockAlertmanagerAlert({ + labels: { foo: 'bar', buzz: 'bazz' }, + status: { state: AlertState.Suppressed, silencedBy: ['12345'], inhibitedBy: [] }, + }), + mockAlertmanagerAlert({ + labels: { foo: 'bar', buzz: 'bazz' }, + status: { state: AlertState.Suppressed, silencedBy: ['12345'], inhibitedBy: [] }, + }), + ]) + ); + +///////////////// +// Datasources // +///////////////// + +// TODO: Add more accurate endpoint responses as tests require +const datasourceBuildInfoHandler = () => + http.get('/api/datasources/proxy/uid/:datasourceUid/api/v1/status/buildinfo', () => HttpResponse.json({})); + +////////////// +// Silences // +////////////// + +const silencesListHandler = (silences = mockSilences) => + http.get('/api/alertmanager/:datasourceUid/api/v2/silences', () => HttpResponse.json(silences)); + +const createSilenceHandler = () => + http.post('/api/alertmanager/:datasourceUid/api/v2/silences', () => + HttpResponse.json({ silenceId: '4bda5b38-7939-4887-9ec2-16323b8e3b4e' }) + ); + +/** + * All mock handlers that are required across Alerting tests + */ +const allHandlers = [ + alertmanagerChoiceHandler(), + silencesListHandler(), + createSilenceHandler(), + alertmanagerAlertsListHandler(), + datasourceBuildInfoHandler(), +]; + +export default allHandlers;