path_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package model
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. )
  7. var testPaths = map[string]Path{
  8. "mistral:latest": {name: "mistral", tag: "latest"},
  9. "mistral": {name: "mistral"},
  10. "mistral:30B": {name: "mistral", tag: "30B"},
  11. "mistral:7b": {name: "mistral", tag: "7b"},
  12. "mistral:7b+Q4_0": {name: "mistral", tag: "7b", build: "Q4_0"},
  13. "mistral+KQED": {name: "mistral", build: "KQED"},
  14. "mistral.x-3:7b+Q4_0": {name: "mistral.x-3", tag: "7b", build: "Q4_0"},
  15. "mistral:7b+q4_0": {name: "mistral", tag: "7b", build: "Q4_0"},
  16. "llama2": {name: "llama2"},
  17. // invalid (includes fuzzing trophies)
  18. "+": {},
  19. "mistral:7b+Q4_0:latest": {},
  20. "mi tral": {},
  21. "x/y/z/foo": {},
  22. "/0": {},
  23. "0 /0": {},
  24. "0 /": {},
  25. "0/": {},
  26. ":": {},
  27. ":/0": {},
  28. "+0/00000": {},
  29. "0+.\xf2\x80\xf6\x9d00000\xe5\x99\xe6\xd900\xd90\xa60\x91\xdc0\xff\xbf\x99\xe800\xb9\xdc\xd6\xc300\x970\xfb\xfd0\xe0\x8a\xe1\xad\xd40\x9700\xa80\x980\xdd0000\xb00\x91000\xfe0\x89\x9b\x90\x93\x9f0\xe60\xf7\x84\xb0\x87\xa5\xff0\xa000\x9a\x85\xf6\x85\xfe\xa9\xf9\xe9\xde00\xf4\xe0\x8f\x81\xad\xde00\xd700\xaa\xe000000\xb1\xee0\x91": {},
  30. "0//0": {},
  31. "m+^^^": {},
  32. "file:///etc/passwd": {},
  33. "file:///etc/passwd:latest": {},
  34. "file:///etc/passwd:latest+u": {},
  35. strings.Repeat("a", MaxPathLength): {name: strings.Repeat("a", MaxPathLength)},
  36. strings.Repeat("a", MaxPathLength+1): {},
  37. }
  38. func TestPathParts(t *testing.T) {
  39. const wantNumParts = 5
  40. var p Path
  41. if len(p.Parts()) != wantNumParts {
  42. t.Errorf("Parts() = %d; want %d", len(p.Parts()), wantNumParts)
  43. }
  44. }
  45. func TestParsePath(t *testing.T) {
  46. for s, want := range testPaths {
  47. for _, prefix := range []string{"", "https://", "http://"} {
  48. // We should get the same results with or without the
  49. // http(s) prefixes
  50. s := prefix + s
  51. t.Run(s, func(t *testing.T) {
  52. got := ParsePath(s)
  53. if got != want {
  54. t.Errorf("ParsePath(%q) = %q; want %q", s, got, want)
  55. }
  56. // test round-trip
  57. if ParsePath(got.String()) != got {
  58. t.Errorf("String() = %s; want %s", got.String(), s)
  59. }
  60. if got.Valid() && got.Name() == "" {
  61. t.Errorf("Valid() = true; Name() = %q; want non-empty name", got.Name())
  62. } else if !got.Valid() && got.Name() != "" {
  63. t.Errorf("Valid() = false; Name() = %q; want empty name", got.Name())
  64. }
  65. })
  66. }
  67. }
  68. }
  69. func TestPathComplete(t *testing.T) {
  70. cases := []struct {
  71. in string
  72. complete bool
  73. completeWithoutBuild bool
  74. }{
  75. {"", false, false},
  76. {"example.com/mistral:7b+x", false, false},
  77. {"example.com/mistral:7b+Q4_0", false, false},
  78. {"mistral:7b+x", false, false},
  79. {"example.com/x/mistral:latest+Q4_0", true, true},
  80. {"example.com/x/mistral:latest", false, true},
  81. }
  82. for _, tt := range cases {
  83. t.Run(tt.in, func(t *testing.T) {
  84. p := ParsePath(tt.in)
  85. t.Logf("ParsePath(%q) = %#v", tt.in, p)
  86. if g := p.Complete(); g != tt.complete {
  87. t.Errorf("Complete(%q) = %v; want %v", tt.in, g, tt.complete)
  88. }
  89. if g := p.CompleteWithoutBuild(); g != tt.completeWithoutBuild {
  90. t.Errorf("CompleteWithoutBuild(%q) = %v; want %v", tt.in, g, tt.completeWithoutBuild)
  91. }
  92. })
  93. }
  94. }
  95. func TestPathStringVariants(t *testing.T) {
  96. cases := []struct {
  97. in string
  98. nameAndTag string
  99. nameTagAndBuild string
  100. }{
  101. {"x/y/z:8n+I", "z:8n", "z:8n+I"},
  102. {"x/y/z:8n", "z:8n", "z:8n"},
  103. }
  104. for _, tt := range cases {
  105. t.Run(tt.in, func(t *testing.T) {
  106. p := ParsePath(tt.in)
  107. t.Logf("ParsePath(%q) = %#v", tt.in, p)
  108. if g := p.NameAndTag(); g != tt.nameAndTag {
  109. t.Errorf("NameAndTag(%q) = %q; want %q", tt.in, g, tt.nameAndTag)
  110. }
  111. if g := p.NameTagAndBuild(); g != tt.nameTagAndBuild {
  112. t.Errorf("NameTagAndBuild(%q) = %q; want %q", tt.in, g, tt.nameTagAndBuild)
  113. }
  114. })
  115. }
  116. }
  117. func TestPathFull(t *testing.T) {
  118. const empty = "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/!(MISSING NAME):!(MISSING TAG)+!(MISSING BUILD)"
  119. cases := []struct {
  120. in string
  121. wantFull string
  122. }{
  123. {"", empty},
  124. {"example.com/mistral:7b+x", "!(MISSING DOMAIN)/example.com/mistral:7b+X"},
  125. {"example.com/mistral:7b+Q4_0", "!(MISSING DOMAIN)/example.com/mistral:7b+Q4_0"},
  126. {"example.com/x/mistral:latest", "example.com/x/mistral:latest+!(MISSING BUILD)"},
  127. {"example.com/x/mistral:latest+Q4_0", "example.com/x/mistral:latest+Q4_0"},
  128. {"mistral:7b+x", "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/mistral:7b+X"},
  129. {"mistral:7b+q4_0", "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/mistral:7b+Q4_0"},
  130. {"mistral:7b+Q4_0", "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/mistral:7b+Q4_0"},
  131. {"mistral:latest", "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/mistral:latest+!(MISSING BUILD)"},
  132. {"mistral", "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/mistral:!(MISSING TAG)+!(MISSING BUILD)"},
  133. {"mistral:30b", "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/mistral:30b+!(MISSING BUILD)"},
  134. }
  135. for _, tt := range cases {
  136. t.Run(tt.in, func(t *testing.T) {
  137. p := ParsePath(tt.in)
  138. t.Logf("ParsePath(%q) = %#v", tt.in, p)
  139. if g := p.Full(); g != tt.wantFull {
  140. t.Errorf("Full(%q) = %q; want %q", tt.in, g, tt.wantFull)
  141. }
  142. })
  143. }
  144. }
  145. func TestParsePathAllocs(t *testing.T) {
  146. // test allocations
  147. var r Path
  148. allocs := testing.AllocsPerRun(1000, func() {
  149. r = ParsePath("example.com/mistral:7b+Q4_0")
  150. })
  151. _ = r
  152. if allocs > 0 {
  153. t.Errorf("ParsePath allocs = %v; want 0", allocs)
  154. }
  155. }
  156. func BenchmarkParsePath(b *testing.B) {
  157. b.ReportAllocs()
  158. var r Path
  159. for i := 0; i < b.N; i++ {
  160. r = ParsePath("example.com/mistral:7b+Q4_0")
  161. }
  162. _ = r
  163. }
  164. func FuzzParsePath(f *testing.F) {
  165. f.Add("example.com/mistral:7b+Q4_0")
  166. f.Add("example.com/mistral:7b+q4_0")
  167. f.Add("example.com/mistral:7b+x")
  168. f.Add("x/y/z:8n+I")
  169. f.Fuzz(func(t *testing.T, s string) {
  170. r0 := ParsePath(s)
  171. if !r0.Valid() {
  172. if r0 != (Path{}) {
  173. t.Errorf("expected invalid path to be zero value; got %#v", r0)
  174. }
  175. t.Skipf("invalid path: %q", s)
  176. }
  177. for _, p := range r0.Parts() {
  178. if len(p) > MaxPathLength {
  179. t.Errorf("part too long: %q", p)
  180. }
  181. }
  182. if !strings.EqualFold(r0.String(), s) {
  183. t.Errorf("String() did not round-trip with case insensitivity: %q\ngot = %q\nwant = %q", s, r0.String(), s)
  184. }
  185. r1 := ParsePath(r0.String())
  186. if r0 != r1 {
  187. t.Errorf("round-trip mismatch: %+v != %+v", r0, r1)
  188. }
  189. })
  190. }
  191. func ExampleMerge() {
  192. r := Merge(
  193. ParsePath("mistral"),
  194. ParsePath("registry.ollama.com/XXXXX:latest+Q4_0"),
  195. )
  196. fmt.Println(r)
  197. // Output:
  198. // registry.ollama.com/mistral:latest+Q4_0
  199. }