Skip to content

Modern Docker Management, Designed for Everyone

refactor: use go-digest library for better digest management (#2164)

Kyle Mendell939cf2b61d92
diff --git a/backend/go.mod b/backend/go.mod
index d398a7211..7f63d5897 100644
--- a/backend/go.mod
+++ b/backend/go.mod
@@ -3,7 +3,6 @@ module github.com/getarcaneapp/arcane/backend
go 1.26.0
replace github.com/getarcaneapp/arcane/cli => ../cli
-
replace github.com/getarcaneapp/arcane/types => ../types
require (
diff --git a/backend/internal/services/container_registry_service.go b/backend/internal/services/container_registry_service.go
index f0007eef0..26f0bcb56 100644
--- a/backend/internal/services/container_registry_service.go
+++ b/backend/internal/services/container_registry_service.go
@@ -20,6 +20,7 @@ import (
"github.com/getarcaneapp/arcane/backend/pkg/utils"
"github.com/getarcaneapp/arcane/backend/pkg/utils/cache"
utilsdistribution "github.com/getarcaneapp/arcane/backend/pkg/utils/distribution"
+ "github.com/getarcaneapp/arcane/backend/pkg/utils/imagedigest"
"github.com/getarcaneapp/arcane/backend/pkg/utils/mapper"
utilsregistry "github.com/getarcaneapp/arcane/backend/pkg/utils/registry"
"github.com/getarcaneapp/arcane/types/containerregistry"
@@ -529,9 +530,9 @@ func (s *ContainerRegistryService) inspectImageDigestViaDaemonInternal(ctx conte
inspectResult, err := dockerClient.DistributionInspect(ctx, normalizedRef, client.DistributionInspectOptions{})
if err == nil {
- digest := strings.TrimSpace(string(inspectResult.Descriptor.Digest))
- if digest == "" {
- return nil, fmt.Errorf("distribution inspect returned empty digest for %s", normalizedRef)
+ digest, normalizeErr := imagedigest.Normalize(inspectResult.Descriptor.Digest.String())
+ if normalizeErr != nil {
+ return nil, fmt.Errorf("distribution inspect returned invalid digest for %s: %w", normalizedRef, normalizeErr)
}
return &registryDigestResult{
Digest: digest,
@@ -563,9 +564,9 @@ func (s *ContainerRegistryService) inspectImageDigestViaDaemonInternal(ctx conte
EncodedRegistryAuth: authHeader,
})
if err == nil {
- digest := strings.TrimSpace(string(inspectResult.Descriptor.Digest))
- if digest == "" {
- return nil, fmt.Errorf("distribution inspect returned empty digest for %s", normalizedRef)
+ digest, normalizeErr := imagedigest.Normalize(inspectResult.Descriptor.Digest.String())
+ if normalizeErr != nil {
+ return nil, fmt.Errorf("distribution inspect returned invalid digest for %s: %w", normalizedRef, normalizeErr)
}
return &registryDigestResult{
Digest: digest,
diff --git a/backend/internal/services/container_registry_service_test.go b/backend/internal/services/container_registry_service_test.go
index 90d296a90..3de11728e 100644
--- a/backend/internal/services/container_registry_service_test.go
+++ b/backend/internal/services/container_registry_service_test.go
@@ -343,6 +343,8 @@ func TestContainerRegistryService_TestRegistry_SkipsLoginForEmptyCredentials(t *
}
func TestContainerRegistryService_InspectImageDigest_AnonymousSuccess(t *testing.T) {
+ wantDigest := digest.FromString("anonymous-success").String()
+
svc := NewContainerRegistryService(nil, func(context.Context) (RegistryDaemonClient, error) {
return &fakeRegistryDaemonClient{
distributionInspectFn: func(ctx context.Context, imageRef string, options client.DistributionInspectOptions) (client.DistributionInspectResult, error) {
@@ -351,7 +353,7 @@ func TestContainerRegistryService_InspectImageDigest_AnonymousSuccess(t *testing
return client.DistributionInspectResult{
DistributionInspect: dockerregistry.DistributionInspect{
Descriptor: ocispec.Descriptor{
- Digest: digest.Digest("sha256:feedface"),
+ Digest: digest.Digest(wantDigest),
},
},
}, nil
@@ -361,7 +363,7 @@ func TestContainerRegistryService_InspectImageDigest_AnonymousSuccess(t *testing
result, err := svc.inspectImageDigestInternal(context.Background(), "registry.example.com:5443/team/app:1.2.3", nil)
require.NoError(t, err)
- assert.Equal(t, "sha256:feedface", result.Digest)
+ assert.Equal(t, wantDigest, result.Digest)
assert.Equal(t, "anonymous", result.AuthMethod)
assert.Equal(t, "registry.example.com:5443", result.AuthRegistry)
}
@@ -369,6 +371,7 @@ func TestContainerRegistryService_InspectImageDigest_AnonymousSuccess(t *testing
func TestContainerRegistryService_InspectImageDigest_RetriesWithStoredCredentials(t *testing.T) {
_, db := setupImageServiceAuthTest(t)
createTestPullRegistry(t, db, "https://index.docker.io/v1/", "docker-user", "docker-token")
+ wantDigest := digest.FromString("stored-credentials").String()
var calls int
svc := NewContainerRegistryService(db, func(context.Context) (RegistryDaemonClient, error) {
@@ -389,7 +392,7 @@ func TestContainerRegistryService_InspectImageDigest_RetriesWithStoredCredential
return client.DistributionInspectResult{
DistributionInspect: dockerregistry.DistributionInspect{
Descriptor: ocispec.Descriptor{
- Digest: digest.Digest("sha256:cafebabe"),
+ Digest: digest.Digest(wantDigest),
},
},
}, nil
@@ -400,16 +403,18 @@ func TestContainerRegistryService_InspectImageDigest_RetriesWithStoredCredential
result, err := svc.inspectImageDigestInternal(context.Background(), "registry-1.docker.io/library/nginx:latest", nil)
require.NoError(t, err)
assert.Equal(t, 2, calls)
- assert.Equal(t, "sha256:cafebabe", result.Digest)
+ assert.Equal(t, wantDigest, result.Digest)
assert.Equal(t, "credential", result.AuthMethod)
assert.Equal(t, "docker-user", result.AuthUsername)
assert.True(t, result.UsedCredential)
}
func TestContainerRegistryService_InspectImageDigest_FallsBackWhenDistributionNotFound(t *testing.T) {
+ wantDigest := digest.FromString("fallback-not-found").String()
+
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/v2/team/app/manifests/1.2.3" {
- w.Header().Set("Docker-Content-Digest", "sha256:fallback404")
+ w.Header().Set("Docker-Content-Digest", wantDigest)
w.WriteHeader(http.StatusOK)
return
}
@@ -436,15 +441,17 @@ func TestContainerRegistryService_InspectImageDigest_FallsBackWhenDistributionNo
result, err := svc.inspectImageDigestInternal(context.Background(), serverURL.Host+"/team/app:1.2.3", nil)
require.NoError(t, err)
assert.Equal(t, 1, calls)
- assert.Equal(t, "sha256:fallback404", result.Digest)
+ assert.Equal(t, wantDigest, result.Digest)
assert.Equal(t, "anonymous", result.AuthMethod)
assert.Equal(t, serverURL.Host, result.AuthRegistry)
}
func TestContainerRegistryService_InspectImageDigest_FallsBackWhenDistributionForbidden(t *testing.T) {
+ wantDigest := digest.FromString("fallback-forbidden").String()
+
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/v2/team/app/manifests/1.2.3" {
- w.Header().Set("Docker-Content-Digest", "sha256:fallback403")
+ w.Header().Set("Docker-Content-Digest", wantDigest)
w.WriteHeader(http.StatusOK)
return
}
@@ -471,13 +478,14 @@ func TestContainerRegistryService_InspectImageDigest_FallsBackWhenDistributionFo
result, err := svc.inspectImageDigestInternal(context.Background(), serverURL.Host+"/team/app:1.2.3", nil)
require.NoError(t, err)
assert.Equal(t, 1, calls)
- assert.Equal(t, "sha256:fallback403", result.Digest)
+ assert.Equal(t, wantDigest, result.Digest)
assert.Equal(t, "anonymous", result.AuthMethod)
assert.Equal(t, serverURL.Host, result.AuthRegistry)
}
func TestContainerRegistryService_InspectImageDigest_RetriesStoredCredentialsAfterRegistryAuth403(t *testing.T) {
_, db := setupImageServiceAuthTest(t)
+ wantDigest := digest.FromString("stored-credential-fallback").String()
var authHeaders []string
var tokenURL string
@@ -492,7 +500,7 @@ func TestContainerRegistryService_InspectImageDigest_RetriesStoredCredentialsAft
case 2:
w.WriteHeader(http.StatusForbidden)
case 3:
- w.Header().Set("Docker-Content-Digest", "sha256:stored-credential")
+ w.Header().Set("Docker-Content-Digest", wantDigest)
w.WriteHeader(http.StatusOK)
default:
t.Fatalf("unexpected manifest call %d", len(authHeaders))
@@ -535,7 +543,7 @@ func TestContainerRegistryService_InspectImageDigest_RetriesStoredCredentialsAft
result, err := svc.inspectImageDigestInternal(context.Background(), serverURL.Host+"/team/app:1.2.3", nil)
require.NoError(t, err)
- assert.Equal(t, "sha256:stored-credential", result.Digest)
+ assert.Equal(t, wantDigest, result.Digest)
assert.Equal(t, "credential", result.AuthMethod)
assert.Equal(t, "stored-user", result.AuthUsername)
assert.True(t, result.UsedCredential)
diff --git a/backend/internal/services/image_update_service.go b/backend/internal/services/image_update_service.go
index a2c8c65e5..1a58b11d8 100644
--- a/backend/internal/services/image_update_service.go
+++ b/backend/internal/services/image_update_service.go
@@ -12,6 +12,7 @@ import (
"github.com/getarcaneapp/arcane/backend/internal/database"
"github.com/getarcaneapp/arcane/backend/internal/models"
"github.com/getarcaneapp/arcane/backend/pkg/libarcane/crypto"
+ "github.com/getarcaneapp/arcane/backend/pkg/utils/imagedigest"
registry "github.com/getarcaneapp/arcane/backend/pkg/utils/registry"
"github.com/getarcaneapp/arcane/types/containerregistry"
"github.com/getarcaneapp/arcane/types/imageupdate"
@@ -211,7 +212,7 @@ func (s *ImageUpdateService) parseImageReference(imageRef string) *ImageParts {
// Fallback parser for cases where the official parser fails
func (s *ImageUpdateService) parseImageReferenceFallback(imageRef string) *ImageParts {
var registryHost, repository, tag string
- if strings.Contains(imageRef, "@sha256:") {
+ if _, ok := imagedigest.FromReferenceSuffix(imageRef); ok {
digestParts := strings.Split(imageRef, "@")
if len(digestParts) != 2 {
return nil
@@ -350,16 +351,16 @@ func (s *ImageUpdateService) inspectLocalImageSnapshotInternal(ctx context.Conte
// Extract all digests from RepoDigests
if len(inspectResponse.RepoDigests) > 0 {
for _, repoDigest := range inspectResponse.RepoDigests {
- // Format: repository@sha256:...
- digestParts := strings.Split(repoDigest, "@")
- if len(digestParts) == 2 {
- digest := digestParts[1]
- allDigests = append(allDigests, digest)
-
- // Use first digest as primary if not yet set
- if primaryDigest == "" {
- primaryDigest = digest
- }
+ digest, ok := imagedigest.FromReferenceSuffix(repoDigest)
+ if !ok {
+ continue
+ }
+
+ allDigests = append(allDigests, digest)
+
+ // Use first digest as primary if not yet set
+ if primaryDigest == "" {
+ primaryDigest = digest
}
}
}
diff --git a/backend/internal/services/image_update_service_test.go b/backend/internal/services/image_update_service_test.go
index 08ca6afd5..9862ad703 100644
--- a/backend/internal/services/image_update_service_test.go
+++ b/backend/internal/services/image_update_service_test.go
@@ -18,6 +18,7 @@ import (
glsqlite "github.com/glebarez/sqlite"
dockertypesimage "github.com/moby/moby/api/types/image"
"github.com/moby/moby/client"
+ digest "github.com/opencontainers/go-digest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
ref "go.podman.io/image/v5/docker/reference"
@@ -27,6 +28,9 @@ import (
// TestParseImageReference tests the parseImageReference function with various image formats
// This is used for digest-based update checking
func TestImageUpdateService_ParseImageReference(t *testing.T) {
+ alpineDigest := digest.FromString("alpine").String()
+ serviceDigest := digest.FromString("registry-app-service").String()
+
tests := []struct {
name string
imageRef string
@@ -92,14 +96,14 @@ func TestImageUpdateService_ParseImageReference(t *testing.T) {
},
{
name: "Image with digest",
- imageRef: "alpine@sha256:1234567890abcdef",
+ imageRef: "alpine@" + alpineDigest,
wantRegistry: "docker.io",
wantRepository: "library/alpine",
wantTag: "latest",
},
{
name: "Custom registry image with digest",
- imageRef: "registry.io/app/service@sha256:abcdef123456",
+ imageRef: "registry.io/app/service@" + serviceDigest,
wantRegistry: "registry.io",
wantRepository: "app/service",
wantTag: "latest",
@@ -205,9 +209,11 @@ func TestImageUpdateService_GetLocalImageDigestWithAll_Logic(t *testing.T) {
t.Run("Multiple digests in RepoDigests", func(t *testing.T) {
// This test demonstrates the expected behavior
// In practice, you'd use a mock Docker client
+ firstDigest := digest.FromString("redis-primary").String()
+ secondDigest := digest.FromString("redis-secondary").String()
repoDigests := []string{
- "docker.io/library/redis@sha256:abc123",
- "redis@sha256:def456",
+ "docker.io/library/redis@" + firstDigest,
+ "redis@" + secondDigest,
}
var allDigests []string
@@ -219,8 +225,8 @@ func TestImageUpdateService_GetLocalImageDigestWithAll_Logic(t *testing.T) {
}
assert.Len(t, allDigests, 2)
- assert.Contains(t, allDigests, "sha256:abc123")
- assert.Contains(t, allDigests, "sha256:def456")
+ assert.Contains(t, allDigests, firstDigest)
+ assert.Contains(t, allDigests, secondDigest)
})
}
@@ -399,8 +405,10 @@ func newImageUpdateFallbackServer(t *testing.T, repositoryTag, localDigest, remo
func TestImageUpdateService_CheckImageUpdate_UsesRegistryFallback(t *testing.T) {
db := setupImageUpdateTestDB(t)
+ localDigest := digest.FromString("localdigest").String()
+ remoteDigest := digest.FromString("remotedigest").String()
- server := newImageUpdateFallbackServer(t, "team/app:1.2.3", "sha256:localdigest", "sha256:remotedigest")
+ server := newImageUpdateFallbackServer(t, "team/app:1.2.3", localDigest, remoteDigest)
defer server.Close()
serverURL, err := url.Parse(server.URL)
@@ -425,20 +433,22 @@ func TestImageUpdateService_CheckImageUpdate_UsesRegistryFallback(t *testing.T)
require.NotNil(t, result)
assert.True(t, result.HasUpdate)
assert.Equal(t, "digest", result.UpdateType)
- assert.Equal(t, "sha256:localdigest", result.CurrentDigest)
- assert.Equal(t, "sha256:remotedigest", result.LatestDigest)
+ assert.Equal(t, localDigest, result.CurrentDigest)
+ assert.Equal(t, remoteDigest, result.LatestDigest)
assert.Equal(t, "anonymous", result.AuthMethod)
assert.Equal(t, serverURL.Host, result.AuthRegistry)
var saved models.ImageUpdateRecord
require.NoError(t, db.WithContext(context.Background()).Where("id = ?", "sha256:local-image-id").First(&saved).Error)
- assert.Equal(t, "sha256:remotedigest", stringPtrToString(saved.LatestDigest))
+ assert.Equal(t, remoteDigest, stringPtrToString(saved.LatestDigest))
}
func TestImageUpdateService_CheckMultipleImages_UsesRegistryFallback(t *testing.T) {
db := setupImageUpdateTestDB(t)
+ localDigest := digest.FromString("batchlocal").String()
+ remoteDigest := digest.FromString("batchremote").String()
- server := newImageUpdateFallbackServer(t, "team/app:1.2.3", "sha256:batchlocal", "sha256:batchremote")
+ server := newImageUpdateFallbackServer(t, "team/app:1.2.3", localDigest, remoteDigest)
defer server.Close()
serverURL, err := url.Parse(server.URL)
@@ -465,14 +475,14 @@ func TestImageUpdateService_CheckMultipleImages_UsesRegistryFallback(t *testing.
result := results[imageRef]
require.NotNil(t, result)
assert.True(t, result.HasUpdate)
- assert.Equal(t, "sha256:batchlocal", result.CurrentDigest)
- assert.Equal(t, "sha256:batchremote", result.LatestDigest)
+ assert.Equal(t, localDigest, result.CurrentDigest)
+ assert.Equal(t, remoteDigest, result.LatestDigest)
assert.Equal(t, "anonymous", result.AuthMethod)
assert.Equal(t, serverURL.Host, result.AuthRegistry)
var saved models.ImageUpdateRecord
require.NoError(t, db.WithContext(context.Background()).Where("id = ?", "sha256:local-image-id").First(&saved).Error)
- assert.Equal(t, "sha256:batchremote", stringPtrToString(saved.LatestDigest))
+ assert.Equal(t, remoteDigest, stringPtrToString(saved.LatestDigest))
}
// TestNotificationSentLogic tests the notification_sent flag behavior
diff --git a/backend/pkg/libarcane/startup/runtime_identity_nonlinux.go b/backend/pkg/libarcane/startup/runtime_identity_nonlinux.go
index 0f05c8931..2146784c7 100644
--- a/backend/pkg/libarcane/startup/runtime_identity_nonlinux.go
+++ b/backend/pkg/libarcane/startup/runtime_identity_nonlinux.go
@@ -8,6 +8,11 @@ import (
"runtime"
)
+var (
+ _ = runtimeIdentitySupplementaryGroupsInternal
+ _ = dockerSocketPathInternal
+)
+
func reexecWithRuntimeIdentityInternal(_ context.Context, req runtimeIdentityRequest) error {
return fmt.Errorf("runtime identity switching is only supported on linux, current platform is %s", runtime.GOOS)
}
diff --git a/backend/pkg/libarcane/updater/digest.go b/backend/pkg/libarcane/updater/digest.go
index 03afa21bd..d60da4f02 100644
--- a/backend/pkg/libarcane/updater/digest.go
+++ b/backend/pkg/libarcane/updater/digest.go
@@ -6,6 +6,7 @@ import (
"log/slog"
"strings"
+ "github.com/getarcaneapp/arcane/backend/pkg/utils/imagedigest"
"github.com/moby/moby/client"
ref "go.podman.io/image/v5/docker/reference"
@@ -99,11 +100,8 @@ func (c *DigestChecker) getLocalDigest(ctx context.Context, imageRef string) (st
// Try to get digest from RepoDigests
for _, rd := range inspect.RepoDigests {
- if strings.Contains(rd, "@sha256:") {
- parts := strings.Split(rd, "@")
- if len(parts) == 2 {
- return parts[1], nil
- }
+ if normalized, ok := imagedigest.FromReferenceSuffix(rd); ok {
+ return normalized, nil
}
}
diff --git a/backend/pkg/libarcane/updater/digest_test.go b/backend/pkg/libarcane/updater/digest_test.go
index 7e8eba444..64c67be6a 100644
--- a/backend/pkg/libarcane/updater/digest_test.go
+++ b/backend/pkg/libarcane/updater/digest_test.go
@@ -3,10 +3,13 @@ package updater
import (
"testing"
+ digest "github.com/opencontainers/go-digest"
"github.com/stretchr/testify/assert"
)
func TestParseImageRef(t *testing.T) {
+ redisDigest := digest.FromString("redis").String()
+
tests := []struct {
name string
imageRef string
@@ -30,7 +33,7 @@ func TestParseImageRef(t *testing.T) {
},
{
name: "digest reference defaults to latest tag",
- imageRef: "docker.io/library/redis@sha256:abcdef",
+ imageRef: "docker.io/library/redis@" + redisDigest,
wantHost: "docker.io",
wantRepo: "library/redis",
wantTag: "latest",
diff --git a/backend/pkg/utils/distribution/distribution.go b/backend/pkg/utils/distribution/distribution.go
index d44653f46..34b49ef80 100644
--- a/backend/pkg/utils/distribution/distribution.go
+++ b/backend/pkg/utils/distribution/distribution.go
@@ -11,6 +11,7 @@ import (
"strings"
"time"
+ "github.com/getarcaneapp/arcane/backend/pkg/utils/imagedigest"
ref "go.podman.io/image/v5/docker/reference"
)
@@ -339,13 +340,13 @@ func basicAuthHeaderInternal(username, token string) string {
}
func extractDigestFromHeadersInternal(headers http.Header) string {
- if digest := headers.Get("Docker-Content-Digest"); digest != "" {
- return digest
+ if normalized, err := imagedigest.Normalize(headers.Get("Docker-Content-Digest")); err == nil {
+ return normalized
}
etag := strings.Trim(headers.Get("ETag"), `"`)
- if strings.HasPrefix(etag, "sha256:") {
- return etag
+ if normalized, err := imagedigest.Normalize(etag); err == nil {
+ return normalized
}
return ""
diff --git a/backend/pkg/utils/distribution/distribution_test.go b/backend/pkg/utils/distribution/distribution_test.go
index 387dd0707..f9923bd0c 100644
--- a/backend/pkg/utils/distribution/distribution_test.go
+++ b/backend/pkg/utils/distribution/distribution_test.go
@@ -7,6 +7,7 @@ import (
"net/http/httptest"
"testing"
+ digest "github.com/opencontainers/go-digest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -56,6 +57,7 @@ func TestIsFallbackEligibleDaemonError(t *testing.T) {
func TestFetchDigestWithHTTPClient_FallsBackToGetOnMethodNotAllowed(t *testing.T) {
var headCalls int
var getCalls int
+ wantDigest := digest.FromString("method-not-allowed").String()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
@@ -64,7 +66,7 @@ func TestFetchDigestWithHTTPClient_FallsBackToGetOnMethodNotAllowed(t *testing.T
w.WriteHeader(http.StatusMethodNotAllowed)
case http.MethodGet:
getCalls++
- w.Header().Set("Docker-Content-Digest", "sha256:method-not-allowed")
+ w.Header().Set("Docker-Content-Digest", wantDigest)
w.WriteHeader(http.StatusOK)
default:
t.Fatalf("unexpected method %s", r.Method)
@@ -81,7 +83,7 @@ func TestFetchDigestWithHTTPClient_FallsBackToGetOnMethodNotAllowed(t *testing.T
server.Client(),
)
require.NoError(t, err)
- assert.Equal(t, "sha256:method-not-allowed", digest)
+ assert.Equal(t, wantDigest, digest)
assert.Equal(t, 1, headCalls)
assert.Equal(t, 1, getCalls)
}
diff --git a/backend/pkg/utils/imagedigest/digest.go b/backend/pkg/utils/imagedigest/digest.go
new file mode 100644
index 000000000..d6e698791
--- /dev/null
+++ b/backend/pkg/utils/imagedigest/digest.go
@@ -0,0 +1,31 @@
+package imagedigest
+
+import (
+ "fmt"
+ "strings"
+
+ digest "github.com/opencontainers/go-digest"
+)
+
+func Normalize(value string) (string, error) {
+ parsed, err := digest.Parse(strings.TrimSpace(value))
+ if err != nil {
+ return "", fmt.Errorf("invalid OCI digest %q: %w", value, err)
+ }
+
+ return parsed.String(), nil
+}
+
+func FromReferenceSuffix(ref string) (string, bool) {
+ _, digestValue, ok := strings.Cut(strings.TrimSpace(ref), "@")
+ if !ok {
+ return "", false
+ }
+
+ normalized, err := Normalize(digestValue)
+ if err != nil {
+ return "", false
+ }
+
+ return normalized, true
+}
diff --git a/backend/pkg/utils/imagedigest/digest_test.go b/backend/pkg/utils/imagedigest/digest_test.go
new file mode 100644
index 000000000..6015b9b3e
--- /dev/null
+++ b/backend/pkg/utils/imagedigest/digest_test.go
@@ -0,0 +1,35 @@
+package imagedigest
+
+import (
+ "testing"
+
+ digest "github.com/opencontainers/go-digest"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestNormalize(t *testing.T) {
+ want := digest.FromString("arcane").String()
+
+ got, err := Normalize(" " + want + " ")
+ require.NoError(t, err)
+ assert.Equal(t, want, got)
+}
+
+func TestNormalize_InvalidDigest(t *testing.T) {
+ _, err := Normalize("sha256:not-a-valid-digest")
+ require.Error(t, err)
+}
+
+func TestFromReferenceSuffix(t *testing.T) {
+ want := digest.FromString("arcane-reference").String()
+
+ got, ok := FromReferenceSuffix("docker.io/library/nginx@" + want)
+ require.True(t, ok)
+ assert.Equal(t, want, got)
+}
+
+func TestFromReferenceSuffix_InvalidDigest(t *testing.T) {
+ _, ok := FromReferenceSuffix("docker.io/library/nginx@sha256:bad")
+ assert.False(t, ok)
+}