| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package testutil // import "go.opentelemetry.io/collector/internal/testutil" |
| 5 |
|
| 6 |
import ( |
| 7 |
"net" |
| 8 |
"os/exec" |
| 9 |
"runtime" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/stretchr/testify/assert" |
| 14 |
"github.com/stretchr/testify/require" |
| 15 |
) |
| 16 |
|
| 17 |
type portpair struct { |
| 18 |
first string |
| 19 |
last string |
| 20 |
} |
| 21 |
|
| 22 |
// GetAvailableLocalAddress finds an available local port and returns an endpoint |
| 23 |
// describing it. The port is available for opening when this function returns |
| 24 |
// provided that there is no race by some other code to grab the same port |
| 25 |
// immediately. |
| 26 |
func GetAvailableLocalAddress(tb testing.TB) string { |
| 27 |
return findAvailable(tb, "tcp4") |
| 28 |
} |
| 29 |
|
| 30 |
// GetAvailableLocalIPv6Address is IPv6 version of GetAvailableLocalAddress. |
| 31 |
func GetAvailableLocalIPv6Address(tb testing.TB) string { |
| 32 |
return findAvailable(tb, "tcp6") |
| 33 |
} |
| 34 |
|
| 35 |
func findAvailable(tb testing.TB, network string) string { |
| 36 |
// Retry has been added for windows as net.Listen can return a port that is not actually available. Details can be |
| 37 |
// found in https://github.com/docker/for-win/issues/3171 but to summarize Hyper-V will reserve ranges of ports |
| 38 |
// which do not show up under the "netstat -ano" but can only be found by |
| 39 |
// "netsh interface ipv4 show excludedportrange protocol=tcp". We'll use []exclusions to hold those ranges and |
| 40 |
// retry if the port returned by GetAvailableLocalAddress falls in one of those them. |
| 41 |
var exclusions []portpair |
| 42 |
portFound := false |
| 43 |
if runtime.GOOS == "windows" { |
| 44 |
exclusions = getExclusionsList(tb, network) |
| 45 |
} |
| 46 |
|
| 47 |
var endpoint string |
| 48 |
for !portFound { |
| 49 |
endpoint = findAvailableAddress(tb, network) |
| 50 |
_, port, err := net.SplitHostPort(endpoint) |
| 51 |
require.NoError(tb, err) |
| 52 |
portFound = true |
| 53 |
if runtime.GOOS == "windows" { |
| 54 |
for _, pair := range exclusions { |
| 55 |
if port >= pair.first && port <= pair.last { |
| 56 |
portFound = false |
| 57 |
break |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
return endpoint |
| 64 |
} |
| 65 |
|
| 66 |
func findAvailableAddress(tb testing.TB, network string) string { |
| 67 |
var host string |
| 68 |
switch network { |
| 69 |
case "tcp", "tcp4": |
| 70 |
host = "localhost" |
| 71 |
case "tcp6": |
| 72 |
host = "[::1]" |
| 73 |
} |
| 74 |
require.NotEmpty(tb, host, "network must be either of tcp, tcp4 or tcp6") |
| 75 |
|
| 76 |
ln, err := net.Listen("tcp", host+":0") |
| 77 |
require.NoError(tb, err, "Failed to get a free local port") |
| 78 |
// There is a possible race if something else takes this same port before |
| 79 |
// the test uses it, however, that is unlikely in practice. |
| 80 |
defer func() { |
| 81 |
assert.NoError(tb, ln.Close()) |
| 82 |
}() |
| 83 |
return ln.Addr().String() |
| 84 |
} |
| 85 |
|
| 86 |
// Get excluded ports on Windows from the command: netsh interface ipv4 show excludedportrange protocol=tcp |
| 87 |
func getExclusionsList(tb testing.TB, network string) []portpair { |
| 88 |
var cmdTCP *exec.Cmd |
| 89 |
switch network { |
| 90 |
case "tcp", "tcp4": |
| 91 |
cmdTCP = exec.Command("netsh", "interface", "ipv4", "show", "excludedportrange", "protocol=tcp") |
| 92 |
case "tcp6": |
| 93 |
cmdTCP = exec.Command("netsh", "interface", "ipv6", "show", "excludedportrange", "protocol=tcp") |
| 94 |
} |
| 95 |
require.NotZero(tb, cmdTCP, "network must be either of tcp, tcp4 or tcp6") |
| 96 |
|
| 97 |
outputTCP, errTCP := cmdTCP.CombinedOutput() |
| 98 |
require.NoError(tb, errTCP) |
| 99 |
exclusions := createExclusionsList(tb, string(outputTCP)) |
| 100 |
|
| 101 |
cmdUDP := exec.Command("netsh", "interface", "ipv4", "show", "excludedportrange", "protocol=udp") |
| 102 |
outputUDP, errUDP := cmdUDP.CombinedOutput() |
| 103 |
require.NoError(tb, errUDP) |
| 104 |
exclusions = append(exclusions, createExclusionsList(tb, string(outputUDP))...) |
| 105 |
|
| 106 |
return exclusions |
| 107 |
} |
| 108 |
|
| 109 |
func createExclusionsList(tb testing.TB, exclusionsText string) []portpair { |
| 110 |
var exclusions []portpair |
| 111 |
|
| 112 |
parts := strings.Split(exclusionsText, "--------") |
| 113 |
require.Len(tb, parts, 3) |
| 114 |
portsText := strings.Split(parts[2], "*") |
| 115 |
require.Greater(tb, len(portsText), 1) // original text may have a suffix like " - Administered port exclusions." |
| 116 |
lines := strings.SplitSeq(portsText[0], "\n") |
| 117 |
for line := range lines { |
| 118 |
if strings.TrimSpace(line) != "" { |
| 119 |
entries := strings.Fields(strings.TrimSpace(line)) |
| 120 |
require.Len(tb, entries, 2) |
| 121 |
pair := portpair{entries[0], entries[1]} |
| 122 |
exclusions = append(exclusions, pair) |
| 123 |
} |
| 124 |
} |
| 125 |
return exclusions |
| 126 |
} |