store_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package blobstore
  2. import (
  3. "errors"
  4. "iter"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "time"
  9. "github.com/ollama/ollama/x/model"
  10. "kr.dev/diff"
  11. )
  12. const (
  13. blobNameHello = "sha256-2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
  14. )
  15. func TestStoreBasicBlob(t *testing.T) {
  16. dir := t.TempDir()
  17. checkDir(t, dir, nil)
  18. st, err := Open(dir)
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. now := time.Now()
  23. st.now = func() time.Time { return now }
  24. checkDir(t, dir, []string{
  25. "blobs/",
  26. })
  27. id, size, err := PutBytes(st, []byte("hello"))
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. if id != ParseID(blobNameHello) {
  32. t.Errorf("unexpected ID: %s", id)
  33. }
  34. if size != 5 {
  35. t.Errorf("unexpected size: %d", size)
  36. }
  37. checkDir(t, dir, []string{
  38. "blobs/",
  39. "blobs/" + blobNameHello,
  40. })
  41. got, err := st.Get(id)
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. diff.Test(t, t.Errorf, got, Entry{
  46. ID: id,
  47. Size: 5,
  48. Time: now,
  49. })
  50. file := st.OutputFilename(id)
  51. wantFile := filepath.Join(dir, "blobs", blobNameHello)
  52. if file != wantFile {
  53. t.Errorf("unexpected file: %s", file)
  54. }
  55. // Check tags
  56. name := model.ParseName("registry.ollama.ai/library/test:latest+KQED")
  57. t.Logf("RESOLVING: %q", name.Parts())
  58. }
  59. // checkDir checks that the directory at dir contains the files in want. The
  60. // files in want must be relative to dir.
  61. //
  62. // direcotories are suffixed with a slash (e.g. "foo/" instead of "foo").
  63. //
  64. // want must be in lexicographic order.
  65. func checkDir(t testing.TB, dir string, want []string) {
  66. t.Helper()
  67. var matches []string
  68. for path, err := range walkDir(dir) {
  69. t.Helper()
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. t.Logf("found %s", path)
  74. if path == "./" {
  75. continue
  76. }
  77. path = filepath.ToSlash(path)
  78. matches = append(matches, path)
  79. }
  80. diff.Test(t, t.Errorf, matches, want)
  81. }
  82. var errStop = errors.New("stop")
  83. func walkDir(dir string) iter.Seq2[string, error] {
  84. return func(yield func(string, error) bool) {
  85. err := filepath.WalkDir(dir, func(path string, info os.DirEntry, err error) error {
  86. if err != nil {
  87. return err
  88. }
  89. path, err = filepath.Rel(dir, path)
  90. if err != nil {
  91. return err
  92. }
  93. path = filepath.ToSlash(path)
  94. if info.IsDir() {
  95. path += "/"
  96. }
  97. if !yield(path, nil) {
  98. return errStop
  99. }
  100. return nil
  101. })
  102. if !errors.Is(err, errStop) && err != nil {
  103. yield("", err)
  104. }
  105. }
  106. }