factory_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package apachereceiver
  4. import (
  5. "context"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/require"
  9. "go.opentelemetry.io/collector/component"
  10. "go.opentelemetry.io/collector/consumer/consumertest"
  11. "go.opentelemetry.io/collector/receiver/receivertest"
  12. "go.opentelemetry.io/collector/receiver/scraperhelper"
  13. )
  14. func TestType(t *testing.T) {
  15. factory := NewFactory()
  16. ft := factory.Type()
  17. require.EqualValues(t, "apache", ft)
  18. }
  19. func TestValidConfig(t *testing.T) {
  20. factory := NewFactory()
  21. require.NoError(t, component.ValidateConfig(factory.CreateDefaultConfig()))
  22. }
  23. func TestCreateMetricsReceiver(t *testing.T) {
  24. factory := NewFactory()
  25. metricsReceiver, err := factory.CreateMetricsReceiver(
  26. context.Background(),
  27. receivertest.NewNopCreateSettings(),
  28. &Config{
  29. ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
  30. CollectionInterval: 10 * time.Second,
  31. },
  32. },
  33. consumertest.NewNop(),
  34. )
  35. require.NoError(t, err)
  36. require.NotNil(t, metricsReceiver)
  37. }
  38. func TestPortValidate(t *testing.T) {
  39. testCases := []struct {
  40. desc string
  41. endpoint string
  42. expectedPort string
  43. }{
  44. {
  45. desc: "http with specified port",
  46. endpoint: "http://localhost:8080/server-status?auto",
  47. expectedPort: "8080",
  48. },
  49. {
  50. desc: "http without specified port",
  51. endpoint: "http://localhost/server-status?auto",
  52. expectedPort: "80",
  53. },
  54. {
  55. desc: "https with specified port",
  56. endpoint: "https://localhost:8080/server-status?auto",
  57. expectedPort: "8080",
  58. },
  59. {
  60. desc: "https without specified port",
  61. endpoint: "https://localhost/server-status?auto",
  62. expectedPort: "443",
  63. },
  64. {
  65. desc: "unknown protocol with specified port",
  66. endpoint: "abc://localhost:8080/server-status?auto",
  67. expectedPort: "8080",
  68. },
  69. {
  70. desc: "port unresolvable",
  71. endpoint: "abc://localhost/server-status?auto",
  72. expectedPort: "",
  73. },
  74. }
  75. for _, tc := range testCases {
  76. t.Run(tc.desc, func(t *testing.T) {
  77. cfg := NewFactory().CreateDefaultConfig().(*Config)
  78. cfg.Endpoint = tc.endpoint
  79. _, port, err := parseResourceAttributes(tc.endpoint)
  80. require.NoError(t, err)
  81. require.Equal(t, tc.expectedPort, port)
  82. })
  83. }
  84. }