123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482 |
- import {
- DataSourceApi,
- DataSourceInstanceSettings,
- DataSourceJsonData,
- DataSourcePluginMeta,
- DataSourceRef,
- ScopedVars,
- } from '@grafana/data';
- import { config, DataSourceSrv, GetDataSourceListFilters } from '@grafana/runtime';
- import { contextSrv } from 'app/core/services/context_srv';
- import { DatasourceSrv } from 'app/features/plugins/datasource_srv';
- import {
- AlertmanagerAlert,
- AlertManagerCortexConfig,
- AlertmanagerGroup,
- AlertmanagerStatus,
- AlertState,
- GrafanaManagedReceiverConfig,
- Silence,
- SilenceState,
- } from 'app/plugins/datasource/alertmanager/types';
- import { AccessControlAction, FolderDTO } from 'app/types';
- import { Alert, AlertingRule, CombinedRule, RecordingRule, RuleGroup, RuleNamespace } from 'app/types/unified-alerting';
- import {
- GrafanaAlertStateDecision,
- GrafanaRuleDefinition,
- PromAlertingRuleState,
- PromRuleType,
- RulerAlertingRuleDTO,
- RulerGrafanaRuleDTO,
- RulerRuleGroupDTO,
- RulerRulesConfigDTO,
- } from 'app/types/unified-alerting-dto';
- let nextDataSourceId = 1;
- export function mockDataSource<T extends DataSourceJsonData = DataSourceJsonData>(
- partial: Partial<DataSourceInstanceSettings<T>> = {},
- meta: Partial<DataSourcePluginMeta> = {}
- ): DataSourceInstanceSettings<T> {
- const id = partial.id ?? nextDataSourceId++;
- return {
- id,
- uid: `mock-ds-${nextDataSourceId}`,
- type: 'prometheus',
- name: `Prometheus-${id}`,
- access: 'proxy',
- jsonData: {} as T,
- meta: {
- info: {
- logos: {
- small: 'https://prometheus.io/assets/prometheus_logo_grey.svg',
- large: 'https://prometheus.io/assets/prometheus_logo_grey.svg',
- },
- },
- ...meta,
- } as any as DataSourcePluginMeta,
- ...partial,
- };
- }
- export const mockPromAlert = (partial: Partial<Alert> = {}): Alert => ({
- activeAt: '2021-03-18T13:47:05.04938691Z',
- annotations: {
- message: 'alert with severity "warning"',
- },
- labels: {
- alertname: 'myalert',
- severity: 'warning',
- },
- state: PromAlertingRuleState.Firing,
- value: '1e+00',
- ...partial,
- });
- export const mockRulerGrafanaRule = (
- partial: Partial<RulerGrafanaRuleDTO> = {},
- partialDef: Partial<GrafanaRuleDefinition> = {}
- ): RulerGrafanaRuleDTO => {
- return {
- for: '1m',
- grafana_alert: {
- uid: '123',
- title: 'myalert',
- namespace_uid: '123',
- namespace_id: 1,
- condition: 'A',
- no_data_state: GrafanaAlertStateDecision.Alerting,
- exec_err_state: GrafanaAlertStateDecision.Alerting,
- data: [
- {
- datasourceUid: '123',
- refId: 'A',
- queryType: 'huh',
- model: {} as any,
- },
- ],
- ...partialDef,
- },
- annotations: {
- message: 'alert with severity "{{.warning}}}"',
- },
- labels: {
- severity: 'warning',
- },
- ...partial,
- };
- };
- export const mockRulerAlertingRule = (partial: Partial<RulerAlertingRuleDTO> = {}): RulerAlertingRuleDTO => ({
- alert: 'alert1',
- expr: 'up = 1',
- labels: {
- severity: 'warning',
- },
- annotations: {
- summary: 'test alert',
- },
- });
- export const mockRulerRuleGroup = (partial: Partial<RulerRuleGroupDTO> = {}): RulerRuleGroupDTO => ({
- name: 'group1',
- rules: [mockRulerAlertingRule()],
- ...partial,
- });
- export const mockPromAlertingRule = (partial: Partial<AlertingRule> = {}): AlertingRule => {
- return {
- type: PromRuleType.Alerting,
- alerts: [mockPromAlert()],
- name: 'myalert',
- query: 'foo > 1',
- lastEvaluation: '2021-03-23T08:19:05.049595312Z',
- evaluationTime: 0.000395601,
- annotations: {
- message: 'alert with severity "{{.warning}}}"',
- },
- labels: {
- severity: 'warning',
- },
- state: PromAlertingRuleState.Firing,
- health: 'OK',
- ...partial,
- };
- };
- export const mockPromRecordingRule = (partial: Partial<RecordingRule> = {}): RecordingRule => {
- return {
- type: PromRuleType.Recording,
- query: 'bar < 3',
- labels: {
- cluster: 'eu-central',
- },
- health: 'OK',
- name: 'myrecordingrule',
- lastEvaluation: '2021-03-23T08:19:05.049595312Z',
- evaluationTime: 0.000395601,
- ...partial,
- };
- };
- export const mockPromRuleGroup = (partial: Partial<RuleGroup> = {}): RuleGroup => {
- return {
- name: 'mygroup',
- interval: 60,
- rules: [mockPromAlertingRule()],
- ...partial,
- };
- };
- export const mockPromRuleNamespace = (partial: Partial<RuleNamespace> = {}): RuleNamespace => {
- return {
- dataSourceName: 'Prometheus-1',
- name: 'default',
- groups: [mockPromRuleGroup()],
- ...partial,
- };
- };
- export const mockAlertmanagerAlert = (partial: Partial<AlertmanagerAlert> = {}): AlertmanagerAlert => {
- return {
- annotations: {
- summary: 'US-Central region is on fire',
- },
- endsAt: '2021-06-22T21:49:28.562Z',
- fingerprint: '88e013643c3df34ac3',
- receivers: [{ name: 'pagerduty' }],
- startsAt: '2021-06-21T17:25:28.562Z',
- status: { inhibitedBy: [], silencedBy: [], state: AlertState.Active },
- updatedAt: '2021-06-22T21:45:28.564Z',
- generatorURL: 'https://play.grafana.com/explore',
- labels: { severity: 'warning', region: 'US-Central' },
- ...partial,
- };
- };
- export const mockAlertGroup = (partial: Partial<AlertmanagerGroup> = {}): AlertmanagerGroup => {
- return {
- labels: {
- severity: 'warning',
- region: 'US-Central',
- },
- receiver: {
- name: 'pagerduty',
- },
- alerts: [
- mockAlertmanagerAlert(),
- mockAlertmanagerAlert({
- status: { state: AlertState.Suppressed, silencedBy: ['123456abcdef'], inhibitedBy: [] },
- labels: { severity: 'warning', region: 'US-Central', foo: 'bar', ...partial.labels },
- }),
- ],
- ...partial,
- };
- };
- export const mockSilence = (partial: Partial<Silence> = {}): Silence => {
- return {
- id: '1a2b3c4d5e6f',
- matchers: [{ name: 'foo', value: 'bar', isEqual: true, isRegex: false }],
- startsAt: new Date().toISOString(),
- endsAt: new Date(Date.now() + 60 * 60 * 1000).toISOString(),
- updatedAt: new Date().toISOString(),
- createdBy: config.bootData.user.name || 'admin',
- comment: 'Silence noisy alerts',
- status: {
- state: SilenceState.Active,
- },
- ...partial,
- };
- };
- export class MockDataSourceSrv implements DataSourceSrv {
- datasources: Record<string, DataSourceApi> = {};
- // @ts-ignore
- private settingsMapByName: Record<string, DataSourceInstanceSettings> = {};
- private settingsMapByUid: Record<string, DataSourceInstanceSettings> = {};
- private settingsMapById: Record<string, DataSourceInstanceSettings> = {};
- // @ts-ignore
- private templateSrv = {
- getVariables: () => [],
- replace: (name: any) => name,
- };
- defaultName = '';
- constructor(datasources: Record<string, DataSourceInstanceSettings>) {
- this.datasources = {};
- this.settingsMapByName = Object.values(datasources).reduce<Record<string, DataSourceInstanceSettings>>(
- (acc, ds) => {
- acc[ds.name] = ds;
- return acc;
- },
- {}
- );
- for (const dsSettings of Object.values(this.settingsMapByName)) {
- this.settingsMapByUid[dsSettings.uid] = dsSettings;
- this.settingsMapById[dsSettings.id] = dsSettings;
- if (dsSettings.isDefault) {
- this.defaultName = dsSettings.name;
- }
- }
- }
- get(name?: string | null | DataSourceRef, scopedVars?: ScopedVars): Promise<DataSourceApi> {
- return DatasourceSrv.prototype.get.call(this, name, scopedVars);
- //return Promise.reject(new Error('not implemented'));
- }
- /**
- * Get a list of data sources
- */
- getList(filters?: GetDataSourceListFilters): DataSourceInstanceSettings[] {
- return DatasourceSrv.prototype.getList.call(this, filters);
- }
- /**
- * Get settings and plugin metadata by name or uid
- */
- getInstanceSettings(nameOrUid: string | null | undefined): DataSourceInstanceSettings | undefined {
- return (
- DatasourceSrv.prototype.getInstanceSettings.call(this, nameOrUid) ||
- ({ meta: { info: { logos: {} } } } as unknown as DataSourceInstanceSettings)
- );
- }
- async loadDatasource(name: string): Promise<DataSourceApi<any, any>> {
- return DatasourceSrv.prototype.loadDatasource.call(this, name);
- }
- reload() {}
- }
- export const mockGrafanaReceiver = (
- type: string,
- overrides: Partial<GrafanaManagedReceiverConfig> = {}
- ): GrafanaManagedReceiverConfig => ({
- type: type,
- name: type,
- disableResolveMessage: false,
- settings: {},
- ...overrides,
- });
- export const someGrafanaAlertManagerConfig: AlertManagerCortexConfig = {
- template_files: {
- 'first template': 'first template content',
- 'second template': 'second template content',
- 'third template': 'third template',
- },
- alertmanager_config: {
- route: {
- receiver: 'default',
- },
- receivers: [
- {
- name: 'default',
- grafana_managed_receiver_configs: [mockGrafanaReceiver('email')],
- },
- {
- name: 'critical',
- grafana_managed_receiver_configs: [mockGrafanaReceiver('slack'), mockGrafanaReceiver('pagerduty')],
- },
- ],
- },
- };
- export const someCloudAlertManagerStatus: AlertmanagerStatus = {
- cluster: {
- peers: [],
- status: 'ok',
- },
- uptime: '10 hours',
- versionInfo: {
- branch: '',
- version: '',
- goVersion: '',
- buildDate: '',
- buildUser: '',
- revision: '',
- },
- config: {
- route: {
- receiver: 'default-email',
- },
- receivers: [
- {
- name: 'default-email',
- email_configs: [
- {
- to: 'example@example.com',
- },
- ],
- },
- ],
- },
- };
- export const someCloudAlertManagerConfig: AlertManagerCortexConfig = {
- template_files: {
- 'foo template': 'foo content',
- },
- alertmanager_config: {
- route: {
- receiver: 'cloud-receiver',
- routes: [
- {
- receiver: 'foo-receiver',
- },
- {
- receiver: 'bar-receiver',
- },
- ],
- },
- receivers: [
- {
- name: 'cloud-receiver',
- email_configs: [
- {
- to: 'domas.lapinskas@grafana.com',
- },
- ],
- slack_configs: [
- {
- api_url: 'http://slack1',
- channel: '#mychannel',
- actions: [
- {
- text: 'action1text',
- type: 'action1type',
- url: 'http://action1',
- },
- ],
- fields: [
- {
- title: 'field1',
- value: 'text1',
- },
- {
- title: 'field2',
- value: 'text2',
- },
- ],
- },
- ],
- },
- ],
- },
- };
- export const somePromRules = (dataSourceName = 'Prometheus'): RuleNamespace[] => [
- {
- dataSourceName,
- name: 'namespace1',
- groups: [
- mockPromRuleGroup({ name: 'group1', rules: [mockPromAlertingRule({ name: 'alert1' })] }),
- mockPromRuleGroup({ name: 'group2', rules: [mockPromAlertingRule({ name: 'alert2' })] }),
- ],
- },
- {
- dataSourceName,
- name: 'namespace2',
- groups: [mockPromRuleGroup({ name: 'group3', rules: [mockPromAlertingRule({ name: 'alert3' })] })],
- },
- ];
- export const someRulerRules: RulerRulesConfigDTO = {
- namespace1: [
- mockRulerRuleGroup({ name: 'group1', rules: [mockRulerAlertingRule({ alert: 'alert1' })] }),
- mockRulerRuleGroup({ name: 'group2', rules: [mockRulerAlertingRule({ alert: 'alert2' })] }),
- ],
- namespace2: [mockRulerRuleGroup({ name: 'group3', rules: [mockRulerAlertingRule({ alert: 'alert3' })] })],
- };
- export const mockCombinedRule = (partial?: Partial<CombinedRule>): CombinedRule => ({
- name: 'mockRule',
- query: 'expr',
- group: {
- name: 'mockCombinedRuleGroup',
- rules: [],
- },
- namespace: {
- name: 'mockCombinedNamespace',
- groups: [{ name: 'mockCombinedRuleGroup', rules: [] }],
- rulesSource: 'grafana',
- },
- labels: {},
- annotations: {},
- promRule: mockPromAlertingRule(),
- rulerRule: mockRulerAlertingRule(),
- ...partial,
- });
- export const mockFolder = (partial?: Partial<FolderDTO>): FolderDTO => {
- return {
- id: 1,
- uid: 'gdev-1',
- title: 'Gdev',
- version: 1,
- url: '',
- canAdmin: true,
- canDelete: true,
- canEdit: true,
- canSave: true,
- ...partial,
- };
- };
- export const enableRBAC = () => {
- jest.spyOn(contextSrv, 'accessControlEnabled').mockReturnValue(true);
- };
- export const disableRBAC = () => {
- jest.spyOn(contextSrv, 'accessControlEnabled').mockReturnValue(false);
- };
- export const grantUserPermissions = (permissions: AccessControlAction[]) => {
- jest
- .spyOn(contextSrv, 'hasPermission')
- .mockImplementation((action) => permissions.includes(action as AccessControlAction));
- };
|