query_ctrl.test.ts 11 KB

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