annotationSupport.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { AnnotationQuery } from '@grafana/data';
  2. import { CloudWatchAnnotationSupport } from './annotationSupport';
  3. import { CloudWatchAnnotationQuery, LegacyAnnotationQuery } from './types';
  4. const metricStatAnnotationQuery: CloudWatchAnnotationQuery = {
  5. queryMode: 'Annotations',
  6. region: 'us-east-2',
  7. namespace: 'AWS/EC2',
  8. period: '300',
  9. metricName: 'CPUUtilization',
  10. dimensions: { InstanceId: 'i-123' },
  11. matchExact: true,
  12. statistic: 'Average',
  13. refId: 'anno',
  14. prefixMatching: false,
  15. actionPrefix: '',
  16. alarmNamePrefix: '',
  17. };
  18. const prefixMatchingAnnotationQuery: CloudWatchAnnotationQuery = {
  19. queryMode: 'Annotations',
  20. region: 'us-east-2',
  21. namespace: '',
  22. period: '300',
  23. metricName: '',
  24. dimensions: undefined,
  25. statistic: 'Average',
  26. refId: 'anno',
  27. prefixMatching: true,
  28. actionPrefix: 'arn',
  29. alarmNamePrefix: 'test-alarm',
  30. };
  31. const annotationQuery: AnnotationQuery<CloudWatchAnnotationQuery> = {
  32. name: 'Anno',
  33. enable: false,
  34. iconColor: '',
  35. target: metricStatAnnotationQuery!,
  36. };
  37. const legacyAnnotationQuery: LegacyAnnotationQuery = {
  38. name: 'Anno',
  39. enable: false,
  40. iconColor: '',
  41. region: '',
  42. namespace: 'AWS/EC2',
  43. period: '300',
  44. metricName: 'CPUUtilization',
  45. dimensions: { InstanceId: 'i-123' },
  46. matchExact: true,
  47. statistic: '',
  48. refId: '',
  49. prefixMatching: false,
  50. actionPrefix: '',
  51. alarmNamePrefix: '',
  52. target: {
  53. limit: 0,
  54. matchAny: false,
  55. tags: [],
  56. type: '',
  57. },
  58. alias: '',
  59. builtIn: 0,
  60. datasource: undefined,
  61. expression: '',
  62. hide: false,
  63. id: '',
  64. type: '',
  65. statistics: [],
  66. };
  67. describe('annotationSupport', () => {
  68. describe('when prepareAnnotation', () => {
  69. describe('is being called with new style annotations', () => {
  70. it('should return the same query without changing it', () => {
  71. const preparedAnnotation = CloudWatchAnnotationSupport.prepareAnnotation(annotationQuery);
  72. expect(preparedAnnotation).toEqual(annotationQuery);
  73. });
  74. });
  75. describe('is being called with legacy annotations', () => {
  76. it('should return a new query', () => {
  77. const preparedAnnotation = CloudWatchAnnotationSupport.prepareAnnotation(legacyAnnotationQuery);
  78. expect(preparedAnnotation).not.toEqual(annotationQuery);
  79. });
  80. it('should set default values if not given', () => {
  81. const preparedAnnotation = CloudWatchAnnotationSupport.prepareAnnotation(legacyAnnotationQuery);
  82. expect(preparedAnnotation.target?.statistic).toEqual('Average');
  83. expect(preparedAnnotation.target?.region).toEqual('default');
  84. expect(preparedAnnotation.target?.queryMode).toEqual('Annotations');
  85. expect(preparedAnnotation.target?.refId).toEqual('annotationQuery');
  86. });
  87. it('should not set default values if given', () => {
  88. const annotation = CloudWatchAnnotationSupport.prepareAnnotation({
  89. ...legacyAnnotationQuery,
  90. statistic: 'Min',
  91. region: 'us-east-2',
  92. queryMode: 'Annotations',
  93. refId: 'A',
  94. });
  95. expect(annotation.target?.statistic).toEqual('Min');
  96. expect(annotation.target?.region).toEqual('us-east-2');
  97. expect(annotation.target?.queryMode).toEqual('Annotations');
  98. expect(annotation.target?.refId).toEqual('A');
  99. });
  100. });
  101. });
  102. describe('when prepareQuery', () => {
  103. describe('is being called without a target', () => {
  104. it('should return undefined', () => {
  105. const preparedQuery = CloudWatchAnnotationSupport.prepareQuery({
  106. ...annotationQuery,
  107. target: undefined,
  108. });
  109. expect(preparedQuery).toBeUndefined();
  110. });
  111. });
  112. describe('is being called with a complete metric stat query', () => {
  113. it('should return the annotation target', () => {
  114. expect(CloudWatchAnnotationSupport.prepareQuery(annotationQuery)).toEqual(annotationQuery.target);
  115. });
  116. });
  117. describe('is being called with an incomplete metric stat query', () => {
  118. it('should return undefined', () => {
  119. const preparedQuery = CloudWatchAnnotationSupport.prepareQuery({
  120. ...annotationQuery,
  121. target: {
  122. ...annotationQuery.target!,
  123. dimensions: {},
  124. metricName: '',
  125. statistic: undefined,
  126. },
  127. });
  128. expect(preparedQuery).toBeUndefined();
  129. });
  130. });
  131. describe('is being called with an incomplete prefix matching query', () => {
  132. it('should return the annotation target', () => {
  133. const query = {
  134. ...annotationQuery,
  135. target: prefixMatchingAnnotationQuery,
  136. };
  137. expect(CloudWatchAnnotationSupport.prepareQuery(query)).toEqual(query.target);
  138. });
  139. });
  140. describe('is being called with an incomplete prefix matching query', () => {
  141. it('should return undefined', () => {
  142. const query = {
  143. ...annotationQuery,
  144. target: {
  145. ...prefixMatchingAnnotationQuery,
  146. actionPrefix: '',
  147. },
  148. };
  149. expect(CloudWatchAnnotationSupport.prepareQuery(query)).toBeUndefined();
  150. });
  151. });
  152. });
  153. });