Debian Codegraph @

internal/testutil/testutil_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package testutil
5
6 import (
7 "net"
8 "testing"
9
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 )
13
14 func TestGetAvailableLocalAddress(t *testing.T) {
15 endpoint := GetAvailableLocalAddress(t)
16
17 // Endpoint should be free.
18 ln0, err := net.Listen("tcp", endpoint)
19 require.NoError(t, err)
20 require.NotNil(t, ln0)
21 t.Cleanup(func() {
22 assert.NoError(t, ln0.Close())
23 })
24
25 // Ensure that the endpoint wasn't something like ":0" by checking that a
26 // second listener will fail.
27 ln1, err := net.Listen("tcp", endpoint)
28 require.Error(t, err)
29 require.Nil(t, ln1)
30 }
31
32 func TestGetAvailableLocalIpv6Address(t *testing.T) {
33 endpoint := GetAvailableLocalIPv6Address(t)
34
35 // Endpoint should be free.
36 ln0, err := net.Listen("tcp", endpoint)
37 require.NoError(t, err)
38 require.NotNil(t, ln0)
39 t.Cleanup(func() {
40 assert.NoError(t, ln0.Close())
41 })
42
43 // Ensure that the endpoint wasn't something like ":0" by checking that a
44 // second listener will fail.
45 ln1, err := net.Listen("tcp", endpoint)
46 require.Error(t, err)
47 require.Nil(t, ln1)
48 }
49
50 func TestCreateExclusionsList(t *testing.T) {
51 // Test two examples of typical output from "netsh interface ipv4 show excludedportrange protocol=tcp"
52 emptyExclusionsText := `
53
54 Protocol tcp Port Exclusion Ranges
55
56 Start Port End Port
57 ---------- --------
58
59 * - Administered port exclusions.`
60
61 exclusionsText := `
62
63 Start Port End Port
64 ---------- --------
65 49697 49796
66 49797 49896
67
68 * - Administered port exclusions.
69 `
70 exclusions := createExclusionsList(t, exclusionsText)
71 require.Len(t, exclusions, 2)
72
73 emptyExclusions := createExclusionsList(t, emptyExclusionsText)
74 require.Empty(t, emptyExclusions)
75 }