-
Notifications
You must be signed in to change notification settings - Fork 19
[test] Add tests for config.LoadFromFile and related helpers #2381
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
Merged
lpcox
merged 1 commit into
main
from
test-coverage/config-loadfromfile-6c3bb4bc1b671cce
Mar 24, 2026
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,338 @@ | ||||||||||||||
| package config | ||||||||||||||
|
|
||||||||||||||
| import ( | ||||||||||||||
| "os" | ||||||||||||||
| "path/filepath" | ||||||||||||||
| "testing" | ||||||||||||||
|
|
||||||||||||||
| "github.com/stretchr/testify/assert" | ||||||||||||||
| "github.com/stretchr/testify/require" | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| // writeTempTOML writes content to a temp file and returns its path. | ||||||||||||||
| func writeTempTOML(t *testing.T, content string) string { | ||||||||||||||
| t.Helper() | ||||||||||||||
| dir := t.TempDir() | ||||||||||||||
| path := filepath.Join(dir, "config.toml") | ||||||||||||||
| require.NoError(t, os.WriteFile(path, []byte(content), 0600)) | ||||||||||||||
| return path | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // validDockerServerTOML is a minimal valid TOML config with a single stdio (docker) server. | ||||||||||||||
| const validDockerServerTOML = ` | ||||||||||||||
| [servers.github] | ||||||||||||||
| command = "docker" | ||||||||||||||
| args = ["run", "--rm", "-i", "ghcr.io/github/github-mcp-server:latest"] | ||||||||||||||
| ` | ||||||||||||||
|
|
||||||||||||||
| // validHTTPServerTOML is a minimal valid TOML config with a single HTTP server. | ||||||||||||||
| const validHTTPServerTOML = ` | ||||||||||||||
| [servers.myservice] | ||||||||||||||
| type = "http" | ||||||||||||||
| url = "http://localhost:9090/mcp" | ||||||||||||||
| ` | ||||||||||||||
|
|
||||||||||||||
| // TestLoadFromFile_FileNotFound verifies that LoadFromFile returns an error | ||||||||||||||
| // when the specified file path does not exist. | ||||||||||||||
| func TestLoadFromFile_FileNotFound(t *testing.T) { | ||||||||||||||
| cfg, err := LoadFromFile("/nonexistent/path/to/config.toml") | ||||||||||||||
| require.Error(t, err) | ||||||||||||||
| assert.Nil(t, cfg) | ||||||||||||||
| assert.Contains(t, err.Error(), "failed to open config file") | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // TestLoadFromFile_InvalidTOML verifies that LoadFromFile returns an error | ||||||||||||||
| // when the TOML file contains a syntax error. | ||||||||||||||
| func TestLoadFromFile_InvalidTOML(t *testing.T) { | ||||||||||||||
| path := writeTempTOML(t, ` | ||||||||||||||
| [servers.github | ||||||||||||||
| command = "docker" | ||||||||||||||
| `) | ||||||||||||||
| cfg, err := LoadFromFile(path) | ||||||||||||||
| require.Error(t, err) | ||||||||||||||
| assert.Nil(t, cfg) | ||||||||||||||
| assert.Contains(t, err.Error(), "failed to parse TOML") | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // TestLoadFromFile_EmptyServers verifies that LoadFromFile returns an error | ||||||||||||||
| // when the TOML file defines no servers. | ||||||||||||||
| func TestLoadFromFile_EmptyServers(t *testing.T) { | ||||||||||||||
| path := writeTempTOML(t, ` | ||||||||||||||
| [gateway] | ||||||||||||||
| port = 3000 | ||||||||||||||
| api_key = "test-key" | ||||||||||||||
| `) | ||||||||||||||
| cfg, err := LoadFromFile(path) | ||||||||||||||
| require.Error(t, err) | ||||||||||||||
| assert.Nil(t, cfg) | ||||||||||||||
| assert.Contains(t, err.Error(), "no servers defined") | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // TestLoadFromFile_StdioNonDockerCommand verifies that LoadFromFile returns an error | ||||||||||||||
| // when a stdio server uses a command other than "docker" (Spec §3.2.1). | ||||||||||||||
| func TestLoadFromFile_StdioNonDockerCommand(t *testing.T) { | ||||||||||||||
| path := writeTempTOML(t, ` | ||||||||||||||
| [servers.badserver] | ||||||||||||||
| command = "python" | ||||||||||||||
| args = ["server.py"] | ||||||||||||||
| `) | ||||||||||||||
| cfg, err := LoadFromFile(path) | ||||||||||||||
| require.Error(t, err) | ||||||||||||||
| assert.Nil(t, cfg) | ||||||||||||||
| assert.Contains(t, err.Error(), "badserver") | ||||||||||||||
| assert.Contains(t, err.Error(), "docker") | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // TestLoadFromFile_StdioLocalTypeNonDockerCommand verifies that stdio servers | ||||||||||||||
| // declared with type = "local" are also required to use docker. | ||||||||||||||
| func TestLoadFromFile_StdioLocalTypeNonDockerCommand(t *testing.T) { | ||||||||||||||
| path := writeTempTOML(t, ` | ||||||||||||||
| [servers.localserver] | ||||||||||||||
| type = "local" | ||||||||||||||
| command = "node" | ||||||||||||||
| args = ["server.js"] | ||||||||||||||
| `) | ||||||||||||||
| cfg, err := LoadFromFile(path) | ||||||||||||||
| require.Error(t, err) | ||||||||||||||
| assert.Nil(t, cfg) | ||||||||||||||
| assert.Contains(t, err.Error(), "localserver") | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // TestLoadFromFile_HTTPServerValid verifies that an HTTP server does not require | ||||||||||||||
| // the docker command (only stdio servers need containerization). | ||||||||||||||
| func TestLoadFromFile_HTTPServerValid(t *testing.T) { | ||||||||||||||
| path := writeTempTOML(t, validHTTPServerTOML) | ||||||||||||||
| cfg, err := LoadFromFile(path) | ||||||||||||||
| require.NoError(t, err) | ||||||||||||||
| require.NotNil(t, cfg) | ||||||||||||||
| assert.Len(t, cfg.Servers, 1) | ||||||||||||||
| server, ok := cfg.Servers["myservice"] | ||||||||||||||
| require.True(t, ok) | ||||||||||||||
| assert.Equal(t, "http", server.Type) | ||||||||||||||
| assert.Equal(t, "http://localhost:9090/mcp", server.URL) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // TestLoadFromFile_AppliesGatewayDefaults verifies that when no [gateway] section | ||||||||||||||
| // is present, default values are applied for port, startup timeout, and tool timeout. | ||||||||||||||
| func TestLoadFromFile_AppliesGatewayDefaults(t *testing.T) { | ||||||||||||||
| path := writeTempTOML(t, validDockerServerTOML) | ||||||||||||||
| cfg, err := LoadFromFile(path) | ||||||||||||||
| require.NoError(t, err) | ||||||||||||||
| require.NotNil(t, cfg) | ||||||||||||||
| require.NotNil(t, cfg.Gateway) | ||||||||||||||
| assert.Equal(t, DefaultPort, cfg.Gateway.Port) | ||||||||||||||
| assert.Equal(t, DefaultStartupTimeout, cfg.Gateway.StartupTimeout) | ||||||||||||||
| assert.Equal(t, DefaultToolTimeout, cfg.Gateway.ToolTimeout) | ||||||||||||||
| // Payload defaults from config_payload.go init() | ||||||||||||||
| assert.Equal(t, DefaultPayloadDir, cfg.Gateway.PayloadDir) | ||||||||||||||
| assert.Equal(t, DefaultPayloadSizeThreshold, cfg.Gateway.PayloadSizeThreshold) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // TestLoadFromFile_PreservesExplicitGatewayValues verifies that when the [gateway] | ||||||||||||||
| // section defines values, those values are preserved and not overwritten by defaults. | ||||||||||||||
| func TestLoadFromFile_PreservesExplicitGatewayValues(t *testing.T) { | ||||||||||||||
| path := writeTempTOML(t, ` | ||||||||||||||
| [gateway] | ||||||||||||||
| port = 8888 | ||||||||||||||
| api_key = "my-secret" | ||||||||||||||
| startup_timeout = 30 | ||||||||||||||
| tool_timeout = 60 | ||||||||||||||
|
|
||||||||||||||
| [servers.github] | ||||||||||||||
| command = "docker" | ||||||||||||||
| args = ["run", "--rm", "-i", "ghcr.io/github/github-mcp-server:latest"] | ||||||||||||||
| `) | ||||||||||||||
| cfg, err := LoadFromFile(path) | ||||||||||||||
| require.NoError(t, err) | ||||||||||||||
| require.NotNil(t, cfg) | ||||||||||||||
| assert.Equal(t, 8888, cfg.Gateway.Port) | ||||||||||||||
| assert.Equal(t, "my-secret", cfg.Gateway.APIKey) | ||||||||||||||
| assert.Equal(t, 30, cfg.Gateway.StartupTimeout) | ||||||||||||||
| assert.Equal(t, 60, cfg.Gateway.ToolTimeout) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // TestLoadFromFile_ServerFields verifies that all ServerConfig fields are parsed correctly. | ||||||||||||||
| func TestLoadFromFile_ServerFields(t *testing.T) { | ||||||||||||||
| path := writeTempTOML(t, ` | ||||||||||||||
| [servers.github] | ||||||||||||||
| command = "docker" | ||||||||||||||
| args = ["run", "--rm", "-i", "-e", "GITHUB_TOKEN", "ghcr.io/github/github-mcp-server:latest"] | ||||||||||||||
|
|
||||||||||||||
| [servers.github.env] | ||||||||||||||
| GITHUB_TOKEN = "mytoken" | ||||||||||||||
| `) | ||||||||||||||
| cfg, err := LoadFromFile(path) | ||||||||||||||
| require.NoError(t, err) | ||||||||||||||
| require.NotNil(t, cfg) | ||||||||||||||
| server := cfg.Servers["github"] | ||||||||||||||
| require.NotNil(t, server) | ||||||||||||||
| assert.Equal(t, "docker", server.Command) | ||||||||||||||
| assert.Contains(t, server.Args, "run") | ||||||||||||||
| assert.Equal(t, "mytoken", server.Env["GITHUB_TOKEN"]) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // TestLoadFromFile_UnknownKeysDoNotCauseError verifies that unknown configuration | ||||||||||||||
| // keys produce a warning log but do not prevent the config from loading. | ||||||||||||||
| func TestLoadFromFile_UnknownKeysDoNotCauseError(t *testing.T) { | ||||||||||||||
| path := writeTempTOML(t, ` | ||||||||||||||
| [gateway] | ||||||||||||||
| prot = 3000 | ||||||||||||||
|
|
||||||||||||||
| [servers.github] | ||||||||||||||
| command = "docker" | ||||||||||||||
| args = ["run", "--rm", "-i", "ghcr.io/github/github-mcp-server:latest"] | ||||||||||||||
| `) | ||||||||||||||
| // Unknown key "prot" (typo for "port") should warn but not error | ||||||||||||||
| cfg, err := LoadFromFile(path) | ||||||||||||||
| require.NoError(t, err) | ||||||||||||||
| require.NotNil(t, cfg) | ||||||||||||||
| // Port should use default since "prot" was not recognized | ||||||||||||||
| assert.Equal(t, DefaultPort, cfg.Gateway.Port) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // TestLoadFromFile_TrustedBotsEmptyArray verifies that an explicitly set but | ||||||||||||||
| // empty trusted_bots array is rejected (spec §4.1.3.4). | ||||||||||||||
| func TestLoadFromFile_TrustedBotsEmptyArray(t *testing.T) { | ||||||||||||||
| path := writeTempTOML(t, ` | ||||||||||||||
| [gateway] | ||||||||||||||
| trusted_bots = [] | ||||||||||||||
|
|
||||||||||||||
| [servers.github] | ||||||||||||||
| command = "docker" | ||||||||||||||
| args = ["run", "--rm", "-i", "ghcr.io/github/github-mcp-server:latest"] | ||||||||||||||
| `) | ||||||||||||||
| cfg, err := LoadFromFile(path) | ||||||||||||||
| require.Error(t, err) | ||||||||||||||
| assert.Nil(t, cfg) | ||||||||||||||
| assert.Contains(t, err.Error(), "trusted_bots") | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // TestLoadFromFile_TrustedBotsEmptyString verifies that a trusted_bots entry | ||||||||||||||
| // that is an empty or whitespace-only string is rejected. | ||||||||||||||
| func TestLoadFromFile_TrustedBotsEmptyString(t *testing.T) { | ||||||||||||||
| path := writeTempTOML(t, ` | ||||||||||||||
| [gateway] | ||||||||||||||
| trusted_bots = [" "] | ||||||||||||||
|
|
||||||||||||||
| [servers.github] | ||||||||||||||
| command = "docker" | ||||||||||||||
| args = ["run", "--rm", "-i", "ghcr.io/github/github-mcp-server:latest"] | ||||||||||||||
| `) | ||||||||||||||
| cfg, err := LoadFromFile(path) | ||||||||||||||
| require.Error(t, err) | ||||||||||||||
| assert.Nil(t, cfg) | ||||||||||||||
| assert.Contains(t, err.Error(), "trusted_bots") | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // TestLoadFromFile_TrustedBotsValid verifies that a non-empty trusted_bots list | ||||||||||||||
| // with valid entries loads successfully. | ||||||||||||||
| func TestLoadFromFile_TrustedBotsValid(t *testing.T) { | ||||||||||||||
| path := writeTempTOML(t, ` | ||||||||||||||
| [gateway] | ||||||||||||||
| trusted_bots = ["my-bot[bot]", "another-bot[bot]"] | ||||||||||||||
|
|
||||||||||||||
| [servers.github] | ||||||||||||||
| command = "docker" | ||||||||||||||
| args = ["run", "--rm", "-i", "ghcr.io/github/github-mcp-server:latest"] | ||||||||||||||
| `) | ||||||||||||||
| cfg, err := LoadFromFile(path) | ||||||||||||||
| require.NoError(t, err) | ||||||||||||||
| require.NotNil(t, cfg) | ||||||||||||||
| assert.Equal(t, []string{"my-bot[bot]", "another-bot[bot]"}, cfg.Gateway.TrustedBots) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // TestLoadFromFile_MultipleServers verifies that multiple servers of different types | ||||||||||||||
| // are parsed correctly. | ||||||||||||||
| func TestLoadFromFile_MultipleServers(t *testing.T) { | ||||||||||||||
|
Comment on lines
+244
to
+246
|
||||||||||||||
| // TestLoadFromFile_MultipleServers verifies that multiple servers of different types | |
| // are parsed correctly. | |
| func TestLoadFromFile_MultipleServers(t *testing.T) { | |
| // TestLoadFromFile_MultipleServersCore verifies that multiple servers of different types | |
| // are parsed correctly. | |
| func TestLoadFromFile_MultipleServersCore(t *testing.T) { |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR description states these files/functions previously had 0% coverage and no
*_test.gofiles existed, but the repo already contains substantial tests forLoadFromFile,applyGatewayDefaults,GetAPIKey,validateContainerID, andGetGatewayPortFromEnv(e.g.,internal/config/config_test.go,internal/config/validation_env_test.go). Please update the PR description or clarify what coverage gap these new tests address to avoid confusion.