routes_delete_test.go 3.2 KB

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