getAlertingValidationMessage.test.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import {
  2. DataSourceApi,
  3. PluginMeta,
  4. DataTransformerConfig,
  5. DataSourceInstanceSettings,
  6. DataSourceRef,
  7. } from '@grafana/data';
  8. import { DataSourceSrv } from '@grafana/runtime';
  9. import { ElasticsearchQuery } from '../../plugins/datasource/elasticsearch/types';
  10. import { getAlertingValidationMessage } from './getAlertingValidationMessage';
  11. describe('getAlertingValidationMessage', () => {
  12. describe('when called with some targets containing template variables', () => {
  13. it('then it should return false', async () => {
  14. let call = 0;
  15. const datasource: DataSourceApi = {
  16. meta: { alerting: true } as any as PluginMeta,
  17. targetContainsTemplate: () => {
  18. if (call === 0) {
  19. call++;
  20. return true;
  21. }
  22. return false;
  23. },
  24. name: 'some name',
  25. uid: 'some uid',
  26. } as any as DataSourceApi;
  27. const getMock = jest.fn().mockResolvedValue(datasource);
  28. const datasourceSrv: DataSourceSrv = {
  29. get: (ref: DataSourceRef) => {
  30. return getMock(ref.uid);
  31. },
  32. getList(): DataSourceInstanceSettings[] {
  33. return [];
  34. },
  35. getInstanceSettings: (() => {}) as any,
  36. reload: () => jest.fn(),
  37. };
  38. const targets: ElasticsearchQuery[] = [
  39. { refId: 'A', query: '@hostname:$hostname' },
  40. { refId: 'B', query: '@instance:instance' },
  41. ];
  42. const transformations: DataTransformerConfig[] = [];
  43. const result = await getAlertingValidationMessage(transformations, targets, datasourceSrv, {
  44. uid: datasource.uid,
  45. });
  46. expect(result).toBe('');
  47. expect(getMock).toHaveBeenCalledTimes(2);
  48. expect(getMock).toHaveBeenCalledWith(datasource.uid);
  49. });
  50. });
  51. describe('when called with some targets using a datasource that does not support alerting', () => {
  52. it('then it should return false', async () => {
  53. const alertingDatasource: DataSourceApi = {
  54. meta: { alerting: true } as any as PluginMeta,
  55. targetContainsTemplate: () => false,
  56. name: 'alertingDatasource',
  57. } as any as DataSourceApi;
  58. const datasource: DataSourceApi = {
  59. meta: { alerting: false } as any as PluginMeta,
  60. targetContainsTemplate: () => false,
  61. name: 'datasource',
  62. } as any as DataSourceApi;
  63. const datasourceSrv: DataSourceSrv = {
  64. get: (name: string) => {
  65. if (name === datasource.name) {
  66. return Promise.resolve(datasource);
  67. }
  68. return Promise.resolve(alertingDatasource);
  69. },
  70. getInstanceSettings: (() => {}) as any,
  71. getList(): DataSourceInstanceSettings[] {
  72. return [];
  73. },
  74. reload: () => jest.fn(),
  75. };
  76. const targets: any[] = [
  77. { refId: 'A', query: 'some query', datasource: 'alertingDatasource' },
  78. { refId: 'B', query: 'some query', datasource: 'datasource' },
  79. ];
  80. const transformations: DataTransformerConfig[] = [];
  81. const result = await getAlertingValidationMessage(transformations, targets, datasourceSrv, {
  82. uid: datasource.name,
  83. });
  84. expect(result).toBe('');
  85. });
  86. });
  87. describe('when called with all targets containing template variables', () => {
  88. it('then it should return false', async () => {
  89. const datasource: DataSourceApi = {
  90. meta: { alerting: true } as any as PluginMeta,
  91. targetContainsTemplate: () => true,
  92. name: 'some name',
  93. } as any as DataSourceApi;
  94. const getMock = jest.fn().mockResolvedValue(datasource);
  95. const datasourceSrv: DataSourceSrv = {
  96. get: (ref: DataSourceRef) => {
  97. return getMock(ref.uid);
  98. },
  99. getInstanceSettings: (() => {}) as any,
  100. getList(): DataSourceInstanceSettings[] {
  101. return [];
  102. },
  103. reload: () => jest.fn(),
  104. };
  105. const targets: ElasticsearchQuery[] = [
  106. { refId: 'A', query: '@hostname:$hostname' },
  107. { refId: 'B', query: '@instance:$instance' },
  108. ];
  109. const transformations: DataTransformerConfig[] = [];
  110. const result = await getAlertingValidationMessage(transformations, targets, datasourceSrv, {
  111. uid: datasource.name,
  112. });
  113. expect(result).toBe('Template variables are not supported in alert queries');
  114. expect(getMock).toHaveBeenCalledTimes(2);
  115. expect(getMock).toHaveBeenCalledWith(datasource.name);
  116. });
  117. });
  118. describe('when called with all targets using a datasource that does not support alerting', () => {
  119. it('then it should return false', async () => {
  120. const datasource: DataSourceApi = {
  121. meta: { alerting: false } as any as PluginMeta,
  122. targetContainsTemplate: () => false,
  123. name: 'some name',
  124. uid: 'theid',
  125. } as any as DataSourceApi;
  126. const getMock = jest.fn().mockResolvedValue(datasource);
  127. const datasourceSrv: DataSourceSrv = {
  128. get: (ref: DataSourceRef) => {
  129. return getMock(ref.uid);
  130. },
  131. getInstanceSettings: (() => {}) as any,
  132. getList(): DataSourceInstanceSettings[] {
  133. return [];
  134. },
  135. reload: () => jest.fn(),
  136. };
  137. const targets: ElasticsearchQuery[] = [
  138. { refId: 'A', query: '@hostname:hostname' },
  139. { refId: 'B', query: '@instance:instance' },
  140. ];
  141. const transformations: DataTransformerConfig[] = [];
  142. const result = await getAlertingValidationMessage(transformations, targets, datasourceSrv, {
  143. uid: datasource.uid,
  144. });
  145. expect(result).toBe('The datasource does not support alerting queries');
  146. expect(getMock).toHaveBeenCalledTimes(2);
  147. expect(getMock).toHaveBeenCalledWith(datasource.uid);
  148. });
  149. });
  150. describe('when called with transformations', () => {
  151. it('then it should return false', async () => {
  152. const datasource: DataSourceApi = {
  153. meta: { alerting: true } as any as PluginMeta,
  154. targetContainsTemplate: () => false,
  155. name: 'some name',
  156. } as any as DataSourceApi;
  157. const getMock = jest.fn().mockResolvedValue(datasource);
  158. const datasourceSrv: DataSourceSrv = {
  159. get: (ref: DataSourceRef) => {
  160. return getMock(ref.uid);
  161. },
  162. getInstanceSettings: (() => {}) as any,
  163. getList(): DataSourceInstanceSettings[] {
  164. return [];
  165. },
  166. reload: () => jest.fn(),
  167. };
  168. const targets: ElasticsearchQuery[] = [
  169. { refId: 'A', query: '@hostname:hostname' },
  170. { refId: 'B', query: '@instance:instance' },
  171. ];
  172. const transformations: DataTransformerConfig[] = [{ id: 'A', options: null }];
  173. const result = await getAlertingValidationMessage(transformations, targets, datasourceSrv, {
  174. uid: datasource.uid,
  175. });
  176. expect(result).toBe('Transformations are not supported in alert queries');
  177. expect(getMock).toHaveBeenCalledTimes(0);
  178. });
  179. });
  180. });