scraper_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package haproxyreceiver
  4. import (
  5. "context"
  6. "fmt"
  7. "net"
  8. "os"
  9. "path/filepath"
  10. "testing"
  11. "github.com/stretchr/testify/require"
  12. "go.opentelemetry.io/collector/receiver/receivertest"
  13. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
  14. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
  15. )
  16. func Test_scraper_readStats(t *testing.T) {
  17. f, err := os.MkdirTemp("", "haproxytest")
  18. require.NoError(t, err)
  19. socketAddr := filepath.Join(f, "testhaproxy.sock")
  20. l, err := net.Listen("unix", socketAddr)
  21. require.NoError(t, err)
  22. defer l.Close()
  23. go func() {
  24. c, err2 := l.Accept()
  25. require.NoError(t, err2)
  26. buf := make([]byte, 512)
  27. nr, err2 := c.Read(buf)
  28. require.NoError(t, err2)
  29. data := string(buf[0:nr])
  30. switch data {
  31. case "show stats\n":
  32. stats, err2 := os.ReadFile(filepath.Join("testdata", "stats.txt"))
  33. require.NoError(t, err2)
  34. _, err2 = c.Write(stats)
  35. require.NoError(t, err2)
  36. default:
  37. require.Fail(t, fmt.Sprintf("invalid message: %v", data))
  38. }
  39. }()
  40. haProxyCfg := newDefaultConfig().(*Config)
  41. haProxyCfg.Endpoint = socketAddr
  42. s := newScraper(haProxyCfg, receivertest.NewNopCreateSettings())
  43. m, err := s.scrape(context.Background())
  44. require.NoError(t, err)
  45. require.NotNil(t, m)
  46. expectedFile := filepath.Join("testdata", "scraper", "expected.yaml")
  47. expectedMetrics, err := golden.ReadMetrics(expectedFile)
  48. require.NoError(t, err)
  49. require.NoError(t, pmetrictest.CompareMetrics(expectedMetrics, m, pmetrictest.IgnoreStartTimestamp(),
  50. pmetrictest.IgnoreTimestamp(), pmetrictest.IgnoreResourceAttributeValue("haproxy.addr"),
  51. pmetrictest.IgnoreResourceMetricsOrder()))
  52. }