From e39407886d52a57daa8d3e05f31e56f0c088d002 Mon Sep 17 00:00:00 2001 From: yusing Date: Wed, 4 Jun 2025 23:00:53 +0800 Subject: [PATCH] fix: improved docker image parsing --- internal/docker/container_helper.go | 3 +++ internal/docker/container_test.go | 37 +++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/internal/docker/container_helper.go b/internal/docker/container_helper.go index a7da34e..5a8b495 100644 --- a/internal/docker/container_helper.go +++ b/internal/docker/container_helper.go @@ -48,10 +48,13 @@ func (c containerHelper) parseImage() *ContainerImage { im.Author = strings.Join(slashSep[:len(slashSep)-1], "/") im.Name = slashSep[len(slashSep)-1] } else { + im.Author = "library" im.Name = slashSep[0] } if len(colonSep) > 1 { im.Tag = colonSep[1] + } else { + im.Tag = "latest" } return im } diff --git a/internal/docker/container_test.go b/internal/docker/container_test.go index 4aa184a..b252e02 100644 --- a/internal/docker/container_test.go +++ b/internal/docker/container_test.go @@ -76,3 +76,40 @@ func TestContainerHostNetworkMode(t *testing.T) { }) } } + +func TestImageNameParsing(t *testing.T) { + tests := []struct { + full string + author string + image string + tag string + }{ + { + full: "ghcr.io/tensorchord/pgvecto-rs", + author: "ghcr.io/tensorchord", + image: "pgvecto-rs", + tag: "latest", + }, + { + full: "redis:latest", + author: "library", + image: "redis", + tag: "latest", + }, + { + full: "redis:7.4.0-alpine", + author: "library", + image: "redis", + tag: "7.4.0-alpine", + }, + } + for _, tt := range tests { + t.Run(tt.full, func(t *testing.T) { + helper := containerHelper{&container.SummaryTrimmed{Image: tt.full}} + im := helper.parseImage() + ExpectEqual(t, im.Author, tt.author) + ExpectEqual(t, im.Name, tt.image) + ExpectEqual(t, im.Tag, tt.tag) + }) + } +}