modelpath_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. }