feat: proxy PreZero priority endpoints to FastAPI
This commit is contained in:
1 parent
a0ad60ec53
commit
7e9c2d9663
5 files changed
+564
-11
No files matched your search
@@ -11,6 +11,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -176,6 +177,28 @@ func (c *Client) TrainStatus(ctx context.Context, jobID string) (int, []byte, er
|
||||
return c.controlRequest(ctx, http.MethodGet, "/train/"+url.PathEscape(jobID), "", nil)
|
||||
}
|
||||
|
||||
// PreZeroSummary proxies GET /prezero/summary.
|
||||
func (c *Client) PreZeroSummary(ctx context.Context) (int, []byte, error) {
|
||||
return c.controlRequest(ctx, http.MethodGet, "/prezero/summary", "", nil)
|
||||
}
|
||||
|
||||
// PreZeroPriority proxies GET /prezero/priority. priority may be empty to
|
||||
// return every tier; limit and offset are always forwarded.
|
||||
func (c *Client) PreZeroPriority(ctx context.Context, priority string, limit, offset int) (int, []byte, error) {
|
||||
query := url.Values{}
|
||||
if priority != "" {
|
||||
query.Set("priority", priority)
|
||||
}
|
||||
query.Set("limit", strconv.Itoa(limit))
|
||||
query.Set("offset", strconv.Itoa(offset))
|
||||
return c.controlRequest(ctx, http.MethodGet, "/prezero/priority?"+query.Encode(), "", nil)
|
||||
}
|
||||
|
||||
// PreZeroContainer proxies GET /prezero/containers/{location_id}.
|
||||
func (c *Client) PreZeroContainer(ctx context.Context, locationID string) (int, []byte, error) {
|
||||
return c.controlRequest(ctx, http.MethodGet, "/prezero/containers/"+url.PathEscape(locationID), "", nil)
|
||||
}
|
||||
|
||||
// controlRequest performs a training/control request and returns the raw
|
||||
// upstream status and body. Non-2xx responses are not errors: the proxy
|
||||
// forwards them so clients see FastAPI's 400/404/409 semantics.
|
||||
|
||||
@@ -305,3 +305,71 @@ func TestTrainStatusForwardsJobIDAndUpstreamError(t *testing.T) {
|
||||
t.Fatalf("status=%d body=%s, want forwarded 404", status, body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreZeroClientsForwardRouteQueryAndStatus(t *testing.T) {
|
||||
var gotPaths, gotQueries []string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
t.Errorf("method=%s, want GET", r.Method)
|
||||
}
|
||||
gotPaths = append(gotPaths, r.URL.Path)
|
||||
gotQueries = append(gotQueries, r.URL.RawQuery)
|
||||
switch {
|
||||
case r.URL.Path == "/prezero/summary":
|
||||
_, _ = w.Write([]byte(`{"summary":{"priority_counts":{"high":88}}}`))
|
||||
case r.URL.Path == "/prezero/priority":
|
||||
_, _ = w.Write([]byte(`{"total":88,"count":1,"containers":[]}`))
|
||||
case r.URL.Path == "/prezero/containers/known-id":
|
||||
_, _ = w.Write([]byte(`{"container":{"location_id":"known-id"}}`))
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
_, _ = w.Write([]byte(`{"error":{"code":"not_found","message":"container not found"}}`))
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
client := New(srv.URL, time.Second)
|
||||
status, body, err := client.PreZeroSummary(context.Background())
|
||||
if err != nil || status != http.StatusOK || !strings.Contains(string(body), `"high":88`) {
|
||||
t.Fatalf("summary status=%d body=%s err=%v", status, body, err)
|
||||
}
|
||||
|
||||
status, body, err = client.PreZeroPriority(context.Background(), "high", 3, 10)
|
||||
if err != nil || status != http.StatusOK || !strings.Contains(string(body), `"total":88`) {
|
||||
t.Fatalf("priority status=%d body=%s err=%v", status, body, err)
|
||||
}
|
||||
if _, _, err := client.PreZeroPriority(context.Background(), "", 50, 0); err != nil {
|
||||
t.Fatalf("priority defaults: %v", err)
|
||||
}
|
||||
|
||||
status, body, err = client.PreZeroContainer(context.Background(), "known-id")
|
||||
if err != nil || status != http.StatusOK || !strings.Contains(string(body), "known-id") {
|
||||
t.Fatalf("container status=%d body=%s err=%v", status, body, err)
|
||||
}
|
||||
status, body, err = client.PreZeroContainer(context.Background(), "missing")
|
||||
if err != nil {
|
||||
t.Fatalf("container missing: %v", err)
|
||||
}
|
||||
if status != http.StatusNotFound || !strings.Contains(string(body), "container not found") {
|
||||
t.Fatalf("container missing status=%d body=%s, want forwarded 404", status, body)
|
||||
}
|
||||
|
||||
wantPaths := []string{
|
||||
"/prezero/summary",
|
||||
"/prezero/priority",
|
||||
"/prezero/priority",
|
||||
"/prezero/containers/known-id",
|
||||
"/prezero/containers/missing",
|
||||
}
|
||||
for i, want := range wantPaths {
|
||||
if gotPaths[i] != want {
|
||||
t.Errorf("request %d path=%q, want %q", i, gotPaths[i], want)
|
||||
}
|
||||
}
|
||||
wantQueries := []string{"", "limit=3&offset=10&priority=high", "limit=50&offset=0", "", ""}
|
||||
for i, want := range wantQueries {
|
||||
if gotQueries[i] != want {
|
||||
t.Errorf("request %d query=%q, want %q", i, gotQueries[i], want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user