From 4ab8a6f67eb9de0de40d478cb0cbec05b1b725c0 Mon Sep 17 00:00:00 2001 From: baude Date: Mon, 22 Mar 2021 13:29:25 -0500 Subject: Improvements for machine clean up ci failures and add appropriate arch,os exclusion tags Signed-off-by: baude --- utils/ports.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 utils/ports.go (limited to 'utils') diff --git a/utils/ports.go b/utils/ports.go new file mode 100644 index 000000000..0a4f67dcc --- /dev/null +++ b/utils/ports.go @@ -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 +} -- cgit v1.2.3-54-g00ecf