config_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package awscloudwatchreceiver
  4. import (
  5. "errors"
  6. "path/filepath"
  7. "testing"
  8. "time"
  9. "github.com/aws/aws-sdk-go/aws"
  10. "github.com/stretchr/testify/require"
  11. "go.opentelemetry.io/collector/component"
  12. "go.opentelemetry.io/collector/confmap/confmaptest"
  13. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awscloudwatchreceiver/internal/metadata"
  14. )
  15. func TestValidate(t *testing.T) {
  16. cases := []struct {
  17. name string
  18. config Config
  19. expectedErr error
  20. }{
  21. {
  22. name: "Valid Config",
  23. config: Config{
  24. Region: "us-west-2",
  25. Logs: &LogsConfig{
  26. MaxEventsPerRequest: defaultEventLimit,
  27. PollInterval: defaultPollInterval,
  28. Groups: GroupConfig{
  29. AutodiscoverConfig: nil,
  30. },
  31. },
  32. },
  33. },
  34. {
  35. name: "Invalid No Region",
  36. config: Config{
  37. Region: "",
  38. },
  39. expectedErr: errNoRegion,
  40. },
  41. {
  42. name: "Nil Logs",
  43. config: Config{
  44. Region: "us-west-2",
  45. Logs: nil,
  46. },
  47. expectedErr: errNoLogsConfigured,
  48. },
  49. {
  50. name: "Invalid Event Limit",
  51. config: Config{
  52. Region: "us-west-2",
  53. Logs: &LogsConfig{
  54. MaxEventsPerRequest: -1,
  55. PollInterval: defaultPollInterval,
  56. },
  57. },
  58. expectedErr: errInvalidEventLimit,
  59. },
  60. {
  61. name: "Invalid Poll Interval",
  62. config: Config{
  63. Region: "us-west-2",
  64. Logs: &LogsConfig{
  65. MaxEventsPerRequest: defaultEventLimit,
  66. PollInterval: 100 * time.Millisecond,
  67. Groups: GroupConfig{
  68. AutodiscoverConfig: nil,
  69. },
  70. },
  71. },
  72. expectedErr: errInvalidPollInterval,
  73. },
  74. {
  75. name: "Invalid Log Group Limit",
  76. config: Config{
  77. Region: "us-east-1",
  78. Logs: &LogsConfig{
  79. MaxEventsPerRequest: defaultEventLimit,
  80. PollInterval: defaultPollInterval,
  81. Groups: GroupConfig{
  82. AutodiscoverConfig: &AutodiscoverConfig{
  83. Limit: -10000,
  84. },
  85. }},
  86. },
  87. expectedErr: errInvalidAutodiscoverLimit,
  88. },
  89. {
  90. name: "Invalid IMDS Endpoint",
  91. config: Config{
  92. Region: "us-east-1",
  93. IMDSEndpoint: "xyz",
  94. Logs: &LogsConfig{
  95. MaxEventsPerRequest: defaultEventLimit,
  96. PollInterval: defaultPollInterval,
  97. Groups: GroupConfig{
  98. AutodiscoverConfig: nil,
  99. },
  100. },
  101. },
  102. expectedErr: errors.New("unable to parse URI for imds_endpoint"),
  103. },
  104. {
  105. name: "Both Logs Autodiscover and Named Set",
  106. config: Config{
  107. Region: "us-east-1",
  108. Logs: &LogsConfig{
  109. MaxEventsPerRequest: defaultEventLimit,
  110. PollInterval: defaultPollInterval,
  111. Groups: GroupConfig{
  112. AutodiscoverConfig: &AutodiscoverConfig{
  113. Limit: defaultEventLimit,
  114. },
  115. NamedConfigs: map[string]StreamConfig{
  116. "some-log-group": {
  117. Names: []*string{aws.String("some-lg-name")},
  118. },
  119. },
  120. },
  121. },
  122. },
  123. expectedErr: errAutodiscoverAndNamedConfigured,
  124. },
  125. }
  126. for _, tc := range cases {
  127. t.Run(tc.name, func(t *testing.T) {
  128. err := tc.config.Validate()
  129. if tc.expectedErr != nil {
  130. require.ErrorContains(t, err, tc.expectedErr.Error())
  131. } else {
  132. require.NoError(t, err)
  133. }
  134. })
  135. }
  136. }
  137. func TestLoadConfig(t *testing.T) {
  138. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  139. require.NoError(t, err)
  140. cases := []struct {
  141. name string
  142. expectedConfig component.Config
  143. }{
  144. {
  145. name: "default",
  146. expectedConfig: &Config{
  147. Region: "us-west-1",
  148. Logs: &LogsConfig{
  149. PollInterval: time.Minute,
  150. MaxEventsPerRequest: defaultEventLimit,
  151. Groups: GroupConfig{
  152. AutodiscoverConfig: &AutodiscoverConfig{
  153. Limit: defaultLogGroupLimit,
  154. },
  155. },
  156. },
  157. },
  158. },
  159. {
  160. name: "prefix-log-group-autodiscover",
  161. expectedConfig: &Config{
  162. Region: "us-west-1",
  163. Logs: &LogsConfig{
  164. PollInterval: time.Minute,
  165. MaxEventsPerRequest: defaultEventLimit,
  166. Groups: GroupConfig{
  167. AutodiscoverConfig: &AutodiscoverConfig{
  168. Limit: 100,
  169. Prefix: "/aws/eks/",
  170. },
  171. },
  172. },
  173. },
  174. },
  175. {
  176. name: "autodiscover-filter-streams",
  177. expectedConfig: &Config{
  178. Region: "us-west-1",
  179. Logs: &LogsConfig{
  180. PollInterval: time.Minute,
  181. MaxEventsPerRequest: defaultEventLimit,
  182. Groups: GroupConfig{
  183. AutodiscoverConfig: &AutodiscoverConfig{
  184. Limit: 100,
  185. Streams: StreamConfig{
  186. Prefixes: []*string{aws.String("kube-api-controller")},
  187. },
  188. },
  189. },
  190. },
  191. },
  192. },
  193. {
  194. name: "autodiscover-filter-streams",
  195. expectedConfig: &Config{
  196. Region: "us-west-1",
  197. Logs: &LogsConfig{
  198. PollInterval: time.Minute,
  199. MaxEventsPerRequest: defaultEventLimit,
  200. Groups: GroupConfig{
  201. AutodiscoverConfig: &AutodiscoverConfig{
  202. Limit: 100,
  203. Streams: StreamConfig{
  204. Prefixes: []*string{aws.String("kube-api-controller")},
  205. },
  206. },
  207. },
  208. },
  209. },
  210. },
  211. {
  212. name: "named-prefix",
  213. expectedConfig: &Config{
  214. Profile: "my-profile",
  215. Region: "us-west-1",
  216. Logs: &LogsConfig{
  217. PollInterval: 5 * time.Minute,
  218. MaxEventsPerRequest: defaultEventLimit,
  219. Groups: GroupConfig{
  220. NamedConfigs: map[string]StreamConfig{
  221. "/aws/eks/dev-0/cluster": {},
  222. },
  223. },
  224. },
  225. },
  226. },
  227. {
  228. name: "named-prefix-with-streams",
  229. expectedConfig: &Config{
  230. Profile: "my-profile",
  231. Region: "us-west-1",
  232. Logs: &LogsConfig{
  233. PollInterval: 5 * time.Minute,
  234. MaxEventsPerRequest: defaultEventLimit,
  235. Groups: GroupConfig{
  236. NamedConfigs: map[string]StreamConfig{
  237. "/aws/eks/dev-0/cluster": {
  238. Names: []*string{aws.String("kube-apiserver-ea9c831555adca1815ae04b87661klasdj")},
  239. },
  240. },
  241. },
  242. },
  243. },
  244. },
  245. }
  246. for _, tc := range cases {
  247. t.Run(tc.name, func(t *testing.T) {
  248. factory := NewFactory()
  249. cfg := factory.CreateDefaultConfig()
  250. loaded, err := cm.Sub(component.NewIDWithName(metadata.Type, tc.name).String())
  251. require.NoError(t, err)
  252. require.NoError(t, component.UnmarshalConfig(loaded, cfg))
  253. require.Equal(t, cfg, tc.expectedConfig)
  254. require.NoError(t, component.ValidateConfig(cfg))
  255. })
  256. }
  257. }