beads/internal/testutil/testdoltcommon.go
Julian Knutsen 671715bb8b
test(testutil): opt-in local dolt sql-server backend for Dolt-backed tests (#6891)
* test(testutil): local dolt sql-server backend behind the Dolt test server API

Add a second backend to internal/testutil behind the unchanged container
API (EnsureDoltContainerForTestMain, RequireDoltContainer,
StartIsolatedDoltContainer[Handle], NewContainerProvider,
RestartSharedDoltContainer, DoltContainerCrashed, IsolatedDoltContainer
Stop/Start/CurrentPort/Exec): the test process spawns `dolt sql-server`
from the pinned dolt CLI instead of a dolthub/dolt-sql-server container.

- BEADS_TEST_DOLT_SERVER=local|container selects it; unset keeps plain
  `go test` on the container (no behavior change) and makes `bazel test`
  use the local backend. Other values fail instead of guessing.
- The CLI (BEADS_TEST_DOLT_BINARY, else PATH) must be the image's version.
- Provisioned like testcontainers' dolt module: root@% (DOLT_ROOT_HOST=%),
  database beads_test, user test/test. Clean env, TZ=UTC, GOMAXPROCS=4,
  own dolt global config with metrics and the version check disabled.
- Loopback port picked here (dolt rejects -P 0), never 3307, retried when
  another process wins the port; readiness requires the server's own
  "Server ready" line plus a bounded ping, so a foreign listener on a
  contested port cannot pass for it.
- State lives under the TMPDIR captured at package init, outside every
  PinSuiteTempRoot root, so suite leak sweeps never see it. The server
  stays in the test's process group (bazel's group kill reaps it) with
  Pdeathsig on Linux.
- BEADS_TEST_REQUIRE_DOLT_CONTAINER=1 now also makes every TestMain that
  only warned about an unavailable server exit 1
  (DoltUnavailableForTestMain), plus the shared proxied harness.
- dolt_benchmark_purge_test finds .dolt_dropped_databases relative to
  Exec's working directory (the data dir in both backends).
- TestDoltServerFingerprint checks each available backend against one
  checked-in fingerprint (users, grants, databases, server variables);
  local-backend tests cover the bind-race retry, 3307, crash detection,
  Stop/Start, Exec cwd, state outside the suite root, and teardown.
- Bazel: the requires-docker variants pin BEADS_TEST_DOLT_SERVER=container
  so the docker lane stays the container A/B control; the test wrapper's
  dolt global config disables metrics and the version check.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(testutil): select the local Dolt test server only when asked for

Under Bazel an unset BEADS_TEST_DOLT_SERVER used to mean the local
backend, so every non-ci `bazel test` started servers for Dolt suites no
lane runs with one (//internal/storage/dolt:dolt_test went from a 16 s
skip to a timeout). Unset now means container everywhere: in a Bazel
action without docker that is the pre-existing warn+skip, and only a
target's env or --test_env=BEADS_TEST_DOLT_SERVER=local starts local
servers. Plain `go test` is unchanged.

The docker variants keep their container pin: a rule env beats
--test_env, so a local-backend invocation cannot flip the docker lane.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(testutil): harden the local Dolt test server's host footprint

- DOLT_DISABLE_EVENT_FLUSH=1 for every dolt the backend runs (and in the
  Bazel test wrapper): the metrics/versioncheck keys stop network calls
  but not the detached `dolt send-metrics` child each command forks on
  exit, which re-created the version probe's removed HOME (one leaked
  bdt-doltver-* per test process) and raced terminate's RemoveAll.
- Every bdt-dolt-*/bdt-doltver-* root records its owner PID, and the
  first use of the backend in a process removes this user's roots whose
  owner is dead (the SweepDeadSuiteRoots rule; a copy, since doltserver's
  tests import testutil). A panicking, timed-out or killed test binary,
  or a package whose TestMain never terminates the singleton, no longer
  leaks a 40-100 MB state dir per run.
- secure_file_priv is the state root's sfp/ directory, set through a
  per-launch --config file (dolt ignores other flags with --config): a
  local server runs as the test user on the host with a passwordless
  root@%, so LOAD_FILE / INTO OUTFILE must not reach the user's files.
  The fingerprint treats that path as the container's "".
- Fingerprint: character_set_server, collation_server,
  max_allowed_packet, transaction_isolation, lower_case_table_names,
  lower_case_file_system (compared on Linux only: APFS reports ON),
  wait_timeout, innodb_lock_wait_timeout.
- The local backend's own tests skip off Linux (no Pdeathsig, so a
  killed test binary would orphan servers on the macOS runners) unless
  BEADS_TEST_DOLT_SERVER=local asks for them.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* ci: compare the container and local Dolt test servers in the domain+uow job

TestDoltServerFingerprint's container half never ran in CI: only the
test-domain-uow job has both the pulled image and the pinned dolt CLI,
and it never ran ./internal/testutil. Run the fingerprint there (with
BEADS_TEST_REQUIRE_DOLT_CONTAINER=1 so the container half cannot skip)
so an image bump that drifts the container side fails in the PR.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-28 04:06:49 -07:00

118 lines
4.3 KiB
Go

package testutil
import (
"fmt"
"net"
"os"
"os/exec"
"strings"
"testing"
"time"
"github.com/steveyegge/beads/internal/testutil/bazeltest"
)
// DoltDockerImage is the Docker image used for Dolt test containers.
const DoltDockerImage = "dolthub/dolt-sql-server:2.2.0"
// EnvRequireDoltContainer, set to "1", turns an unavailable Dolt test server
// (either backend, see BEADS_TEST_DOLT_SERVER) into a failure instead of a
// skip: in RequireDoltContainer and StartIsolatedDoltContainer(Handle), and in
// every TestMain through DoltUnavailableForTestMain. Lanes that exist to run
// the Dolt server suites set it so they cannot pass green having run nothing
// (and, under Bazel, have that vacuous pass cached and shared).
const EnvRequireDoltContainer = "BEADS_TEST_REQUIRE_DOLT_CONTAINER"
// DoltUnavailableForTestMain handles an EnsureDoltContainerForTestMain error
// in a TestMain and reports whether the TestMain must exit non-zero. By
// default it prints the historical warning and returns false, so the package
// runs and its Dolt tests skip. With BEADS_TEST_REQUIRE_DOLT_CONTAINER=1 it
// prints a FATAL line and returns true.
func DoltUnavailableForTestMain(err error) bool {
if os.Getenv(EnvRequireDoltContainer) == "1" {
fmt.Fprintf(os.Stderr, "FATAL: %v, but %s=1; this lane must not skip its Dolt tests\n", err, EnvRequireDoltContainer)
return true
}
fmt.Fprintf(os.Stderr, "WARN: %v, skipping Dolt tests\n", err)
return false
}
// RequireDoltBinary ensures the `dolt` CLI binary is available, and honors
// BEADS_TEST_SKIP=dolt for tests that also depend on the shared
// containerized Dolt SQL server. The test is skipped locally when dolt is
// missing but fatally fails under GitHub Actions (GITHUB_ACTIONS=true) and
// under bazel test, which always provides the pinned dolt. 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 hasTestSkipForDoltBinary("dolt") {
t.Skip("skipping: Dolt tests skipped (BEADS_TEST_SKIP=dolt)")
}
requireDoltBinaryPresent(t)
}
// RequireDoltCLIOnly ensures the `dolt` CLI binary is available, WITHOUT
// honoring BEADS_TEST_SKIP=dolt. Use this for tests that shell out to the
// local `dolt` CLI directly and have no dependency on the shared
// containerized Dolt SQL server — BEADS_TEST_SKIP=dolt is a blanket switch
// meant to exclude tests that need that server, so it must not also skip
// tests that only need the CLI binary.
func RequireDoltCLIOnly(t *testing.T) {
t.Helper()
requireDoltBinaryPresent(t)
}
// requireDoltBinaryPresent checks for the `dolt` CLI binary and fails or
// skips as appropriate. See RequireDoltBinary and RequireDoltCLIOnly.
func requireDoltBinaryPresent(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)
}
// Under Bazel the test wrapper (tools/bazel/test_env.sh) puts the
// pinned dolt from runfiles first on PATH. A skip here would be a
// vacuous PASS that the shared remote cache then serves to everyone.
if bazeltest.IsBazel() {
t.Fatalf("dolt binary missing under bazel test: %v — tools/bazel/test_env.sh must put //tools/bazel:dolt on PATH", err)
}
t.Skipf("dolt binary not found: %v", err)
}
}
func hasTestSkipForDoltBinary(service string) bool {
for _, s := range strings.Split(os.Getenv("BEADS_TEST_SKIP"), ",") {
if strings.TrimSpace(s) == service {
return true
}
}
return false
}
// 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
}