Initial phase 1 baseline implementation
This commit is contained in:
parent
099c53badf
commit
077cf7601a
29 changed files with 1339 additions and 39 deletions
|
|
@ -1,6 +1,8 @@
|
|||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
|
@ -8,16 +10,31 @@ import (
|
|||
"strings"
|
||||
"testing"
|
||||
|
||||
"drive.local/drivev2/internal/application"
|
||||
"drive.local/drivev2/internal/config"
|
||||
)
|
||||
|
||||
type systemServiceStub struct {
|
||||
readinessErr error
|
||||
setupStatus application.SetupStatus
|
||||
setupErr error
|
||||
}
|
||||
|
||||
func (s systemServiceStub) Readiness(context.Context) error {
|
||||
return s.readinessErr
|
||||
}
|
||||
|
||||
func (s systemServiceStub) SetupStatus(context.Context) (application.SetupStatus, error) {
|
||||
return s.setupStatus, s.setupErr
|
||||
}
|
||||
|
||||
func TestLiveHealth(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
server := NewServer(
|
||||
config.Config{HTTPAddress: ":0", WebRoot: t.TempDir()},
|
||||
"test",
|
||||
slog.New(slog.NewTextHandler(io.Discard, nil)),
|
||||
systemServiceStub{},
|
||||
http.NotFoundHandler(),
|
||||
)
|
||||
request := httptest.NewRequest(http.MethodGet, "/health/live", nil)
|
||||
|
|
@ -32,3 +49,50 @@ func TestLiveHealth(t *testing.T) {
|
|||
t.Fatalf("unexpected body: %s", recorder.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadinessReportsDependencyFailure(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
server := NewServer(
|
||||
config.Config{HTTPAddress: ":0", WebRoot: t.TempDir()},
|
||||
slog.New(slog.NewTextHandler(io.Discard, nil)),
|
||||
systemServiceStub{readinessErr: errors.New("database unavailable")},
|
||||
http.NotFoundHandler(),
|
||||
)
|
||||
request := httptest.NewRequest(http.MethodGet, "/health/ready", nil)
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
server.httpServer.Handler.ServeHTTP(recorder, request)
|
||||
|
||||
if recorder.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("status = %d, want %d", recorder.Code, http.StatusServiceUnavailable)
|
||||
}
|
||||
if contentType := recorder.Header().Get("Content-Type"); contentType != "application/problem+json" {
|
||||
t.Fatalf("Content-Type = %q, want application/problem+json", contentType)
|
||||
}
|
||||
if !strings.Contains(recorder.Body.String(), `"code":"dependency_unavailable"`) {
|
||||
t.Fatalf("unexpected body: %s", recorder.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetupStatusUsesApplicationService(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
server := NewServer(
|
||||
config.Config{HTTPAddress: ":0", WebRoot: t.TempDir()},
|
||||
slog.New(slog.NewTextHandler(io.Discard, nil)),
|
||||
systemServiceStub{setupStatus: application.SetupStatus{Initialized: true, Phase: "phase-1", Version: "test"}},
|
||||
http.NotFoundHandler(),
|
||||
)
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/setup/status", nil)
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
server.httpServer.Handler.ServeHTTP(recorder, request)
|
||||
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d", recorder.Code, http.StatusOK)
|
||||
}
|
||||
if !strings.Contains(recorder.Body.String(), `"initialized":true`) {
|
||||
t.Fatalf("unexpected body: %s", recorder.Body.String())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue