f0c3dca49c
Copy the updated version of freeport (sdk/freeport), and tweak it for use in Nomad tests. This means staying below port 10000 to avoid conflicts with the lib/freeport that is still transitively used by the old version of consul that we vendor. Also provide implementations to find ephemeral ports of macOS and Windows environments. Ports acquired through freeport are supposed to be returned to freeport, which this change now also introduces. Many tests are modified to include calls to a cleanup function for Server objects. This should help quite a bit with some flakey tests, but not all of them. Our port problems will not go away completely until we upgrade our vendor version of consul. With Go modules, we'll probably do a 'replace' to swap out other copies of freeport with the one now in 'nomad/helper/freeport'.
47 lines
907 B
Go
47 lines
907 B
Go
//+build darwin
|
|
|
|
package freeport
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"regexp"
|
|
"strconv"
|
|
)
|
|
|
|
/*
|
|
$ sysctl net.inet.ip.portrange.first net.inet.ip.portrange.last
|
|
net.inet.ip.portrange.first: 49152
|
|
net.inet.ip.portrange.last: 65535
|
|
*/
|
|
|
|
const (
|
|
ephPortFirst = "net.inet.ip.portrange.first"
|
|
ephPortLast = "net.inet.ip.portrange.last"
|
|
command = "sysctl"
|
|
)
|
|
|
|
var ephPortRe = regexp.MustCompile(`^\s*(\d+)\s+(\d+)\s*$`)
|
|
|
|
func getEphemeralPortRange() (int, int, error) {
|
|
cmd := exec.Command(command, "-n", ephPortFirst, ephPortLast)
|
|
out, err := cmd.Output()
|
|
if err != nil {
|
|
return 0, 0, err
|
|
}
|
|
|
|
val := string(out)
|
|
|
|
m := ephPortRe.FindStringSubmatch(val)
|
|
if m != nil {
|
|
min, err1 := strconv.Atoi(m[1])
|
|
max, err2 := strconv.Atoi(m[2])
|
|
|
|
if err1 == nil && err2 == nil {
|
|
return min, max, nil
|
|
}
|
|
}
|
|
|
|
return 0, 0, fmt.Errorf("unexpected sysctl value %q for keys %q %q", val, ephPortFirst, ephPortLast)
|
|
}
|