routes_create_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. package server
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "net/http/httptest"
  10. "os"
  11. "path/filepath"
  12. "slices"
  13. "testing"
  14. "github.com/gin-gonic/gin"
  15. "github.com/ollama/ollama/api"
  16. )
  17. var stream bool = false
  18. func createBinFile(t *testing.T) string {
  19. t.Helper()
  20. f, err := os.CreateTemp(t.TempDir(), "")
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. defer f.Close()
  25. if err := binary.Write(f, binary.LittleEndian, []byte("GGUF")); err != nil {
  26. t.Fatal(err)
  27. }
  28. if err := binary.Write(f, binary.LittleEndian, uint32(3)); err != nil {
  29. t.Fatal(err)
  30. }
  31. if err := binary.Write(f, binary.LittleEndian, uint64(0)); err != nil {
  32. t.Fatal(err)
  33. }
  34. if err := binary.Write(f, binary.LittleEndian, uint64(0)); err != nil {
  35. t.Fatal(err)
  36. }
  37. return f.Name()
  38. }
  39. type responseRecorder struct {
  40. *httptest.ResponseRecorder
  41. http.CloseNotifier
  42. }
  43. func NewRecorder() *responseRecorder {
  44. return &responseRecorder{
  45. ResponseRecorder: httptest.NewRecorder(),
  46. }
  47. }
  48. func (t *responseRecorder) CloseNotify() <-chan bool {
  49. return make(chan bool)
  50. }
  51. func createRequest(t *testing.T, fn func(*gin.Context), body any) *httptest.ResponseRecorder {
  52. t.Helper()
  53. w := NewRecorder()
  54. c, _ := gin.CreateTestContext(w)
  55. var b bytes.Buffer
  56. if err := json.NewEncoder(&b).Encode(body); err != nil {
  57. t.Fatal(err)
  58. }
  59. c.Request = &http.Request{
  60. Body: io.NopCloser(&b),
  61. }
  62. fn(c)
  63. return w.ResponseRecorder
  64. }
  65. func checkFileExists(t *testing.T, p string, expect []string) {
  66. t.Helper()
  67. actual, err := filepath.Glob(p)
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. if !slices.Equal(actual, expect) {
  72. t.Fatalf("expected slices to be equal %v", actual)
  73. }
  74. }
  75. func TestCreateFromBin(t *testing.T) {
  76. p := t.TempDir()
  77. t.Setenv("OLLAMA_MODELS", p)
  78. var s Server
  79. w := createRequest(t, s.CreateModelHandler, api.CreateRequest{
  80. Name: "test",
  81. Modelfile: fmt.Sprintf("FROM %s", createBinFile(t)),
  82. Stream: &stream,
  83. })
  84. if w.Code != http.StatusOK {
  85. t.Fatalf("expected status code 200, actual %d", w.Code)
  86. }
  87. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  88. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  89. })
  90. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  91. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  92. filepath.Join(p, "blobs", "sha256-ca239d7bd8ea90e4a5d2e6bf88f8d74a47b14336e73eb4e18bed4dd325018116"),
  93. })
  94. }
  95. func TestCreateFromModel(t *testing.T) {
  96. p := t.TempDir()
  97. t.Setenv("OLLAMA_MODELS", p)
  98. var s Server
  99. w := createRequest(t, s.CreateModelHandler, api.CreateRequest{
  100. Name: "test",
  101. Modelfile: fmt.Sprintf("FROM %s", createBinFile(t)),
  102. Stream: &stream,
  103. })
  104. if w.Code != http.StatusOK {
  105. t.Fatalf("expected status code 200, actual %d", w.Code)
  106. }
  107. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  108. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  109. })
  110. w = createRequest(t, s.CreateModelHandler, api.CreateRequest{
  111. Name: "test2",
  112. Modelfile: "FROM test",
  113. Stream: &stream,
  114. })
  115. if w.Code != http.StatusOK {
  116. t.Fatalf("expected status code 200, actual %d", w.Code)
  117. }
  118. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  119. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  120. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test2", "latest"),
  121. })
  122. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  123. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  124. filepath.Join(p, "blobs", "sha256-ca239d7bd8ea90e4a5d2e6bf88f8d74a47b14336e73eb4e18bed4dd325018116"),
  125. })
  126. }
  127. func TestCreateRemovesLayers(t *testing.T) {
  128. p := t.TempDir()
  129. t.Setenv("OLLAMA_MODELS", p)
  130. var s Server
  131. w := createRequest(t, s.CreateModelHandler, api.CreateRequest{
  132. Name: "test",
  133. Modelfile: fmt.Sprintf("FROM %s\nTEMPLATE {{ .Prompt }}", createBinFile(t)),
  134. Stream: &stream,
  135. })
  136. if w.Code != http.StatusOK {
  137. t.Fatalf("expected status code 200, actual %d", w.Code)
  138. }
  139. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  140. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  141. })
  142. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  143. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  144. filepath.Join(p, "blobs", "sha256-b507b9c2f6ca642bffcd06665ea7c91f235fd32daeefdf875a0f938db05fb315"),
  145. filepath.Join(p, "blobs", "sha256-bc80b03733773e0728011b2f4adf34c458b400e1aad48cb28d61170f3a2ad2d6"),
  146. })
  147. w = createRequest(t, s.CreateModelHandler, api.CreateRequest{
  148. Name: "test",
  149. Modelfile: fmt.Sprintf("FROM %s\nTEMPLATE {{ .System }} {{ .Prompt }}", createBinFile(t)),
  150. Stream: &stream,
  151. })
  152. if w.Code != http.StatusOK {
  153. t.Fatalf("expected status code 200, actual %d", w.Code)
  154. }
  155. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  156. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  157. })
  158. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  159. filepath.Join(p, "blobs", "sha256-8f2c2167d789c6b2302dff965160fa5029f6a24096d262c1cbb469f21a045382"),
  160. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  161. filepath.Join(p, "blobs", "sha256-fe7ac77b725cda2ccad03f88a880ecdfd7a33192d6cae08fce2c0ee1455991ed"),
  162. })
  163. }
  164. func TestCreateUnsetsSystem(t *testing.T) {
  165. p := t.TempDir()
  166. t.Setenv("OLLAMA_MODELS", p)
  167. var s Server
  168. w := createRequest(t, s.CreateModelHandler, api.CreateRequest{
  169. Name: "test",
  170. Modelfile: fmt.Sprintf("FROM %s\nSYSTEM Say hi!", createBinFile(t)),
  171. Stream: &stream,
  172. })
  173. if w.Code != http.StatusOK {
  174. t.Fatalf("expected status code 200, actual %d", w.Code)
  175. }
  176. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  177. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  178. })
  179. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  180. filepath.Join(p, "blobs", "sha256-8585df945d1069bc78b79bd10bb73ba07fbc29b0f5479a31a601c0d12731416e"),
  181. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  182. filepath.Join(p, "blobs", "sha256-f29e82a8284dbdf5910b1555580ff60b04238b8da9d5e51159ada67a4d0d5851"),
  183. })
  184. w = createRequest(t, s.CreateModelHandler, api.CreateRequest{
  185. Name: "test",
  186. Modelfile: fmt.Sprintf("FROM %s\nSYSTEM \"\"", createBinFile(t)),
  187. Stream: &stream,
  188. })
  189. if w.Code != http.StatusOK {
  190. t.Fatalf("expected status code 200, actual %d", w.Code)
  191. }
  192. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  193. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  194. })
  195. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  196. filepath.Join(p, "blobs", "sha256-67d4b8d106af2a5b100a46e9bdc038c71eef2a35c9abac784092654212f97cf5"),
  197. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  198. filepath.Join(p, "blobs", "sha256-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"),
  199. })
  200. bts, err := os.ReadFile(filepath.Join(p, "blobs", "sha256-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"))
  201. if err != nil {
  202. t.Fatal(err)
  203. }
  204. if string(bts) != "" {
  205. t.Fatalf("expected empty string, actual %s", string(bts))
  206. }
  207. }
  208. func TestCreateMergeParameters(t *testing.T) {
  209. p := t.TempDir()
  210. t.Setenv("OLLAMA_MODELS", p)
  211. var s Server
  212. w := createRequest(t, s.CreateModelHandler, api.CreateRequest{
  213. Name: "test",
  214. Modelfile: fmt.Sprintf("FROM %s\nPARAMETER temperature 1\nPARAMETER top_k 10\nPARAMETER stop USER:\nPARAMETER stop ASSISTANT:", createBinFile(t)),
  215. Stream: &stream,
  216. })
  217. if w.Code != http.StatusOK {
  218. t.Fatalf("expected status code 200, actual %d", w.Code)
  219. }
  220. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  221. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  222. })
  223. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  224. filepath.Join(p, "blobs", "sha256-1d0ad71299d48c2fb7ae2b98e683643e771f8a5b72be34942af90d97a91c1e37"),
  225. filepath.Join(p, "blobs", "sha256-4a384beaf47a9cbe452dfa5ab70eea691790f3b35a832d12933a1996685bf2b6"),
  226. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  227. })
  228. // in order to merge parameters, the second model must be created FROM the first
  229. w = createRequest(t, s.CreateModelHandler, api.CreateRequest{
  230. Name: "test2",
  231. Modelfile: "FROM test\nPARAMETER temperature 0.6\nPARAMETER top_p 0.7",
  232. Stream: &stream,
  233. })
  234. if w.Code != http.StatusOK {
  235. t.Fatalf("expected status code 200, actual %d", w.Code)
  236. }
  237. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  238. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  239. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test2", "latest"),
  240. })
  241. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  242. filepath.Join(p, "blobs", "sha256-1d0ad71299d48c2fb7ae2b98e683643e771f8a5b72be34942af90d97a91c1e37"),
  243. filepath.Join(p, "blobs", "sha256-4a384beaf47a9cbe452dfa5ab70eea691790f3b35a832d12933a1996685bf2b6"),
  244. filepath.Join(p, "blobs", "sha256-4cd9d4ba6b734d9b4cbd1e5caa60374c00722e993fce5e1e2d15a33698f71187"),
  245. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  246. filepath.Join(p, "blobs", "sha256-e29a7b3c47287a2489c895d21fe413c20f859a85d20e749492f52a838e36e1ba"),
  247. })
  248. actual, err := os.ReadFile(filepath.Join(p, "blobs", "sha256-e29a7b3c47287a2489c895d21fe413c20f859a85d20e749492f52a838e36e1ba"))
  249. if err != nil {
  250. t.Fatal(err)
  251. }
  252. expect, err := json.Marshal(map[string]any{"temperature": 0.6, "top_k": 10, "top_p": 0.7, "stop": []string{"USER:", "ASSISTANT:"}})
  253. if err != nil {
  254. t.Fatal(err)
  255. }
  256. if !bytes.Equal(bytes.TrimSpace(expect), bytes.TrimSpace(actual)) {
  257. t.Errorf("expected %s, actual %s", string(expect), string(actual))
  258. }
  259. // slices are replaced
  260. w = createRequest(t, s.CreateModelHandler, api.CreateRequest{
  261. Name: "test2",
  262. Modelfile: "FROM test\nPARAMETER temperature 0.6\nPARAMETER top_p 0.7\nPARAMETER stop <|endoftext|>",
  263. Stream: &stream,
  264. })
  265. if w.Code != http.StatusOK {
  266. t.Fatalf("expected status code 200, actual %d", w.Code)
  267. }
  268. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  269. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  270. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test2", "latest"),
  271. })
  272. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  273. filepath.Join(p, "blobs", "sha256-12f58bb75cb3042d69a7e013ab87fb3c3c7088f50ddc62f0c77bd332f0d44d35"),
  274. filepath.Join(p, "blobs", "sha256-1d0ad71299d48c2fb7ae2b98e683643e771f8a5b72be34942af90d97a91c1e37"),
  275. filepath.Join(p, "blobs", "sha256-257aa726584f24970a4f240765e75a7169bfbe7f4966c1f04513d6b6c860583a"),
  276. filepath.Join(p, "blobs", "sha256-4a384beaf47a9cbe452dfa5ab70eea691790f3b35a832d12933a1996685bf2b6"),
  277. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  278. })
  279. actual, err = os.ReadFile(filepath.Join(p, "blobs", "sha256-12f58bb75cb3042d69a7e013ab87fb3c3c7088f50ddc62f0c77bd332f0d44d35"))
  280. if err != nil {
  281. t.Fatal(err)
  282. }
  283. expect, err = json.Marshal(map[string]any{"temperature": 0.6, "top_k": 10, "top_p": 0.7, "stop": []string{"<|endoftext|>"}})
  284. if err != nil {
  285. t.Fatal(err)
  286. }
  287. if !bytes.Equal(bytes.TrimSpace(expect), bytes.TrimSpace(actual)) {
  288. t.Errorf("expected %s, actual %s", string(expect), string(actual))
  289. }
  290. }
  291. func TestCreateReplacesMessages(t *testing.T) {
  292. p := t.TempDir()
  293. t.Setenv("OLLAMA_MODELS", p)
  294. var s Server
  295. w := createRequest(t, s.CreateModelHandler, api.CreateRequest{
  296. Name: "test",
  297. Modelfile: fmt.Sprintf("FROM %s\nMESSAGE assistant \"What is my purpose?\"\nMESSAGE user \"You run tests.\"\nMESSAGE assistant \"Oh, my god.\"", createBinFile(t)),
  298. Stream: &stream,
  299. })
  300. if w.Code != http.StatusOK {
  301. t.Fatalf("expected status code 200, actual %d", w.Code)
  302. }
  303. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  304. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  305. })
  306. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  307. filepath.Join(p, "blobs", "sha256-298baeaf6928a60cf666d88d64a1ba606feb43a2865687c39e40652e407bffc4"),
  308. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  309. filepath.Join(p, "blobs", "sha256-e0e27d47045063ccb167ae852c51d49a98eab33fabaee4633fdddf97213e40b5"),
  310. })
  311. w = createRequest(t, s.CreateModelHandler, api.CreateRequest{
  312. Name: "test2",
  313. Modelfile: "FROM test\nMESSAGE assistant \"You're a test, Harry.\"\nMESSAGE user \"I-I'm a what?\"\nMESSAGE assistant \"A test. And a thumping good one at that, I'd wager.\"",
  314. Stream: &stream,
  315. })
  316. if w.Code != http.StatusOK {
  317. t.Fatalf("expected status code 200, actual %d", w.Code)
  318. }
  319. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  320. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  321. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test2", "latest"),
  322. })
  323. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  324. filepath.Join(p, "blobs", "sha256-298baeaf6928a60cf666d88d64a1ba606feb43a2865687c39e40652e407bffc4"),
  325. filepath.Join(p, "blobs", "sha256-4f48b25fe9969564c82f58eb1cedbdff6484cc0baf474bc6c2a9b37c8da3362a"),
  326. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  327. filepath.Join(p, "blobs", "sha256-a60ecc9da299ec7ede453f99236e5577fd125e143689b646d9f0ddc9971bf4db"),
  328. filepath.Join(p, "blobs", "sha256-e0e27d47045063ccb167ae852c51d49a98eab33fabaee4633fdddf97213e40b5"),
  329. })
  330. type message struct {
  331. Role string `json:"role"`
  332. Content string `json:"content"`
  333. }
  334. f, err := os.Open(filepath.Join(p, "blobs", "sha256-a60ecc9da299ec7ede453f99236e5577fd125e143689b646d9f0ddc9971bf4db"))
  335. if err != nil {
  336. t.Fatal(err)
  337. }
  338. defer f.Close()
  339. var actual []message
  340. if err := json.NewDecoder(f).Decode(&actual); err != nil {
  341. t.Fatal(err)
  342. }
  343. expect := []message{
  344. {Role: "assistant", Content: "You're a test, Harry."},
  345. {Role: "user", Content: "I-I'm a what?"},
  346. {Role: "assistant", Content: "A test. And a thumping good one at that, I'd wager."},
  347. }
  348. if !slices.Equal(actual, expect) {
  349. t.Errorf("expected %s, actual %s", expect, actual)
  350. }
  351. }
  352. func TestCreateTemplateSystem(t *testing.T) {
  353. p := t.TempDir()
  354. t.Setenv("OLLAMA_MODELS", p)
  355. var s Server
  356. w := createRequest(t, s.CreateModelHandler, api.CreateRequest{
  357. Name: "test",
  358. Modelfile: fmt.Sprintf("FROM %s\nTEMPLATE {{ .Prompt }}\nSYSTEM Say hello!\nTEMPLATE {{ .System }} {{ .Prompt }}\nSYSTEM Say bye!", createBinFile(t)),
  359. Stream: &stream,
  360. })
  361. if w.Code != http.StatusOK {
  362. t.Fatalf("expected status code 200, actual %d", w.Code)
  363. }
  364. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  365. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  366. })
  367. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  368. filepath.Join(p, "blobs", "sha256-2b5e330885117c82f3fd75169ea323e141070a2947c11ddb9f79ee0b01c589c1"),
  369. filepath.Join(p, "blobs", "sha256-4c5f51faac758fecaff8db42f0b7382891a4d0c0bb885f7b86be88c814a7cc86"),
  370. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  371. filepath.Join(p, "blobs", "sha256-fe7ac77b725cda2ccad03f88a880ecdfd7a33192d6cae08fce2c0ee1455991ed"),
  372. })
  373. template, err := os.ReadFile(filepath.Join(p, "blobs", "sha256-fe7ac77b725cda2ccad03f88a880ecdfd7a33192d6cae08fce2c0ee1455991ed"))
  374. if err != nil {
  375. t.Fatal(err)
  376. }
  377. if string(template) != "{{ .System }} {{ .Prompt }}" {
  378. t.Errorf("expected \"{{ .System }} {{ .Prompt }}\", actual %s", template)
  379. }
  380. system, err := os.ReadFile(filepath.Join(p, "blobs", "sha256-4c5f51faac758fecaff8db42f0b7382891a4d0c0bb885f7b86be88c814a7cc86"))
  381. if err != nil {
  382. t.Fatal(err)
  383. }
  384. if string(system) != "Say bye!" {
  385. t.Errorf("expected \"Say bye!\", actual %s", system)
  386. }
  387. }
  388. func TestCreateLicenses(t *testing.T) {
  389. p := t.TempDir()
  390. t.Setenv("OLLAMA_MODELS", p)
  391. var s Server
  392. w := createRequest(t, s.CreateModelHandler, api.CreateRequest{
  393. Name: "test",
  394. Modelfile: fmt.Sprintf("FROM %s\nLICENSE MIT\nLICENSE Apache-2.0", createBinFile(t)),
  395. Stream: &stream,
  396. })
  397. if w.Code != http.StatusOK {
  398. t.Fatalf("expected status code 200, actual %d", w.Code)
  399. }
  400. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  401. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  402. })
  403. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  404. filepath.Join(p, "blobs", "sha256-2af71558e438db0b73a20beab92dc278a94e1bbe974c00c1a33e3ab62d53a608"),
  405. filepath.Join(p, "blobs", "sha256-79a39c37536ddee29cbadd5d5e2dcba8ed7f03e431f626ff38432c1c866bb7e2"),
  406. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  407. filepath.Join(p, "blobs", "sha256-e5dcffe836b6ec8a58e492419b550e65fb8cbdc308503979e5dacb33ac7ea3b7"),
  408. })
  409. mit, err := os.ReadFile(filepath.Join(p, "blobs", "sha256-e5dcffe836b6ec8a58e492419b550e65fb8cbdc308503979e5dacb33ac7ea3b7"))
  410. if err != nil {
  411. t.Fatal(err)
  412. }
  413. if string(mit) != "MIT" {
  414. t.Errorf("expected MIT, actual %s", mit)
  415. }
  416. apache, err := os.ReadFile(filepath.Join(p, "blobs", "sha256-2af71558e438db0b73a20beab92dc278a94e1bbe974c00c1a33e3ab62d53a608"))
  417. if err != nil {
  418. t.Fatal(err)
  419. }
  420. if string(apache) != "Apache-2.0" {
  421. t.Errorf("expected Apache-2.0, actual %s", apache)
  422. }
  423. }