Skip to content

Commit 8e807a7

Browse files
committed
Add test to ensure short IDs are properly resolved
1 parent 70d0e03 commit 8e807a7

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

pkg/distribution/distribution/normalize_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package distribution
22

33
import (
4+
"context"
5+
"io"
6+
"path/filepath"
7+
"strings"
48
"testing"
59

10+
"github.com/docker/model-runner/pkg/distribution/builder"
11+
"github.com/docker/model-runner/pkg/distribution/tarball"
612
"github.com/sirupsen/logrus"
713
)
814

@@ -276,6 +282,54 @@ func TestLooksLikeDigest(t *testing.T) {
276282
}
277283
}
278284

285+
func TestNormalizeModelNameWithIDResolution(t *testing.T) {
286+
// Create a client with a temporary store
287+
client, cleanup := createTestClient(t)
288+
defer cleanup()
289+
290+
// Load a test model to get a real ID
291+
testGGUFFile := filepath.Join("..", "assets", "dummy.gguf")
292+
modelID := loadTestModel(t, client, testGGUFFile)
293+
294+
// Extract the short ID (12 hex chars after "sha256:")
295+
if !strings.HasPrefix(modelID, "sha256:") {
296+
t.Fatalf("Expected model ID to start with 'sha256:', got: %s", modelID)
297+
}
298+
shortID := modelID[7:19] // Extract 12 chars after "sha256:"
299+
fullHex := strings.TrimPrefix(modelID, "sha256:")
300+
301+
tests := []struct {
302+
name string
303+
input string
304+
expected string
305+
}{
306+
{
307+
name: "short ID resolves to full ID",
308+
input: shortID,
309+
expected: modelID,
310+
},
311+
{
312+
name: "full hex (without sha256:) resolves to full ID",
313+
input: fullHex,
314+
expected: modelID,
315+
},
316+
{
317+
name: "full digest (with sha256:) returns as-is",
318+
input: modelID,
319+
expected: modelID,
320+
},
321+
}
322+
323+
for _, tt := range tests {
324+
t.Run(tt.name, func(t *testing.T) {
325+
result := client.normalizeModelName(tt.input)
326+
if result != tt.expected {
327+
t.Errorf("normalizeModelName(%q) = %q, want %q", tt.input, result, tt.expected)
328+
}
329+
})
330+
}
331+
}
332+
279333
// Helper function to create a test client with temp store
280334
func createTestClient(t *testing.T) (*Client, func()) {
281335
t.Helper()
@@ -300,3 +354,43 @@ func createTestClient(t *testing.T) (*Client, func()) {
300354

301355
return client, cleanup
302356
}
357+
358+
// Helper function to load a test model and return its ID
359+
func loadTestModel(t *testing.T, client *Client, ggufPath string) string {
360+
t.Helper()
361+
362+
// Load model using LoadModel
363+
pr, pw := io.Pipe()
364+
target, err := tarball.NewTarget(pw)
365+
if err != nil {
366+
t.Fatalf("Failed to create target: %v", err)
367+
}
368+
369+
done := make(chan error)
370+
var id string
371+
go func() {
372+
var err error
373+
id, err = client.LoadModel(pr, nil)
374+
done <- err
375+
}()
376+
377+
bldr, err := builder.FromGGUF(ggufPath)
378+
if err != nil {
379+
t.Fatalf("Failed to create builder from GGUF: %v", err)
380+
}
381+
382+
ctx := context.Background()
383+
if err := bldr.Build(ctx, target, nil); err != nil {
384+
t.Fatalf("Failed to build model: %v", err)
385+
}
386+
387+
if err := <-done; err != nil {
388+
t.Fatalf("Failed to load model: %v", err)
389+
}
390+
391+
if id == "" {
392+
t.Fatal("Model ID is empty")
393+
}
394+
395+
return id
396+
}

0 commit comments

Comments
 (0)