beads/internal/testutil/testdoltcommon.go
2026-05-14 16:56:55 -07:00

54 lines
1.6 KiB
Go

package testutil
import (
"fmt"
"net"
"os"
"os/exec"
"testing"
"time"
)
// DoltDockerImage is the Docker image used for Dolt test containers.
const DoltDockerImage = "dolthub/dolt-sql-server:1.88.1"
// RequireDoltBinary ensures the `dolt` CLI binary is available. The test is
// skipped locally when dolt is missing but fatally fails under GitHub Actions
// (GITHUB_ACTIONS=true). CI is expected to install dolt; a missing binary
// there means the workflow is broken, not that the test should be skipped.
func RequireDoltBinary(t *testing.T) {
t.Helper()
if _, err := exec.LookPath("dolt"); err != nil {
if os.Getenv("GITHUB_ACTIONS") == "true" {
t.Fatalf("dolt binary missing under GITHUB_ACTIONS: %v — the CI workflow must install dolt (see .github/workflows/ci.yml)", err)
}
t.Skipf("dolt binary not found: %v", err)
}
}
// FindFreePort finds an available TCP port by binding to :0.
func FindFreePort() (int, error) {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return 0, err
}
port := l.Addr().(*net.TCPAddr).Port
_ = l.Close()
return port, nil
}
// WaitForServer polls until the server accepts TCP connections on the given port.
func WaitForServer(port int, timeout time.Duration) bool {
deadline := time.Now().Add(timeout)
addr := fmt.Sprintf("127.0.0.1:%d", port)
for time.Now().Before(deadline) {
// #nosec G704 -- addr is always loopback (127.0.0.1) with a test-selected local port.
conn, err := net.DialTimeout("tcp", addr, 500*time.Millisecond)
if err == nil {
_ = conn.Close()
return true
}
time.Sleep(200 * time.Millisecond)
}
return false
}