integration_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. //go:build integration
  4. // +build integration
  5. package apachereceiver
  6. import (
  7. "context"
  8. "fmt"
  9. "io"
  10. "net/http"
  11. "path/filepath"
  12. "strings"
  13. "testing"
  14. "time"
  15. "github.com/testcontainers/testcontainers-go"
  16. "github.com/testcontainers/testcontainers-go/wait"
  17. "go.opentelemetry.io/collector/component"
  18. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/scraperinttest"
  19. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
  20. )
  21. const apachePort = "80"
  22. func TestIntegration(t *testing.T) {
  23. scraperinttest.NewIntegrationTest(
  24. NewFactory(),
  25. scraperinttest.WithContainerRequest(
  26. testcontainers.ContainerRequest{
  27. Image: "httpd:2.4",
  28. Files: []testcontainers.ContainerFile{{
  29. HostFilePath: filepath.Join("testdata", "integration", "httpd.conf"),
  30. ContainerFilePath: "/usr/local/apache2/conf/httpd.conf",
  31. FileMode: 700,
  32. }},
  33. ExposedPorts: []string{apachePort},
  34. WaitingFor: waitStrategy{},
  35. }),
  36. scraperinttest.WithCustomConfig(
  37. func(t *testing.T, cfg component.Config, ci *scraperinttest.ContainerInfo) {
  38. rCfg := cfg.(*Config)
  39. rCfg.ScraperControllerSettings.CollectionInterval = 100 * time.Millisecond
  40. rCfg.Endpoint = fmt.Sprintf("http://%s:%s/server-status?auto", ci.Host(t), ci.MappedPort(t, apachePort))
  41. }),
  42. scraperinttest.WithCompareOptions(
  43. pmetrictest.IgnoreResourceAttributeValue("apache.server.port"),
  44. pmetrictest.IgnoreMetricValues(),
  45. pmetrictest.IgnoreMetricDataPointsOrder(),
  46. pmetrictest.IgnoreStartTimestamp(),
  47. pmetrictest.IgnoreTimestamp(),
  48. ),
  49. ).Run(t)
  50. }
  51. type waitStrategy struct{}
  52. func (ws waitStrategy) WaitUntilReady(ctx context.Context, st wait.StrategyTarget) error {
  53. if err := wait.ForListeningPort(apachePort).
  54. WithStartupTimeout(time.Minute).
  55. WaitUntilReady(ctx, st); err != nil {
  56. return err
  57. }
  58. hostname, err := st.Host(ctx)
  59. if err != nil {
  60. return err
  61. }
  62. port, err := st.MappedPort(ctx, apachePort)
  63. if err != nil {
  64. return err
  65. }
  66. for {
  67. select {
  68. case <-ctx.Done():
  69. return ctx.Err()
  70. case <-time.After(5 * time.Second):
  71. return fmt.Errorf("server startup problem")
  72. case <-time.After(100 * time.Millisecond):
  73. resp, err := http.Get(fmt.Sprintf("http://%s:%s/server-status?auto", hostname, port.Port()))
  74. if err != nil {
  75. continue
  76. }
  77. body, err := io.ReadAll(resp.Body)
  78. if err != nil {
  79. continue
  80. }
  81. if resp.Body.Close() != nil {
  82. continue
  83. }
  84. // The server needs a moment to generate some stats
  85. if strings.Contains(string(body), "ReqPerSec") {
  86. return nil
  87. }
  88. }
  89. }
  90. }