first commit
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultMode = "smoke"
|
||||
DefaultPlatformURL = "http://127.0.0.1:8080"
|
||||
DefaultEndpointID = "run-local"
|
||||
DefaultDisplayName = "Local Run"
|
||||
DefaultVersion = "0.1.0"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Mode string
|
||||
PlatformURL string
|
||||
RunEndpointID string
|
||||
DisplayName string
|
||||
Version string
|
||||
RegistrationToken string
|
||||
WorkspaceRoot string
|
||||
SpoolRoot string
|
||||
MaxJobs int
|
||||
HeartbeatInterval time.Duration
|
||||
PollInterval time.Duration
|
||||
RetryBackoff time.Duration
|
||||
}
|
||||
|
||||
func Load() Config {
|
||||
mode := os.Getenv("RUN_MODE")
|
||||
if mode == "" {
|
||||
mode = DefaultMode
|
||||
}
|
||||
|
||||
platformURL := os.Getenv("RUN_PLATFORM_URL")
|
||||
if platformURL == "" {
|
||||
platformURL = DefaultPlatformURL
|
||||
}
|
||||
|
||||
workspaceRoot := envOrDefault("RUN_WORKSPACE_ROOT", filepath.Join(".", ".run-workspace"))
|
||||
return Config{
|
||||
Mode: mode,
|
||||
PlatformURL: platformURL,
|
||||
RunEndpointID: envOrDefault("RUN_ENDPOINT_ID", DefaultEndpointID),
|
||||
DisplayName: envOrDefault("RUN_DISPLAY_NAME", DefaultDisplayName),
|
||||
Version: envOrDefault("RUN_VERSION", DefaultVersion),
|
||||
RegistrationToken: envOrDefault("RUN_REGISTRATION_TOKEN", "local-registration"),
|
||||
WorkspaceRoot: workspaceRoot,
|
||||
SpoolRoot: envOrDefault("RUN_SPOOL_ROOT", filepath.Join(workspaceRoot, "spool")),
|
||||
MaxJobs: intEnvOrDefault("RUN_MAX_JOBS", 1),
|
||||
HeartbeatInterval: durationEnvOrDefault("RUN_HEARTBEAT_INTERVAL_MS", 15*time.Second),
|
||||
PollInterval: durationEnvOrDefault("RUN_POLL_INTERVAL_MS", 2*time.Second),
|
||||
RetryBackoff: durationEnvOrDefault("RUN_RETRY_BACKOFF_MS", time.Second),
|
||||
}
|
||||
}
|
||||
|
||||
func envOrDefault(key string, fallback string) string {
|
||||
value := os.Getenv(key)
|
||||
if value == "" {
|
||||
return fallback
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func intEnvOrDefault(key string, fallback int) int {
|
||||
value, err := strconv.Atoi(os.Getenv(key))
|
||||
if err != nil || value <= 0 {
|
||||
return fallback
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func durationEnvOrDefault(key string, fallback time.Duration) time.Duration {
|
||||
value, err := strconv.Atoi(os.Getenv(key))
|
||||
if err != nil || value <= 0 {
|
||||
return fallback
|
||||
}
|
||||
return time.Duration(value) * time.Millisecond
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package config
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestLoadUsesDefaults(t *testing.T) {
|
||||
t.Setenv("RUN_MODE", "")
|
||||
t.Setenv("RUN_PLATFORM_URL", "")
|
||||
|
||||
cfg := Load()
|
||||
if cfg.Mode != DefaultMode {
|
||||
t.Fatalf("expected mode %q, got %q", DefaultMode, cfg.Mode)
|
||||
}
|
||||
if cfg.PlatformURL != DefaultPlatformURL {
|
||||
t.Fatalf("expected platform URL %q, got %q", DefaultPlatformURL, cfg.PlatformURL)
|
||||
}
|
||||
if cfg.RunEndpointID != DefaultEndpointID || cfg.DisplayName != DefaultDisplayName || cfg.Version != DefaultVersion {
|
||||
t.Fatalf("expected worker defaults, got %+v", cfg)
|
||||
}
|
||||
if cfg.MaxJobs != 1 || cfg.HeartbeatInterval <= 0 || cfg.PollInterval <= 0 || cfg.RetryBackoff <= 0 {
|
||||
t.Fatalf("expected positive worker scheduling defaults, got %+v", cfg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadUsesEnvironment(t *testing.T) {
|
||||
t.Setenv("RUN_MODE", "worker")
|
||||
t.Setenv("RUN_PLATFORM_URL", "http://platform.test")
|
||||
t.Setenv("RUN_ENDPOINT_ID", "run-edge")
|
||||
t.Setenv("RUN_DISPLAY_NAME", "Edge Run")
|
||||
t.Setenv("RUN_VERSION", "1.2.3")
|
||||
t.Setenv("RUN_REGISTRATION_TOKEN", "registration-token")
|
||||
t.Setenv("RUN_WORKSPACE_ROOT", "/tmp/run-workspace")
|
||||
t.Setenv("RUN_SPOOL_ROOT", "/tmp/run-spool")
|
||||
t.Setenv("RUN_MAX_JOBS", "3")
|
||||
t.Setenv("RUN_HEARTBEAT_INTERVAL_MS", "250")
|
||||
t.Setenv("RUN_POLL_INTERVAL_MS", "125")
|
||||
t.Setenv("RUN_RETRY_BACKOFF_MS", "75")
|
||||
|
||||
cfg := Load()
|
||||
if cfg.Mode != "worker" {
|
||||
t.Fatalf("expected configured mode, got %q", cfg.Mode)
|
||||
}
|
||||
if cfg.PlatformURL != "http://platform.test" {
|
||||
t.Fatalf("expected configured platform URL, got %q", cfg.PlatformURL)
|
||||
}
|
||||
if cfg.RunEndpointID != "run-edge" || cfg.DisplayName != "Edge Run" || cfg.Version != "1.2.3" || cfg.RegistrationToken != "registration-token" {
|
||||
t.Fatalf("expected configured worker identity, got %+v", cfg)
|
||||
}
|
||||
if cfg.WorkspaceRoot != "/tmp/run-workspace" || cfg.SpoolRoot != "/tmp/run-spool" || cfg.MaxJobs != 3 {
|
||||
t.Fatalf("expected configured worker paths/capacity, got %+v", cfg)
|
||||
}
|
||||
if cfg.HeartbeatInterval.Milliseconds() != 250 || cfg.PollInterval.Milliseconds() != 125 || cfg.RetryBackoff.Milliseconds() != 75 {
|
||||
t.Fatalf("expected configured durations, got %+v", cfg)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user