routes_delete_test.go 3.4 KB

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