variableQueryMigrations.test.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { VariableQueryType, OldVariableQuery } from '../types';
  2. import { migrateVariableQuery } from './variableQueryMigrations';
  3. describe('variableQueryMigrations', () => {
  4. describe('migrateVariableQuery', () => {
  5. describe('when metrics query is used', () => {
  6. describe('and region param is left out', () => {
  7. it('should leave an empty region', () => {
  8. const query = migrateVariableQuery('metrics(testNamespace)');
  9. expect(query.queryType).toBe(VariableQueryType.Metrics);
  10. expect(query.namespace).toBe('testNamespace');
  11. expect(query.region).toBe('');
  12. });
  13. });
  14. describe('and region param is defined by user', () => {
  15. it('should use the user defined region', () => {
  16. const query = migrateVariableQuery('metrics(testNamespace2, custom-region)');
  17. expect(query.queryType).toBe(VariableQueryType.Metrics);
  18. expect(query.namespace).toBe('testNamespace2');
  19. expect(query.region).toBe('custom-region');
  20. });
  21. });
  22. });
  23. describe('when dimension_values query is used', () => {
  24. describe('and filter param is left out', () => {
  25. it('should leave an empty filter', () => {
  26. const query = migrateVariableQuery('dimension_values(us-east-1,AWS/RDS,CPUUtilization,DBInstanceIdentifier)');
  27. expect(query.queryType).toBe(VariableQueryType.DimensionValues);
  28. expect(query.region).toBe('us-east-1');
  29. expect(query.namespace).toBe('AWS/RDS');
  30. expect(query.metricName).toBe('CPUUtilization');
  31. expect(query.dimensionKey).toBe('DBInstanceIdentifier');
  32. expect(query.dimensionFilters).toStrictEqual({});
  33. });
  34. });
  35. describe('and filter value is an empty array', () => {
  36. it('should leave an empty filter', () => {
  37. const query = migrateVariableQuery(
  38. 'dimension_values(us-east-1,AWS/RDS,CPUUtilization,DBInstanceIdentifier, [])'
  39. );
  40. expect(query.dimensionFilters).toStrictEqual({});
  41. });
  42. });
  43. describe('and filter param is defined by user', () => {
  44. it('should use the user defined filter', () => {
  45. const query = migrateVariableQuery(
  46. 'dimension_values(us-east-1,AWS/RDS,CPUUtilization,DBInstanceIdentifier,{"InstanceId":"$instance_id"})'
  47. );
  48. expect(query.queryType).toBe(VariableQueryType.DimensionValues);
  49. expect(query.region).toBe('us-east-1');
  50. expect(query.namespace).toBe('AWS/RDS');
  51. expect(query.metricName).toBe('CPUUtilization');
  52. expect(query.dimensionKey).toBe('DBInstanceIdentifier');
  53. expect(query.dimensionFilters).toStrictEqual({ InstanceId: '$instance_id' });
  54. });
  55. it('should migrate json template variables', () => {
  56. const query = migrateVariableQuery(
  57. 'dimension_values(us-east-1,AWS/RDS,CPUUtilization,DBInstanceIdentifier,{"role":${role:json},"pop":${pop:json}})'
  58. );
  59. expect(query.dimensionFilters).toStrictEqual({ role: '$role', pop: '$pop' });
  60. });
  61. });
  62. });
  63. });
  64. describe('when resource_arns query is used', () => {
  65. it('should parse the query', () => {
  66. const query = migrateVariableQuery(
  67. 'resource_arns(eu-west-1,elasticloadbalancing:loadbalancer,{"elasticbeanstalk:environment-name":["myApp-dev","myApp-prod"]})'
  68. );
  69. expect(query.queryType).toBe(VariableQueryType.ResourceArns);
  70. expect(query.region).toBe('eu-west-1');
  71. expect(query.resourceType).toBe('elasticloadbalancing:loadbalancer');
  72. expect(query.tags).toStrictEqual({ 'elasticbeanstalk:environment-name': ['myApp-dev', 'myApp-prod'] });
  73. });
  74. it('should migrate json template variables', () => {
  75. const query = migrateVariableQuery(
  76. 'resource_arns(eu-west-1,elasticloadbalancing:loadbalancer,{"elasticbeanstalk:environment-name":[${jsonVar:json},"test-$singleVar"]})'
  77. );
  78. expect(query.tags).toStrictEqual({ 'elasticbeanstalk:environment-name': ['$jsonVar', 'test-$singleVar'] });
  79. });
  80. it('should parse a empty array for tags', () => {
  81. const query = migrateVariableQuery('resource_arns(eu-west-1,elasticloadbalancing:loadbalancer, [])');
  82. expect(query.tags).toStrictEqual({});
  83. });
  84. });
  85. describe('when ec2_instance_attribute query is used', () => {
  86. it('should parse the query', () => {
  87. const query = migrateVariableQuery('ec2_instance_attribute(us-east-1,rds:db,{"environment":["$environment"]})');
  88. expect(query.queryType).toBe(VariableQueryType.EC2InstanceAttributes);
  89. expect(query.region).toBe('us-east-1');
  90. expect(query.attributeName).toBe('rds:db');
  91. expect(query.ec2Filters).toStrictEqual({ environment: ['$environment'] });
  92. });
  93. it('should migrate json template variables', () => {
  94. const query = migrateVariableQuery('ec2_instance_attribute(us-east-1,rds:db,{"environment":${env:json}})');
  95. expect(query.ec2Filters).toStrictEqual({ environment: ['$env'] });
  96. });
  97. it('should parse an empty array for filters', () => {
  98. const query = migrateVariableQuery('ec2_instance_attribute(us-east-1,rds:db,[])');
  99. expect(query.ec2Filters).toStrictEqual({});
  100. });
  101. });
  102. describe('when OldVariableQuery is used', () => {
  103. const baseOldQuery: OldVariableQuery = {
  104. queryType: VariableQueryType.Regions,
  105. namespace: '',
  106. region: 'us-east-1',
  107. metricName: '',
  108. dimensionKey: '',
  109. dimensionFilters: '',
  110. ec2Filters: '',
  111. instanceID: '',
  112. attributeName: '',
  113. resourceType: '',
  114. tags: '',
  115. refId: '',
  116. };
  117. it('should parse ec2 query', () => {
  118. const oldQuery: OldVariableQuery = {
  119. ...baseOldQuery,
  120. queryType: VariableQueryType.EC2InstanceAttributes,
  121. ec2Filters: '{"environment":["$environment"]}',
  122. attributeName: 'rds:db',
  123. };
  124. const query = migrateVariableQuery(oldQuery);
  125. expect(query.region).toBe('us-east-1');
  126. expect(query.attributeName).toBe('rds:db');
  127. expect(query.ec2Filters).toStrictEqual({ environment: ['$environment'] });
  128. });
  129. it('should parse resource arn query', () => {
  130. const oldQuery: OldVariableQuery = {
  131. ...baseOldQuery,
  132. queryType: VariableQueryType.ResourceArns,
  133. resourceType: 'elasticloadbalancing:loadbalancer',
  134. tags: '{"elasticbeanstalk:environment-name":["myApp-dev","myApp-prod"]}',
  135. };
  136. const query = migrateVariableQuery(oldQuery);
  137. expect(query.region).toBe('us-east-1');
  138. expect(query.resourceType).toBe('elasticloadbalancing:loadbalancer');
  139. expect(query.tags).toStrictEqual({ 'elasticbeanstalk:environment-name': ['myApp-dev', 'myApp-prod'] });
  140. });
  141. it('should parse dimension values query', () => {
  142. const oldQuery: OldVariableQuery = {
  143. ...baseOldQuery,
  144. queryType: VariableQueryType.DimensionValues,
  145. metricName: 'foo',
  146. dimensionKey: 'bar',
  147. dimensionFilters: '{"InstanceId":"$instanceid"}',
  148. };
  149. const query = migrateVariableQuery(oldQuery);
  150. expect(query.metricName).toBe('foo');
  151. expect(query.dimensionKey).toBe('bar');
  152. expect(query.dimensionFilters).toStrictEqual({ InstanceId: '$instanceid' });
  153. });
  154. });
  155. });