@@ -3,233 +3,16 @@ package desktop
33import (
44 "bytes"
55 "context"
6- "encoding/json"
76 "errors"
87 "io"
98 "net/http"
109 "testing"
1110
1211 mockdesktop "github.com/docker/model-runner/cmd/cli/mocks"
13- "github.com/docker/model-runner/pkg/inference/models"
1412 "github.com/stretchr/testify/assert"
15- "github.com/stretchr/testify/require"
1613 "go.uber.org/mock/gomock"
1714)
1815
19- func TestPullHuggingFaceModel (t * testing.T ) {
20- ctrl := gomock .NewController (t )
21- defer ctrl .Finish ()
22-
23- // Test case for pulling a Hugging Face model with mixed case
24- modelName := "hf.co/Bartowski/Llama-3.2-1B-Instruct-GGUF"
25- expectedLowercase := "hf.co/bartowski/llama-3.2-1b-instruct-gguf"
26-
27- mockClient := mockdesktop .NewMockDockerHttpClient (ctrl )
28- mockContext := NewContextForMock (mockClient )
29- client := New (mockContext )
30-
31- mockClient .EXPECT ().Do (gomock .Any ()).Do (func (req * http.Request ) {
32- var reqBody models.ModelCreateRequest
33- err := json .NewDecoder (req .Body ).Decode (& reqBody )
34- require .NoError (t , err )
35- assert .Equal (t , expectedLowercase , reqBody .From )
36- }).Return (& http.Response {
37- StatusCode : http .StatusOK ,
38- Body : io .NopCloser (bytes .NewBufferString (`{"type":"success","message":"Model pulled successfully"}` )),
39- }, nil )
40-
41- printer := NewSimplePrinter (func (s string ) {})
42- _ , _ , err := client .Pull (modelName , printer )
43- assert .NoError (t , err )
44- }
45-
46- func TestChatHuggingFaceModel (t * testing.T ) {
47- ctrl := gomock .NewController (t )
48- defer ctrl .Finish ()
49-
50- // Test case for chatting with a Hugging Face model with mixed case
51- modelName := "hf.co/Bartowski/Llama-3.2-1B-Instruct-GGUF"
52- expectedLowercase := "hf.co/bartowski/llama-3.2-1b-instruct-gguf"
53- prompt := "Hello"
54-
55- mockClient := mockdesktop .NewMockDockerHttpClient (ctrl )
56- mockContext := NewContextForMock (mockClient )
57- client := New (mockContext )
58-
59- mockClient .EXPECT ().Do (gomock .Any ()).Do (func (req * http.Request ) {
60- var reqBody OpenAIChatRequest
61- err := json .NewDecoder (req .Body ).Decode (& reqBody )
62- require .NoError (t , err )
63- assert .Equal (t , expectedLowercase , reqBody .Model )
64- }).Return (& http.Response {
65- StatusCode : http .StatusOK ,
66- Body : io .NopCloser (bytes .NewBufferString ("data: {\" choices\" :[{\" delta\" :{\" content\" :\" Hello there!\" }}]}\n " )),
67- }, nil )
68-
69- err := client .Chat (modelName , prompt , []string {}, func (s string ) {}, false )
70- assert .NoError (t , err )
71- }
72-
73- func TestInspectHuggingFaceModel (t * testing.T ) {
74- ctrl := gomock .NewController (t )
75- defer ctrl .Finish ()
76-
77- // Test case for inspecting a Hugging Face model with mixed case
78- modelName := "hf.co/Bartowski/Llama-3.2-1B-Instruct-GGUF"
79- expectedLowercase := "hf.co/bartowski/llama-3.2-1b-instruct-gguf"
80-
81- mockClient := mockdesktop .NewMockDockerHttpClient (ctrl )
82- mockContext := NewContextForMock (mockClient )
83- client := New (mockContext )
84-
85- mockClient .EXPECT ().Do (gomock .Any ()).Do (func (req * http.Request ) {
86- assert .Contains (t , req .URL .Path , expectedLowercase )
87- }).Return (& http.Response {
88- StatusCode : http .StatusOK ,
89- Body : io .NopCloser (bytes .NewBufferString (`{
90- "id": "sha256:123456789012",
91- "tags": ["` + expectedLowercase + `"],
92- "created": 1234567890,
93- "config": {
94- "format": "gguf",
95- "quantization": "Q4_K_M",
96- "parameters": "1B",
97- "architecture": "llama",
98- "size": "1.2GB"
99- }
100- }` )),
101- }, nil )
102-
103- model , err := client .Inspect (modelName , false )
104- assert .NoError (t , err )
105- assert .Equal (t , expectedLowercase , model .Tags [0 ])
106- }
107-
108- func TestNonHuggingFaceModel (t * testing.T ) {
109- ctrl := gomock .NewController (t )
110- defer ctrl .Finish ()
111-
112- // Test case for a non-Hugging Face model (should not be converted to lowercase)
113- modelName := "docker.io/library/llama2"
114- mockClient := mockdesktop .NewMockDockerHttpClient (ctrl )
115- mockContext := NewContextForMock (mockClient )
116- client := New (mockContext )
117-
118- mockClient .EXPECT ().Do (gomock .Any ()).Do (func (req * http.Request ) {
119- var reqBody models.ModelCreateRequest
120- err := json .NewDecoder (req .Body ).Decode (& reqBody )
121- require .NoError (t , err )
122- assert .Equal (t , modelName , reqBody .From )
123- }).Return (& http.Response {
124- StatusCode : http .StatusOK ,
125- Body : io .NopCloser (bytes .NewBufferString (`{"type":"success","message":"Model pulled successfully"}` )),
126- }, nil )
127-
128- printer := NewSimplePrinter (func (s string ) {})
129- _ , _ , err := client .Pull (modelName , printer )
130- assert .NoError (t , err )
131- }
132-
133- func TestPushHuggingFaceModel (t * testing.T ) {
134- ctrl := gomock .NewController (t )
135- defer ctrl .Finish ()
136-
137- // Test case for pushing a Hugging Face model with mixed case
138- modelName := "hf.co/Bartowski/Llama-3.2-1B-Instruct-GGUF"
139- expectedLowercase := "hf.co/bartowski/llama-3.2-1b-instruct-gguf"
140-
141- mockClient := mockdesktop .NewMockDockerHttpClient (ctrl )
142- mockContext := NewContextForMock (mockClient )
143- client := New (mockContext )
144-
145- mockClient .EXPECT ().Do (gomock .Any ()).Do (func (req * http.Request ) {
146- assert .Contains (t , req .URL .Path , expectedLowercase )
147- }).Return (& http.Response {
148- StatusCode : http .StatusOK ,
149- Body : io .NopCloser (bytes .NewBufferString (`{"type":"success","message":"Model pushed successfully"}` )),
150- }, nil )
151-
152- printer := NewSimplePrinter (func (s string ) {})
153- _ , _ , err := client .Push (modelName , printer )
154- assert .NoError (t , err )
155- }
156-
157- func TestRemoveHuggingFaceModel (t * testing.T ) {
158- ctrl := gomock .NewController (t )
159- defer ctrl .Finish ()
160-
161- // Test case for removing a Hugging Face model with mixed case
162- modelName := "hf.co/Bartowski/Llama-3.2-1B-Instruct-GGUF"
163- expectedLowercase := "hf.co/bartowski/llama-3.2-1b-instruct-gguf"
164-
165- mockClient := mockdesktop .NewMockDockerHttpClient (ctrl )
166- mockContext := NewContextForMock (mockClient )
167- client := New (mockContext )
168-
169- mockClient .EXPECT ().Do (gomock .Any ()).Do (func (req * http.Request ) {
170- assert .Contains (t , req .URL .Path , expectedLowercase )
171- }).Return (& http.Response {
172- StatusCode : http .StatusOK ,
173- Body : io .NopCloser (bytes .NewBufferString ("Model removed successfully" )),
174- }, nil )
175-
176- _ , err := client .Remove ([]string {modelName }, false )
177- assert .NoError (t , err )
178- }
179-
180- func TestTagHuggingFaceModel (t * testing.T ) {
181- ctrl := gomock .NewController (t )
182- defer ctrl .Finish ()
183-
184- // Test case for tagging a Hugging Face model with mixed case
185- sourceModel := "hf.co/Bartowski/Llama-3.2-1B-Instruct-GGUF"
186- expectedLowercase := "hf.co/bartowski/llama-3.2-1b-instruct-gguf"
187- targetRepo := "myrepo"
188- targetTag := "latest"
189-
190- mockClient := mockdesktop .NewMockDockerHttpClient (ctrl )
191- mockContext := NewContextForMock (mockClient )
192- client := New (mockContext )
193-
194- mockClient .EXPECT ().Do (gomock .Any ()).Do (func (req * http.Request ) {
195- assert .Contains (t , req .URL .Path , expectedLowercase )
196- }).Return (& http.Response {
197- StatusCode : http .StatusCreated ,
198- Body : io .NopCloser (bytes .NewBufferString ("Tag created successfully" )),
199- }, nil )
200-
201- assert .NoError (t , client .Tag (sourceModel , targetRepo , targetTag ))
202- }
203-
204- func TestInspectOpenAIHuggingFaceModel (t * testing.T ) {
205- ctrl := gomock .NewController (t )
206- defer ctrl .Finish ()
207-
208- // Test case for inspecting a Hugging Face model with mixed case
209- modelName := "hf.co/Bartowski/Llama-3.2-1B-Instruct-GGUF"
210- expectedLowercase := "hf.co/bartowski/llama-3.2-1b-instruct-gguf"
211-
212- mockClient := mockdesktop .NewMockDockerHttpClient (ctrl )
213- mockContext := NewContextForMock (mockClient )
214- client := New (mockContext )
215-
216- mockClient .EXPECT ().Do (gomock .Any ()).Do (func (req * http.Request ) {
217- assert .Contains (t , req .URL .Path , expectedLowercase )
218- }).Return (& http.Response {
219- StatusCode : http .StatusOK ,
220- Body : io .NopCloser (bytes .NewBufferString (`{
221- "id": "` + expectedLowercase + `",
222- "object": "model",
223- "created": 1234567890,
224- "owned_by": "organization"
225- }` )),
226- }, nil )
227-
228- model , err := client .InspectOpenAI (modelName )
229- assert .NoError (t , err )
230- assert .Equal (t , expectedLowercase , model .ID )
231- }
232-
23316func TestPullRetryOnNetworkError (t * testing.T ) {
23417 ctrl := gomock .NewController (t )
23518 defer ctrl .Finish ()
0 commit comments