AnnotationsWorker.test.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import { Subject, throwError } from 'rxjs';
  2. import { delay } from 'rxjs/operators';
  3. import { AnnotationQuery } from '@grafana/data';
  4. import { setDataSourceSrv } from '@grafana/runtime';
  5. import { silenceConsoleOutput } from '../../../../../test/core/utils/silenceConsoleOutput';
  6. import * as annotationsSrv from '../../../annotations/executeAnnotationQuery';
  7. import { AnnotationsWorker } from './AnnotationsWorker';
  8. import { createDashboardQueryRunner, setDashboardQueryRunnerFactory } from './DashboardQueryRunner';
  9. import { getDefaultOptions, LEGACY_DS_NAME, NEXT_GEN_DS_NAME, toAsyncOfResult } from './testHelpers';
  10. import { DashboardQueryRunnerOptions, DashboardQueryRunnerWorkerResult } from './types';
  11. import { emptyResult } from './utils';
  12. function getTestContext(dataSourceSrvRejects = false) {
  13. jest.clearAllMocks();
  14. const cancellations = new Subject<AnnotationQuery>();
  15. setDashboardQueryRunnerFactory(() => ({
  16. getResult: emptyResult,
  17. run: () => undefined,
  18. cancel: () => undefined,
  19. cancellations: () => cancellations,
  20. destroy: () => undefined,
  21. }));
  22. createDashboardQueryRunner({} as any);
  23. const executeAnnotationQueryMock = jest
  24. .spyOn(annotationsSrv, 'executeAnnotationQuery')
  25. .mockReturnValue(toAsyncOfResult({ events: [{ id: 'NextGen' }] }));
  26. const annotationQueryMock = jest.fn().mockResolvedValue([{ id: 'Legacy' }]);
  27. const dataSourceSrvMock: any = {
  28. get: async (name: string) => {
  29. if (dataSourceSrvRejects) {
  30. return Promise.reject(`Could not find datasource with name: ${name}`);
  31. }
  32. if (name === LEGACY_DS_NAME) {
  33. return {
  34. annotationQuery: annotationQueryMock,
  35. };
  36. }
  37. if (name === NEXT_GEN_DS_NAME) {
  38. return {
  39. annotations: {},
  40. };
  41. }
  42. return {};
  43. },
  44. };
  45. setDataSourceSrv(dataSourceSrvMock);
  46. const options = getDefaultOptions();
  47. return { options, annotationQueryMock, executeAnnotationQueryMock, cancellations };
  48. }
  49. function expectOnResults(args: {
  50. worker: AnnotationsWorker;
  51. options: DashboardQueryRunnerOptions;
  52. done: jest.DoneCallback;
  53. expect: (results: DashboardQueryRunnerWorkerResult) => void;
  54. }) {
  55. const { worker, done, options, expect: expectCallback } = args;
  56. const subscription = worker.work(options).subscribe({
  57. next: (value) => {
  58. try {
  59. expectCallback(value);
  60. subscription.unsubscribe();
  61. done();
  62. } catch (err) {
  63. subscription.unsubscribe();
  64. done(err);
  65. }
  66. },
  67. });
  68. }
  69. describe('AnnotationsWorker', () => {
  70. const worker = new AnnotationsWorker();
  71. describe('when canWork is called with correct props', () => {
  72. it('then it should return true', () => {
  73. const options = getDefaultOptions();
  74. expect(worker.canWork(options)).toBe(true);
  75. });
  76. });
  77. describe('when canWork is called with incorrect props', () => {
  78. it('then it should return false', () => {
  79. const dashboard: any = { annotations: { list: [] } };
  80. const options = { ...getDefaultOptions(), dashboard };
  81. expect(worker.canWork(options)).toBe(false);
  82. });
  83. });
  84. describe('when run is called with incorrect props', () => {
  85. it('then it should return the correct results', async () => {
  86. const dashboard: any = { annotations: { list: [] } };
  87. const options = { ...getDefaultOptions(), dashboard };
  88. await expect(worker.work(options)).toEmitValues([{ alertStates: [], annotations: [] }]);
  89. });
  90. });
  91. describe('when run is called with correct props and all workers are successful', () => {
  92. it('then it should return the correct results', async () => {
  93. const { options, executeAnnotationQueryMock, annotationQueryMock } = getTestContext();
  94. await expect(worker.work(options)).toEmitValuesWith((received) => {
  95. expect(received).toHaveLength(1);
  96. const result = received[0];
  97. expect(result).toEqual({
  98. alertStates: [],
  99. annotations: [
  100. {
  101. id: 'Legacy',
  102. source: {
  103. enable: true,
  104. hide: false,
  105. name: 'Test',
  106. iconColor: 'pink',
  107. snapshotData: undefined,
  108. datasource: 'Legacy',
  109. },
  110. color: '#ffc0cb',
  111. type: 'Test',
  112. isRegion: false,
  113. },
  114. {
  115. id: 'NextGen',
  116. source: {
  117. enable: true,
  118. hide: false,
  119. name: 'Test',
  120. iconColor: 'pink',
  121. snapshotData: undefined,
  122. datasource: 'NextGen',
  123. },
  124. color: '#ffc0cb',
  125. type: 'Test',
  126. isRegion: false,
  127. },
  128. ],
  129. });
  130. expect(executeAnnotationQueryMock).toHaveBeenCalledTimes(1);
  131. expect(annotationQueryMock).toHaveBeenCalledTimes(1);
  132. });
  133. });
  134. });
  135. describe('when run is called with correct props and legacy worker fails', () => {
  136. silenceConsoleOutput();
  137. it('then it should return the correct results', async () => {
  138. const { options, executeAnnotationQueryMock, annotationQueryMock } = getTestContext();
  139. annotationQueryMock.mockRejectedValue({ message: 'Some error' });
  140. await expect(worker.work(options)).toEmitValuesWith((received) => {
  141. expect(received).toHaveLength(1);
  142. const result = received[0];
  143. expect(result).toEqual({
  144. alertStates: [],
  145. annotations: [
  146. {
  147. id: 'NextGen',
  148. source: {
  149. enable: true,
  150. hide: false,
  151. name: 'Test',
  152. iconColor: 'pink',
  153. snapshotData: undefined,
  154. datasource: 'NextGen',
  155. },
  156. color: '#ffc0cb',
  157. type: 'Test',
  158. isRegion: false,
  159. },
  160. ],
  161. });
  162. expect(executeAnnotationQueryMock).toHaveBeenCalledTimes(1);
  163. expect(annotationQueryMock).toHaveBeenCalledTimes(1);
  164. });
  165. });
  166. });
  167. describe('when run is called with correct props and a worker is cancelled', () => {
  168. it('then it should return the correct results', (done) => {
  169. const { options, executeAnnotationQueryMock, annotationQueryMock, cancellations } = getTestContext();
  170. executeAnnotationQueryMock.mockReturnValueOnce(
  171. toAsyncOfResult({ events: [{ id: 'NextGen' }] }).pipe(delay(10000))
  172. );
  173. expectOnResults({
  174. worker,
  175. options,
  176. done,
  177. expect: (results) => {
  178. expect(results).toEqual({
  179. alertStates: [],
  180. annotations: [
  181. {
  182. id: 'Legacy',
  183. source: {
  184. enable: true,
  185. hide: false,
  186. name: 'Test',
  187. iconColor: 'pink',
  188. snapshotData: undefined,
  189. datasource: 'Legacy',
  190. },
  191. color: '#ffc0cb',
  192. type: 'Test',
  193. isRegion: false,
  194. },
  195. ],
  196. });
  197. expect(executeAnnotationQueryMock).toHaveBeenCalledTimes(1);
  198. expect(annotationQueryMock).toHaveBeenCalledTimes(1);
  199. },
  200. });
  201. setTimeout(() => {
  202. // call to async needs to be async or the cancellation will be called before any of the runners have started
  203. cancellations.next(options.dashboard.annotations.list[1]);
  204. }, 100);
  205. });
  206. });
  207. describe('when run is called with correct props and nextgen worker fails', () => {
  208. silenceConsoleOutput();
  209. it('then it should return the correct results', async () => {
  210. const { options, executeAnnotationQueryMock, annotationQueryMock } = getTestContext();
  211. executeAnnotationQueryMock.mockReturnValue(throwError({ message: 'An error' }));
  212. await expect(worker.work(options)).toEmitValuesWith((received) => {
  213. expect(received).toHaveLength(1);
  214. const result = received[0];
  215. expect(result).toEqual({
  216. alertStates: [],
  217. annotations: [
  218. {
  219. id: 'Legacy',
  220. source: {
  221. enable: true,
  222. hide: false,
  223. name: 'Test',
  224. iconColor: 'pink',
  225. snapshotData: undefined,
  226. datasource: 'Legacy',
  227. },
  228. color: '#ffc0cb',
  229. type: 'Test',
  230. isRegion: false,
  231. },
  232. ],
  233. });
  234. expect(executeAnnotationQueryMock).toHaveBeenCalledTimes(1);
  235. expect(annotationQueryMock).toHaveBeenCalledTimes(1);
  236. });
  237. });
  238. });
  239. describe('when run is called with correct props and both workers fail', () => {
  240. silenceConsoleOutput();
  241. it('then it should return the correct results', async () => {
  242. const { options, executeAnnotationQueryMock, annotationQueryMock } = getTestContext();
  243. annotationQueryMock.mockRejectedValue({ message: 'Some error' });
  244. executeAnnotationQueryMock.mockReturnValue(throwError({ message: 'An error' }));
  245. await expect(worker.work(options)).toEmitValuesWith((received) => {
  246. expect(received).toHaveLength(1);
  247. const result = received[0];
  248. expect(result).toEqual({ alertStates: [], annotations: [] });
  249. expect(executeAnnotationQueryMock).toHaveBeenCalledTimes(1);
  250. expect(annotationQueryMock).toHaveBeenCalledTimes(1);
  251. });
  252. });
  253. });
  254. describe('when run is called with correct props and call to datasourceSrv fails', () => {
  255. silenceConsoleOutput();
  256. it('then it should return the correct results', async () => {
  257. const { options, executeAnnotationQueryMock, annotationQueryMock } = getTestContext(true);
  258. await expect(worker.work(options)).toEmitValuesWith((received) => {
  259. expect(received).toHaveLength(1);
  260. const result = received[0];
  261. expect(result).toEqual({ alertStates: [], annotations: [] });
  262. expect(executeAnnotationQueryMock).not.toHaveBeenCalled();
  263. expect(annotationQueryMock).not.toHaveBeenCalled();
  264. });
  265. });
  266. });
  267. });