From 3a7d7028ce40e62a1480af3ef38cc9e25994cc91 Mon Sep 17 00:00:00 2001 From: David Ashpole Date: Sat, 14 Mar 2026 13:11:37 +0000 Subject: [PATCH 1/2] fix port collision flakes in correctness tests --- .../connectors/correctness_test.go | 16 ++++++---- .../traces/correctness_test.go | 16 ++++++---- testbed/correctnesstests/utils.go | 29 +++++++++++++++++-- 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/testbed/correctnesstests/connectors/correctness_test.go b/testbed/correctnesstests/connectors/correctness_test.go index aff464cf9fcdb..875024afe34b9 100644 --- a/testbed/correctnesstests/connectors/correctness_test.go +++ b/testbed/correctnesstests/connectors/correctness_test.go @@ -61,11 +61,6 @@ func testWithGoldenDataset( require.NoError(t, err, "default components resulted in: %v", err) runner := testbed.NewInProcessCollector(factories) validator := testbed.NewCorrectTestValidator(sender.ProtocolName(), receiver.ProtocolName(), dataProvider) - config := correctnesstests.CreateConfigYaml(t, sender, receiver, connector, processors) - log.Println(config) - configCleanup, cfgErr := runner.PrepareConfig(t, config) - require.NoError(t, cfgErr, "collector configuration resulted in: %v", cfgErr) - defer configCleanup() tc := testbed.NewTestCase( t, dataProvider, @@ -78,8 +73,17 @@ func testWithGoldenDataset( ) defer tc.Stop() - tc.EnableRecording() tc.StartBackend() + + // CreateConfigYaml must be called after StartBackend to ensure the receiver port is bound. + // This prevents CreateConfigYaml from picking the same port for Prometheus telemetry. + config := correctnesstests.CreateConfigYaml(t, sender, receiver, connector, processors) + log.Println(config) + configCleanup, cfgErr := runner.PrepareConfig(t, config) + require.NoError(t, cfgErr, "collector configuration resulted in: %v", cfgErr) + defer configCleanup() + + tc.EnableRecording() tc.StartAgent() tc.StartLoad(testbed.LoadOptions{ diff --git a/testbed/correctnesstests/traces/correctness_test.go b/testbed/correctnesstests/traces/correctness_test.go index 9ff0e81a4149a..c68b8ff402403 100644 --- a/testbed/correctnesstests/traces/correctness_test.go +++ b/testbed/correctnesstests/traces/correctness_test.go @@ -59,11 +59,6 @@ func testWithTracingGoldenDataset( require.NoError(t, err, "default components resulted in: %v", err) runner := testbed.NewInProcessCollector(factories) validator := testbed.NewCorrectTestValidator(sender.ProtocolName(), receiver.ProtocolName(), dataProvider) - config := correctnesstests.CreateConfigYaml(t, sender, receiver, nil, processors) - log.Println(config) - configCleanup, cfgErr := runner.PrepareConfig(t, config) - require.NoError(t, cfgErr, "collector configuration resulted in: %v", cfgErr) - defer configCleanup() tc := testbed.NewTestCase( t, dataProvider, @@ -76,8 +71,17 @@ func testWithTracingGoldenDataset( ) defer tc.Stop() - tc.EnableRecording() tc.StartBackend() + + // CreateConfigYaml must be called after StartBackend to ensure the receiver port is bound. + // This prevents CreateConfigYaml from picking the same port for Prometheus telemetry. + config := correctnesstests.CreateConfigYaml(t, sender, receiver, nil, processors) + log.Println(config) + configCleanup, cfgErr := runner.PrepareConfig(t, config) + require.NoError(t, cfgErr, "collector configuration resulted in: %v", cfgErr) + defer configCleanup() + + tc.EnableRecording() tc.StartAgent() tc.StartLoad(testbed.LoadOptions{ diff --git a/testbed/correctnesstests/utils.go b/testbed/correctnesstests/utils.go index 440d4dce27944..b1e9fad3a3b8f 100644 --- a/testbed/correctnesstests/utils.go +++ b/testbed/correctnesstests/utils.go @@ -10,6 +10,7 @@ import ( "path/filepath" "strings" "testing" + "net" "go.uber.org/zap" @@ -35,6 +36,30 @@ func CreateConfigYaml( connector testbed.DataConnector, processors []ProcessorNameAndConfigBody, ) string { + var plannedPorts []int + if sender != nil { + if tcpAddr, ok := sender.GetEndpoint().(*net.TCPAddr); ok { + plannedPorts = append(plannedPorts, tcpAddr.Port) + } + } + + // Avoid picking a telemetry port that is already planned for the sender. + // This prevents port collisions between the collector's Prometheus telemetry + // and the collector's receivers (sender endpoint), which can happen if CreateConfigYaml + // is called before the receivers are started (bound). + telemetryPort := testutil.GetAvailablePort(tb) + found := false + for !found { + found = true + for _, p := range plannedPorts { + if p == telemetryPort { + found = false + telemetryPort = testutil.GetAvailablePort(tb) + break + } + } + } + // Prepare extra processor config section and comma-separated list of extra processor // names to use in corresponding "processors" settings. processorsSections := "" @@ -103,7 +128,7 @@ service: receiver.GenConfigYAMLStr(), processorsSections, connector.GenConfigYAMLStr(), - testutil.GetAvailablePort(tb), + telemetryPort, pipeline1, sender.ProtocolName(), processorsList, @@ -146,7 +171,7 @@ service: sender.GenConfigYAMLStr(), receiver.GenConfigYAMLStr(), processorsSections, - testutil.GetAvailablePort(tb), + telemetryPort, pipeline1, sender.ProtocolName(), processorsList, From 1215f07b3d3ff42df948cef3bbd839acd190618b Mon Sep 17 00:00:00 2001 From: David Ashpole Date: Sat, 14 Mar 2026 13:58:41 +0000 Subject: [PATCH 2/2] lint --- testbed/correctnesstests/utils.go | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/testbed/correctnesstests/utils.go b/testbed/correctnesstests/utils.go index b1e9fad3a3b8f..b2989f46c6a5e 100644 --- a/testbed/correctnesstests/utils.go +++ b/testbed/correctnesstests/utils.go @@ -6,11 +6,12 @@ package correctnesstests // import "github.com/open-telemetry/opentelemetry-coll import ( "bufio" "fmt" + "net" "os" "path/filepath" + "slices" "strings" "testing" - "net" "go.uber.org/zap" @@ -48,16 +49,8 @@ func CreateConfigYaml( // and the collector's receivers (sender endpoint), which can happen if CreateConfigYaml // is called before the receivers are started (bound). telemetryPort := testutil.GetAvailablePort(tb) - found := false - for !found { - found = true - for _, p := range plannedPorts { - if p == telemetryPort { - found = false - telemetryPort = testutil.GetAvailablePort(tb) - break - } - } + for slices.Contains(plannedPorts, telemetryPort) { + telemetryPort = testutil.GetAvailablePort(tb) } // Prepare extra processor config section and comma-separated list of extra processor