modelpath_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package server
  2. import (
  3. "errors"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  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. got, err := GetBlobsPath(tc.digest)
  62. require.ErrorIs(t, tc.err, err, tc.name)
  63. assert.Equal(t, tc.expected, got, tc.name)
  64. })
  65. }
  66. }
  67. func TestParseModelPath(t *testing.T) {
  68. tests := []struct {
  69. name string
  70. arg string
  71. want ModelPath
  72. }{
  73. {
  74. "full path https",
  75. "https://example.com/ns/repo:tag",
  76. ModelPath{
  77. ProtocolScheme: "https",
  78. Registry: "example.com",
  79. Namespace: "ns",
  80. Repository: "repo",
  81. Tag: "tag",
  82. },
  83. },
  84. {
  85. "full path http",
  86. "http://example.com/ns/repo:tag",
  87. ModelPath{
  88. ProtocolScheme: "http",
  89. Registry: "example.com",
  90. Namespace: "ns",
  91. Repository: "repo",
  92. Tag: "tag",
  93. },
  94. },
  95. {
  96. "no protocol",
  97. "example.com/ns/repo:tag",
  98. ModelPath{
  99. ProtocolScheme: "https",
  100. Registry: "example.com",
  101. Namespace: "ns",
  102. Repository: "repo",
  103. Tag: "tag",
  104. },
  105. },
  106. {
  107. "no registry",
  108. "ns/repo:tag",
  109. ModelPath{
  110. ProtocolScheme: "https",
  111. Registry: DefaultRegistry,
  112. Namespace: "ns",
  113. Repository: "repo",
  114. Tag: "tag",
  115. },
  116. },
  117. {
  118. "no namespace",
  119. "repo:tag",
  120. ModelPath{
  121. ProtocolScheme: "https",
  122. Registry: DefaultRegistry,
  123. Namespace: DefaultNamespace,
  124. Repository: "repo",
  125. Tag: "tag",
  126. },
  127. },
  128. {
  129. "no tag",
  130. "repo",
  131. ModelPath{
  132. ProtocolScheme: "https",
  133. Registry: DefaultRegistry,
  134. Namespace: DefaultNamespace,
  135. Repository: "repo",
  136. Tag: DefaultTag,
  137. },
  138. },
  139. }
  140. for _, tc := range tests {
  141. t.Run(tc.name, func(t *testing.T) {
  142. got := ParseModelPath(tc.arg)
  143. if got != tc.want {
  144. t.Errorf("got: %q want: %q", got, tc.want)
  145. }
  146. })
  147. }
  148. }
  149. func TestInsecureModelpath(t *testing.T) {
  150. mp := ParseModelPath("../../..:something")
  151. if _, err := mp.GetManifestPath(); !errors.Is(err, errModelPathInvalid) {
  152. t.Errorf("expected error: %v", err)
  153. }
  154. }