modelpath_test.go 3.1 KB

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