-
Notifications
You must be signed in to change notification settings - Fork 885
feat(configmanager): SeiConfigManager v2 body — validate-passthrough (PLT-775 PR2) #3678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bdchatham
wants to merge
5
commits into
main
Choose a base branch
from
feat/configmanager-v2-body-plt775
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+268
−34
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b8e9513
feat(configmanager): SeiConfigManager v2 body — validate-passthrough …
bdchatham b5a71b7
review(configmanager): xreview fixes (doc-drift, louder error advisor…
bdchatham 13b3a44
fix(configmanager): lint errcheck + surface validation warnings (agen…
bdchatham 9756ba4
test(configmanager): cover env-precedence home resolution (DWC-1)
bdchatham 393578d
docs(configmanager): fix stale env-case comment on TestResolveHomeDir…
bdchatham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "os" | ||
| "path" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/stretchr/testify/require" | ||
| "go.opentelemetry.io/otel/sdk/trace" | ||
|
|
||
| "github.com/sei-protocol/sei-chain/cmd/seid/cmd/configmanager" | ||
| "github.com/sei-protocol/sei-chain/sei-cosmos/client/flags" | ||
| "github.com/sei-protocol/sei-chain/sei-cosmos/server" | ||
| ) | ||
|
|
||
| // errStopPreRun aborts the command after the config-resolution PreRunE, before | ||
| // StartCmd's RunE tries to boot a node. | ||
| var errStopPreRun = errors.New("stop after prerun") | ||
|
|
||
| // runConfigManager runs mgr.Apply inside a StartCmd's PreRunE against homeDir | ||
| // supplied via the --home flag, using seid's real app-config template, and | ||
| // returns the populated server context (the two channels start.go/app.New | ||
| // consume). It mirrors the harness in sei-cosmos/server/util_test.go. | ||
| func runConfigManager(t *testing.T, mgr configmanager.ConfigManager, homeDir string) *server.Context { | ||
| t.Helper() | ||
| cmd := server.StartCmd(nil, "/foobar", []trace.TracerProviderOption{}) | ||
| require.NoError(t, cmd.Flags().Set(flags.FlagHome, homeDir)) | ||
| return execConfigManager(t, mgr, cmd) | ||
| } | ||
|
|
||
| // runConfigManagerEnvHome is runConfigManager's twin that supplies homeDir | ||
| // through the environment instead of --home, exercising the | ||
| // SetEnvPrefix/AutomaticEnv/replacer machinery that resolveHomeDir mirrors from | ||
| // the legacy handler (the flag-driven path never touches it). The env prefix is | ||
| // path.Base(os.Executable()) — the test binary — derived identically by BOTH | ||
| // the legacy handler (sei-cosmos/server/util.go:152,162-164) and v2's | ||
| // resolveHomeDir, so both resolve the same home. viper's lookup key for "home" | ||
| // is ToUpper(prefix + "_" + "home") with the ".","-" -> "_" replacer applied. | ||
| func runConfigManagerEnvHome(t *testing.T, mgr configmanager.ConfigManager, homeDir string) *server.Context { | ||
| t.Helper() | ||
| exe, err := os.Executable() | ||
| require.NoError(t, err) | ||
| envKey := strings.NewReplacer(".", "_", "-", "_").Replace( | ||
| strings.ToUpper(path.Base(exe) + "_" + flags.FlagHome)) | ||
| t.Setenv(envKey, homeDir) | ||
|
|
||
| // Leave --home unset: an unchanged flag default ranks below AutomaticEnv in | ||
| // viper's precedence, so the env value is what resolves. | ||
| cmd := server.StartCmd(nil, "/foobar", []trace.TracerProviderOption{}) | ||
| return execConfigManager(t, mgr, cmd) | ||
| } | ||
|
|
||
| // execConfigManager runs mgr.Apply inside cmd's PreRunE (aborting before the | ||
| // node boots) and returns the populated server context. The caller configures | ||
| // how home is supplied (flag vs env) on cmd beforehand. | ||
| func execConfigManager(t *testing.T, mgr configmanager.ConfigManager, cmd *cobra.Command) *server.Context { | ||
| t.Helper() | ||
| template, appCfg := initAppConfig() | ||
| cmd.PreRunE = func(c *cobra.Command, _ []string) error { | ||
| if err := mgr.Apply(c, template, appCfg); err != nil { | ||
| return err | ||
| } | ||
| return errStopPreRun | ||
| } | ||
|
|
||
| serverCtx := &server.Context{} | ||
| ctx := context.WithValue(context.Background(), server.ServerContextKey, serverCtx) | ||
| require.ErrorIs(t, cmd.ExecuteContext(ctx), errStopPreRun) | ||
| return serverCtx | ||
| } | ||
|
|
||
| // TestConfigManagerLegacyVsV2Differential is the core safety property: the v2 | ||
| // manager must produce the SAME consumed config as the legacy path. v2 reads | ||
| // the config (to validate it) and then re-enters the legacy reader on the | ||
| // operator's ORIGINAL files — it does not rewrite — so the two paths read the | ||
| // SAME home and any difference is a real divergence, not a path artifact. | ||
| // | ||
| // It compares parsed semantics: | ||
| // - serverCtx.Config (the *tmcfg.Config the node runs on), and | ||
| // - serverCtx.Viper.AllSettings() (the AppOptions every Sei section reads via | ||
| // appOpts.Get), both at end-of-PersistentPreRunE and after the start.go | ||
| // chain-id mutation. | ||
| // | ||
| // A realistic fixture (all 11 Sei sections) is generated by letting the legacy | ||
| // handler create the full default config once; both managers then read it. | ||
| func TestConfigManagerLegacyVsV2Differential(t *testing.T) { | ||
| home := t.TempDir() | ||
|
|
||
| // Generate a complete, realistic config via the legacy creator (fresh home). | ||
| _ = runConfigManager(t, configmanager.LegacyConfigManager{}, home) | ||
|
|
||
| // Both managers read the same populated home. v2 is passthrough (no rewrite), | ||
| // so RootDir and every other path-derived field match by construction. | ||
| legacyCtx := runConfigManager(t, configmanager.LegacyConfigManager{}, home) | ||
| v2Ctx := runConfigManager(t, configmanager.SeiConfigManager{}, home) | ||
|
|
||
| require.Equal(t, legacyCtx.Config, v2Ctx.Config, | ||
| "serverCtx.Config differs between legacy and v2") | ||
| require.Equal(t, legacyCtx.Viper.AllSettings(), v2Ctx.Viper.AllSettings(), | ||
| "serverCtx.Viper settings differ between legacy and v2") | ||
|
|
||
| // The start.go chain-id mutation is identical on both vipers; assert parity | ||
| // holds after it too (covers the post-mutation snapshot). | ||
| const chainID = "differential-test-1" | ||
| legacyCtx.Viper.Set(flags.FlagChainID, chainID) | ||
| v2Ctx.Viper.Set(flags.FlagChainID, chainID) | ||
| require.Equal(t, legacyCtx.Viper.AllSettings(), v2Ctx.Viper.AllSettings(), | ||
| "settings diverge after the start.go chain-id mutation") | ||
| } | ||
|
|
||
| // TestConfigManagerLegacyVsV2Differential_EnvHome exercises the env-precedence | ||
| // half of resolveHomeDir's mirror of the legacy handler — the flag-driven | ||
| // differential above never touches SetEnvPrefix/AutomaticEnv. When home is | ||
| // supplied via the environment (not --home), v2 must resolve the SAME home the | ||
| // legacy handler does; otherwise v2 would advisorily validate one dir while the | ||
| // re-entered legacy reader boots on another — a silent drift the advisory | ||
| // design cannot surface (no error, no diagnostic). This pins the seam so a | ||
| // future change to the legacy env-resolution can't diverge undetected. | ||
| func TestConfigManagerLegacyVsV2Differential_EnvHome(t *testing.T) { | ||
| home := t.TempDir() | ||
|
|
||
| // Populate a complete realistic config in `home` via the fresh-home legacy | ||
| // creator, driven entirely through the env var (no --home). | ||
| _ = runConfigManagerEnvHome(t, configmanager.LegacyConfigManager{}, home) | ||
|
|
||
| legacyCtx := runConfigManagerEnvHome(t, configmanager.LegacyConfigManager{}, home) | ||
| v2Ctx := runConfigManagerEnvHome(t, configmanager.SeiConfigManager{}, home) | ||
|
|
||
| // Non-vacuous guard: the env var actually drove resolution. If the key were | ||
| // wrong, both would fall back to StartCmd's "/foobar" default (and the | ||
| // legacy creator would fail writing under it) — this asserts the env path | ||
| // resolved to the temp home, for both managers. | ||
| require.Equal(t, home, v2Ctx.Viper.GetString(flags.FlagHome), | ||
| "env-provided home did not drive v2 resolution") | ||
| require.Equal(t, home, legacyCtx.Viper.GetString(flags.FlagHome), | ||
| "env-provided home did not drive legacy resolution") | ||
|
|
||
| require.Equal(t, legacyCtx.Config, v2Ctx.Config, | ||
| "serverCtx.Config differs between legacy and v2 on the env-home path") | ||
| require.Equal(t, legacyCtx.Viper.AllSettings(), v2Ctx.Viper.AllSettings(), | ||
| "serverCtx.Viper settings differ between legacy and v2 on the env-home path") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.