From d72dd628ad38fee905ec34d21f0d2e589707a709 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Tue, 1 Aug 2023 00:54:56 +0200 Subject: [PATCH] Don't hard-code a single digest in configBlobImageSource MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ... so that newOCI1ImageSource can support other configs. Should not change (test) behavior. Signed-off-by: Miloslav Trmač --- internal/image/docker_schema2_test.go | 15 +++++++++++---- internal/image/oci_test.go | 11 ++++++++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/internal/image/docker_schema2_test.go b/internal/image/docker_schema2_test.go index f2f8a3f1d0..4fe2e81090 100644 --- a/internal/image/docker_schema2_test.go +++ b/internal/image/docker_schema2_test.go @@ -22,6 +22,8 @@ import ( "golang.org/x/exp/slices" ) +const commonFixtureConfigDigest = "sha256:9ca4bda0a6b3727a6ffcc43e981cad0f24e2ec79d338f6ba325b4dfd0756fb8f" + func manifestSchema2FromFixture(t *testing.T, src types.ImageSource, fixture string, mustFail bool) genericManifest { manifest, err := os.ReadFile(filepath.Join("fixtures", fixture)) require.NoError(t, err) @@ -39,7 +41,7 @@ func manifestSchema2FromComponentsLikeFixture(configBlob []byte) genericManifest return manifestSchema2FromComponents(manifest.Schema2Descriptor{ MediaType: "application/octet-stream", Size: 5940, - Digest: "sha256:9ca4bda0a6b3727a6ffcc43e981cad0f24e2ec79d338f6ba325b4dfd0756fb8f", + Digest: commonFixtureConfigDigest, }, nil, configBlob, []manifest.Schema2Descriptor{ { MediaType: "application/vnd.docker.image.rootfs.diff.tar.gzip", @@ -114,7 +116,7 @@ func TestManifestSchema2ConfigInfo(t *testing.T) { } { assert.Equal(t, types.BlobInfo{ Size: 5940, - Digest: "sha256:9ca4bda0a6b3727a6ffcc43e981cad0f24e2ec79d338f6ba325b4dfd0756fb8f", + Digest: commonFixtureConfigDigest, MediaType: "application/octet-stream", }, m.ConfigInfo()) } @@ -123,11 +125,12 @@ func TestManifestSchema2ConfigInfo(t *testing.T) { // configBlobImageSource allows testing various GetBlob behaviors in .ConfigBlob() type configBlobImageSource struct { mocks.ForbiddenImageSource // We inherit almost all of the methods, which just panic() + expectedDigest digest.Digest f func() (io.ReadCloser, int64, error) } func (f configBlobImageSource) GetBlob(ctx context.Context, info types.BlobInfo, _ types.BlobInfoCache) (io.ReadCloser, int64, error) { - if info.Digest.String() != "sha256:9ca4bda0a6b3727a6ffcc43e981cad0f24e2ec79d338f6ba325b4dfd0756fb8f" { + if info.Digest != f.expectedDigest { panic("Unexpected digest in GetBlob") } return f.f() @@ -163,7 +166,10 @@ func TestManifestSchema2ConfigBlob(t *testing.T) { } { var src types.ImageSource if c.cbISfn != nil { - src = configBlobImageSource{f: c.cbISfn} + src = configBlobImageSource{ + expectedDigest: commonFixtureConfigDigest, + f: c.cbISfn, + } } else { src = nil } @@ -350,6 +356,7 @@ func newSchema2ImageSource(t *testing.T, dockerRef string) *schema2ImageSource { return &schema2ImageSource{ configBlobImageSource: configBlobImageSource{ + expectedDigest: commonFixtureConfigDigest, f: func() (io.ReadCloser, int64, error) { return io.NopCloser(bytes.NewReader(realConfigJSON)), int64(len(realConfigJSON)), nil }, diff --git a/internal/image/oci_test.go b/internal/image/oci_test.go index 84398dcf34..0531f842ee 100644 --- a/internal/image/oci_test.go +++ b/internal/image/oci_test.go @@ -15,6 +15,7 @@ import ( "github.com/containers/image/v5/internal/testing/mocks" "github.com/containers/image/v5/manifest" "github.com/containers/image/v5/types" + "github.com/opencontainers/go-digest" imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -33,7 +34,7 @@ func manifestOCI1FromComponentsLikeFixture(configBlob []byte) genericManifest { return manifestOCI1FromComponents(imgspecv1.Descriptor{ MediaType: imgspecv1.MediaTypeImageConfig, Size: 5940, - Digest: "sha256:9ca4bda0a6b3727a6ffcc43e981cad0f24e2ec79d338f6ba325b4dfd0756fb8f", + Digest: commonFixtureConfigDigest, Annotations: map[string]string{ "test-annotation-1": "one", }, @@ -117,7 +118,7 @@ func TestManifestOCI1ConfigInfo(t *testing.T) { } { assert.Equal(t, types.BlobInfo{ Size: 5940, - Digest: "sha256:9ca4bda0a6b3727a6ffcc43e981cad0f24e2ec79d338f6ba325b4dfd0756fb8f", + Digest: commonFixtureConfigDigest, Annotations: map[string]string{ "test-annotation-1": "one", }, @@ -156,7 +157,10 @@ func TestManifestOCI1ConfigBlob(t *testing.T) { } { var src types.ImageSource if c.cbISfn != nil { - src = configBlobImageSource{f: c.cbISfn} + src = configBlobImageSource{ + expectedDigest: commonFixtureConfigDigest, + f: c.cbISfn, + } } else { src = nil } @@ -362,6 +366,7 @@ func newOCI1ImageSource(t *testing.T, dockerRef string) *oci1ImageSource { return &oci1ImageSource{ configBlobImageSource: configBlobImageSource{ + expectedDigest: digest.FromBytes(realConfigJSON), f: func() (io.ReadCloser, int64, error) { return io.NopCloser(bytes.NewReader(realConfigJSON)), int64(len(realConfigJSON)), nil },