-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move top-level packages under the internal package (#190)
- Loading branch information
1 parent
9ff6074
commit c67aa3d
Showing
60 changed files
with
183 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
confmap/provider/eecprovider/go.mod → internal/confmap/provider/eecprovider/go.mod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package testutil // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/testutil" | ||
|
||
import ( | ||
"net" | ||
"os/exec" | ||
"runtime" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/featuregate" | ||
) | ||
|
||
type portpair struct { | ||
first string | ||
last string | ||
} | ||
|
||
// GetAvailableLocalAddress finds an available local port on tcp network and returns an endpoint | ||
// describing it. The port is available for opening when this function returns | ||
// provided that there is no race by some other code to grab the same port | ||
// immediately. | ||
func GetAvailableLocalAddress(t testing.TB) string { | ||
return GetAvailableLocalNetworkAddress(t, "tcp") | ||
} | ||
|
||
// GetAvailableLocalNetworkAddress finds an available local port on specified network and returns an endpoint | ||
// describing it. The port is available for opening when this function returns | ||
// provided that there is no race by some other code to grab the same port | ||
// immediately. | ||
func GetAvailableLocalNetworkAddress(t testing.TB, network string) string { | ||
// Retry has been added for windows as net.Listen can return a port that is not actually available. Details can be | ||
// found in https://github.com/docker/for-win/issues/3171 but to summarize Hyper-V will reserve ranges of ports | ||
// which do not show up under the "netstat -ano" but can only be found by | ||
// "netsh interface ipv4 show excludedportrange protocol=tcp". We'll use []exclusions to hold those ranges and | ||
// retry if the port returned by GetAvailableLocalAddress falls in one of those them. | ||
var exclusions []portpair | ||
|
||
portFound := false | ||
if runtime.GOOS == "windows" { | ||
exclusions = getExclusionsList(t) | ||
} | ||
|
||
var endpoint string | ||
for !portFound { | ||
endpoint = findAvailableAddress(t, network) | ||
_, port, err := net.SplitHostPort(endpoint) | ||
require.NoError(t, err) | ||
portFound = true | ||
if runtime.GOOS == "windows" { | ||
for _, pair := range exclusions { | ||
if port >= pair.first && port <= pair.last { | ||
portFound = false | ||
break | ||
} | ||
} | ||
} | ||
} | ||
|
||
return endpoint | ||
} | ||
|
||
func findAvailableAddress(t testing.TB, network string) string { | ||
switch network { | ||
// net.Listen supported network strings | ||
case "tcp", "tcp4", "tcp6", "unix", "unixpacket": | ||
ln, err := net.Listen(network, "localhost:0") | ||
require.NoError(t, err, "Failed to get a free local port") | ||
// There is a possible race if something else takes this same port before | ||
// the test uses it, however, that is unlikely in practice. | ||
defer func() { | ||
assert.NoError(t, ln.Close()) | ||
}() | ||
return ln.Addr().String() | ||
// net.ListenPacket supported network strings | ||
case "udp", "udp4", "udp6", "unixgram": | ||
ln, err := net.ListenPacket(network, "localhost:0") | ||
require.NoError(t, err, "Failed to get a free local port") | ||
// There is a possible race if something else takes this same port before | ||
// the test uses it, however, that is unlikely in practice. | ||
defer func() { | ||
assert.NoError(t, ln.Close()) | ||
}() | ||
return ln.LocalAddr().String() | ||
} | ||
return "" | ||
} | ||
|
||
// Get excluded ports on Windows from the command: netsh interface ipv4 show excludedportrange protocol=tcp | ||
func getExclusionsList(t testing.TB) []portpair { | ||
cmdTCP := exec.Command("netsh", "interface", "ipv4", "show", "excludedportrange", "protocol=tcp") | ||
outputTCP, errTCP := cmdTCP.CombinedOutput() | ||
require.NoError(t, errTCP) | ||
exclusions := createExclusionsList(t, string(outputTCP)) | ||
|
||
cmdUDP := exec.Command("netsh", "interface", "ipv4", "show", "excludedportrange", "protocol=udp") | ||
outputUDP, errUDP := cmdUDP.CombinedOutput() | ||
require.NoError(t, errUDP) | ||
exclusions = append(exclusions, createExclusionsList(t, string(outputUDP))...) | ||
|
||
return exclusions | ||
} | ||
|
||
func createExclusionsList(t testing.TB, exclusionsText string) []portpair { | ||
var exclusions []portpair | ||
|
||
parts := strings.Split(exclusionsText, "--------") | ||
require.Equal(t, len(parts), 3) | ||
portsText := strings.Split(parts[2], "*") | ||
require.Greater(t, len(portsText), 1) // original text may have a suffix like " - Administered port exclusions." | ||
lines := strings.Split(portsText[0], "\n") | ||
for _, line := range lines { | ||
if strings.TrimSpace(line) != "" { | ||
entries := strings.Fields(strings.TrimSpace(line)) | ||
require.Equal(t, len(entries), 2) | ||
pair := portpair{entries[0], entries[1]} | ||
exclusions = append(exclusions, pair) | ||
} | ||
} | ||
return exclusions | ||
} | ||
|
||
// Force the state of feature gate for a test | ||
// usage: defer SetFeatureGateForTest("gateName", true)() | ||
func SetFeatureGateForTest(t testing.TB, gate *featuregate.Gate, enabled bool) func() { | ||
originalValue := gate.IsEnabled() | ||
require.NoError(t, featuregate.GlobalRegistry().Set(gate.ID(), enabled)) | ||
return func() { | ||
require.NoError(t, featuregate.GlobalRegistry().Set(gate.ID(), originalValue)) | ||
} | ||
} | ||
|
||
func GetAvailablePort(t testing.TB) int { | ||
endpoint := GetAvailableLocalAddress(t) | ||
_, port, err := net.SplitHostPort(endpoint) | ||
require.NoError(t, err) | ||
|
||
portInt, err := strconv.Atoi(port) | ||
require.NoError(t, err) | ||
|
||
return portInt | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters