Skip to content

Commit d5b3d2a

Browse files
committed
e2e: use the first available port instead of a fixed one
Signed-off-by: Miguel Molina <[email protected]>
1 parent 91f290d commit d5b3d2a

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

e2e/e2e_test.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"database/sql"
55
"flag"
66
"fmt"
7+
"net"
78
"os"
89
"os/exec"
910
"testing"
@@ -18,6 +19,8 @@ var (
1819
bin = flag.String("gitbase-bin", "", "path to the gitbase binary to test")
1920
repos = flag.String("gitbase-repos", "", "path to the gitbase repos to test")
2021
version = flag.String("gitbase-version", "", "(optional) version of the binary")
22+
23+
port int
2124
)
2225

2326
func TestMain(m *testing.M) {
@@ -42,13 +45,19 @@ func TestMain(m *testing.M) {
4245
}
4346
}
4447

48+
port, err = findPort()
49+
if err != nil {
50+
fmt.Println("unable to find an available port: ", err)
51+
os.Exit(1)
52+
}
53+
4554
var done = make(chan error)
4655
cmd := exec.Command(
4756
path,
4857
"server",
4958
"--directories="+*repos,
5059
"--host=127.0.0.1",
51-
"--port=3308",
60+
fmt.Sprintf("--port=%d", port),
5261
"--index=indexes",
5362
)
5463

@@ -84,7 +93,7 @@ func TestMain(m *testing.M) {
8493
}
8594

8695
func connect(t *testing.T) (*sql.DB, func()) {
87-
db, err := sql.Open("mysql", "root:@tcp(127.0.0.1:3308)/gitbase")
96+
db, err := sql.Open("mysql", fmt.Sprintf("root:@tcp(127.0.0.1:%d)/gitbase", port))
8897
if err != nil {
8998
t.Errorf("unexpected error connecting to gitbase: %s", err)
9099
}
@@ -94,6 +103,23 @@ func connect(t *testing.T) (*sql.DB, func()) {
94103
}
95104
}
96105

106+
func findPort() (int, error) {
107+
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
108+
if err != nil {
109+
return 0, err
110+
}
111+
112+
l, err := net.ListenTCP("tcp", addr)
113+
if err != nil {
114+
return 0, err
115+
}
116+
117+
port := l.Addr().(*net.TCPAddr).Port
118+
_ = l.Close()
119+
120+
return port, nil
121+
}
122+
97123
func TestVersion(t *testing.T) {
98124
if *version == "" {
99125
t.Skip("no version provided, skipping")

0 commit comments

Comments
 (0)