Skip to content

[WIP] [EXP] proofing #4194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions cmd/nerdctl/builder/builder_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/containerd/nerdctl/mod/tigron/test"

"github.com/containerd/nerdctl/v2/pkg/buildkitutil"
"github.com/containerd/nerdctl/v2/pkg/referenceutil"
"github.com/containerd/nerdctl/v2/pkg/testutil"
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
)
Expand Down Expand Up @@ -152,14 +153,19 @@ CMD ["echo", "nerdctl-builder-debug-test-string"]`, testutil.CommonImage)
// FIXME: this test should be rewritten to dynamically retrieve the ids, and use images
// available on all platforms
oldImage := testutil.BusyboxImage
oldImageSha := "7b3ccabffc97de872a30dfd234fd972a66d247c8cfc69b0550f276481852627c"
parsedOldImage, err := referenceutil.Parse(oldImage)
assert.NilError(helpers.T(), err)
oldImageSha := parsedOldImage.Digest.String()

newImage := testutil.AlpineImage
newImageSha := "ec14c7992a97fc11425907e908340c6c3d6ff602f5f13d899e6b7027c9b4133a"
parsedNewImage, err := referenceutil.Parse(newImage)
assert.NilError(helpers.T(), err)
newImageSha := parsedNewImage.Digest.String()

helpers.Ensure("pull", "--quiet", oldImage)
helpers.Ensure("tag", oldImage, newImage)
helpers.Ensure("tag", oldImage, parsedNewImage.Domain+"/"+parsedNewImage.Path+":"+parsedNewImage.Tag)

dockerfile := fmt.Sprintf(`FROM %s`, newImage)
dockerfile := fmt.Sprintf(`FROM %s`, parsedNewImage.Domain+"/"+parsedNewImage.Path+":"+parsedNewImage.Tag)
data.Temp().Save(dockerfile, "Dockerfile")
data.Labels().Set("oldImageSha", oldImageSha)
data.Labels().Set("newImageSha", newImageSha)
Expand Down
13 changes: 7 additions & 6 deletions cmd/nerdctl/compose/compose_images_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"
"testing"

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

Expand Down Expand Up @@ -65,16 +66,16 @@ volumes:
base.ComposeCmd("-f", comp.YAMLFullPath(), "up", "-d").AssertOK()
defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").Run()

wordpressImageName := strings.Split(testutil.WordpressImage, ":")[0]
dbImageName := strings.Split(testutil.MariaDBImage, ":")[0]
wordpressImageName, _ := referenceutil.Parse(testutil.WordpressImage)
dbImageName, _ := referenceutil.Parse(testutil.MariaDBImage)

// check one service image
base.ComposeCmd("-f", comp.YAMLFullPath(), "images", "db").AssertOutContains(dbImageName)
base.ComposeCmd("-f", comp.YAMLFullPath(), "images", "db").AssertOutNotContains(wordpressImageName)
base.ComposeCmd("-f", comp.YAMLFullPath(), "images", "db").AssertOutContains(dbImageName.Name())
base.ComposeCmd("-f", comp.YAMLFullPath(), "images", "db").AssertOutNotContains(wordpressImageName.Name())

// check all service images
base.ComposeCmd("-f", comp.YAMLFullPath(), "images").AssertOutContains(dbImageName)
base.ComposeCmd("-f", comp.YAMLFullPath(), "images").AssertOutContains(wordpressImageName)
base.ComposeCmd("-f", comp.YAMLFullPath(), "images").AssertOutContains(dbImageName.Name())
base.ComposeCmd("-f", comp.YAMLFullPath(), "images").AssertOutContains(wordpressImageName.Name())
}

func TestComposeImagesJson(t *testing.T) {
Expand Down
86 changes: 61 additions & 25 deletions cmd/nerdctl/image/image_history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,42 @@ type historyObj struct {
Comment string
}

const createdAt = "2025-02-13T19:28:36-08:00"

// Expected content of the common image on arm64
var (
createdAtTime, _ = time.Parse(time.RFC3339, createdAt)
expectedHistory = []historyObj{
{
CreatedBy: "CMD [\"/bin/sh\"]",
Size: "0B",
CreatedAt: createdAt,
Snapshot: "<missing>",
Comment: "buildkit.dockerfile.v0",
CreatedSince: formatter.TimeSinceInHuman(createdAtTime),
},
{
CreatedBy: "ADD alpine-minirootfs-3.21.3-aarch64.tar.gz …",
Size: "8.843MB",
CreatedAt: createdAt,
Snapshot: "sha256:a16e98724c05975ee8c40d8fe389c3481373d…",
Comment: "buildkit.dockerfile.v0",
CreatedSince: formatter.TimeSinceInHuman(createdAtTime),
},
}
expectedHistoryNoTrunc = []historyObj{
{
Snapshot: "<missing>",
Size: "0",
},
{
Snapshot: "sha256:a16e98724c05975ee8c40d8fe389c3481373d34ab20a1cf52ea2accc43f71f4c",
CreatedBy: "ADD alpine-minirootfs-3.21.3-aarch64.tar.gz / # buildkit",
Size: "8843264",
},
}
)

func decode(stdout string) ([]historyObj, error) {
dec := json.NewDecoder(strings.NewReader(stdout))
object := []historyObj{}
Expand Down Expand Up @@ -95,35 +131,35 @@ func TestImageHistory(t *testing.T) {
assert.NilError(t, err, info)
assert.Equal(t, len(history), 2, info)

localTimeL1, _ := time.Parse(time.RFC3339, "2021-03-31T10:21:23-07:00")
localTimeL2, _ := time.Parse(time.RFC3339, "2021-03-31T10:21:21-07:00")
compTime1, _ := time.Parse(time.RFC3339, history[0].CreatedAt)
compTime2, _ := time.Parse(time.RFC3339, history[1].CreatedAt)
assert.Equal(t, compTime1.UTC().String(), localTimeL1.UTC().String(), info)
assert.Equal(t, history[0].CreatedBy, "/bin/sh -c #(nop) CMD [\"/bin/sh\"]", info)
assert.Equal(t, compTime2.UTC().String(), localTimeL2.UTC().String(), info)
assert.Equal(t, history[1].CreatedBy, "/bin/sh -c #(nop) ADD file:3b16ffee2b26d8af5…", info)

assert.Equal(t, history[0].Size, "0B", info)
assert.Equal(t, history[0].CreatedSince, formatter.TimeSinceInHuman(compTime1), info)
assert.Equal(t, history[0].Snapshot, "<missing>", info)
assert.Equal(t, history[0].Comment, "", info)

assert.Equal(t, history[1].Size, "5.947MB", info)
assert.Equal(t, history[1].CreatedSince, formatter.TimeSinceInHuman(compTime2), info)
assert.Equal(t, history[1].Snapshot, "sha256:56bf55b8eed1f0b4794a30386e4d1d3da949c…", info)
assert.Equal(t, history[1].Comment, "", info)
h0Time, _ := time.Parse(time.RFC3339, history[0].CreatedAt)
h1Time, _ := time.Parse(time.RFC3339, history[1].CreatedAt)
comp0Time, _ := time.Parse(time.RFC3339, expectedHistory[0].CreatedAt)
comp1Time, _ := time.Parse(time.RFC3339, expectedHistory[1].CreatedAt)

assert.Equal(t, h0Time.UTC().String(), comp0Time.UTC().String(), info)
assert.Equal(t, history[0].CreatedBy, expectedHistory[0].CreatedBy, info)
assert.Equal(t, history[0].Size, expectedHistory[0].Size, info)
assert.Equal(t, history[0].CreatedSince, expectedHistory[0].CreatedSince, info)
assert.Equal(t, history[0].Snapshot, expectedHistory[0].Snapshot, info)
assert.Equal(t, history[0].Comment, expectedHistory[0].Comment, info)

assert.Equal(t, h1Time.UTC().String(), comp1Time.UTC().String(), info)
assert.Equal(t, history[1].CreatedBy, expectedHistory[1].CreatedBy, info)
assert.Equal(t, history[1].Size, expectedHistory[1].Size, info)
assert.Equal(t, history[1].CreatedSince, expectedHistory[1].CreatedSince, info)
assert.Equal(t, history[1].Snapshot, expectedHistory[1].Snapshot, info)
assert.Equal(t, history[1].Comment, expectedHistory[1].Comment, info)
}),
},
{
Description: "no human - dates and sizes and not prettyfied",
Description: "no human - dates and sizes are not prettyfied",
Command: test.Command("image", "history", "--human=false", "--format=json", testutil.CommonImage),
Expected: test.Expects(0, nil, func(stdout string, info string, t *testing.T) {
history, err := decode(stdout)
assert.NilError(t, err, info)
assert.Equal(t, history[0].Size, "0", info)
assert.Equal(t, history[0].Size, expectedHistoryNoTrunc[0].Size, info)
assert.Equal(t, history[0].CreatedSince, history[0].CreatedAt, info)
assert.Equal(t, history[1].Size, "5947392", info)
assert.Equal(t, history[1].Size, expectedHistoryNoTrunc[1].Size, info)
assert.Equal(t, history[1].CreatedSince, history[1].CreatedAt, info)
}),
},
Expand All @@ -133,22 +169,22 @@ func TestImageHistory(t *testing.T) {
Expected: test.Expects(0, nil, func(stdout string, info string, t *testing.T) {
history, err := decode(stdout)
assert.NilError(t, err, info)
assert.Equal(t, history[1].Snapshot, "sha256:56bf55b8eed1f0b4794a30386e4d1d3da949c25bcb5155e898097cd75dc77c2a")
assert.Equal(t, history[1].CreatedBy, "/bin/sh -c #(nop) ADD file:3b16ffee2b26d8af5db152fcc582aaccd9e1ec9e3343874e9969a205550fe07d in / ")
assert.Equal(t, history[1].Snapshot, expectedHistoryNoTrunc[1].Snapshot)
assert.Equal(t, history[1].CreatedBy, expectedHistoryNoTrunc[1].CreatedBy)
}),
},
{
Description: "Quiet has no effect with format, so, go no-json, no-trunc",
Command: test.Command("image", "history", "--human=false", "--no-trunc", "--quiet", testutil.CommonImage),
Expected: test.Expects(0, nil, func(stdout string, info string, t *testing.T) {
assert.Equal(t, stdout, "<missing>\nsha256:56bf55b8eed1f0b4794a30386e4d1d3da949c25bcb5155e898097cd75dc77c2a\n")
assert.Equal(t, stdout, expectedHistoryNoTrunc[0].Snapshot+"\n"+expectedHistoryNoTrunc[1].Snapshot+"\n")
}),
},
{
Description: "With quiet, trunc has no effect",
Command: test.Command("image", "history", "--human=false", "--no-trunc", "--quiet", testutil.CommonImage),
Expected: test.Expects(0, nil, func(stdout string, info string, t *testing.T) {
assert.Equal(t, stdout, "<missing>\nsha256:56bf55b8eed1f0b4794a30386e4d1d3da949c25bcb5155e898097cd75dc77c2a\n")
assert.Equal(t, stdout, expectedHistoryNoTrunc[0].Snapshot+"\n"+expectedHistoryNoTrunc[1].Snapshot+"\n")
}),
},
},
Expand Down
37 changes: 21 additions & 16 deletions cmd/nerdctl/image/image_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/containerd/nerdctl/mod/tigron/require"
"github.com/containerd/nerdctl/mod/tigron/test"

"github.com/containerd/nerdctl/v2/pkg/referenceutil"
"github.com/containerd/nerdctl/v2/pkg/tabutil"
"github.com/containerd/nerdctl/v2/pkg/testutil"
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
Expand All @@ -40,10 +41,12 @@ import (
func TestImages(t *testing.T) {
nerdtest.Setup()

commonImage, _ := referenceutil.Parse(testutil.CommonImage)

testCase := &test.Case{
Require: require.Not(nerdtest.Docker),
Setup: func(data test.Data, helpers test.Helpers) {
helpers.Ensure("pull", "--quiet", testutil.CommonImage)
helpers.Ensure("pull", "--quiet", commonImage.String())
helpers.Ensure("pull", "--quiet", testutil.NginxAlpineImage)
},
SubTests: []*test.Case{
Expand All @@ -66,7 +69,7 @@ func TestImages(t *testing.T) {
for _, line := range lines[1:] {
repo, _ := tab.ReadRow(line, "REPOSITORY")
tag, _ := tab.ReadRow(line, "TAG")
if repo+":"+tag == testutil.CommonImage {
if repo+":"+tag == commonImage.FamiliarName()+":"+commonImage.Tag {
found = true
break
}
Expand All @@ -78,11 +81,11 @@ func TestImages(t *testing.T) {
},
{
Description: "With names",
Command: test.Command("images", "--names", testutil.CommonImage),
Command: test.Command("images", "--names", commonImage.String()),
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
Output: expect.All(
expect.Contains(testutil.CommonImage),
expect.Contains(commonImage.String()),
func(stdout string, info string, t *testing.T) {
lines := strings.Split(strings.TrimSpace(stdout), "\n")
assert.Assert(t, len(lines) >= 2, info)
Expand All @@ -92,7 +95,7 @@ func TestImages(t *testing.T) {
found := false
for _, line := range lines[1:] {
name, _ := tab.ReadRow(line, "NAME")
if name == testutil.CommonImage {
if name == commonImage.String() {
found = true
break
}
Expand Down Expand Up @@ -135,19 +138,21 @@ func TestImages(t *testing.T) {
func TestImagesFilter(t *testing.T) {
nerdtest.Setup()

commonImage, _ := referenceutil.Parse(testutil.CommonImage)

testCase := &test.Case{
Require: nerdtest.Build,
Setup: func(data test.Data, helpers test.Helpers) {
helpers.Ensure("pull", "--quiet", testutil.CommonImage)
helpers.Ensure("tag", testutil.CommonImage, "taggedimage:one-fragment-one")
helpers.Ensure("tag", testutil.CommonImage, "taggedimage:two-fragment-two")
helpers.Ensure("pull", "--quiet", commonImage.String())
helpers.Ensure("tag", commonImage.String(), "taggedimage:one-fragment-one")
helpers.Ensure("tag", commonImage.String(), "taggedimage:two-fragment-two")

dockerfile := fmt.Sprintf(`FROM %s
CMD ["echo", "nerdctl-build-test-string"] \n
LABEL foo=bar
LABEL version=0.1
RUN echo "actually creating a layer so that docker sets the createdAt time"
`, testutil.CommonImage)
`, commonImage.String())
buildCtx := data.Temp().Path()
err := os.WriteFile(filepath.Join(buildCtx, "Dockerfile"), []byte(dockerfile), 0o600)
assert.NilError(helpers.T(), err)
Expand Down Expand Up @@ -237,32 +242,32 @@ RUN echo "actually creating a layer so that docker sets the createdAt time"
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
Output: expect.All(
expect.Contains(testutil.ImageRepo(testutil.CommonImage)),
expect.Contains(commonImage.FamiliarName(), commonImage.Tag),
expect.DoesNotContain(data.Labels().Get("builtImageID")),
),
}
},
},
{
Description: "since=" + testutil.CommonImage,
Command: test.Command("images", "--filter", fmt.Sprintf("since=%s", testutil.CommonImage)),
Description: "since=" + commonImage.String(),
Command: test.Command("images", "--filter", fmt.Sprintf("since=%s", commonImage.String())),
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
Output: expect.All(
expect.Contains(data.Labels().Get("builtImageID")),
expect.DoesNotContain(testutil.ImageRepo(testutil.CommonImage)),
expect.DoesNotContain(commonImage.Tag),
),
}
},
},
{
Description: "since=" + testutil.CommonImage + " " + testutil.CommonImage,
Command: test.Command("images", "--filter", fmt.Sprintf("since=%s", testutil.CommonImage), testutil.CommonImage),
Description: "since=" + commonImage.String() + " " + commonImage.String(),
Command: test.Command("images", "--filter", fmt.Sprintf("since=%s", commonImage.String()), commonImage.String()),
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
Output: expect.DoesNotContain(
data.Labels().Get("builtImageID"),
testutil.ImageRepo(testutil.CommonImage),
commonImage.Tag,
),
}
},
Expand Down
4 changes: 3 additions & 1 deletion cmd/nerdctl/image/image_pull_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/containerd/nerdctl/mod/tigron/require"
"github.com/containerd/nerdctl/mod/tigron/test"

"github.com/containerd/nerdctl/v2/pkg/referenceutil"
"github.com/containerd/nerdctl/v2/pkg/testutil"
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest/registry"
Expand Down Expand Up @@ -113,6 +114,7 @@ func TestImagePullPlainHttpWithDefaultPort(t *testing.T) {
nerdtest.Setup()

var reg *registry.Server
im, _ := referenceutil.Parse(testutil.CommonImage)
dockerfile := fmt.Sprintf(`FROM %s
CMD ["echo", "nerdctl-build-test-string"]
`, testutil.CommonImage)
Expand All @@ -130,7 +132,7 @@ CMD ["echo", "nerdctl-build-test-string"]
reg = nerdtest.RegistryWithNoAuth(data, helpers, 80, false)
reg.Setup(data, helpers)
testImageRef := fmt.Sprintf("%s/%s:%s",
reg.IP.String(), data.Identifier(), strings.Split(testutil.CommonImage, ":")[1])
reg.IP.String(), data.Identifier(), im.Tag)
buildCtx := data.Temp().Path()

helpers.Ensure("build", "-t", testImageRef, buildCtx)
Expand Down
Loading
Loading