2019-07-15 16:19:31 +00:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-03-22 20:22:57 +00:00
|
|
|
"encoding/json"
|
2023-10-09 10:07:50 +00:00
|
|
|
"fmt"
|
2022-03-22 20:22:57 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
2023-10-09 10:07:50 +00:00
|
|
|
"strings"
|
2019-07-15 16:19:31 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-03-22 20:22:57 +00:00
|
|
|
"github.com/containerd/containerd"
|
|
|
|
"github.com/containerd/containerd/content"
|
|
|
|
"github.com/containerd/containerd/content/local"
|
|
|
|
"github.com/containerd/containerd/images"
|
|
|
|
"github.com/containerd/containerd/platforms"
|
2023-07-07 11:40:24 +00:00
|
|
|
"github.com/docker/docker/api/types/image"
|
2019-07-15 16:19:31 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2022-03-22 20:22:57 +00:00
|
|
|
"github.com/docker/docker/testutil/registry"
|
|
|
|
"github.com/opencontainers/go-digest"
|
|
|
|
"github.com/opencontainers/image-spec/specs-go"
|
2023-05-08 09:57:52 +00:00
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
2023-10-09 10:07:50 +00:00
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/skip"
|
2019-07-15 16:19:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestImagePullPlatformInvalid(t *testing.T) {
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := setupTest(t)
|
|
|
|
|
2019-07-15 16:19:31 +00:00
|
|
|
client := testEnv.APIClient()
|
|
|
|
|
2023-07-07 11:40:24 +00:00
|
|
|
_, err := client.ImagePull(ctx, "docker.io/library/hello-world:latest", image.PullOptions{Platform: "foobar"})
|
2019-07-15 16:19:31 +00:00
|
|
|
assert.Assert(t, err != nil)
|
2024-01-21 12:20:01 +00:00
|
|
|
assert.Check(t, is.ErrorContains(err, "unknown operating system or architecture"))
|
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsInvalidParameter))
|
2019-07-15 16:19:31 +00:00
|
|
|
}
|
2022-03-22 20:22:57 +00:00
|
|
|
|
2023-05-08 09:57:52 +00:00
|
|
|
func createTestImage(ctx context.Context, t testing.TB, store content.Store) ocispec.Descriptor {
|
2022-03-22 20:22:57 +00:00
|
|
|
w, err := store.Writer(ctx, content.WithRef("layer"))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer w.Close()
|
|
|
|
|
|
|
|
// Empty layer with just a root dir
|
|
|
|
const layer = `./0000775000000000000000000000000014201045023007702 5ustar rootroot`
|
|
|
|
|
|
|
|
_, err = w.Write([]byte(layer))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
err = w.Commit(ctx, int64(len(layer)), digest.FromBytes([]byte(layer)))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
layerDigest := w.Digest()
|
2024-01-21 12:20:01 +00:00
|
|
|
assert.Check(t, w.Close())
|
2022-03-22 20:22:57 +00:00
|
|
|
|
2023-05-08 09:57:52 +00:00
|
|
|
img := ocispec.Image{
|
2023-04-28 22:51:30 +00:00
|
|
|
Platform: platforms.DefaultSpec(),
|
|
|
|
RootFS: ocispec.RootFS{Type: "layers", DiffIDs: []digest.Digest{layerDigest}},
|
|
|
|
Config: ocispec.ImageConfig{WorkingDir: "/"},
|
2022-03-22 20:22:57 +00:00
|
|
|
}
|
|
|
|
imgJSON, err := json.Marshal(img)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
w, err = store.Writer(ctx, content.WithRef("config"))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer w.Close()
|
|
|
|
_, err = w.Write(imgJSON)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.NilError(t, w.Commit(ctx, int64(len(imgJSON)), digest.FromBytes(imgJSON)))
|
|
|
|
|
|
|
|
configDigest := w.Digest()
|
2024-01-21 12:20:01 +00:00
|
|
|
assert.Check(t, w.Close())
|
2022-03-22 20:22:57 +00:00
|
|
|
|
|
|
|
info, err := store.Info(ctx, layerDigest)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2023-05-08 09:57:52 +00:00
|
|
|
manifest := ocispec.Manifest{
|
2022-03-22 20:22:57 +00:00
|
|
|
Versioned: specs.Versioned{
|
|
|
|
SchemaVersion: 2,
|
|
|
|
},
|
|
|
|
MediaType: images.MediaTypeDockerSchema2Manifest,
|
2023-05-08 09:57:52 +00:00
|
|
|
Config: ocispec.Descriptor{
|
2022-03-22 20:22:57 +00:00
|
|
|
MediaType: images.MediaTypeDockerSchema2Config,
|
|
|
|
Digest: configDigest,
|
|
|
|
Size: int64(len(imgJSON)),
|
|
|
|
},
|
2023-05-08 09:57:52 +00:00
|
|
|
Layers: []ocispec.Descriptor{{
|
2022-03-22 20:22:57 +00:00
|
|
|
MediaType: images.MediaTypeDockerSchema2Layer,
|
|
|
|
Digest: layerDigest,
|
|
|
|
Size: info.Size,
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
manifestJSON, err := json.Marshal(manifest)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
w, err = store.Writer(ctx, content.WithRef("manifest"))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer w.Close()
|
|
|
|
_, err = w.Write(manifestJSON)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.NilError(t, w.Commit(ctx, int64(len(manifestJSON)), digest.FromBytes(manifestJSON)))
|
|
|
|
|
|
|
|
manifestDigest := w.Digest()
|
2024-01-21 12:20:01 +00:00
|
|
|
assert.Check(t, w.Close())
|
2022-03-22 20:22:57 +00:00
|
|
|
|
2023-05-08 09:57:52 +00:00
|
|
|
return ocispec.Descriptor{
|
2022-03-22 20:22:57 +00:00
|
|
|
MediaType: images.MediaTypeDockerSchema2Manifest,
|
|
|
|
Digest: manifestDigest,
|
|
|
|
Size: int64(len(manifestJSON)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that pulling by an already cached digest but for a different ref (that should not have that digest)
|
|
|
|
// verifies with the remote that the digest exists in that repo.
|
2024-01-21 12:20:01 +00:00
|
|
|
func TestImagePullStoredDigestForOtherRepo(t *testing.T) {
|
2022-10-19 11:22:14 +00:00
|
|
|
skip.If(t, testEnv.IsRemoteDaemon, "cannot run daemon when remote daemon")
|
2023-06-14 09:46:00 +00:00
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType == "windows", "We don't run a test registry on Windows")
|
2022-10-19 11:22:14 +00:00
|
|
|
skip.If(t, testEnv.IsRootless, "Rootless has a different view of localhost (needed for test registry access)")
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := setupTest(t)
|
2022-03-22 20:22:57 +00:00
|
|
|
|
|
|
|
reg := registry.NewV2(t, registry.WithStdout(os.Stdout), registry.WithStderr(os.Stderr))
|
|
|
|
defer reg.Close()
|
|
|
|
reg.WaitReady(t)
|
|
|
|
|
|
|
|
// First create an image and upload it to our local registry
|
|
|
|
// Then we'll download it so that we can make sure the content is available in dockerd's manifest cache.
|
|
|
|
// Then we'll try to pull the same digest but with a different repo name.
|
|
|
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
store, err := local.NewStore(dir)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
desc := createTestImage(ctx, t, store)
|
|
|
|
|
|
|
|
remote := path.Join(registry.DefaultURL, "test:latest")
|
|
|
|
|
|
|
|
c8dClient, err := containerd.New("", containerd.WithServices(containerd.WithContentStore(store)))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
c8dClient.Push(ctx, remote, desc)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
client := testEnv.APIClient()
|
2023-07-07 11:40:24 +00:00
|
|
|
rdr, err := client.ImagePull(ctx, remote, image.PullOptions{})
|
2022-03-22 20:22:57 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
defer rdr.Close()
|
2024-01-21 12:20:01 +00:00
|
|
|
_, err = io.Copy(io.Discard, rdr)
|
|
|
|
assert.Check(t, err)
|
2022-03-22 20:22:57 +00:00
|
|
|
|
|
|
|
// Now, pull a totally different repo with a the same digest
|
2023-07-07 11:40:24 +00:00
|
|
|
rdr, err = client.ImagePull(ctx, path.Join(registry.DefaultURL, "other:image@"+desc.Digest.String()), image.PullOptions{})
|
2022-03-22 20:22:57 +00:00
|
|
|
if rdr != nil {
|
2024-01-21 12:20:01 +00:00
|
|
|
assert.Check(t, rdr.Close())
|
2022-03-22 20:22:57 +00:00
|
|
|
}
|
|
|
|
assert.Assert(t, err != nil, "Expected error, got none: %v", err)
|
|
|
|
assert.Assert(t, errdefs.IsNotFound(err), err)
|
2024-01-21 12:20:01 +00:00
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
|
2022-03-22 20:22:57 +00:00
|
|
|
}
|
2023-10-09 10:07:50 +00:00
|
|
|
|
|
|
|
// TestImagePullNonExisting pulls non-existing images from the central registry, with different
|
|
|
|
// combinations of implicit tag and library prefix.
|
|
|
|
func TestImagePullNonExisting(t *testing.T) {
|
|
|
|
ctx := setupTest(t)
|
|
|
|
|
|
|
|
for _, ref := range []string{
|
|
|
|
"asdfasdf:foobar",
|
|
|
|
"library/asdfasdf:foobar",
|
|
|
|
"asdfasdf",
|
|
|
|
"asdfasdf:latest",
|
|
|
|
"library/asdfasdf",
|
|
|
|
"library/asdfasdf:latest",
|
|
|
|
} {
|
|
|
|
ref := ref
|
|
|
|
all := strings.Contains(ref, ":")
|
|
|
|
t.Run(ref, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client := testEnv.APIClient()
|
2023-07-07 11:40:24 +00:00
|
|
|
rdr, err := client.ImagePull(ctx, ref, image.PullOptions{
|
2023-10-09 10:07:50 +00:00
|
|
|
All: all,
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
rdr.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedMsg := fmt.Sprintf("pull access denied for %s, repository does not exist or may require 'docker login'", "asdfasdf")
|
2024-01-21 12:20:01 +00:00
|
|
|
assert.Check(t, is.ErrorContains(err, expectedMsg))
|
2023-10-09 10:07:50 +00:00
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
|
|
|
|
if all {
|
|
|
|
// pull -a on a nonexistent registry should fall back as well
|
|
|
|
assert.Check(t, !strings.Contains(err.Error(), "unauthorized"), `message should not contain "unauthorized"`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|