Skip to content

Commit ff7ee3e

Browse files
committed
checkpoint: add unit tests for checkpoint create command
add unit tests for checkpoint create command. Signed-off-by: ChengyuZhu6 <[email protected]>
1 parent 747001f commit ff7ee3e

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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/test"
25+
26+
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
27+
"github.com/containerd/nerdctl/v2/pkg/testutil"
28+
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
29+
)
30+
31+
func TestCheckpointCreateErrors(t *testing.T) {
32+
testCase := nerdtest.Setup()
33+
testCase.SubTests = []*test.Case{
34+
{
35+
Description: "too-few-arguments",
36+
Command: test.Command("checkpoint", "create", "too-few-arguments"),
37+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
38+
return &test.Expected{
39+
ExitCode: 1,
40+
}
41+
},
42+
},
43+
{
44+
Description: "too-many-arguments",
45+
Command: test.Command("checkpoint", "create", "too", "many", "arguments"),
46+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
47+
return &test.Expected{
48+
ExitCode: 1,
49+
}
50+
},
51+
},
52+
{
53+
Description: "invalid-container-id",
54+
Command: test.Command("checkpoint", "create", "foo", "bar"),
55+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
56+
return &test.Expected{
57+
ExitCode: 1,
58+
Errors: []error{errors.New("error creating checkpoint for container: foo")},
59+
}
60+
},
61+
},
62+
}
63+
64+
testCase.Run(t)
65+
}
66+
67+
func TestCheckpointCreate(t *testing.T) {
68+
const (
69+
checkpointName = "checkpoint-bar"
70+
checkpointDir = "/dir/foo"
71+
)
72+
testCase := nerdtest.Setup()
73+
if rootlessutil.IsRootless() {
74+
t.Skip("test skipped for rootless containers")
75+
}
76+
77+
testCase.SubTests = []*test.Case{
78+
{
79+
Description: "leave-running=true",
80+
Setup: func(data test.Data, helpers test.Helpers) {
81+
helpers.Ensure("run", "-d", "--name", data.Identifier("container-running"), testutil.CommonImage, "sleep", "infinity")
82+
},
83+
Cleanup: func(data test.Data, helpers test.Helpers) {
84+
helpers.Anyhow("rm", "-f", data.Identifier("container-running"))
85+
},
86+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
87+
return helpers.Command("checkpoint", "create", "--leave-running", "--checkpoint-dir", checkpointDir, data.Identifier("container-running"), checkpointName+"running")
88+
},
89+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
90+
return &test.Expected{
91+
ExitCode: 0,
92+
Output: expect.Equals(checkpointName + "running\n"),
93+
}
94+
},
95+
},
96+
{
97+
Description: "leave-running=false",
98+
Setup: func(data test.Data, helpers test.Helpers) {
99+
helpers.Ensure("run", "-d", "--name", data.Identifier("container-exit"), testutil.CommonImage, "sleep", "infinity")
100+
},
101+
Cleanup: func(data test.Data, helpers test.Helpers) {
102+
helpers.Anyhow("rm", "-f", data.Identifier("container-exit"))
103+
},
104+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
105+
return helpers.Command("checkpoint", "create", "--checkpoint-dir", checkpointDir, data.Identifier("container-exit"), checkpointName+"exit")
106+
},
107+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
108+
return &test.Expected{
109+
ExitCode: 0,
110+
Output: expect.Equals(checkpointName + "exit\n"),
111+
}
112+
},
113+
},
114+
}
115+
116+
testCase.Run(t)
117+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
"testing"
21+
22+
"github.com/containerd/nerdctl/v2/pkg/testutil"
23+
)
24+
25+
func TestMain(m *testing.M) {
26+
testutil.M(m)
27+
}

0 commit comments

Comments
 (0)