|
| 1 | +//go:build e2e |
| 2 | + |
| 3 | +package e2e |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/docker/model-runner/cmd/cli/desktop" |
| 12 | +) |
| 13 | + |
| 14 | +func TestE2E_APIErrors(t *testing.T) { |
| 15 | + t.Run("InvalidModel", func(t *testing.T) { |
| 16 | + status, body := doJSON(t, http.MethodPost, serverURL+"/engines/v1/chat/completions", |
| 17 | + desktop.OpenAIChatRequest{ |
| 18 | + Model: "nonexistent/model:latest", |
| 19 | + Messages: []desktop.OpenAIChatMessage{{Role: "user", Content: "hi"}}, |
| 20 | + }) |
| 21 | + if status == http.StatusOK { |
| 22 | + t.Fatalf("expected error for invalid model, got 200: %s", body) |
| 23 | + } |
| 24 | + t.Logf("invalid model: status=%d", status) |
| 25 | + }) |
| 26 | + |
| 27 | + t.Run("MalformedJSON", func(t *testing.T) { |
| 28 | + req, _ := http.NewRequestWithContext(t.Context(), http.MethodPost, |
| 29 | + serverURL+"/engines/v1/chat/completions", |
| 30 | + strings.NewReader(`{bad json`)) |
| 31 | + req.Header.Set("Content-Type", "application/json") |
| 32 | + resp, err := http.DefaultClient.Do(req) |
| 33 | + if err != nil { |
| 34 | + t.Fatalf("request failed: %v", err) |
| 35 | + } |
| 36 | + resp.Body.Close() |
| 37 | + if resp.StatusCode == http.StatusOK { |
| 38 | + t.Fatal("expected error for malformed JSON") |
| 39 | + } |
| 40 | + t.Logf("malformed JSON: status=%d", resp.StatusCode) |
| 41 | + }) |
| 42 | + |
| 43 | + t.Run("EmptyMessages", func(t *testing.T) { |
| 44 | + status, body := doJSON(t, http.MethodPost, serverURL+"/engines/v1/chat/completions", |
| 45 | + desktop.OpenAIChatRequest{ |
| 46 | + Model: ggufModel, |
| 47 | + Messages: []desktop.OpenAIChatMessage{}, |
| 48 | + }) |
| 49 | + // Some backends accept empty messages, some don't — just verify no 500. |
| 50 | + if status == http.StatusInternalServerError { |
| 51 | + t.Fatalf("unexpected 500 for empty messages: %s", body) |
| 52 | + } |
| 53 | + t.Logf("empty messages: status=%d", status) |
| 54 | + }) |
| 55 | +} |
| 56 | + |
| 57 | +func TestE2E_ModelLifecycle(t *testing.T) { |
| 58 | + model := ggufModel |
| 59 | + |
| 60 | + t.Run("PullIdempotent", func(t *testing.T) { |
| 61 | + pullModel(t, model) |
| 62 | + output := pullModel(t, model) |
| 63 | + if !strings.Contains(output, "Using cached model") { |
| 64 | + t.Errorf("expected 'Using cached model' in second pull, got: %s", output) |
| 65 | + } |
| 66 | + }) |
| 67 | + |
| 68 | + t.Run("InspectModel", func(t *testing.T) { |
| 69 | + status, body := doJSON(t, http.MethodGet, |
| 70 | + serverURL+"/engines/v1/models/"+model, nil) |
| 71 | + if status != http.StatusOK { |
| 72 | + t.Fatalf("inspect: status=%d body=%s", status, body) |
| 73 | + } |
| 74 | + var m struct { |
| 75 | + ID string `json:"id"` |
| 76 | + } |
| 77 | + if err := json.Unmarshal(body, &m); err != nil { |
| 78 | + t.Fatalf("decode: %v", err) |
| 79 | + } |
| 80 | + if m.ID == "" { |
| 81 | + t.Fatal("empty model ID in inspect response") |
| 82 | + } |
| 83 | + t.Logf("inspect: id=%s", m.ID) |
| 84 | + }) |
| 85 | + |
| 86 | + t.Run("OllamaShow", func(t *testing.T) { |
| 87 | + status, body := doJSON(t, http.MethodPost, serverURL+"/api/show", |
| 88 | + map[string]string{"name": model}) |
| 89 | + if status != http.StatusOK { |
| 90 | + t.Fatalf("show: status=%d body=%s", status, body) |
| 91 | + } |
| 92 | + var show struct { |
| 93 | + Details struct { |
| 94 | + Format string `json:"format"` |
| 95 | + } `json:"details"` |
| 96 | + } |
| 97 | + if err := json.Unmarshal(body, &show); err != nil { |
| 98 | + t.Fatalf("decode: %v", err) |
| 99 | + } |
| 100 | + t.Logf("format: %s", show.Details.Format) |
| 101 | + }) |
| 102 | + |
| 103 | + t.Run("Remove", func(t *testing.T) { |
| 104 | + removeModel(t, model) |
| 105 | + }) |
| 106 | +} |
0 commit comments