feat: health check now return json, definitely
This commit is contained in:
@@ -55,11 +55,7 @@ func runHealthCheck(cfg *config.Config) error {
|
||||
ctx := context.Background()
|
||||
result := compositeChecker.CheckWithVersion(ctx, version.GetVersion())
|
||||
|
||||
if IsJSONOutput() {
|
||||
return outputJSON(result)
|
||||
}
|
||||
|
||||
return printHealthResult(result)
|
||||
}
|
||||
|
||||
func createDatabaseChecker(cfg *config.Config) (health.Checker, error) {
|
||||
@@ -96,34 +92,3 @@ func createSMTPChecker(cfg *config.Config) health.Checker {
|
||||
|
||||
return health.NewSMTPChecker(smtpConfig)
|
||||
}
|
||||
|
||||
func printHealthResult(result health.OverallResult) error {
|
||||
fmt.Printf("Health Status: %s\n", result.Status)
|
||||
fmt.Printf("Version: %s\n", result.Version)
|
||||
fmt.Printf("Timestamp: %s\n", result.Timestamp.Format(time.RFC3339))
|
||||
fmt.Println()
|
||||
|
||||
if len(result.Services) == 0 {
|
||||
fmt.Println("No services configured.")
|
||||
return nil
|
||||
}
|
||||
|
||||
fmt.Println("Services:")
|
||||
for name, service := range result.Services {
|
||||
fmt.Printf(" %s:\n", name)
|
||||
fmt.Printf(" Status: %s\n", service.Status)
|
||||
fmt.Printf(" Latency: %s\n", service.Latency)
|
||||
if service.Message != "" {
|
||||
fmt.Printf(" Message: %s\n", service.Message)
|
||||
}
|
||||
if len(service.Details) > 0 {
|
||||
fmt.Printf(" Details:\n")
|
||||
for key, value := range service.Details {
|
||||
fmt.Printf(" %s: %v\n", key, value)
|
||||
}
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -4,10 +4,8 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"goyco/internal/config"
|
||||
"goyco/internal/health"
|
||||
"goyco/internal/testutils"
|
||||
)
|
||||
|
||||
@@ -89,117 +87,3 @@ func TestCreateSMTPChecker(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestPrintHealthResult(t *testing.T) {
|
||||
t.Run("healthy result", func(t *testing.T) {
|
||||
result := health.OverallResult{
|
||||
Status: health.StatusHealthy,
|
||||
Version: "v1.0.0",
|
||||
Timestamp: time.Now().UTC(),
|
||||
Services: map[string]health.Result{
|
||||
"database": {
|
||||
Status: health.StatusHealthy,
|
||||
Latency: 2 * time.Millisecond,
|
||||
Details: map[string]any{
|
||||
"ping_time": "2ms",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
oldStdout := os.Stdout
|
||||
r, w, _ := os.Pipe()
|
||||
os.Stdout = w
|
||||
|
||||
err := printHealthResult(result)
|
||||
|
||||
w.Close()
|
||||
os.Stdout = oldStdout
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
buf := make([]byte, 1024)
|
||||
n, _ := r.Read(buf)
|
||||
output := string(buf[:n])
|
||||
|
||||
if !strings.Contains(output, "Health Status: healthy") {
|
||||
t.Errorf("expected 'Health Status: healthy', got %q", output)
|
||||
}
|
||||
|
||||
if !strings.Contains(output, "Version: v1.0.0") {
|
||||
t.Errorf("expected 'Version: v1.0.0', got %q", output)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("degraded result with message", func(t *testing.T) {
|
||||
result := health.OverallResult{
|
||||
Status: health.StatusDegraded,
|
||||
Version: "v1.0.0",
|
||||
Timestamp: time.Now().UTC(),
|
||||
Services: map[string]health.Result{
|
||||
"smtp": {
|
||||
Status: health.StatusDegraded,
|
||||
Message: "Connection failed",
|
||||
Latency: 5 * time.Millisecond,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
oldStdout := os.Stdout
|
||||
r, w, _ := os.Pipe()
|
||||
os.Stdout = w
|
||||
|
||||
err := printHealthResult(result)
|
||||
|
||||
w.Close()
|
||||
os.Stdout = oldStdout
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
buf := make([]byte, 1024)
|
||||
n, _ := r.Read(buf)
|
||||
output := string(buf[:n])
|
||||
|
||||
if !strings.Contains(output, "Health Status: degraded") {
|
||||
t.Errorf("expected 'Health Status: degraded', got %q", output)
|
||||
}
|
||||
|
||||
if !strings.Contains(output, "Connection failed") {
|
||||
t.Errorf("expected error message in output, got %q", output)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("empty services", func(t *testing.T) {
|
||||
result := health.OverallResult{
|
||||
Status: health.StatusHealthy,
|
||||
Version: "v1.0.0",
|
||||
Timestamp: time.Now().UTC(),
|
||||
Services: map[string]health.Result{},
|
||||
}
|
||||
|
||||
oldStdout := os.Stdout
|
||||
r, w, _ := os.Pipe()
|
||||
os.Stdout = w
|
||||
|
||||
err := printHealthResult(result)
|
||||
|
||||
w.Close()
|
||||
os.Stdout = oldStdout
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
buf := make([]byte, 1024)
|
||||
n, _ := r.Read(buf)
|
||||
output := string(buf[:n])
|
||||
|
||||
if !strings.Contains(output, "No services configured") {
|
||||
t.Errorf("expected 'No services configured', got %q", output)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user