query_ctrl.test.ts 10 KB

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