routes_delete_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package server
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "net/http"
  6. "path/filepath"
  7. "testing"
  8. "github.com/gin-gonic/gin"
  9. "github.com/ollama/ollama/api"
  10. "github.com/ollama/ollama/types/model"
  11. )
  12. func TestDelete(t *testing.T) {
  13. gin.SetMode(gin.TestMode)
  14. p := t.TempDir()
  15. t.Setenv("OLLAMA_MODELS", p)
  16. var s Server
  17. _, digest := createBinFile(t, nil, nil)
  18. w := createRequest(t, s.CreateHandler, api.CreateRequest{
  19. Name: "test",
  20. Files: map[string]string{"test.gguf": digest},
  21. })
  22. if w.Code != http.StatusOK {
  23. t.Fatalf("expected status code 200, actual %d", w.Code)
  24. }
  25. w = createRequest(t, s.CreateHandler, api.CreateRequest{
  26. Name: "test2",
  27. Files: map[string]string{"test.gguf": digest},
  28. Template: "{{ .System }} {{ .Prompt }}",
  29. })
  30. if w.Code != http.StatusOK {
  31. t.Fatalf("expected status code 200, actual %d", w.Code)
  32. }
  33. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  34. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  35. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test2", "latest"),
  36. })
  37. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  38. filepath.Join(p, "blobs", "sha256-8f2c2167d789c6b2302dff965160fa5029f6a24096d262c1cbb469f21a045382"),
  39. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  40. filepath.Join(p, "blobs", "sha256-ca239d7bd8ea90e4a5d2e6bf88f8d74a47b14336e73eb4e18bed4dd325018116"),
  41. filepath.Join(p, "blobs", "sha256-fe7ac77b725cda2ccad03f88a880ecdfd7a33192d6cae08fce2c0ee1455991ed"),
  42. })
  43. w = createRequest(t, s.DeleteHandler, api.DeleteRequest{Name: "test"})
  44. if w.Code != http.StatusOK {
  45. t.Fatalf("expected status code 200, actual %d", w.Code)
  46. }
  47. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  48. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test2", "latest"),
  49. })
  50. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  51. filepath.Join(p, "blobs", "sha256-8f2c2167d789c6b2302dff965160fa5029f6a24096d262c1cbb469f21a045382"),
  52. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  53. filepath.Join(p, "blobs", "sha256-fe7ac77b725cda2ccad03f88a880ecdfd7a33192d6cae08fce2c0ee1455991ed"),
  54. })
  55. w = createRequest(t, s.DeleteHandler, api.DeleteRequest{Name: "test2"})
  56. if w.Code != http.StatusOK {
  57. t.Fatalf("expected status code 200, actual %d", w.Code)
  58. }
  59. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{})
  60. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{})
  61. }
  62. func TestDeleteDuplicateLayers(t *testing.T) {
  63. gin.SetMode(gin.TestMode)
  64. p := t.TempDir()
  65. t.Setenv("OLLAMA_MODELS", p)
  66. var s Server
  67. n := model.ParseName("test")
  68. var b bytes.Buffer
  69. if err := json.NewEncoder(&b).Encode(&ConfigV2{}); err != nil {
  70. t.Fatal(err)
  71. }
  72. config, err := NewLayer(&b, "application/vnd.docker.container.image.v1+json")
  73. if err != nil {
  74. t.Fatal(err)
  75. }
  76. // create a manifest with duplicate layers
  77. if err := WriteManifest(n, config, []Layer{config}); err != nil {
  78. t.Fatal(err)
  79. }
  80. w := createRequest(t, s.DeleteHandler, api.DeleteRequest{Name: "test"})
  81. if w.Code != http.StatusOK {
  82. t.Errorf("expected status code 200, actual %d", w.Code)
  83. }
  84. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{})
  85. }