File: push_test.go

package info (click to toggle)
golang-github-containers-common 0.64.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 5,932 kB
  • sloc: makefile: 132; sh: 111
file content (192 lines) | stat: -rw-r--r-- 6,420 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//go:build !remote

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"
)

func TestPush(t *testing.T) {
	runtime := testNewRuntime(t)
	ctx := context.Background()

	// Prefetch alpine.
	pullOptions := &PullOptions{}
	pullOptions.Writer = os.Stdout
	_, err := runtime.Pull(ctx, "quay.io/libpod/alpine:latest", config.PullPolicyAlways, pullOptions)
	require.NoError(t, err)

	pushOptions := &PushOptions{}
	pushOptions.Writer = os.Stdout

	workdir := t.TempDir()

	for _, test := range []struct {
		source      string
		destination string
		expectError bool
	}{
		{"alpine", "dir:" + workdir + "/dir", false},
		{"alpine", "oci:" + workdir + "/oci", false},
		{"alpine", "oci-archive:" + workdir + "/oci-archive", false},
		{"alpine", "docker-archive:" + workdir + "/docker-archive", false},
		{"alpine", "containers-storage:localhost/another:alpine", false},
	} {
		_, err := runtime.Push(ctx, test.source, test.destination, pushOptions)
		if test.expectError {
			require.Error(t, err, "%v", test)
			continue
		}
		require.NoError(t, err, "%v", test)
		pulledImages, err := runtime.Pull(ctx, test.destination, config.PullPolicyAlways, pullOptions)
		require.NoError(t, err, "%v", test)
		require.Len(t, pulledImages, 1, "%v", test)
	}

	// Now there should only be two images: alpine in Docker format and
	// alpine in OCI format.
	listOptions := ListImagesOptions{SetListData: true}
	listedImages, err := runtime.ListImages(ctx, &listOptions)
	require.NoError(t, err, "error listing images")
	require.Len(t, listedImages, 2, "there should only be two images (alpine in Docke/OCI)")
	for _, image := range listedImages {
		require.NotNil(t, image.ListData.IsDangling, "IsDangling should be set")
	}

	// And now remove all of them.
	rmReports, rmErrors := runtime.RemoveImages(ctx, nil, nil)
	require.Len(t, rmErrors, 0)
	require.Len(t, rmReports, 2)

	for i, image := range listedImages {
		require.Equal(t, image.ID(), rmReports[i].ID)
		require.True(t, rmReports[i].Removed)
	}
}

func TestPushOtherPlatform(t *testing.T) {
	runtime := testNewRuntime(t)
	ctx := context.Background()

	// Prefetch alpine.
	pullOptions := &PullOptions{}
	pullOptions.Writer = os.Stdout
	pullOptions.Architecture = "arm64"
	pulledImages, err := runtime.Pull(ctx, "quay.io/libpod/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)

	pushOptions := &PushOptions{}
	pushOptions.Writer = os.Stdout
	tmp, err := os.CreateTemp(t.TempDir(), "")
	require.NoError(t, err)
	tmp.Close()
	_, err = runtime.Push(ctx, "quay.io/libpod/alpine:latest", "docker-archive:"+tmp.Name(), pushOptions)
	require.NoError(t, err)
}

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

	// Prefetch alpine.
	pullOptions := &PullOptions{}
	pullOptions.Writer = os.Stdout
	pullOptions.Architecture = "arm64"
	pulledImages, err := runtime.Pull(ctx, "quay.io/libpod/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, "quay.io/libpod/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, "quay.io/libpod/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, "quay.io/libpod/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, "quay.io/libpod/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)
}