forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[chore]Port postgresql receiver integration test to new framework (op…
- Loading branch information
1 parent
9bd64b7
commit 61e0cb6
Showing
1 changed file
with
40 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,144 +1,62 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//go:build integration | ||
// +build integration | ||
|
||
package postgresqlreceiver | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/testcontainers/testcontainers-go" | ||
"github.com/testcontainers/testcontainers-go/wait" | ||
"go.opentelemetry.io/collector/component/componenttest" | ||
"go.opentelemetry.io/collector/consumer/consumertest" | ||
"go.opentelemetry.io/collector/receiver/receivertest" | ||
"go.opentelemetry.io/collector/component" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/golden" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/scraperinttest" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest" | ||
) | ||
|
||
type configFunc func(hostname string) *Config | ||
|
||
// cleanupFunc exists to allow integration test cases to clean any registries it had | ||
// to modify in order to change behavior of the integration test. i.e. featuregates | ||
type cleanupFunc func() | ||
|
||
type testCase struct { | ||
name string | ||
cfg configFunc | ||
cleanup cleanupFunc | ||
expectedFile string | ||
} | ||
const postgresqlPort = "5432" | ||
|
||
func TestIntegration(t *testing.T) { | ||
testCases := []testCase{ | ||
{ | ||
name: "single_db", | ||
cfg: func(hostname string) *Config { | ||
f := NewFactory() | ||
cfg := f.CreateDefaultConfig().(*Config) | ||
cfg.Endpoint = net.JoinHostPort(hostname, "15432") | ||
cfg.Databases = []string{"otel"} | ||
cfg.Username = "otelu" | ||
cfg.Password = "otelp" | ||
cfg.Insecure = true | ||
return cfg | ||
}, | ||
expectedFile: filepath.Join("testdata", "integration", "expected_single_db.yaml"), | ||
}, | ||
{ | ||
name: "multi_db", | ||
cfg: func(hostname string) *Config { | ||
f := NewFactory() | ||
cfg := f.CreateDefaultConfig().(*Config) | ||
cfg.Endpoint = net.JoinHostPort(hostname, "15432") | ||
cfg.Databases = []string{"otel", "otel2"} | ||
cfg.Username = "otelu" | ||
cfg.Password = "otelp" | ||
cfg.Insecure = true | ||
return cfg | ||
}, | ||
expectedFile: filepath.Join("testdata", "integration", "expected_multi_db.yaml"), | ||
}, | ||
{ | ||
name: "all_db", | ||
cfg: func(hostname string) *Config { | ||
f := NewFactory() | ||
cfg := f.CreateDefaultConfig().(*Config) | ||
cfg.Endpoint = net.JoinHostPort(hostname, "15432") | ||
cfg.Databases = []string{} | ||
cfg.Username = "otelu" | ||
cfg.Password = "otelp" | ||
cfg.Insecure = true | ||
return cfg | ||
}, | ||
expectedFile: filepath.Join("testdata", "integration", "expected_all_db.yaml"), | ||
}, | ||
} | ||
|
||
container := getContainer(t, testcontainers.ContainerRequest{ | ||
FromDockerfile: testcontainers.FromDockerfile{ | ||
Context: filepath.Join("testdata", "integration"), | ||
Dockerfile: "Dockerfile.postgresql", | ||
}, | ||
ExposedPorts: []string{"15432:5432"}, | ||
WaitingFor: wait.ForListeningPort("5432"). | ||
WithStartupTimeout(2 * time.Minute), | ||
}) | ||
defer func() { | ||
require.NoError(t, container.Terminate(context.Background())) | ||
}() | ||
hostname, err := container.Host(context.Background()) | ||
require.NoError(t, err) | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
if tc.cleanup != nil { | ||
defer tc.cleanup() | ||
} | ||
expectedMetrics, err := golden.ReadMetrics(tc.expectedFile) | ||
require.NoError(t, err) | ||
|
||
f := NewFactory() | ||
consumer := new(consumertest.MetricsSink) | ||
settings := receivertest.NewNopCreateSettings() | ||
rcvr, err := f.CreateMetricsReceiver(context.Background(), settings, tc.cfg(hostname), consumer) | ||
require.NoError(t, err, "failed creating metrics receiver") | ||
require.NoError(t, rcvr.Start(context.Background(), componenttest.NewNopHost())) | ||
require.Eventuallyf(t, func() bool { | ||
return consumer.DataPointCount() > 0 | ||
}, 2*time.Minute, 1*time.Second, "failed to receive more than 0 metrics") | ||
|
||
actualMetrics := consumer.AllMetrics()[0] | ||
|
||
require.NoError(t, pmetrictest.CompareMetrics( | ||
expectedMetrics, actualMetrics, | ||
pmetrictest.IgnoreResourceMetricsOrder(), | ||
pmetrictest.IgnoreMetricValues(), | ||
pmetrictest.IgnoreSubsequentDataPoints("postgresql.backends"), | ||
pmetrictest.IgnoreMetricDataPointsOrder(), | ||
pmetrictest.IgnoreStartTimestamp(), | ||
pmetrictest.IgnoreTimestamp(), | ||
)) | ||
}) | ||
} | ||
t.Run("single_db", integrationTest("single_db", []string{"otel"})) | ||
t.Run("multi_db", integrationTest("multi_db", []string{"otel", "otel2"})) | ||
t.Run("all_db", integrationTest("all_db", []string{})) | ||
} | ||
|
||
func getContainer(t *testing.T, req testcontainers.ContainerRequest) testcontainers.Container { | ||
require.NoError(t, req.Validate()) | ||
container, err := testcontainers.GenericContainer( | ||
context.Background(), | ||
testcontainers.GenericContainerRequest{ | ||
ContainerRequest: req, | ||
Started: true, | ||
}) | ||
require.NoError(t, err) | ||
return container | ||
func integrationTest(name string, databases []string) func(*testing.T) { | ||
expectedFile := filepath.Join("testdata", "integration", "expected_"+name+".yaml") | ||
return scraperinttest.NewIntegrationTest( | ||
NewFactory(), | ||
scraperinttest.WithContainerRequest( | ||
testcontainers.ContainerRequest{ | ||
FromDockerfile: testcontainers.FromDockerfile{ | ||
Context: filepath.Join("testdata", "integration"), | ||
Dockerfile: "Dockerfile.postgresql", | ||
}, | ||
ExposedPorts: []string{postgresqlPort}, | ||
WaitingFor: wait.ForListeningPort(postgresqlPort). | ||
WithStartupTimeout(2 * time.Minute), | ||
}), | ||
scraperinttest.WithCustomConfig( | ||
func(t *testing.T, cfg component.Config, ci *scraperinttest.ContainerInfo) { | ||
rCfg := cfg.(*Config) | ||
rCfg.CollectionInterval = time.Second | ||
rCfg.Endpoint = net.JoinHostPort(ci.Host(t), ci.MappedPort(t, postgresqlPort)) | ||
rCfg.Databases = databases | ||
rCfg.Username = "otelu" | ||
rCfg.Password = "otelp" | ||
rCfg.Insecure = true | ||
}), | ||
scraperinttest.WithExpectedFile(expectedFile), | ||
scraperinttest.WithCompareOptions( | ||
pmetrictest.IgnoreResourceMetricsOrder(), | ||
pmetrictest.IgnoreMetricValues(), | ||
pmetrictest.IgnoreSubsequentDataPoints("postgresql.backends"), | ||
pmetrictest.IgnoreMetricDataPointsOrder(), | ||
pmetrictest.IgnoreStartTimestamp(), | ||
pmetrictest.IgnoreTimestamp(), | ||
), | ||
).Run | ||
} |