Skip to content

Commit

Permalink
Use golden files with haproxy test
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme committed Mar 25, 2024
1 parent 7e1f0cc commit 3647d72
Show file tree
Hide file tree
Showing 5 changed files with 8,577 additions and 99 deletions.
3 changes: 3 additions & 0 deletions tests/go.mod
Expand Up @@ -8,6 +8,8 @@ require (
github.com/go-sql-driver/mysql v1.8.0
github.com/google/uuid v1.6.0
github.com/knadh/koanf v1.5.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden v0.96.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.96.0
github.com/shirou/gopsutil/v3 v3.24.2
github.com/stretchr/testify v1.9.0
github.com/testcontainers/testcontainers-go v0.20.1
Expand Down Expand Up @@ -70,6 +72,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/mostynb/go-grpc-compression v1.2.2 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.96.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc5 // indirect
github.com/opencontainers/runc v1.1.12 // indirect
Expand Down
6 changes: 6 additions & 0 deletions tests/go.sum
Expand Up @@ -922,6 +922,12 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/npillmayer/nestext v0.1.3/go.mod h1:h2lrijH8jpicr25dFY+oAJLyzlya6jhnuG+zWp9L0Uk=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden v0.96.0 h1:6xhEYeFRjui33hCCP8tD9B2R2VCGNdNrzi+pivp0osk=
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden v0.96.0/go.mod h1:7CDMyrWBi/iST+UVvheDNjZX8VWyboTJqkXHz7gpLDI=
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.96.0 h1:iynLFjnG869r53AIhiavbEVMZoPqCba7Mijm+9MRdOo=
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.96.0/go.mod h1:FuTdjIZj7Un07dcbJs06IF1DJiYfpQkc4oklhNWE8fg=
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.96.0 h1:nzAR1IjPcbgLNFmJElLPyRlLOfijAkQcWo4L9CXixu4=
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.96.0/go.mod h1:Zn0A4V5t3uNr2FYsgnzT4t0OBqdOk8jcPjgHgy3jHG0=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.0.2 h1:9yCKha/T5XdGtO0q9Q9a6T5NUCsTn/DrBg0D7ufOcFM=
Expand Down
117 changes: 115 additions & 2 deletions tests/receivers/smartagent/haproxy/haproxy_test.go
Expand Up @@ -17,13 +17,126 @@
package tests

import (
"context"
"fmt"
"path/filepath"
"runtime"
"testing"
"time"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver/otlpreceiver"
"go.opentelemetry.io/collector/receiver/receivertest"
"go.uber.org/zap"

"github.com/signalfx/splunk-otel-collector/tests/testutils"
)

func TestHaproxyReceiverProvidesAllMetrics(t *testing.T) {
testutils.AssertAllMetricsReceived(
t, "all.yaml", "all_metrics_config.yaml", nil, nil,
checkGoldenFile(t, "all_metrics_config.yaml", "all_expected.yaml",
pmetrictest.IgnoreMetricDataPointsOrder(),
pmetrictest.IgnoreTimestamp(),
pmetrictest.IgnoreStartTimestamp(),
pmetrictest.IgnoreMetricAttributeValue("unique_proxy_id",
"haproxy_bytes_out",
"haproxy_compress_bypass",
"haproxy_compress_in",
"haproxy_compress_out",
"haproxy_compress_responses",
"haproxy_denied_request",
"haproxy_denied_response",
"haproxy_intercepted_requests",
"haproxy_connection_rate",
"haproxy_connection_rate_max",
"haproxy_connection_total",
"haproxy_request_rate_max",
"haproxy_error_connections",
"haproxy_retries",
"haproxy_bytes_in",
"haproxy_bytes_out",
"haproxy_compress_bypass",
"haproxy_active_servers",
"haproxy_backup_servers",
"haproxy_client_aborts",
"haproxy_redispatched",
"haproxy_request_total",
"haproxy_response_1xx",
"haproxy_response_2xx",
"haproxy_response_3xx",
"haproxy_response_4xx",
"haproxy_response_5xx",
"haproxy_response_other",
"haproxy_response_time_average",
"haproxy_server_aborts",
"haproxy_server_selected_total",
"haproxy_session_current",
"haproxy_session_limit",
"haproxy_session_max",
"haproxy_session_rate",
"haproxy_session_rate_max",
"haproxy_session_time_average",
"haproxy_session_total",
"haproxy_status",

),
pmetrictest.IgnoreMetricsOrder(),
pmetrictest.IgnoreMetricDataPointsOrder(),
pmetrictest.IgnoreMetricValues(
"haproxy_intercepted_requests",
"haproxy_bytes_in",
"haproxy_bytes_out",
"haproxy_connection_total",
"haproxy_request_total",
"haproxy_response_2xx",
"haproxy_session_current",
"haproxy_session_total",
),
)
}

func checkGoldenFile(t *testing.T, configFile string, expectedFilePath string, options ...pmetrictest.CompareMetricsOption) {
f := otlpreceiver.NewFactory()
port := testutils.GetAvailablePort(t)
c := f.CreateDefaultConfig().(*otlpreceiver.Config)
c.GRPC.NetAddr.Endpoint = fmt.Sprintf("localhost:%d", port)
sink := &consumertest.MetricsSink{}
receiver, err := f.CreateMetricsReceiver(context.Background(), receivertest.NewNopCreateSettings(), c, sink)
require.NoError(t, err)
require.NoError(t, receiver.Start(context.Background(), componenttest.NewNopHost()))
t.Cleanup(func() {
require.NoError(t, receiver.Shutdown(context.Background()))
})
logger, _ := zap.NewDevelopment()

dockerHost := "0.0.0.0"
if runtime.GOOS == "darwin" {
dockerHost = "host.docker.internal"
}
p, err := testutils.NewCollectorContainer().
WithConfigPath(filepath.Join("testdata", configFile)).
WithLogger(logger).
WithEnv(map[string]string{"OTLP_ENDPOINT": fmt.Sprintf("%s:%d", dockerHost, port)}).
Build()
require.NoError(t, err)
require.NoError(t, p.Start())
t.Cleanup(func() {
require.NoError(t, p.Shutdown())
})

expected, err := golden.ReadMetrics(filepath.Join("testdata", expectedFilePath))
//require.NoError(t, err)

assert.EventuallyWithT(t, func(tt *assert.CollectT) {
if len(sink.AllMetrics()) == 0 {
assert.Fail(tt, "No metrics collected")
return
}
err := pmetrictest.CompareMetrics(expected, sink.AllMetrics()[len(sink.AllMetrics())-1], options...)
assert.NoError(tt, err)
}, 10*time.Second, 1*time.Second)
}

0 comments on commit 3647d72

Please sign in to comment.