modelpath_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package server
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. "github.com/ollama/ollama/envconfig"
  9. )
  10. func TestGetBlobsPath(t *testing.T) {
  11. // GetBlobsPath expects an actual directory to exist
  12. dir, err := os.MkdirTemp("", "ollama-test")
  13. require.NoError(t, err)
  14. defer os.RemoveAll(dir)
  15. tests := []struct {
  16. name string
  17. digest string
  18. expected string
  19. err error
  20. }{
  21. {
  22. "empty digest",
  23. "",
  24. filepath.Join(dir, "blobs"),
  25. nil,
  26. },
  27. {
  28. "valid with colon",
  29. "sha256:456402914e838a953e0cf80caa6adbe75383d9e63584a964f504a7bbb8f7aad9",
  30. filepath.Join(dir, "blobs", "sha256-456402914e838a953e0cf80caa6adbe75383d9e63584a964f504a7bbb8f7aad9"),
  31. nil,
  32. },
  33. {
  34. "valid with dash",
  35. "sha256-456402914e838a953e0cf80caa6adbe75383d9e63584a964f504a7bbb8f7aad9",
  36. filepath.Join(dir, "blobs", "sha256-456402914e838a953e0cf80caa6adbe75383d9e63584a964f504a7bbb8f7aad9"),
  37. nil,
  38. },
  39. {
  40. "digest too short",
  41. "sha256-45640291",
  42. "",
  43. ErrInvalidDigestFormat,
  44. },
  45. {
  46. "digest too long",
  47. "sha256-456402914e838a953e0cf80caa6adbe75383d9e63584a964f504a7bbb8f7aad9aaaaaaaaaa",
  48. "",
  49. ErrInvalidDigestFormat,
  50. },
  51. {
  52. "digest invalid chars",
  53. "../sha256-456402914e838a953e0cf80caa6adbe75383d9e63584a964f504a7bbb8f7a",
  54. "",
  55. ErrInvalidDigestFormat,
  56. },
  57. }
  58. for _, tc := range tests {
  59. t.Run(tc.name, func(t *testing.T) {
  60. t.Setenv("OLLAMA_MODELS", dir)
  61. envconfig.LoadConfig()
  62. got, err := GetBlobsPath(tc.digest)
  63. require.ErrorIs(t, tc.err, err, tc.name)
  64. assert.Equal(t, tc.expected, got, tc.name)
  65. })
  66. }
  67. }
  68. func TestParseModelPath(t *testing.T) {
  69. tests := []struct {
  70. name string
  71. arg string
  72. want ModelPath
  73. }{
  74. {
  75. "full path https",
  76. "https://example.com/ns/repo:tag",
  77. ModelPath{
  78. ProtocolScheme: "https",
  79. Registry: "example.com",
  80. Namespace: "ns",
  81. Repository: "repo",
  82. Tag: "tag",
  83. },
  84. },
  85. {
  86. "full path http",
  87. "http://example.com/ns/repo:tag",
  88. ModelPath{
  89. ProtocolScheme: "http",
  90. Registry: "example.com",
  91. Namespace: "ns",
  92. Repository: "repo",
  93. Tag: "tag",
  94. },
  95. },
  96. {
  97. "no protocol",
  98. "example.com/ns/repo:tag",
  99. ModelPath{
  100. ProtocolScheme: "https",
  101. Registry: "example.com",
  102. Namespace: "ns",
  103. Repository: "repo",
  104. Tag: "tag",
  105. },
  106. },
  107. {
  108. "no registry",
  109. "ns/repo:tag",
  110. ModelPath{
  111. ProtocolScheme: "https",
  112. Registry: DefaultRegistry,
  113. Namespace: "ns",
  114. Repository: "repo",
  115. Tag: "tag",
  116. },
  117. },
  118. {
  119. "no namespace",
  120. "repo:tag",
  121. ModelPath{
  122. ProtocolScheme: "https",
  123. Registry: DefaultRegistry,
  124. Namespace: DefaultNamespace,
  125. Repository: "repo",
  126. Tag: "tag",
  127. },
  128. },
  129. {
  130. "no tag",
  131. "repo",
  132. ModelPath{
  133. ProtocolScheme: "https",
  134. Registry: DefaultRegistry,
  135. Namespace: DefaultNamespace,
  136. Repository: "repo",
  137. Tag: DefaultTag,
  138. },
  139. },
  140. }
  141. for _, tc := range tests {
  142. t.Run(tc.name, func(t *testing.T) {
  143. got := ParseModelPath(tc.arg)
  144. if got != tc.want {
  145. t.Errorf("got: %q want: %q", got, tc.want)
  146. }
  147. })
  148. }
  149. }
  150. func TestMigrateRegistryDomain(t *testing.T) {
  151. p := t.TempDir()
  152. t.Setenv("OLLAMA_MODELS", p)
  153. envconfig.LoadConfig()
  154. manifests := []string{
  155. filepath.Join("registry.ollama.ai", "library", "llama3", "7b"),
  156. filepath.Join("registry.ollama.ai", "library", "mistral", "latest"),
  157. filepath.Join("registry.other.com", "library", "llama3", "13b"),
  158. }
  159. for _, manifest := range manifests {
  160. n := filepath.Join(p, "manifests", manifest)
  161. if err := os.MkdirAll(filepath.Dir(n), 0o750); err != nil {
  162. t.Fatal(err)
  163. }
  164. f, err := os.Create(n)
  165. if err != nil {
  166. t.Fatal(err)
  167. }
  168. if err := f.Close(); err != nil {
  169. t.Fatal(err)
  170. }
  171. }
  172. t.Run("migrate", func(t *testing.T) {
  173. if err := migrateRegistryDomain(); err != nil {
  174. t.Fatal(err)
  175. }
  176. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  177. filepath.Join(p, "manifests", DefaultRegistry, "library", "llama3", "7b"),
  178. filepath.Join(p, "manifests", DefaultRegistry, "library", "mistral", "latest"),
  179. filepath.Join(p, "manifests", "registry.other.com", "library", "llama3", "13b"),
  180. })
  181. })
  182. t.Run("idempotent", func(t *testing.T) {
  183. // subsequent run should be a noop
  184. if err := migrateRegistryDomain(); err != nil {
  185. t.Fatal(err)
  186. }
  187. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  188. filepath.Join(p, "manifests", DefaultRegistry, "library", "llama3", "7b"),
  189. filepath.Join(p, "manifests", DefaultRegistry, "library", "mistral", "latest"),
  190. filepath.Join(p, "manifests", "registry.other.com", "library", "llama3", "13b"),
  191. })
  192. })
  193. t.Run("no migration needed", func(t *testing.T) {
  194. n := filepath.Join(p, "manifests", "registry.ollama.ai", "library", "gemma", "7b")
  195. if err := os.MkdirAll(filepath.Dir(n), 0o750); err != nil {
  196. t.Fatal(err)
  197. }
  198. f, err := os.Create(n)
  199. if err != nil {
  200. t.Fatal(err)
  201. }
  202. if err := f.Close(); err != nil {
  203. t.Fatal(err)
  204. }
  205. if err := migrateRegistryDomain(); err != nil {
  206. t.Fatal(err)
  207. }
  208. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  209. filepath.Join(p, "manifests", DefaultRegistry, "library", "llama3", "7b"),
  210. filepath.Join(p, "manifests", DefaultRegistry, "library", "mistral", "latest"),
  211. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "gemma", "7b"),
  212. filepath.Join(p, "manifests", "registry.other.com", "library", "llama3", "13b"),
  213. })
  214. })
  215. t.Run("no migration source", func(t *testing.T) {
  216. // cleanup premigration directories
  217. if err := os.RemoveAll(filepath.Join(p, "manifests", "registry.ollama.ai")); err != nil {
  218. t.Fatal(err)
  219. }
  220. if err := migrateRegistryDomain(); err != nil {
  221. t.Fatal(err)
  222. }
  223. })
  224. }