Add mock server handlers necessary for Silences

This commit is contained in:
Tom Ratcliffe 2024-04-25 16:10:12 +01:00 committed by Tom Ratcliffe
parent 8f27ec2521
commit 11ed882c84
2 changed files with 60 additions and 0 deletions

View File

@ -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> = {}): 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> = {}): NotifiersState => {
return {
email: [

View File

@ -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;