Merge pull request #531 from vrothberg/libimage-tests

libimage: add unit tests
This commit is contained in:
OpenShift Merge Robot 2021-05-07 05:21:26 -04:00 committed by GitHub
commit 3991f443f0
15 changed files with 134 additions and 4 deletions

View File

@ -3,20 +3,21 @@
testing_task:
env:
GOPATH: "/go"
CIRRUS_WORKING_DIR: "${GOPATH}/src/github.com/containers/common"
GOSRC: "$CIRRUS_WORKING_DIR"
container:
image: "docker.io/library/golang"
image: "registry.fedoraproject.org/fedora:34"
cpu: 2
memory: 2
test_script:
- apt-get update && apt-get -qq install libdevmapper-dev libbtrfs-dev libseccomp-dev
- dnf install -y go containers-common device-mapper-devel libseccomp-devel btrfs-progs-devel
- make vendor
- make build
- make install.tools
- make validate
- make vendor
- make test

View File

@ -108,6 +108,7 @@ test: test-unit
.PHONY: test-unit
test-unit:
go test --tags $(BUILDTAGS) -v ./libimage
go test --tags $(BUILDTAGS) -v $(PROJECT)/pkg/...
go test --tags remote,seccomp,$(BUILDTAGS) -v $(PROJECT)/pkg/...

View File

@ -177,7 +177,12 @@ func (r *Runtime) copyFromDefault(ctx context.Context, ref types.ImageReference,
imageName = "sha256:" + storageName[1:]
} else {
storageName = manifest.Annotations["org.opencontainers.image.ref.name"]
imageName = storageName
named, err := NormalizeName(storageName)
if err != nil {
return nil, err
}
imageName = named.String()
storageName = imageName
}
default:

View File

@ -0,0 +1,67 @@
package libimage
import (
"context"
"fmt"
"os"
"testing"
"github.com/containers/common/pkg/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPull(t *testing.T) {
runtime, cleanup := testNewRuntime(t)
defer cleanup()
ctx := context.Background()
pullOptions := &PullOptions{}
pullOptions.Writer = os.Stdout
for _, test := range []struct {
input string
expectError bool
numImages int
names []string
}{
// DOCKER ARCHIVE
{"docker-archive:testdata/docker-name-only.tar.xz", false, 1, []string{"localhost/pretty-empty:latest"}},
{"docker-archive:testdata/docker-registry-name.tar.xz", false, 1, []string{"example.com/empty:latest"}},
{"docker-archive:testdata/docker-two-names.tar.xz", false, 2, []string{"example.com/empty:latest", "localhost/pretty-empty:latest"}},
{"docker-archive:testdata/docker-two-images.tar.xz", true, 0, nil}, // LOAD must be used here
{"docker-archive:testdata/docker-unnamed.tar.xz", false, 1, []string{"ec9293436c2e66da44edb9efb8d41f6b13baf62283ebe846468bc992d76d7951"}},
// OCI ARCHIVE
{"oci-archive:testdata/oci-name-only.tar.gz", false, 1, []string{"localhost/pretty-empty:latest"}},
{"oci-archive:testdata/oci-non-docker-name.tar.gz", true, 0, nil},
{"oci-archive:testdata/oci-registry-name.tar.gz", false, 1, []string{"example.com/empty:latest"}},
{"oci-archive:testdata/oci-unnamed.tar.gz", false, 1, []string{"5c8aca8137ac47e84c69ae93ce650ce967917cc001ba7aad5494073fac75b8b6"}},
// REGISTRY
{"alpine", false, 1, []string{"docker.io/library/alpine:latest"}},
{"docker://alpine", false, 1, []string{"docker.io/library/alpine:latest"}},
{"docker.io/library/alpine", false, 1, []string{"docker.io/library/alpine:latest"}},
{"docker://docker.io/library/alpine", false, 1, []string{"docker.io/library/alpine:latest"}},
} {
pulledImages, err := runtime.Pull(ctx, test.input, config.PullPolicyAlways, pullOptions)
if test.expectError {
require.Error(t, err, test.input)
continue
}
require.NoError(t, err, test.input)
require.Len(t, pulledImages, test.numImages)
// Now lookup an image with the expected name and compare IDs.
image, resolvedName, err := runtime.LookupImage(test.names[0], nil)
require.NoError(t, err, test.input)
require.Equal(t, test.names[0], resolvedName, fmt.Sprintf("%v", image.Names()))
require.Equal(t, pulledImages[0].ID(), image.ID(), test.input)
// Now remove the image.
rmReports, rmErrors := runtime.RemoveImages(ctx, test.names, &RemoveImagesOptions{Force: true})
require.Len(t, rmErrors, 0)
require.Len(t, rmReports, 1)
assert.Equal(t, image.ID(), rmReports[0].ID)
assert.True(t, rmReports[0].Removed)
}
}

View File

@ -0,0 +1,47 @@
package libimage
import (
"io/ioutil"
"os"
"testing"
"github.com/containers/image/v5/types"
"github.com/containers/storage"
"github.com/containers/storage/pkg/reexec"
"github.com/stretchr/testify/require"
)
func TestMain(m *testing.M) {
if reexec.Init() {
return
}
os.Exit(m.Run())
}
// Create a new Runtime that can be used for testing. The second return value
// is a clean-up function that should be called by users to make sure all
// temporary test data gets removed.
func testNewRuntime(t *testing.T) (runtime *Runtime, cleanup func()) {
workdir, err := ioutil.TempDir("", "testStorageRuntime")
require.NoError(t, err)
storeOptions := &storage.StoreOptions{
RunRoot: workdir,
GraphRoot: workdir,
GraphDriverName: "vfs",
}
// Make sure that the tests do not use the host's registries.conf.
systemContext := &types.SystemContext{
SystemRegistriesConfPath: "testdata/registries.conf",
SystemRegistriesConfDirPath: "/dev/null",
}
runtime, err = RuntimeFromStoreOptions(&RuntimeOptions{SystemContext: systemContext}, storeOptions)
require.NoError(t, err)
cleanup = func() {
runtime.Shutdown(true)
_ = os.RemoveAll(workdir)
}
return runtime, cleanup
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,9 @@
unqualified-search-registries=["docker.io", "quay.io"]
#[[registry]]
# In Nov. 2020, Docker rate-limits image pulling. To avoid hitting these
# limits while testing, always use the google mirror for qualified and
# unqualified `docker.io` images.
# Ref: https://cloud.google.com/container-registry/docs/pulling-cached-images
#prefix="docker.io"
#location="mirror.gcr.io"