Skip to content

Commit 49049c1

Browse files
committed
Rewrite commit tests
Signed-off-by: apostasie <[email protected]>
1 parent a4a7282 commit 49049c1

File tree

2 files changed

+112
-93
lines changed

2 files changed

+112
-93
lines changed

cmd/nerdctl/container/container_commit_linux_test.go

Lines changed: 59 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -21,76 +21,70 @@ import (
2121
"testing"
2222

2323
"github.com/containerd/nerdctl/v2/pkg/testutil"
24+
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
25+
"github.com/containerd/nerdctl/v2/pkg/testutil/test"
2426
)
2527

26-
/*
27-
This test below is meant to assert that https://github.com/containerd/nerdctl/issues/827 is NOT fixed.
28-
Obviously, once we fix the issue, it should be replaced by something that assert it works.
29-
Unfortunately, this is flaky.
30-
It will regularly succeed or fail, making random PR fail the Kube check.
31-
*/
32-
33-
func TestKubeCommitPush(t *testing.T) {
34-
t.Parallel()
35-
36-
base := testutil.NewBaseForKubernetes(t)
37-
tID := testutil.Identifier(t)
38-
39-
var containerID string
40-
// var registryIP string
41-
42-
setup := func() {
43-
testutil.KubectlHelper(base, "run", "--image", testutil.CommonImage, tID, "--", "sleep", "Inf").
44-
AssertOK()
45-
46-
testutil.KubectlHelper(base, "wait", "pod", tID, "--for=condition=ready", "--timeout=1m").
47-
AssertOK()
48-
49-
testutil.KubectlHelper(base, "exec", tID, "--", "mkdir", "-p", "/tmp/whatever").
50-
AssertOK()
51-
52-
cmd := testutil.KubectlHelper(base, "get", "pods", tID, "-o", "jsonpath={ .status.containerStatuses[0].containerID }")
53-
cmd.Run()
54-
containerID = strings.TrimPrefix(cmd.Out(), "containerd://")
55-
56-
// This below is missing configuration to allow for plain http communication
57-
// This is left here for future work to successfully start a registry usable in the cluster
58-
/*
59-
// Start a registry
60-
testutil.KubectlHelper(base, "run", "--port", "5000", "--image", testutil.RegistryImageStable, "testregistry").
61-
AssertOK()
62-
63-
testutil.KubectlHelper(base, "wait", "pod", "testregistry", "--for=condition=ready", "--timeout=1m").
64-
AssertOK()
65-
66-
cmd = testutil.KubectlHelper(base, "get", "pods", tID, "-o", "jsonpath={ .status.hostIPs[0].ip }")
67-
cmd.Run()
68-
registryIP = cmd.Out()
69-
70-
cmd = testutil.KubectlHelper(base, "apply", "-f", "-", fmt.Sprintf(`apiVersion: v1
71-
kind: ConfigMap
72-
metadata:
73-
name: local-registry
74-
namespace: nerdctl-test
75-
data:
76-
localRegistryHosting.v1: |
77-
host: "%s:5000"
78-
help: "https://kind.sigs.k8s.io/docs/user/local-registry/"
79-
`, registryIP))
80-
*/
81-
28+
func TestKubeCommitSave(t *testing.T) {
29+
testCase := nerdtest.Setup()
30+
31+
testCase.Require = nerdtest.OnlyKubernetes
32+
33+
testCase.Setup = func(data test.Data, helpers test.Helpers) {
34+
containerID := ""
35+
// NOTE: kubectl namespaces are not the same as containerd namespaces.
36+
// We still want kube test objects segregated in their own Kube API namespace.
37+
nerdtest.KubeCtlCommand(helpers, "create", "namespace", "nerdctl-test-k8s").Run(&test.Expected{})
38+
nerdtest.KubeCtlCommand(helpers, "run", "--image", testutil.CommonImage, data.Identifier(), "--", "sleep", "Inf").Run(&test.Expected{})
39+
nerdtest.KubeCtlCommand(helpers, "wait", "pod", data.Identifier(), "--for=condition=ready", "--timeout=1m").Run(&test.Expected{})
40+
nerdtest.KubeCtlCommand(helpers, "exec", data.Identifier(), "--", "mkdir", "-p", "/tmp/whatever").Run(&test.Expected{})
41+
nerdtest.KubeCtlCommand(helpers, "get", "pods", data.Identifier(), "-o", "jsonpath={ .status.containerStatuses[0].containerID }").Run(&test.Expected{
42+
Output: func(stdout string, info string, t *testing.T) {
43+
containerID = strings.TrimPrefix(stdout, "containerd://")
44+
},
45+
})
46+
data.Set("containerID", containerID)
8247
}
8348

84-
tearDown := func() {
85-
testutil.KubectlHelper(base, "delete", "pod", "--all").Run()
49+
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
50+
nerdtest.KubeCtlCommand(helpers, "delete", "pod", "--all").Run(nil)
8651
}
8752

88-
tearDown()
89-
t.Cleanup(tearDown)
90-
setup()
53+
testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand {
54+
helpers.Ensure("commit", data.Get("containerID"), "testcommitsave")
55+
return helpers.Command("save", "testcommitsave")
56+
}
9157

92-
t.Run("test commit / push on Kube (https://github.com/containerd/nerdctl/issues/827)", func(t *testing.T) {
93-
base.Cmd("commit", containerID, "testcommitsave").AssertOK()
94-
base.Cmd("save", "testcommitsave").AssertOK()
95-
})
58+
testCase.Expected = test.Expects(0, nil, nil)
59+
60+
testCase.Run(t)
61+
62+
// This below is missing configuration to allow for plain http communication
63+
// This is left here for future work to successfully start a registry usable in the cluster
64+
/*
65+
// Start a registry
66+
nerdtest.KubeCtlCommand(helpers, "run", "--port", "5000", "--image", testutil.RegistryImageStable, "testregistry").
67+
Run(&test.Expected{})
68+
69+
nerdtest.KubeCtlCommand(helpers, "wait", "pod", "testregistry", "--for=condition=ready", "--timeout=1m").
70+
AssertOK()
71+
72+
cmd = nerdtest.KubeCtlCommand(helpers, "get", "pods", tID, "-o", "jsonpath={ .status.hostIPs[0].ip }")
73+
cmd.Run(&test.Expected{
74+
Output: func(stdout string, info string, t *testing.T) {
75+
registryIP = stdout
76+
},
77+
})
78+
79+
cmd = nerdtest.KubeCtlCommand(helpers, "apply", "-f", "-", fmt.Sprintf(`apiVersion: v1
80+
kind: ConfigMap
81+
metadata:
82+
name: local-registry
83+
namespace: nerdctl-test
84+
data:
85+
localRegistryHosting.v1: |
86+
host: "%s:5000"
87+
help: "https://kind.sigs.k8s.io/docs/user/local-registry/"
88+
`, registryIP))
89+
*/
9690
}

cmd/nerdctl/container/container_commit_test.go

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,64 @@
1717
package container
1818

1919
import (
20-
"fmt"
2120
"testing"
2221

2322
"github.com/containerd/nerdctl/v2/pkg/testutil"
23+
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
24+
"github.com/containerd/nerdctl/v2/pkg/testutil/test"
2425
)
2526

2627
func TestCommit(t *testing.T) {
27-
t.Parallel()
28-
base := testutil.NewBase(t)
29-
switch base.Info().CgroupDriver {
30-
case "none", "":
31-
t.Skip("requires cgroup (for pausing)")
32-
}
33-
testContainer := testutil.Identifier(t)
34-
testImage := testutil.Identifier(t) + "-img"
35-
defer base.Cmd("rm", "-f", testContainer).Run()
36-
defer base.Cmd("rmi", testImage).Run()
37-
38-
for _, pause := range []string{
39-
"true",
40-
"false",
41-
} {
42-
base.Cmd("run", "-d", "--name", testContainer, testutil.CommonImage, "sleep", "infinity").AssertOK()
43-
base.EnsureContainerStarted(testContainer)
44-
base.Cmd("exec", testContainer, "sh", "-euxc", `echo hello-test-commit > /foo`).AssertOK()
45-
base.Cmd(
46-
"commit",
47-
"-c", `CMD ["/foo"]`,
48-
"-c", `ENTRYPOINT ["cat"]`,
49-
fmt.Sprintf("--pause=%s", pause),
50-
testContainer, testImage).AssertOK()
51-
base.Cmd("run", "--rm", testImage).AssertOutExactly("hello-test-commit\n")
52-
base.Cmd("rm", "-f", testContainer).Run()
53-
base.Cmd("rmi", testImage).Run()
28+
testCase := nerdtest.Setup()
29+
30+
testCase.SubTests = []*test.Case{
31+
{
32+
Description: "with pause",
33+
Require: nerdtest.CGroup,
34+
Cleanup: func(data test.Data, helpers test.Helpers) {
35+
helpers.Anyhow("rm", "-f", data.Identifier())
36+
helpers.Anyhow("rmi", "-f", data.Identifier())
37+
},
38+
Setup: func(data test.Data, helpers test.Helpers) {
39+
helpers.Ensure("run", "-d", "--name", data.Identifier(), testutil.CommonImage, "sleep", "infinity")
40+
nerdtest.EnsureContainerStarted(helpers, data.Identifier())
41+
helpers.Ensure("exec", data.Identifier(), "sh", "-euxc", `echo hello-test-commit > /foo`)
42+
},
43+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
44+
helpers.Ensure(
45+
"commit",
46+
"-c", `CMD ["/foo"]`,
47+
"-c", `ENTRYPOINT ["cat"]`,
48+
"--pause=true",
49+
data.Identifier(), data.Identifier())
50+
return helpers.Command("run", "--rm", data.Identifier())
51+
},
52+
Expected: test.Expects(0, nil, test.Equals("hello-test-commit\n")),
53+
},
54+
{
55+
Description: "no pause",
56+
Require: test.Not(test.Windows),
57+
Cleanup: func(data test.Data, helpers test.Helpers) {
58+
helpers.Anyhow("rm", "-f", data.Identifier())
59+
helpers.Anyhow("rmi", "-f", data.Identifier())
60+
},
61+
Setup: func(data test.Data, helpers test.Helpers) {
62+
helpers.Ensure("run", "-d", "--name", data.Identifier(), testutil.CommonImage, "sleep", "infinity")
63+
nerdtest.EnsureContainerStarted(helpers, data.Identifier())
64+
helpers.Ensure("exec", data.Identifier(), "sh", "-euxc", `echo hello-test-commit > /foo`)
65+
},
66+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
67+
helpers.Ensure(
68+
"commit",
69+
"-c", `CMD ["/foo"]`,
70+
"-c", `ENTRYPOINT ["cat"]`,
71+
"--pause=false",
72+
data.Identifier(), data.Identifier())
73+
return helpers.Command("run", "--rm", data.Identifier())
74+
},
75+
Expected: test.Expects(0, nil, test.Equals("hello-test-commit\n")),
76+
},
5477
}
78+
79+
testCase.Run(t)
5580
}

0 commit comments

Comments
 (0)