- go mod init github.com/dal/kaos - Config paket sa .env učitavanjem i validacijom - Supervisor skeleton paket - Entry point (cmd/kaos-supervisor/main.go) - Makefile (build, test, vet, clean, all) - .env.example, .gitignore - 6 config testova — svi prolaze Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
104 lines
2.4 KiB
Go
104 lines
2.4 KiB
Go
// Package config tests verify configuration loading from
|
|
// environment variables and .env files.
|
|
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestLoad_WithValidEnv(t *testing.T) {
|
|
t.Setenv("KAOS_TIMEOUT", "30m")
|
|
t.Setenv("KAOS_PROJECT_PATH", "/tmp/test")
|
|
|
|
cfg, err := Load()
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
if cfg.Timeout != 30*time.Minute {
|
|
t.Errorf("expected timeout 30m, got %v", cfg.Timeout)
|
|
}
|
|
|
|
if cfg.ProjectPath != "/tmp/test" {
|
|
t.Errorf("expected project path /tmp/test, got %s", cfg.ProjectPath)
|
|
}
|
|
}
|
|
|
|
func TestLoad_MissingTimeout(t *testing.T) {
|
|
t.Setenv("KAOS_TIMEOUT", "")
|
|
t.Setenv("KAOS_PROJECT_PATH", "/tmp/test")
|
|
|
|
_, err := Load()
|
|
if err == nil {
|
|
t.Fatal("expected error for missing KAOS_TIMEOUT")
|
|
}
|
|
}
|
|
|
|
func TestLoad_InvalidTimeout(t *testing.T) {
|
|
t.Setenv("KAOS_TIMEOUT", "not-a-duration")
|
|
t.Setenv("KAOS_PROJECT_PATH", "/tmp/test")
|
|
|
|
_, err := Load()
|
|
if err == nil {
|
|
t.Fatal("expected error for invalid KAOS_TIMEOUT")
|
|
}
|
|
}
|
|
|
|
func TestLoad_MissingProjectPath(t *testing.T) {
|
|
t.Setenv("KAOS_TIMEOUT", "30m")
|
|
t.Setenv("KAOS_PROJECT_PATH", "")
|
|
|
|
_, err := Load()
|
|
if err == nil {
|
|
t.Fatal("expected error for missing KAOS_PROJECT_PATH")
|
|
}
|
|
}
|
|
|
|
func TestLoadEnvFile(t *testing.T) {
|
|
dir := t.TempDir()
|
|
envPath := filepath.Join(dir, ".env")
|
|
|
|
content := "TEST_KAOS_VAR=hello\n# comment\nTEST_KAOS_OTHER=world\n"
|
|
if err := os.WriteFile(envPath, []byte(content), 0644); err != nil {
|
|
t.Fatalf("failed to write .env: %v", err)
|
|
}
|
|
|
|
// Ensure vars are not set
|
|
os.Unsetenv("TEST_KAOS_VAR")
|
|
os.Unsetenv("TEST_KAOS_OTHER")
|
|
|
|
loadEnvFile(envPath)
|
|
|
|
if v := os.Getenv("TEST_KAOS_VAR"); v != "hello" {
|
|
t.Errorf("expected TEST_KAOS_VAR=hello, got %q", v)
|
|
}
|
|
if v := os.Getenv("TEST_KAOS_OTHER"); v != "world" {
|
|
t.Errorf("expected TEST_KAOS_OTHER=world, got %q", v)
|
|
}
|
|
|
|
// Cleanup
|
|
os.Unsetenv("TEST_KAOS_VAR")
|
|
os.Unsetenv("TEST_KAOS_OTHER")
|
|
}
|
|
|
|
func TestLoadEnvFile_DoesNotOverride(t *testing.T) {
|
|
dir := t.TempDir()
|
|
envPath := filepath.Join(dir, ".env")
|
|
|
|
content := "TEST_KAOS_EXISTING=from_file\n"
|
|
if err := os.WriteFile(envPath, []byte(content), 0644); err != nil {
|
|
t.Fatalf("failed to write .env: %v", err)
|
|
}
|
|
|
|
t.Setenv("TEST_KAOS_EXISTING", "from_env")
|
|
|
|
loadEnvFile(envPath)
|
|
|
|
if v := os.Getenv("TEST_KAOS_EXISTING"); v != "from_env" {
|
|
t.Errorf("expected TEST_KAOS_EXISTING=from_env, got %q", v)
|
|
}
|
|
}
|