-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_models_test.go
More file actions
179 lines (148 loc) · 4.35 KB
/
Copy pathhandler_models_test.go
File metadata and controls
179 lines (148 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"openwebui-ollama-proxy/ollama"
)
// // // // // // // // // //
func TestBuildShowResponse(t *testing.T) {
resp := buildShowResponse("llama3:8b")
if resp.Name != "llama3:8b" {
t.Fatalf("Name = %q", resp.Name)
}
if resp.Model != "llama3:8b" {
t.Fatalf("Model = %q", resp.Model)
}
if resp.Details.Format != "proxy" {
t.Fatalf("Format = %q", resp.Details.Format)
}
if resp.Details.Family != "unknown" {
t.Fatalf("Family = %q", resp.Details.Family)
}
if resp.Template != "{{ .Prompt }}" {
t.Fatalf("Template = %q", resp.Template)
}
if resp.Modelfile != "FROM llama3:8b" {
t.Fatalf("Modelfile = %q", resp.Modelfile)
}
if resp.Digest != "proxy-llama3:8b" {
t.Fatalf("Digest = %q", resp.Digest)
}
if resp.ModifiedAt == "" {
t.Fatal("ModifiedAt is empty")
}
}
// // // //
func TestHandlePs(t *testing.T) {
srv := &Server{mux: http.NewServeMux()}
srv.mux.HandleFunc("GET /api/ps", srv.handlePs)
req := httptest.NewRequest(http.MethodGet, "/api/ps", nil)
w := httptest.NewRecorder()
srv.mux.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status = %d", w.Code)
}
var resp ollama.PsResponse
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("decode: %v", err)
}
if len(resp.Models) != 0 {
t.Fatalf("models = %d, want 0", len(resp.Models))
}
}
func TestHandleShow(t *testing.T) {
dir := t.TempDir()
srv := &Server{
cacheDir: dir,
maxBodySize: 1 << 20,
showTTL: 0, // no caching
mux: http.NewServeMux(),
}
srv.mux.HandleFunc("POST /api/show", srv.handleShow)
body := `{"model":"gpt-4o"}`
req := httptest.NewRequest(http.MethodPost, "/api/show", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
srv.mux.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status = %d, body = %s", w.Code, w.Body.String())
}
var resp ollama.ShowResponse
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("decode: %v", err)
}
if resp.Name != "gpt-4o" {
t.Fatalf("name = %q", resp.Name)
}
if resp.Details.Format != "proxy" {
t.Fatalf("format = %q", resp.Details.Format)
}
}
func TestHandleShow_EmptyModel(t *testing.T) {
srv := &Server{
maxBodySize: 1 << 20,
mux: http.NewServeMux(),
}
srv.mux.HandleFunc("POST /api/show", srv.handleShow)
body := `{"model":""}`
req := httptest.NewRequest(http.MethodPost, "/api/show", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
srv.mux.ServeHTTP(w, req)
if w.Code != http.StatusBadRequest {
t.Fatalf("status = %d, want %d", w.Code, http.StatusBadRequest)
}
}
func TestHandleShow_InvalidJSON(t *testing.T) {
srv := &Server{
maxBodySize: 1 << 20,
mux: http.NewServeMux(),
}
srv.mux.HandleFunc("POST /api/show", srv.handleShow)
req := httptest.NewRequest(http.MethodPost, "/api/show", strings.NewReader("not json"))
w := httptest.NewRecorder()
srv.mux.ServeHTTP(w, req)
if w.Code != http.StatusBadRequest {
t.Fatalf("status = %d, want %d", w.Code, http.StatusBadRequest)
}
}
// // // //
func TestHandleForbidden(t *testing.T) {
srv := &Server{mux: http.NewServeMux()}
srv.mux.HandleFunc("POST /api/pull", srv.handleForbidden)
req := httptest.NewRequest(http.MethodPost, "/api/pull", nil)
w := httptest.NewRecorder()
srv.mux.ServeHTTP(w, req)
if w.Code != http.StatusForbidden {
t.Fatalf("status = %d, want %d", w.Code, http.StatusForbidden)
}
}
func TestHandleEmbedNotSupported(t *testing.T) {
srv := &Server{mux: http.NewServeMux()}
srv.mux.HandleFunc("POST /api/embed", srv.handleEmbedNotSupported)
req := httptest.NewRequest(http.MethodPost, "/api/embed", nil)
w := httptest.NewRecorder()
srv.mux.ServeHTTP(w, req)
if w.Code != http.StatusNotImplemented {
t.Fatalf("status = %d, want %d", w.Code, http.StatusNotImplemented)
}
}
// // // // benchmarks // // // //
func BenchmarkBuildShowResponse(b *testing.B) {
for i := 0; i < b.N; i++ {
buildShowResponse("llama3:70b-instruct-q4_0")
}
}
func BenchmarkHandlePs(b *testing.B) {
srv := &Server{mux: http.NewServeMux()}
srv.mux.HandleFunc("GET /api/ps", srv.handlePs)
req := httptest.NewRequest(http.MethodGet, "/api/ps", nil)
b.ResetTimer()
for i := 0; i < b.N; i++ {
w := httptest.NewRecorder()
srv.mux.ServeHTTP(w, req)
}
}