query_ctrl.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. jest.mock('./css/query_editor.css', () => {
  2. return {};
  3. });
  4. import { AzureMonitorQueryCtrl } from './query_ctrl';
  5. // @ts-ignore
  6. import Q from 'q';
  7. import { TemplateSrv } from 'app/features/templating/template_srv';
  8. import { auto } from 'angular';
  9. describe('AzureMonitorQueryCtrl', () => {
  10. let queryCtrl: any;
  11. beforeEach(() => {
  12. AzureMonitorQueryCtrl.prototype.panelCtrl = {
  13. events: { on: () => {} },
  14. panel: { scopedVars: [], targets: [] },
  15. };
  16. AzureMonitorQueryCtrl.prototype.target = {} as any;
  17. AzureMonitorQueryCtrl.prototype.datasource = {
  18. $q: Q,
  19. appInsightsDatasource: { isConfigured: () => false },
  20. azureMonitorDatasource: { isConfigured: () => false },
  21. };
  22. queryCtrl = new AzureMonitorQueryCtrl({}, {} as auto.IInjectorService, new TemplateSrv());
  23. });
  24. describe('init query_ctrl variables', () => {
  25. it('should set default query type to Azure Monitor', () => {
  26. expect(queryCtrl.target.queryType).toBe('Azure Monitor');
  27. });
  28. it('should set default App Insights editor to be builder', () => {
  29. expect(queryCtrl.target.appInsights.rawQuery).toBe(false);
  30. });
  31. it('should set query parts to select', () => {
  32. expect(queryCtrl.target.azureMonitor.resourceGroup).toBe('select');
  33. expect(queryCtrl.target.azureMonitor.metricDefinition).toBe('select');
  34. expect(queryCtrl.target.azureMonitor.resourceName).toBe('select');
  35. expect(queryCtrl.target.azureMonitor.metricName).toBe('select');
  36. expect(queryCtrl.target.appInsights.groupBy).toBe('none');
  37. });
  38. });
  39. describe('when the query type is Azure Monitor', () => {
  40. describe('and getOptions for the Resource Group dropdown is called', () => {
  41. const response = [{ text: 'nodeapp', value: 'nodeapp' }, { text: 'otherapp', value: 'otherapp' }];
  42. beforeEach(() => {
  43. queryCtrl.datasource.getResourceGroups = () => {
  44. return queryCtrl.datasource.$q.when(response);
  45. };
  46. queryCtrl.datasource.azureMonitorDatasource = {
  47. isConfigured: () => {
  48. return true;
  49. },
  50. };
  51. });
  52. it('should return a list of Resource Groups', () => {
  53. return queryCtrl.getResourceGroups('').then((result: any) => {
  54. expect(result[0].text).toBe('nodeapp');
  55. });
  56. });
  57. });
  58. describe('when getOptions for the Metric Definition dropdown is called', () => {
  59. describe('and resource group has a value', () => {
  60. const response = [
  61. { text: 'Microsoft.Compute/virtualMachines', value: 'Microsoft.Compute/virtualMachines' },
  62. { text: 'Microsoft.Network/publicIPAddresses', value: 'Microsoft.Network/publicIPAddresses' },
  63. ];
  64. beforeEach(() => {
  65. queryCtrl.target.subscription = 'sub1';
  66. queryCtrl.target.azureMonitor.resourceGroup = 'test';
  67. queryCtrl.datasource.getMetricDefinitions = function(subscriptionId: any, query: any) {
  68. expect(subscriptionId).toBe('sub1');
  69. expect(query).toBe('test');
  70. return this.$q.when(response);
  71. };
  72. });
  73. it('should return a list of Metric Definitions', () => {
  74. return queryCtrl.getMetricDefinitions('').then((result: any) => {
  75. expect(result[0].text).toBe('Microsoft.Compute/virtualMachines');
  76. expect(result[1].text).toBe('Microsoft.Network/publicIPAddresses');
  77. });
  78. });
  79. });
  80. describe('and resource group has no value', () => {
  81. beforeEach(() => {
  82. queryCtrl.target.azureMonitor.resourceGroup = 'select';
  83. });
  84. it('should return without making a call to datasource', () => {
  85. expect(queryCtrl.getMetricDefinitions('')).toBe(undefined);
  86. });
  87. });
  88. });
  89. describe('when getOptions for the ResourceNames dropdown is called', () => {
  90. describe('and resourceGroup and metricDefinition have values', () => {
  91. const response = [{ text: 'test1', value: 'test1' }, { text: 'test2', value: 'test2' }];
  92. beforeEach(() => {
  93. queryCtrl.target.subscription = 'sub1';
  94. queryCtrl.target.azureMonitor.resourceGroup = 'test';
  95. queryCtrl.target.azureMonitor.metricDefinition = 'Microsoft.Compute/virtualMachines';
  96. queryCtrl.datasource.getResourceNames = function(
  97. subscriptionId: any,
  98. resourceGroup: any,
  99. metricDefinition: any
  100. ) {
  101. expect(subscriptionId).toBe('sub1');
  102. expect(resourceGroup).toBe('test');
  103. expect(metricDefinition).toBe('Microsoft.Compute/virtualMachines');
  104. return this.$q.when(response);
  105. };
  106. });
  107. it('should return a list of Resource Names', () => {
  108. return queryCtrl.getResourceNames('').then((result: any) => {
  109. expect(result[0].text).toBe('test1');
  110. expect(result[1].text).toBe('test2');
  111. });
  112. });
  113. });
  114. describe('and resourceGroup and metricDefinition do not have values', () => {
  115. beforeEach(() => {
  116. queryCtrl.target.azureMonitor.resourceGroup = 'select';
  117. queryCtrl.target.azureMonitor.metricDefinition = 'select';
  118. });
  119. it('should return without making a call to datasource', () => {
  120. expect(queryCtrl.getResourceNames('')).toBe(undefined);
  121. });
  122. });
  123. });
  124. describe('when getOptions for the Metric Names dropdown is called', () => {
  125. describe('and resourceGroup, metricDefinition and resourceName have values', () => {
  126. const response = [{ text: 'metric1', value: 'metric1' }, { text: 'metric2', value: 'metric2' }];
  127. beforeEach(() => {
  128. queryCtrl.target.subscription = 'sub1';
  129. queryCtrl.target.azureMonitor.resourceGroup = 'test';
  130. queryCtrl.target.azureMonitor.metricDefinition = 'Microsoft.Compute/virtualMachines';
  131. queryCtrl.target.azureMonitor.resourceName = 'test';
  132. queryCtrl.datasource.getMetricNames = function(
  133. subscriptionId: any,
  134. resourceGroup: any,
  135. metricDefinition: any,
  136. resourceName: any
  137. ) {
  138. expect(subscriptionId).toBe('sub1');
  139. expect(resourceGroup).toBe('test');
  140. expect(metricDefinition).toBe('Microsoft.Compute/virtualMachines');
  141. expect(resourceName).toBe('test');
  142. return this.$q.when(response);
  143. };
  144. });
  145. it('should return a list of Metric Names', () => {
  146. return queryCtrl.getMetricNames('').then((result: any) => {
  147. expect(result[0].text).toBe('metric1');
  148. expect(result[1].text).toBe('metric2');
  149. });
  150. });
  151. });
  152. describe('and resourceGroup, metricDefinition and resourceName do not have values', () => {
  153. beforeEach(() => {
  154. queryCtrl.target.azureMonitor.resourceGroup = 'select';
  155. queryCtrl.target.azureMonitor.metricDefinition = 'select';
  156. queryCtrl.target.azureMonitor.resourceName = 'select';
  157. });
  158. it('should return without making a call to datasource', () => {
  159. expect(queryCtrl.getMetricNames('')).toBe(undefined);
  160. });
  161. });
  162. });
  163. describe('when onMetricNameChange is triggered for the Metric Names dropdown', () => {
  164. const response: any = {
  165. primaryAggType: 'Average',
  166. supportAggOptions: ['Average', 'Total'],
  167. supportedTimeGrains: ['PT1M', 'P1D'],
  168. dimensions: [],
  169. };
  170. beforeEach(() => {
  171. queryCtrl.target.subscription = 'sub1';
  172. queryCtrl.target.azureMonitor.resourceGroup = 'test';
  173. queryCtrl.target.azureMonitor.metricDefinition = 'Microsoft.Compute/virtualMachines';
  174. queryCtrl.target.azureMonitor.resourceName = 'test';
  175. queryCtrl.target.azureMonitor.metricName = 'Percentage CPU';
  176. queryCtrl.datasource.getMetricMetadata = function(
  177. subscription: any,
  178. resourceGroup: any,
  179. metricDefinition: any,
  180. resourceName: any,
  181. metricName: any
  182. ) {
  183. expect(subscription).toBe('sub1');
  184. expect(resourceGroup).toBe('test');
  185. expect(metricDefinition).toBe('Microsoft.Compute/virtualMachines');
  186. expect(resourceName).toBe('test');
  187. expect(metricName).toBe('Percentage CPU');
  188. return this.$q.when(response);
  189. };
  190. });
  191. it('should set the options and default selected value for the Aggregations dropdown', () => {
  192. queryCtrl.onMetricNameChange().then(() => {
  193. expect(queryCtrl.target.azureMonitor.aggregation).toBe('Average');
  194. expect(queryCtrl.target.azureMonitor.aggOptions).toBe(['Average', 'Total']);
  195. expect(queryCtrl.target.azureMonitor.timeGrains).toBe(['PT1M', 'P1D']);
  196. });
  197. });
  198. });
  199. });
  200. describe('and query type is Application Insights', () => {
  201. describe('when getOptions for the Metric Names dropdown is called', () => {
  202. const response = [{ text: 'metric1', value: 'metric1' }, { text: 'metric2', value: 'metric2' }];
  203. beforeEach(() => {
  204. queryCtrl.datasource.appInsightsDatasource.isConfigured = () => true;
  205. queryCtrl.datasource.getAppInsightsMetricNames = () => {
  206. return queryCtrl.datasource.$q.when(response);
  207. };
  208. });
  209. it('should return a list of Metric Names', () => {
  210. return queryCtrl.getAppInsightsMetricNames().then((result: any) => {
  211. expect(result[0].text).toBe('metric1');
  212. expect(result[1].text).toBe('metric2');
  213. });
  214. });
  215. });
  216. describe('when getOptions for the GroupBy segments dropdown is called', () => {
  217. beforeEach(() => {
  218. queryCtrl.target.appInsights.groupByOptions = ['opt1', 'opt2'];
  219. });
  220. it('should return a list of GroupBy segments', () => {
  221. const result = queryCtrl.getAppInsightsGroupBySegments('');
  222. expect(result[0].text).toBe('opt1');
  223. expect(result[0].value).toBe('opt1');
  224. expect(result[1].text).toBe('opt2');
  225. expect(result[1].value).toBe('opt2');
  226. });
  227. });
  228. describe('when onAppInsightsMetricNameChange is triggered for the Metric Names dropdown', () => {
  229. const response = {
  230. primaryAggType: 'avg',
  231. supportedAggTypes: ['avg', 'sum'],
  232. supportedGroupBy: ['client/os', 'client/city'],
  233. };
  234. beforeEach(() => {
  235. queryCtrl.target.appInsights.metricName = 'requests/failed';
  236. queryCtrl.datasource.getAppInsightsMetricMetadata = function(metricName: string) {
  237. expect(metricName).toBe('requests/failed');
  238. return this.$q.when(response);
  239. };
  240. });
  241. it('should set the options and default selected value for the Aggregations dropdown', () => {
  242. return queryCtrl.onAppInsightsMetricNameChange().then(() => {
  243. expect(queryCtrl.target.appInsights.aggregation).toBe('avg');
  244. expect(queryCtrl.target.appInsights.aggOptions).toContain('avg');
  245. expect(queryCtrl.target.appInsights.aggOptions).toContain('sum');
  246. expect(queryCtrl.target.appInsights.groupByOptions).toContain('client/os');
  247. expect(queryCtrl.target.appInsights.groupByOptions).toContain('client/city');
  248. });
  249. });
  250. });
  251. });
  252. });