Skip to content

Commit

Permalink
Don't hard-code a single digest in configBlobImageSource
Browse files Browse the repository at this point in the history
... so that newOCI1ImageSource can support other configs.

Should not change (test) behavior.

Signed-off-by: Miloslav Trmač <[email protected]>
  • Loading branch information
mtrmac committed Aug 4, 2023
1 parent 7f811a3 commit d72dd62
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
15 changes: 11 additions & 4 deletions internal/image/docker_schema2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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",
Expand Down Expand Up @@ -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())
}
Expand All @@ -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()
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
},
Expand Down
11 changes: 8 additions & 3 deletions internal/image/oci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
},
Expand Down Expand Up @@ -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",
},
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
},
Expand Down

0 comments on commit d72dd62

Please sign in to comment.