routes_delete_test.go 3.4 KB

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