Skip to content

Commit

Permalink
libimage/copier: wire ForceCompressionFormat for image copy
Browse files Browse the repository at this point in the history
Implement containers/image#2068 for
libimage/copier.

Signed-off-by: Aditya R <[email protected]>
  • Loading branch information
flouthoc committed Aug 11, 2023
1 parent 16ed6ef commit acd9733
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
5 changes: 5 additions & 0 deletions libimage/copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ type CopyOptions struct {
CompressionFormat *compression.Algorithm
// CompressionLevel specifies what compression level is used
CompressionLevel *int
// ForceCompressionFormat ensures that the compression algorithm set in
// CompressionFormat is used exclusively, and blobs of other compression
// algorithms are not reused.
ForceCompressionFormat bool

// containers-auth.json(5) file to use when authenticating against
// container registries.
Expand Down Expand Up @@ -294,6 +298,7 @@ func (r *Runtime) newCopier(options *CopyOptions) (*copier, error) {
c.imageCopyOptions.ProgressInterval = time.Second
}

c.imageCopyOptions.ForceCompressionFormat = options.ForceCompressionFormat
c.imageCopyOptions.ForceManifestMIMEType = options.ManifestMIMEType
c.imageCopyOptions.SourceCtx = c.systemContext
c.imageCopyOptions.DestinationCtx = c.systemContext
Expand Down
98 changes: 98 additions & 0 deletions libimage/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package libimage
import (
"context"
"os"
"path/filepath"
"testing"

"github.com/containers/common/pkg/config"
"github.com/containers/image/v5/pkg/compression"
"github.com/containers/image/v5/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -96,3 +100,97 @@ func TestPushOtherPlatform(t *testing.T) {
_, err = runtime.Push(ctx, "docker.io/library/alpine:latest", "docker-archive:"+tmp.Name(), pushOptions)
require.NoError(t, err)
}

func TestPushWithForceCompression(t *testing.T) {
runtime, cleanup := testNewRuntime(t)
defer cleanup()
ctx := context.Background()

// Prefetch alpine.
pullOptions := &PullOptions{}
pullOptions.Writer = os.Stdout
pullOptions.Architecture = "arm64"
pulledImages, err := runtime.Pull(ctx, "docker.io/library/alpine:latest", config.PullPolicyAlways, pullOptions)
require.NoError(t, err)
require.Len(t, pulledImages, 1)

data, err := pulledImages[0].Inspect(ctx, nil)
require.NoError(t, err)
require.Equal(t, "arm64", data.Architecture)

// Push newly pulled alpine to directory with uncompressed blobs
pushOptions := &PushOptions{}
pushOptions.SystemContext = &types.SystemContext{}
pushOptions.SystemContext.DirForceDecompress = true
pushOptions.Writer = os.Stdout
dirDest := t.TempDir()
_, err = runtime.Push(ctx, "docker.io/library/alpine:latest", "dir:"+dirDest, pushOptions)
require.NoError(t, err)

// Pull uncompressed alpine from `dir:dirDest` as source.
pullOptions = &PullOptions{}
pullOptions.Writer = os.Stdout
pullOptions.Architecture = "arm64"
pulledImages, err = runtime.Pull(ctx, "dir:"+dirDest, config.PullPolicyAlways, pullOptions)
require.NoError(t, err)
require.Len(t, pulledImages, 1)

// create `oci` image from uncompressed alpine.
pushOptions = &PushOptions{}
pushOptions.OciAcceptUncompressedLayers = true
pushOptions.Writer = os.Stdout
ociDest := t.TempDir()
_, err = runtime.Push(ctx, "docker.io/library/alpine:latest", "oci:"+ociDest, pushOptions)
require.NoError(t, err)

// blobs from first push
entries, err := os.ReadDir(filepath.Join(ociDest, "blobs", "sha256"))
require.NoError(t, err)
blobsFirstPush := []string{}
for _, e := range entries {
blobsFirstPush = append(blobsFirstPush, e.Name())
}

// Note: Compression is changed from `uncompressed` to `Gzip` but blobs
// should still be same since `ForceCompressionFormat` is `false`.
pushOptions = &PushOptions{}
pushOptions.Writer = os.Stdout
pushOptions.CompressionFormat = &compression.Gzip
pushOptions.ForceCompressionFormat = false
_, err = runtime.Push(ctx, "docker.io/library/alpine:latest", "oci:"+ociDest, pushOptions)
require.NoError(t, err)

// blobs from second push
entries, err = os.ReadDir(filepath.Join(ociDest, "blobs", "sha256"))
require.NoError(t, err)
blobsSecondPush := []string{}
for _, e := range entries {
blobsSecondPush = append(blobsSecondPush, e.Name())
}

// All blobs of first push should be equivalent to blobs of
// second push since same compression was used
assert.Equal(t, blobsSecondPush, blobsFirstPush)

// Note: Compression is changed from `uncompressed` to `Gzip` but blobs
// should still be same since `ForceCompressionFormat` is `false`.
pushOptions = &PushOptions{}
pushOptions.Writer = os.Stdout
pushOptions.CompressionFormat = &compression.Gzip
pushOptions.ForceCompressionFormat = true
_, err = runtime.Push(ctx, "docker.io/library/alpine:latest", "oci:"+ociDest, pushOptions)
require.NoError(t, err)

// collect blobs from third push
entries, err = os.ReadDir(filepath.Join(ociDest, "blobs", "sha256"))
require.NoError(t, err)
blobsThirdPush := []string{}
for _, e := range entries {
blobsThirdPush = append(blobsThirdPush, e.Name())
}

// All blobs of third push should not be equivalent to blobs of
// first push since new compression was used and `ForceCompressionFormat`
// was `true`.
assert.NotEqual(t, blobsThirdPush, blobsFirstPush)
}

0 comments on commit acd9733

Please sign in to comment.