config_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package flinkmetricsreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/flinkmetricsreceiver"
  4. import (
  5. "errors"
  6. "fmt"
  7. "path/filepath"
  8. "testing"
  9. "time"
  10. "github.com/stretchr/testify/require"
  11. "go.opentelemetry.io/collector/component"
  12. "go.opentelemetry.io/collector/config/confighttp"
  13. "go.opentelemetry.io/collector/confmap/confmaptest"
  14. "go.opentelemetry.io/collector/receiver/scraperhelper"
  15. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/flinkmetricsreceiver/internal/metadata"
  16. )
  17. func TestValidate(t *testing.T) {
  18. testCases := []struct {
  19. desc string
  20. cfg *Config
  21. expectedErr error
  22. }{
  23. {
  24. desc: "invalid endpoint",
  25. cfg: &Config{
  26. HTTPClientSettings: confighttp.HTTPClientSettings{
  27. Endpoint: "invalid://endpoint: 12efg",
  28. },
  29. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  30. },
  31. expectedErr: fmt.Errorf("\"endpoint\" must be in the form of <scheme>://<hostname>:<port>: %w", errors.New(`parse "invalid://endpoint: 12efg": invalid port ": 12efg" after host`)),
  32. },
  33. {
  34. desc: "valid config",
  35. cfg: &Config{
  36. HTTPClientSettings: confighttp.HTTPClientSettings{
  37. Endpoint: defaultEndpoint,
  38. },
  39. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  40. },
  41. expectedErr: nil,
  42. },
  43. }
  44. for _, tc := range testCases {
  45. t.Run(tc.desc, func(t *testing.T) {
  46. actualErr := tc.cfg.Validate()
  47. if tc.expectedErr != nil {
  48. require.EqualError(t, actualErr, tc.expectedErr.Error())
  49. } else {
  50. require.NoError(t, actualErr)
  51. }
  52. })
  53. }
  54. }
  55. func TestLoadConfig(t *testing.T) {
  56. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  57. require.NoError(t, err)
  58. factory := NewFactory()
  59. cfg := factory.CreateDefaultConfig()
  60. sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "").String())
  61. require.NoError(t, err)
  62. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  63. expected := factory.CreateDefaultConfig().(*Config)
  64. expected.Endpoint = "http://localhost:8081"
  65. expected.CollectionInterval = 10 * time.Second
  66. require.Equal(t, expected, cfg)
  67. }