286 lines
10 KiB
Go
286 lines
10 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"pulsecast/api/internal/config"
|
|
"pulsecast/api/internal/ml"
|
|
)
|
|
|
|
func newTrainServer(t *testing.T, fastapiURL string) *httptest.Server {
|
|
t.Helper()
|
|
_, ts := newTestServer(t, ml.New(fastapiURL, 500*time.Millisecond), func(cfg *config.Config) {
|
|
cfg.FastAPIURL = fastapiURL
|
|
cfg.ControlUser = "control"
|
|
cfg.ControlPass = "secret"
|
|
})
|
|
return ts
|
|
}
|
|
|
|
func controlRequest(t *testing.T, method, url, contentType string, body io.Reader, user, pass string) (*http.Response, []byte) {
|
|
t.Helper()
|
|
req, err := http.NewRequest(method, url, body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if contentType != "" {
|
|
req.Header.Set("Content-Type", contentType)
|
|
}
|
|
if user != "" {
|
|
req.SetBasicAuth(user, pass)
|
|
}
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
t.Fatalf("%s %s: %v", method, url, err)
|
|
}
|
|
defer resp.Body.Close()
|
|
raw, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
t.Fatalf("read %s %s: %v", method, url, err)
|
|
}
|
|
return resp, raw
|
|
}
|
|
|
|
func TestTrainJSONProxyAndControlAuth(t *testing.T) {
|
|
var calls atomic.Int32
|
|
var gotBody []byte
|
|
fastapi := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost || r.URL.Path != "/train" {
|
|
http.Error(w, "unexpected", http.StatusNotFound)
|
|
return
|
|
}
|
|
calls.Add(1)
|
|
if ct := r.Header.Get("Content-Type"); ct != "application/json" {
|
|
t.Errorf("FastAPI Content-Type=%q, want application/json", ct)
|
|
}
|
|
gotBody, _ = io.ReadAll(r.Body)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusAccepted)
|
|
_, _ = w.Write([]byte(`{"job_id":"job-1","status":"queued","model":"small","submitted_at":"2026-11-14T09:00:00Z"}`))
|
|
}))
|
|
defer fastapi.Close()
|
|
ts := newTrainServer(t, fastapi.URL)
|
|
|
|
body := `{"path":"/tmp/new.csv"}`
|
|
if resp, _ := controlRequest(t, http.MethodPost, ts.URL+"/api/train", "application/json", strings.NewReader(body), "", ""); resp.StatusCode != http.StatusUnauthorized {
|
|
t.Fatalf("no auth status=%d, want 401", resp.StatusCode)
|
|
} else if resp.Header.Get("WWW-Authenticate") == "" {
|
|
t.Error("missing WWW-Authenticate header")
|
|
}
|
|
if resp, _ := controlRequest(t, http.MethodPost, ts.URL+"/api/train", "application/json", strings.NewReader(body), "control", "wrong"); resp.StatusCode != http.StatusUnauthorized {
|
|
t.Fatalf("bad auth status=%d, want 401", resp.StatusCode)
|
|
}
|
|
if got := calls.Load(); got != 0 {
|
|
t.Fatalf("FastAPI calls=%d before auth succeeded, want 0", got)
|
|
}
|
|
|
|
resp, raw := controlRequest(t, http.MethodPost, ts.URL+"/api/train", "application/json", strings.NewReader(body), "control", "secret")
|
|
if resp.StatusCode != http.StatusAccepted {
|
|
t.Fatalf("train status=%d body=%s, want 202", resp.StatusCode, raw)
|
|
}
|
|
var job map[string]any
|
|
if err := json.Unmarshal(raw, &job); err != nil {
|
|
t.Fatalf("decode train response: %v", err)
|
|
}
|
|
if job["job_id"] != "job-1" || job["status"] != "queued" {
|
|
t.Fatalf("train response=%v", job)
|
|
}
|
|
|
|
var forwarded ml.TrainRequest
|
|
if err := json.Unmarshal(gotBody, &forwarded); err != nil {
|
|
t.Fatalf("decode forwarded body: %v", err)
|
|
}
|
|
if forwarded.Path != "/tmp/new.csv" || forwarded.Model != "small" {
|
|
t.Fatalf("forwarded body=%+v, want path and default model=small", forwarded)
|
|
}
|
|
|
|
if resp, _ := controlRequest(t, http.MethodGet, ts.URL+"/api/train/job-1", "", nil, "", ""); resp.StatusCode != http.StatusUnauthorized {
|
|
t.Fatalf("status without auth=%d, want 401", resp.StatusCode)
|
|
}
|
|
}
|
|
|
|
func TestTrainStatusProxy(t *testing.T) {
|
|
fastapi := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
switch r.URL.Path {
|
|
case "/train/job-1":
|
|
if r.Method != http.MethodGet {
|
|
t.Errorf("method=%s, want GET", r.Method)
|
|
}
|
|
_, _ = w.Write([]byte(`{"job_id":"job-1","status":"succeeded","model":"forecast","metrics":{"accuracy":0.9},"model_version":"v2","error":null}`))
|
|
default:
|
|
w.WriteHeader(http.StatusNotFound)
|
|
_, _ = w.Write([]byte(`{"detail":"job not found"}`))
|
|
}
|
|
}))
|
|
defer fastapi.Close()
|
|
ts := newTrainServer(t, fastapi.URL)
|
|
|
|
resp, raw := controlRequest(t, http.MethodGet, ts.URL+"/api/train/job-1", "", nil, "control", "secret")
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("status=%d body=%s, want 200", resp.StatusCode, raw)
|
|
}
|
|
var job struct {
|
|
JobID string `json:"job_id"`
|
|
Status string `json:"status"`
|
|
Metrics map[string]any `json:"metrics"`
|
|
Error any `json:"error"`
|
|
}
|
|
if err := json.Unmarshal(raw, &job); err != nil {
|
|
t.Fatalf("decode status response: %v", err)
|
|
}
|
|
if job.JobID != "job-1" || job.Status != "succeeded" || job.Metrics["accuracy"] != 0.9 {
|
|
t.Fatalf("status response=%+v", job)
|
|
}
|
|
|
|
resp, raw = controlRequest(t, http.MethodGet, ts.URL+"/api/train/missing", "", nil, "control", "secret")
|
|
if resp.StatusCode != http.StatusNotFound {
|
|
t.Fatalf("unknown job status=%d body=%s, want 404", resp.StatusCode, raw)
|
|
}
|
|
if !json.Valid(raw) {
|
|
t.Fatalf("unknown job body is not JSON: %s", raw)
|
|
}
|
|
}
|
|
|
|
func TestTrainMultipartProxy(t *testing.T) {
|
|
var gotFile, gotModel string
|
|
fastapi := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost || r.URL.Path != "/train" {
|
|
http.Error(w, "unexpected", http.StatusNotFound)
|
|
return
|
|
}
|
|
if !strings.HasPrefix(r.Header.Get("Content-Type"), "multipart/form-data; boundary=") {
|
|
t.Errorf("Content-Type=%q, want multipart/form-data", r.Header.Get("Content-Type"))
|
|
}
|
|
if err := r.ParseMultipartForm(1 << 20); err != nil {
|
|
t.Errorf("parse multipart: %v", err)
|
|
http.Error(w, "bad multipart", http.StatusBadRequest)
|
|
return
|
|
}
|
|
gotModel = r.FormValue("model")
|
|
file, _, err := r.FormFile("file")
|
|
if err != nil {
|
|
t.Errorf("form file: %v", err)
|
|
} else {
|
|
raw, _ := io.ReadAll(file)
|
|
file.Close()
|
|
gotFile = string(raw)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusAccepted)
|
|
_, _ = w.Write([]byte(`{"job_id":"job-2","status":"queued","model":"forecast","submitted_at":"2026-11-14T09:00:00Z"}`))
|
|
}))
|
|
defer fastapi.Close()
|
|
ts := newTrainServer(t, fastapi.URL)
|
|
|
|
var buf bytes.Buffer
|
|
mw := multipart.NewWriter(&buf)
|
|
part, err := mw.CreateFormFile("file", "new-data.csv")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := part.Write([]byte("timestamp,count\n2026-11-01T10:00:00Z,42\n")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := mw.WriteField("model", "forecast"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := mw.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
resp, raw := controlRequest(t, http.MethodPost, ts.URL+"/api/train", mw.FormDataContentType(), &buf, "control", "secret")
|
|
if resp.StatusCode != http.StatusAccepted {
|
|
t.Fatalf("multipart status=%d body=%s, want 202", resp.StatusCode, raw)
|
|
}
|
|
if gotModel != "forecast" {
|
|
t.Errorf("forwarded model=%q, want forecast", gotModel)
|
|
}
|
|
if !strings.Contains(gotFile, "42") {
|
|
t.Errorf("forwarded file=%q, want CSV content", gotFile)
|
|
}
|
|
}
|
|
|
|
func TestTrainValidationAndUpstreamErrors(t *testing.T) {
|
|
var calls atomic.Int32
|
|
fastapi := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
calls.Add(1)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if r.Method == http.MethodPost {
|
|
w.WriteHeader(http.StatusConflict)
|
|
_, _ = w.Write([]byte(`{"detail":"a training job is already running"}`))
|
|
return
|
|
}
|
|
http.NotFound(w, r)
|
|
}))
|
|
defer fastapi.Close()
|
|
ts := newTrainServer(t, fastapi.URL)
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
contentType string
|
|
body string
|
|
}{
|
|
{"unknown model", "application/json", `{"path":"/tmp/x.csv","model":"huge"}`},
|
|
{"missing path", "application/json", `{"model":"small"}`},
|
|
{"malformed json", "application/json", `{`},
|
|
{"wrong content type", "text/plain", `path=/tmp/x.csv`},
|
|
} {
|
|
resp, raw := controlRequest(t, http.MethodPost, ts.URL+"/api/train", tc.contentType, strings.NewReader(tc.body), "control", "secret")
|
|
if resp.StatusCode != http.StatusBadRequest {
|
|
t.Errorf("%s: status=%d body=%s, want 400", tc.name, resp.StatusCode, raw)
|
|
}
|
|
}
|
|
if got := calls.Load(); got != 0 {
|
|
t.Fatalf("FastAPI calls=%d for invalid requests, want 0", got)
|
|
}
|
|
|
|
resp, raw := controlRequest(t, http.MethodPost, ts.URL+"/api/train", "application/json", strings.NewReader(`{"path":"/tmp/x.csv","model":"forecast"}`), "control", "secret")
|
|
if resp.StatusCode != http.StatusConflict {
|
|
t.Fatalf("busy status=%d body=%s, want upstream 409", resp.StatusCode, raw)
|
|
}
|
|
|
|
if resp, _ := controlRequest(t, http.MethodPost, ts.URL+"/api/train/job-1", "application/json", strings.NewReader(`{}`), "control", "secret"); resp.StatusCode != http.StatusMethodNotAllowed {
|
|
t.Errorf("POST job status=%d, want 405", resp.StatusCode)
|
|
} else if allow := resp.Header.Get("Allow"); allow != "GET" {
|
|
t.Errorf("Allow=%q, want GET", allow)
|
|
}
|
|
if resp, _ := controlRequest(t, http.MethodGet, ts.URL+"/api/train", "", nil, "control", "secret"); resp.StatusCode != http.StatusMethodNotAllowed {
|
|
t.Errorf("GET train status=%d, want 405", resp.StatusCode)
|
|
}
|
|
if resp, _ := controlRequest(t, http.MethodGet, ts.URL+"/api/train/a/b", "", nil, "control", "secret"); resp.StatusCode != http.StatusNotFound {
|
|
t.Errorf("nested job path status=%d, want 404", resp.StatusCode)
|
|
}
|
|
}
|
|
|
|
func TestTrainFastAPIDownAndAuthUnconfigured(t *testing.T) {
|
|
_, down := newTestServer(t, ml.New("http://127.0.0.1:1", 50*time.Millisecond), func(cfg *config.Config) {
|
|
cfg.FastAPIURL = "http://127.0.0.1:1"
|
|
cfg.ControlUser = "control"
|
|
cfg.ControlPass = "secret"
|
|
})
|
|
resp, raw := controlRequest(t, http.MethodPost, down.URL+"/api/train", "application/json", strings.NewReader(`{"path":"/tmp/x.csv"}`), "control", "secret")
|
|
if resp.StatusCode != http.StatusBadGateway {
|
|
t.Fatalf("train with FastAPI down status=%d body=%s, want 502", resp.StatusCode, raw)
|
|
}
|
|
resp, raw = controlRequest(t, http.MethodGet, down.URL+"/api/train/job-1", "", nil, "control", "secret")
|
|
if resp.StatusCode != http.StatusBadGateway {
|
|
t.Fatalf("status with FastAPI down=%d body=%s, want 502", resp.StatusCode, raw)
|
|
}
|
|
|
|
_, unconfigured := newTestServer(t, &stubML{}, nil)
|
|
resp, raw = controlRequest(t, http.MethodPost, unconfigured.URL+"/api/train", "application/json", strings.NewReader(`{"path":"/tmp/x.csv"}`), "control", "secret")
|
|
if resp.StatusCode != http.StatusServiceUnavailable {
|
|
t.Fatalf("train without control env status=%d body=%s, want 503", resp.StatusCode, raw)
|
|
}
|
|
}
|