Skip to content

Commit

Permalink
Migrate image tests to new tooling
Browse files Browse the repository at this point in the history
Signed-off-by: apostasie <[email protected]>
  • Loading branch information
apostasie committed Sep 25, 2024
1 parent 1b7190c commit 741bc21
Show file tree
Hide file tree
Showing 17 changed files with 1,601 additions and 1,075 deletions.
48 changes: 48 additions & 0 deletions cmd/nerdctl/helpers/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@
package helpers

import (
"context"
"os"
"path/filepath"
"testing"

"gotest.tools/v3/assert"

containerd "github.com/containerd/containerd/v2/client"
"github.com/containerd/containerd/v2/core/content"

"github.com/containerd/nerdctl/v2/pkg/buildkitutil"
"github.com/containerd/nerdctl/v2/pkg/testutil"
)

func CreateBuildContext(t *testing.T, dockerfile string) string {
Expand All @@ -30,3 +37,44 @@ func CreateBuildContext(t *testing.T, dockerfile string) string {
assert.NilError(t, err)
return tmpDir
}

func RmiAll(base *testutil.Base) {
base.T.Logf("Pruning images")
imageIDs := base.Cmd("images", "--no-trunc", "-a", "-q").OutLines()
// remove empty output line at the end
imageIDs = imageIDs[:len(imageIDs)-1]
// use `Run` on purpose (same below) because `rmi all` may fail on individual
// image id that has an expected running container (e.g. a registry)
base.Cmd(append([]string{"rmi", "-f"}, imageIDs...)...).Run()

base.T.Logf("Pruning build caches")
if _, err := buildkitutil.GetBuildkitHost(testutil.Namespace); err == nil {
base.Cmd("builder", "prune", "--force").AssertOK()
}

// For BuildKit >= 0.11, pruning cache isn't enough to remove manifest blobs that are referred by build history blobs
// https://github.com/containerd/nerdctl/pull/1833
if base.Target == testutil.Nerdctl {
base.T.Logf("Pruning all content blobs")
addr := base.ContainerdAddress()
client, err := containerd.New(addr, containerd.WithDefaultNamespace(testutil.Namespace))
assert.NilError(base.T, err)
cs := client.ContentStore()
ctx := context.TODO()
wf := func(info content.Info) error {
base.T.Logf("Pruning blob %+v", info)
if err := cs.Delete(ctx, info.Digest); err != nil {
base.T.Log(err)
}
return nil
}
if err := cs.Walk(ctx, wf); err != nil {
base.T.Log(err)
}

base.T.Logf("Pruning all images (again?)")
imageIDs = base.Cmd("images", "--no-trunc", "-a", "-q").OutLines()
base.T.Logf("pruning following images: %+v", imageIDs)
base.Cmd(append([]string{"rmi", "-f"}, imageIDs...)...).Run()
}
}
46 changes: 0 additions & 46 deletions cmd/nerdctl/helpers/testing_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package helpers

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand All @@ -32,10 +31,6 @@ import (

"gotest.tools/v3/assert"

containerd "github.com/containerd/containerd/v2/client"
"github.com/containerd/containerd/v2/core/content"

"github.com/containerd/nerdctl/v2/pkg/buildkitutil"
"github.com/containerd/nerdctl/v2/pkg/testutil"
"github.com/containerd/nerdctl/v2/pkg/testutil/nettestutil"
)
Expand Down Expand Up @@ -137,47 +132,6 @@ func NewCosignKeyPair(t testing.TB, path string, password string) *CosignKeyPair
}
}

func RmiAll(base *testutil.Base) {
base.T.Logf("Pruning images")
imageIDs := base.Cmd("images", "--no-trunc", "-a", "-q").OutLines()
// remove empty output line at the end
imageIDs = imageIDs[:len(imageIDs)-1]
// use `Run` on purpose (same below) because `rmi all` may fail on individual
// image id that has an expected running container (e.g. a registry)
base.Cmd(append([]string{"rmi", "-f"}, imageIDs...)...).Run()

base.T.Logf("Pruning build caches")
if _, err := buildkitutil.GetBuildkitHost(testutil.Namespace); err == nil {
base.Cmd("builder", "prune", "--force").AssertOK()
}

// For BuildKit >= 0.11, pruning cache isn't enough to remove manifest blobs that are referred by build history blobs
// https://github.com/containerd/nerdctl/pull/1833
if base.Target == testutil.Nerdctl {
base.T.Logf("Pruning all content blobs")
addr := base.ContainerdAddress()
client, err := containerd.New(addr, containerd.WithDefaultNamespace(testutil.Namespace))
assert.NilError(base.T, err)
cs := client.ContentStore()
ctx := context.TODO()
wf := func(info content.Info) error {
base.T.Logf("Pruning blob %+v", info)
if err := cs.Delete(ctx, info.Digest); err != nil {
base.T.Log(err)
}
return nil
}
if err := cs.Walk(ctx, wf); err != nil {
base.T.Log(err)
}

base.T.Logf("Pruning all images (again?)")
imageIDs = base.Cmd("images", "--no-trunc", "-a", "-q").OutLines()
base.T.Logf("pruning following images: %+v", imageIDs)
base.Cmd(append([]string{"rmi", "-f"}, imageIDs...)...).Run()
}
}

func ComposeUp(t *testing.T, base *testutil.Base, dockerComposeYAML string, opts ...string) {
comp := testutil.NewComposeDir(t, dockerComposeYAML)
defer comp.CleanUp()
Expand Down
149 changes: 103 additions & 46 deletions cmd/nerdctl/image/image_convert_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,63 +20,120 @@ import (
"fmt"
"testing"

"gotest.tools/v3/icmd"

"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
"github.com/containerd/nerdctl/v2/pkg/testutil"
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
"github.com/containerd/nerdctl/v2/pkg/testutil/test"
"github.com/containerd/nerdctl/v2/pkg/testutil/testregistry"
)

func TestImageConvertNydus(t *testing.T) {
testutil.RequireExecutable(t, "nydus-image")
testutil.DockerIncompatible(t)

base := testutil.NewBase(t)
t.Parallel()
func TestImageConvert(t *testing.T) {
nerdtest.Setup()

convertedImage := testutil.Identifier(t) + ":nydus"
base.Cmd("rmi", convertedImage).Run()
base.Cmd("pull", testutil.CommonImage).AssertOK()
base.Cmd("image", "convert", "--nydus", "--oci",
testutil.CommonImage, convertedImage).AssertOK()
defer base.Cmd("rmi", convertedImage).Run()
testCase := &test.Case{
Description: "Test image conversion",
Require: test.Require(
test.Not(test.Windows),
test.Not(nerdtest.Docker),
),
Setup: func(data test.Data, helpers test.Helpers) {
helpers.Ensure("pull", testutil.CommonImage)
},
SubTests: []*test.Case{
{
Description: "esgz",
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rmi", data.Identifier())
},
Command: func(data test.Data, helpers test.Helpers) test.Command {
return helpers.Command("image", "convert", "--oci", "--estargz", testutil.CommonImage, data.Identifier())
},
Expected: test.Expects(0, nil, nil),
},
{
Description: "nydus",
Require: test.Require(
test.Binary("nydus-image"),
),
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rmi", data.Identifier())
},
Command: func(data test.Data, helpers test.Helpers) test.Command {
return helpers.Command("image", "convert", "--oci", "--nydus", testutil.CommonImage, data.Identifier())
},
Expected: test.Expects(0, nil, nil),
},
{
Description: "zstd",
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rmi", data.Identifier())
},
Command: func(data test.Data, helpers test.Helpers) test.Command {
return helpers.Command("image", "convert", "--oci", "--zstd", "--zstd-compression-level", "3", testutil.CommonImage, data.Identifier())
},
Expected: test.Expects(0, nil, nil),
},
{
Description: "zstdchunked",
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rmi", data.Identifier())
},
Command: func(data test.Data, helpers test.Helpers) test.Command {
return helpers.Command("image", "convert", "--oci", "--zstdchunked", "--zstdchunked-compression-level", "3", testutil.CommonImage, data.Identifier())
},
Expected: test.Expects(0, nil, nil),
},
},
}

// use `nydusify` check whether the convertd nydus image is valid
testCase.Run(t)

// skip if rootless
if rootlessutil.IsRootless() {
t.Skip("Nydusify check is not supported rootless mode.")
}
}

// skip if nydusify and nydusd are not installed
testutil.RequireExecutable(t, "nydusify")
testutil.RequireExecutable(t, "nydusd")
func TestImageConvertNydusVerify(t *testing.T) {
nerdtest.Setup()

// setup local docker registry
registry := testregistry.NewWithNoAuth(base, 0, false)
remoteImage := fmt.Sprintf("%s:%d/nydusd-image:test", "localhost", registry.Port)
t.Cleanup(func() {
base.Cmd("rmi", remoteImage).Run()
registry.Cleanup(nil)
})
var registry *testregistry.RegistryServer

base.Cmd("tag", convertedImage, remoteImage).AssertOK()
base.Cmd("push", remoteImage).AssertOK()
nydusifyCmd := testutil.Cmd{
Cmd: icmd.Command(
"nydusify",
"check",
"--source",
testutil.CommonImage,
"--target",
remoteImage,
"--source-insecure",
"--target-insecure",
testCase := &test.Case{
Description: "TestImageConvertNydusVerify",
Require: test.Require(
test.Linux,
test.Binary("nydus-image"),
test.Binary("nydusify"),
test.Binary("nydusd"),
test.Not(nerdtest.Docker),
test.Not(nerdtest.Rootless),
),
Base: base,
Setup: func(data test.Data, helpers test.Helpers) {
helpers.Ensure("pull", testutil.CommonImage)
base := testutil.NewBase(t)
registry = testregistry.NewWithNoAuth(base, 80, false)
helpers.Ensure("image", "convert", "--nydus", "--oci",
testutil.CommonImage, data.Identifier())
data.Set("remoteImage", fmt.Sprintf("%s:%d/nydusd-image:test", "localhost", registry.Port))
helpers.Ensure("tag", data.Identifier(), data.Get("remoteImage"))
helpers.Ensure("push", data.Get("remoteImage"))
},
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rmi", data.Get("remoteImage"))
helpers.Anyhow("rmi", data.Identifier())
if registry != nil {
registry.Cleanup(nil)
}
},
Command: func(data test.Data, helpers test.Helpers) test.Command {
return helpers.CustomCommand("nydusify",
"check",
"--source",
testutil.CommonImage,
"--target",
data.Get("remoteImage"),
"--source-insecure",
"--target-insecure",
)
},
Expected: test.Expects(0, nil, nil),
}

// nydus is creating temporary files - make sure we are in a proper location for that
nydusifyCmd.Cmd.Dir = base.T.TempDir()
nydusifyCmd.AssertOK()
testCase.Run(t)
}
70 changes: 0 additions & 70 deletions cmd/nerdctl/image/image_convert_test.go

This file was deleted.

Loading

0 comments on commit 741bc21

Please sign in to comment.