mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-20 18:31:15 +00:00
test/integration: Make localPort reusable as FindFreeLocalPort
This commit is contained in:
parent
6f70b977ff
commit
82dc083016
@ -56,20 +56,6 @@ var groupVersionForDiscovery = metav1.GroupVersionForDiscovery{
|
|||||||
Version: groupVersion.Version,
|
Version: groupVersion.Version,
|
||||||
}
|
}
|
||||||
|
|
||||||
func localPort() (int, error) {
|
|
||||||
l, err := net.Listen("tcp", ":0")
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
defer l.Close()
|
|
||||||
addr := strings.Split(l.Addr().String(), ":")
|
|
||||||
port, err := strconv.Atoi(addr[len(addr)-1])
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
return port, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAggregatedAPIServer(t *testing.T) {
|
func TestAggregatedAPIServer(t *testing.T) {
|
||||||
stopCh := make(chan struct{})
|
stopCh := make(chan struct{})
|
||||||
defer close(stopCh)
|
defer close(stopCh)
|
||||||
@ -106,7 +92,7 @@ func TestAggregatedAPIServer(t *testing.T) {
|
|||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
// always get a fresh port in case something claimed the old one
|
// always get a fresh port in case something claimed the old one
|
||||||
kubePort, err := localPort()
|
kubePort, err := framework.FindFreeLocalPort()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -179,7 +165,7 @@ func TestAggregatedAPIServer(t *testing.T) {
|
|||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
// always get a fresh port in case something claimed the old one
|
// always get a fresh port in case something claimed the old one
|
||||||
wardlePortInt, err := localPort()
|
wardlePortInt, err := framework.FindFreeLocalPort()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -256,7 +242,7 @@ func TestAggregatedAPIServer(t *testing.T) {
|
|||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
// always get a fresh port in case something claimed the old one
|
// always get a fresh port in case something claimed the old one
|
||||||
aggregatorPortInt, err := localPort()
|
aggregatorPortInt, err := framework.FindFreeLocalPort()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
goruntime "runtime"
|
goruntime "runtime"
|
||||||
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -509,3 +510,25 @@ func RunParallel(task Task, numTasks, numWorkers int) {
|
|||||||
wg.Wait()
|
wg.Wait()
|
||||||
close(semCh)
|
close(semCh)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindFreeLocalPort returns the number of an available port number on
|
||||||
|
// the loopback interface. Useful for determining the port to launch
|
||||||
|
// a server on. Error handling required - there is a non-zero chance
|
||||||
|
// that the returned port number will be bound by another process
|
||||||
|
// after this function returns.
|
||||||
|
func FindFreeLocalPort() (int, error) {
|
||||||
|
l, err := net.Listen("tcp", ":0")
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
defer l.Close()
|
||||||
|
_, portStr, err := net.SplitHostPort(l.Addr().String())
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
port, err := strconv.Atoi(portStr)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return port, nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user