receiver_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package sqlqueryreceiver
  4. import (
  5. "context"
  6. "database/sql"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/require"
  10. "go.opentelemetry.io/collector/component/componenttest"
  11. "go.opentelemetry.io/collector/consumer/consumertest"
  12. "go.opentelemetry.io/collector/receiver/receivertest"
  13. "go.opentelemetry.io/collector/receiver/scraperhelper"
  14. "go.uber.org/zap"
  15. )
  16. func TestCreateLogsReceiver(t *testing.T) {
  17. createReceiver := createLogsReceiverFunc(fakeDBConnect, mkFakeClient)
  18. ctx := context.Background()
  19. receiver, err := createReceiver(
  20. ctx,
  21. receivertest.NewNopCreateSettings(),
  22. &Config{
  23. ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
  24. CollectionInterval: 10 * time.Second,
  25. },
  26. Driver: "mydriver",
  27. DataSource: "my-datasource",
  28. Queries: []Query{{
  29. SQL: "select * from foo",
  30. Logs: []LogsCfg{
  31. {},
  32. },
  33. }},
  34. },
  35. consumertest.NewNop(),
  36. )
  37. require.NoError(t, err)
  38. err = receiver.Start(ctx, componenttest.NewNopHost())
  39. require.NoError(t, err)
  40. }
  41. func TestCreateMetricsReceiver(t *testing.T) {
  42. createReceiver := createMetricsReceiverFunc(fakeDBConnect, mkFakeClient)
  43. ctx := context.Background()
  44. receiver, err := createReceiver(
  45. ctx,
  46. receivertest.NewNopCreateSettings(),
  47. &Config{
  48. ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
  49. CollectionInterval: 10 * time.Second,
  50. InitialDelay: time.Second,
  51. },
  52. Driver: "mydriver",
  53. DataSource: "my-datasource",
  54. Queries: []Query{{
  55. SQL: "select * from foo",
  56. Metrics: []MetricCfg{{
  57. MetricName: "my-metric",
  58. ValueColumn: "my-column",
  59. }},
  60. }},
  61. },
  62. consumertest.NewNop(),
  63. )
  64. require.NoError(t, err)
  65. err = receiver.Start(ctx, componenttest.NewNopHost())
  66. require.NoError(t, err)
  67. }
  68. func fakeDBConnect(string, string) (*sql.DB, error) {
  69. return nil, nil
  70. }
  71. func mkFakeClient(db, string, *zap.Logger) dbClient {
  72. return &fakeDBClient{stringMaps: [][]stringMap{{{"foo": "111"}}}}
  73. }