dive/internal/source/platform_test.go
Alex Goodman 06f9a64c72
add regression tests for issues resolved by the stereoscope port (#740)
* add regression test for the reported tar-slip payload in extract

selects /etc/motd in a layer that also has etc/motd/../../../victim/.bashrc, the name from the report, and checks nothing lands outside the cwd while the real file is still extracted.

Refs #722

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* cover extract from a docker 25+ OCI layout save

runs the extract test against test-oci-docker-image (the blobs/sha256 layout docker 25+ writes) as well as the legacy save, so ctrl+e on those images actually writes the selected file.

Refs #687

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* add regression test for loading through the docker desktop context

Fake docker daemon on ~/.docker/run/docker.sock behind a current desktop-linux context, the way Docker Desktop sets it up, and the image loads from it without a pull.

Refs #542, #453

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* add regression test for pulling through a rootless docker context

Fake daemon on a rootless socket behind a current rootless context, missing the image until pulled. The pull and the save both land on that daemon, with no docker cli on PATH.

Refs #418

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* add regression test for docker API version negotiation

Fake docker 29 daemon that only takes API 1.44 to 1.52 and answers outside that with the daemon's own errors. A load through it works and every call uses the negotiated 1.52.

Refs #664

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* add regression test for DOCKER_TLS_VERIFY with certs in the docker config dir

DOCKER_HOST=tcp:// plus DOCKER_TLS_VERIFY=1, no DOCKER_CERT_PATH, ca/cert/key in the config dir, against a fake daemon that requires a client cert. The load goes through, and fails once the client cert is gone.

Refs #545

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* add regression test for the podman source on macOS

The podman source on darwin finds the machine API socket through a faked `podman machine inspect` and loads the image from a fake podman API there, with nothing else pointing at a socket.

Refs #432

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* add oci-dir layout load test

writes an OCI image layout dir to disk (what skopeo oci:<dir> or crane produce) and loads it through oci-dir: and --source oci-dir.

Refs #423

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* add registry source load test

pushes an image to an in-process go-containerregistry registry on loopback and loads it via registry:// and --source registry, no daemon involved.

Refs #406, #130

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* add multi-arch platform selection tests

builds a linux/amd64 + linux/arm64 OCI index (with a buildx style attestation entry) and checks --platform loads the matching image from an oci-dir and a registry, and fails for a platform the index lacks. Also runs the CLI with --platform end to end.

Refs #442

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* add container-engine config file test

runs dive on a scheme-less archive path with only container-engine set in the config file, with a docker control that has to fail. v0.13.1 read the --source flag's viper key and ignored container-engine.

Refs #643

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* add regression test for loading a local image from the docker daemon

a fake docker daemon that already holds the image serves inspect and save, the test checks the image is read from it and never pulled.

Refs #359

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* add regression test for podman pulling a missing image

a fake podman API (via CONTAINER_HOST) 404s the image until it is pulled, the test checks dive pulls it through the API and loads it, for podman:// and --source podman.

Refs #310

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* add regression test for containerd image store saves

- builds what `docker save` writes for an image in the containerd store: gzip layers (one under 1KB), index.json pointing at the multi-platform index with the other platform's blobs missing, and a buildkit attestation manifest
- loads it as a docker-archive and through a fake docker daemon (`dive test`), and checks the export matches the same image saved as a plain single manifest archive

Refs #718, #452

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* add regression test for kaniko image archives

writes the image with go-containerregistry's tarball writer (what kaniko's `--tarPath` uses, config named `sha256:<hex>` and `<hex>.tar.gz` layers) and checks `docker-archive://` reads it the same as a `docker save` of that image.

Refs #318

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* add regression test for whiteouts of missing paths in the TUI

layers whiteout paths no lower layer has (Windows images) and repeat a whiteout whose target is already gone (kaniko and apt's auxfiles), plus a char device. The file tree view model builds every layer selection without error and marks the real removal.

Refs #306

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* tidy go.sum for the in-process registry test

go-containerregistry's pkg/registry pulls golang.org/x/mod and x/tools sums into the test graph.

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

---------

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
2026-11-16 23:48:33 -05:00

73 lines
2.6 KiB
Go

package source
import (
"context"
"testing"
"github.com/anchore/stereoscope/pkg/file"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/layout"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// multiArchIndex is a linux/amd64 + linux/arm64 index with the "unknown/unknown" attestation manifest buildx adds
func multiArchIndex(t *testing.T) v1.ImageIndex {
t.Helper()
idx := mutate.IndexMediaType(empty.Index, types.OCIImageIndex)
for _, arch := range []string{"amd64", "arm64"} {
idx = mutate.AppendManifests(idx, mutate.IndexAddendum{
Add: archImage(t, arch),
Descriptor: v1.Descriptor{Platform: &v1.Platform{OS: "linux", Architecture: arch}},
})
}
return mutate.AppendManifests(idx, mutate.IndexAddendum{
Add: archImage(t, "attestation"),
Descriptor: v1.Descriptor{
Platform: &v1.Platform{OS: "unknown", Architecture: "unknown"},
Annotations: map[string]string{"vnd.docker.reference.type": "attestation-manifest"},
},
})
}
// regression for #442: --platform picks one image out of a multi-arch index, whatever the host arch is
func TestLoad_platform(t *testing.T) {
idx := multiArchIndex(t)
dir := t.TempDir()
_, err := layout.Write(dir, idx)
require.NoError(t, err)
host := startRegistry(t)
ref := host + "/dive/multiarch:1"
r, err := name.ParseReference(ref)
require.NoError(t, err)
require.NoError(t, remote.WriteIndex(r, idx))
for src, input := range map[string]string{OCIDir: "oci-dir:" + dir, Registry: "registry://" + ref} {
t.Run(src, func(t *testing.T) {
for arch, other := range map[string]string{"amd64": "arm64", "arm64": "amd64"} {
t.Run(arch, func(t *testing.T) {
img, err := Load(context.Background(), input, Config{Platform: "linux/" + arch})
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, img.Cleanup()) })
assert.Equal(t, arch, img.Metadata.Architecture)
tree := img.SquashedTree()
assert.True(t, tree.HasPath(file.Path("/"+arch)), "expected the %s image's files", arch)
assert.False(t, tree.HasPath(file.Path("/"+other)), "got the %s image's files", other)
})
}
// a platform the index doesn't have fails instead of quietly loading another one
_, err := Load(context.Background(), input, Config{Platform: "linux/s390x"})
require.Error(t, err)
})
}
}