* ci(bazel): nogo replaces go vet and golangci-lint (ga-96smfk.18) Lint and vet now run as nogo inside the Bazel build, so they gate wherever the Bazel lanes execute (rbe-west), and the golangci-lint and go vet jobs leave pr.yml and main.yml. - //tools/nogo (own go.mod, joined to beads' for go_deps only through gazelle.go.work): go test's vet checks (cmd/go's defaultVetFlags, the set scripts/ci/go-test-vet.sh ran) plus the eight linters .golangci.yml enables, each a wrapper over the library version golangci-lint v2.10.1 uses (depguard, errcheck, forbidigo, gosec, misspell, sloglint, unconvert, unparam). - .golangci.yml stays the single lint policy: Bazel embeds it and tools/nogo/internal/golangci applies it as golangci-lint does (enabled set, settings, run.tests: false as library-unit-only analysis, lax generated-file detection, path/path-except/text exclusion rules), with strict decoding so an unimplemented key fails the build. - gascity's rules_go //nolint-range patch and x/tools ctrlflow patch, byte-identical. - Windows/darwin: cross-configured, not a golangci leg. The pure lane's release cross-compile (//tools/bazel:release_cross, every library and binary for all eight release platforms, cgo off) runs nogo on every compile, a superset of golangci-lint's windows and darwin legs. scripts/pr-lint (make ci-pr-lint, bd preflight) runs `bazel build --config=nogo //...` plus release_cross for windows_amd64,darwin_arm64. release_cross skips tools/nogo, a separate Go module `go build ./...` never compiled. - pr.yml: scripts-go-checks (vet) and pr-lint-wrapper (x3) are gone, with their ci-gate entries; main.yml: pr-lint-wrapper (x6) and go-vet-cache. scripts/ci/go-test-vet.sh and install-golangci-lint.sh are deleted. - pre-commit hook and .pre-commit-config.yaml run `make lint-changed LINT_CHANGED_SCOPE=staged` (nogo over the staged packages); Makefile adds lint, vet and lint-changed. pr-lint.sh no longer runs gofmt (//scripts/repochecks:fmt_test owns it; ga-96smfk.41). - internal/testutil/integration: five gosec findings in integration-tagged library files golangci-lint never built, now linted by the integration lanes, carry reasoned //nolint directives. Parity: with the exclusion rules removed, golangci-lint and nogo report the same 3,916 findings natively and 3,859 each for windows and darwin (file, line, linter, text); with .golangci.yml as committed both report none. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * ci(bazel): equivalence.py leaves nested Go modules' tests out of go test's set tools/nogo is its own Go module, so `go test ./...` in the root never runs its tests; Bazel's unit lane does. Its packages are neither expected nor extra. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
374 lines
11 KiB
Go
374 lines
11 KiB
Go
//go:build integration && !windows
|
|
|
|
// Package integration provides shared test infrastructure for multi-process
|
|
// integration tests (GH#2763). It offers process lifecycle management,
|
|
// subprocess orchestration, and environment filtering for tests that validate
|
|
// real dolt server behavior under production-like concurrency.
|
|
package integration
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/steveyegge/beads/internal/testutil/bazeltest"
|
|
)
|
|
|
|
// ProcessRegistry tracks spawned processes and ensures cleanup via t.Cleanup.
|
|
// Each registered process gets SIGTERM → 5s grace → SIGKILL escalation.
|
|
type ProcessRegistry struct {
|
|
mu sync.Mutex
|
|
procs map[int]*os.Process
|
|
t *testing.T
|
|
}
|
|
|
|
// NewProcessRegistry creates a registry that automatically kills all tracked
|
|
// processes when the test completes.
|
|
func NewProcessRegistry(t *testing.T) *ProcessRegistry {
|
|
t.Helper()
|
|
r := &ProcessRegistry{
|
|
procs: make(map[int]*os.Process),
|
|
t: t,
|
|
}
|
|
t.Cleanup(r.killAll)
|
|
return r
|
|
}
|
|
|
|
// Register adds a process to the registry for lifecycle tracking.
|
|
func (r *ProcessRegistry) Register(p *os.Process) {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
r.procs[p.Pid] = p
|
|
}
|
|
|
|
// Deregister removes a process from the registry (e.g., after clean Stop).
|
|
func (r *ProcessRegistry) Deregister(pid int) {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
delete(r.procs, pid)
|
|
}
|
|
|
|
// killAll sends SIGTERM, waits 5s, then SIGKILL to all tracked processes.
|
|
func (r *ProcessRegistry) killAll() {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
for pid, p := range r.procs {
|
|
r.t.Logf("ProcessRegistry: cleaning up PID %d", pid)
|
|
if err := p.Signal(syscall.SIGTERM); err != nil {
|
|
// Process may already be dead.
|
|
continue
|
|
}
|
|
done := make(chan struct{})
|
|
go func() {
|
|
// Wait() only works for child processes. If it fails (ECHILD),
|
|
// fall back to polling IsProcessAlive.
|
|
if _, err := p.Wait(); err != nil {
|
|
for i := 0; i < 50; i++ {
|
|
if !IsProcessAlive(pid) {
|
|
break
|
|
}
|
|
time.Sleep(100 * time.Millisecond)
|
|
}
|
|
}
|
|
close(done)
|
|
}()
|
|
select {
|
|
case <-done:
|
|
// Clean exit after SIGTERM.
|
|
case <-time.After(5 * time.Second):
|
|
r.t.Logf("ProcessRegistry: PID %d did not exit after SIGTERM, sending SIGKILL", pid)
|
|
_ = p.Signal(syscall.SIGKILL)
|
|
<-done
|
|
}
|
|
}
|
|
r.procs = make(map[int]*os.Process)
|
|
}
|
|
|
|
// SubprocessRunner builds a test binary once and spawns subprocesses from it.
|
|
// The binary is compiled with the integration build tag.
|
|
type SubprocessRunner struct {
|
|
once sync.Once
|
|
testBin string
|
|
buildErr error
|
|
modRoot string
|
|
pkg string
|
|
tags string
|
|
}
|
|
|
|
// NewSubprocessRunner creates a runner that will compile the given package.
|
|
// modRoot is the Go module root, pkg is the package path (e.g., "./internal/storage/dolt/").
|
|
func NewSubprocessRunner(modRoot, pkg string) *SubprocessRunner {
|
|
return &SubprocessRunner{
|
|
modRoot: modRoot,
|
|
pkg: pkg,
|
|
tags: "integration",
|
|
}
|
|
}
|
|
|
|
// GoBinaryEnv names the variable that points SubprocessRunner at a Go
|
|
// command other than the `go` on PATH. Under Bazel there is no toolchain on
|
|
// PATH, so a go_test that builds through SubprocessRunner declares the pinned
|
|
// SDK as data and sets it from its BUILD file:
|
|
//
|
|
// data = ["@go_sdk//:bin/go", "@go_sdk//:tools", "//:go.mod", "//:go.sum"],
|
|
// env = {"BEADS_TEST_GO_BINARY": "$(rlocationpath @go_sdk//:bin/go)"},
|
|
const GoBinaryEnv = "BEADS_TEST_GO_BINARY"
|
|
|
|
// goBinary returns the go command to build with: "go" from PATH unless
|
|
// BEADS_TEST_GO_BINARY is set, which is then resolved like any other runfile.
|
|
func goBinary() (string, error) {
|
|
if os.Getenv(GoBinaryEnv) == "" {
|
|
return "go", nil
|
|
}
|
|
return bazeltest.RunfileEnv(GoBinaryEnv)
|
|
}
|
|
|
|
// PrebuiltTestBinaryEnv names a variable that hands SubprocessRunner an
|
|
// already-built test binary of the package it would compile, instead of
|
|
// running `go test -c`. Under Bazel no action has the module's source tree
|
|
// and module cache that `go test -c` needs, but the test binary that is
|
|
// running is that package's test binary, integration-tagged, so the target
|
|
// passes it (resolved like any other runfile):
|
|
//
|
|
// env = {"BEADS_TEST_SUBPROCESS_BINARY": "$(rlocationpath <the go_test>)"},
|
|
const PrebuiltTestBinaryEnv = "BEADS_TEST_SUBPROCESS_BINARY"
|
|
|
|
// Build compiles the test binary (once). Returns the path to the binary.
|
|
// With BEADS_TEST_SUBPROCESS_BINARY set it compiles nothing and returns that
|
|
// binary.
|
|
func (r *SubprocessRunner) Build(t *testing.T) string {
|
|
t.Helper()
|
|
r.once.Do(func() {
|
|
if os.Getenv(PrebuiltTestBinaryEnv) != "" {
|
|
bin, err := bazeltest.RunfileEnv(PrebuiltTestBinaryEnv)
|
|
if err != nil {
|
|
r.buildErr = fmt.Errorf("prebuilt test binary: %w", err)
|
|
return
|
|
}
|
|
t.Logf("SubprocessRunner: using prebuilt test binary %s (%s) for %s", bin, PrebuiltTestBinaryEnv, r.pkg)
|
|
r.testBin = bin
|
|
return
|
|
}
|
|
r.testBin = filepath.Join(t.TempDir(), "integration.test")
|
|
t.Logf("SubprocessRunner: building test binary: go test -tags %s -c -o %s %s", r.tags, r.testBin, r.pkg)
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
|
defer cancel()
|
|
goBin, err := goBinary()
|
|
if err != nil {
|
|
r.buildErr = fmt.Errorf("failed to build test binary: %w", err)
|
|
return
|
|
}
|
|
build := exec.CommandContext(ctx, goBin, "test",
|
|
"-tags", r.tags,
|
|
"-c",
|
|
"-o", r.testBin,
|
|
r.pkg,
|
|
)
|
|
build.Dir = r.modRoot
|
|
build.Env = append(os.Environ(), "CGO_ENABLED=1")
|
|
var stdout, stderr bytes.Buffer
|
|
build.Stdout = &stdout
|
|
build.Stderr = &stderr
|
|
if err := build.Run(); err != nil {
|
|
r.buildErr = fmt.Errorf("failed to build test binary: %w\nstderr: %s\nstdout: %s", err, stderr.String(), stdout.String())
|
|
return
|
|
}
|
|
if err := os.Chmod(r.testBin, 0700); err != nil { //nolint:gosec // G302: the test binary must be executable
|
|
r.buildErr = fmt.Errorf("failed to chmod test binary: %w", err)
|
|
}
|
|
})
|
|
if r.buildErr != nil {
|
|
t.Fatalf("SubprocessRunner: %v", r.buildErr)
|
|
}
|
|
return r.testBin
|
|
}
|
|
|
|
// Spawn starts a subprocess running the given test helper function.
|
|
// The sentinel env var (e.g., BEADS_SCHEMA_INIT_HELPER=1) triggers the helper.
|
|
// Extra env vars are merged with the filtered base environment.
|
|
// Uses process groups for reliable cleanup.
|
|
func (r *SubprocessRunner) Spawn(ctx context.Context, t *testing.T, helperTest string, env map[string]string) *exec.Cmd {
|
|
t.Helper()
|
|
bin := r.Build(t)
|
|
|
|
// Per-subprocess timeout (30s default, or context deadline).
|
|
subCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
|
t.Cleanup(cancel)
|
|
|
|
cmd := exec.CommandContext(subCtx, bin, "-test.run=^"+helperTest+"$", "-test.v")
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
|
cmd.Env = FilterEnv(os.Environ())
|
|
for k, v := range env {
|
|
cmd.Env = append(cmd.Env, k+"="+v)
|
|
}
|
|
return cmd
|
|
}
|
|
|
|
// safeEnvKeys is an allowlist of environment variable prefixes that are safe
|
|
// to pass to test subprocesses.
|
|
var safeEnvKeys = []string{
|
|
"PATH=",
|
|
"HOME=",
|
|
"TMPDIR=",
|
|
"LANG=",
|
|
"USER=",
|
|
"SHELL=",
|
|
"GOPATH=",
|
|
"GOROOT=",
|
|
"GOMODCACHE=",
|
|
"GOCACHE=",
|
|
"GOFLAGS=",
|
|
"CGO_ENABLED=",
|
|
"CGO_CFLAGS=",
|
|
"CGO_LDFLAGS=",
|
|
"CGO_CPPFLAGS=",
|
|
"CC=",
|
|
"CXX=",
|
|
"PKG_CONFIG_PATH=",
|
|
"LD_LIBRARY_PATH=",
|
|
"DYLD_LIBRARY_PATH=",
|
|
"BEADS_", // All BEADS_ test control vars.
|
|
"DOLT_", // Dolt configuration.
|
|
"TZ=",
|
|
"LC_",
|
|
"XDG_",
|
|
"TERM=",
|
|
}
|
|
|
|
// FilterEnv returns a filtered copy of the environment, keeping only safe
|
|
// variables. This prevents leaking credentials to test subprocesses.
|
|
func FilterEnv(environ []string) []string {
|
|
var filtered []string
|
|
for _, e := range environ {
|
|
for _, prefix := range safeEnvKeys {
|
|
if strings.HasPrefix(e, prefix) {
|
|
filtered = append(filtered, e)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return filtered
|
|
}
|
|
|
|
// RequireDolt skips the test if the dolt binary is not available on PATH
|
|
// or if BEADS_TEST_SKIP contains "dolt".
|
|
func RequireDolt(t *testing.T) string {
|
|
t.Helper()
|
|
if skip := os.Getenv("BEADS_TEST_SKIP"); skip != "" {
|
|
for _, s := range strings.Split(skip, ",") {
|
|
if strings.TrimSpace(s) == "dolt" {
|
|
t.Skip("skipping: Dolt tests skipped (BEADS_TEST_SKIP=dolt)")
|
|
}
|
|
}
|
|
}
|
|
doltPath, err := exec.LookPath("dolt")
|
|
if err != nil {
|
|
t.Skip("dolt binary not found on PATH, skipping integration test")
|
|
}
|
|
return doltPath
|
|
}
|
|
|
|
// InitDoltDir initializes a fresh dolt data directory in dir.
|
|
// Returns the path to the dolt database directory.
|
|
func InitDoltDir(t *testing.T, dir string) string {
|
|
t.Helper()
|
|
RequireDolt(t)
|
|
|
|
doltDir := filepath.Join(dir, "dolt")
|
|
if err := os.MkdirAll(doltDir, 0700); err != nil {
|
|
t.Fatalf("failed to create dolt dir: %v", err)
|
|
}
|
|
|
|
cmd := exec.Command("dolt", "init")
|
|
cmd.Dir = doltDir
|
|
cmd.Env = append(os.Environ(),
|
|
"DOLT_ROOT_PATH="+dir,
|
|
"HOME="+dir,
|
|
)
|
|
var stderr bytes.Buffer
|
|
cmd.Stderr = &stderr
|
|
if err := cmd.Run(); err != nil {
|
|
t.Fatalf("dolt init failed: %v\nstderr: %s", err, stderr.String())
|
|
}
|
|
return doltDir
|
|
}
|
|
|
|
// ModuleRoot finds the Go module root by searching for go.mod.
|
|
func ModuleRoot(t *testing.T) string {
|
|
t.Helper()
|
|
dir, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("failed to get working directory: %v", err)
|
|
}
|
|
for {
|
|
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
|
|
return dir
|
|
}
|
|
parent := filepath.Dir(dir)
|
|
if parent == dir {
|
|
t.Fatal("could not find go.mod in any parent directory")
|
|
}
|
|
dir = parent
|
|
}
|
|
}
|
|
|
|
// IsProcessAlive checks if a process with the given PID exists and is alive.
|
|
func IsProcessAlive(pid int) bool {
|
|
p, err := os.FindProcess(pid)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return p.Signal(syscall.Signal(0)) == nil
|
|
}
|
|
|
|
// WaitForPort polls until a TCP connection to host:port succeeds or timeout expires.
|
|
func WaitForPort(t *testing.T, host string, port int, timeout time.Duration) {
|
|
t.Helper()
|
|
deadline := time.Now().Add(timeout)
|
|
addr := net.JoinHostPort(host, strconv.Itoa(port))
|
|
for time.Now().Before(deadline) {
|
|
conn, err := net.DialTimeout("tcp", addr, 500*time.Millisecond)
|
|
if err == nil {
|
|
_ = conn.Close()
|
|
return
|
|
}
|
|
time.Sleep(200 * time.Millisecond)
|
|
}
|
|
t.Fatalf("WaitForPort: %s not reachable after %v", addr, timeout)
|
|
}
|
|
|
|
// ReadPIDFile reads and parses a PID file, returning 0 if missing or corrupt.
|
|
func ReadPIDFile(path string) int {
|
|
data, err := os.ReadFile(path) //nolint:gosec // G304: a state file path the harness itself derived
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
pid, err := strconv.Atoi(strings.TrimSpace(string(data)))
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
return pid
|
|
}
|
|
|
|
// ReadPortFile reads and parses a port file, returning 0 if missing or corrupt.
|
|
func ReadPortFile(path string) int {
|
|
data, err := os.ReadFile(path) //nolint:gosec // G304: a state file path the harness itself derived
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
port, err := strconv.Atoi(strings.TrimSpace(string(data)))
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
return port
|
|
}
|