mirror of
https://github.com/containers/podman.git
synced 2025-08-06 03:19:52 +08:00
Improvements for machine
clean up ci failures and add appropriate arch,os exclusion tags Signed-off-by: baude <bbaude@redhat.com>
This commit is contained in:
26
utils/ports.go
Normal file
26
utils/ports.go
Normal file
@ -0,0 +1,26 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Find a random, open port on the host
|
||||
func GetRandomPort() (int, error) {
|
||||
l, err := net.Listen("tcp", ":0")
|
||||
if err != nil {
|
||||
return 0, errors.Wrapf(err, "unable to get free TCP port")
|
||||
}
|
||||
defer l.Close()
|
||||
_, randomPort, err := net.SplitHostPort(l.Addr().String())
|
||||
if err != nil {
|
||||
return 0, errors.Wrapf(err, "unable to determine free port")
|
||||
}
|
||||
rp, err := strconv.Atoi(randomPort)
|
||||
if err != nil {
|
||||
return 0, errors.Wrapf(err, "unable to convert random port to int")
|
||||
}
|
||||
return rp, nil
|
||||
}
|
Reference in New Issue
Block a user