123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- import { Subject, throwError } from 'rxjs';
- import { delay } from 'rxjs/operators';
- import { AnnotationQuery } from '@grafana/data';
- import { setDataSourceSrv } from '@grafana/runtime';
- import { silenceConsoleOutput } from '../../../../../test/core/utils/silenceConsoleOutput';
- import * as annotationsSrv from '../../../annotations/executeAnnotationQuery';
- import { AnnotationsWorker } from './AnnotationsWorker';
- import { createDashboardQueryRunner, setDashboardQueryRunnerFactory } from './DashboardQueryRunner';
- import { getDefaultOptions, LEGACY_DS_NAME, NEXT_GEN_DS_NAME, toAsyncOfResult } from './testHelpers';
- import { DashboardQueryRunnerOptions, DashboardQueryRunnerWorkerResult } from './types';
- import { emptyResult } from './utils';
- function getTestContext(dataSourceSrvRejects = false) {
- jest.clearAllMocks();
- const cancellations = new Subject<AnnotationQuery>();
- setDashboardQueryRunnerFactory(() => ({
- getResult: emptyResult,
- run: () => undefined,
- cancel: () => undefined,
- cancellations: () => cancellations,
- destroy: () => undefined,
- }));
- createDashboardQueryRunner({} as any);
- const executeAnnotationQueryMock = jest
- .spyOn(annotationsSrv, 'executeAnnotationQuery')
- .mockReturnValue(toAsyncOfResult({ events: [{ id: 'NextGen' }] }));
- const annotationQueryMock = jest.fn().mockResolvedValue([{ id: 'Legacy' }]);
- const dataSourceSrvMock: any = {
- get: async (name: string) => {
- if (dataSourceSrvRejects) {
- return Promise.reject(`Could not find datasource with name: ${name}`);
- }
- if (name === LEGACY_DS_NAME) {
- return {
- annotationQuery: annotationQueryMock,
- };
- }
- if (name === NEXT_GEN_DS_NAME) {
- return {
- annotations: {},
- };
- }
- return {};
- },
- };
- setDataSourceSrv(dataSourceSrvMock);
- const options = getDefaultOptions();
- return { options, annotationQueryMock, executeAnnotationQueryMock, cancellations };
- }
- function expectOnResults(args: {
- worker: AnnotationsWorker;
- options: DashboardQueryRunnerOptions;
- done: jest.DoneCallback;
- expect: (results: DashboardQueryRunnerWorkerResult) => void;
- }) {
- const { worker, done, options, expect: expectCallback } = args;
- const subscription = worker.work(options).subscribe({
- next: (value) => {
- try {
- expectCallback(value);
- subscription.unsubscribe();
- done();
- } catch (err) {
- subscription.unsubscribe();
- done(err);
- }
- },
- });
- }
- describe('AnnotationsWorker', () => {
- const worker = new AnnotationsWorker();
- describe('when canWork is called with correct props', () => {
- it('then it should return true', () => {
- const options = getDefaultOptions();
- expect(worker.canWork(options)).toBe(true);
- });
- });
- describe('when canWork is called with incorrect props', () => {
- it('then it should return false', () => {
- const dashboard: any = { annotations: { list: [] } };
- const options = { ...getDefaultOptions(), dashboard };
- expect(worker.canWork(options)).toBe(false);
- });
- });
- describe('when run is called with incorrect props', () => {
- it('then it should return the correct results', async () => {
- const dashboard: any = { annotations: { list: [] } };
- const options = { ...getDefaultOptions(), dashboard };
- await expect(worker.work(options)).toEmitValues([{ alertStates: [], annotations: [] }]);
- });
- });
- describe('when run is called with correct props and all workers are successful', () => {
- it('then it should return the correct results', async () => {
- const { options, executeAnnotationQueryMock, annotationQueryMock } = getTestContext();
- await expect(worker.work(options)).toEmitValuesWith((received) => {
- expect(received).toHaveLength(1);
- const result = received[0];
- expect(result).toEqual({
- alertStates: [],
- annotations: [
- {
- id: 'Legacy',
- source: {
- enable: true,
- hide: false,
- name: 'Test',
- iconColor: 'pink',
- snapshotData: undefined,
- datasource: 'Legacy',
- },
- color: '#ffc0cb',
- type: 'Test',
- isRegion: false,
- },
- {
- id: 'NextGen',
- source: {
- enable: true,
- hide: false,
- name: 'Test',
- iconColor: 'pink',
- snapshotData: undefined,
- datasource: 'NextGen',
- },
- color: '#ffc0cb',
- type: 'Test',
- isRegion: false,
- },
- ],
- });
- expect(executeAnnotationQueryMock).toHaveBeenCalledTimes(1);
- expect(annotationQueryMock).toHaveBeenCalledTimes(1);
- });
- });
- });
- describe('when run is called with correct props and legacy worker fails', () => {
- silenceConsoleOutput();
- it('then it should return the correct results', async () => {
- const { options, executeAnnotationQueryMock, annotationQueryMock } = getTestContext();
- annotationQueryMock.mockRejectedValue({ message: 'Some error' });
- await expect(worker.work(options)).toEmitValuesWith((received) => {
- expect(received).toHaveLength(1);
- const result = received[0];
- expect(result).toEqual({
- alertStates: [],
- annotations: [
- {
- id: 'NextGen',
- source: {
- enable: true,
- hide: false,
- name: 'Test',
- iconColor: 'pink',
- snapshotData: undefined,
- datasource: 'NextGen',
- },
- color: '#ffc0cb',
- type: 'Test',
- isRegion: false,
- },
- ],
- });
- expect(executeAnnotationQueryMock).toHaveBeenCalledTimes(1);
- expect(annotationQueryMock).toHaveBeenCalledTimes(1);
- });
- });
- });
- describe('when run is called with correct props and a worker is cancelled', () => {
- it('then it should return the correct results', (done) => {
- const { options, executeAnnotationQueryMock, annotationQueryMock, cancellations } = getTestContext();
- executeAnnotationQueryMock.mockReturnValueOnce(
- toAsyncOfResult({ events: [{ id: 'NextGen' }] }).pipe(delay(10000))
- );
- expectOnResults({
- worker,
- options,
- done,
- expect: (results) => {
- expect(results).toEqual({
- alertStates: [],
- annotations: [
- {
- id: 'Legacy',
- source: {
- enable: true,
- hide: false,
- name: 'Test',
- iconColor: 'pink',
- snapshotData: undefined,
- datasource: 'Legacy',
- },
- color: '#ffc0cb',
- type: 'Test',
- isRegion: false,
- },
- ],
- });
- expect(executeAnnotationQueryMock).toHaveBeenCalledTimes(1);
- expect(annotationQueryMock).toHaveBeenCalledTimes(1);
- },
- });
- setTimeout(() => {
- // call to async needs to be async or the cancellation will be called before any of the runners have started
- cancellations.next(options.dashboard.annotations.list[1]);
- }, 100);
- });
- });
- describe('when run is called with correct props and nextgen worker fails', () => {
- silenceConsoleOutput();
- it('then it should return the correct results', async () => {
- const { options, executeAnnotationQueryMock, annotationQueryMock } = getTestContext();
- executeAnnotationQueryMock.mockReturnValue(throwError({ message: 'An error' }));
- await expect(worker.work(options)).toEmitValuesWith((received) => {
- expect(received).toHaveLength(1);
- const result = received[0];
- expect(result).toEqual({
- alertStates: [],
- annotations: [
- {
- id: 'Legacy',
- source: {
- enable: true,
- hide: false,
- name: 'Test',
- iconColor: 'pink',
- snapshotData: undefined,
- datasource: 'Legacy',
- },
- color: '#ffc0cb',
- type: 'Test',
- isRegion: false,
- },
- ],
- });
- expect(executeAnnotationQueryMock).toHaveBeenCalledTimes(1);
- expect(annotationQueryMock).toHaveBeenCalledTimes(1);
- });
- });
- });
- describe('when run is called with correct props and both workers fail', () => {
- silenceConsoleOutput();
- it('then it should return the correct results', async () => {
- const { options, executeAnnotationQueryMock, annotationQueryMock } = getTestContext();
- annotationQueryMock.mockRejectedValue({ message: 'Some error' });
- executeAnnotationQueryMock.mockReturnValue(throwError({ message: 'An error' }));
- await expect(worker.work(options)).toEmitValuesWith((received) => {
- expect(received).toHaveLength(1);
- const result = received[0];
- expect(result).toEqual({ alertStates: [], annotations: [] });
- expect(executeAnnotationQueryMock).toHaveBeenCalledTimes(1);
- expect(annotationQueryMock).toHaveBeenCalledTimes(1);
- });
- });
- });
- describe('when run is called with correct props and call to datasourceSrv fails', () => {
- silenceConsoleOutput();
- it('then it should return the correct results', async () => {
- const { options, executeAnnotationQueryMock, annotationQueryMock } = getTestContext(true);
- await expect(worker.work(options)).toEmitValuesWith((received) => {
- expect(received).toHaveLength(1);
- const result = received[0];
- expect(result).toEqual({ alertStates: [], annotations: [] });
- expect(executeAnnotationQueryMock).not.toHaveBeenCalled();
- expect(annotationQueryMock).not.toHaveBeenCalled();
- });
- });
- });
- });
|