package client_test import ( "context" "errors" "net/http" "net/http/httptest" "slices" "strings" "testing" "time" "gitea.mynodes.xyz/ilker/llmgw/terraform-provider-llmgw/internal/client" "gitea.mynodes.xyz/ilker/llmgw/terraform-provider-llmgw/internal/fakeapi" ) func serve(t *testing.T) (*fakeapi.API, *client.Client) { t.Helper() api := fakeapi.New("secret", client.NodeView{ID: "gpu01", State: "ready", GPUTotal: 8, GPUFree: 6}) srv := httptest.NewServer(api) t.Cleanup(srv.Close) c, err := client.New(srv.URL, "secret", "terraform") if err != nil { t.Fatal(err) } return api, c } func TestAModelIsAddedReadScaledAndRemoved(t *testing.T) { api, c := serve(t) ctx := context.Background() err := c.AddModel(ctx, client.Model{Name: "qwen", Source: "/m/qwen.gguf", Profiles: []client.Profile{{Name: "cpu", Engine: "llamacpp", MinMemoryGB: 4}}, Scaling: client.Scaling{Min: 1}}) if err != nil { t.Fatal(err) } m, err := c.Model(ctx, "qwen") if err != nil { t.Fatal(err) } // As the catalog filled it in. if m.Scaling.Max != 1 || m.Profiles[0].TP != 1 || m.Profiles[0].StartupTimeout != 20*time.Minute { t.Errorf("read back %+v", m) } if err := c.Scale(ctx, "qwen", client.ScaleRequest{Min: 0, Max: 2, IdleTimeout: 30 * time.Minute}); err != nil { t.Fatal(err) } if m, _ := api.Model("qwen"); m.Scaling != (client.Scaling{Min: 0, Max: 2, IdleTimeout: 30 * time.Minute}) { t.Errorf("scaled to %+v", m.Scaling) } if err := c.RemoveModel(ctx, "qwen"); err != nil { t.Fatal(err) } if _, err := c.Model(ctx, "qwen"); !client.IsNotFound(err) { t.Errorf("a removed model read as %v", err) } } func TestEveryChangeSaysItIsJSONAndWhoMadeIt(t *testing.T) { // The admin listener refuses a change without application/json, a DELETE // with no body included; and the audit log names whoever the change says // it is from. api, c := serve(t) ctx := context.Background() if err := c.AddModel(ctx, client.Model{Name: "m", Source: "/m"}); err != nil { t.Fatal(err) } if err := c.RemoveModel(ctx, "m"); err != nil { t.Fatalf("a DELETE with no body: %v", err) } if got := api.Actors(); !slices.Equal(got, []string{"terraform", "terraform"}) { t.Errorf("actors %v", got) } } func TestARefusalCarriesWhatTheAPISaid(t *testing.T) { _, c := serve(t) ctx := context.Background() _, err := c.SetQuota(ctx, client.QuotaRequest{Scope: "team:Nobody/x", Limit: client.Resources{GPUs: ptr(1)}}) var e *client.Error if err == nil || !strings.Contains(err.Error(), `there is no team "Nobody/x"`) { t.Fatalf("got %v", err) } if !errors.As(err, &e) || e.Status != http.StatusBadRequest { t.Errorf("not a 400: %#v", err) } } func TestAWrongTokenIsRefused(t *testing.T) { api := fakeapi.New("secret") srv := httptest.NewServer(api) defer srv.Close() c, err := client.New(srv.URL, "wrong", "") if err != nil { t.Fatal(err) } var e *client.Error if _, err := c.Models(context.Background()); !errors.As(err, &e) || e.Status != http.StatusUnauthorized { t.Errorf("got %v", err) } } func TestANameWithASlashIsOneSegment(t *testing.T) { // A quota's scope is named by path, Acme/ml, and must not become two // segments of the URL on its way to DELETE /api/v1/quotas/{scope}. api, c := serve(t) ctx := context.Background() id, err := c.SetQuota(ctx, client.QuotaRequest{Scope: "team:Acme/ml", Guarantee: client.Resources{GPUs: ptr(2)}}) if err != nil || id != "team:t1" { t.Fatalf("set %q, %v", id, err) } if err := c.ClearQuota(ctx, "team:Acme/ml"); err != nil { t.Fatal(err) } if _, ok := api.Quota("team:t1"); ok { t.Error("the quota is still there") } } func TestAPolicyIsAttachedByPathAndDetachedByID(t *testing.T) { api, c := serve(t) ctx := context.Background() a, err := c.Attach(ctx, "team:Acme/ml", "finance") if err != nil { t.Fatal(err) } if a != (client.Attachment{Scope: "team", ScopeID: "t1", Policy: "finance"}) { t.Errorf("attached %+v", a) } g, err := c.Guard(ctx) if err != nil || len(g.Attachments) != 1 || g.Attachments[0] != a { t.Fatalf("guard %+v, %v", g, err) } if err := c.Detach(ctx, "team:t1", "finance"); err != nil { t.Fatal(err) } if len(api.Attachments()) != 0 { t.Error("still attached") } if err := c.Detach(ctx, "team:t1", "finance"); !client.IsNotFound(err) { t.Errorf("detaching twice: %v", err) } } func TestTheNodesAreListed(t *testing.T) { _, c := serve(t) ns, err := c.Nodes(context.Background()) if err != nil || len(ns) != 1 || ns[0].ID != "gpu01" || ns[0].GPUFree != 6 { t.Errorf("nodes %+v, %v", ns, err) } } func TestAnEndpointNotListedIsNotCalled(t *testing.T) { // Endpoints is what the contract test checks against the API's document; // a call outside it would be one nothing checks. if slices.Contains(client.Endpoints, "POST /api/v1/restart") { t.Skip("restart is listed now") } if !strings.Contains(client.CallUnlisted(), "add it to Endpoints") { t.Error("an unlisted endpoint was called") } } func TestABadEndpointOrNoTokenIsRefusedUpFront(t *testing.T) { if _, err := client.New("127.0.0.1:9090", "t", ""); err == nil { t.Error("an address without a scheme was taken") } if _, err := client.New("http://127.0.0.1:9090", "", ""); err == nil { t.Error("no token was taken") } } func ptr(n int64) *int64 { return &n }