mirror of
https://tangled.org/evan.jarrett.net/at-container-registry
synced 2026-08-31 05:07:09 +00:00
364 lines
9.5 KiB
Go
364 lines
9.5 KiB
Go
package hold
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// setupEnv sets environment variables for testing and returns a cleanup function
|
|
func setupEnv(t *testing.T, vars map[string]string) func() {
|
|
// Save original env
|
|
original := make(map[string]string)
|
|
for k := range vars {
|
|
original[k] = os.Getenv(k)
|
|
}
|
|
|
|
// Set test env vars
|
|
for k, v := range vars {
|
|
if err := os.Setenv(k, v); err != nil {
|
|
t.Fatalf("Failed to set env %s: %v", k, err)
|
|
}
|
|
}
|
|
|
|
// Return cleanup function
|
|
return func() {
|
|
for k, v := range original {
|
|
if v == "" {
|
|
os.Unsetenv(k)
|
|
} else {
|
|
os.Setenv(k, v)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLoadConfigFromEnv_Success(t *testing.T) {
|
|
cleanup := setupEnv(t, map[string]string{
|
|
"HOLD_PUBLIC_URL": "https://hold.example.com",
|
|
"HOLD_SERVER_ADDR": ":9000",
|
|
"HOLD_PUBLIC": "true",
|
|
"TEST_MODE": "true",
|
|
"HOLD_OWNER": "did:plc:owner123",
|
|
"HOLD_ALLOW_ALL_CREW": "true",
|
|
"STORAGE_DRIVER": "filesystem",
|
|
"STORAGE_ROOT_DIR": "/tmp/test-storage",
|
|
"HOLD_DATABASE_DIR": "/tmp/test-db",
|
|
"HOLD_KEY_PATH": "/tmp/test-key.pem",
|
|
})
|
|
defer cleanup()
|
|
|
|
cfg, err := LoadConfigFromEnv()
|
|
if err != nil {
|
|
t.Fatalf("Expected success, got error: %v", err)
|
|
}
|
|
|
|
// Verify server config
|
|
if cfg.Server.PublicURL != "https://hold.example.com" {
|
|
t.Errorf("Expected PublicURL=https://hold.example.com, got %s", cfg.Server.PublicURL)
|
|
}
|
|
if cfg.Server.Addr != ":9000" {
|
|
t.Errorf("Expected Addr=:9000, got %s", cfg.Server.Addr)
|
|
}
|
|
if !cfg.Server.Public {
|
|
t.Error("Expected Public=true")
|
|
}
|
|
if !cfg.Server.TestMode {
|
|
t.Error("Expected TestMode=true")
|
|
}
|
|
if cfg.Server.ReadTimeout != 5*time.Minute {
|
|
t.Errorf("Expected ReadTimeout=5m, got %v", cfg.Server.ReadTimeout)
|
|
}
|
|
|
|
// Verify registration config
|
|
if cfg.Registration.OwnerDID != "did:plc:owner123" {
|
|
t.Errorf("Expected OwnerDID=did:plc:owner123, got %s", cfg.Registration.OwnerDID)
|
|
}
|
|
if !cfg.Registration.AllowAllCrew {
|
|
t.Error("Expected AllowAllCrew=true")
|
|
}
|
|
|
|
// Verify database config
|
|
if cfg.Database.Path != "/tmp/test-db" {
|
|
t.Errorf("Expected Database.Path=/tmp/test-db, got %s", cfg.Database.Path)
|
|
}
|
|
if cfg.Database.KeyPath != "/tmp/test-key.pem" {
|
|
t.Errorf("Expected Database.KeyPath=/tmp/test-key.pem, got %s", cfg.Database.KeyPath)
|
|
}
|
|
}
|
|
|
|
func TestLoadConfigFromEnv_MissingPublicURL(t *testing.T) {
|
|
cleanup := setupEnv(t, map[string]string{
|
|
"HOLD_PUBLIC_URL": "", // Missing required field
|
|
"STORAGE_DRIVER": "filesystem",
|
|
})
|
|
defer cleanup()
|
|
|
|
_, err := LoadConfigFromEnv()
|
|
if err == nil {
|
|
t.Error("Expected error for missing HOLD_PUBLIC_URL")
|
|
}
|
|
}
|
|
|
|
func TestLoadConfigFromEnv_Defaults(t *testing.T) {
|
|
cleanup := setupEnv(t, map[string]string{
|
|
"HOLD_PUBLIC_URL": "https://hold.example.com",
|
|
"STORAGE_DRIVER": "filesystem",
|
|
// Don't set optional vars - test defaults
|
|
"HOLD_SERVER_ADDR": "",
|
|
"HOLD_PUBLIC": "",
|
|
"TEST_MODE": "",
|
|
"HOLD_OWNER": "",
|
|
"HOLD_ALLOW_ALL_CREW": "",
|
|
"AWS_REGION": "",
|
|
"STORAGE_ROOT_DIR": "",
|
|
"HOLD_DATABASE_DIR": "",
|
|
})
|
|
defer cleanup()
|
|
|
|
cfg, err := LoadConfigFromEnv()
|
|
if err != nil {
|
|
t.Fatalf("Expected success, got error: %v", err)
|
|
}
|
|
|
|
// Verify defaults
|
|
if cfg.Server.Addr != ":8080" {
|
|
t.Errorf("Expected default Addr=:8080, got %s", cfg.Server.Addr)
|
|
}
|
|
if cfg.Server.Public {
|
|
t.Error("Expected default Public=false")
|
|
}
|
|
if cfg.Server.TestMode {
|
|
t.Error("Expected default TestMode=false")
|
|
}
|
|
if cfg.Server.DisablePresignedURLs {
|
|
t.Error("Expected default DisablePresignedURLs=false")
|
|
}
|
|
if cfg.Registration.OwnerDID != "" {
|
|
t.Error("Expected default OwnerDID to be empty")
|
|
}
|
|
if cfg.Registration.AllowAllCrew {
|
|
t.Error("Expected default AllowAllCrew=false")
|
|
}
|
|
if cfg.Database.Path != "/var/lib/atcr-hold" {
|
|
t.Errorf("Expected default Database.Path=/var/lib/atcr-hold, got %s", cfg.Database.Path)
|
|
}
|
|
}
|
|
|
|
func TestLoadConfigFromEnv_KeyPathDefault(t *testing.T) {
|
|
cleanup := setupEnv(t, map[string]string{
|
|
"HOLD_PUBLIC_URL": "https://hold.example.com",
|
|
"STORAGE_DRIVER": "filesystem",
|
|
"HOLD_DATABASE_DIR": "/custom/db/path",
|
|
"HOLD_KEY_PATH": "", // Should default to {Database.Path}/signing.key
|
|
})
|
|
defer cleanup()
|
|
|
|
cfg, err := LoadConfigFromEnv()
|
|
if err != nil {
|
|
t.Fatalf("Expected success, got error: %v", err)
|
|
}
|
|
|
|
expectedKeyPath := filepath.Join("/custom/db/path", "signing.key")
|
|
if cfg.Database.KeyPath != expectedKeyPath {
|
|
t.Errorf("Expected KeyPath=%s, got %s", expectedKeyPath, cfg.Database.KeyPath)
|
|
}
|
|
}
|
|
|
|
func TestLoadConfigFromEnv_DisablePresignedURLs(t *testing.T) {
|
|
cleanup := setupEnv(t, map[string]string{
|
|
"HOLD_PUBLIC_URL": "https://hold.example.com",
|
|
"STORAGE_DRIVER": "filesystem",
|
|
"DISABLE_PRESIGNED_URLS": "true",
|
|
})
|
|
defer cleanup()
|
|
|
|
cfg, err := LoadConfigFromEnv()
|
|
if err != nil {
|
|
t.Fatalf("Expected success, got error: %v", err)
|
|
}
|
|
|
|
if !cfg.Server.DisablePresignedURLs {
|
|
t.Error("Expected DisablePresignedURLs=true")
|
|
}
|
|
}
|
|
|
|
func TestBuildStorageConfig_S3_Complete(t *testing.T) {
|
|
cleanup := setupEnv(t, map[string]string{
|
|
"AWS_ACCESS_KEY_ID": "test-access-key",
|
|
"AWS_SECRET_ACCESS_KEY": "test-secret-key",
|
|
"AWS_REGION": "us-west-2",
|
|
"S3_BUCKET": "test-bucket",
|
|
"S3_ENDPOINT": "https://s3.example.com",
|
|
})
|
|
defer cleanup()
|
|
|
|
cfg, err := buildStorageConfig("s3")
|
|
if err != nil {
|
|
t.Fatalf("Expected success, got error: %v", err)
|
|
}
|
|
|
|
s3Params, ok := cfg.Storage["s3"]
|
|
if !ok {
|
|
t.Fatal("Expected s3 storage config")
|
|
}
|
|
|
|
params := map[string]any(s3Params)
|
|
|
|
if params["accesskey"] != "test-access-key" {
|
|
t.Errorf("Expected accesskey=test-access-key, got %v", params["accesskey"])
|
|
}
|
|
if params["secretkey"] != "test-secret-key" {
|
|
t.Errorf("Expected secretkey=test-secret-key, got %v", params["secretkey"])
|
|
}
|
|
if params["region"] != "us-west-2" {
|
|
t.Errorf("Expected region=us-west-2, got %v", params["region"])
|
|
}
|
|
if params["bucket"] != "test-bucket" {
|
|
t.Errorf("Expected bucket=test-bucket, got %v", params["bucket"])
|
|
}
|
|
if params["regionendpoint"] != "https://s3.example.com" {
|
|
t.Errorf("Expected regionendpoint=https://s3.example.com, got %v", params["regionendpoint"])
|
|
}
|
|
}
|
|
|
|
func TestBuildStorageConfig_S3_NoEndpoint(t *testing.T) {
|
|
cleanup := setupEnv(t, map[string]string{
|
|
"AWS_ACCESS_KEY_ID": "test-key",
|
|
"AWS_SECRET_ACCESS_KEY": "test-secret",
|
|
"S3_BUCKET": "test-bucket",
|
|
"S3_ENDPOINT": "", // No custom endpoint
|
|
"AWS_REGION": "", // Test default region
|
|
})
|
|
defer cleanup()
|
|
|
|
cfg, err := buildStorageConfig("s3")
|
|
if err != nil {
|
|
t.Fatalf("Expected success, got error: %v", err)
|
|
}
|
|
|
|
s3Params, ok := cfg.Storage["s3"]
|
|
if !ok {
|
|
t.Fatal("Expected s3 storage config")
|
|
}
|
|
|
|
params := map[string]any(s3Params)
|
|
|
|
// Should have default region
|
|
if params["region"] != "us-east-1" {
|
|
t.Errorf("Expected default region=us-east-1, got %v", params["region"])
|
|
}
|
|
|
|
// Should not have regionendpoint
|
|
if _, exists := params["regionendpoint"]; exists {
|
|
t.Error("Expected no regionendpoint when S3_ENDPOINT not set")
|
|
}
|
|
}
|
|
|
|
func TestBuildStorageConfig_S3_MissingBucket(t *testing.T) {
|
|
cleanup := setupEnv(t, map[string]string{
|
|
"AWS_ACCESS_KEY_ID": "test-key",
|
|
"AWS_SECRET_ACCESS_KEY": "test-secret",
|
|
"S3_BUCKET": "", // Missing required field
|
|
})
|
|
defer cleanup()
|
|
|
|
_, err := buildStorageConfig("s3")
|
|
if err == nil {
|
|
t.Error("Expected error for missing S3_BUCKET")
|
|
}
|
|
}
|
|
|
|
func TestBuildStorageConfig_Filesystem(t *testing.T) {
|
|
cleanup := setupEnv(t, map[string]string{
|
|
"STORAGE_ROOT_DIR": "/custom/storage/path",
|
|
})
|
|
defer cleanup()
|
|
|
|
cfg, err := buildStorageConfig("filesystem")
|
|
if err != nil {
|
|
t.Fatalf("Expected success, got error: %v", err)
|
|
}
|
|
|
|
fsParams, ok := cfg.Storage["filesystem"]
|
|
if !ok {
|
|
t.Fatal("Expected filesystem storage config")
|
|
}
|
|
|
|
params := map[string]any(fsParams)
|
|
|
|
if params["rootdirectory"] != "/custom/storage/path" {
|
|
t.Errorf("Expected rootdirectory=/custom/storage/path, got %v", params["rootdirectory"])
|
|
}
|
|
}
|
|
|
|
func TestBuildStorageConfig_Filesystem_Default(t *testing.T) {
|
|
cleanup := setupEnv(t, map[string]string{
|
|
"STORAGE_ROOT_DIR": "", // Test default
|
|
})
|
|
defer cleanup()
|
|
|
|
cfg, err := buildStorageConfig("filesystem")
|
|
if err != nil {
|
|
t.Fatalf("Expected success, got error: %v", err)
|
|
}
|
|
|
|
fsParams, ok := cfg.Storage["filesystem"]
|
|
if !ok {
|
|
t.Fatal("Expected filesystem storage config")
|
|
}
|
|
|
|
params := map[string]any(fsParams)
|
|
|
|
if params["rootdirectory"] != "/var/lib/atcr/hold" {
|
|
t.Errorf("Expected default rootdirectory=/var/lib/atcr/hold, got %v", params["rootdirectory"])
|
|
}
|
|
}
|
|
|
|
func TestBuildStorageConfig_UnsupportedDriver(t *testing.T) {
|
|
cleanup := setupEnv(t, map[string]string{})
|
|
defer cleanup()
|
|
|
|
_, err := buildStorageConfig("azure")
|
|
if err == nil {
|
|
t.Error("Expected error for unsupported driver")
|
|
}
|
|
}
|
|
|
|
func TestGetEnvOrDefault_Set(t *testing.T) {
|
|
cleanup := setupEnv(t, map[string]string{
|
|
"TEST_VAR": "custom-value",
|
|
})
|
|
defer cleanup()
|
|
|
|
result := getEnvOrDefault("TEST_VAR", "default-value")
|
|
if result != "custom-value" {
|
|
t.Errorf("Expected custom-value, got %s", result)
|
|
}
|
|
}
|
|
|
|
func TestGetEnvOrDefault_NotSet(t *testing.T) {
|
|
cleanup := setupEnv(t, map[string]string{
|
|
"TEST_VAR": "",
|
|
})
|
|
defer cleanup()
|
|
|
|
result := getEnvOrDefault("TEST_VAR", "default-value")
|
|
if result != "default-value" {
|
|
t.Errorf("Expected default-value, got %s", result)
|
|
}
|
|
}
|
|
|
|
func TestGetEnvOrDefault_EmptyString(t *testing.T) {
|
|
cleanup := setupEnv(t, map[string]string{
|
|
"TEST_VAR": "",
|
|
})
|
|
defer cleanup()
|
|
|
|
result := getEnvOrDefault("TEST_VAR", "")
|
|
if result != "" {
|
|
t.Errorf("Expected empty string, got %s", result)
|
|
}
|
|
}
|