metricQueryMigrations.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { config } from '@grafana/runtime';
  2. import { CloudWatchMetricsQuery } from '../types';
  3. import { migrateAliasPatterns } from './metricQueryMigrations';
  4. describe('metricQueryMigrations', () => {
  5. interface TestScenario {
  6. description?: string;
  7. alias: string;
  8. label?: string;
  9. }
  10. describe('migrateAliasPatterns', () => {
  11. const baseQuery: CloudWatchMetricsQuery = {
  12. statistic: 'Average',
  13. refId: 'A',
  14. id: '',
  15. region: 'us-east-2',
  16. namespace: 'AWS/EC2',
  17. period: '300',
  18. alias: '',
  19. metricName: 'CPUUtilization',
  20. dimensions: {},
  21. matchExact: false,
  22. expression: '',
  23. };
  24. describe('when feature is enabled', () => {
  25. describe('and label was not previously migrated', () => {
  26. const cases: TestScenario[] = [
  27. { description: 'Metric name', alias: '{{metric}}', label: "${PROP('MetricName')}" },
  28. { description: 'Trim pattern', alias: '{{ metric }}', label: "${PROP('MetricName')}" },
  29. { description: 'Namespace', alias: '{{namespace}}', label: "${PROP('Namespace')}" },
  30. { description: 'Period', alias: '{{period}}', label: "${PROP('Period')}" },
  31. { description: 'Region', alias: '{{region}}', label: "${PROP('Region')}" },
  32. { description: 'Statistic', alias: '{{stat}}', label: "${PROP('Stat')}" },
  33. { description: 'Label', alias: '{{label}}', label: '${LABEL}' },
  34. {
  35. description: 'Non-existing alias pattern',
  36. alias: '{{anything_else}}',
  37. label: "${PROP('Dim.anything_else')}",
  38. },
  39. {
  40. description: 'Combined patterns',
  41. alias: 'some {{combination}} of {{label}} and {{metric}}',
  42. label: "some ${PROP('Dim.combination')} of ${LABEL} and ${PROP('MetricName')}",
  43. },
  44. {
  45. description: 'Combined patterns not trimmed',
  46. alias: 'some {{combination }}{{ label}} and {{metric}}',
  47. label: "some ${PROP('Dim.combination')}${LABEL} and ${PROP('MetricName')}",
  48. },
  49. ];
  50. test.each(cases)('given old alias %p, it should be migrated to label: %p', ({ alias, label }) => {
  51. config.featureToggles.cloudWatchDynamicLabels = true;
  52. const testQuery = { ...baseQuery, alias };
  53. const result = migrateAliasPatterns(testQuery);
  54. expect(result.label).toBe(label);
  55. expect(result.alias).toBe(alias);
  56. });
  57. });
  58. describe('and label was previously migrated', () => {
  59. const cases: TestScenario[] = [
  60. {
  61. alias: '',
  62. label: "${PROP('MetricName')}",
  63. },
  64. { alias: '{{metric}}', label: "${PROP('Period')}" },
  65. { alias: '{{namespace}}', label: '' },
  66. ];
  67. test.each(cases)('it should not be migrated once again', ({ alias, label }) => {
  68. config.featureToggles.cloudWatchDynamicLabels = true;
  69. const testQuery = { ...baseQuery, alias, label };
  70. const result = migrateAliasPatterns(testQuery);
  71. expect(result.label).toBe(label);
  72. expect(result.alias).toBe(alias);
  73. });
  74. });
  75. });
  76. describe('when feature is disabled', () => {
  77. const cases: TestScenario[] = [
  78. { alias: '{{period}}', label: "${PROP('MetricName')}" },
  79. { alias: '{{metric}}', label: '' },
  80. { alias: '', label: "${PROP('MetricName')}" },
  81. { alias: '', label: undefined },
  82. ];
  83. test.each(cases)('should not migrate alias', ({ alias, label }) => {
  84. config.featureToggles.cloudWatchDynamicLabels = false;
  85. const testQuery = { ...baseQuery, alias: `${alias}` };
  86. if (label !== undefined) {
  87. testQuery.label = label;
  88. }
  89. const result = migrateAliasPatterns(testQuery);
  90. expect(result.label).toBe(label);
  91. expect(result.alias).toBe(alias);
  92. });
  93. });
  94. });
  95. });