/cmd/bd: remove dolt version and config stuff in proxied-server mode
This commit is contained in:
parent
80af94c0a7
commit
68afa3d51f
3 changed files with 77 additions and 667 deletions
|
|
@ -19,162 +19,18 @@ import (
|
|||
"github.com/steveyegge/beads/internal/storage/dbproxy/proxy"
|
||||
)
|
||||
|
||||
// archiveLevelSupportNoticeFormat is the one-time stderr notice emitted when
|
||||
// generating a fresh managed-server config.yaml against an external dolt
|
||||
// that predates auto_gc_behavior.archive_level support. Never fires for an
|
||||
// existing config.yaml (ensureProxiedServerConfig only renders on first
|
||||
// bootstrap), so this is not a per-invocation warning.
|
||||
const archiveLevelSupportNoticeFormat = "Info: external dolt predates archive_level config support (need >= Dolt %s); " +
|
||||
"this managed server's background auto-GC may still produce zstd archives.\n"
|
||||
|
||||
const managedListenerHostRemediation = "set listener.host to 127.0.0.1 (or another 127.0.0.0/8 / ::1 " +
|
||||
"loopback address); for non-local topology use " +
|
||||
"--proxied-server-external-host with --proxied-server-external-port, " +
|
||||
"--proxied-server-external-socket-path, or switch to non-proxied server mode"
|
||||
|
||||
// managedProxiedServerConfigMarker is written as a YAML comment header on
|
||||
// every config.yaml Beads itself generates (renderProxiedServerConfig).
|
||||
// YAML comments are ignored by Dolt's own parser, so this is invisible to
|
||||
// dolt sql-server; Beads uses it to tell its own config.yaml apart from a
|
||||
// user-supplied one (env var / sidecar custom path, or a default-path file
|
||||
// pre-dating this marker) before deciding whether it is safe to
|
||||
// automatically reconcile auto_gc_behavior.archive_level on load
|
||||
// (reconcileManagedProxiedServerConfig) versus leaving the file untouched
|
||||
// and only warning (warnUnmanagedProxiedServerConfig). See
|
||||
// gastownhall/beads#4986.
|
||||
const managedProxiedServerConfigMarker = "# Managed by Beads - safe to delete (will be regenerated). " +
|
||||
"Hand edits may be lost on upgrade. See gastownhall/beads#4986.\n"
|
||||
|
||||
// isManagedProxiedServerConfig reports whether body was generated by
|
||||
// renderProxiedServerConfig (carries managedProxiedServerConfigMarker as
|
||||
// its first line) rather than hand-written or supplied by a user.
|
||||
func isManagedProxiedServerConfig(body []byte) bool {
|
||||
return bytes.HasPrefix(body, []byte(managedProxiedServerConfigMarker))
|
||||
}
|
||||
|
||||
// reconcileManagedProxiedServerConfig parse-modify-rewrites a Beads-managed
|
||||
// config.yaml's auto_gc_behavior.archive_level so it matches the resolved
|
||||
// external dolt's capability, preserving every other field (including
|
||||
// port, and sibling auto_gc_behavior fields such as a user-set "enable")
|
||||
// by round tripping through the servercfg YAML structs rather than string
|
||||
// surgery:
|
||||
//
|
||||
// - archiveLevelSupported and archive_level is absent: set it to 0
|
||||
// (auto-GC stays enabled), creating the auto_gc_behavior block if
|
||||
// necessary but preserving any existing sibling fields on it.
|
||||
// - !archiveLevelSupported and archive_level is present: clear only
|
||||
// archive_level, so a dolt downgrade cannot hard-fail on
|
||||
// yaml.UnmarshalStrict seeing a key its own older YAMLConfig struct
|
||||
// does not have. The block itself is only removed if doing so leaves
|
||||
// it entirely empty (no enable, no incremental_file_size either).
|
||||
//
|
||||
// Deliberately inspects only ArchiveLevel_, not "the whole block is
|
||||
// present" — an earlier version of this function tested
|
||||
// cfg.BehaviorConfig.AutoGCBehavior != nil, which meant a block carrying
|
||||
// only a user-set "enable" (no archive_level) both failed to gain
|
||||
// archive_level when supported, and lost its "enable" setting entirely
|
||||
// when the whole block was stripped on an unsupported binary.
|
||||
//
|
||||
// Returns the rewritten bytes (with the marker re-prepended) and whether a
|
||||
// rewrite is actually needed; callers should skip the write when unchanged.
|
||||
func reconcileManagedProxiedServerConfig(body []byte, archiveLevelSupported bool) ([]byte, bool, error) {
|
||||
cfg, err := servercfg.NewYamlConfig(body)
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("parse: %w", err)
|
||||
}
|
||||
|
||||
gc := cfg.BehaviorConfig.AutoGCBehavior
|
||||
hasArchiveLevel := gc != nil && gc.ArchiveLevel_ != nil
|
||||
changed := false
|
||||
|
||||
switch {
|
||||
case archiveLevelSupported && !hasArchiveLevel:
|
||||
if gc == nil {
|
||||
gc = &servercfg.AutoGCBehaviorYAMLConfig{}
|
||||
cfg.BehaviorConfig.AutoGCBehavior = gc
|
||||
}
|
||||
archiveLevel := 0
|
||||
gc.ArchiveLevel_ = &archiveLevel
|
||||
changed = true
|
||||
case !archiveLevelSupported && hasArchiveLevel:
|
||||
gc.ArchiveLevel_ = nil
|
||||
if isAutoGCBehaviorEmpty(gc) {
|
||||
cfg.BehaviorConfig.AutoGCBehavior = nil
|
||||
}
|
||||
changed = true
|
||||
}
|
||||
|
||||
if !changed {
|
||||
return body, false, nil
|
||||
}
|
||||
|
||||
rendered, err := yaml.Marshal(cfg)
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("render: %w", err)
|
||||
}
|
||||
return append([]byte(managedProxiedServerConfigMarker), rendered...), true, nil
|
||||
}
|
||||
|
||||
// isAutoGCBehaviorEmpty reports whether gc has no fields set at all, i.e.
|
||||
// it is now safe to omit the whole auto_gc_behavior block rather than
|
||||
// render an empty "{}" or "auto_gc_behavior:" with no children.
|
||||
func isAutoGCBehaviorEmpty(gc *servercfg.AutoGCBehaviorYAMLConfig) bool {
|
||||
return gc.Enable_ == nil && gc.ArchiveLevel_ == nil && gc.IncrementalFileSize_ == nil
|
||||
}
|
||||
|
||||
// warnUnmanagedProxiedServerConfig emits a one-line notice for a config.yaml
|
||||
// Beads never rewrites (no managedProxiedServerConfigMarker: a user-custom
|
||||
// path, or a default-path file predating this marker). It surfaces the same
|
||||
// two risks reconcileManagedProxiedServerConfig fixes automatically for a
|
||||
// Beads-owned config, without ever touching the file:
|
||||
// - archiveLevelSupported and archive_level is absent: zstd auto-GC may
|
||||
// still be active; tell the user how to opt in.
|
||||
// - !archiveLevelSupported and archive_level is present: the resolved
|
||||
// external dolt may refuse to start on this file (yaml.UnmarshalStrict
|
||||
// on an unrecognized key).
|
||||
//
|
||||
// Deliberately checks only ArchiveLevel_ (via autoGCArchiveLevelIsSet), not
|
||||
// "the whole auto_gc_behavior block is present" — the same field-vs-block
|
||||
// distinction reconcileManagedProxiedServerConfig applies. A block carrying
|
||||
// only a user-set "enable" (no archive_level) would otherwise both miss the
|
||||
// zstd-risk warning on a capable binary, and get a false
|
||||
// refuse-to-start warning on an incapable one.
|
||||
//
|
||||
// cfg must be the ALREADY-PARSED config (via servercfg.YamlConfigFromFile,
|
||||
// not NewYamlConfig on raw bytes) — callers own an unmanaged file's parse,
|
||||
// and that parse must be interpolation-aware (see the ensureProxiedServerConfig
|
||||
// doc comment on why raw-bytes parsing is wrong for these files).
|
||||
func warnUnmanagedProxiedServerConfig(path string, cfg servercfg.ServerConfig, archiveLevelSupported bool) {
|
||||
hasArchiveLevel := autoGCArchiveLevelIsSet(cfg)
|
||||
switch {
|
||||
case archiveLevelSupported && !hasArchiveLevel:
|
||||
fmt.Fprintf(os.Stderr,
|
||||
"Warning: %s is not Beads-managed (no marker); zstd auto-GC may still be active. "+
|
||||
"To opt in, add 'auto_gc_behavior: {archive_level: 0}' under the top-level 'behavior:' key.\n",
|
||||
path)
|
||||
case !archiveLevelSupported && hasArchiveLevel:
|
||||
fmt.Fprintf(os.Stderr,
|
||||
"Warning: %s sets auto_gc_behavior.archive_level, but the resolved external dolt predates support "+
|
||||
"for it (need >= Dolt %s); the server may refuse to start. Remove the key or upgrade dolt.\n",
|
||||
path, doltserver.MinDoltVersionForArchiveLevelConfig)
|
||||
}
|
||||
}
|
||||
|
||||
// autoGCArchiveLevelIsSet reports whether cfg's auto_gc_behavior.archive_level
|
||||
// key is explicitly set — not just "the auto_gc_behavior block is present"
|
||||
// (a block can carry only a sibling field like "enable"). Both
|
||||
// servercfg.YamlConfigFromFile and servercfg.NewYamlConfig always return a
|
||||
// *servercfg.YAMLConfig underneath the ServerConfig interface in the
|
||||
// pinned module, so the type assertion below is expected to succeed; if it
|
||||
// ever doesn't, this falls back to the coarser "block is present" test
|
||||
// rather than panicking.
|
||||
func autoGCArchiveLevelIsSet(cfg servercfg.ServerConfig) bool {
|
||||
if yc, ok := cfg.(*servercfg.YAMLConfig); ok {
|
||||
return yc.BehaviorConfig.AutoGCBehavior != nil && yc.BehaviorConfig.AutoGCBehavior.ArchiveLevel_ != nil
|
||||
}
|
||||
return cfg.AutoGCBehavior() != nil
|
||||
}
|
||||
|
||||
const (
|
||||
proxiedServerConfigName = "config.yaml"
|
||||
proxiedServerLogName = "server.log"
|
||||
|
|
@ -249,29 +105,8 @@ func resolveProxiedServerLogPath(beadsDir string) (path string, isCustom bool, e
|
|||
return filepath.Join(root, proxiedServerLogName), false, nil
|
||||
}
|
||||
|
||||
// ensureProxiedServerConfig resolves (creating if needed) the managed
|
||||
// sql-server config.yaml. archiveLevelSupported gates whether a freshly
|
||||
// generated config sets auto_gc_behavior.archive_level: 0 (see
|
||||
// renderProxiedServerConfig).
|
||||
//
|
||||
// Parsing an EXISTING file (custom path, or a pre-marker default-path file
|
||||
// we only warn about — never a Beads-managed one) MUST go through
|
||||
// servercfg.YamlConfigFromFile, not servercfg.NewYamlConfig on raw bytes:
|
||||
// YamlConfigFromFile expands ${VAR}-style environment placeholders before
|
||||
// parsing (servercfg/env_interpolate.go), and a user's custom config is
|
||||
// entitled to use that syntax the same way `dolt sql-server --config`
|
||||
// itself would accept it. NewYamlConfig on raw bytes is only safe for a
|
||||
// Beads-managed file (marker present), because Beads itself never writes
|
||||
// interpolation syntax into a file it generates.
|
||||
//
|
||||
// The managed listener policy (validateManagedServerConfigPolicy) is applied
|
||||
// exactly once, on the resolved path, via the interpolation-aware
|
||||
// validateProxiedServerConfig — never on raw pre-interpolation bytes. A
|
||||
// marker config whose listener.host is a ${VAR} placeholder is therefore
|
||||
// judged on what it expands to, matching the degrade-gracefully contract for
|
||||
// hand-edited managed files below.
|
||||
func ensureProxiedServerConfig(beadsDir string, archiveLevelSupported bool) (string, error) {
|
||||
path, err := resolveOrCreateProxiedServerConfig(beadsDir, archiveLevelSupported)
|
||||
func ensureProxiedServerConfig(beadsDir string) (string, error) {
|
||||
path, err := resolveOrCreateProxiedServerConfig(beadsDir)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
@ -281,11 +116,7 @@ func ensureProxiedServerConfig(beadsDir string, archiveLevelSupported bool) (str
|
|||
return path, nil
|
||||
}
|
||||
|
||||
// resolveOrCreateProxiedServerConfig is ensureProxiedServerConfig without the
|
||||
// final policy gate: it only resolves, generates, reconciles, and warns. All
|
||||
// callers go through the exported entry point so the policy check cannot be
|
||||
// skipped or duplicated per branch.
|
||||
func resolveOrCreateProxiedServerConfig(beadsDir string, archiveLevelSupported bool) (string, error) {
|
||||
func resolveOrCreateProxiedServerConfig(beadsDir string) (string, error) {
|
||||
path, isCustom, err := resolveProxiedServerConfigPath(beadsDir)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
|
@ -299,13 +130,6 @@ func resolveOrCreateProxiedServerConfig(beadsDir string, archiveLevelSupported b
|
|||
if !info.Mode().IsRegular() {
|
||||
return "", fmt.Errorf("ensureProxiedServerConfig: custom config %s: not a regular file", path)
|
||||
}
|
||||
cfg, err := servercfg.YamlConfigFromFile(filesys.LocalFS, path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("ensureProxiedServerConfig: custom config %s: parse: %w", path, err)
|
||||
}
|
||||
// A custom config is always user-owned, whether or not it happens to
|
||||
// carry the marker — never rewritten, only warned about.
|
||||
warnUnmanagedProxiedServerConfig(path, cfg, archiveLevelSupported)
|
||||
return path, nil
|
||||
}
|
||||
|
||||
|
|
@ -316,58 +140,6 @@ func resolveOrCreateProxiedServerConfig(beadsDir string, archiveLevelSupported b
|
|||
|
||||
switch _, err := os.Stat(path); {
|
||||
case err == nil:
|
||||
// The marker is a literal comment line Beads itself writes and never
|
||||
// interpolates — safe to check on raw bytes before deciding which
|
||||
// (interpolation-aware or not) parse path applies below.
|
||||
body, err := os.ReadFile(path) //nolint:gosec // G304: path is Beads' own default config.yaml location, not user-request input
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("ensureProxiedServerConfig: read existing config %s: %w", path, err)
|
||||
}
|
||||
if isManagedProxiedServerConfig(body) {
|
||||
// Beads-managed: never carries interpolation syntax, so raw-bytes
|
||||
// parsing (inside reconcileManagedProxiedServerConfig) is safe and
|
||||
// avoids a redundant interpolation pass. BUT the marker only
|
||||
// proves Beads generated the file originally — an operator can
|
||||
// still hand-edit a managed file afterward (e.g. add a ${VAR}
|
||||
// placeholder), which the non-interpolating raw parse cannot
|
||||
// handle. That must degrade gracefully rather than fail the
|
||||
// whole command: dolt itself would still start fine against
|
||||
// such a file once its own interpolation pass runs.
|
||||
reconciled, changed, rerr := reconcileManagedProxiedServerConfig(body, archiveLevelSupported)
|
||||
if rerr != nil {
|
||||
fmt.Fprintf(os.Stderr,
|
||||
"Warning: %s carries the Beads-managed marker but failed to parse (%v); treating as "+
|
||||
"unmanaged (not rewriting). If this file was hand-edited (e.g. with ${VAR} "+
|
||||
"placeholders), that's expected — dolt itself will interpolate it at startup.\n",
|
||||
path, rerr)
|
||||
cfg, cerr := servercfg.YamlConfigFromFile(filesys.LocalFS, path)
|
||||
if cerr != nil {
|
||||
return "", fmt.Errorf("ensureProxiedServerConfig: existing config %s: parse: %w", path, cerr)
|
||||
}
|
||||
warnUnmanagedProxiedServerConfig(path, cfg, archiveLevelSupported)
|
||||
return path, nil
|
||||
}
|
||||
if changed {
|
||||
// os.Rename (inside atomicWriteFile) does not follow a
|
||||
// symlink at its destination — it replaces whatever is AT
|
||||
// that path, symlink or not. Resolve to the physical file
|
||||
// first so a symlinked managed config is rewritten through
|
||||
// the symlink (the symlink itself survives) rather than
|
||||
// having the symlink replaced by a regular file.
|
||||
if werr := atomicWriteFile(resolveConfigWriteTarget(path), reconciled); werr != nil {
|
||||
return "", fmt.Errorf("ensureProxiedServerConfig: rewrite managed config %s: %w", path, werr)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Pre-marker default-path file (created by a Beads version before
|
||||
// this fix) — treat like a custom config: interpolation-aware
|
||||
// parse, never rewritten, only warned about.
|
||||
cfg, err := servercfg.YamlConfigFromFile(filesys.LocalFS, path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("ensureProxiedServerConfig: existing config %s: parse: %w", path, err)
|
||||
}
|
||||
warnUnmanagedProxiedServerConfig(path, cfg, archiveLevelSupported)
|
||||
}
|
||||
return path, nil
|
||||
case !os.IsNotExist(err):
|
||||
return "", fmt.Errorf("ensureProxiedServerConfig: stat %s: %w", path, err)
|
||||
|
|
@ -378,19 +150,10 @@ func resolveOrCreateProxiedServerConfig(beadsDir string, archiveLevelSupported b
|
|||
return "", fmt.Errorf("ensureProxiedServerConfig: pick free port: %w", err)
|
||||
}
|
||||
|
||||
if !archiveLevelSupported {
|
||||
fmt.Fprintf(os.Stderr, archiveLevelSupportNoticeFormat, doltserver.MinDoltVersionForArchiveLevelConfig)
|
||||
}
|
||||
|
||||
body, err := renderProxiedServerConfig(port, archiveLevelSupported)
|
||||
body, err := renderProxiedServerConfig(port)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("ensureProxiedServerConfig: render YAML: %w", err)
|
||||
}
|
||||
// See the reconcile call site above for why the write target is
|
||||
// resolved through any symlink first: this branch only runs after
|
||||
// os.Stat(path) reported IsNotExist, which is also true for a dangling
|
||||
// symlink — resolveConfigWriteTarget falls back to path itself in that
|
||||
// case, so a dangling symlink is simply replaced (self-healed) here.
|
||||
if err := atomicWriteFile(resolveConfigWriteTarget(path), body); err != nil {
|
||||
return "", fmt.Errorf("ensureProxiedServerConfig: write %s: %w", path, err)
|
||||
}
|
||||
|
|
@ -556,17 +319,7 @@ func validateProxiedServerLogPath(path string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// renderProxiedServerConfig renders the managed sql-server config.yaml. When
|
||||
// archiveLevelSupported is true it sets auto_gc_behavior.archive_level: 0,
|
||||
// so this server's background auto-GC writes classic Snappy table files
|
||||
// instead of zstd archives (gastownhall/beads#4986); auto-GC itself stays
|
||||
// enabled (the sibling "enable" key is left unset, which Dolt defaults to
|
||||
// true). When false, the key is omitted entirely — an older external dolt's
|
||||
// own YAMLConfig struct may not have this field, and Dolt's config loader
|
||||
// uses yaml.UnmarshalStrict, so an unrecognized key is a hard parse error at
|
||||
// server startup rather than a silently-ignored one. Callers must gate
|
||||
// archiveLevelSupported on doltserver.SupportsArchiveLevelConfig.
|
||||
func renderProxiedServerConfig(port int, archiveLevelSupported bool) ([]byte, error) {
|
||||
func renderProxiedServerConfig(port int) ([]byte, error) {
|
||||
host := proxiedServerListenerHost
|
||||
logLevel := string(servercfg.LogLevel_Info)
|
||||
yc := &servercfg.YAMLConfig{
|
||||
|
|
@ -576,11 +329,9 @@ func renderProxiedServerConfig(port int, archiveLevelSupported bool) ([]byte, er
|
|||
PortNumber: &port,
|
||||
},
|
||||
}
|
||||
if archiveLevelSupported {
|
||||
archiveLevel := 0
|
||||
yc.BehaviorConfig.AutoGCBehavior = &servercfg.AutoGCBehaviorYAMLConfig{
|
||||
ArchiveLevel_: &archiveLevel,
|
||||
}
|
||||
archiveLevel := 0
|
||||
yc.BehaviorConfig.AutoGCBehavior = &servercfg.AutoGCBehaviorYAMLConfig{
|
||||
ArchiveLevel_: &archiveLevel,
|
||||
}
|
||||
body, err := yaml.Marshal(yc)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
|
|
@ -13,35 +14,10 @@ import (
|
|||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// renderManagedConfigWithAutoGCBehavior builds a Beads-managed config.yaml
|
||||
// (marker included) whose auto_gc_behavior block is exactly gc — unlike
|
||||
// renderProxiedServerConfig, which only ever sets ArchiveLevel_. Used to
|
||||
// seed the sibling-field-preservation tests below (a user-set "enable"
|
||||
// alongside — or in place of — archive_level).
|
||||
func renderManagedConfigWithAutoGCBehavior(t *testing.T, port int, gc *servercfg.AutoGCBehaviorYAMLConfig) []byte {
|
||||
t.Helper()
|
||||
host := proxiedServerListenerHost
|
||||
logLevel := string(servercfg.LogLevel_Info)
|
||||
yc := &servercfg.YAMLConfig{
|
||||
LogLevelStr: &logLevel,
|
||||
ListenerConfig: servercfg.ListenerYAMLConfig{
|
||||
HostStr: &host,
|
||||
PortNumber: &port,
|
||||
},
|
||||
BehaviorConfig: servercfg.BehaviorYAMLConfig{
|
||||
AutoGCBehavior: gc,
|
||||
},
|
||||
}
|
||||
body, err := yaml.Marshal(yc)
|
||||
require.NoError(t, err)
|
||||
return append([]byte(managedProxiedServerConfigMarker), body...)
|
||||
}
|
||||
|
||||
func TestRenderProxiedServerConfig_RoundTrips(t *testing.T) {
|
||||
body, err := renderProxiedServerConfig(54321, true)
|
||||
body, err := renderProxiedServerConfig(54321)
|
||||
require.NoError(t, err)
|
||||
|
||||
cfg, err := servercfg.NewYamlConfig(body)
|
||||
|
|
@ -52,41 +28,23 @@ func TestRenderProxiedServerConfig_RoundTrips(t *testing.T) {
|
|||
assert.Equal(t, servercfg.LogLevel_Info, cfg.LogLevel(), "LogLevel mismatch")
|
||||
}
|
||||
|
||||
// TestRenderProxiedServerConfig_ArchiveLevel covers the actual Snappy-GC fix
|
||||
// (gastownhall/beads#4986): when archiveLevelSupported is true, the
|
||||
// generated config sets auto_gc_behavior.archive_level: 0 with auto-GC left
|
||||
// enabled; when false, the key is omitted entirely so an older external
|
||||
// dolt's yaml.UnmarshalStrict loader never sees an unrecognized key.
|
||||
func TestRenderProxiedServerConfig_ArchiveLevel(t *testing.T) {
|
||||
t.Run("supported: archive_level 0, auto-GC enabled", func(t *testing.T) {
|
||||
body, err := renderProxiedServerConfig(54321, true)
|
||||
require.NoError(t, err)
|
||||
assert.Contains(t, string(body), "archive_level: 0")
|
||||
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 be set when archiveLevelSupported is true")
|
||||
assert.Equal(t, 0, gc.ArchiveLevel())
|
||||
assert.True(t, gc.Enable(), "auto-GC must remain enabled")
|
||||
})
|
||||
|
||||
t.Run("unsupported: no auto_gc_behavior key at all", func(t *testing.T) {
|
||||
body, err := renderProxiedServerConfig(54321, false)
|
||||
require.NoError(t, err)
|
||||
assert.NotContains(t, string(body), "archive_level")
|
||||
assert.NotContains(t, string(body), "auto_gc_behavior")
|
||||
|
||||
cfg, err := servercfg.NewYamlConfig(body)
|
||||
require.NoError(t, err)
|
||||
assert.Nil(t, cfg.AutoGCBehavior())
|
||||
})
|
||||
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, true)
|
||||
path1, err := ensureProxiedServerConfig(beadsDir)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, filepath.Join(beadsDir, "dolt", "config.yaml"), path1)
|
||||
|
||||
|
|
@ -96,7 +54,7 @@ func TestEnsureProxiedServerConfig_CreatesAndIsIdempotent(t *testing.T) {
|
|||
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, true)
|
||||
path2, err := ensureProxiedServerConfig(beadsDir)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, path1, path2)
|
||||
|
||||
|
|
@ -116,12 +74,12 @@ func TestEnsureProxiedServerConfig_ReusesExistingConfig(t *testing.T) {
|
|||
root := filepath.Join(beadsDir, "dolt")
|
||||
require.NoError(t, os.MkdirAll(root, 0o755))
|
||||
|
||||
existing, err := renderProxiedServerConfig(45678, true)
|
||||
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, true)
|
||||
path, err := ensureProxiedServerConfig(beadsDir)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, cfgPath, path)
|
||||
|
||||
|
|
@ -130,10 +88,8 @@ func TestEnsureProxiedServerConfig_ReusesExistingConfig(t *testing.T) {
|
|||
assert.Equal(t, existing, got, "existing config.yaml must be reused unchanged")
|
||||
}
|
||||
|
||||
// TestIsManagedProxiedServerConfig covers the marker-detection helper in
|
||||
// isolation from any file I/O.
|
||||
func TestIsManagedProxiedServerConfig(t *testing.T) {
|
||||
managed, err := renderProxiedServerConfig(1234, true)
|
||||
managed, err := renderProxiedServerConfig(1234)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, isManagedProxiedServerConfig(managed))
|
||||
|
||||
|
|
@ -141,266 +97,75 @@ func TestIsManagedProxiedServerConfig(t *testing.T) {
|
|||
assert.False(t, isManagedProxiedServerConfig(nil))
|
||||
}
|
||||
|
||||
// TestReconcileManagedProxiedServerConfig_AddsKey covers the (a) direction
|
||||
// (gastownhall/beads#4986 round 2 major 2): a Beads-managed config that
|
||||
// predates archive_level support gains the key once the resolved dolt
|
||||
// supports it, with every other field (including port) preserved via the
|
||||
// servercfg struct round trip.
|
||||
func TestReconcileManagedProxiedServerConfig_AddsKey(t *testing.T) {
|
||||
before, err := renderProxiedServerConfig(45678, false)
|
||||
require.NoError(t, err)
|
||||
require.NotContains(t, string(before), "archive_level")
|
||||
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")
|
||||
stub := "#!/bin/sh\necho \"$@\" >> " + sentinel + "\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"))
|
||||
|
||||
after, changed, err := reconcileManagedProxiedServerConfig(before, true)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, changed)
|
||||
assert.True(t, isManagedProxiedServerConfig(after), "marker must survive reconcile")
|
||||
|
||||
cfg, err := servercfg.NewYamlConfig(after)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 45678, cfg.Port(), "port must be preserved")
|
||||
gc := cfg.AutoGCBehavior()
|
||||
require.NotNil(t, gc)
|
||||
assert.Equal(t, 0, gc.ArchiveLevel())
|
||||
assert.True(t, gc.Enable())
|
||||
}
|
||||
|
||||
// TestReconcileManagedProxiedServerConfig_StripsKey covers the (b)
|
||||
// direction: a Beads-managed config written with archive_level set loses
|
||||
// the key when the resolved dolt no longer supports it (e.g. a downgrade),
|
||||
// so a later yaml.UnmarshalStrict on that older dolt cannot hard-fail.
|
||||
func TestReconcileManagedProxiedServerConfig_StripsKey(t *testing.T) {
|
||||
before, err := renderProxiedServerConfig(45678, true)
|
||||
require.NoError(t, err)
|
||||
require.Contains(t, string(before), "archive_level")
|
||||
|
||||
after, changed, err := reconcileManagedProxiedServerConfig(before, false)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, changed)
|
||||
assert.NotContains(t, string(after), "archive_level")
|
||||
assert.NotContains(t, string(after), "auto_gc_behavior")
|
||||
|
||||
cfg, err := servercfg.NewYamlConfig(after)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 45678, cfg.Port(), "port must be preserved")
|
||||
assert.Nil(t, cfg.AutoGCBehavior())
|
||||
}
|
||||
|
||||
// TestReconcileManagedProxiedServerConfig_NoOpWhenAlreadyAligned asserts
|
||||
// reconcile reports changed=false (and ensureProxiedServerConfig therefore
|
||||
// skips the rewrite) when the config already matches the probe result —
|
||||
// the steady-state case on every ordinary bd invocation.
|
||||
func TestReconcileManagedProxiedServerConfig_NoOpWhenAlreadyAligned(t *testing.T) {
|
||||
t.Run("already has key, still supported", func(t *testing.T) {
|
||||
body, err := renderProxiedServerConfig(1, true)
|
||||
require.NoError(t, err)
|
||||
after, changed, err := reconcileManagedProxiedServerConfig(body, true)
|
||||
require.NoError(t, err)
|
||||
assert.False(t, changed)
|
||||
assert.Equal(t, body, after)
|
||||
})
|
||||
t.Run("already lacks key, still unsupported", func(t *testing.T) {
|
||||
body, err := renderProxiedServerConfig(1, false)
|
||||
require.NoError(t, err)
|
||||
after, changed, err := reconcileManagedProxiedServerConfig(body, false)
|
||||
require.NoError(t, err)
|
||||
assert.False(t, changed)
|
||||
assert.Equal(t, body, after)
|
||||
})
|
||||
}
|
||||
|
||||
// TestReconcileManagedProxiedServerConfig_PreservesSiblingFields covers the
|
||||
// round-3 verification finding (gastownhall/beads#4986): reconcile must
|
||||
// inspect and modify only ArchiveLevel_, not treat "the whole
|
||||
// auto_gc_behavior block is present" as equivalent to "archive_level is
|
||||
// set". A block carrying only a user-set "enable" (no archive_level) must
|
||||
// both gain archive_level when supported, and keep its "enable" setting
|
||||
// when the (now-absent-again) archive_level is later stripped on an
|
||||
// unsupported binary — the earlier (buggy) whole-block check would have
|
||||
// either left archive_level unset in the first case, or discarded "enable"
|
||||
// entirely in the second.
|
||||
func TestReconcileManagedProxiedServerConfig_PreservesSiblingFields(t *testing.T) {
|
||||
enable := true
|
||||
|
||||
t.Run("block with only enable gains archive_level; enable preserved", func(t *testing.T) {
|
||||
before := renderManagedConfigWithAutoGCBehavior(t, 45678, &servercfg.AutoGCBehaviorYAMLConfig{
|
||||
Enable_: &enable,
|
||||
})
|
||||
require.Contains(t, string(before), "enable: true")
|
||||
require.NotContains(t, string(before), "archive_level")
|
||||
|
||||
after, changed, err := reconcileManagedProxiedServerConfig(before, true)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, changed)
|
||||
|
||||
cfg, err := servercfg.NewYamlConfig(after)
|
||||
require.NoError(t, err)
|
||||
gc := cfg.AutoGCBehavior()
|
||||
require.NotNil(t, gc)
|
||||
assert.Equal(t, 0, gc.ArchiveLevel())
|
||||
assert.True(t, gc.Enable(), "enable must be preserved, not defaulted/lost")
|
||||
})
|
||||
|
||||
t.Run("block with only enable on unsupported binary is untouched", func(t *testing.T) {
|
||||
before := renderManagedConfigWithAutoGCBehavior(t, 45678, &servercfg.AutoGCBehaviorYAMLConfig{
|
||||
Enable_: &enable,
|
||||
})
|
||||
|
||||
after, changed, err := reconcileManagedProxiedServerConfig(before, false)
|
||||
require.NoError(t, err)
|
||||
assert.False(t, changed, "nothing to strip when archive_level was never set")
|
||||
assert.Equal(t, before, after)
|
||||
})
|
||||
|
||||
t.Run("block with both on unsupported binary keeps enable, loses archive_level", func(t *testing.T) {
|
||||
archiveLevel := 0
|
||||
before := renderManagedConfigWithAutoGCBehavior(t, 45678, &servercfg.AutoGCBehaviorYAMLConfig{
|
||||
Enable_: &enable,
|
||||
ArchiveLevel_: &archiveLevel,
|
||||
})
|
||||
require.Contains(t, string(before), "enable: true")
|
||||
require.Contains(t, string(before), "archive_level: 0")
|
||||
|
||||
after, changed, err := reconcileManagedProxiedServerConfig(before, false)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, changed)
|
||||
assert.NotContains(t, string(after), "archive_level")
|
||||
|
||||
cfg, err := servercfg.NewYamlConfig(after)
|
||||
require.NoError(t, err)
|
||||
gc := cfg.AutoGCBehavior()
|
||||
require.NotNil(t, gc, "block must survive since enable is still set")
|
||||
assert.True(t, gc.Enable(), "enable must be preserved when only archive_level is stripped")
|
||||
})
|
||||
}
|
||||
|
||||
// TestEnsureProxiedServerConfig_ManagedWithoutKeyGainsIt is the
|
||||
// ensureProxiedServerConfig-level counterpart to
|
||||
// TestReconcileManagedProxiedServerConfig_AddsKey: an upgraded install
|
||||
// whose config.yaml predates this fix gets archive_level added in place on
|
||||
// the very next command, without a manual regenerate.
|
||||
func TestEnsureProxiedServerConfig_ManagedWithoutKeyGainsIt(t *testing.T) {
|
||||
beadsDir := t.TempDir()
|
||||
root := filepath.Join(beadsDir, "dolt")
|
||||
require.NoError(t, os.MkdirAll(root, 0o755))
|
||||
cfgPath := filepath.Join(root, "config.yaml")
|
||||
|
||||
seed, err := renderProxiedServerConfig(45678, false)
|
||||
_, err := ensureProxiedServerConfig(beadsDir) // fresh bootstrap
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, os.WriteFile(cfgPath, seed, 0o600))
|
||||
|
||||
path, err := ensureProxiedServerConfig(beadsDir, true)
|
||||
_, err = ensureProxiedServerConfig(beadsDir) // steady state
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, cfgPath, path)
|
||||
|
||||
got, err := os.ReadFile(cfgPath)
|
||||
require.NoError(t, err)
|
||||
assert.Contains(t, string(got), "archive_level: 0")
|
||||
|
||||
cfg, err := servercfg.NewYamlConfig(got)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 45678, cfg.Port(), "port must survive the rewrite")
|
||||
if body, rerr := os.ReadFile(sentinel); rerr == nil {
|
||||
t.Fatalf("ensureProxiedServerConfig forked dolt: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
// TestEnsureProxiedServerConfig_ManagedWithKeyOnOldBinaryLosesIt is the
|
||||
// ensureProxiedServerConfig-level counterpart to
|
||||
// TestReconcileManagedProxiedServerConfig_StripsKey: a dolt downgrade after
|
||||
// a managed config already set archive_level must not hard-fail server
|
||||
// startup on the next command.
|
||||
func TestEnsureProxiedServerConfig_ManagedWithKeyOnOldBinaryLosesIt(t *testing.T) {
|
||||
beadsDir := t.TempDir()
|
||||
root := filepath.Join(beadsDir, "dolt")
|
||||
require.NoError(t, os.MkdirAll(root, 0o755))
|
||||
cfgPath := filepath.Join(root, "config.yaml")
|
||||
|
||||
seed, err := renderProxiedServerConfig(45678, true)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, os.WriteFile(cfgPath, seed, 0o600))
|
||||
|
||||
path, err := ensureProxiedServerConfig(beadsDir, false)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, cfgPath, path)
|
||||
|
||||
got, err := os.ReadFile(cfgPath)
|
||||
require.NoError(t, err)
|
||||
assert.NotContains(t, string(got), "archive_level")
|
||||
|
||||
cfg, err := servercfg.NewYamlConfig(got)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 45678, cfg.Port(), "port must survive the rewrite")
|
||||
}
|
||||
|
||||
// TestEnsureProxiedServerConfig_CustomConfigUntouchedWithWarning asserts a
|
||||
// genuinely user-owned config (env var / sidecar custom path) is NEVER
|
||||
// rewritten, and instead gets a one-line stderr warning when it lacks
|
||||
// archive_level on a capable binary.
|
||||
func TestEnsureProxiedServerConfig_CustomConfigUntouchedWithWarning(t *testing.T) {
|
||||
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")
|
||||
before, err := renderProxiedServerConfig(54321, false)
|
||||
require.NoError(t, err)
|
||||
// Strip the marker: this is meant to look like a genuinely hand-written
|
||||
// custom config, not one Beads happened to generate elsewhere.
|
||||
before = before[len(managedProxiedServerConfigMarker):]
|
||||
require.NoError(t, os.WriteFile(customPath, before, 0o600))
|
||||
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 got string
|
||||
var err error
|
||||
var path string
|
||||
got = captureStderr(t, func() {
|
||||
path, err = ensureProxiedServerConfig(bd, true)
|
||||
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, after, "custom config must never be rewritten")
|
||||
assert.Contains(t, got, "not Beads-managed")
|
||||
assert.Contains(t, got, customPath)
|
||||
assert.Equal(t, before, string(after), "custom config must never be rewritten")
|
||||
assert.Empty(t, got, "custom configs must not produce per-command warnings")
|
||||
}
|
||||
|
||||
// TestEnsureProxiedServerConfig_PreMarkerDefaultConfigUntouchedWithWarning
|
||||
// covers the other unmanaged case: a default-path config.yaml written by a
|
||||
// Beads version that predates the marker. Same contract as the custom-path
|
||||
// case — never rewritten, only warned about — since Beads cannot prove it
|
||||
// owns the file.
|
||||
func TestEnsureProxiedServerConfig_PreMarkerDefaultConfigUntouchedWithWarning(t *testing.T) {
|
||||
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")
|
||||
|
||||
seed, err := renderProxiedServerConfig(45678, false)
|
||||
require.NoError(t, err)
|
||||
seed = seed[len(managedProxiedServerConfigMarker):] // simulate a pre-marker file
|
||||
require.NoError(t, os.WriteFile(cfgPath, seed, 0o600))
|
||||
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, true)
|
||||
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, after, "pre-marker default config must never be rewritten")
|
||||
assert.Contains(t, got, "not Beads-managed")
|
||||
assert.Equal(t, seed, string(after), "pre-marker default config must never be rewritten")
|
||||
assert.Empty(t, got)
|
||||
}
|
||||
|
||||
// TestEnsureProxiedServerConfig_CustomConfigEnvVarInterpolation covers the
|
||||
// round-3 verification finding (gastownhall/beads#4986): a custom config
|
||||
// using Dolt's ${VAR}-style environment interpolation (see
|
||||
// servercfg/env_interpolate.go, applied by YamlConfigFromFile before
|
||||
// parsing) must be ACCEPTED, not rejected. An earlier version of this
|
||||
// function validated custom configs via servercfg.NewYamlConfig on raw
|
||||
// bytes, which skips interpolation entirely and would fail on a valid
|
||||
// ${VAR} placeholder with a YAML/type error before the server ever starts.
|
||||
func TestEnsureProxiedServerConfig_CustomConfigEnvVarInterpolation(t *testing.T) {
|
||||
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "")
|
||||
t.Setenv("BEADS_TEST_PROXIED_SERVER_PORT", "54321")
|
||||
|
|
@ -408,165 +173,65 @@ func TestEnsureProxiedServerConfig_CustomConfigEnvVarInterpolation(t *testing.T)
|
|||
|
||||
customDir := t.TempDir()
|
||||
customPath := filepath.Join(customDir, "my-server.yaml")
|
||||
// listener.port as a raw ${VAR} placeholder — only valid after
|
||||
// interpolation; NewYamlConfig on the raw bytes would fail to unmarshal
|
||||
// "${BEADS_TEST_PROXIED_SERVER_PORT}" into the int Port field.
|
||||
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, true)
|
||||
path, err := ensureProxiedServerConfig(bd)
|
||||
require.NoError(t, err, "a valid ${VAR} custom config must not be rejected pre-start")
|
||||
assert.Equal(t, customPath, path)
|
||||
|
||||
// The file on disk must still carry the literal, un-interpolated
|
||||
// placeholder — ensureProxiedServerConfig never rewrites custom configs.
|
||||
after, err := os.ReadFile(customPath)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tmpl, string(after))
|
||||
}
|
||||
|
||||
// TestEnsureProxiedServerConfig_ManagedFileWithPlaceholderDegradesGracefully
|
||||
// covers the round-4 verification finding (gastownhall/beads#4986): the
|
||||
// managedProxiedServerConfigMarker only proves Beads generated a file
|
||||
// ORIGINALLY, not that an operator hasn't hand-edited it since (e.g. to add
|
||||
// a ${VAR} placeholder). reconcileManagedProxiedServerConfig's raw-bytes
|
||||
// parse (deliberately non-interpolating — see its doc comment) then fails.
|
||||
//
|
||||
// Verified before this fix: that parse error propagated as a hard error
|
||||
// out of ensureProxiedServerConfig, which cmd/bd/uow_factory.go's
|
||||
// newManagedProxiedServerUOWProvider returns directly — aborting the
|
||||
// ENTIRE bd command before ever attempting to start the managed dolt
|
||||
// server, even though a real dolt process would happily interpolate and
|
||||
// start against the same file. This test asserts the fixed behavior:
|
||||
// degrade to the unmanaged (warn-only, never-rewrite) treatment instead.
|
||||
func TestEnsureProxiedServerConfig_ManagedFileWithPlaceholderDegradesGracefully(t *testing.T) {
|
||||
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")
|
||||
|
||||
// Marker present (looks Beads-managed) but hand-edited afterward with a
|
||||
// ${VAR} placeholder in a field NewYamlConfig cannot unmarshal without
|
||||
// interpolation (an int field fed a string).
|
||||
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, true)
|
||||
path, err = ensureProxiedServerConfig(beadsDir)
|
||||
})
|
||||
require.NoError(t, err, "a managed file that fails the raw parse must not fail the whole command")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, cfgPath, path)
|
||||
assert.Contains(t, got, "failed to parse", "must explain why it's degrading to unmanaged treatment")
|
||||
assert.Empty(t, got)
|
||||
|
||||
after, err := os.ReadFile(cfgPath)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, seed, string(after), "file must be left untouched when the managed parse fails")
|
||||
assert.Equal(t, seed, string(after), "a hand-edited managed file must be left untouched")
|
||||
}
|
||||
|
||||
// TestWarnUnmanagedProxiedServerConfig_EnableOnlyFieldLevel covers the
|
||||
// round-4 verification finding (gastownhall/beads#4986): the unmanaged
|
||||
// warning must inspect ArchiveLevel_ specifically, not "the whole
|
||||
// auto_gc_behavior block is present" — otherwise an enable-only custom
|
||||
// config both misses the zstd-risk warning on a capable dolt, and gets a
|
||||
// FALSE incompatibility warning on an incapable one (since the block is
|
||||
// present even though archive_level itself was never set).
|
||||
func TestWarnUnmanagedProxiedServerConfig_EnableOnlyFieldLevel(t *testing.T) {
|
||||
enable := true
|
||||
|
||||
newEnableOnlyCustomConfig := func(t *testing.T) string {
|
||||
t.Helper()
|
||||
t.Setenv("BEADS_PROXIED_SERVER_CONFIG", "")
|
||||
body := renderManagedConfigWithAutoGCBehavior(t, 54321, &servercfg.AutoGCBehaviorYAMLConfig{
|
||||
Enable_: &enable,
|
||||
})
|
||||
// Strip the marker: this must be evaluated as a genuinely
|
||||
// hand-written custom config, not a Beads-managed one.
|
||||
body = body[len(managedProxiedServerConfigMarker):]
|
||||
require.NotContains(t, string(body), "archive_level")
|
||||
|
||||
bd := t.TempDir()
|
||||
customPath := filepath.Join(t.TempDir(), "my-server.yaml")
|
||||
require.NoError(t, os.WriteFile(customPath, body, 0o600))
|
||||
writeProxiedClientInfo(t, bd, &configfile.ProxiedServerClientInfo{ConfigPath: customPath})
|
||||
return bd
|
||||
}
|
||||
|
||||
t.Run("capable dolt: gets the zstd-risk warning", func(t *testing.T) {
|
||||
bd := newEnableOnlyCustomConfig(t)
|
||||
var err error
|
||||
got := captureStderr(t, func() {
|
||||
_, err = ensureProxiedServerConfig(bd, true)
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.Contains(t, got, "zstd auto-GC may still be active",
|
||||
"an enable-only block must still be treated as archive_level-absent")
|
||||
})
|
||||
|
||||
t.Run("incapable dolt: no false incompatibility warning", func(t *testing.T) {
|
||||
bd := newEnableOnlyCustomConfig(t)
|
||||
var err error
|
||||
got := captureStderr(t, func() {
|
||||
_, err = ensureProxiedServerConfig(bd, false)
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, got,
|
||||
"an enable-only block (no archive_level) has nothing incompatible with an older dolt; "+
|
||||
"the old block-presence check would have warned here anyway")
|
||||
})
|
||||
}
|
||||
|
||||
// TestEnsureProxiedServerConfig_ManagedConfigBehindSymlinkPreservesSymlink
|
||||
// covers the round-4 verification finding (gastownhall/beads#4986):
|
||||
// os.Rename's destination argument does not follow a symlink — it
|
||||
// replaces whatever is directly AT that path. Before this fix, reconciling
|
||||
// a managed config.yaml that was itself a symlink (e.g. an operator
|
||||
// centralizing config outside the per-project .beads/dolt directory) would
|
||||
// silently replace the symlink with a regular file at the SAME location,
|
||||
// losing the indirection and leaving whatever the symlink pointed at
|
||||
// unmodified and now orphaned.
|
||||
func TestEnsureProxiedServerConfig_ManagedConfigBehindSymlinkPreservesSymlink(t *testing.T) {
|
||||
func TestEnsureProxiedServerConfig_DanglingSymlinkSelfHeals(t *testing.T) {
|
||||
beadsDir := t.TempDir()
|
||||
root := filepath.Join(beadsDir, "dolt")
|
||||
require.NoError(t, os.MkdirAll(root, 0o755))
|
||||
|
||||
// The physical file lives elsewhere; config.yaml in the managed
|
||||
// location is a symlink to it.
|
||||
physicalPath := filepath.Join(t.TempDir(), "real-config.yaml")
|
||||
seed, err := renderProxiedServerConfig(45678, false) // lacks archive_level: 0
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, os.WriteFile(physicalPath, seed, 0o600))
|
||||
|
||||
cfgPath := filepath.Join(root, "config.yaml")
|
||||
if err := os.Symlink(physicalPath, cfgPath); err != nil {
|
||||
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, true) // now supported -> should reconcile
|
||||
path, err := ensureProxiedServerConfig(beadsDir)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, cfgPath, path)
|
||||
|
||||
// The symlink itself must survive, still pointing at the same physical file.
|
||||
linkInfo, err := os.Lstat(cfgPath)
|
||||
info, err := os.Lstat(cfgPath)
|
||||
require.NoError(t, err)
|
||||
require.True(t, linkInfo.Mode()&os.ModeSymlink != 0, "config.yaml must still be a symlink")
|
||||
target, err := os.Readlink(cfgPath)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, physicalPath, target)
|
||||
assert.True(t, info.Mode().IsRegular(), "dangling symlink must be replaced by the generated config")
|
||||
|
||||
// The PHYSICAL file must carry the reconciled content.
|
||||
physicalBody, err := os.ReadFile(physicalPath)
|
||||
body, err := os.ReadFile(cfgPath)
|
||||
require.NoError(t, err)
|
||||
assert.Contains(t, string(physicalBody), "archive_level: 0")
|
||||
|
||||
// Reading through the symlink must observe the same reconciled content.
|
||||
throughLink, err := os.ReadFile(cfgPath)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, physicalBody, throughLink)
|
||||
assert.True(t, isManagedProxiedServerConfig(body))
|
||||
}
|
||||
|
||||
func TestProxiedServerPathHelpers(t *testing.T) {
|
||||
|
|
@ -661,7 +326,7 @@ func TestResolveProxiedServerConfigPath(t *testing.T) {
|
|||
// 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, true)
|
||||
body, err := renderProxiedServerConfig(54321)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, os.WriteFile(path, body, 0o600))
|
||||
return path
|
||||
|
|
@ -678,7 +343,7 @@ func TestEnsureProxiedServerConfig_CustomPathExists(t *testing.T) {
|
|||
customPath := writeValidServerYAML(t, filepath.Join(customDir, "my-server.yaml"))
|
||||
|
||||
writeProxiedClientInfo(t, bd, &configfile.ProxiedServerClientInfo{ConfigPath: customPath})
|
||||
got, err := ensureProxiedServerConfig(bd, true)
|
||||
got, err := ensureProxiedServerConfig(bd)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, customPath, got)
|
||||
|
||||
|
|
@ -695,7 +360,7 @@ func TestEnsureProxiedServerConfig_CustomPathMissing(t *testing.T) {
|
|||
missing := filepath.Join(t.TempDir(), "does-not-exist.yaml")
|
||||
|
||||
writeProxiedClientInfo(t, bd, &configfile.ProxiedServerClientInfo{ConfigPath: missing})
|
||||
_, err := ensureProxiedServerConfig(bd, true)
|
||||
_, err := ensureProxiedServerConfig(bd)
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), missing)
|
||||
}
|
||||
|
|
@ -711,7 +376,7 @@ func TestEnsureProxiedServerConfig_CustomPathInvalidYAML(t *testing.T) {
|
|||
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, true)
|
||||
_, err := ensureProxiedServerConfig(bd)
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), bad)
|
||||
assert.Contains(t, strings.ToLower(err.Error()), "parse")
|
||||
|
|
@ -725,7 +390,7 @@ func TestEnsureProxiedServerConfig_CustomPathIsDirectory(t *testing.T) {
|
|||
dir := t.TempDir()
|
||||
|
||||
writeProxiedClientInfo(t, bd, &configfile.ProxiedServerClientInfo{ConfigPath: dir})
|
||||
_, err := ensureProxiedServerConfig(bd, true)
|
||||
_, err := ensureProxiedServerConfig(bd)
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), dir)
|
||||
assert.Contains(t, err.Error(), "not a regular file")
|
||||
|
|
@ -1065,7 +730,7 @@ func TestManagedListenerPolicyEveryConfigPathAtInitAndRuntime(t *testing.T) {
|
|||
initErr := validateManagedProxiedServerConfigAtInit(beadsDir, explicitPath, "")
|
||||
assertManagedListenerPolicyError(t, initErr)
|
||||
|
||||
_, runtimeErr := ensureProxiedServerConfig(beadsDir, true)
|
||||
_, runtimeErr := ensureProxiedServerConfig(beadsDir)
|
||||
assertManagedListenerPolicyError(t, runtimeErr)
|
||||
})
|
||||
}
|
||||
|
|
@ -1078,7 +743,7 @@ func TestManagedListenerPolicyLoopbackCustomConfigPassesInitAndRuntime(t *testin
|
|||
writeProxiedClientInfo(t, beadsDir, &configfile.ProxiedServerClientInfo{ConfigPath: path})
|
||||
|
||||
require.NoError(t, validateManagedProxiedServerConfigAtInit(beadsDir, "", ""))
|
||||
got, err := ensureProxiedServerConfig(beadsDir, true)
|
||||
got, err := ensureProxiedServerConfig(beadsDir)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, path, got)
|
||||
}
|
||||
|
|
@ -1098,7 +763,7 @@ func TestManagedListenerPolicyOmittedHostRejected(t *testing.T) {
|
|||
require.Error(t, initErr)
|
||||
assert.Contains(t, initErr.Error(), "listener.host is omitted")
|
||||
|
||||
_, runtimeErr := ensureProxiedServerConfig(beadsDir, true)
|
||||
_, runtimeErr := ensureProxiedServerConfig(beadsDir)
|
||||
require.Error(t, runtimeErr)
|
||||
assert.Contains(t, runtimeErr.Error(), "listener.host is omitted")
|
||||
}
|
||||
|
|
@ -1123,7 +788,7 @@ func TestManagedListenerPolicyMarkerInterpolatedHost(t *testing.T) {
|
|||
beadsDir := t.TempDir()
|
||||
want := writeInterpolatedHostConfig(t, beadsDir)
|
||||
|
||||
got, err := ensureProxiedServerConfig(beadsDir, true)
|
||||
got, err := ensureProxiedServerConfig(beadsDir)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, want, got)
|
||||
})
|
||||
|
|
@ -1134,7 +799,7 @@ func TestManagedListenerPolicyMarkerInterpolatedHost(t *testing.T) {
|
|||
beadsDir := t.TempDir()
|
||||
writeInterpolatedHostConfig(t, beadsDir)
|
||||
|
||||
_, err := ensureProxiedServerConfig(beadsDir, true)
|
||||
_, err := ensureProxiedServerConfig(beadsDir)
|
||||
assertManagedListenerPolicyError(t, err)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -376,13 +376,7 @@ func newManagedProxiedServerUOWProvider(ctx context.Context, beadsDir string, to
|
|||
return nil, fmt.Errorf("newProxiedServerUOWProvider: proxied server root (from env or %s): %w", configfile.ProxiedServerClientInfoFileName, err)
|
||||
}
|
||||
|
||||
// Gate auto_gc_behavior.archive_level: 0 on the resolved external dolt's
|
||||
// version — Dolt's YAML config loader uses yaml.UnmarshalStrict, so an
|
||||
// older dolt whose own YAMLConfig struct lacks this field would refuse
|
||||
// to start rather than ignore the unknown key (gastownhall/beads#4986).
|
||||
archiveLevelSupported := doltserver.SupportsArchiveLevelConfig(doltBin)
|
||||
|
||||
configPath, err := ensureProxiedServerConfig(beadsDir, archiveLevelSupported)
|
||||
configPath, err := ensureProxiedServerConfig(beadsDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue