variables.test.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { dimensionVariable, labelsVariable, setupMockedDataSource } from './__mocks__/CloudWatchDataSource';
  2. import { VariableQuery, VariableQueryType } from './types';
  3. import { CloudWatchVariableSupport } from './variables';
  4. const defaultQuery: VariableQuery = {
  5. queryType: VariableQueryType.Regions,
  6. namespace: 'foo',
  7. region: 'bar',
  8. metricName: '',
  9. dimensionKey: '',
  10. instanceID: '',
  11. attributeName: '',
  12. resourceType: '',
  13. refId: '',
  14. };
  15. const ds = setupMockedDataSource({ variables: [labelsVariable, dimensionVariable] });
  16. ds.datasource.getRegions = jest.fn().mockResolvedValue([{ label: 'a', value: 'a' }]);
  17. ds.datasource.getNamespaces = jest.fn().mockResolvedValue([{ label: 'b', value: 'b' }]);
  18. ds.datasource.getMetrics = jest.fn().mockResolvedValue([{ label: 'c', value: 'c' }]);
  19. ds.datasource.getDimensionKeys = jest.fn().mockResolvedValue([{ label: 'd', value: 'd' }]);
  20. const getDimensionValues = jest.fn().mockResolvedValue([{ label: 'e', value: 'e' }]);
  21. const getEbsVolumeIds = jest.fn().mockResolvedValue([{ label: 'f', value: 'f' }]);
  22. const getEc2InstanceAttribute = jest.fn().mockResolvedValue([{ label: 'g', value: 'g' }]);
  23. const getResourceARNs = jest.fn().mockResolvedValue([{ label: 'h', value: 'h' }]);
  24. const variables = new CloudWatchVariableSupport(ds.datasource);
  25. describe('variables', () => {
  26. it('should run regions', async () => {
  27. const result = await variables.execute({ ...defaultQuery });
  28. expect(result).toEqual([{ text: 'a', value: 'a', expandable: true }]);
  29. });
  30. it('should run namespaces', async () => {
  31. const result = await variables.execute({ ...defaultQuery, queryType: VariableQueryType.Namespaces });
  32. expect(result).toEqual([{ text: 'b', value: 'b', expandable: true }]);
  33. });
  34. it('should run metrics', async () => {
  35. const result = await variables.execute({ ...defaultQuery, queryType: VariableQueryType.Metrics });
  36. expect(result).toEqual([{ text: 'c', value: 'c', expandable: true }]);
  37. });
  38. it('should run dimension keys', async () => {
  39. const result = await variables.execute({ ...defaultQuery, queryType: VariableQueryType.DimensionKeys });
  40. expect(result).toEqual([{ text: 'd', value: 'd', expandable: true }]);
  41. });
  42. describe('dimension values', () => {
  43. const query = {
  44. ...defaultQuery,
  45. queryType: VariableQueryType.DimensionValues,
  46. metricName: 'abc',
  47. dimensionKey: 'efg',
  48. dimensionFilters: { a: 'b' },
  49. };
  50. beforeEach(() => {
  51. ds.datasource.getDimensionValues = getDimensionValues;
  52. getDimensionValues.mockClear();
  53. });
  54. it('should not run if dimension key not set', async () => {
  55. const result = await variables.execute({ ...query, dimensionKey: '' });
  56. expect(getDimensionValues).not.toBeCalled();
  57. expect(result).toEqual([]);
  58. });
  59. it('should not run if metric name not set', async () => {
  60. const result = await variables.execute({ ...query, metricName: '' });
  61. expect(getDimensionValues).not.toBeCalled();
  62. expect(result).toEqual([]);
  63. });
  64. it('should run if values are set', async () => {
  65. const result = await variables.execute(query);
  66. expect(getDimensionValues).toBeCalledWith(
  67. query.region,
  68. query.namespace,
  69. query.metricName,
  70. query.dimensionKey,
  71. query.dimensionFilters
  72. );
  73. expect(result).toEqual([{ text: 'e', value: 'e', expandable: true }]);
  74. });
  75. });
  76. describe('EBS volume ids', () => {
  77. beforeEach(() => {
  78. ds.datasource.getEbsVolumeIds = getEbsVolumeIds;
  79. getEbsVolumeIds.mockClear();
  80. });
  81. it('should not run if instance id not set', async () => {
  82. const result = await variables.execute({ ...defaultQuery, queryType: VariableQueryType.EBSVolumeIDs });
  83. expect(getEbsVolumeIds).not.toBeCalled();
  84. expect(result).toEqual([]);
  85. });
  86. it('should run if instance id set', async () => {
  87. const result = await variables.execute({
  88. ...defaultQuery,
  89. queryType: VariableQueryType.EBSVolumeIDs,
  90. instanceID: 'foo',
  91. });
  92. expect(getEbsVolumeIds).toBeCalledWith(defaultQuery.region, 'foo');
  93. expect(result).toEqual([{ text: 'f', value: 'f', expandable: true }]);
  94. });
  95. });
  96. describe('EC2 instance attributes', () => {
  97. const query = {
  98. ...defaultQuery,
  99. queryType: VariableQueryType.EC2InstanceAttributes,
  100. attributeName: 'abc',
  101. ec2Filters: { a: ['b'] },
  102. };
  103. beforeEach(() => {
  104. ds.datasource.getEc2InstanceAttribute = getEc2InstanceAttribute;
  105. getEc2InstanceAttribute.mockClear();
  106. });
  107. it('should not run if instance id not set', async () => {
  108. const result = await variables.execute({ ...query, attributeName: '' });
  109. expect(getEc2InstanceAttribute).not.toBeCalled();
  110. expect(result).toEqual([]);
  111. });
  112. it('should run if instance id set', async () => {
  113. const result = await variables.execute(query);
  114. expect(getEc2InstanceAttribute).toBeCalledWith(query.region, query.attributeName, { a: ['b'] });
  115. expect(result).toEqual([{ text: 'g', value: 'g', expandable: true }]);
  116. });
  117. });
  118. describe('resource arns', () => {
  119. const query = {
  120. ...defaultQuery,
  121. queryType: VariableQueryType.ResourceArns,
  122. resourceType: 'abc',
  123. tags: { a: ['b'] },
  124. };
  125. beforeEach(() => {
  126. ds.datasource.getResourceARNs = getResourceARNs;
  127. getResourceARNs.mockClear();
  128. });
  129. it('should not run if instance id not set', async () => {
  130. const result = await variables.execute({ ...query, resourceType: '' });
  131. expect(getResourceARNs).not.toBeCalled();
  132. expect(result).toEqual([]);
  133. });
  134. it('should run if instance id set', async () => {
  135. const result = await variables.execute(query);
  136. expect(getResourceARNs).toBeCalledWith(query.region, query.resourceType, { a: ['b'] });
  137. expect(result).toEqual([{ text: 'h', value: 'h', expandable: true }]);
  138. });
  139. });
  140. it('should run statistics', async () => {
  141. const result = await variables.execute({ ...defaultQuery, queryType: VariableQueryType.Statistics });
  142. expect(result).toEqual([
  143. { text: 'Average', value: 'Average', expandable: true },
  144. { text: 'Maximum', value: 'Maximum', expandable: true },
  145. { text: 'Minimum', value: 'Minimum', expandable: true },
  146. { text: 'Sum', value: 'Sum', expandable: true },
  147. { text: 'SampleCount', value: 'SampleCount', expandable: true },
  148. ]);
  149. });
  150. });