Skip to content

Commit 3024d33

Browse files
authored
Merge pull request #6454 from zclyne/yifan/scheduler-unit-test-port
make healthz/metrics server ports of the scheduler/descheduler unit tests to be detected automatically to avoid flaky test failures
2 parents f4f63c8 + a129087 commit 3024d33

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

cmd/descheduler/app/descheduler_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package app
1818

1919
import (
2020
"context"
21+
"fmt"
2122
"net/http"
2223
"testing"
2324
"time"
@@ -27,6 +28,7 @@ import (
2728

2829
"github.com/karmada-io/karmada/cmd/descheduler/app/options"
2930
"github.com/karmada-io/karmada/pkg/util/names"
31+
testingutil "github.com/karmada-io/karmada/pkg/util/testing"
3032
)
3133

3234
func TestNewDeschedulerCommand(t *testing.T) {
@@ -66,8 +68,10 @@ func TestDeschedulerCommandFlagParsing(t *testing.T) {
6668
}
6769

6870
func TestServeHealthzAndMetrics(t *testing.T) {
69-
healthAddress := "127.0.0.1:8082"
70-
metricsAddress := "127.0.0.1:8083"
71+
ports, err := testingutil.GetFreePorts("127.0.0.1", 2)
72+
require.NoError(t, err)
73+
healthAddress := fmt.Sprintf("127.0.0.1:%d", ports[0])
74+
metricsAddress := fmt.Sprintf("127.0.0.1:%d", ports[1])
7175

7276
go serveHealthzAndMetrics(healthAddress, metricsAddress)
7377

cmd/scheduler/app/scheduler_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package app
1818

1919
import (
2020
"context"
21+
"fmt"
2122
"net/http"
2223
"testing"
2324
"time"
@@ -27,6 +28,7 @@ import (
2728

2829
"github.com/karmada-io/karmada/cmd/scheduler/app/options"
2930
"github.com/karmada-io/karmada/pkg/util/names"
31+
testingutil "github.com/karmada-io/karmada/pkg/util/testing"
3032
)
3133

3234
func TestNewSchedulerCommand(t *testing.T) {
@@ -66,8 +68,10 @@ func TestSchedulerCommandFlagParsing(t *testing.T) {
6668
}
6769

6870
func TestServeHealthzAndMetrics(t *testing.T) {
69-
healthAddress := "127.0.0.1:8082"
70-
metricsAddress := "127.0.0.1:8083"
71+
ports, err := testingutil.GetFreePorts("127.0.0.1", 2)
72+
require.NoError(t, err)
73+
healthAddress := fmt.Sprintf("127.0.0.1:%d", ports[0])
74+
metricsAddress := fmt.Sprintf("127.0.0.1:%d", ports[1])
7175

7276
go serveHealthzAndMetrics(healthAddress, metricsAddress)
7377

pkg/util/testing/helpers.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import (
2121
"crypto/rsa"
2222
"crypto/x509"
2323
"encoding/pem"
24+
"fmt"
2425
"math/big"
26+
"net"
2527
"time"
2628
)
2729

@@ -69,3 +71,37 @@ func GenerateTestCACertificate() (string, string, error) {
6971

7072
return string(certPEMData), string(privKeyPEMData), nil
7173
}
74+
75+
// GetFreePorts attempts to find n available TCP ports on the specified host. It
76+
// returns a slice of allocated port numbers or an error if it fails to acquire
77+
// them.
78+
func GetFreePorts(host string, n int) ([]int, error) {
79+
ports := make([]int, 0, n)
80+
listeners := make([]net.Listener, 0, n)
81+
82+
// Make sure we close all listeners if there's an error.
83+
defer func() {
84+
for _, l := range listeners {
85+
l.Close()
86+
}
87+
}()
88+
89+
for i := 0; i < n; i++ {
90+
listener, err := net.Listen("tcp", fmt.Sprintf("%s:0", host))
91+
if err != nil {
92+
return nil, err
93+
}
94+
listeners = append(listeners, listener)
95+
tcpAddr, ok := listener.Addr().(*net.TCPAddr)
96+
if !ok {
97+
return nil, fmt.Errorf("listener address is not a *net.TCPAddr")
98+
}
99+
ports = append(ports, tcpAddr.Port)
100+
}
101+
102+
// At this point we have all ports, so we can close the listeners.
103+
for _, l := range listeners {
104+
l.Close()
105+
}
106+
return ports, nil
107+
}

0 commit comments

Comments
 (0)