File size: 713 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package harness

import (
	"net"
	"strings"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestFindOpenPorts(t *testing.T) {
	// This test is more of a sanity check than anything else.
	//
	// Finding an open port is dependent on the system's network state, so it's
	// difficult to write a deterministic test for it. This test is just to
	// ensure that the function doesn't panic and returns a valid port.
	ports, err := findOpenPorts(1)
	if err != nil {
		t.Fatal(err)
	}
	if !strings.Contains(ports[0], ":") {
		t.Fatalf("expected port to contain colon, got %s", ports[0])
	}

	// Ensure the port is actually open
	l, err := net.Listen("tcp", ports[0])
	require.NoError(t, err)
	defer l.Close()
}