* fix(dbproxy): prove the managed dolt owns its port; move a taken Beads-chosen port
The managed-local backend's dolt port comes from a bind-and-close
(proxy.PickFreePort) when bd writes the generated config. The dolt
sql-server binds it only later, after the proxy child has run dolt
config and init and booted the server. If another process takes the port
in that gap, dolt logs "Port N already in use." and exits. But
DoltServer.waitReady only dialed the port, so it accepted the other
process's listener as ready. The proxy then published proxy.pid, its
backend health check saw dolt gone, and it shut down. bd init's first
ping got "unexpected EOF" and every retry after that got "connection
refused". Under parallel tests this hit any test that starts its own
managed backend.
Readiness now requires proof that the process Start launched owns the
port:
- DoltServer sends the child's stdout/stderr through a watcher that
forwards to the server log. A dial counts only after dolt's own
"Server ready. Accepting connections." line has appeared on that
output. That line is info level, and every generated config uses
info. A config with a quieter log_level has no such signal and keeps
the dial-only check.
- A child that exits first is reported as the new server.ErrPortInUse
when its output said its port was taken.
Recovery: DoltServer.SetPortRepicker lets Start move the config to a
fresh port, re-read it, and relaunch, up to 5 ports. dolt config/init
run only on the first attempt. bd's repicker,
repickManagedProxiedServerPort, needs to tell a port Beads chose from
one an operator set, so generated configs now record the chosen port
on a "# beads-chosen-listener-port: N" line after the managed marker.
The repicker moves the port only when the file is Beads-generated,
carries that record, and its listener.port is still the recorded port.
Otherwise the port is treated as pinned and Start fails with
ErrPortInUse. That includes configs generated before this change. It
rewrites only listener.port and the record, through a yaml.Node so the
rest of the file is kept, and writes the file atomically.
db-proxy-child installs it for the local backend.
The proxy's waitForServerReady also re-checks Running after its dial.
Start has already proved ownership, and a running dolt holds its
listener, so only a backend that exited between the two checks could
let a dial reach another process.
Tests:
- server: a held port is not accepted as ready (ErrPortInUse; this
test fails on main), a repicker brings the server up on a new port
that the pid record and DSN both use, and a declining repicker ends
Start with ErrPortInUse.
- cmd/bd: the generated config records its port, a Beads-chosen port
moves while hand edits survive, and a hand-set port, a config
without the record, an operator config, and a stale in-use port are
all refused with the file left untouched.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(dbproxy): move a taken managed port via a runtime config, never config.yaml
This reworks cd00c5acb after review. That commit recovered from a taken
Beads-chosen port by rewriting the managed config.yaml. That broke the
file's own contract ("Never rewritten afterward; hand edits are
preserved", #4986), and #5024 had deferred that contract change to an
RFC.
dolt sql-server has no command-line way around the file: with --config,
-P/--port is silently ignored (checked against dolt 2.1.8 and 2.2.0, in
both argument orders). So on ErrPortInUse the proxy child now writes
proxy-child.runtime-config.yaml (server.RuntimeConfigFileName) in the
server root. That file is the operator config with only listener.port
moved to a free port. The child relaunches dolt from it and records the
effective port where runtime readers already look: the backend pid
record and the DoltServer's dial/DSN config. The runtime config is
removed when the server stops, when Start fails, and by the next Start.
config.yaml is never written.
- server.PortRepicker becomes server.PortConflictPolicy. It only decides
whether the port may move; the server does the move.
- bd's policy (managedPortConflictPolicy) allows a move when the file
carries the managed marker and either:
- has the port record and listener.port still equals it, or
- is byte-identical to the config bd generated before the record
existed, so existing unedited workspaces recover too.
- Everything else is pinned: operator configs, hand-set ports,
hand-edited legacy files, and configs with ${...} placeholders (a
runtime copy would put the expanded values on disk).
- The record line now says plainly that Beads runs on a free port
without editing the file, and that deleting the line or changing
listener.port pins the port. The record parser now requires the
digits to end at a space or line end and to be in 1..65535.
- ErrPortInUse messages say what to do: free port P, set listener.port
in <path> to a free port, or (for a generated file) delete it so
Beads regenerates it. The give-up message names the last port tried.
- A dolt that never logs the ready line is still not accepted. A
grace-period fallback would let a foreign listener through while dolt
is still starting. The timeout error now names the missing line, says
whether anything answered, and points at the escape hatch: at
log_level warning, readiness is dial-only.
- Port-conflict detection matches go-mysql-server's "Port host:port
already in use." and the Windows bind error, and requires the SQL port
in the kernel's bind error, so a metrics or MCP listener failure is
not taken for the SQL port.
- Spawn error paths reap with cmd.Wait(). cmd.WaitDelay is set, and a
comment notes that the backend's output is now a pipe into the proxy
child.
Tests:
- Output-watcher table tests: split, one-byte and CRLF writes, output
past the 4 KiB tail, the MCP ready line, port boundaries, the
go-mysql-server and Windows forms, another listener's bind error, the
unix-socket warning, and nil log.
- A real dolt at log_level warning starts through dial-only readiness.
- With a stand-in dolt (the test binary in fake mode, wired in
TestMain): Start gives up after maxStartPortAttempts, a missing ready
line is diagnosed, plus a positive control.
- With real dolt: a move keeps config.yaml byte-identical, runs from the
runtime config, and the runtime config is removed on Stop. A declining
policy and the no-policy remedy are covered.
- The bd policy's allow and pin cases.
- The managed-local lifecycle test's config/pid port check accepts the
runtime config.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(dbproxy): ask the port policy once, about the operator's port; copy raw config text
Re-review of the runtime-config rework found a regression. After the
first move, Start asked the PortConflictPolicy about the runtime port.
bd's policy re-reads config.yaml, whose listener.port is still the
operator's port, so it refused with "listener.port is A, not the in-use
port B". The "up to 5 ports" retry therefore never ran in production;
the fake-dolt test missed it because its policy always allowed.
Start now asks the policy once per Start, on the first conflict, about
the operator config's port. Ports Start picked itself are its own to move
again. Start also re-reads the operator config up front, so every Start
begins from that config's port.
The runtime config is now built from config.yaml's raw text
(servercfg.NewYamlConfig) instead of the env-interpolated
YamlConfigFromFile result. dolt interpolates the runtime file when it
reads it, so the old way expanded placeholders twice: "$$" escapes lost a
level and placeholder values landed on disk. The server reads the
runtime file back with interpolation for its own Dial/DSN view. Since
placeholders no longer expand on disk, bd's policy stops refusing
configs that contain them.
Tests:
- bd's real policy driving a real DoltServer with a stand-in dolt that
reports every port taken: Start makes five launches on five distinct
ports, starting with config.yaml's, then gives up. It is not refused
on the second move, and config.yaml stays unchanged.
- A frozen byte-for-byte literal of the config bd v1.3.0/v1.3.1
generates. The legacy render must equal it, and the policy must find
it eligible.
- The runtime config keeps placeholders and "$$" verbatim and is mode
0600, while the server dials the interpolated host.
- The give-up test asserts the policy is asked once, about the
operator's port.
- The missing-ready-line test timeout is raised to 30s for the slow
fake-dolt exec under race.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(dbproxy): classify a child that exits before its identity is captured
TestManagedPortConflictPolicy_ConsecutiveCollisions failed on the farm
with "capture child birth identity: procid: process is no longer
running", after 2 launches instead of 5. Its stand-in dolt reports
"Port N already in use." and exits immediately, and it exited before
Start captured its birth identity. Start treated that capture failure as
fatal, so an ErrPortInUse that Start could recover from became a hard
error. A real dolt that finds its port taken can exit just as fast, so
this race exists in the product, not only in the test.
Every spawn step before the child is handed to its supervising
goroutine (identity capture, root identity, pid record) now fails
through failSpawn. failSpawn kills and reaps the child; reaping also
drains its output into the watcher. The child's own exit then decides
the error:
- the child reported its port taken: ErrPortInUse, so the normal policy
and retry path runs;
- the child exited by itself for another reason: "exited (status N)
before startup completed";
- the child was alive and we killed it: the step's own error, as
before.
On Windows, a killed child reports an ordinary exit code, so there an
exit cannot be told from a kill. Only the port-in-use classification
applies there.
procid.Capture is reached through a package var so tests can make the
capture lose the race deterministically.
Tests, using a shell-script stand-in dolt that starts in milliseconds:
- a fast port-in-use exit that beats the capture is ErrPortInUse, and
with a policy it is retried up to the 5-port budget;
- a fast exit for another reason reports its status;
- a capture failure while the child is alive stays fatal and kills the
child;
- ten immediate port-in-use exits with the real capture are all
ErrPortInUse.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1462 lines
55 KiB
Go
1462 lines
55 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/dolthub/dolt/go/libraries/doltcore/servercfg"
|
|
"github.com/dolthub/dolt/go/libraries/utils/filesys"
|
|
"github.com/steveyegge/beads/internal/configfile"
|
|
"github.com/steveyegge/beads/internal/storage/dbproxy/server"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestRenderProxiedServerConfig_RoundTrips(t *testing.T) {
|
|
body, err := renderProxiedServerConfig(54321)
|
|
require.NoError(t, err)
|
|
|
|
cfg, err := servercfg.NewYamlConfig(body)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, proxiedServerListenerHost, cfg.Host(), "Host mismatch")
|
|
assert.Equal(t, 54321, cfg.Port(), "Port mismatch")
|
|
assert.Equal(t, servercfg.LogLevel_Info, cfg.LogLevel(), "LogLevel mismatch")
|
|
}
|
|
|
|
func TestRenderProxiedServerConfig_ArchiveLevel(t *testing.T) {
|
|
body, err := renderProxiedServerConfig(54321)
|
|
require.NoError(t, err)
|
|
assert.Contains(t, string(body), "archive_level: 0")
|
|
|
|
cfg, err := servercfg.NewYamlConfig(body)
|
|
require.NoError(t, err)
|
|
gc := cfg.AutoGCBehavior()
|
|
require.NotNil(t, gc, "AutoGCBehavior must always be set")
|
|
assert.Equal(t, 0, gc.ArchiveLevel())
|
|
assert.True(t, gc.Enable(), "auto-GC must remain enabled")
|
|
}
|
|
|
|
func TestEnsureProxiedServerConfig_CreatesAndIsIdempotent(t *testing.T) {
|
|
beadsDir := t.TempDir()
|
|
|
|
path1, err := ensureProxiedServerConfig(beadsDir)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, filepath.Join(beadsDir, "dolt", "config.yaml"), path1)
|
|
|
|
body1, err := os.ReadFile(path1)
|
|
require.NoError(t, err)
|
|
require.NotEmpty(t, body1)
|
|
require.True(t, strings.Contains(string(body1), proxiedServerListenerHost))
|
|
|
|
// Second call must NOT rewrite — running daemon is bound to the existing port.
|
|
path2, err := ensureProxiedServerConfig(beadsDir)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, path1, path2)
|
|
|
|
body2, err := os.ReadFile(path2)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, body1, body2, "second call must not rewrite the file")
|
|
|
|
// Round-trip: dolt's own loader must accept what we wrote.
|
|
loaded, err := servercfg.YamlConfigFromFile(filesys.LocalFS, path2)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, proxiedServerListenerHost, loaded.Host())
|
|
assert.Greater(t, loaded.Port(), 0)
|
|
}
|
|
|
|
func TestEnsureProxiedServerConfig_ReusesExistingConfig(t *testing.T) {
|
|
beadsDir := t.TempDir()
|
|
root := filepath.Join(beadsDir, "dolt")
|
|
require.NoError(t, os.MkdirAll(root, 0o755))
|
|
|
|
existing, err := renderProxiedServerConfig(45678)
|
|
require.NoError(t, err)
|
|
cfgPath := filepath.Join(root, "config.yaml")
|
|
require.NoError(t, os.WriteFile(cfgPath, existing, 0o600))
|
|
|
|
path, err := ensureProxiedServerConfig(beadsDir)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, cfgPath, path)
|
|
|
|
got, err := os.ReadFile(cfgPath)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, existing, got, "existing config.yaml must be reused unchanged")
|
|
}
|
|
|
|
func TestIsManagedProxiedServerConfig(t *testing.T) {
|
|
managed, err := renderProxiedServerConfig(1234)
|
|
require.NoError(t, err)
|
|
assert.True(t, isManagedProxiedServerConfig(managed))
|
|
|
|
assert.False(t, isManagedProxiedServerConfig([]byte("listener:\n port: 1234\n")))
|
|
assert.False(t, isManagedProxiedServerConfig(nil))
|
|
}
|
|
|
|
func TestEnsureProxiedServerConfig_NeverForksDolt(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("shell-script stub binary is POSIX-only")
|
|
}
|
|
binDir := t.TempDir()
|
|
sentinel := filepath.Join(binDir, "invoked")
|
|
quotedSentinel := "'" + strings.ReplaceAll(sentinel, "'", `'\''`) + "'"
|
|
stub := "#!/bin/sh\necho \"$@\" >> " + quotedSentinel + "\nexit 0\n"
|
|
require.NoError(t, os.WriteFile(filepath.Join(binDir, "dolt"), []byte(stub), 0o700))
|
|
t.Setenv("PATH", binDir+string(os.PathListSeparator)+os.Getenv("PATH"))
|
|
|
|
require.NoError(t, exec.Command("dolt", "version").Run(), "stub dolt must be reachable and runnable")
|
|
require.FileExists(t, sentinel, "stub dolt must record its invocations, else this test cannot fail")
|
|
require.NoError(t, os.Remove(sentinel))
|
|
|
|
beadsDir := t.TempDir()
|
|
_, err := ensureProxiedServerConfig(beadsDir) // fresh bootstrap
|
|
require.NoError(t, err)
|
|
_, err = ensureProxiedServerConfig(beadsDir) // steady state
|
|
require.NoError(t, err)
|
|
|
|
if body, rerr := os.ReadFile(sentinel); rerr == nil {
|
|
t.Fatalf("ensureProxiedServerConfig forked dolt: %s", body)
|
|
}
|
|
}
|
|
|
|
func TestEnsureProxiedServerConfig_CustomConfigUntouched(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "")
|
|
bd := t.TempDir()
|
|
|
|
customDir := t.TempDir()
|
|
customPath := filepath.Join(customDir, "my-server.yaml")
|
|
const before = "listener:\n host: 127.0.0.1\n port: 54321\n"
|
|
require.NoError(t, os.WriteFile(customPath, []byte(before), 0o600))
|
|
|
|
writeProxiedClientInfo(t, bd, &configfile.ProxiedServerClientInfo{ConfigPath: customPath})
|
|
|
|
var err error
|
|
var path string
|
|
got := captureStderr(t, func() {
|
|
path, err = ensureProxiedServerConfig(bd)
|
|
})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, customPath, path)
|
|
|
|
after, err := os.ReadFile(customPath)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, before, string(after), "custom config must never be rewritten")
|
|
assert.Empty(t, got, "custom configs must not produce per-command warnings")
|
|
}
|
|
|
|
func TestEnsureProxiedServerConfig_PreMarkerDefaultConfigUntouched(t *testing.T) {
|
|
beadsDir := t.TempDir()
|
|
root := filepath.Join(beadsDir, "dolt")
|
|
require.NoError(t, os.MkdirAll(root, 0o755))
|
|
cfgPath := filepath.Join(root, "config.yaml")
|
|
|
|
const seed = "listener:\n host: 127.0.0.1\n port: 45678\n"
|
|
require.NoError(t, os.WriteFile(cfgPath, []byte(seed), 0o600))
|
|
|
|
var err error
|
|
var path string
|
|
got := captureStderr(t, func() {
|
|
path, err = ensureProxiedServerConfig(beadsDir)
|
|
})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, cfgPath, path)
|
|
|
|
after, err := os.ReadFile(cfgPath)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, seed, string(after), "pre-marker default config must never be rewritten")
|
|
assert.Empty(t, got)
|
|
}
|
|
|
|
func TestEnsureProxiedServerConfig_CustomConfigEnvVarInterpolation(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "")
|
|
t.Setenv("BEADS_TEST_PROXIED_SERVER_PORT", "54321")
|
|
bd := t.TempDir()
|
|
|
|
customDir := t.TempDir()
|
|
customPath := filepath.Join(customDir, "my-server.yaml")
|
|
const tmpl = "listener:\n host: 127.0.0.1\n port: ${BEADS_TEST_PROXIED_SERVER_PORT}\n"
|
|
require.NoError(t, os.WriteFile(customPath, []byte(tmpl), 0o600))
|
|
|
|
writeProxiedClientInfo(t, bd, &configfile.ProxiedServerClientInfo{ConfigPath: customPath})
|
|
|
|
path, err := ensureProxiedServerConfig(bd)
|
|
require.NoError(t, err, "a valid ${VAR} custom config must not be rejected pre-start")
|
|
assert.Equal(t, customPath, path)
|
|
|
|
after, err := os.ReadFile(customPath)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, tmpl, string(after))
|
|
}
|
|
|
|
func TestEnsureProxiedServerConfig_HandEditedManagedFileWithPlaceholder(t *testing.T) {
|
|
t.Setenv("BEADS_TEST_PROXIED_SERVER_PORT2", "54321")
|
|
beadsDir := t.TempDir()
|
|
root := filepath.Join(beadsDir, "dolt")
|
|
require.NoError(t, os.MkdirAll(root, 0o755))
|
|
cfgPath := filepath.Join(root, "config.yaml")
|
|
|
|
seed := managedProxiedServerConfigMarker + "listener:\n host: 127.0.0.1\n port: ${BEADS_TEST_PROXIED_SERVER_PORT2}\n"
|
|
require.NoError(t, os.WriteFile(cfgPath, []byte(seed), 0o600))
|
|
|
|
var path string
|
|
var err error
|
|
got := captureStderr(t, func() {
|
|
path, err = ensureProxiedServerConfig(beadsDir)
|
|
})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, cfgPath, path)
|
|
assert.Empty(t, got)
|
|
|
|
after, err := os.ReadFile(cfgPath)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, seed, string(after), "a hand-edited managed file must be left untouched")
|
|
}
|
|
|
|
func TestEnsureProxiedServerConfig_DanglingSymlinkSelfHeals(t *testing.T) {
|
|
beadsDir := t.TempDir()
|
|
root := filepath.Join(beadsDir, "dolt")
|
|
require.NoError(t, os.MkdirAll(root, 0o755))
|
|
|
|
cfgPath := filepath.Join(root, "config.yaml")
|
|
if err := os.Symlink(filepath.Join(t.TempDir(), "gone.yaml"), cfgPath); err != nil {
|
|
t.Skipf("symlink not supported on this platform: %v", err)
|
|
}
|
|
|
|
path, err := ensureProxiedServerConfig(beadsDir)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, cfgPath, path)
|
|
|
|
info, err := os.Lstat(cfgPath)
|
|
require.NoError(t, err)
|
|
assert.True(t, info.Mode().IsRegular(), "dangling symlink must be replaced by the generated config")
|
|
|
|
body, err := os.ReadFile(cfgPath)
|
|
require.NoError(t, err)
|
|
assert.True(t, isManagedProxiedServerConfig(body))
|
|
}
|
|
|
|
func TestProxiedServerPathHelpers(t *testing.T) {
|
|
bd := "/tmp/some/.beads"
|
|
assert.Equal(t, "/tmp/some/.beads/dolt", proxiedServerRoot(bd))
|
|
assert.Equal(t, "/tmp/some/.beads/dolt/config.yaml", proxiedServerConfigPath(bd))
|
|
assert.Equal(t, "/tmp/some/.beads/dolt/server.log", proxiedServerLogPath(bd))
|
|
}
|
|
|
|
// TestInitCommandRegistersProxiedServerFlag verifies the --proxied-server flag
|
|
// is wired into initCmd. Flag-presence regression test.
|
|
func TestInitCommandRegistersProxiedServerFlag(t *testing.T) {
|
|
flag := initCmd.Flags().Lookup("proxied-server")
|
|
require.NotNil(t, flag, "init command does not register --proxied-server")
|
|
assert.Equal(t, "false", flag.DefValue, "--proxied-server should default to false")
|
|
}
|
|
|
|
// TestInitCommandRegistersServerConfigFlag verifies the --proxied-server-config-path flag
|
|
// is wired into initCmd.
|
|
func TestInitCommandRegistersServerConfigFlag(t *testing.T) {
|
|
flag := initCmd.Flags().Lookup("proxied-server-config-path")
|
|
require.NotNil(t, flag, "init command does not register --proxied-server-config-path")
|
|
assert.Equal(t, "", flag.DefValue, "--proxied-server-config-path should default to empty")
|
|
}
|
|
|
|
func writeProxiedClientInfo(t *testing.T, beadsDir string, info *configfile.ProxiedServerClientInfo) {
|
|
t.Helper()
|
|
require.NoError(t, configfile.SaveProxiedServerClientInfo(beadsDir, info))
|
|
}
|
|
|
|
func TestResolveProxiedServerConfigPath(t *testing.T) {
|
|
t.Run("no sidecar, no env, returns default and !isCustom", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "")
|
|
bd := t.TempDir()
|
|
path, isCustom, err := resolveProxiedServerConfigPath(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, proxiedServerConfigPath(bd), path)
|
|
assert.False(t, isCustom)
|
|
})
|
|
|
|
t.Run("empty sidecar, no env, returns default and !isCustom", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "")
|
|
bd := t.TempDir()
|
|
writeProxiedClientInfo(t, bd, &configfile.ProxiedServerClientInfo{})
|
|
path, isCustom, err := resolveProxiedServerConfigPath(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, proxiedServerConfigPath(bd), path)
|
|
assert.False(t, isCustom)
|
|
})
|
|
|
|
t.Run("sidecar relative joins beadsDir and isCustom", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "")
|
|
bd := t.TempDir()
|
|
writeProxiedClientInfo(t, bd, &configfile.ProxiedServerClientInfo{ConfigPath: "configs/server.yaml"})
|
|
path, isCustom, err := resolveProxiedServerConfigPath(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, filepath.Join(bd, "configs/server.yaml"), path)
|
|
assert.True(t, isCustom)
|
|
})
|
|
|
|
t.Run("sidecar absolute returned as-is and isCustom", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "")
|
|
bd := t.TempDir()
|
|
writeProxiedClientInfo(t, bd, &configfile.ProxiedServerClientInfo{ConfigPath: "/etc/dolt/server.yaml"})
|
|
path, isCustom, err := resolveProxiedServerConfigPath(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "/etc/dolt/server.yaml", path)
|
|
assert.True(t, isCustom)
|
|
})
|
|
|
|
t.Run("env beats sidecar and isCustom", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "/from/env.yaml")
|
|
bd := t.TempDir()
|
|
writeProxiedClientInfo(t, bd, &configfile.ProxiedServerClientInfo{ConfigPath: "configs/from-meta.yaml"})
|
|
path, isCustom, err := resolveProxiedServerConfigPath(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "/from/env.yaml", path)
|
|
assert.True(t, isCustom)
|
|
})
|
|
|
|
t.Run("env with no sidecar still wins", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "/from/env.yaml")
|
|
bd := t.TempDir()
|
|
path, isCustom, err := resolveProxiedServerConfigPath(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "/from/env.yaml", path)
|
|
assert.True(t, isCustom)
|
|
})
|
|
}
|
|
|
|
// writeValidServerYAML writes a minimal valid dolt sql-server YAML to path
|
|
// and returns the path. Used to exercise the custom-config success path.
|
|
func writeValidServerYAML(t *testing.T, path string) string {
|
|
t.Helper()
|
|
body, err := renderProxiedServerConfig(54321)
|
|
require.NoError(t, err)
|
|
require.NoError(t, os.WriteFile(path, body, 0o600))
|
|
return path
|
|
}
|
|
|
|
// TestEnsureProxiedServerConfig_CustomPathExists asserts that when a custom
|
|
// path is configured, ensureProxiedServerConfig returns it unchanged AND does
|
|
// not auto-create the default <beadsDir>/proxieddb/config.yaml.
|
|
func TestEnsureProxiedServerConfig_CustomPathExists(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "")
|
|
bd := t.TempDir()
|
|
|
|
customDir := t.TempDir()
|
|
customPath := writeValidServerYAML(t, filepath.Join(customDir, "my-server.yaml"))
|
|
|
|
writeProxiedClientInfo(t, bd, &configfile.ProxiedServerClientInfo{ConfigPath: customPath})
|
|
got, err := ensureProxiedServerConfig(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, customPath, got)
|
|
|
|
defaultPath := proxiedServerConfigPath(bd)
|
|
_, statErr := os.Stat(defaultPath)
|
|
assert.True(t, os.IsNotExist(statErr), "default config must not be auto-created when a custom path is configured (got err=%v)", statErr)
|
|
}
|
|
|
|
// TestEnsureProxiedServerConfig_CustomPathMissing asserts a clear error when
|
|
// the user-supplied path doesn't exist. bd never auto-creates user files.
|
|
func TestEnsureProxiedServerConfig_CustomPathMissing(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "")
|
|
bd := t.TempDir()
|
|
missing := filepath.Join(t.TempDir(), "does-not-exist.yaml")
|
|
|
|
writeProxiedClientInfo(t, bd, &configfile.ProxiedServerClientInfo{ConfigPath: missing})
|
|
_, err := ensureProxiedServerConfig(bd)
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), missing)
|
|
}
|
|
|
|
// TestEnsureProxiedServerConfig_CustomPathInvalidYAML asserts that a
|
|
// non-parsable YAML at the custom path is rejected up front rather than
|
|
// crashing the daemon downstream.
|
|
func TestEnsureProxiedServerConfig_CustomPathInvalidYAML(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "")
|
|
bd := t.TempDir()
|
|
bad := filepath.Join(t.TempDir(), "bad.yaml")
|
|
// Unclosed flow sequence — guaranteed YAML parse error.
|
|
require.NoError(t, os.WriteFile(bad, []byte("listener: [host: 127.0.0.1\n"), 0o600))
|
|
|
|
writeProxiedClientInfo(t, bd, &configfile.ProxiedServerClientInfo{ConfigPath: bad})
|
|
_, err := ensureProxiedServerConfig(bd)
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), bad)
|
|
assert.Contains(t, strings.ToLower(err.Error()), "parse")
|
|
}
|
|
|
|
// TestEnsureProxiedServerConfig_CustomPathIsDirectory asserts that pointing
|
|
// the custom path at a directory (or other non-regular file) is rejected.
|
|
func TestEnsureProxiedServerConfig_CustomPathIsDirectory(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "")
|
|
bd := t.TempDir()
|
|
dir := t.TempDir()
|
|
|
|
writeProxiedClientInfo(t, bd, &configfile.ProxiedServerClientInfo{ConfigPath: dir})
|
|
_, err := ensureProxiedServerConfig(bd)
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), dir)
|
|
assert.Contains(t, err.Error(), "not a regular file")
|
|
}
|
|
|
|
// TestInitCommandRegistersServerLogPathFlag verifies the --proxied-server-log-path
|
|
// flag is wired into initCmd.
|
|
func TestInitCommandRegistersServerLogPathFlag(t *testing.T) {
|
|
flag := initCmd.Flags().Lookup("proxied-server-log-path")
|
|
require.NotNil(t, flag, "init command does not register --proxied-server-log-path")
|
|
assert.Equal(t, "", flag.DefValue, "--proxied-server-log-path should default to empty")
|
|
}
|
|
|
|
// TestResolveProxiedServerLogPath mirrors TestResolveProxiedServerConfigPath
|
|
// for the log-path resolver.
|
|
func TestResolveProxiedServerLogPath(t *testing.T) {
|
|
t.Run("no sidecar, no env, returns default and !isCustom", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_LOG", "")
|
|
bd := t.TempDir()
|
|
path, isCustom, err := resolveProxiedServerLogPath(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, proxiedServerLogPath(bd), path)
|
|
assert.False(t, isCustom)
|
|
})
|
|
|
|
t.Run("empty sidecar, no env, returns default and !isCustom", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_LOG", "")
|
|
bd := t.TempDir()
|
|
writeProxiedClientInfo(t, bd, &configfile.ProxiedServerClientInfo{})
|
|
path, isCustom, err := resolveProxiedServerLogPath(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, proxiedServerLogPath(bd), path)
|
|
assert.False(t, isCustom)
|
|
})
|
|
|
|
t.Run("sidecar relative joins beadsDir and isCustom", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_LOG", "")
|
|
bd := t.TempDir()
|
|
writeProxiedClientInfo(t, bd, &configfile.ProxiedServerClientInfo{LogPath: "logs/server.log"})
|
|
path, isCustom, err := resolveProxiedServerLogPath(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, filepath.Join(bd, "logs/server.log"), path)
|
|
assert.True(t, isCustom)
|
|
})
|
|
|
|
t.Run("sidecar absolute returned as-is and isCustom", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_LOG", "")
|
|
bd := t.TempDir()
|
|
writeProxiedClientInfo(t, bd, &configfile.ProxiedServerClientInfo{LogPath: "/var/log/beads/server.log"})
|
|
path, isCustom, err := resolveProxiedServerLogPath(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "/var/log/beads/server.log", path)
|
|
assert.True(t, isCustom)
|
|
})
|
|
|
|
t.Run("env beats sidecar and isCustom", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_LOG", "/from/env.log")
|
|
bd := t.TempDir()
|
|
writeProxiedClientInfo(t, bd, &configfile.ProxiedServerClientInfo{LogPath: "logs/from-meta.log"})
|
|
path, isCustom, err := resolveProxiedServerLogPath(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "/from/env.log", path)
|
|
assert.True(t, isCustom)
|
|
})
|
|
|
|
t.Run("env with no sidecar still wins", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_LOG", "/from/env.log")
|
|
bd := t.TempDir()
|
|
path, isCustom, err := resolveProxiedServerLogPath(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "/from/env.log", path)
|
|
assert.True(t, isCustom)
|
|
})
|
|
}
|
|
|
|
// TestValidateProxiedServerLogPath covers the early-bailout validator.
|
|
// Contract: parent dir must exist; the file may not exist (the daemon's
|
|
// O_CREATE|O_APPEND open handles that); if it exists it must be a regular
|
|
// file.
|
|
func TestValidateProxiedServerLogPath(t *testing.T) {
|
|
t.Run("parent dir missing rejected", func(t *testing.T) {
|
|
// /<tmp>/nope/server.log — parent /<tmp>/nope doesn't exist.
|
|
path := filepath.Join(t.TempDir(), "nope", "server.log")
|
|
err := validateProxiedServerLogPath(path)
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "parent directory")
|
|
})
|
|
|
|
t.Run("path doesn't exist but parent does, accepted", func(t *testing.T) {
|
|
// Daemon will create the file via O_CREATE|O_APPEND.
|
|
path := filepath.Join(t.TempDir(), "server.log")
|
|
require.NoError(t, validateProxiedServerLogPath(path))
|
|
})
|
|
|
|
t.Run("existing regular file accepted", func(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "server.log")
|
|
require.NoError(t, os.WriteFile(path, []byte("preexisting log content\n"), 0o600))
|
|
require.NoError(t, validateProxiedServerLogPath(path))
|
|
})
|
|
|
|
t.Run("existing directory rejected", func(t *testing.T) {
|
|
dir := filepath.Join(t.TempDir(), "server.log")
|
|
require.NoError(t, os.Mkdir(dir, 0o755))
|
|
err := validateProxiedServerLogPath(dir)
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "not a regular file")
|
|
})
|
|
|
|
t.Run("symlink to directory rejected", func(t *testing.T) {
|
|
base := t.TempDir()
|
|
realDir := filepath.Join(base, "actual-dir")
|
|
require.NoError(t, os.Mkdir(realDir, 0o755))
|
|
link := filepath.Join(base, "server.log")
|
|
if err := os.Symlink(realDir, link); err != nil {
|
|
t.Skipf("symlink not supported on this platform: %v", err)
|
|
}
|
|
err := validateProxiedServerLogPath(link)
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "not a regular file")
|
|
})
|
|
|
|
t.Run("parent path is a regular file, not a dir, rejected", func(t *testing.T) {
|
|
base := t.TempDir()
|
|
fileAsParent := filepath.Join(base, "blocker")
|
|
require.NoError(t, os.WriteFile(fileAsParent, []byte("x"), 0o600))
|
|
// /<tmp>/blocker/server.log — "blocker" is a file, not a dir.
|
|
err := validateProxiedServerLogPath(filepath.Join(fileAsParent, "server.log"))
|
|
require.Error(t, err)
|
|
})
|
|
}
|
|
|
|
// TestValidateProxiedServerConfig covers the standalone validator that
|
|
// init.go uses for early proxied-server-config-path validation.
|
|
//
|
|
// The validator deliberately emits source-neutral errors (just the path)
|
|
// because the value may come from a CLI flag, BEADS_PROXIED_SERVER_CONFIG,
|
|
// or the proxied_server_client_info.json sidecar. Callers prepend their
|
|
// own label.
|
|
func TestValidateProxiedServerConfig(t *testing.T) {
|
|
t.Run("valid YAML passes", func(t *testing.T) {
|
|
path := writeValidServerYAML(t, filepath.Join(t.TempDir(), "ok.yaml"))
|
|
require.NoError(t, validateProxiedServerConfig(path))
|
|
})
|
|
t.Run("missing path errors with the path in the message", func(t *testing.T) {
|
|
missing := filepath.Join(t.TempDir(), "nope.yaml")
|
|
err := validateProxiedServerConfig(missing)
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), missing)
|
|
})
|
|
t.Run("directory rejected", func(t *testing.T) {
|
|
err := validateProxiedServerConfig(t.TempDir())
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "not a regular file")
|
|
})
|
|
t.Run("invalid YAML rejected", func(t *testing.T) {
|
|
bad := filepath.Join(t.TempDir(), "bad.yaml")
|
|
require.NoError(t, os.WriteFile(bad, []byte("listener: [host: 127.0.0.1\n"), 0o600))
|
|
err := validateProxiedServerConfig(bad)
|
|
require.Error(t, err)
|
|
assert.Contains(t, strings.ToLower(err.Error()), "parse")
|
|
})
|
|
}
|
|
|
|
func TestValidateManagedServerConfigPolicy(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
mutate func(cfg *servercfg.YAMLConfig)
|
|
wantErr string // substring of the expected error; "" means accepted
|
|
}{
|
|
{name: "IPv4 loopback"},
|
|
{
|
|
name: "IPv4 loopback range",
|
|
mutate: func(cfg *servercfg.YAMLConfig) { cfg.ListenerConfig.HostStr = stringPtr("127.42.0.9") },
|
|
},
|
|
{
|
|
name: "IPv6 loopback",
|
|
mutate: func(cfg *servercfg.YAMLConfig) { cfg.ListenerConfig.HostStr = stringPtr("::1") },
|
|
},
|
|
{
|
|
name: "IPv4-mapped IPv6 loopback",
|
|
mutate: func(cfg *servercfg.YAMLConfig) { cfg.ListenerConfig.HostStr = stringPtr("::ffff:127.0.0.1") },
|
|
},
|
|
{
|
|
name: "omitted host rejected",
|
|
mutate: func(cfg *servercfg.YAMLConfig) { cfg.ListenerConfig.HostStr = nil },
|
|
wantErr: "listener.host is omitted",
|
|
},
|
|
{
|
|
name: "IPv4 wildcard",
|
|
mutate: func(cfg *servercfg.YAMLConfig) { cfg.ListenerConfig.HostStr = stringPtr("0.0.0.0") },
|
|
wantErr: "numeric loopback IP literal",
|
|
},
|
|
{
|
|
name: "IPv6 wildcard",
|
|
mutate: func(cfg *servercfg.YAMLConfig) { cfg.ListenerConfig.HostStr = stringPtr("::") },
|
|
wantErr: "numeric loopback IP literal",
|
|
},
|
|
{
|
|
name: "public IPv4",
|
|
mutate: func(cfg *servercfg.YAMLConfig) { cfg.ListenerConfig.HostStr = stringPtr("203.0.113.7") },
|
|
wantErr: "numeric loopback IP literal",
|
|
},
|
|
{
|
|
name: "public IPv6",
|
|
mutate: func(cfg *servercfg.YAMLConfig) { cfg.ListenerConfig.HostStr = stringPtr("2001:db8::7") },
|
|
wantErr: "numeric loopback IP literal",
|
|
},
|
|
{
|
|
name: "localhost hostname",
|
|
mutate: func(cfg *servercfg.YAMLConfig) { cfg.ListenerConfig.HostStr = stringPtr("localhost") },
|
|
wantErr: "numeric loopback IP literal",
|
|
},
|
|
{
|
|
name: "other hostname",
|
|
mutate: func(cfg *servercfg.YAMLConfig) { cfg.ListenerConfig.HostStr = stringPtr("db.internal") },
|
|
wantErr: "numeric loopback IP literal",
|
|
},
|
|
{
|
|
name: "garbage",
|
|
mutate: func(cfg *servercfg.YAMLConfig) { cfg.ListenerConfig.HostStr = stringPtr("not an address !") },
|
|
wantErr: "numeric loopback IP literal",
|
|
},
|
|
{
|
|
name: "explicit empty binds wildcard",
|
|
mutate: func(cfg *servercfg.YAMLConfig) { cfg.ListenerConfig.HostStr = stringPtr("") },
|
|
wantErr: "numeric loopback IP literal",
|
|
},
|
|
{
|
|
name: "unix socket rejected",
|
|
mutate: func(cfg *servercfg.YAMLConfig) { cfg.ListenerConfig.Socket = stringPtr("/tmp/mysql.sock") },
|
|
wantErr: "listener.socket",
|
|
},
|
|
{
|
|
name: "empty socket means default socket, rejected",
|
|
mutate: func(cfg *servercfg.YAMLConfig) {
|
|
cfg.ListenerConfig.Socket = stringPtr("")
|
|
},
|
|
wantErr: "listener.socket",
|
|
},
|
|
{
|
|
name: "remotesapi port rejected",
|
|
mutate: func(cfg *servercfg.YAMLConfig) {
|
|
port := 8080
|
|
cfg.RemotesapiConfig.Port_ = &port
|
|
},
|
|
wantErr: "remotesapi.port",
|
|
},
|
|
{
|
|
name: "cluster block rejected",
|
|
mutate: func(cfg *servercfg.YAMLConfig) { cfg.ClusterCfg = &servercfg.ClusterYAMLConfig{} },
|
|
wantErr: "cluster block",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
cfg := &servercfg.YAMLConfig{
|
|
ListenerConfig: servercfg.ListenerYAMLConfig{HostStr: stringPtr("127.0.0.1")},
|
|
}
|
|
if tt.mutate != nil {
|
|
tt.mutate(cfg)
|
|
}
|
|
err := validateManagedServerConfigPolicy(cfg)
|
|
if tt.wantErr == "" {
|
|
require.NoError(t, err)
|
|
return
|
|
}
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), tt.wantErr)
|
|
assert.Contains(t, err.Error(), "127.0.0.1")
|
|
assert.Contains(t, err.Error(), "--proxied-server-external-host")
|
|
assert.Contains(t, err.Error(), "--proxied-server-external-port")
|
|
assert.Contains(t, err.Error(), "--proxied-server-external-socket-path")
|
|
assert.NotContains(t, err.Error(), "--server-")
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestManagedListenerPolicyEveryConfigPathAtInitAndRuntime(t *testing.T) {
|
|
type setupFn func(t *testing.T, beadsDir string) string
|
|
tests := []struct {
|
|
name string
|
|
setup setupFn
|
|
}{
|
|
{
|
|
name: "environment custom",
|
|
setup: func(t *testing.T, beadsDir string) string {
|
|
path := writeListenerConfig(t, filepath.Join(t.TempDir(), "env.yaml"), "0.0.0.0", false, false)
|
|
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", path)
|
|
return ""
|
|
},
|
|
},
|
|
{
|
|
name: "sidecar custom",
|
|
setup: func(t *testing.T, beadsDir string) string {
|
|
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "")
|
|
path := writeListenerConfig(t, filepath.Join(t.TempDir(), "sidecar.yaml"), "0.0.0.0", false, false)
|
|
writeProxiedClientInfo(t, beadsDir, &configfile.ProxiedServerClientInfo{ConfigPath: path})
|
|
return ""
|
|
},
|
|
},
|
|
{
|
|
name: "pre-marker default path",
|
|
setup: func(t *testing.T, beadsDir string) string {
|
|
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "")
|
|
path := proxiedServerConfigPath(beadsDir)
|
|
writeListenerConfig(t, path, "0.0.0.0", false, false)
|
|
return ""
|
|
},
|
|
},
|
|
{
|
|
name: "marker file hand edit",
|
|
setup: func(t *testing.T, beadsDir string) string {
|
|
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "")
|
|
path := proxiedServerConfigPath(beadsDir)
|
|
writeListenerConfig(t, path, "0.0.0.0", true, false)
|
|
return ""
|
|
},
|
|
},
|
|
{
|
|
name: "marker parse fallback with interpolation",
|
|
setup: func(t *testing.T, beadsDir string) string {
|
|
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "")
|
|
t.Setenv("BEADS_TEST_MANAGED_LISTENER_PORT", "54321")
|
|
path := proxiedServerConfigPath(beadsDir)
|
|
writeListenerConfig(t, path, "0.0.0.0", true, true)
|
|
return ""
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
beadsDir := t.TempDir()
|
|
explicitPath := tt.setup(t, beadsDir)
|
|
|
|
initErr := validateManagedProxiedServerConfigAtInit(beadsDir, explicitPath, "")
|
|
assertManagedListenerPolicyError(t, initErr)
|
|
|
|
_, runtimeErr := ensureProxiedServerConfig(beadsDir)
|
|
assertManagedListenerPolicyError(t, runtimeErr)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestManagedListenerPolicyLoopbackCustomConfigPassesInitAndRuntime(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "")
|
|
beadsDir := t.TempDir()
|
|
path := writeListenerConfig(t, filepath.Join(t.TempDir(), "loopback.yaml"), "127.0.0.1", false, false)
|
|
writeProxiedClientInfo(t, beadsDir, &configfile.ProxiedServerClientInfo{ConfigPath: path})
|
|
|
|
require.NoError(t, validateManagedProxiedServerConfigAtInit(beadsDir, "", ""))
|
|
got, err := ensureProxiedServerConfig(beadsDir)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, path, got)
|
|
}
|
|
|
|
// TestManagedListenerPolicyOmittedHostRejected pins the policy decision that
|
|
// an omitted listener.host is NOT trusted: servercfg resolves it to
|
|
// "localhost", which CheckForUnixSocket also treats as license to open the
|
|
// shared-namespace default unix socket.
|
|
func TestManagedListenerPolicyOmittedHostRejected(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "")
|
|
beadsDir := t.TempDir()
|
|
path := filepath.Join(t.TempDir(), "omitted.yaml")
|
|
require.NoError(t, os.WriteFile(path, []byte("listener:\n port: 54321\n"), 0o600))
|
|
writeProxiedClientInfo(t, beadsDir, &configfile.ProxiedServerClientInfo{ConfigPath: path})
|
|
|
|
initErr := validateManagedProxiedServerConfigAtInit(beadsDir, "", "")
|
|
require.Error(t, initErr)
|
|
assert.Contains(t, initErr.Error(), "listener.host is omitted")
|
|
|
|
_, runtimeErr := ensureProxiedServerConfig(beadsDir)
|
|
require.Error(t, runtimeErr)
|
|
assert.Contains(t, runtimeErr.Error(), "listener.host is omitted")
|
|
}
|
|
|
|
// TestManagedListenerPolicyMarkerInterpolatedHost pins the interpolation
|
|
// contract: a marker config whose listener.host is a ${VAR} placeholder is
|
|
// judged on what the placeholder expands to, not on the raw text.
|
|
func TestManagedListenerPolicyMarkerInterpolatedHost(t *testing.T) {
|
|
writeInterpolatedHostConfig := func(t *testing.T, beadsDir string) string {
|
|
t.Helper()
|
|
path := proxiedServerConfigPath(beadsDir)
|
|
require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o700))
|
|
body := managedProxiedServerConfigMarker +
|
|
"listener:\n host: \"${BEADS_TEST_MANAGED_LISTENER_HOST}\"\n port: 54321\n"
|
|
require.NoError(t, os.WriteFile(path, []byte(body), 0o600))
|
|
return path
|
|
}
|
|
|
|
t.Run("expands to loopback, accepted", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "")
|
|
t.Setenv("BEADS_TEST_MANAGED_LISTENER_HOST", "127.0.0.1")
|
|
beadsDir := t.TempDir()
|
|
want := writeInterpolatedHostConfig(t, beadsDir)
|
|
|
|
got, err := ensureProxiedServerConfig(beadsDir)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, want, got)
|
|
})
|
|
|
|
t.Run("expands to wildcard, rejected", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "")
|
|
t.Setenv("BEADS_TEST_MANAGED_LISTENER_HOST", "0.0.0.0")
|
|
beadsDir := t.TempDir()
|
|
writeInterpolatedHostConfig(t, beadsDir)
|
|
|
|
_, err := ensureProxiedServerConfig(beadsDir)
|
|
assertManagedListenerPolicyError(t, err)
|
|
})
|
|
}
|
|
|
|
// TestManagedListenerPolicyRootPathFlagAtInit pins the F5c fix: at init time
|
|
// the sidecar is not persisted yet, so the --proxied-server-root-path flag
|
|
// value must be threaded into the validator explicitly for a pre-existing
|
|
// config.yaml under that root to be policy-checked.
|
|
func TestManagedListenerPolicyRootPathFlagAtInit(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "")
|
|
t.Setenv("BEADS_PROXIED_SERVER_ROOT_PATH", "")
|
|
beadsDir := t.TempDir()
|
|
rootDir := t.TempDir()
|
|
|
|
t.Run("bad config under flagged root rejected", func(t *testing.T) {
|
|
writeListenerConfig(t, filepath.Join(rootDir, proxiedServerConfigName), "0.0.0.0", false, false)
|
|
err := validateManagedProxiedServerConfigAtInit(beadsDir, "", rootDir)
|
|
assertManagedListenerPolicyError(t, err)
|
|
})
|
|
|
|
t.Run("missing config under flagged root allowed", func(t *testing.T) {
|
|
emptyRoot := t.TempDir()
|
|
require.NoError(t, validateManagedProxiedServerConfigAtInit(beadsDir, "", emptyRoot))
|
|
})
|
|
}
|
|
|
|
func stringPtr(value string) *string {
|
|
return &value
|
|
}
|
|
|
|
func writeListenerConfig(t *testing.T, path, host string, marker, interpolatedPort bool) string {
|
|
t.Helper()
|
|
require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o700))
|
|
body := ""
|
|
if marker {
|
|
body = managedProxiedServerConfigMarker
|
|
}
|
|
port := "54321"
|
|
if interpolatedPort {
|
|
port = "${BEADS_TEST_MANAGED_LISTENER_PORT}"
|
|
}
|
|
body += fmt.Sprintf("listener:\n host: %q\n port: %s\n", host, port)
|
|
require.NoError(t, os.WriteFile(path, []byte(body), 0o600))
|
|
return path
|
|
}
|
|
|
|
func assertManagedListenerPolicyError(t *testing.T, err error) {
|
|
t.Helper()
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "numeric loopback IP literal")
|
|
assert.Contains(t, err.Error(), "127.0.0.1")
|
|
assert.Contains(t, err.Error(), "--proxied-server-external-host")
|
|
assert.Contains(t, err.Error(), "--proxied-server-external-port")
|
|
assert.Contains(t, err.Error(), "--proxied-server-external-socket-path")
|
|
assert.NotContains(t, err.Error(), "--server-")
|
|
}
|
|
|
|
// TestCheckExistingBeadsDataAt_ProxiedServerNoData asserts that a proxied
|
|
// workspace with metadata.json but no actual <beadsDir>/proxieddb/<dbName>/.dolt
|
|
// directory is treated as a fresh clone — init is allowed to proceed so the
|
|
// caller can bootstrap.
|
|
func TestCheckExistingBeadsDataAt_ProxiedServerNoData(t *testing.T) {
|
|
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
|
require.NoError(t, os.MkdirAll(beadsDir, 0o755))
|
|
|
|
metadata := map[string]interface{}{
|
|
"database": "dolt",
|
|
"backend": "dolt",
|
|
"dolt_mode": "proxied-server",
|
|
"dolt_database": "myproj",
|
|
}
|
|
data, err := json.Marshal(metadata)
|
|
require.NoError(t, err)
|
|
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, "metadata.json"), data, 0o644))
|
|
|
|
// No <beadsDir>/proxieddb/myproj/.dolt — fresh-clone scenario.
|
|
if err := checkExistingBeadsDataAt(beadsDir, "myproj"); err != nil {
|
|
t.Fatalf("fresh proxied workspace should allow init, got: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestCheckExistingBeadsDataAt_ProxiedServerWithExistingDB asserts that the
|
|
// mere existence of the resolved proxied-server root blocks re-init in
|
|
// proxied-server mode. We deliberately don't peek deeper than the directory
|
|
// itself — the internal layout (wrapper db dir, per-db subdirs) is an
|
|
// implementation detail of the daemon. The custom-root sub-test additionally
|
|
// asserts that a workspace pointed at a custom root via metadata.json's
|
|
// dolt_proxied_server_root_path is also caught — otherwise re-init would
|
|
// silently bypass the safety check.
|
|
func TestCheckExistingBeadsDataAt_ProxiedServerWithExistingDB(t *testing.T) {
|
|
t.Run("default root", func(t *testing.T) {
|
|
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
|
require.NoError(t, os.MkdirAll(beadsDir, 0o755))
|
|
|
|
metadata := map[string]interface{}{
|
|
"database": "dolt",
|
|
"backend": "dolt",
|
|
"dolt_mode": "proxied-server",
|
|
"dolt_database": "myproj",
|
|
}
|
|
data, err := json.Marshal(metadata)
|
|
require.NoError(t, err)
|
|
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, "metadata.json"), data, 0o644))
|
|
|
|
// Materialize <beadsDir>/dolt/ — that alone should be enough to
|
|
// trip the guard, regardless of what's inside.
|
|
proxiedRoot := filepath.Join(beadsDir, "dolt")
|
|
require.NoError(t, os.MkdirAll(proxiedRoot, 0o755))
|
|
|
|
err = checkExistingBeadsDataAt(beadsDir, "myproj")
|
|
require.Error(t, err, "existing proxied-server root directory should block init")
|
|
assert.Contains(t, err.Error(), "already initialized")
|
|
assert.Contains(t, err.Error(), proxiedRoot)
|
|
})
|
|
|
|
t.Run("custom root via proxied_server_client_info.json", func(t *testing.T) {
|
|
// Ensure no env override leaks across tests — the resolver checks
|
|
// BEADS_PROXIED_SERVER_ROOT_PATH before the sidecar.
|
|
t.Setenv("BEADS_PROXIED_SERVER_ROOT_PATH", "")
|
|
|
|
base := t.TempDir()
|
|
beadsDir := filepath.Join(base, ".beads")
|
|
require.NoError(t, os.MkdirAll(beadsDir, 0o755))
|
|
|
|
// Custom root well outside <beadsDir>/proxieddb so the test fails
|
|
// loudly if the safety check still hardcodes the default location.
|
|
customRoot := filepath.Join(base, "custom-root")
|
|
require.NoError(t, os.MkdirAll(customRoot, 0o755))
|
|
|
|
metadata := map[string]interface{}{
|
|
"database": "dolt",
|
|
"backend": "dolt",
|
|
"dolt_mode": "proxied-server",
|
|
"dolt_database": "myproj",
|
|
}
|
|
data, err := json.Marshal(metadata)
|
|
require.NoError(t, err)
|
|
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, "metadata.json"), data, 0o644))
|
|
writeProxiedClientInfo(t, beadsDir, &configfile.ProxiedServerClientInfo{RootPath: customRoot})
|
|
|
|
// Sanity: the default location should NOT exist — proves the guard
|
|
// fired off the resolved root, not the default.
|
|
defaultRoot := filepath.Join(beadsDir, "dolt")
|
|
_, statErr := os.Stat(defaultRoot)
|
|
require.True(t, os.IsNotExist(statErr), "default <beadsDir>/dolt must not exist for this test to be meaningful")
|
|
|
|
err = checkExistingBeadsDataAt(beadsDir, "myproj")
|
|
require.Error(t, err, "existing custom root should block init")
|
|
assert.Contains(t, err.Error(), "already initialized")
|
|
assert.Contains(t, err.Error(), customRoot, "error must cite the custom root, not the default")
|
|
assert.NotContains(t, err.Error(), defaultRoot, "error must not cite a default location bd never used")
|
|
})
|
|
}
|
|
|
|
// TestInitCommandRegistersServerRootPathFlag verifies the --proxied-server-root-path
|
|
// flag is wired into initCmd.
|
|
func TestInitCommandRegistersServerRootPathFlag(t *testing.T) {
|
|
flag := initCmd.Flags().Lookup("proxied-server-root-path")
|
|
require.NotNil(t, flag, "init command does not register --proxied-server-root-path")
|
|
assert.Equal(t, "", flag.DefValue, "--proxied-server-root-path should default to empty")
|
|
}
|
|
|
|
func TestInitCommandRegistersProxiedServerExternalFlags(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
defaultText string
|
|
}{
|
|
{"proxied-server-external-host", ""},
|
|
{"proxied-server-external-port", "0"},
|
|
{"proxied-server-external-socket-path", ""},
|
|
{"proxied-server-external-user", ""},
|
|
{"proxied-server-external-tls", "false"},
|
|
{"proxied-server-external-tls-ca-cert-path", ""},
|
|
{"proxied-server-external-tls-cert-path", ""},
|
|
{"proxied-server-external-tls-key-path", ""},
|
|
{"proxied-server-external-tls-server-name", ""},
|
|
{"proxied-server-external-tls-skip-verify", "false"},
|
|
{"proxied-server-external-keep-alive", "0s"},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
f := initCmd.Flags().Lookup(tc.name)
|
|
require.NotNil(t, f, "init command does not register --%s", tc.name)
|
|
assert.Equal(t, tc.defaultText, f.DefValue, "--%s default", tc.name)
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestResolveProxiedServerRootPath mirrors TestResolveProxiedServerLogPath /
|
|
// TestResolveProxiedServerConfigPath for the root-path resolver.
|
|
func TestResolveProxiedServerRootPath(t *testing.T) {
|
|
t.Run("no sidecar, no env, returns default", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_ROOT_PATH", "")
|
|
bd := t.TempDir()
|
|
got, err := resolveProxiedServerRootPath(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, proxiedServerRoot(bd), got)
|
|
})
|
|
|
|
t.Run("empty sidecar, no env, returns default", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_ROOT_PATH", "")
|
|
bd := t.TempDir()
|
|
writeProxiedClientInfo(t, bd, &configfile.ProxiedServerClientInfo{})
|
|
got, err := resolveProxiedServerRootPath(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, proxiedServerRoot(bd), got)
|
|
})
|
|
|
|
t.Run("sidecar relative joins beadsDir", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_ROOT_PATH", "")
|
|
bd := t.TempDir()
|
|
writeProxiedClientInfo(t, bd, &configfile.ProxiedServerClientInfo{RootPath: "alt-proxieddb"})
|
|
got, err := resolveProxiedServerRootPath(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, filepath.Join(bd, "alt-proxieddb"), got)
|
|
})
|
|
|
|
t.Run("sidecar absolute returned as-is", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_ROOT_PATH", "")
|
|
bd := t.TempDir()
|
|
writeProxiedClientInfo(t, bd, &configfile.ProxiedServerClientInfo{RootPath: "/var/lib/beads/proxieddb"})
|
|
got, err := resolveProxiedServerRootPath(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "/var/lib/beads/proxieddb", got)
|
|
})
|
|
|
|
t.Run("env beats sidecar", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_ROOT_PATH", "/from/env-root")
|
|
bd := t.TempDir()
|
|
writeProxiedClientInfo(t, bd, &configfile.ProxiedServerClientInfo{RootPath: "alt-from-meta"})
|
|
got, err := resolveProxiedServerRootPath(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "/from/env-root", got)
|
|
})
|
|
|
|
t.Run("env with no sidecar still wins", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_ROOT_PATH", "/from/env-root")
|
|
bd := t.TempDir()
|
|
got, err := resolveProxiedServerRootPath(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "/from/env-root", got)
|
|
})
|
|
|
|
t.Run("corrupt sidecar surfaces error", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_ROOT_PATH", "")
|
|
bd := t.TempDir()
|
|
require.NoError(t, os.WriteFile(
|
|
configfile.ProxiedServerClientInfoPath(bd),
|
|
[]byte("not json{"),
|
|
0o600,
|
|
))
|
|
_, err := resolveProxiedServerRootPath(bd)
|
|
require.Error(t, err)
|
|
})
|
|
}
|
|
|
|
// TestValidateProxiedServerRootPath covers the early-bailout validator.
|
|
// Contract: path is allowed to NOT exist (runtime mkdir creates); if it
|
|
// exists, info.IsDir() must be true. os.Stat follows symlinks, so a
|
|
// symlink-to-dir reports as a dir (accepted) and a symlink-to-file reports
|
|
// as a regular file (rejected).
|
|
func TestValidateProxiedServerRootPath(t *testing.T) {
|
|
t.Run("path doesn't exist accepted", func(t *testing.T) {
|
|
// Runtime os.MkdirAll in the dolt store will create it.
|
|
path := filepath.Join(t.TempDir(), "does-not-exist")
|
|
require.NoError(t, validateProxiedServerRootPath(path))
|
|
})
|
|
|
|
t.Run("nested missing path accepted", func(t *testing.T) {
|
|
// Even nested non-existent paths are fine — MkdirAll handles it.
|
|
path := filepath.Join(t.TempDir(), "a", "b", "c")
|
|
require.NoError(t, validateProxiedServerRootPath(path))
|
|
})
|
|
|
|
t.Run("existing directory accepted", func(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "existing-dir")
|
|
require.NoError(t, os.Mkdir(path, 0o755))
|
|
require.NoError(t, validateProxiedServerRootPath(path))
|
|
})
|
|
|
|
t.Run("existing regular file rejected", func(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "regular-file")
|
|
require.NoError(t, os.WriteFile(path, []byte("x"), 0o600))
|
|
err := validateProxiedServerRootPath(path)
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "not a directory")
|
|
})
|
|
|
|
t.Run("symlink to file rejected", func(t *testing.T) {
|
|
base := t.TempDir()
|
|
realFile := filepath.Join(base, "real-file")
|
|
require.NoError(t, os.WriteFile(realFile, []byte("x"), 0o600))
|
|
link := filepath.Join(base, "link-to-file")
|
|
if err := os.Symlink(realFile, link); err != nil {
|
|
t.Skipf("symlink not supported on this platform: %v", err)
|
|
}
|
|
err := validateProxiedServerRootPath(link)
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "not a directory")
|
|
})
|
|
|
|
t.Run("symlink to dir accepted", func(t *testing.T) {
|
|
base := t.TempDir()
|
|
realDir := filepath.Join(base, "real-dir")
|
|
require.NoError(t, os.Mkdir(realDir, 0o755))
|
|
link := filepath.Join(base, "link-to-dir")
|
|
if err := os.Symlink(realDir, link); err != nil {
|
|
t.Skipf("symlink not supported on this platform: %v", err)
|
|
}
|
|
// os.Stat follows symlinks → reports as dir → accepted.
|
|
require.NoError(t, validateProxiedServerRootPath(link))
|
|
})
|
|
}
|
|
|
|
// TestResolveProxiedServerConfigPath_FollowsCustomRoot locks down the
|
|
// cascade: with no per-flag override, the config path's default fallback
|
|
// must compute against the resolved root, so --proxied-server-root-path
|
|
// alone moves config.yaml. The cascaded default is still NOT marked
|
|
// isCustom — bd still owns the YAML's lifecycle, just under a custom root.
|
|
// When the per-flag override IS set, it wins regardless of the root.
|
|
func TestResolveProxiedServerConfigPath_FollowsCustomRoot(t *testing.T) {
|
|
t.Run("custom root cascades into default config path", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "")
|
|
t.Setenv("BEADS_PROXIED_SERVER_ROOT_PATH", "")
|
|
bd := t.TempDir()
|
|
customRoot := filepath.Join(bd, "alt-root")
|
|
writeProxiedClientInfo(t, bd, &configfile.ProxiedServerClientInfo{RootPath: customRoot})
|
|
path, isCustom, err := resolveProxiedServerConfigPath(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, filepath.Join(customRoot, "config.yaml"), path)
|
|
assert.False(t, isCustom, "cascaded default is NOT user-owned")
|
|
})
|
|
|
|
t.Run("custom root via env cascades", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "")
|
|
bd := t.TempDir()
|
|
envRoot := filepath.Join(bd, "env-root")
|
|
t.Setenv("BEADS_PROXIED_SERVER_ROOT_PATH", envRoot)
|
|
path, isCustom, err := resolveProxiedServerConfigPath(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, filepath.Join(envRoot, "config.yaml"), path)
|
|
assert.False(t, isCustom)
|
|
})
|
|
|
|
t.Run("per-flag config override wins over root cascade", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "")
|
|
t.Setenv("BEADS_PROXIED_SERVER_ROOT_PATH", "")
|
|
bd := t.TempDir()
|
|
writeProxiedClientInfo(t, bd, &configfile.ProxiedServerClientInfo{
|
|
RootPath: filepath.Join(bd, "alt-root"),
|
|
ConfigPath: "/etc/dolt/explicit.yaml",
|
|
})
|
|
path, isCustom, err := resolveProxiedServerConfigPath(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "/etc/dolt/explicit.yaml", path)
|
|
assert.True(t, isCustom, "explicit override is user-owned")
|
|
})
|
|
|
|
t.Run("no overrides falls back to <beadsDir>/dolt (preserves pre-cascade default)", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "")
|
|
t.Setenv("BEADS_PROXIED_SERVER_ROOT_PATH", "")
|
|
bd := t.TempDir()
|
|
path, isCustom, err := resolveProxiedServerConfigPath(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, filepath.Join(bd, "dolt", "config.yaml"), path)
|
|
assert.False(t, isCustom)
|
|
})
|
|
}
|
|
|
|
// TestResolveProxiedServerLogPath_FollowsCustomRoot mirrors the config
|
|
// cascade test for the log resolver.
|
|
func TestResolveProxiedServerLogPath_FollowsCustomRoot(t *testing.T) {
|
|
t.Run("custom root cascades into default log path", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_LOG", "")
|
|
t.Setenv("BEADS_PROXIED_SERVER_ROOT_PATH", "")
|
|
bd := t.TempDir()
|
|
customRoot := filepath.Join(bd, "alt-root")
|
|
writeProxiedClientInfo(t, bd, &configfile.ProxiedServerClientInfo{RootPath: customRoot})
|
|
path, isCustom, err := resolveProxiedServerLogPath(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, filepath.Join(customRoot, "server.log"), path)
|
|
assert.False(t, isCustom, "cascaded default is NOT user-owned")
|
|
})
|
|
|
|
t.Run("custom root via env cascades", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_LOG", "")
|
|
bd := t.TempDir()
|
|
envRoot := filepath.Join(bd, "env-root")
|
|
t.Setenv("BEADS_PROXIED_SERVER_ROOT_PATH", envRoot)
|
|
path, isCustom, err := resolveProxiedServerLogPath(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, filepath.Join(envRoot, "server.log"), path)
|
|
assert.False(t, isCustom)
|
|
})
|
|
|
|
t.Run("per-flag log override wins over root cascade", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_LOG", "")
|
|
t.Setenv("BEADS_PROXIED_SERVER_ROOT_PATH", "")
|
|
bd := t.TempDir()
|
|
writeProxiedClientInfo(t, bd, &configfile.ProxiedServerClientInfo{
|
|
RootPath: filepath.Join(bd, "alt-root"),
|
|
LogPath: "/var/log/explicit.log",
|
|
})
|
|
path, isCustom, err := resolveProxiedServerLogPath(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "/var/log/explicit.log", path)
|
|
assert.True(t, isCustom)
|
|
})
|
|
|
|
t.Run("no overrides falls back to <beadsDir>/dolt (preserves pre-cascade default)", func(t *testing.T) {
|
|
t.Setenv("BEADS_PROXIED_SERVER_LOG", "")
|
|
t.Setenv("BEADS_PROXIED_SERVER_ROOT_PATH", "")
|
|
bd := t.TempDir()
|
|
path, isCustom, err := resolveProxiedServerLogPath(bd)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, filepath.Join(bd, "dolt", "server.log"), path)
|
|
assert.False(t, isCustom)
|
|
})
|
|
}
|
|
|
|
func TestRenderProxiedServerConfig_RecordsChosenPort(t *testing.T) {
|
|
body, err := renderProxiedServerConfig(54321)
|
|
require.NoError(t, err)
|
|
require.True(t, isManagedProxiedServerConfig(body))
|
|
port, _, ok := cutManagedPortRecord(body[len(managedProxiedServerConfigMarker):])
|
|
require.True(t, ok, "generated config must carry the chosen-port record")
|
|
assert.Equal(t, 54321, port)
|
|
}
|
|
|
|
func TestRenderLegacyProxiedServerConfig_IsMarkerPlusYAML(t *testing.T) {
|
|
legacy, err := renderLegacyProxiedServerConfig(40001)
|
|
require.NoError(t, err)
|
|
require.True(t, isManagedProxiedServerConfig(legacy))
|
|
_, _, ok := cutManagedPortRecord(legacy[len(managedProxiedServerConfigMarker):])
|
|
assert.False(t, ok, "the legacy form has no port record")
|
|
cfg, err := servercfg.NewYamlConfig(legacy)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, 40001, cfg.Port())
|
|
}
|
|
|
|
func TestCutManagedPortRecord(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
line string
|
|
port int
|
|
ok bool
|
|
}{
|
|
{line: managedPortRecordPrefix + "40001 (Beads chose this port.)\n", port: 40001, ok: true},
|
|
{line: managedPortRecordPrefix + "40001\n", port: 40001, ok: true},
|
|
{line: managedPortRecordPrefix + "4000abc\n"},
|
|
{line: managedPortRecordPrefix + "0\n"},
|
|
{line: managedPortRecordPrefix + "70000\n"},
|
|
{line: managedPortRecordPrefix + "\n"},
|
|
{line: managedPortRecordPrefix + "40001"},
|
|
{line: "log_level: info\n"},
|
|
} {
|
|
port, _, ok := cutManagedPortRecord([]byte(tc.line + "log_level: info\n"))
|
|
if tc.line == managedPortRecordPrefix+"40001" {
|
|
// No newline before the YAML in this case: the record runs into it.
|
|
port, _, ok = cutManagedPortRecord([]byte(tc.line))
|
|
}
|
|
assert.Equal(t, tc.ok, ok, "%q", tc.line)
|
|
if tc.ok {
|
|
assert.Equal(t, tc.port, port, "%q", tc.line)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestManagedPortConflictPolicy_AllowsBeadsChosenPort(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
body func(t *testing.T) []byte
|
|
}{
|
|
{name: "generated config", body: func(t *testing.T) []byte {
|
|
b, err := renderProxiedServerConfig(40001)
|
|
require.NoError(t, err)
|
|
return b
|
|
}},
|
|
{name: "generated config edited elsewhere, port unchanged", body: func(t *testing.T) []byte {
|
|
b, err := renderProxiedServerConfig(40001)
|
|
require.NoError(t, err)
|
|
return []byte(strings.Replace(string(b), "log_level: info", "log_level: debug", 1))
|
|
}},
|
|
{name: "generated config with an environment placeholder, port unchanged", body: func(t *testing.T) []byte {
|
|
// The server copies the raw text, so the placeholder's value never
|
|
// lands in the runtime config.
|
|
b, err := renderProxiedServerConfig(40001)
|
|
require.NoError(t, err)
|
|
return []byte(strings.Replace(string(b), "log_level: info", "log_level: ${BD_TEST_LOG_LEVEL}", 1))
|
|
}},
|
|
{name: "unedited config generated before the record existed", body: func(t *testing.T) []byte {
|
|
b, err := renderLegacyProxiedServerConfig(40001)
|
|
require.NoError(t, err)
|
|
return b
|
|
}},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Setenv("BD_TEST_LOG_LEVEL", "info")
|
|
path := filepath.Join(t.TempDir(), "config.yaml")
|
|
body := tc.body(t)
|
|
require.NoError(t, os.WriteFile(path, body, 0o600))
|
|
require.NoError(t, managedPortConflictPolicy(path, 40001))
|
|
after, err := os.ReadFile(path)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, string(body), string(after), "the policy must never modify the config")
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestManagedPortConflictPolicy_PinsOtherPorts(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
body func(t *testing.T) []byte
|
|
inUse int
|
|
errHas []string
|
|
}{
|
|
{
|
|
name: "hand-set port",
|
|
body: func(t *testing.T) []byte {
|
|
b, err := renderProxiedServerConfig(40001)
|
|
require.NoError(t, err)
|
|
// The YAML key, not the "...-listener-port: 40001" record above it.
|
|
return []byte(strings.Replace(string(b), "\n port: 40001", "\n port: 40002", 1))
|
|
},
|
|
inUse: 40002,
|
|
errHas: []string{"set by hand", "free port 40002", "delete"},
|
|
},
|
|
{
|
|
name: "hand-edited config generated before the record existed",
|
|
body: func(t *testing.T) []byte {
|
|
b, err := renderLegacyProxiedServerConfig(40001)
|
|
require.NoError(t, err)
|
|
return []byte(strings.Replace(string(b), "log_level: info", "log_level: debug", 1))
|
|
},
|
|
inUse: 40001,
|
|
errHas: []string{"edited by hand", "pinned", "delete"},
|
|
},
|
|
{
|
|
name: "operator config",
|
|
body: func(t *testing.T) []byte {
|
|
return []byte("listener:\n host: 127.0.0.1\n port: 40001\n")
|
|
},
|
|
inUse: 40001,
|
|
errHas: []string{"not a Beads-generated config", "set listener.port in"},
|
|
},
|
|
{
|
|
name: "in-use port is not the config's port",
|
|
body: func(t *testing.T) []byte {
|
|
b, err := renderProxiedServerConfig(40001)
|
|
require.NoError(t, err)
|
|
return b
|
|
},
|
|
inUse: 40009,
|
|
errHas: []string{"not the in-use port"},
|
|
},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Setenv("BD_TEST_LOG_LEVEL", "info")
|
|
path := filepath.Join(t.TempDir(), "config.yaml")
|
|
body := tc.body(t)
|
|
require.NoError(t, os.WriteFile(path, body, 0o600))
|
|
err := managedPortConflictPolicy(path, tc.inUse)
|
|
require.Error(t, err)
|
|
for _, want := range tc.errHas {
|
|
assert.Contains(t, err.Error(), want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// legacyGeneratedConfigV13 is, byte for byte, the config.yaml that bd v1.3.0
|
|
// and v1.3.1 generate for port 40001 (marker plus yaml.Marshal of the
|
|
// rendered servercfg.YAMLConfig, before the port record existed). It is
|
|
// frozen here so a dolt or yaml upgrade that changes Marshal output cannot
|
|
// silently stop existing workspaces from matching the legacy form.
|
|
const legacyGeneratedConfigV13 = "# Generated by Beads on first run - safe to delete (will be regenerated). " +
|
|
"Never rewritten afterward; hand edits are preserved. See gastownhall/beads#4986.\n" +
|
|
"log_level: info\nbehavior:\n auto_gc_behavior:\n archive_level: 0\nlistener:\n host: 127.0.0.1\n port: 40001\nuser_session_vars: []\njwks: []\n"
|
|
|
|
func TestManagedPortConflictPolicy_LegacyV13GoldenIsEligible(t *testing.T) {
|
|
legacy, err := renderLegacyProxiedServerConfig(40001)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, legacyGeneratedConfigV13, string(legacy), "legacy render drifted from what v1.3.x wrote")
|
|
|
|
path := filepath.Join(t.TempDir(), "config.yaml")
|
|
require.NoError(t, os.WriteFile(path, []byte(legacyGeneratedConfigV13), 0o600))
|
|
require.NoError(t, managedPortConflictPolicy(path, 40001))
|
|
}
|
|
|
|
// TestManagedPortConflictPolicy_ConsecutiveCollisions drives a real
|
|
// server.DoltServer with bd's real policy through repeated port conflicts:
|
|
// the stand-in dolt reports its port taken on every launch, so every move
|
|
// Start makes collides again. The policy must be consulted about the
|
|
// operator's port only, so Start keeps moving until it gives up after its
|
|
// port budget, rather than being refused on the second move because the
|
|
// runtime port is not config.yaml's port. config.yaml must stay unchanged.
|
|
func TestManagedPortConflictPolicy_ConsecutiveCollisions(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("stand-in dolt is a shell script")
|
|
}
|
|
dir := t.TempDir()
|
|
fakeDolt := filepath.Join(dir, "dolt")
|
|
launches := filepath.Join(dir, "launches")
|
|
script := `#!/bin/sh
|
|
case "$1" in
|
|
config) echo fake; exit 0 ;;
|
|
init) exit 0 ;;
|
|
sql-server)
|
|
cfg="$3"
|
|
port=$(sed -n 's/^ *port: *\([0-9][0-9]*\).*/\1/p' "$cfg" | head -n 1)
|
|
echo "$port" >> "` + launches + `"
|
|
echo "Port $port already in use." >&2
|
|
exit 1 ;;
|
|
esac
|
|
exit 2
|
|
`
|
|
require.NoError(t, os.WriteFile(fakeDolt, []byte(script), 0o755))
|
|
|
|
root := filepath.Join(dir, "root")
|
|
require.NoError(t, os.MkdirAll(root, 0o755))
|
|
cfgPath := filepath.Join(root, "config.yaml")
|
|
body, err := renderProxiedServerConfig(40001)
|
|
require.NoError(t, err)
|
|
require.NoError(t, os.WriteFile(cfgPath, body, 0o600))
|
|
|
|
s, err := server.NewDoltServer(fakeDolt, root, cfgPath, filepath.Join(dir, "server.log"), 0, "")
|
|
require.NoError(t, err)
|
|
s.SetPortConflictPolicy(managedPortConflictPolicy)
|
|
t.Cleanup(func() { _ = s.Stop(context.Background()) })
|
|
|
|
err = s.Start(context.Background())
|
|
require.Error(t, err)
|
|
assert.ErrorIs(t, err, server.ErrPortInUse)
|
|
assert.Contains(t, err.Error(), "gave up after 5 ports", "the policy must not refuse the second and later moves")
|
|
assert.NotContains(t, err.Error(), "not the in-use port")
|
|
|
|
got, err := os.ReadFile(launches)
|
|
require.NoError(t, err)
|
|
ports := strings.Fields(string(got))
|
|
require.Len(t, ports, 5, "one launch per port: %v", ports)
|
|
assert.Equal(t, "40001", ports[0], "the first launch uses config.yaml's port")
|
|
seen := map[string]bool{}
|
|
for _, p := range ports {
|
|
assert.False(t, seen[p], "port %s launched twice: %v", p, ports)
|
|
seen[p] = true
|
|
}
|
|
|
|
after, err := os.ReadFile(cfgPath)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, string(body), string(after), "config.yaml must never be modified")
|
|
_, err = os.Stat(filepath.Join(root, server.RuntimeConfigFileName))
|
|
assert.True(t, os.IsNotExist(err), "a failed Start must not leave a runtime config")
|
|
}
|