const EtcdImage = "gcr.io/etcd-development/etcd:" + EtcdVersion
const EtcdVersion = "v3.5.14"
const MockDatabase = "mydatabase"
const PlainConnStr = "grpc://localhost:6060"
const PlainServerAddress = "localhost:6060"
const PostgresImage = "docker.io/postgres:19beta2-alpine"
const TLSServerAddress = "localhost:5050"
var CA = `-----BEGIN CERTIFICATE-----
MIIDPzCCAiegAwIBAgIUeENQlQFVH5h7HszFJLLWo+KCQwQwDQYJKoZIhvcNAQEL
BQAwEjEQMA4GA1UEAwwHcGd3YXRjaDAeFw0yNTA2MTEwNTI1MDJaFw0zNTA2MDkw
NTI1MDJaMBIxEDAOBgNVBAMMB3Bnd2F0Y2gwggEiMA0GCSqGSIb3DQEBAQUAA4IB
DwAwggEKAoIBAQDtTW+kyvb3Y5OaYlriKp8HkHt95lfOgxQNZQfiREfEyLWU59bx
0ZIvFmejK38Qc0dlca9d+5tEkxotsbggJflLljfmnzhxsuZpr8SjmDd1m8XSo0IA
oDlVbKO6SZMlsyq3QrAOYAjG1LTPlATqvGAOs9NfFonjwoPXCjIwSfa+wexe5dRD
gJ114AKXw3ck5ZQ4Pw+w5ylgNSfVl548WY9DSOA+6HlZ17MYA1qmMOTwKae5fmsc
xlRPoIV3EJrKas7VlbebDOXOSXDV+9aMW6ox1xanUUDUgabzkzntfmuOttaIX1g1
nSMuHa7EEQF7lxgdg9OU8i/jygdlGcgBUYjtAgMBAAGjgYwwgYkwDAYDVR0TBAUw
AwEB/zAdBgNVHQ4EFgQUKGJs7OXINd2WL6X2meH90eINoJQwTQYDVR0jBEYwRIAU
KGJs7OXINd2WL6X2meH90eINoJShFqQUMBIxEDAOBgNVBAMMB3Bnd2F0Y2iCFHhD
UJUBVR+Yex7MxSSy1qPigkMEMAsGA1UdDwQEAwIBBjANBgkqhkiG9w0BAQsFAAOC
AQEABdY/4rsgMu+sCqEdacNzHqAz9X1ew37y1UONngm/7LPqbQrzzg/fBvOOJLcd
IzMJPtpdwokPYOW29jw/hY4R1RWr8012zc8Z0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CAFile)
var TestContext = log.WithLogger(context.Background(), log.NewNoopLogger())
func AuthInterceptor(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error)
func BlackholeListener(t *testing.T) (string, func())
BlackholeListener starts a TCP listener that accepts connections but never reads, writes, or closes them. Each accepted connection runs in its own goroutine that blocks until the listener is shut down.
This simulates a half-open TCP connection: the client sees an established TCP session, but no application-layer response ever arrives. The kernel keeps the connection open until the local read deadline (if any) fires or the peer gives up. Callers pair this with a context-bound round-trip to convert such stalls into bounded client-side failures.
The address is registered for cleanup via t.Cleanup, so a single BlackholeListener(t) call is leak-free under normal test runs. The returned close func is idempotent and may also be invoked explicitly by the test for early teardown.
Concurrency: the accept loop and any number of accepted connections run concurrently. Accepted goroutines block until ctx is cancelled; the close func waits for them via wg.Wait before returning.
func LoadServerTLSCredentials() (credentials.TransportCredentials, error)
func NewFakeExporter(t *testing.T, body string) *httptest.Server
NewFakeExporter starts an httptest.Server that responds to any request with the provided Prometheus exposition-format body and Content-Type "text/plain; version=0.0.4". The server is automatically closed when the test finishes via t.Cleanup.
func SetupEtcdContainer() (*etcd.EtcdContainer, func(), error)
func SetupPostgresContainer() (*postgres.PostgresContainer, func(), error)
func SetupPostgresContainerWithConfig(configPath string) (*postgres.PostgresContainer, func(), error)
Creates a PostgreSQL container with CSV logging enabled. This is useful for testing log parsing functionality with server_log_event_counts metric.
func SetupPostgresContainerWithInitScripts(scripts ...string) (*postgres.PostgresContainer, func(), error)
func SetupRPCServers() (func(), error)
BlockingBatchResults makes the first Query call honor ctx cancellation.
type BlockingBatchResults struct {
ctx context.Context
closed bool
}
func (b *BlockingBatchResults) Close() error
func (b *BlockingBatchResults) Err() error
func (b *BlockingBatchResults) Exec() (pgconn.CommandTag, error)
Exec blocks until ctx.Done() and returns ctx.Err().
func (b *BlockingBatchResults) Query() (pgx.Rows, error)
Query blocks until ctx.Done() and returns ctx.Err().
func (b *BlockingBatchResults) QueryRow() pgx.Row
QueryRow blocks until ctx.Done() and returns a Row whose Scan returns ctx.Err().
BlockingPool is a wedged PgxPoolIface used by fault-injection tests.
It embeds db.PgxPoolIface as a nil interface so the type satisfies db.PgxPoolIface; only the methods overridden below are usable. Any direct call to a non-overridden method panics (nil interface call).
Ping, Query, SendBatch, and Acquire all block until the supplied context is cancelled, then return ctx.Err(). This simulates a pool whose connections are stuck on the wire — the failure mode that client-side deadlines must convert into bounded failures.
type BlockingPool struct {
db.PgxPoolIface
}
func (BlockingPool) Acquire(ctx context.Context) (*pgxpool.Conn, error)
Acquire blocks until ctx.Done() and returns ctx.Err().
func (BlockingPool) Close()
Close is a no-op so the pool can be embedded without panicking.
func (BlockingPool) Exec(ctx context.Context, _ string, _ ...any) (pgconn.CommandTag, error)
Exec blocks until ctx.Done() and returns ctx.Err().
func (BlockingPool) Ping(ctx context.Context) error
Ping blocks until ctx.Done() and returns ctx.Err().
func (BlockingPool) Query(ctx context.Context, _ string, _ ...any) (pgx.Rows, error)
Query blocks until ctx.Done() and returns ctx.Err().
func (BlockingPool) QueryRow(ctx context.Context, _ string, _ ...any) pgx.Row
QueryRow blocks until ctx.Done() and returns a row whose Scan returns ctx.Err().
func (BlockingPool) SendBatch(ctx context.Context, _ *pgx.Batch) pgx.BatchResults
first Query call returns ctx.Err(). SendBatch blocks until ctx.Done() and returns a BatchResults whose first Query call returns ctx.Err().
MockMetricsReaderWriter implements MetricsReaderWriter interface
type MockMetricsReaderWriter struct {
GetMetricsFunc func() (*metrics.Metrics, error)
UpdateMetricFunc func(name string, m metrics.Metric) error
CreateMetricFunc func(name string, m metrics.Metric) error
DeleteMetricFunc func(name string) error
DeletePresetFunc func(name string) error
UpdatePresetFunc func(name string, preset metrics.Preset) error
CreatePresetFunc func(name string, preset metrics.Preset) error
WriteMetricsFunc func(metricDefs *metrics.Metrics) error
}
func (m *MockMetricsReaderWriter) CreateMetric(name string, metric metrics.Metric) error
func (m *MockMetricsReaderWriter) CreatePreset(name string, preset metrics.Preset) error
func (m *MockMetricsReaderWriter) DeleteMetric(name string) error
func (m *MockMetricsReaderWriter) DeletePreset(name string) error
func (m *MockMetricsReaderWriter) GetMetrics() (*metrics.Metrics, error)
func (m *MockMetricsReaderWriter) UpdateMetric(name string, metric metrics.Metric) error
func (m *MockMetricsReaderWriter) UpdatePreset(name string, preset metrics.Preset) error
func (m *MockMetricsReaderWriter) WriteMetrics(metricDefs *metrics.Metrics) error
MockSourcesReaderWriter implements SourcesReaderWriter interface
type MockSourcesReaderWriter struct {
GetSourcesFunc func() (sources.Sources, error)
UpdateSourceFunc func(md sources.Source) error
CreateSourceFunc func(md sources.Source) error
DeleteSourceFunc func(name string) error
WriteSourcesFunc func(sources.Sources) error
}
func (m *MockSourcesReaderWriter) CreateSource(md sources.Source) error
func (m *MockSourcesReaderWriter) DeleteSource(name string) error
func (m *MockSourcesReaderWriter) GetSources() (sources.Sources, error)
func (m *MockSourcesReaderWriter) UpdateSource(md sources.Source) error
func (m *MockSourcesReaderWriter) WriteSources(srcs sources.Sources) error
Receiver implements the ReceiverServer interface for testing purposes
type Receiver struct {
pb.UnimplementedReceiverServer
}
func (receiver *Receiver) DefineMetrics(_ context.Context, metricsStruct *structpb.Struct) (*pb.Reply, error)
func (receiver *Receiver) SyncMetric(_ context.Context, syncReq *pb.SyncReq) (*pb.Reply, error)
func (receiver *Receiver) UpdateMeasurements(_ context.Context, msg *pb.MeasurementEnvelope) (*pb.Reply, error)
SafeBuffer is a goroutine-safe string buffer that satisfies io.Writer. Use String() to read the accumulated output.
type SafeBuffer struct {
mu sync.Mutex
sb strings.Builder
}
func NewTestLogger(t *testing.T, level logrus.Level) (context.Context, *SafeBuffer)
NewTestLogger creates a capturing logger at the given level, injects it into a context derived from t.Context(), and returns both the context and the output buffer. Use the buffer in assertions to verify log output:
ctx, out := testutil.NewTestLogger(t, logrus.WarnLevel) doSomething(ctx) assert.Contains(t, out.String(), "expected warning")
func (b *SafeBuffer) String() string
String returns a snapshot of the accumulated log output.
func (b *SafeBuffer) Write(p []byte) (int, error)
blockingRow makes pgx.Row.Scan honor ctx cancellation.
type blockingRow struct {
ctx context.Context
}
func (b blockingRow) Scan(_ ...any) error
Scan blocks until ctx.Done() and returns ctx.Err().