feat: add dynamic path detection for container and host test environments
- Add isRunningInContainer() to detect test runtime environment - Add getUploadPath() to resolve upload paths (container vs host) - Add getCachePath() to resolve cache paths appropriately - Update setupTestServer() to use dynamic path helpers - Support environment variable overrides for flexibility - Add os import for file system checks
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -49,6 +50,56 @@ func trimSpace(s string) string {
|
|||||||
return strings.TrimSpace(s)
|
return strings.TrimSpace(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isRunningInContainer detects if tests are running inside a Docker container
|
||||||
|
func isRunningInContainer() bool {
|
||||||
|
// Check for container-specific marker file
|
||||||
|
if _, err := os.Stat("/.dockerenv"); err == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if /app/uploads exists (container path)
|
||||||
|
if _, err := os.Stat("/app/uploads"); err == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check environment variable (explicit override)
|
||||||
|
if os.Getenv("TEST_IN_CONTAINER") == "true" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// getUploadPath returns the appropriate upload path based on runtime environment
|
||||||
|
func getUploadPath() string {
|
||||||
|
// Check for explicit override first
|
||||||
|
if path := os.Getenv("TEST_UPLOAD_PATH"); path != "" {
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
|
||||||
|
if isRunningInContainer() {
|
||||||
|
return "/app/uploads" // Container path (right side of volume mount)
|
||||||
|
}
|
||||||
|
|
||||||
|
return "./uploads" // Host path (left side of volume mount)
|
||||||
|
}
|
||||||
|
|
||||||
|
// getCachePath returns the appropriate cache path based on runtime environment
|
||||||
|
func getCachePath() string {
|
||||||
|
// Check for explicit override first
|
||||||
|
if path := os.Getenv("TEST_CACHE_PATH"); path != "" {
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
|
||||||
|
if isRunningInContainer() {
|
||||||
|
return "/app/cache/kepub" // Container path (volume mount)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note: This is a Docker volume on host, not a folder
|
||||||
|
// Tests using this should handle the volume appropriately
|
||||||
|
return "/app/cache/kepub"
|
||||||
|
}
|
||||||
|
|
||||||
// setupTestServer creates a test server with a test database
|
// setupTestServer creates a test server with a test database
|
||||||
// Returns: (*httptest.Server, *database.Queries, *config.Config)
|
// Returns: (*httptest.Server, *database.Queries, *config.Config)
|
||||||
func setupTestServer(t *testing.T) (*httptest.Server, *database.Queries, *config.Config) {
|
func setupTestServer(t *testing.T) (*httptest.Server, *database.Queries, *config.Config) {
|
||||||
@@ -59,7 +110,7 @@ func setupTestServer(t *testing.T) (*httptest.Server, *database.Queries, *config
|
|||||||
cfg.ServerPort = "0" // Use random port for tests
|
cfg.ServerPort = "0" // Use random port for tests
|
||||||
cfg.BaseURL = "http://localhost"
|
cfg.BaseURL = "http://localhost"
|
||||||
cfg.JWTSecret = "test-secret-key"
|
cfg.JWTSecret = "test-secret-key"
|
||||||
cfg.UploadPath = "./test-uploads"
|
cfg.UploadPath = getUploadPath()
|
||||||
cfg.TestMode = true
|
cfg.TestMode = true
|
||||||
cfg.RateLimitEnabled = false
|
cfg.RateLimitEnabled = false
|
||||||
cfg.RequestsPerMinute = 1000
|
cfg.RequestsPerMinute = 1000
|
||||||
@@ -94,7 +145,7 @@ func setupTestServer(t *testing.T) (*httptest.Server, *database.Queries, *config
|
|||||||
queueHandler := handlers.NewQueueHandler(queries, queueProcessor)
|
queueHandler := handlers.NewQueueHandler(queries, queueProcessor)
|
||||||
|
|
||||||
// Create conversion service for OPDS
|
// Create conversion service for OPDS
|
||||||
conversionService := services.NewConversionService(queries, "/var/bookhoard/cache/kepub")
|
conversionService := services.NewConversionService(queries, getCachePath())
|
||||||
opdsHandler := handlers.NewOPDSHandler(queries, conversionService)
|
opdsHandler := handlers.NewOPDSHandler(queries, conversionService)
|
||||||
|
|
||||||
// Create Echo instance
|
// Create Echo instance
|
||||||
|
|||||||
Reference in New Issue
Block a user