ref_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package blob
  2. import (
  3. "strings"
  4. "testing"
  5. )
  6. // test refs
  7. const (
  8. refTooLong = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  9. )
  10. var testRefs = map[string]Ref{
  11. "mistral:latest": {name: "mistral", tag: "latest"},
  12. "mistral": {name: "mistral"},
  13. "mistral:30B": {name: "mistral", tag: "30B"},
  14. "mistral:7b": {name: "mistral", tag: "7b"},
  15. "mistral:7b+Q4_0": {name: "mistral", tag: "7b", build: "Q4_0"},
  16. "mistral+KQED": {name: "mistral", build: "KQED"},
  17. "mistral.x-3:7b+Q4_0": {name: "mistral.x-3", tag: "7b", build: "Q4_0"},
  18. "mistral:7b+q4_0": {name: "mistral", tag: "7b", build: "Q4_0"},
  19. "llama2": {name: "llama2"},
  20. // invalid (includes fuzzing trophies)
  21. "mistral:7b+Q4_0:latest": {},
  22. "mi tral": {},
  23. "x/y/z/foo": {},
  24. "/0": {},
  25. "0 /0": {},
  26. "0 /": {},
  27. "0/": {},
  28. ":": {},
  29. ":/0": {},
  30. "+0/00000": {},
  31. "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": {},
  32. "0//0": {},
  33. }
  34. func TestRefParts(t *testing.T) {
  35. const wantNumParts = 5
  36. var ref Ref
  37. if len(ref.Parts()) != wantNumParts {
  38. t.Errorf("Parts() = %d; want %d", len(ref.Parts()), wantNumParts)
  39. }
  40. }
  41. func TestParseRef(t *testing.T) {
  42. for s, want := range testRefs {
  43. t.Run(s, func(t *testing.T) {
  44. got := ParseRef(s)
  45. if got != want {
  46. t.Errorf("ParseRef(%q) = %q; want %q", s, got, want)
  47. }
  48. // test round-trip
  49. if ParseRef(got.String()) != got {
  50. t.Errorf("String() = %s; want %s", got.String(), s)
  51. }
  52. })
  53. }
  54. }
  55. func TestRefFull(t *testing.T) {
  56. const empty = "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/!(MISSING NAME):!(MISSING TAG)+!(MISSING BUILD)"
  57. cases := []struct {
  58. in string
  59. wantFull string
  60. }{
  61. {"", empty},
  62. {"example.com/mistral:7b+x", "!(MISSING DOMAIN)/example.com/mistral:7b+X"},
  63. {"example.com/mistral:7b+Q4_0", "!(MISSING DOMAIN)/example.com/mistral:7b+Q4_0"},
  64. {"example.com/x/mistral:latest", "example.com/x/mistral:latest+!(MISSING BUILD)"},
  65. {"example.com/x/mistral:latest+Q4_0", "example.com/x/mistral:latest+Q4_0"},
  66. {"mistral:7b+x", "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/mistral:7b+X"},
  67. {"mistral:7b+q4_0", "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/mistral:7b+Q4_0"},
  68. {"mistral:7b+Q4_0", "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/mistral:7b+Q4_0"},
  69. {"mistral:latest", "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/mistral:latest+!(MISSING BUILD)"},
  70. {"mistral", "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/mistral:!(MISSING TAG)+!(MISSING BUILD)"},
  71. {"mistral:30b", "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/mistral:30b+!(MISSING BUILD)"},
  72. }
  73. for _, tt := range cases {
  74. t.Run(tt.in, func(t *testing.T) {
  75. ref := ParseRef(tt.in)
  76. t.Logf("ParseRef(%q) = %#v", tt.in, ref)
  77. if g := ref.Full(); g != tt.wantFull {
  78. t.Errorf("Full(%q) = %q; want %q", tt.in, g, tt.wantFull)
  79. }
  80. })
  81. }
  82. }
  83. func TestParseRefAllocs(t *testing.T) {
  84. // test allocations
  85. allocs := testing.AllocsPerRun(1000, func() {
  86. ParseRef("example.com/mistral:7b+Q4_0")
  87. })
  88. if allocs > 0 {
  89. t.Errorf("ParseRef allocs = %v; want 0", allocs)
  90. }
  91. }
  92. func BenchmarkParseRef(b *testing.B) {
  93. b.ReportAllocs()
  94. var r Ref
  95. for i := 0; i < b.N; i++ {
  96. r = ParseRef("example.com/mistral:7b+Q4_0")
  97. }
  98. _ = r
  99. }
  100. func FuzzParseRef(f *testing.F) {
  101. f.Add("example.com/mistral:7b+Q4_0")
  102. f.Add("example.com/mistral:7b+q4_0")
  103. f.Add("example.com/mistral:7b+x")
  104. f.Add("x/y/z:8n+I")
  105. f.Fuzz(func(t *testing.T, s string) {
  106. r0 := ParseRef(s)
  107. if !r0.Valid() {
  108. if r0 != (Ref{}) {
  109. t.Errorf("expected invalid ref to be zero value; got %#v", r0)
  110. }
  111. t.Skipf("invalid ref: %q", s)
  112. }
  113. if !strings.EqualFold(r0.String(), s) {
  114. t.Errorf("String() did not round-trip with case insensitivity: %q\ngot = %q\nwant = %q", s, r0.String(), s)
  115. }
  116. r1 := ParseRef(r0.String())
  117. if r0 != r1 {
  118. t.Errorf("round-trip mismatch: %+v != %+v", r0, r1)
  119. }
  120. })
  121. }