routes_delete_test.go 3.3 KB

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