Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: add missing healthcheck cli command (#2929)
Kyle Mendell3748a624a1b8
diff --git a/backend/cli/health.go b/backend/cli/health.gonew file mode 100644index 000000000..a65cf42f4--- /dev/null+++ b/backend/cli/health.go@@ -0,0 +1,103 @@+package cli++import (+ "context"+ "errors"+ "fmt"+ "net"+ "net/http"+ "net/url"+ "strconv"+ "strings"+ "time"++ "github.com/getarcaneapp/arcane/backend/v2/internal/config"+ "github.com/spf13/cobra"+)++const (+ defaultHealthTimeout = 5 * time.Second+ defaultHealthPort = "3552"+ healthHost = "127.0.0.1"+ healthPath = "/api/health"+)++var healthTimeout time.Duration++var HealthCmd = &cobra.Command{+ Use: "health",+ Short: "Check API health",+ RunE: func(cmd *cobra.Command, args []string) error {+ cfg := config.Load()+ timeout := healthTimeout+ if timeout <= 0 {+ timeout = defaultHealthTimeout+ }++ if err := runHealthCommandInternal(cmd.Context(), cfg, timeout); err != nil {+ return err+ }++ return nil+ },+}++func buildHealthURLInternal(cfg *config.Config) (string, error) {+ if cfg == nil {+ return "", errors.New("health config is nil")+ }++ port := strings.TrimSpace(cfg.Port)+ if port == "" {+ port = defaultHealthPort+ }+ if _, err := strconv.Atoi(port); err != nil {+ return "", fmt.Errorf("invalid health port %q: %w", port, err)+ }++ hostPort := net.JoinHostPort(healthHost, port)++ u := &url.URL{+ Scheme: "http",+ Host: hostPort,+ Path: healthPath,+ }++ return u.String(), nil+}++func runHealthCommandInternal(ctx context.Context, cfg *config.Config, timeout time.Duration) error {+ if timeout <= 0 {+ timeout = defaultHealthTimeout+ }++ healthURL, err := buildHealthURLInternal(cfg)+ if err != nil {+ return err+ }++ request, err := http.NewRequestWithContext(ctx, http.MethodHead, healthURL, nil)+ if err != nil {+ return fmt.Errorf("health check request creation failed: %w", err)+ }++ client := &http.Client{Timeout: timeout}+ resp, err := client.Do(request)+ if err != nil {+ return fmt.Errorf("health check request failed: %w", err)+ }+ defer func() {+ _ = resp.Body.Close()+ }()++ if resp.StatusCode < 200 || resp.StatusCode >= 300 {+ return fmt.Errorf("health check failed with status %d", resp.StatusCode)+ }++ return nil+}++func init() {+ HealthCmd.Flags().DurationVar(&healthTimeout, "timeout", defaultHealthTimeout, "Health check timeout (e.g. 5s)")+ rootCmd.AddCommand(HealthCmd)+}diff --git a/backend/cli/health_test.go b/backend/cli/health_test.gonew file mode 100644index 000000000..24ddbd95b--- /dev/null+++ b/backend/cli/health_test.go@@ -0,0 +1,125 @@+package cli++import (+ "context"+ "fmt"+ "net/http"+ "net/http/httptest"+ "strconv"+ "strings"+ "testing"+ "time"++ "github.com/getarcaneapp/arcane/backend/v2/internal/config"+)++func TestBuildHealthURLInternalDefaults(t *testing.T) {+ t.Run("default_port", func(t *testing.T) {+ cfg := &config.Config{}+ healthURL, err := buildHealthURLInternal(cfg)+ if err != nil {+ t.Fatalf("build health URL failed: %v", err)+ }++ if !strings.HasPrefix(healthURL, "http://127.0.0.1:3552") {+ t.Fatalf("unexpected health URL: %s", healthURL)+ }+ if !strings.HasSuffix(healthURL, "/api/health") {+ t.Fatalf("expected health URL path /api/health, got: %s", healthURL)+ }+ })++ t.Run("explicit_port", func(t *testing.T) {+ cfg := &config.Config{Port: "8443"}+ healthURL, err := buildHealthURLInternal(cfg)+ if err != nil {+ t.Fatalf("build health URL failed: %v", err)+ }+ if !strings.HasPrefix(healthURL, "http://127.0.0.1:8443") {+ t.Fatalf("unexpected health URL: %s", healthURL)+ }+ })+}++func TestBuildHealthURLInternalInvalidPort(t *testing.T) {+ cfg := &config.Config{Port: "invalid-port"}+ _, err := buildHealthURLInternal(cfg)+ if err == nil {+ t.Fatal("expected invalid health URL port to fail")+ }+}++func TestRunHealthCommandInternal(t *testing.T) {+ s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ if r.Method != http.MethodHead {+ t.Fatalf("expected HEAD request, got: %s", r.Method)+ }+ if r.URL.Path != "/api/health" {+ t.Fatalf("unexpected path: %s", r.URL.Path)+ }+ w.WriteHeader(http.StatusOK)+ }))+ defer s.Close()++ _, port, ok := strings.Cut(strings.TrimPrefix(s.URL, "http://"), ":")+ if !ok {+ t.Fatalf("failed to parse port from %s", s.URL)+ }+ if strings.Contains(port, "/") {+ port = strings.SplitN(port, "/", 2)[0]+ }+ if _, err := strconv.Atoi(port); err != nil {+ t.Fatalf("invalid port in test server URL %q: %v", s.URL, err)+ }++ cfg := &config.Config{Port: port}+ if err := runHealthCommandInternal(context.Background(), cfg, 2*time.Second); err != nil {+ t.Fatalf("health command failed: %v", err)+ }+}++func TestRunHealthCommandInternalNon2xx(t *testing.T) {+ s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {+ w.WriteHeader(http.StatusServiceUnavailable)+ }))+ defer s.Close()++ _, port, ok := strings.Cut(strings.TrimPrefix(s.URL, "http://"), ":")+ if !ok {+ t.Fatalf("failed to parse port from %s", s.URL)+ }+ if strings.Contains(port, "/") {+ port = strings.SplitN(port, "/", 2)[0]+ }++ cfg := &config.Config{Port: port}+ err := runHealthCommandInternal(context.Background(), cfg, 2*time.Second)+ if err == nil {+ t.Fatal("expected non-2xx response to fail")+ }+ if !strings.Contains(err.Error(), "health check failed with status") {+ t.Fatalf("unexpected error: %v", err)+ }+}++func TestRunHealthCommandInternalConnectionFailure(t *testing.T) {+ err := runHealthCommandInternal(context.Background(), &config.Config{Port: "1"}, 200*time.Millisecond)+ if err == nil {+ t.Fatal("expected connection failure")+ }+}++func TestRunHealthCommandInternalDefaultTimeoutFallback(t *testing.T) {+ err := runHealthCommandInternal(context.Background(), &config.Config{Port: "1"}, 0)+ if err == nil {+ t.Fatal("expected connection failure")+ }+ if !strings.Contains(err.Error(), "health check request failed") {+ t.Fatalf("unexpected error: %v", err)+ }++ // Ensure timeout path is exercised by checking the string formatting path.+ if !strings.Contains(fmt.Sprint(defaultHealthTimeout), "5s") {+ t.Fatalf("unexpected default timeout: %s", defaultHealthTimeout)+ }+}diff --git a/docker/examples/compose.basic.yaml b/docker/examples/compose.basic.yamlindex 180ffe739..ec1ae92aa 100644--- a/docker/examples/compose.basic.yaml+++ b/docker/examples/compose.basic.yaml@@ -25,12 +25,7 @@ services:cgroup: hosthealthcheck:test:- [- "CMD",- "curl",- "-fsS",- "http://localhost:3552/api/health",- ]+ ["CMD", "./arcane", "health", "--timeout", "2s"]interval: 10stimeout: 3sretries: 5diff --git a/docker/examples/compose.proxy.yaml b/docker/examples/compose.proxy.yamlindex f192cd663..bab0d64e7 100644--- a/docker/examples/compose.proxy.yaml+++ b/docker/examples/compose.proxy.yaml@@ -58,12 +58,7 @@ services:- docker-socket-proxyhealthcheck:test:- [- "CMD",- "curl",- "-fsS",- "http://localhost:3552/api/health",- ]+ ["CMD", "./arcane", "health", "--timeout", "2s"]interval: 10stimeout: 3sretries: 5