config_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package awscloudwatchmetricsreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awscloudwatchmetricsreceiver"
  4. import (
  5. "errors"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func TestValidate(t *testing.T) {
  11. cases := []struct {
  12. name string
  13. config Config
  14. expectedErr error
  15. }{
  16. {
  17. name: "Valid config",
  18. config: Config{
  19. Region: "eu-west-1",
  20. PollInterval: time.Minute * 5,
  21. Metrics: &MetricsConfig{
  22. Names: []*NamedConfig{{
  23. Namespace: "AWS/EC2",
  24. MetricName: "CPUUtilizaition",
  25. Period: time.Second * 60,
  26. AwsAggregation: "Sum",
  27. Dimensions: []MetricDimensionsConfig{{
  28. Name: "InstanceId",
  29. Value: " i-1234567890abcdef0",
  30. }},
  31. }},
  32. },
  33. },
  34. },
  35. {
  36. name: "No metrics defined",
  37. config: Config{
  38. Region: "eu-west-1",
  39. PollInterval: time.Minute * 5,
  40. },
  41. expectedErr: errNoMetricsConfigured,
  42. },
  43. {
  44. name: "No named config defined",
  45. config: Config{
  46. Region: "eu-west-1",
  47. PollInterval: time.Minute * 5,
  48. Metrics: &MetricsConfig{},
  49. },
  50. expectedErr: errNoMetricsConfigured,
  51. },
  52. {
  53. name: "No namespace defined",
  54. config: Config{
  55. Region: "eu-west-1",
  56. PollInterval: time.Minute * 5,
  57. Metrics: &MetricsConfig{
  58. Names: []*NamedConfig{{
  59. Namespace: "",
  60. }},
  61. },
  62. },
  63. expectedErr: errNoNamespaceConfigured,
  64. },
  65. {
  66. name: "No metric name defined",
  67. config: Config{
  68. Region: "eu-west-1",
  69. PollInterval: time.Minute * 5,
  70. Metrics: &MetricsConfig{
  71. Names: []*NamedConfig{{
  72. Namespace: "AWS/EC2",
  73. MetricName: "",
  74. AwsAggregation: "Sum",
  75. }},
  76. },
  77. },
  78. expectedErr: errNoMetricsConfigured,
  79. },
  80. {
  81. name: "Bad AWS Aggregation",
  82. config: Config{
  83. Region: "eu-west-1",
  84. PollInterval: time.Minute * 5,
  85. Metrics: &MetricsConfig{
  86. Names: []*NamedConfig{{
  87. Namespace: "AWS/EC2",
  88. MetricName: "CPUUtilizaition",
  89. Period: time.Second * 60,
  90. AwsAggregation: "Last",
  91. }},
  92. },
  93. },
  94. expectedErr: errInvalidAwsAggregation,
  95. },
  96. {
  97. name: "P99 AWS Aggregation",
  98. config: Config{
  99. Region: "eu-west-1",
  100. PollInterval: time.Minute * 5,
  101. Metrics: &MetricsConfig{
  102. Names: []*NamedConfig{{
  103. Namespace: "AWS/EC2",
  104. MetricName: "CPUUtilizaition",
  105. Period: time.Second * 60,
  106. AwsAggregation: "p99",
  107. }},
  108. },
  109. },
  110. },
  111. {
  112. name: "TS99 AWS Aggregation",
  113. config: Config{
  114. Region: "eu-west-1",
  115. PollInterval: time.Minute * 5,
  116. Metrics: &MetricsConfig{
  117. Names: []*NamedConfig{{
  118. Namespace: "AWS/EC2",
  119. MetricName: "CPUUtilizaition",
  120. Period: time.Second * 60,
  121. AwsAggregation: "TS99",
  122. }},
  123. },
  124. },
  125. },
  126. {
  127. name: "Multiple Metrics",
  128. config: Config{
  129. Region: "eu-west-1",
  130. PollInterval: time.Minute * 5,
  131. Metrics: &MetricsConfig{
  132. Names: []*NamedConfig{{
  133. Namespace: "AWS/EC2",
  134. MetricName: "CPUUtilizaition",
  135. Period: time.Second * 60,
  136. AwsAggregation: "TS99",
  137. },
  138. {
  139. Namespace: "AWS/EC2",
  140. MetricName: "CPUUtilizaition",
  141. Period: time.Second * 60,
  142. AwsAggregation: "TS99"},
  143. },
  144. },
  145. },
  146. },
  147. {
  148. name: "Invalid region",
  149. config: Config{
  150. Region: "",
  151. },
  152. expectedErr: errNoRegion,
  153. },
  154. {
  155. name: "Invalid IMDS endpoint url",
  156. config: Config{
  157. Region: "eu-west-1",
  158. IMDSEndpoint: "xyz",
  159. },
  160. expectedErr: errors.New("unable to parse URI for imds_endpoint"),
  161. },
  162. {
  163. name: "Polling Interval < 1s",
  164. config: Config{
  165. Region: "eu-west-1",
  166. PollInterval: time.Millisecond * 500,
  167. },
  168. expectedErr: errInvalidPollInterval,
  169. },
  170. {
  171. name: "Invalid dimensions name and value",
  172. config: Config{
  173. Region: "eu-west-1",
  174. PollInterval: time.Minute * 5,
  175. Metrics: &MetricsConfig{
  176. Names: []*NamedConfig{{
  177. Namespace: "AWS/EC2",
  178. MetricName: "CPUUtilizaition",
  179. Period: time.Second * 60,
  180. AwsAggregation: "Sum",
  181. Dimensions: []MetricDimensionsConfig{{
  182. Name: "",
  183. Value: "",
  184. }},
  185. }},
  186. },
  187. },
  188. expectedErr: errEmptyDimensions,
  189. },
  190. {
  191. name: "Dimension name begins with colon",
  192. config: Config{
  193. Region: "eu-west-1",
  194. PollInterval: time.Minute * 5,
  195. Metrics: &MetricsConfig{
  196. Names: []*NamedConfig{{
  197. Namespace: "AWS/EC2",
  198. MetricName: "CPUUtilizaition",
  199. Period: time.Second * 60,
  200. AwsAggregation: "Sum",
  201. Dimensions: []MetricDimensionsConfig{{
  202. Name: ":BucketName",
  203. Value: "open-telemetry",
  204. }},
  205. }},
  206. },
  207. },
  208. expectedErr: errDimensionColonPrefix,
  209. },
  210. }
  211. for _, tc := range cases {
  212. t.Run(tc.name, func(t *testing.T) {
  213. err := tc.config.Validate()
  214. if tc.expectedErr != nil {
  215. require.ErrorContains(t, err, tc.expectedErr.Error())
  216. } else {
  217. require.NoError(t, err)
  218. }
  219. })
  220. }
  221. }