modelpath_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. )
  9. func TestGetBlobsPath(t *testing.T) {
  10. // GetBlobsPath expects an actual directory to exist
  11. dir, err := os.MkdirTemp("", "ollama-test")
  12. require.NoError(t, err)
  13. defer os.RemoveAll(dir)
  14. tests := []struct {
  15. name string
  16. digest string
  17. expected string
  18. err error
  19. }{
  20. {
  21. "empty digest",
  22. "",
  23. filepath.Join(dir, "blobs"),
  24. nil,
  25. },
  26. {
  27. "valid with colon",
  28. "sha256:456402914e838a953e0cf80caa6adbe75383d9e63584a964f504a7bbb8f7aad9",
  29. filepath.Join(dir, "blobs", "sha256-456402914e838a953e0cf80caa6adbe75383d9e63584a964f504a7bbb8f7aad9"),
  30. nil,
  31. },
  32. {
  33. "valid with dash",
  34. "sha256-456402914e838a953e0cf80caa6adbe75383d9e63584a964f504a7bbb8f7aad9",
  35. filepath.Join(dir, "blobs", "sha256-456402914e838a953e0cf80caa6adbe75383d9e63584a964f504a7bbb8f7aad9"),
  36. nil,
  37. },
  38. {
  39. "digest too short",
  40. "sha256-45640291",
  41. "",
  42. ErrInvalidDigestFormat,
  43. },
  44. {
  45. "digest too long",
  46. "sha256-456402914e838a953e0cf80caa6adbe75383d9e63584a964f504a7bbb8f7aad9aaaaaaaaaa",
  47. "",
  48. ErrInvalidDigestFormat,
  49. },
  50. {
  51. "digest invalid chars",
  52. "../sha256-456402914e838a953e0cf80caa6adbe75383d9e63584a964f504a7bbb8f7a",
  53. "",
  54. ErrInvalidDigestFormat,
  55. },
  56. }
  57. for _, tc := range tests {
  58. t.Run(tc.name, func(t *testing.T) {
  59. t.Setenv("OLLAMA_MODELS", dir)
  60. got, err := GetBlobsPath(tc.digest)
  61. require.ErrorIs(t, tc.err, err, tc.name)
  62. assert.Equal(t, tc.expected, got, tc.name)
  63. })
  64. }
  65. }
  66. func TestParseModelPath(t *testing.T) {
  67. tests := []struct {
  68. name string
  69. arg string
  70. want ModelPath
  71. }{
  72. {
  73. "full path https",
  74. "https://example.com/ns/repo:tag",
  75. ModelPath{
  76. ProtocolScheme: "https",
  77. Registry: "example.com",
  78. Namespace: "ns",
  79. Repository: "repo",
  80. Tag: "tag",
  81. },
  82. },
  83. {
  84. "full path http",
  85. "http://example.com/ns/repo:tag",
  86. ModelPath{
  87. ProtocolScheme: "http",
  88. Registry: "example.com",
  89. Namespace: "ns",
  90. Repository: "repo",
  91. Tag: "tag",
  92. },
  93. },
  94. {
  95. "no protocol",
  96. "example.com/ns/repo:tag",
  97. ModelPath{
  98. ProtocolScheme: "https",
  99. Registry: "example.com",
  100. Namespace: "ns",
  101. Repository: "repo",
  102. Tag: "tag",
  103. },
  104. },
  105. {
  106. "no registry",
  107. "ns/repo:tag",
  108. ModelPath{
  109. ProtocolScheme: "https",
  110. Registry: DefaultRegistry,
  111. Namespace: "ns",
  112. Repository: "repo",
  113. Tag: "tag",
  114. },
  115. },
  116. {
  117. "no namespace",
  118. "repo:tag",
  119. ModelPath{
  120. ProtocolScheme: "https",
  121. Registry: DefaultRegistry,
  122. Namespace: DefaultNamespace,
  123. Repository: "repo",
  124. Tag: "tag",
  125. },
  126. },
  127. {
  128. "no tag",
  129. "repo",
  130. ModelPath{
  131. ProtocolScheme: "https",
  132. Registry: DefaultRegistry,
  133. Namespace: DefaultNamespace,
  134. Repository: "repo",
  135. Tag: DefaultTag,
  136. },
  137. },
  138. }
  139. for _, tc := range tests {
  140. t.Run(tc.name, func(t *testing.T) {
  141. got := ParseModelPath(tc.arg)
  142. if got != tc.want {
  143. t.Errorf("got: %q want: %q", got, tc.want)
  144. }
  145. })
  146. }
  147. }