Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ const (
DefaultMaxArtifactSize = 20
)

// Configuration value constants.
const (
TransportHTTP = "http"
TransportStdio = "stdio"
BackendDocker = "docker"
BackendLocal = "local"
LogModeProduction = "production"
LogModeDevelopment = "development"
LogLevelInfo = "info"
)

// Config represents the application configuration.
type Config struct {
Server ServerConfig `mapstructure:"server"`
Expand Down Expand Up @@ -104,20 +115,20 @@ func New() (*Config, error) {
// setDefaults sets the default configuration values.
func setDefaults(v *viper.Viper) {
// Server defaults
v.SetDefault("server.transport", "stdio")
v.SetDefault("server.transport", TransportStdio)
v.SetDefault("server.http_port", DefaultHTTPPort)

// Sandbox defaults
v.SetDefault("sandbox.backend", "docker")
v.SetDefault("sandbox.backend", BackendDocker)
v.SetDefault("sandbox.timeout_sec", DefaultTimeoutSec)
v.SetDefault("sandbox.memory_mb", DefaultMemoryMB)
v.SetDefault("sandbox.max_artifact_size_mb", DefaultMaxArtifactSize)
v.SetDefault("sandbox.network_enabled", false)
v.SetDefault("sandbox.enable_local_backend", false)

// Logging defaults
v.SetDefault("logging.mode", "production")
v.SetDefault("logging.level", "info")
v.SetDefault("logging.mode", LogModeProduction)
v.SetDefault("logging.level", LogLevelInfo)
}

// loadEnvironmentVariables loads environment variables from the config file preserving case
Expand Down Expand Up @@ -168,7 +179,7 @@ func loadLanguageEnvironments(rawConfig map[string]any, config *Config) error {

// validate ensures the configuration is valid.
func (c *Config) validate() error {
if t := c.Server.Transport; t != "stdio" && t != "http" {
if t := c.Server.Transport; t != TransportStdio && t != TransportHTTP {
return fmt.Errorf("invalid server.transport: %s, must be 'stdio' or 'http'", t)
}

Expand All @@ -185,19 +196,19 @@ func (c *Config) validate() error {
}

supportedBackends := map[string]bool{
"docker": true,
"podman": true,
"local": c.Sandbox.EnableLocalBackend,
BackendDocker: true,
"podman": true,
BackendLocal: c.Sandbox.EnableLocalBackend,
}
if !supportedBackends[c.Sandbox.Backend] {
return fmt.Errorf("unsupported sandbox.backend: %s", c.Sandbox.Backend)
}

if m := c.Logging.Mode; m != "production" && m != "development" {
if m := c.Logging.Mode; m != LogModeProduction && m != LogModeDevelopment {
return fmt.Errorf("invalid logging.mode: %s, must be 'production' or 'development'", m)
}

logLevels := map[string]bool{"debug": true, "info": true, "warn": true, "error": true, "dpanic": true, "panic": true, "fatal": true}
logLevels := map[string]bool{"debug": true, LogLevelInfo: true, "warn": true, "error": true, "dpanic": true, "panic": true, "fatal": true}
if !logLevels[c.Logging.Level] {
return fmt.Errorf("invalid logging.level: %s", c.Logging.Level)
}
Expand Down
58 changes: 29 additions & 29 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ func TestConfigValidation(t *testing.T) {
// Test that a valid config does not fail validation
cfg := &Config{
Server: ServerConfig{
Transport: "http",
Transport: TransportHTTP,
HTTPPort: 8080,
},
Sandbox: SandboxConfig{
Backend: "docker",
Backend: BackendDocker,
TimeoutSec: 30,
MemoryMB: 512,
MaxArtifactSizeMB: 20,
NetworkEnabled: false,
EnableLocalBackend: false,
},
Logging: LoggingConfig{
Mode: "production",
Level: "info",
Mode: LogModeProduction,
Level: LogLevelInfo,
},
Languages: map[string]Language{
"python": {
Expand All @@ -45,16 +45,16 @@ func TestConfigValidation(t *testing.T) {
HTTPPort: 8080,
},
Sandbox: SandboxConfig{
Backend: "docker",
Backend: BackendDocker,
TimeoutSec: 30,
MemoryMB: 512,
MaxArtifactSizeMB: 20,
NetworkEnabled: false,
EnableLocalBackend: false,
},
Logging: LoggingConfig{
Mode: "production",
Level: "info",
Mode: LogModeProduction,
Level: LogLevelInfo,
},
}

Expand All @@ -66,20 +66,20 @@ func TestConfigValidation(t *testing.T) {
t.Run("InvalidSandboxTimeout", func(t *testing.T) {
cfg := &Config{
Server: ServerConfig{
Transport: "http",
Transport: TransportHTTP,
HTTPPort: 8080,
},
Sandbox: SandboxConfig{
Backend: "docker",
Backend: BackendDocker,
TimeoutSec: 0, // Invalid: must be positive
MemoryMB: 512,
MaxArtifactSizeMB: 20,
NetworkEnabled: false,
EnableLocalBackend: false,
},
Logging: LoggingConfig{
Mode: "production",
Level: "info",
Mode: LogModeProduction,
Level: LogLevelInfo,
},
}

Expand All @@ -91,20 +91,20 @@ func TestConfigValidation(t *testing.T) {
t.Run("InvalidSandboxMemory", func(t *testing.T) {
cfg := &Config{
Server: ServerConfig{
Transport: "http",
Transport: TransportHTTP,
HTTPPort: 8080,
},
Sandbox: SandboxConfig{
Backend: "docker",
Backend: BackendDocker,
TimeoutSec: 30,
MemoryMB: 0, // Invalid: must be positive
MaxArtifactSizeMB: 20,
NetworkEnabled: false,
EnableLocalBackend: false,
},
Logging: LoggingConfig{
Mode: "production",
Level: "info",
Mode: LogModeProduction,
Level: LogLevelInfo,
},
}

Expand All @@ -116,11 +116,11 @@ func TestConfigValidation(t *testing.T) {
t.Run("InvalidLoggingMode", func(t *testing.T) {
cfg := &Config{
Server: ServerConfig{
Transport: "http",
Transport: TransportHTTP,
HTTPPort: 8080,
},
Sandbox: SandboxConfig{
Backend: "docker",
Backend: BackendDocker,
TimeoutSec: 30,
MemoryMB: 512,
MaxArtifactSizeMB: 20,
Expand All @@ -129,7 +129,7 @@ func TestConfigValidation(t *testing.T) {
},
Logging: LoggingConfig{
Mode: "invalid_mode", // Invalid mode
Level: "info",
Level: LogLevelInfo,
},
}

Expand All @@ -141,19 +141,19 @@ func TestConfigValidation(t *testing.T) {
t.Run("InvalidLogLevel", func(t *testing.T) {
cfg := &Config{
Server: ServerConfig{
Transport: "http",
Transport: TransportHTTP,
HTTPPort: 8080,
},
Sandbox: SandboxConfig{
Backend: "docker",
Backend: BackendDocker,
TimeoutSec: 30,
MemoryMB: 512,
MaxArtifactSizeMB: 20,
NetworkEnabled: false,
EnableLocalBackend: false,
},
Logging: LoggingConfig{
Mode: "production",
Mode: LogModeProduction,
Level: "invalid_level", // Invalid level
},
}
Expand All @@ -166,20 +166,20 @@ func TestConfigValidation(t *testing.T) {
t.Run("ValidBackendWhenLocalEnabled", func(t *testing.T) {
cfg := &Config{
Server: ServerConfig{
Transport: "http",
Transport: TransportHTTP,
HTTPPort: 8080,
},
Sandbox: SandboxConfig{
Backend: "local",
Backend: BackendLocal,
TimeoutSec: 30,
MemoryMB: 512,
MaxArtifactSizeMB: 20,
NetworkEnabled: false,
EnableLocalBackend: true, // Local backend enabled
},
Logging: LoggingConfig{
Mode: "production",
Level: "info",
Mode: LogModeProduction,
Level: LogLevelInfo,
},
}

Expand All @@ -190,20 +190,20 @@ func TestConfigValidation(t *testing.T) {
t.Run("InvalidBackendWhenLocalNotEnabled", func(t *testing.T) {
cfg := &Config{
Server: ServerConfig{
Transport: "http",
Transport: TransportHTTP,
HTTPPort: 8080,
},
Sandbox: SandboxConfig{
Backend: "local",
Backend: BackendLocal,
TimeoutSec: 30,
MemoryMB: 512,
MaxArtifactSizeMB: 20,
NetworkEnabled: false,
EnableLocalBackend: false, // Local backend not enabled
},
Logging: LoggingConfig{
Mode: "production",
Level: "info",
Mode: LogModeProduction,
Level: LogLevelInfo,
},
}

Expand Down
32 changes: 16 additions & 16 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestIntegrationConfigLoggerSandbox(t *testing.T) {
// Test that config validation works properly with logger initialization
cfg := &config.Config{
Server: config.ServerConfig{
Transport: "stdio",
Transport: config.TransportStdio,
HTTPPort: 8080,
},
Sandbox: config.SandboxConfig{
Expand All @@ -31,7 +31,7 @@ func TestIntegrationConfigLoggerSandbox(t *testing.T) {
EnableLocalBackend: false,
},
Logging: config.LoggingConfig{
Mode: "development",
Mode: config.LogModeDevelopment,
Level: "debug",
},
Languages: map[string]config.Language{},
Expand All @@ -50,23 +50,23 @@ func TestIntegrationConfigLoggerSandbox(t *testing.T) {
t.Run("ConfigLoggerSandboxFactoryIntegration", func(t *testing.T) {
cfg := &config.Config{
Server: config.ServerConfig{
Transport: "stdio",
Transport: config.TransportStdio,
HTTPPort: 8080,
},
Sandbox: config.SandboxConfig{
Backend: "local", // Use local backend for testing without Docker
TimeoutSec: 10, // Short timeout for tests
MemoryMB: 128, // Lower memory for faster tests
Backend: config.BackendLocal, // Use local backend for testing without Docker
TimeoutSec: 10, // Short timeout for tests
MemoryMB: 128, // Lower memory for faster tests
MaxArtifactSizeMB: 5,
NetworkEnabled: false,
EnableLocalBackend: true, // Must be enabled for local backend
},
Logging: config.LoggingConfig{
Mode: "development",
Mode: config.LogModeDevelopment,
Level: "info",
},
Languages: map[string]config.Language{
"python": {
sandbox.LanguagePython: {
Image: "python:3.11-slim",
Environment: map[string]string{"PYTHONPATH": "/workdir"},
},
Expand All @@ -91,23 +91,23 @@ func TestIntegrationConfigLoggerSandbox(t *testing.T) {
t.Run("FullMCPIntegration", func(t *testing.T) {
cfg := &config.Config{
Server: config.ServerConfig{
Transport: "stdio",
Transport: config.TransportStdio,
HTTPPort: 8080,
},
Sandbox: config.SandboxConfig{
Backend: "local",
Backend: config.BackendLocal,
TimeoutSec: 5,
MemoryMB: 128,
MaxArtifactSizeMB: 5,
NetworkEnabled: false,
EnableLocalBackend: true,
},
Logging: config.LoggingConfig{
Mode: "development",
Mode: config.LogModeDevelopment,
Level: "info",
},
Languages: map[string]config.Language{
"python": {
sandbox.LanguagePython: {
Environment: map[string]string{},
},
"nodejs": {
Expand Down Expand Up @@ -151,15 +151,15 @@ func TestIntegrationSandboxExecution(t *testing.T) {
t.Run("LocalExecutorCreation", func(t *testing.T) {
cfg := &config.Config{
Sandbox: config.SandboxConfig{
Backend: "local",
Backend: config.BackendLocal,
TimeoutSec: 5,
MemoryMB: 128,
MaxArtifactSizeMB: 5,
NetworkEnabled: false,
EnableLocalBackend: true,
},
Languages: map[string]config.Language{
"python": {
sandbox.LanguagePython: {
Environment: map[string]string{},
},
},
Expand All @@ -176,15 +176,15 @@ func TestIntegrationSandboxExecution(t *testing.T) {
t.Run("ExecutorWithDifferentLanguages", func(t *testing.T) {
cfg := &config.Config{
Sandbox: config.SandboxConfig{
Backend: "local",
Backend: config.BackendLocal,
TimeoutSec: 5,
MemoryMB: 128,
MaxArtifactSizeMB: 5,
NetworkEnabled: false,
EnableLocalBackend: true,
},
Languages: map[string]config.Language{
"python": {
sandbox.LanguagePython: {
Environment: map[string]string{},
},
"nodejs": {
Expand Down
Loading
Loading