Skip to content

Commit 7a8cece

Browse files
committed
Fix non-normalized name in list servers command.
1 parent ab87e3e commit 7a8cece

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

pkg/catalog_next/server.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ func ListServers(ctx context.Context, dao db.DAO, catalogRef string, filters []s
7171
return err
7272
}
7373

74+
ref, err := name.ParseReference(catalogRef)
75+
if err != nil {
76+
return fmt.Errorf("failed to parse oci-reference %s: %w", catalogRef, err)
77+
}
78+
if !oci.IsValidInputReference(ref) {
79+
return fmt.Errorf("reference %s must be a valid OCI reference without a digest", catalogRef)
80+
}
81+
82+
catalogRef = oci.FullNameWithoutDigest(ref)
83+
7484
// Get the catalog
7585
dbCatalog, err := dao.GetCatalog(ctx, catalogRef)
7686
if err != nil {

pkg/catalog_next/server_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,49 @@ func TestListServersCatalogNotFound(t *testing.T) {
600600
assert.Contains(t, err.Error(), "failed to get catalog")
601601
}
602602

603+
func TestListServersNormalizesCatalogRef(t *testing.T) {
604+
dao := setupTestDB(t)
605+
ctx := t.Context()
606+
607+
// Create a catalog with a normalized reference (what the db would store)
608+
catalogObj := Catalog{
609+
Ref: "test/catalog:latest",
610+
CatalogArtifact: CatalogArtifact{
611+
Title: "Test Catalog",
612+
Servers: []Server{
613+
{
614+
Type: workingset.ServerTypeImage,
615+
Image: "docker/server1:v1",
616+
Snapshot: &workingset.ServerSnapshot{
617+
Server: catalog.Server{
618+
Name: "my-server",
619+
},
620+
},
621+
},
622+
},
623+
},
624+
}
625+
626+
dbCat, err := catalogObj.ToDb()
627+
require.NoError(t, err)
628+
err = dao.UpsertCatalog(ctx, dbCat)
629+
require.NoError(t, err)
630+
631+
// Query with a non-normalized reference (without :latest tag)
632+
// This should still find the catalog because the code normalizes the ref
633+
output := captureStdout(t, func() {
634+
err := ListServers(ctx, dao, "test/catalog", []string{}, workingset.OutputFormatJSON)
635+
require.NoError(t, err)
636+
})
637+
638+
var result map[string]any
639+
err = json.Unmarshal([]byte(output), &result)
640+
require.NoError(t, err)
641+
642+
servers := result["servers"].([]any)
643+
assert.Len(t, servers, 1)
644+
}
645+
603646
func TestListServersYAMLFormat(t *testing.T) {
604647
dao := setupTestDB(t)
605648
ctx := t.Context()

0 commit comments

Comments
 (0)