2023-12-15 00:47:40 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2023-12-15 19:59:18 +00:00
|
|
|
"bytes"
|
2023-12-15 00:47:40 +00:00
|
|
|
"context"
|
2023-12-15 02:18:25 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-12-15 00:47:40 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2023-12-15 02:18:25 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2023-12-15 00:47:40 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-12-15 02:18:25 +00:00
|
|
|
|
|
|
|
"github.com/jmorganca/ollama/api"
|
|
|
|
"github.com/jmorganca/ollama/parser"
|
2023-12-15 00:47:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func setupServer(t *testing.T) (*Server, error) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
return NewServer()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Routes(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
Name string
|
|
|
|
Method string
|
|
|
|
Path string
|
|
|
|
Setup func(t *testing.T, req *http.Request)
|
|
|
|
Expected func(t *testing.T, resp *http.Response)
|
|
|
|
}
|
2023-12-15 19:59:18 +00:00
|
|
|
|
2023-12-18 18:41:02 +00:00
|
|
|
createTestFile := func(t *testing.T, name string) string {
|
|
|
|
f, err := os.CreateTemp(t.TempDir(), name)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
_, err = f.Write([]byte("GGUF"))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
_, err = f.Write([]byte{0x2, 0})
|
2023-12-15 19:59:18 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2023-12-18 18:41:02 +00:00
|
|
|
return f.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
createTestModel := func(t *testing.T, name string) {
|
|
|
|
fname := createTestFile(t, "ollama-model")
|
|
|
|
|
|
|
|
modelfile := strings.NewReader(fmt.Sprintf("FROM %s", fname))
|
2023-12-15 19:59:18 +00:00
|
|
|
commands, err := parser.Parse(modelfile)
|
|
|
|
assert.Nil(t, err)
|
2023-12-18 18:41:02 +00:00
|
|
|
fn := func(resp api.ProgressResponse) {
|
|
|
|
t.Logf("Status: %s", resp.Status)
|
|
|
|
}
|
2023-12-15 19:59:18 +00:00
|
|
|
err = CreateModel(context.TODO(), name, "", commands, fn)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
2023-12-15 00:47:40 +00:00
|
|
|
|
|
|
|
testCases := []testCase{
|
|
|
|
{
|
|
|
|
Name: "Version Handler",
|
|
|
|
Method: http.MethodGet,
|
|
|
|
Path: "/api/version",
|
|
|
|
Setup: func(t *testing.T, req *http.Request) {
|
|
|
|
},
|
|
|
|
Expected: func(t *testing.T, resp *http.Response) {
|
|
|
|
contentType := resp.Header.Get("Content-Type")
|
|
|
|
assert.Equal(t, contentType, "application/json; charset=utf-8")
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, `{"version":"0.0.0"}`, string(body))
|
|
|
|
},
|
|
|
|
},
|
2023-12-15 02:18:25 +00:00
|
|
|
{
|
|
|
|
Name: "Tags Handler (no tags)",
|
|
|
|
Method: http.MethodGet,
|
|
|
|
Path: "/api/tags",
|
|
|
|
Expected: func(t *testing.T, resp *http.Response) {
|
|
|
|
contentType := resp.Header.Get("Content-Type")
|
|
|
|
assert.Equal(t, contentType, "application/json; charset=utf-8")
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
var modelList api.ListResponse
|
|
|
|
|
|
|
|
err = json.Unmarshal(body, &modelList)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, 0, len(modelList.Models))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Tags Handler (yes tags)",
|
|
|
|
Method: http.MethodGet,
|
|
|
|
Path: "/api/tags",
|
|
|
|
Setup: func(t *testing.T, req *http.Request) {
|
2023-12-15 19:59:18 +00:00
|
|
|
createTestModel(t, "test-model")
|
2023-12-15 02:18:25 +00:00
|
|
|
},
|
|
|
|
Expected: func(t *testing.T, resp *http.Response) {
|
|
|
|
contentType := resp.Header.Get("Content-Type")
|
|
|
|
assert.Equal(t, contentType, "application/json; charset=utf-8")
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
var modelList api.ListResponse
|
|
|
|
err = json.Unmarshal(body, &modelList)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, 1, len(modelList.Models))
|
2023-12-15 19:59:18 +00:00
|
|
|
assert.Equal(t, modelList.Models[0].Name, "test-model:latest")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Create Model Handler",
|
|
|
|
Method: http.MethodPost,
|
|
|
|
Path: "/api/create",
|
|
|
|
Setup: func(t *testing.T, req *http.Request) {
|
2023-12-18 18:41:02 +00:00
|
|
|
f, err := os.CreateTemp(t.TempDir(), "ollama-model")
|
2023-12-15 19:59:18 +00:00
|
|
|
assert.Nil(t, err)
|
2023-12-18 18:41:02 +00:00
|
|
|
defer f.Close()
|
2023-12-15 19:59:18 +00:00
|
|
|
|
|
|
|
stream := false
|
|
|
|
createReq := api.CreateRequest{
|
|
|
|
Name: "t-bone",
|
|
|
|
Modelfile: fmt.Sprintf("FROM %s", f.Name()),
|
|
|
|
Stream: &stream,
|
|
|
|
}
|
|
|
|
jsonData, err := json.Marshal(createReq)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
req.Body = io.NopCloser(bytes.NewReader(jsonData))
|
|
|
|
},
|
|
|
|
Expected: func(t *testing.T, resp *http.Response) {
|
|
|
|
contentType := resp.Header.Get("Content-Type")
|
|
|
|
assert.Equal(t, "application/json", contentType)
|
|
|
|
_, err := io.ReadAll(resp.Body)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, resp.StatusCode, 200)
|
|
|
|
|
|
|
|
model, err := GetModel("t-bone")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "t-bone:latest", model.ShortName)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Copy Model Handler",
|
|
|
|
Method: http.MethodPost,
|
|
|
|
Path: "/api/copy",
|
|
|
|
Setup: func(t *testing.T, req *http.Request) {
|
|
|
|
createTestModel(t, "hamshank")
|
|
|
|
copyReq := api.CopyRequest{
|
|
|
|
Source: "hamshank",
|
|
|
|
Destination: "beefsteak",
|
|
|
|
}
|
|
|
|
jsonData, err := json.Marshal(copyReq)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
req.Body = io.NopCloser(bytes.NewReader(jsonData))
|
|
|
|
},
|
|
|
|
Expected: func(t *testing.T, resp *http.Response) {
|
|
|
|
model, err := GetModel("beefsteak")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "beefsteak:latest", model.ShortName)
|
2023-12-15 02:18:25 +00:00
|
|
|
},
|
|
|
|
},
|
2023-12-15 00:47:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s, err := setupServer(t)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
router := s.GenerateRoutes()
|
|
|
|
|
|
|
|
httpSrv := httptest.NewServer(router)
|
|
|
|
t.Cleanup(httpSrv.Close)
|
|
|
|
|
2023-12-15 02:18:25 +00:00
|
|
|
workDir, err := os.MkdirTemp("", "ollama-test")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
defer os.RemoveAll(workDir)
|
|
|
|
os.Setenv("OLLAMA_MODELS", workDir)
|
|
|
|
|
2023-12-15 00:47:40 +00:00
|
|
|
for _, tc := range testCases {
|
2023-12-18 18:41:02 +00:00
|
|
|
t.Logf("Running Test: [%s]", tc.Name)
|
2023-12-15 00:47:40 +00:00
|
|
|
u := httpSrv.URL + tc.Path
|
|
|
|
req, err := http.NewRequestWithContext(context.TODO(), tc.Method, u, nil)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
if tc.Setup != nil {
|
|
|
|
tc.Setup(t, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := httpSrv.Client().Do(req)
|
2023-12-15 19:59:18 +00:00
|
|
|
defer resp.Body.Close()
|
2023-12-15 00:47:40 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
if tc.Expected != nil {
|
|
|
|
tc.Expected(t, resp)
|
|
|
|
}
|
2023-12-15 19:59:18 +00:00
|
|
|
|
2023-12-15 00:47:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|