Skip to content

Commit cf14215

Browse files
committed
checkpoint: add unit test for checkpoint rm
add unit test for checkpoint rm Signed-off-by: ChengyuZhu6 <[email protected]>
1 parent 447d4cc commit cf14215

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package checkpoint
18+
19+
import (
20+
"errors"
21+
"testing"
22+
23+
"github.com/containerd/nerdctl/mod/tigron/expect"
24+
"github.com/containerd/nerdctl/mod/tigron/require"
25+
"github.com/containerd/nerdctl/mod/tigron/test"
26+
27+
"github.com/containerd/nerdctl/v2/pkg/testutil"
28+
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
29+
)
30+
31+
func TestCheckpointRemoveErrors(t *testing.T) {
32+
testCase := nerdtest.Setup()
33+
34+
testCase.Require = require.All(
35+
require.Not(nerdtest.Rootless),
36+
// Docker version 28.x has a known regression that breaks Checkpoint/Restore functionality.
37+
// The issue is tracked in the moby/moby project as https://github.com/moby/moby/issues/50750.
38+
require.Not(nerdtest.Docker),
39+
)
40+
testCase.SubTests = []*test.Case{
41+
{
42+
Description: "too-few-arguments",
43+
Command: test.Command("checkpoint", "rm", "too-few-arguments"),
44+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
45+
return &test.Expected{
46+
ExitCode: 1,
47+
}
48+
},
49+
},
50+
{
51+
Description: "too-many-arguments",
52+
Command: test.Command("checkpoint", "rm", "too", "many", "arguments"),
53+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
54+
return &test.Expected{
55+
ExitCode: 1,
56+
}
57+
},
58+
},
59+
{
60+
Description: "invalid-container-id",
61+
Command: test.Command("checkpoint", "rm", "foo", "bar"),
62+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
63+
return &test.Expected{
64+
ExitCode: 1,
65+
Errors: []error{errors.New("error removing checkpoint for container: foo")},
66+
}
67+
},
68+
},
69+
}
70+
71+
testCase.Run(t)
72+
}
73+
74+
func TestCheckpointRemove(t *testing.T) {
75+
const (
76+
checkpointName = "checkpoint-remove"
77+
checkpointDir = "/dir/remove"
78+
)
79+
testCase := nerdtest.Setup()
80+
testCase.Require = require.All(
81+
require.Not(nerdtest.Rootless),
82+
// Docker version 28.x has a known regression that breaks Checkpoint/Restore functionality.
83+
// The issue is tracked in the moby/moby project as https://github.com/moby/moby/issues/50750.
84+
require.Not(nerdtest.Docker),
85+
)
86+
testCase.SubTests = []*test.Case{
87+
{
88+
Description: "remove-existing",
89+
Setup: func(data test.Data, helpers test.Helpers) {
90+
helpers.Ensure("run", "-d", "--name", data.Identifier("container-running-remove"), testutil.CommonImage, "sleep", "infinity")
91+
helpers.Ensure("checkpoint", "create", "--checkpoint-dir", checkpointDir, data.Identifier("container-running-remove"), checkpointName)
92+
},
93+
Cleanup: func(data test.Data, helpers test.Helpers) {
94+
helpers.Anyhow("rm", "-f", data.Identifier("container-running-remove"))
95+
helpers.Anyhow("rmi", "-f", testutil.CommonImage)
96+
},
97+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
98+
return helpers.Command("checkpoint", "rm", "--checkpoint-dir", checkpointDir, data.Identifier("container-running-remove"), checkpointName)
99+
},
100+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
101+
return &test.Expected{
102+
ExitCode: 0,
103+
Output: expect.Equals(""),
104+
}
105+
},
106+
},
107+
{
108+
Description: "remove-nonexistent-checkpoint",
109+
Setup: func(data test.Data, helpers test.Helpers) {
110+
helpers.Ensure("run", "-d", "--name", data.Identifier("container-clean-remove"), testutil.CommonImage, "sleep", "infinity")
111+
},
112+
Cleanup: func(data test.Data, helpers test.Helpers) {
113+
helpers.Anyhow("rm", "-f", data.Identifier("container-clean-remove"))
114+
helpers.Anyhow("rmi", "-f", testutil.CommonImage)
115+
},
116+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
117+
return helpers.Command("checkpoint", "rm", "--checkpoint-dir", checkpointDir, data.Identifier("container-clean-remove"), checkpointName)
118+
},
119+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
120+
return &test.Expected{
121+
ExitCode: 1,
122+
Errors: []error{errors.New("checkpoint " + checkpointName + " does not exist for container")},
123+
}
124+
},
125+
},
126+
}
127+
128+
testCase.Run(t)
129+
}

0 commit comments

Comments
 (0)