redis_scraper_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package redisreceiver
  4. import (
  5. "context"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. "go.opentelemetry.io/collector/config/configtls"
  10. "go.opentelemetry.io/collector/receiver/receivertest"
  11. "go.uber.org/zap"
  12. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/redisreceiver/internal/metadata"
  13. )
  14. func TestRedisRunnable(t *testing.T) {
  15. logger, _ := zap.NewDevelopment()
  16. settings := receivertest.NewNopCreateSettings()
  17. settings.Logger = logger
  18. cfg := createDefaultConfig().(*Config)
  19. cfg.Endpoint = "localhost:6379"
  20. rs := &redisScraper{mb: metadata.NewMetricsBuilder(cfg.MetricsBuilderConfig, settings)}
  21. runner, err := newRedisScraperWithClient(newFakeClient(), settings, cfg)
  22. require.NoError(t, err)
  23. md, err := runner.Scrape(context.Background())
  24. require.NoError(t, err)
  25. // + 6 because there are two keyspace entries each of which has three metrics
  26. // -1 because maxmemory is by default disabled, so recorder is there, but there won't be data point
  27. assert.Equal(t, len(rs.dataPointRecorders())+6-1, md.DataPointCount())
  28. rm := md.ResourceMetrics().At(0)
  29. ilm := rm.ScopeMetrics().At(0)
  30. il := ilm.Scope()
  31. assert.Equal(t, "otelcol/redisreceiver", il.Name())
  32. }
  33. func TestNewReceiver_invalid_endpoint(t *testing.T) {
  34. c := createDefaultConfig().(*Config)
  35. _, err := createMetricsReceiver(context.Background(), receivertest.NewNopCreateSettings(), c, nil)
  36. assert.Error(t, err)
  37. assert.Contains(t, err.Error(), "invalid endpoint")
  38. }
  39. func TestNewReceiver_invalid_auth_error(t *testing.T) {
  40. c := createDefaultConfig().(*Config)
  41. c.TLS = configtls.TLSClientSetting{
  42. TLSSetting: configtls.TLSSetting{
  43. CAFile: "/invalid",
  44. },
  45. }
  46. r, err := createMetricsReceiver(context.Background(), receivertest.NewNopCreateSettings(), c, nil)
  47. assert.Error(t, err)
  48. assert.Contains(t, err.Error(), "failed to load TLS config")
  49. assert.Nil(t, r)
  50. }